xref: /openbmc/linux/sound/soc/soc-pcm.c (revision 0be429f9fcfccfbbbcdb40e154a5c2328da17ca9)
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 
40*0be429f9SKuninori Morimoto static void soc_rtd_shutdown(struct snd_soc_pcm_runtime *rtd,
41*0be429f9SKuninori Morimoto 			     struct snd_pcm_substream *substream)
42*0be429f9SKuninori Morimoto {
43*0be429f9SKuninori Morimoto 	if (rtd->dai_link->ops &&
44*0be429f9SKuninori Morimoto 	    rtd->dai_link->ops->shutdown)
45*0be429f9SKuninori Morimoto 		rtd->dai_link->ops->shutdown(substream);
46*0be429f9SKuninori Morimoto }
47*0be429f9SKuninori Morimoto 
4890996f43SLars-Peter Clausen /**
4924894b76SLars-Peter Clausen  * snd_soc_runtime_activate() - Increment active count for PCM runtime components
5024894b76SLars-Peter Clausen  * @rtd: ASoC PCM runtime that is activated
5124894b76SLars-Peter Clausen  * @stream: Direction of the PCM stream
5224894b76SLars-Peter Clausen  *
5324894b76SLars-Peter Clausen  * Increments the active count for all the DAIs and components attached to a PCM
5424894b76SLars-Peter Clausen  * runtime. Should typically be called when a stream is opened.
5524894b76SLars-Peter Clausen  *
5672b745e3SPeter Ujfalusi  * Must be called with the rtd->card->pcm_mutex being held
5724894b76SLars-Peter Clausen  */
5824894b76SLars-Peter Clausen void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
5924894b76SLars-Peter Clausen {
6024894b76SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
610b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
622e5894d7SBenoit Cousson 	int i;
6324894b76SLars-Peter Clausen 
6472b745e3SPeter Ujfalusi 	lockdep_assert_held(&rtd->card->pcm_mutex);
6524894b76SLars-Peter Clausen 
6624894b76SLars-Peter Clausen 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
6724894b76SLars-Peter Clausen 		cpu_dai->playback_active++;
680b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai)
690b7990e3SKuninori Morimoto 			codec_dai->playback_active++;
7024894b76SLars-Peter Clausen 	} else {
7124894b76SLars-Peter Clausen 		cpu_dai->capture_active++;
720b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai)
730b7990e3SKuninori Morimoto 			codec_dai->capture_active++;
7424894b76SLars-Peter Clausen 	}
7524894b76SLars-Peter Clausen 
7624894b76SLars-Peter Clausen 	cpu_dai->active++;
77cdde4ccbSLars-Peter Clausen 	cpu_dai->component->active++;
780b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
790b7990e3SKuninori Morimoto 		codec_dai->active++;
800b7990e3SKuninori Morimoto 		codec_dai->component->active++;
812e5894d7SBenoit Cousson 	}
8224894b76SLars-Peter Clausen }
8324894b76SLars-Peter Clausen 
8424894b76SLars-Peter Clausen /**
8524894b76SLars-Peter Clausen  * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
8624894b76SLars-Peter Clausen  * @rtd: ASoC PCM runtime that is deactivated
8724894b76SLars-Peter Clausen  * @stream: Direction of the PCM stream
8824894b76SLars-Peter Clausen  *
8924894b76SLars-Peter Clausen  * Decrements the active count for all the DAIs and components attached to a PCM
9024894b76SLars-Peter Clausen  * runtime. Should typically be called when a stream is closed.
9124894b76SLars-Peter Clausen  *
9272b745e3SPeter Ujfalusi  * Must be called with the rtd->card->pcm_mutex being held
9324894b76SLars-Peter Clausen  */
9424894b76SLars-Peter Clausen void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
9524894b76SLars-Peter Clausen {
9624894b76SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
970b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
982e5894d7SBenoit Cousson 	int i;
9924894b76SLars-Peter Clausen 
10072b745e3SPeter Ujfalusi 	lockdep_assert_held(&rtd->card->pcm_mutex);
10124894b76SLars-Peter Clausen 
10224894b76SLars-Peter Clausen 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
10324894b76SLars-Peter Clausen 		cpu_dai->playback_active--;
1040b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai)
1050b7990e3SKuninori Morimoto 			codec_dai->playback_active--;
10624894b76SLars-Peter Clausen 	} else {
10724894b76SLars-Peter Clausen 		cpu_dai->capture_active--;
1080b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai)
1090b7990e3SKuninori Morimoto 			codec_dai->capture_active--;
11024894b76SLars-Peter Clausen 	}
11124894b76SLars-Peter Clausen 
11224894b76SLars-Peter Clausen 	cpu_dai->active--;
113cdde4ccbSLars-Peter Clausen 	cpu_dai->component->active--;
1140b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1150b7990e3SKuninori Morimoto 		codec_dai->component->active--;
1160b7990e3SKuninori Morimoto 		codec_dai->active--;
1172e5894d7SBenoit Cousson 	}
11824894b76SLars-Peter Clausen }
11924894b76SLars-Peter Clausen 
12024894b76SLars-Peter Clausen /**
121208a1589SLars-Peter Clausen  * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
122208a1589SLars-Peter Clausen  * @rtd: The ASoC PCM runtime that should be checked.
123208a1589SLars-Peter Clausen  *
124208a1589SLars-Peter Clausen  * This function checks whether the power down delay should be ignored for a
125208a1589SLars-Peter Clausen  * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
126208a1589SLars-Peter Clausen  * been configured to ignore the delay, or if none of the components benefits
127208a1589SLars-Peter Clausen  * from having the delay.
128208a1589SLars-Peter Clausen  */
129208a1589SLars-Peter Clausen bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
130208a1589SLars-Peter Clausen {
131fbb16563SKuninori Morimoto 	struct snd_soc_component *component;
1322e5894d7SBenoit Cousson 	bool ignore = true;
133613fb500SKuninori Morimoto 	int i;
1342e5894d7SBenoit Cousson 
135208a1589SLars-Peter Clausen 	if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
136208a1589SLars-Peter Clausen 		return true;
137208a1589SLars-Peter Clausen 
138613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
13972c38184SKuninori Morimoto 		ignore &= !component->driver->use_pmdown_time;
140fbb16563SKuninori Morimoto 
141fbb16563SKuninori Morimoto 	return ignore;
142208a1589SLars-Peter Clausen }
143208a1589SLars-Peter Clausen 
144208a1589SLars-Peter Clausen /**
14590996f43SLars-Peter Clausen  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
14690996f43SLars-Peter Clausen  * @substream: the pcm substream
14790996f43SLars-Peter Clausen  * @hw: the hardware parameters
14890996f43SLars-Peter Clausen  *
14990996f43SLars-Peter Clausen  * Sets the substream runtime hardware parameters.
15090996f43SLars-Peter Clausen  */
15190996f43SLars-Peter Clausen int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
15290996f43SLars-Peter Clausen 	const struct snd_pcm_hardware *hw)
15390996f43SLars-Peter Clausen {
15490996f43SLars-Peter Clausen 	struct snd_pcm_runtime *runtime = substream->runtime;
15590996f43SLars-Peter Clausen 	runtime->hw.info = hw->info;
15690996f43SLars-Peter Clausen 	runtime->hw.formats = hw->formats;
15790996f43SLars-Peter Clausen 	runtime->hw.period_bytes_min = hw->period_bytes_min;
15890996f43SLars-Peter Clausen 	runtime->hw.period_bytes_max = hw->period_bytes_max;
15990996f43SLars-Peter Clausen 	runtime->hw.periods_min = hw->periods_min;
16090996f43SLars-Peter Clausen 	runtime->hw.periods_max = hw->periods_max;
16190996f43SLars-Peter Clausen 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
16290996f43SLars-Peter Clausen 	runtime->hw.fifo_size = hw->fifo_size;
16390996f43SLars-Peter Clausen 	return 0;
16490996f43SLars-Peter Clausen }
16590996f43SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
16690996f43SLars-Peter Clausen 
16701d7584cSLiam Girdwood /* DPCM stream event, send event to FE and all active BEs. */
16823607025SLiam Girdwood int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
16901d7584cSLiam Girdwood 	int event)
17001d7584cSLiam Girdwood {
17101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
17201d7584cSLiam Girdwood 
1738d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, dir, dpcm) {
17401d7584cSLiam Girdwood 
17501d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
17601d7584cSLiam Girdwood 
177103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
17801d7584cSLiam Girdwood 				be->dai_link->name, event, dir);
17901d7584cSLiam Girdwood 
180b1cd2e34SBanajit Goswami 		if ((event == SND_SOC_DAPM_STREAM_STOP) &&
181b1cd2e34SBanajit Goswami 		    (be->dpcm[dir].users >= 1))
182b1cd2e34SBanajit Goswami 			continue;
183b1cd2e34SBanajit Goswami 
18401d7584cSLiam Girdwood 		snd_soc_dapm_stream_event(be, dir, event);
18501d7584cSLiam Girdwood 	}
18601d7584cSLiam Girdwood 
18701d7584cSLiam Girdwood 	snd_soc_dapm_stream_event(fe, dir, event);
18801d7584cSLiam Girdwood 
18901d7584cSLiam Girdwood 	return 0;
19001d7584cSLiam Girdwood }
19101d7584cSLiam Girdwood 
19217841020SDong Aisheng static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
19317841020SDong Aisheng 					struct snd_soc_dai *soc_dai)
194ddee627cSLiam Girdwood {
195ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
196ddee627cSLiam Girdwood 	int ret;
197ddee627cSLiam Girdwood 
1983635bf09SNicolin Chen 	if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
1993635bf09SNicolin Chen 				rtd->dai_link->symmetric_rates)) {
2003635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
2013635bf09SNicolin Chen 				soc_dai->rate);
202ddee627cSLiam Girdwood 
2034dcdd43bSLars-Peter Clausen 		ret = snd_pcm_hw_constraint_single(substream->runtime,
204ddee627cSLiam Girdwood 						SNDRV_PCM_HW_PARAM_RATE,
2054dcdd43bSLars-Peter Clausen 						soc_dai->rate);
206ddee627cSLiam Girdwood 		if (ret < 0) {
20717841020SDong Aisheng 			dev_err(soc_dai->dev,
2083635bf09SNicolin Chen 				"ASoC: Unable to apply rate constraint: %d\n",
209103d84a3SLiam Girdwood 				ret);
210ddee627cSLiam Girdwood 			return ret;
211ddee627cSLiam Girdwood 		}
2123635bf09SNicolin Chen 	}
2133635bf09SNicolin Chen 
2143635bf09SNicolin Chen 	if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
2153635bf09SNicolin Chen 				rtd->dai_link->symmetric_channels)) {
2163635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
2173635bf09SNicolin Chen 				soc_dai->channels);
2183635bf09SNicolin Chen 
2194dcdd43bSLars-Peter Clausen 		ret = snd_pcm_hw_constraint_single(substream->runtime,
2203635bf09SNicolin Chen 						SNDRV_PCM_HW_PARAM_CHANNELS,
2213635bf09SNicolin Chen 						soc_dai->channels);
2223635bf09SNicolin Chen 		if (ret < 0) {
2233635bf09SNicolin Chen 			dev_err(soc_dai->dev,
2243635bf09SNicolin Chen 				"ASoC: Unable to apply channel symmetry constraint: %d\n",
2253635bf09SNicolin Chen 				ret);
2263635bf09SNicolin Chen 			return ret;
2273635bf09SNicolin Chen 		}
2283635bf09SNicolin Chen 	}
2293635bf09SNicolin Chen 
2303635bf09SNicolin Chen 	if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
2313635bf09SNicolin Chen 				rtd->dai_link->symmetric_samplebits)) {
2323635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
2333635bf09SNicolin Chen 				soc_dai->sample_bits);
2343635bf09SNicolin Chen 
2354dcdd43bSLars-Peter Clausen 		ret = snd_pcm_hw_constraint_single(substream->runtime,
2363635bf09SNicolin Chen 						SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2373635bf09SNicolin Chen 						soc_dai->sample_bits);
2383635bf09SNicolin Chen 		if (ret < 0) {
2393635bf09SNicolin Chen 			dev_err(soc_dai->dev,
2403635bf09SNicolin Chen 				"ASoC: Unable to apply sample bits symmetry constraint: %d\n",
2413635bf09SNicolin Chen 				ret);
2423635bf09SNicolin Chen 			return ret;
2433635bf09SNicolin Chen 		}
2443635bf09SNicolin Chen 	}
245ddee627cSLiam Girdwood 
246ddee627cSLiam Girdwood 	return 0;
247ddee627cSLiam Girdwood }
248ddee627cSLiam Girdwood 
2493635bf09SNicolin Chen static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
2503635bf09SNicolin Chen 				struct snd_pcm_hw_params *params)
2513635bf09SNicolin Chen {
2523635bf09SNicolin Chen 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
2533635bf09SNicolin Chen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2540b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
2552e5894d7SBenoit Cousson 	unsigned int rate, channels, sample_bits, symmetry, i;
2563635bf09SNicolin Chen 
2573635bf09SNicolin Chen 	rate = params_rate(params);
2583635bf09SNicolin Chen 	channels = params_channels(params);
2593635bf09SNicolin Chen 	sample_bits = snd_pcm_format_physical_width(params_format(params));
2603635bf09SNicolin Chen 
2613635bf09SNicolin Chen 	/* reject unmatched parameters when applying symmetry */
2623635bf09SNicolin Chen 	symmetry = cpu_dai->driver->symmetric_rates ||
2633635bf09SNicolin Chen 		rtd->dai_link->symmetric_rates;
2642e5894d7SBenoit Cousson 
2650b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
2660b7990e3SKuninori Morimoto 		symmetry |= codec_dai->driver->symmetric_rates;
2672e5894d7SBenoit Cousson 
2683635bf09SNicolin Chen 	if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
2693635bf09SNicolin Chen 		dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
2703635bf09SNicolin Chen 				cpu_dai->rate, rate);
2713635bf09SNicolin Chen 		return -EINVAL;
2723635bf09SNicolin Chen 	}
2733635bf09SNicolin Chen 
2743635bf09SNicolin Chen 	symmetry = cpu_dai->driver->symmetric_channels ||
2753635bf09SNicolin Chen 		rtd->dai_link->symmetric_channels;
2762e5894d7SBenoit Cousson 
2770b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
2780b7990e3SKuninori Morimoto 		symmetry |= codec_dai->driver->symmetric_channels;
2792e5894d7SBenoit Cousson 
2803635bf09SNicolin Chen 	if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
2813635bf09SNicolin Chen 		dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
2823635bf09SNicolin Chen 				cpu_dai->channels, channels);
2833635bf09SNicolin Chen 		return -EINVAL;
2843635bf09SNicolin Chen 	}
2853635bf09SNicolin Chen 
2863635bf09SNicolin Chen 	symmetry = cpu_dai->driver->symmetric_samplebits ||
2873635bf09SNicolin Chen 		rtd->dai_link->symmetric_samplebits;
2882e5894d7SBenoit Cousson 
2890b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
2900b7990e3SKuninori Morimoto 		symmetry |= codec_dai->driver->symmetric_samplebits;
2912e5894d7SBenoit Cousson 
2923635bf09SNicolin Chen 	if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
2933635bf09SNicolin Chen 		dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
2943635bf09SNicolin Chen 				cpu_dai->sample_bits, sample_bits);
2953635bf09SNicolin Chen 		return -EINVAL;
2963635bf09SNicolin Chen 	}
297ddee627cSLiam Girdwood 
298ddee627cSLiam Girdwood 	return 0;
299ddee627cSLiam Girdwood }
300ddee627cSLiam Girdwood 
30162e5f676SLars-Peter Clausen static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
30262e5f676SLars-Peter Clausen {
30362e5f676SLars-Peter Clausen 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
30462e5f676SLars-Peter Clausen 	struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
30562e5f676SLars-Peter Clausen 	struct snd_soc_dai_link *link = rtd->dai_link;
3060b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
3072e5894d7SBenoit Cousson 	unsigned int symmetry, i;
30862e5f676SLars-Peter Clausen 
3092e5894d7SBenoit Cousson 	symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
3102e5894d7SBenoit Cousson 		cpu_driver->symmetric_channels || link->symmetric_channels ||
3112e5894d7SBenoit Cousson 		cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
3122e5894d7SBenoit Cousson 
3130b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
3142e5894d7SBenoit Cousson 		symmetry = symmetry ||
3150b7990e3SKuninori Morimoto 			codec_dai->driver->symmetric_rates ||
3160b7990e3SKuninori Morimoto 			codec_dai->driver->symmetric_channels ||
3170b7990e3SKuninori Morimoto 			codec_dai->driver->symmetric_samplebits;
3182e5894d7SBenoit Cousson 
3192e5894d7SBenoit Cousson 	return symmetry;
32062e5f676SLars-Peter Clausen }
32162e5f676SLars-Peter Clausen 
3222e5894d7SBenoit Cousson static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
32358ba9b25SMark Brown {
3242e5894d7SBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
325c6068d3aSTakashi Iwai 	int ret;
32658ba9b25SMark Brown 
32758ba9b25SMark Brown 	if (!bits)
32858ba9b25SMark Brown 		return;
32958ba9b25SMark Brown 
3300e2a3751SLars-Peter Clausen 	ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
33158ba9b25SMark Brown 	if (ret != 0)
3320e2a3751SLars-Peter Clausen 		dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
3330e2a3751SLars-Peter Clausen 				 bits, ret);
33458ba9b25SMark Brown }
33558ba9b25SMark Brown 
336c8dd1fecSBenoit Cousson static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
337bd477c31SLars-Peter Clausen {
338c8dd1fecSBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
339c8dd1fecSBenoit Cousson 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3402e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
3412e5894d7SBenoit Cousson 	int i;
342c8dd1fecSBenoit Cousson 	unsigned int bits = 0, cpu_bits;
343c8dd1fecSBenoit Cousson 
344c8dd1fecSBenoit Cousson 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3450b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
3462e5894d7SBenoit Cousson 			if (codec_dai->driver->playback.sig_bits == 0) {
3472e5894d7SBenoit Cousson 				bits = 0;
3482e5894d7SBenoit Cousson 				break;
3492e5894d7SBenoit Cousson 			}
3502e5894d7SBenoit Cousson 			bits = max(codec_dai->driver->playback.sig_bits, bits);
3512e5894d7SBenoit Cousson 		}
352c8dd1fecSBenoit Cousson 		cpu_bits = cpu_dai->driver->playback.sig_bits;
353c8dd1fecSBenoit Cousson 	} else {
3540b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
3555e63dfccSDaniel Mack 			if (codec_dai->driver->capture.sig_bits == 0) {
3562e5894d7SBenoit Cousson 				bits = 0;
3572e5894d7SBenoit Cousson 				break;
3582e5894d7SBenoit Cousson 			}
3592e5894d7SBenoit Cousson 			bits = max(codec_dai->driver->capture.sig_bits, bits);
3602e5894d7SBenoit Cousson 		}
361c8dd1fecSBenoit Cousson 		cpu_bits = cpu_dai->driver->capture.sig_bits;
362c8dd1fecSBenoit Cousson 	}
363c8dd1fecSBenoit Cousson 
3642e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, bits);
3652e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, cpu_bits);
366c8dd1fecSBenoit Cousson }
367c8dd1fecSBenoit Cousson 
3682e5894d7SBenoit Cousson static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
369bd477c31SLars-Peter Clausen {
3702e5894d7SBenoit Cousson 	struct snd_pcm_runtime *runtime = substream->runtime;
37178e45c99SLars-Peter Clausen 	struct snd_pcm_hardware *hw = &runtime->hw;
3722e5894d7SBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
3730b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
3742e5894d7SBenoit Cousson 	struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
3752e5894d7SBenoit Cousson 	struct snd_soc_dai_driver *codec_dai_drv;
3762e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *codec_stream;
3772e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *cpu_stream;
3782e5894d7SBenoit Cousson 	unsigned int chan_min = 0, chan_max = UINT_MAX;
3792e5894d7SBenoit Cousson 	unsigned int rate_min = 0, rate_max = UINT_MAX;
3802e5894d7SBenoit Cousson 	unsigned int rates = UINT_MAX;
3812e5894d7SBenoit Cousson 	u64 formats = ULLONG_MAX;
3822e5894d7SBenoit Cousson 	int i;
38378e45c99SLars-Peter Clausen 
3842e5894d7SBenoit Cousson 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3852e5894d7SBenoit Cousson 		cpu_stream = &cpu_dai_drv->playback;
38616d7ea91SLars-Peter Clausen 	else
3872e5894d7SBenoit Cousson 		cpu_stream = &cpu_dai_drv->capture;
38878e45c99SLars-Peter Clausen 
3892e5894d7SBenoit Cousson 	/* first calculate min/max only for CODECs in the DAI link */
3900b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
391cde79035SRicard Wanderlof 
392cde79035SRicard Wanderlof 		/*
393cde79035SRicard Wanderlof 		 * Skip CODECs which don't support the current stream type.
394cde79035SRicard Wanderlof 		 * Otherwise, since the rate, channel, and format values will
395cde79035SRicard Wanderlof 		 * zero in that case, we would have no usable settings left,
396cde79035SRicard Wanderlof 		 * causing the resulting setup to fail.
397cde79035SRicard Wanderlof 		 * At least one CODEC should match, otherwise we should have
398cde79035SRicard Wanderlof 		 * bailed out on a higher level, since there would be no
399cde79035SRicard Wanderlof 		 * CODEC to support the transfer direction in that case.
400cde79035SRicard Wanderlof 		 */
4010b7990e3SKuninori Morimoto 		if (!snd_soc_dai_stream_valid(codec_dai,
402cde79035SRicard Wanderlof 					      substream->stream))
403cde79035SRicard Wanderlof 			continue;
404cde79035SRicard Wanderlof 
4050b7990e3SKuninori Morimoto 		codec_dai_drv = codec_dai->driver;
4062e5894d7SBenoit Cousson 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4072e5894d7SBenoit Cousson 			codec_stream = &codec_dai_drv->playback;
4082e5894d7SBenoit Cousson 		else
4092e5894d7SBenoit Cousson 			codec_stream = &codec_dai_drv->capture;
4102e5894d7SBenoit Cousson 		chan_min = max(chan_min, codec_stream->channels_min);
4112e5894d7SBenoit Cousson 		chan_max = min(chan_max, codec_stream->channels_max);
4122e5894d7SBenoit Cousson 		rate_min = max(rate_min, codec_stream->rate_min);
4132e5894d7SBenoit Cousson 		rate_max = min_not_zero(rate_max, codec_stream->rate_max);
4142e5894d7SBenoit Cousson 		formats &= codec_stream->formats;
4152e5894d7SBenoit Cousson 		rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
4162e5894d7SBenoit Cousson 	}
4172e5894d7SBenoit Cousson 
4182e5894d7SBenoit Cousson 	/*
4192e5894d7SBenoit Cousson 	 * chan min/max cannot be enforced if there are multiple CODEC DAIs
4202e5894d7SBenoit Cousson 	 * connected to a single CPU DAI, use CPU DAI's directly and let
4212e5894d7SBenoit Cousson 	 * channel allocation be fixed up later
4222e5894d7SBenoit Cousson 	 */
4232e5894d7SBenoit Cousson 	if (rtd->num_codecs > 1) {
4242e5894d7SBenoit Cousson 		chan_min = cpu_stream->channels_min;
4252e5894d7SBenoit Cousson 		chan_max = cpu_stream->channels_max;
4262e5894d7SBenoit Cousson 	}
4272e5894d7SBenoit Cousson 
4282e5894d7SBenoit Cousson 	hw->channels_min = max(chan_min, cpu_stream->channels_min);
4292e5894d7SBenoit Cousson 	hw->channels_max = min(chan_max, cpu_stream->channels_max);
4302e5894d7SBenoit Cousson 	if (hw->formats)
4312e5894d7SBenoit Cousson 		hw->formats &= formats & cpu_stream->formats;
4322e5894d7SBenoit Cousson 	else
4332e5894d7SBenoit Cousson 		hw->formats = formats & cpu_stream->formats;
4342e5894d7SBenoit Cousson 	hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
43578e45c99SLars-Peter Clausen 
43678e45c99SLars-Peter Clausen 	snd_pcm_limit_hw_rates(runtime);
43778e45c99SLars-Peter Clausen 
43878e45c99SLars-Peter Clausen 	hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
4392e5894d7SBenoit Cousson 	hw->rate_min = max(hw->rate_min, rate_min);
44078e45c99SLars-Peter Clausen 	hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
4412e5894d7SBenoit Cousson 	hw->rate_max = min_not_zero(hw->rate_max, rate_max);
442bd477c31SLars-Peter Clausen }
443bd477c31SLars-Peter Clausen 
444e7ecfdb7SKuninori Morimoto static int soc_pcm_components_open(struct snd_pcm_substream *substream,
445e7ecfdb7SKuninori Morimoto 				   struct snd_soc_component **last)
446e7ecfdb7SKuninori Morimoto {
447e7ecfdb7SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
448e7ecfdb7SKuninori Morimoto 	struct snd_soc_component *component;
449613fb500SKuninori Morimoto 	int i, ret = 0;
450e7ecfdb7SKuninori Morimoto 
451613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
452e7ecfdb7SKuninori Morimoto 		*last = component;
453e7ecfdb7SKuninori Morimoto 
4544a81e8f3SKuninori Morimoto 		ret = snd_soc_component_module_get_when_open(component);
4554a81e8f3SKuninori Morimoto 		if (ret < 0) {
456e7ecfdb7SKuninori Morimoto 			dev_err(component->dev,
457e7ecfdb7SKuninori Morimoto 				"ASoC: can't get module %s\n",
458e7ecfdb7SKuninori Morimoto 				component->name);
4594a81e8f3SKuninori Morimoto 			return ret;
460e7ecfdb7SKuninori Morimoto 		}
461e7ecfdb7SKuninori Morimoto 
462ae2f4849SKuninori Morimoto 		ret = snd_soc_component_open(component, substream);
463e7ecfdb7SKuninori Morimoto 		if (ret < 0) {
464e7ecfdb7SKuninori Morimoto 			dev_err(component->dev,
465e7ecfdb7SKuninori Morimoto 				"ASoC: can't open component %s: %d\n",
466e7ecfdb7SKuninori Morimoto 				component->name, ret);
467e7ecfdb7SKuninori Morimoto 			return ret;
468e7ecfdb7SKuninori Morimoto 		}
469e7ecfdb7SKuninori Morimoto 	}
470e7ecfdb7SKuninori Morimoto 	*last = NULL;
471e7ecfdb7SKuninori Morimoto 	return 0;
472e7ecfdb7SKuninori Morimoto }
473e7ecfdb7SKuninori Morimoto 
474244e2936SCharles Keepax static int soc_pcm_components_close(struct snd_pcm_substream *substream,
475244e2936SCharles Keepax 				    struct snd_soc_component *last)
476244e2936SCharles Keepax {
477244e2936SCharles Keepax 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
478244e2936SCharles Keepax 	struct snd_soc_component *component;
479613fb500SKuninori Morimoto 	int i, ret = 0;
480244e2936SCharles Keepax 
481613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
482244e2936SCharles Keepax 		if (component == last)
483244e2936SCharles Keepax 			break;
484244e2936SCharles Keepax 
4853672beb8SKuninori Morimoto 		ret |= snd_soc_component_close(component, substream);
4864a81e8f3SKuninori Morimoto 		snd_soc_component_module_put_when_close(component);
487244e2936SCharles Keepax 	}
488244e2936SCharles Keepax 
4893672beb8SKuninori Morimoto 	return ret;
490244e2936SCharles Keepax }
491244e2936SCharles Keepax 
49258ba9b25SMark Brown /*
493ddee627cSLiam Girdwood  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
494ddee627cSLiam Girdwood  * then initialized and any private data can be allocated. This also calls
495ef050becSCharles Keepax  * startup for the cpu DAI, component, machine and codec DAI.
496ddee627cSLiam Girdwood  */
497ddee627cSLiam Girdwood static int soc_pcm_open(struct snd_pcm_substream *substream)
498ddee627cSLiam Girdwood {
499ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
500ddee627cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
50190be711eSKuninori Morimoto 	struct snd_soc_component *component;
502ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5032e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
5042e5894d7SBenoit Cousson 	const char *codec_dai_name = "multicodec";
505244e2936SCharles Keepax 	int i, ret = 0;
506ddee627cSLiam Girdwood 
50776c39e86SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
50876c39e86SKuninori Morimoto 		pinctrl_pm_select_default_state(component->dev);
50990be711eSKuninori Morimoto 
510613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
51190be711eSKuninori Morimoto 		pm_runtime_get_sync(component->dev);
512d6652ef8SMark Brown 
51372b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
514ddee627cSLiam Girdwood 
515ddee627cSLiam Girdwood 	/* startup the audio subsystem */
5165a52a045SKuninori Morimoto 	ret = snd_soc_dai_startup(cpu_dai, substream);
517ddee627cSLiam Girdwood 	if (ret < 0) {
5185a52a045SKuninori Morimoto 		dev_err(cpu_dai->dev, "ASoC: can't open interface %s: %d\n",
5195a52a045SKuninori Morimoto 			cpu_dai->name, ret);
520ddee627cSLiam Girdwood 		goto out;
521ddee627cSLiam Girdwood 	}
522ddee627cSLiam Girdwood 
523e7ecfdb7SKuninori Morimoto 	ret = soc_pcm_components_open(substream, &component);
524e7ecfdb7SKuninori Morimoto 	if (ret < 0)
525b8135864SKuninori Morimoto 		goto component_err;
526b8135864SKuninori Morimoto 
5270b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
5285a52a045SKuninori Morimoto 		ret = snd_soc_dai_startup(codec_dai, substream);
529ddee627cSLiam Girdwood 		if (ret < 0) {
5302e5894d7SBenoit Cousson 			dev_err(codec_dai->dev,
5312e5894d7SBenoit Cousson 				"ASoC: can't open codec %s: %d\n",
5322e5894d7SBenoit Cousson 				codec_dai->name, ret);
533ddee627cSLiam Girdwood 			goto codec_dai_err;
534ddee627cSLiam Girdwood 		}
535ddee627cSLiam Girdwood 
5362e5894d7SBenoit Cousson 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5372e5894d7SBenoit Cousson 			codec_dai->tx_mask = 0;
5382e5894d7SBenoit Cousson 		else
5392e5894d7SBenoit Cousson 			codec_dai->rx_mask = 0;
5402e5894d7SBenoit Cousson 	}
5412e5894d7SBenoit Cousson 
542f183f927SKuninori Morimoto 	ret = soc_rtd_startup(rtd, substream);
543ddee627cSLiam Girdwood 	if (ret < 0) {
544103d84a3SLiam Girdwood 		pr_err("ASoC: %s startup failed: %d\n",
54525bfe662SMark Brown 		       rtd->dai_link->name, ret);
546ddee627cSLiam Girdwood 		goto machine_err;
547ddee627cSLiam Girdwood 	}
548ddee627cSLiam Girdwood 
54901d7584cSLiam Girdwood 	/* Dynamic PCM DAI links compat checks use dynamic capabilities */
55001d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
55101d7584cSLiam Girdwood 		goto dynamic;
55201d7584cSLiam Girdwood 
553ddee627cSLiam Girdwood 	/* Check that the codec and cpu DAIs are compatible */
5542e5894d7SBenoit Cousson 	soc_pcm_init_runtime_hw(substream);
5552e5894d7SBenoit Cousson 
5562e5894d7SBenoit Cousson 	if (rtd->num_codecs == 1)
5572e5894d7SBenoit Cousson 		codec_dai_name = rtd->codec_dai->name;
558ddee627cSLiam Girdwood 
55962e5f676SLars-Peter Clausen 	if (soc_pcm_has_symmetry(substream))
56062e5f676SLars-Peter Clausen 		runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
56162e5f676SLars-Peter Clausen 
562ddee627cSLiam Girdwood 	ret = -EINVAL;
563ddee627cSLiam Girdwood 	if (!runtime->hw.rates) {
564103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
5652e5894d7SBenoit Cousson 			codec_dai_name, cpu_dai->name);
566ddee627cSLiam Girdwood 		goto config_err;
567ddee627cSLiam Girdwood 	}
568ddee627cSLiam Girdwood 	if (!runtime->hw.formats) {
569103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
5702e5894d7SBenoit Cousson 			codec_dai_name, cpu_dai->name);
571ddee627cSLiam Girdwood 		goto config_err;
572ddee627cSLiam Girdwood 	}
573ddee627cSLiam Girdwood 	if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
574ddee627cSLiam Girdwood 	    runtime->hw.channels_min > runtime->hw.channels_max) {
575103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
5762e5894d7SBenoit Cousson 				codec_dai_name, cpu_dai->name);
577ddee627cSLiam Girdwood 		goto config_err;
578ddee627cSLiam Girdwood 	}
579ddee627cSLiam Girdwood 
580c8dd1fecSBenoit Cousson 	soc_pcm_apply_msb(substream);
58158ba9b25SMark Brown 
582ddee627cSLiam Girdwood 	/* Symmetry only applies if we've already got an active stream. */
58317841020SDong Aisheng 	if (cpu_dai->active) {
58417841020SDong Aisheng 		ret = soc_pcm_apply_symmetry(substream, cpu_dai);
58517841020SDong Aisheng 		if (ret != 0)
58617841020SDong Aisheng 			goto config_err;
58717841020SDong Aisheng 	}
58817841020SDong Aisheng 
5890b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
5900b7990e3SKuninori Morimoto 		if (codec_dai->active) {
5910b7990e3SKuninori Morimoto 			ret = soc_pcm_apply_symmetry(substream, codec_dai);
592ddee627cSLiam Girdwood 			if (ret != 0)
593ddee627cSLiam Girdwood 				goto config_err;
594ddee627cSLiam Girdwood 		}
5952e5894d7SBenoit Cousson 	}
596ddee627cSLiam Girdwood 
597103d84a3SLiam Girdwood 	pr_debug("ASoC: %s <-> %s info:\n",
5982e5894d7SBenoit Cousson 			codec_dai_name, cpu_dai->name);
599103d84a3SLiam Girdwood 	pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
600103d84a3SLiam Girdwood 	pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
601ddee627cSLiam Girdwood 		 runtime->hw.channels_max);
602103d84a3SLiam Girdwood 	pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
603ddee627cSLiam Girdwood 		 runtime->hw.rate_max);
604ddee627cSLiam Girdwood 
60501d7584cSLiam Girdwood dynamic:
60624894b76SLars-Peter Clausen 
60724894b76SLars-Peter Clausen 	snd_soc_runtime_activate(rtd, substream->stream);
60824894b76SLars-Peter Clausen 
60972b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
610ddee627cSLiam Girdwood 	return 0;
611ddee627cSLiam Girdwood 
612ddee627cSLiam Girdwood config_err:
613*0be429f9SKuninori Morimoto 	soc_rtd_shutdown(rtd, substream);
614ddee627cSLiam Girdwood 
615ddee627cSLiam Girdwood machine_err:
6162e5894d7SBenoit Cousson 	i = rtd->num_codecs;
617ddee627cSLiam Girdwood 
618ddee627cSLiam Girdwood codec_dai_err:
619330fcb51SKuninori Morimoto 	for_each_rtd_codec_dai_rollback(rtd, i, codec_dai)
620330fcb51SKuninori Morimoto 		snd_soc_dai_shutdown(codec_dai, substream);
6212e5894d7SBenoit Cousson 
622b8135864SKuninori Morimoto component_err:
623244e2936SCharles Keepax 	soc_pcm_components_close(substream, component);
624e7ecfdb7SKuninori Morimoto 
625330fcb51SKuninori Morimoto 	snd_soc_dai_shutdown(cpu_dai, substream);
626ddee627cSLiam Girdwood out:
62772b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
628d6652ef8SMark Brown 
629613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
63090be711eSKuninori Morimoto 		pm_runtime_mark_last_busy(component->dev);
63190be711eSKuninori Morimoto 		pm_runtime_put_autosuspend(component->dev);
6323f809783SSanyog Kale 	}
6333f809783SSanyog Kale 
63476c39e86SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
63576c39e86SKuninori Morimoto 		if (!component->active)
63676c39e86SKuninori Morimoto 			pinctrl_pm_select_sleep_state(component->dev);
637d6652ef8SMark Brown 
638ddee627cSLiam Girdwood 	return ret;
639ddee627cSLiam Girdwood }
640ddee627cSLiam Girdwood 
6414bf2e385SCurtis Malainey static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
642a342031cSJerome Brunet {
643a342031cSJerome Brunet 	/*
644a342031cSJerome Brunet 	 * Currently nothing to do for c2c links
645a342031cSJerome Brunet 	 * Since c2c links are internal nodes in the DAPM graph and
646a342031cSJerome Brunet 	 * don't interface with the outside world or application layer
647a342031cSJerome Brunet 	 * we don't have to do any special handling on close.
648a342031cSJerome Brunet 	 */
649a342031cSJerome Brunet }
650a342031cSJerome Brunet 
651ddee627cSLiam Girdwood /*
652ddee627cSLiam Girdwood  * Called by ALSA when a PCM substream is closed. Private data can be
653ef050becSCharles Keepax  * freed here. The cpu DAI, codec DAI, machine and components are also
654ddee627cSLiam Girdwood  * shutdown.
655ddee627cSLiam Girdwood  */
65691d5e6b4SLiam Girdwood static int soc_pcm_close(struct snd_pcm_substream *substream)
657ddee627cSLiam Girdwood {
658ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
65990be711eSKuninori Morimoto 	struct snd_soc_component *component;
660ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6612e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
6622e5894d7SBenoit Cousson 	int i;
663ddee627cSLiam Girdwood 
66472b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
665ddee627cSLiam Girdwood 
66624894b76SLars-Peter Clausen 	snd_soc_runtime_deactivate(rtd, substream->stream);
667ddee627cSLiam Girdwood 
66817841020SDong Aisheng 	/* clear the corresponding DAIs rate when inactive */
66917841020SDong Aisheng 	if (!cpu_dai->active)
67017841020SDong Aisheng 		cpu_dai->rate = 0;
67117841020SDong Aisheng 
6720b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
67317841020SDong Aisheng 		if (!codec_dai->active)
67417841020SDong Aisheng 			codec_dai->rate = 0;
6752e5894d7SBenoit Cousson 	}
67625b76791SSascha Hauer 
677ae11601bSRamesh Babu 	snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
678ae11601bSRamesh Babu 
679330fcb51SKuninori Morimoto 	snd_soc_dai_shutdown(cpu_dai, substream);
680ddee627cSLiam Girdwood 
681330fcb51SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
682330fcb51SKuninori Morimoto 		snd_soc_dai_shutdown(codec_dai, substream);
683ddee627cSLiam Girdwood 
684*0be429f9SKuninori Morimoto 	soc_rtd_shutdown(rtd, substream);
685ddee627cSLiam Girdwood 
686244e2936SCharles Keepax 	soc_pcm_components_close(substream, NULL);
687b8135864SKuninori Morimoto 
6883f4cf797SKuninori Morimoto 	snd_soc_dapm_stream_stop(rtd, substream->stream);
689ddee627cSLiam Girdwood 
69072b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
691d6652ef8SMark Brown 
692613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
69390be711eSKuninori Morimoto 		pm_runtime_mark_last_busy(component->dev);
69490be711eSKuninori Morimoto 		pm_runtime_put_autosuspend(component->dev);
6953f809783SSanyog Kale 	}
6963f809783SSanyog Kale 
69776c39e86SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
69876c39e86SKuninori Morimoto 		if (!component->active)
69976c39e86SKuninori Morimoto 			pinctrl_pm_select_sleep_state(component->dev);
700d6652ef8SMark Brown 
701ddee627cSLiam Girdwood 	return 0;
702ddee627cSLiam Girdwood }
703ddee627cSLiam Girdwood 
704ddee627cSLiam Girdwood /*
705ddee627cSLiam Girdwood  * Called by ALSA when the PCM substream is prepared, can set format, sample
706ddee627cSLiam Girdwood  * rate, etc.  This function is non atomic and can be called multiple times,
707ddee627cSLiam Girdwood  * it can refer to the runtime info.
708ddee627cSLiam Girdwood  */
709ddee627cSLiam Girdwood static int soc_pcm_prepare(struct snd_pcm_substream *substream)
710ddee627cSLiam Girdwood {
711ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
712b8135864SKuninori Morimoto 	struct snd_soc_component *component;
713ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7142e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
7152e5894d7SBenoit Cousson 	int i, ret = 0;
716ddee627cSLiam Girdwood 
71772b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
718ddee627cSLiam Girdwood 
71975ab9eb6SKuninori Morimoto 	if (rtd->dai_link->ops->prepare) {
720ddee627cSLiam Girdwood 		ret = rtd->dai_link->ops->prepare(substream);
721ddee627cSLiam Girdwood 		if (ret < 0) {
722103d84a3SLiam Girdwood 			dev_err(rtd->card->dev, "ASoC: machine prepare error:"
723103d84a3SLiam Girdwood 				" %d\n", ret);
724ddee627cSLiam Girdwood 			goto out;
725ddee627cSLiam Girdwood 		}
726ddee627cSLiam Girdwood 	}
727ddee627cSLiam Girdwood 
728613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
7296d537233SKuninori Morimoto 		ret = snd_soc_component_prepare(component, substream);
730b8135864SKuninori Morimoto 		if (ret < 0) {
731b8135864SKuninori Morimoto 			dev_err(component->dev,
732b8135864SKuninori Morimoto 				"ASoC: platform prepare error: %d\n", ret);
733b8135864SKuninori Morimoto 			goto out;
734b8135864SKuninori Morimoto 		}
735b8135864SKuninori Morimoto 	}
736b8135864SKuninori Morimoto 
7370b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
7384beb8e10SKuninori Morimoto 		ret = snd_soc_dai_prepare(codec_dai, substream);
739ddee627cSLiam Girdwood 		if (ret < 0) {
7402e5894d7SBenoit Cousson 			dev_err(codec_dai->dev,
74190cc7f1cSJarkko Nikula 				"ASoC: codec DAI prepare error: %d\n",
74290cc7f1cSJarkko Nikula 				ret);
743ddee627cSLiam Girdwood 			goto out;
744ddee627cSLiam Girdwood 		}
745ddee627cSLiam Girdwood 	}
746ddee627cSLiam Girdwood 
7474beb8e10SKuninori Morimoto 	ret = snd_soc_dai_prepare(cpu_dai, substream);
748ddee627cSLiam Girdwood 	if (ret < 0) {
74990cc7f1cSJarkko Nikula 		dev_err(cpu_dai->dev,
75090cc7f1cSJarkko Nikula 			"ASoC: cpu DAI prepare error: %d\n", ret);
751ddee627cSLiam Girdwood 		goto out;
752ddee627cSLiam Girdwood 	}
753ddee627cSLiam Girdwood 
754ddee627cSLiam Girdwood 	/* cancel any delayed stream shutdown that is pending */
755ddee627cSLiam Girdwood 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
7569bffb1fbSMisael Lopez Cruz 	    rtd->pop_wait) {
7579bffb1fbSMisael Lopez Cruz 		rtd->pop_wait = 0;
758ddee627cSLiam Girdwood 		cancel_delayed_work(&rtd->delayed_work);
759ddee627cSLiam Girdwood 	}
760ddee627cSLiam Girdwood 
761d9b0951bSLiam Girdwood 	snd_soc_dapm_stream_event(rtd, substream->stream,
762ddee627cSLiam Girdwood 			SND_SOC_DAPM_STREAM_START);
763ddee627cSLiam Girdwood 
7640b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
7650b7990e3SKuninori Morimoto 		snd_soc_dai_digital_mute(codec_dai, 0,
7662e5894d7SBenoit Cousson 					 substream->stream);
767ae11601bSRamesh Babu 	snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
768ddee627cSLiam Girdwood 
769ddee627cSLiam Girdwood out:
77072b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
771ddee627cSLiam Girdwood 	return ret;
772ddee627cSLiam Girdwood }
773ddee627cSLiam Girdwood 
7742e5894d7SBenoit Cousson static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
7752e5894d7SBenoit Cousson 				       unsigned int mask)
7762e5894d7SBenoit Cousson {
7772e5894d7SBenoit Cousson 	struct snd_interval *interval;
7782e5894d7SBenoit Cousson 	int channels = hweight_long(mask);
7792e5894d7SBenoit Cousson 
7802e5894d7SBenoit Cousson 	interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
7812e5894d7SBenoit Cousson 	interval->min = channels;
7822e5894d7SBenoit Cousson 	interval->max = channels;
7832e5894d7SBenoit Cousson }
7842e5894d7SBenoit Cousson 
785244e2936SCharles Keepax static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
786244e2936SCharles Keepax 				      struct snd_soc_component *last)
787244e2936SCharles Keepax {
788244e2936SCharles Keepax 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
789244e2936SCharles Keepax 	struct snd_soc_component *component;
790613fb500SKuninori Morimoto 	int i, ret = 0;
791244e2936SCharles Keepax 
792613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
793244e2936SCharles Keepax 		if (component == last)
794244e2936SCharles Keepax 			break;
795244e2936SCharles Keepax 
796eae7136aSKuninori Morimoto 		ret |= snd_soc_component_hw_free(component, substream);
797244e2936SCharles Keepax 	}
798244e2936SCharles Keepax 
799eae7136aSKuninori Morimoto 	return ret;
800244e2936SCharles Keepax }
801244e2936SCharles Keepax 
802ddee627cSLiam Girdwood /*
803ddee627cSLiam Girdwood  * Called by ALSA when the hardware params are set by application. This
804ddee627cSLiam Girdwood  * function can also be called multiple times and can allocate buffers
805ddee627cSLiam Girdwood  * (using snd_pcm_lib_* ). It's non-atomic.
806ddee627cSLiam Girdwood  */
807ddee627cSLiam Girdwood static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
808ddee627cSLiam Girdwood 				struct snd_pcm_hw_params *params)
809ddee627cSLiam Girdwood {
810ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
811b8135864SKuninori Morimoto 	struct snd_soc_component *component;
812ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
8130b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
814244e2936SCharles Keepax 	int i, ret = 0;
815ddee627cSLiam Girdwood 
81672b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
8175cca5951SShengjiu Wang 
8185cca5951SShengjiu Wang 	ret = soc_pcm_params_symmetry(substream, params);
8195cca5951SShengjiu Wang 	if (ret)
8205cca5951SShengjiu Wang 		goto out;
8215cca5951SShengjiu Wang 
82275ab9eb6SKuninori Morimoto 	if (rtd->dai_link->ops->hw_params) {
823ddee627cSLiam Girdwood 		ret = rtd->dai_link->ops->hw_params(substream, params);
824ddee627cSLiam Girdwood 		if (ret < 0) {
825103d84a3SLiam Girdwood 			dev_err(rtd->card->dev, "ASoC: machine hw_params"
826103d84a3SLiam Girdwood 				" failed: %d\n", ret);
827ddee627cSLiam Girdwood 			goto out;
828ddee627cSLiam Girdwood 		}
829ddee627cSLiam Girdwood 	}
830ddee627cSLiam Girdwood 
8310b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
8322e5894d7SBenoit Cousson 		struct snd_pcm_hw_params codec_params;
8332e5894d7SBenoit Cousson 
834cde79035SRicard Wanderlof 		/*
835cde79035SRicard Wanderlof 		 * Skip CODECs which don't support the current stream type,
836cde79035SRicard Wanderlof 		 * the idea being that if a CODEC is not used for the currently
837cde79035SRicard Wanderlof 		 * set up transfer direction, it should not need to be
838cde79035SRicard Wanderlof 		 * configured, especially since the configuration used might
839cde79035SRicard Wanderlof 		 * not even be supported by that CODEC. There may be cases
840cde79035SRicard Wanderlof 		 * however where a CODEC needs to be set up although it is
841cde79035SRicard Wanderlof 		 * actually not being used for the transfer, e.g. if a
842cde79035SRicard Wanderlof 		 * capture-only CODEC is acting as an LRCLK and/or BCLK master
843cde79035SRicard Wanderlof 		 * for the DAI link including a playback-only CODEC.
844cde79035SRicard Wanderlof 		 * If this becomes necessary, we will have to augment the
845cde79035SRicard Wanderlof 		 * machine driver setup with information on how to act, so
846cde79035SRicard Wanderlof 		 * we can do the right thing here.
847cde79035SRicard Wanderlof 		 */
848cde79035SRicard Wanderlof 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
849cde79035SRicard Wanderlof 			continue;
850cde79035SRicard Wanderlof 
8512e5894d7SBenoit Cousson 		/* copy params for each codec */
8522e5894d7SBenoit Cousson 		codec_params = *params;
8532e5894d7SBenoit Cousson 
8542e5894d7SBenoit Cousson 		/* fixup params based on TDM slot masks */
855570f18b6SRander Wang 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
856570f18b6SRander Wang 		    codec_dai->tx_mask)
8572e5894d7SBenoit Cousson 			soc_pcm_codec_params_fixup(&codec_params,
8582e5894d7SBenoit Cousson 						   codec_dai->tx_mask);
859570f18b6SRander Wang 
860570f18b6SRander Wang 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
861570f18b6SRander Wang 		    codec_dai->rx_mask)
8622e5894d7SBenoit Cousson 			soc_pcm_codec_params_fixup(&codec_params,
8632e5894d7SBenoit Cousson 						   codec_dai->rx_mask);
8642e5894d7SBenoit Cousson 
865aa6166c2SKuninori Morimoto 		ret = snd_soc_dai_hw_params(codec_dai, substream,
866aa6166c2SKuninori Morimoto 					    &codec_params);
86793e6958aSBenoit Cousson 		if(ret < 0)
868ddee627cSLiam Girdwood 			goto codec_err;
869ddee627cSLiam Girdwood 
8702e5894d7SBenoit Cousson 		codec_dai->rate = params_rate(&codec_params);
8712e5894d7SBenoit Cousson 		codec_dai->channels = params_channels(&codec_params);
8722e5894d7SBenoit Cousson 		codec_dai->sample_bits = snd_pcm_format_physical_width(
8732e5894d7SBenoit Cousson 						params_format(&codec_params));
874078a85f2SCharles Keepax 
875078a85f2SCharles Keepax 		snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
876ddee627cSLiam Girdwood 	}
877ddee627cSLiam Girdwood 
878aa6166c2SKuninori Morimoto 	ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
87993e6958aSBenoit Cousson 	if (ret < 0)
880ddee627cSLiam Girdwood 		goto interface_err;
881ddee627cSLiam Girdwood 
882ca58221dSKuninori Morimoto 	/* store the parameters for each DAIs */
883ca58221dSKuninori Morimoto 	cpu_dai->rate = params_rate(params);
884ca58221dSKuninori Morimoto 	cpu_dai->channels = params_channels(params);
885ca58221dSKuninori Morimoto 	cpu_dai->sample_bits =
886ca58221dSKuninori Morimoto 		snd_pcm_format_physical_width(params_format(params));
887ca58221dSKuninori Morimoto 
888ca58221dSKuninori Morimoto 	snd_soc_dapm_update_dai(substream, params, cpu_dai);
889ca58221dSKuninori Morimoto 
890613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
891245c539aSKuninori Morimoto 		ret = snd_soc_component_hw_params(component, substream, params);
892244e2936SCharles Keepax 		if (ret < 0) {
893b8135864SKuninori Morimoto 			dev_err(component->dev,
894b8135864SKuninori Morimoto 				"ASoC: %s hw params failed: %d\n",
895244e2936SCharles Keepax 				component->name, ret);
896b8135864SKuninori Morimoto 			goto component_err;
897244e2936SCharles Keepax 		}
898244e2936SCharles Keepax 	}
899244e2936SCharles Keepax 	component = NULL;
900b8135864SKuninori Morimoto 
901ddee627cSLiam Girdwood out:
90272b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
903ddee627cSLiam Girdwood 	return ret;
904ddee627cSLiam Girdwood 
905b8135864SKuninori Morimoto component_err:
906244e2936SCharles Keepax 	soc_pcm_components_hw_free(substream, component);
907b8135864SKuninori Morimoto 
908846faaedSKuninori Morimoto 	snd_soc_dai_hw_free(cpu_dai, substream);
9092371abdcSKuninori Morimoto 	cpu_dai->rate = 0;
910ddee627cSLiam Girdwood 
911ddee627cSLiam Girdwood interface_err:
9122e5894d7SBenoit Cousson 	i = rtd->num_codecs;
913ddee627cSLiam Girdwood 
914ddee627cSLiam Girdwood codec_err:
9156d11b128SKuninori Morimoto 	for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
916f47b9ad9SJerome Brunet 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
917f47b9ad9SJerome Brunet 			continue;
918f47b9ad9SJerome Brunet 
919846faaedSKuninori Morimoto 		snd_soc_dai_hw_free(codec_dai, substream);
9202e5894d7SBenoit Cousson 		codec_dai->rate = 0;
9212e5894d7SBenoit Cousson 	}
9222e5894d7SBenoit Cousson 
92375ab9eb6SKuninori Morimoto 	if (rtd->dai_link->ops->hw_free)
924ddee627cSLiam Girdwood 		rtd->dai_link->ops->hw_free(substream);
925ddee627cSLiam Girdwood 
92672b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
927ddee627cSLiam Girdwood 	return ret;
928ddee627cSLiam Girdwood }
929ddee627cSLiam Girdwood 
930ddee627cSLiam Girdwood /*
931ddee627cSLiam Girdwood  * Frees resources allocated by hw_params, can be called multiple times
932ddee627cSLiam Girdwood  */
933ddee627cSLiam Girdwood static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
934ddee627cSLiam Girdwood {
935ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
936ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
9372e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
9387f62b6eeSNicolin Chen 	bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
9392e5894d7SBenoit Cousson 	int i;
940ddee627cSLiam Girdwood 
94172b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
942ddee627cSLiam Girdwood 
943d3383420SNicolin Chen 	/* clear the corresponding DAIs parameters when going to be inactive */
944d3383420SNicolin Chen 	if (cpu_dai->active == 1) {
945d3383420SNicolin Chen 		cpu_dai->rate = 0;
946d3383420SNicolin Chen 		cpu_dai->channels = 0;
947d3383420SNicolin Chen 		cpu_dai->sample_bits = 0;
948d3383420SNicolin Chen 	}
949d3383420SNicolin Chen 
9500b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
951d3383420SNicolin Chen 		if (codec_dai->active == 1) {
952d3383420SNicolin Chen 			codec_dai->rate = 0;
953d3383420SNicolin Chen 			codec_dai->channels = 0;
954d3383420SNicolin Chen 			codec_dai->sample_bits = 0;
955d3383420SNicolin Chen 		}
9562e5894d7SBenoit Cousson 	}
957d3383420SNicolin Chen 
958ddee627cSLiam Girdwood 	/* apply codec digital mute */
9590b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
9600b7990e3SKuninori Morimoto 		if ((playback && codec_dai->playback_active == 1) ||
9610b7990e3SKuninori Morimoto 		    (!playback && codec_dai->capture_active == 1))
9620b7990e3SKuninori Morimoto 			snd_soc_dai_digital_mute(codec_dai, 1,
9632e5894d7SBenoit Cousson 						 substream->stream);
9642e5894d7SBenoit Cousson 	}
965ddee627cSLiam Girdwood 
966ddee627cSLiam Girdwood 	/* free any machine hw params */
96775ab9eb6SKuninori Morimoto 	if (rtd->dai_link->ops->hw_free)
968ddee627cSLiam Girdwood 		rtd->dai_link->ops->hw_free(substream);
969ddee627cSLiam Girdwood 
970b8135864SKuninori Morimoto 	/* free any component resources */
971244e2936SCharles Keepax 	soc_pcm_components_hw_free(substream, NULL);
972b8135864SKuninori Morimoto 
973ddee627cSLiam Girdwood 	/* now free hw params for the DAIs  */
9740b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
975f47b9ad9SJerome Brunet 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
976f47b9ad9SJerome Brunet 			continue;
977f47b9ad9SJerome Brunet 
978846faaedSKuninori Morimoto 		snd_soc_dai_hw_free(codec_dai, substream);
9792e5894d7SBenoit Cousson 	}
980ddee627cSLiam Girdwood 
981846faaedSKuninori Morimoto 	snd_soc_dai_hw_free(cpu_dai, substream);
982ddee627cSLiam Girdwood 
98372b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
984ddee627cSLiam Girdwood 	return 0;
985ddee627cSLiam Girdwood }
986ddee627cSLiam Girdwood 
9874378f1fbSPeter Ujfalusi static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd)
988ddee627cSLiam Girdwood {
989ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
990b8135864SKuninori Morimoto 	struct snd_soc_component *component;
991ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
9922e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
9932e5894d7SBenoit Cousson 	int i, ret;
994ddee627cSLiam Girdwood 
9954378f1fbSPeter Ujfalusi 	if (rtd->dai_link->ops->trigger) {
9964378f1fbSPeter Ujfalusi 		ret = rtd->dai_link->ops->trigger(substream, cmd);
997ddee627cSLiam Girdwood 		if (ret < 0)
998ddee627cSLiam Girdwood 			return ret;
999ddee627cSLiam Girdwood 	}
1000ddee627cSLiam Girdwood 
1001613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
10025693d50cSKuninori Morimoto 		ret = snd_soc_component_trigger(component, substream, cmd);
1003b8135864SKuninori Morimoto 		if (ret < 0)
1004b8135864SKuninori Morimoto 			return ret;
1005b8135864SKuninori Morimoto 	}
1006b8135864SKuninori Morimoto 
1007901e822bSDan Carpenter 	ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
1008ddee627cSLiam Girdwood 	if (ret < 0)
1009ddee627cSLiam Girdwood 		return ret;
10104792b0dbSJarkko Nikula 
10114378f1fbSPeter Ujfalusi 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
10124378f1fbSPeter Ujfalusi 		ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
10134378f1fbSPeter Ujfalusi 		if (ret < 0)
10144378f1fbSPeter Ujfalusi 			return ret;
10154378f1fbSPeter Ujfalusi 	}
10164378f1fbSPeter Ujfalusi 
10174378f1fbSPeter Ujfalusi 	return 0;
10184378f1fbSPeter Ujfalusi }
10194378f1fbSPeter Ujfalusi 
10204378f1fbSPeter Ujfalusi static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd)
10214378f1fbSPeter Ujfalusi {
10224378f1fbSPeter Ujfalusi 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
10234378f1fbSPeter Ujfalusi 	struct snd_soc_component *component;
10244378f1fbSPeter Ujfalusi 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
10254378f1fbSPeter Ujfalusi 	struct snd_soc_dai *codec_dai;
10264378f1fbSPeter Ujfalusi 	int i, ret;
10274378f1fbSPeter Ujfalusi 
10284378f1fbSPeter Ujfalusi 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
10294378f1fbSPeter Ujfalusi 		ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
10304378f1fbSPeter Ujfalusi 		if (ret < 0)
10314378f1fbSPeter Ujfalusi 			return ret;
10324378f1fbSPeter Ujfalusi 	}
10334378f1fbSPeter Ujfalusi 
10344378f1fbSPeter Ujfalusi 	ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
10354378f1fbSPeter Ujfalusi 	if (ret < 0)
10364378f1fbSPeter Ujfalusi 		return ret;
10374378f1fbSPeter Ujfalusi 
1038613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
10394378f1fbSPeter Ujfalusi 		ret = snd_soc_component_trigger(component, substream, cmd);
10404378f1fbSPeter Ujfalusi 		if (ret < 0)
10414378f1fbSPeter Ujfalusi 			return ret;
10424378f1fbSPeter Ujfalusi 	}
10434378f1fbSPeter Ujfalusi 
104475ab9eb6SKuninori Morimoto 	if (rtd->dai_link->ops->trigger) {
10454792b0dbSJarkko Nikula 		ret = rtd->dai_link->ops->trigger(substream, cmd);
10464792b0dbSJarkko Nikula 		if (ret < 0)
10474792b0dbSJarkko Nikula 			return ret;
10484792b0dbSJarkko Nikula 	}
10494792b0dbSJarkko Nikula 
1050ddee627cSLiam Girdwood 	return 0;
1051ddee627cSLiam Girdwood }
1052ddee627cSLiam Girdwood 
10534378f1fbSPeter Ujfalusi static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
10544378f1fbSPeter Ujfalusi {
10554378f1fbSPeter Ujfalusi 	int ret;
10564378f1fbSPeter Ujfalusi 
10574378f1fbSPeter Ujfalusi 	switch (cmd) {
10584378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_START:
10594378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_RESUME:
10604378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
10614378f1fbSPeter Ujfalusi 		ret = soc_pcm_trigger_start(substream, cmd);
10624378f1fbSPeter Ujfalusi 		break;
10634378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_STOP:
10644378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_SUSPEND:
10654378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
10664378f1fbSPeter Ujfalusi 		ret = soc_pcm_trigger_stop(substream, cmd);
10674378f1fbSPeter Ujfalusi 		break;
10684378f1fbSPeter Ujfalusi 	default:
10694378f1fbSPeter Ujfalusi 		return -EINVAL;
10704378f1fbSPeter Ujfalusi 	}
10714378f1fbSPeter Ujfalusi 
10724378f1fbSPeter Ujfalusi 	return ret;
10734378f1fbSPeter Ujfalusi }
10744378f1fbSPeter Ujfalusi 
107545c0a188SMark Brown static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
107645c0a188SMark Brown 				   int cmd)
107707bf84aaSLiam Girdwood {
107807bf84aaSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
107907bf84aaSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
10802e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
10812e5894d7SBenoit Cousson 	int i, ret;
108207bf84aaSLiam Girdwood 
10830b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
10845c0769afSKuninori Morimoto 		ret = snd_soc_dai_bespoke_trigger(codec_dai, substream, cmd);
108507bf84aaSLiam Girdwood 		if (ret < 0)
108607bf84aaSLiam Girdwood 			return ret;
108707bf84aaSLiam Girdwood 	}
108807bf84aaSLiam Girdwood 
1089901e822bSDan Carpenter 	ret = snd_soc_dai_bespoke_trigger(cpu_dai, substream, cmd);
109007bf84aaSLiam Girdwood 	if (ret < 0)
109107bf84aaSLiam Girdwood 		return ret;
10925c0769afSKuninori Morimoto 
109307bf84aaSLiam Girdwood 	return 0;
109407bf84aaSLiam Girdwood }
1095ddee627cSLiam Girdwood /*
1096ddee627cSLiam Girdwood  * soc level wrapper for pointer callback
1097ef050becSCharles Keepax  * If cpu_dai, codec_dai, component driver has the delay callback, then
1098ddee627cSLiam Girdwood  * the runtime->delay will be updated accordingly.
1099ddee627cSLiam Girdwood  */
1100ddee627cSLiam Girdwood static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1101ddee627cSLiam Girdwood {
1102ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1103ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
11042e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
1105ddee627cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
1106ddee627cSLiam Girdwood 	snd_pcm_uframes_t offset = 0;
1107ddee627cSLiam Girdwood 	snd_pcm_sframes_t delay = 0;
11082e5894d7SBenoit Cousson 	snd_pcm_sframes_t codec_delay = 0;
11092e5894d7SBenoit Cousson 	int i;
1110ddee627cSLiam Girdwood 
11119fb4c2bfSAkshu Agrawal 	/* clearing the previous total delay */
11129fb4c2bfSAkshu Agrawal 	runtime->delay = 0;
11139fb4c2bfSAkshu Agrawal 
11140035e256SKuninori Morimoto 	offset = snd_soc_pcm_component_pointer(substream);
1115b8135864SKuninori Morimoto 
11169fb4c2bfSAkshu Agrawal 	/* base delay if assigned in pointer callback */
11179fb4c2bfSAkshu Agrawal 	delay = runtime->delay;
1118b8135864SKuninori Morimoto 
11191dea80d4SKuninori Morimoto 	delay += snd_soc_dai_delay(cpu_dai, substream);
1120ddee627cSLiam Girdwood 
11210b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
11222e5894d7SBenoit Cousson 		codec_delay = max(codec_delay,
11231dea80d4SKuninori Morimoto 				  snd_soc_dai_delay(codec_dai, substream));
11242e5894d7SBenoit Cousson 	}
11252e5894d7SBenoit Cousson 	delay += codec_delay;
1126ddee627cSLiam Girdwood 
1127ddee627cSLiam Girdwood 	runtime->delay = delay;
1128ddee627cSLiam Girdwood 
1129ddee627cSLiam Girdwood 	return offset;
1130ddee627cSLiam Girdwood }
1131ddee627cSLiam Girdwood 
113201d7584cSLiam Girdwood /* connect a FE and BE */
113301d7584cSLiam Girdwood static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
113401d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
113501d7584cSLiam Girdwood {
113601d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
1137a9764869SKaiChieh Chuang 	unsigned long flags;
1138bd0b609eSTakashi Iwai #ifdef CONFIG_DEBUG_FS
11390632fa04SHans de Goede 	char *name;
1140bd0b609eSTakashi Iwai #endif
114101d7584cSLiam Girdwood 
114201d7584cSLiam Girdwood 	/* only add new dpcms */
11438d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
114401d7584cSLiam Girdwood 		if (dpcm->be == be && dpcm->fe == fe)
114501d7584cSLiam Girdwood 			return 0;
114601d7584cSLiam Girdwood 	}
114701d7584cSLiam Girdwood 
114801d7584cSLiam Girdwood 	dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
114901d7584cSLiam Girdwood 	if (!dpcm)
115001d7584cSLiam Girdwood 		return -ENOMEM;
115101d7584cSLiam Girdwood 
115201d7584cSLiam Girdwood 	dpcm->be = be;
115301d7584cSLiam Girdwood 	dpcm->fe = fe;
115401d7584cSLiam Girdwood 	be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
115501d7584cSLiam Girdwood 	dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1156a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
115701d7584cSLiam Girdwood 	list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
115801d7584cSLiam Girdwood 	list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1159a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
116001d7584cSLiam Girdwood 
116101d7584cSLiam Girdwood 	dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
116201d7584cSLiam Girdwood 			stream ? "capture" : "playback",  fe->dai_link->name,
116301d7584cSLiam Girdwood 			stream ? "<-" : "->", be->dai_link->name);
116401d7584cSLiam Girdwood 
1165f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS
11660632fa04SHans de Goede 	name = kasprintf(GFP_KERNEL, "%s:%s", be->dai_link->name,
11670632fa04SHans de Goede 			 stream ? "capture" : "playback");
11680632fa04SHans de Goede 	if (name) {
11690632fa04SHans de Goede 		dpcm->debugfs_state = debugfs_create_dir(name,
1170fee531d6SGreg Kroah-Hartman 							 fe->debugfs_dpcm_root);
11710632fa04SHans de Goede 		debugfs_create_u32("state", 0644, dpcm->debugfs_state,
11720632fa04SHans de Goede 				   &dpcm->state);
11730632fa04SHans de Goede 		kfree(name);
11740632fa04SHans de Goede 	}
1175f86dcef8SLiam Girdwood #endif
117601d7584cSLiam Girdwood 	return 1;
117701d7584cSLiam Girdwood }
117801d7584cSLiam Girdwood 
117901d7584cSLiam Girdwood /* reparent a BE onto another FE */
118001d7584cSLiam Girdwood static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
118101d7584cSLiam Girdwood 			struct snd_soc_pcm_runtime *be, int stream)
118201d7584cSLiam Girdwood {
118301d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
118401d7584cSLiam Girdwood 	struct snd_pcm_substream *fe_substream, *be_substream;
118501d7584cSLiam Girdwood 
118601d7584cSLiam Girdwood 	/* reparent if BE is connected to other FEs */
118701d7584cSLiam Girdwood 	if (!be->dpcm[stream].users)
118801d7584cSLiam Girdwood 		return;
118901d7584cSLiam Girdwood 
119001d7584cSLiam Girdwood 	be_substream = snd_soc_dpcm_get_substream(be, stream);
119101d7584cSLiam Girdwood 
1192d2e24d64SKuninori Morimoto 	for_each_dpcm_fe(be, stream, dpcm) {
119301d7584cSLiam Girdwood 		if (dpcm->fe == fe)
119401d7584cSLiam Girdwood 			continue;
119501d7584cSLiam Girdwood 
119601d7584cSLiam Girdwood 		dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
119701d7584cSLiam Girdwood 			stream ? "capture" : "playback",
119801d7584cSLiam Girdwood 			dpcm->fe->dai_link->name,
119901d7584cSLiam Girdwood 			stream ? "<-" : "->", dpcm->be->dai_link->name);
120001d7584cSLiam Girdwood 
120101d7584cSLiam Girdwood 		fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
120201d7584cSLiam Girdwood 		be_substream->runtime = fe_substream->runtime;
120301d7584cSLiam Girdwood 		break;
120401d7584cSLiam Girdwood 	}
120501d7584cSLiam Girdwood }
120601d7584cSLiam Girdwood 
120701d7584cSLiam Girdwood /* disconnect a BE and FE */
120823607025SLiam Girdwood void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
120901d7584cSLiam Girdwood {
121001d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm, *d;
1211a9764869SKaiChieh Chuang 	unsigned long flags;
121201d7584cSLiam Girdwood 
12138d6258a4SKuninori Morimoto 	for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1214103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
121501d7584cSLiam Girdwood 				stream ? "capture" : "playback",
121601d7584cSLiam Girdwood 				dpcm->be->dai_link->name);
121701d7584cSLiam Girdwood 
121801d7584cSLiam Girdwood 		if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
121901d7584cSLiam Girdwood 			continue;
122001d7584cSLiam Girdwood 
122101d7584cSLiam Girdwood 		dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
122201d7584cSLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name,
122301d7584cSLiam Girdwood 			stream ? "<-" : "->", dpcm->be->dai_link->name);
122401d7584cSLiam Girdwood 
122501d7584cSLiam Girdwood 		/* BEs still alive need new FE */
122601d7584cSLiam Girdwood 		dpcm_be_reparent(fe, dpcm->be, stream);
122701d7584cSLiam Girdwood 
1228f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS
1229fee531d6SGreg Kroah-Hartman 		debugfs_remove_recursive(dpcm->debugfs_state);
1230f86dcef8SLiam Girdwood #endif
1231a9764869SKaiChieh Chuang 		spin_lock_irqsave(&fe->card->dpcm_lock, flags);
123201d7584cSLiam Girdwood 		list_del(&dpcm->list_be);
123301d7584cSLiam Girdwood 		list_del(&dpcm->list_fe);
1234a9764869SKaiChieh Chuang 		spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
123501d7584cSLiam Girdwood 		kfree(dpcm);
123601d7584cSLiam Girdwood 	}
123701d7584cSLiam Girdwood }
123801d7584cSLiam Girdwood 
123901d7584cSLiam Girdwood /* get BE for DAI widget and stream */
124001d7584cSLiam Girdwood static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
124101d7584cSLiam Girdwood 		struct snd_soc_dapm_widget *widget, int stream)
124201d7584cSLiam Girdwood {
124301d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *be;
12447afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
12451a497983SMengdong Lin 	int i;
124601d7584cSLiam Girdwood 
12473c146465SLiam Girdwood 	dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
12483c146465SLiam Girdwood 
124901d7584cSLiam Girdwood 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1250bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, be) {
125101d7584cSLiam Girdwood 
125235ea0655SLiam Girdwood 			if (!be->dai_link->no_pcm)
125335ea0655SLiam Girdwood 				continue;
125435ea0655SLiam Girdwood 
12553c146465SLiam Girdwood 			dev_dbg(card->dev, "ASoC: try BE : %s\n",
12563c146465SLiam Girdwood 				be->cpu_dai->playback_widget ?
12573c146465SLiam Girdwood 				be->cpu_dai->playback_widget->name : "(not set)");
12583c146465SLiam Girdwood 
12592e5894d7SBenoit Cousson 			if (be->cpu_dai->playback_widget == widget)
126001d7584cSLiam Girdwood 				return be;
12612e5894d7SBenoit Cousson 
12627afecb30SKuninori Morimoto 			for_each_rtd_codec_dai(be, i, dai) {
12632e5894d7SBenoit Cousson 				if (dai->playback_widget == widget)
12642e5894d7SBenoit Cousson 					return be;
12652e5894d7SBenoit Cousson 			}
126601d7584cSLiam Girdwood 		}
126701d7584cSLiam Girdwood 	} else {
126801d7584cSLiam Girdwood 
1269bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, be) {
127001d7584cSLiam Girdwood 
127135ea0655SLiam Girdwood 			if (!be->dai_link->no_pcm)
127235ea0655SLiam Girdwood 				continue;
127335ea0655SLiam Girdwood 
12743c146465SLiam Girdwood 			dev_dbg(card->dev, "ASoC: try BE %s\n",
12753c146465SLiam Girdwood 				be->cpu_dai->capture_widget ?
12763c146465SLiam Girdwood 				be->cpu_dai->capture_widget->name : "(not set)");
12773c146465SLiam Girdwood 
12782e5894d7SBenoit Cousson 			if (be->cpu_dai->capture_widget == widget)
127901d7584cSLiam Girdwood 				return be;
12802e5894d7SBenoit Cousson 
12817afecb30SKuninori Morimoto 			for_each_rtd_codec_dai(be, i, dai) {
12822e5894d7SBenoit Cousson 				if (dai->capture_widget == widget)
12832e5894d7SBenoit Cousson 					return be;
12842e5894d7SBenoit Cousson 			}
128501d7584cSLiam Girdwood 		}
128601d7584cSLiam Girdwood 	}
128701d7584cSLiam Girdwood 
12883c146465SLiam Girdwood 	/* dai link name and stream name set correctly ? */
1289103d84a3SLiam Girdwood 	dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
129001d7584cSLiam Girdwood 		stream ? "capture" : "playback", widget->name);
129101d7584cSLiam Girdwood 	return NULL;
129201d7584cSLiam Girdwood }
129301d7584cSLiam Girdwood 
129401d7584cSLiam Girdwood static inline struct snd_soc_dapm_widget *
129537018610SBenoit Cousson 	dai_get_widget(struct snd_soc_dai *dai, int stream)
129601d7584cSLiam Girdwood {
129701d7584cSLiam Girdwood 	if (stream == SNDRV_PCM_STREAM_PLAYBACK)
129837018610SBenoit Cousson 		return dai->playback_widget;
129901d7584cSLiam Girdwood 	else
130037018610SBenoit Cousson 		return dai->capture_widget;
130101d7584cSLiam Girdwood }
130201d7584cSLiam Girdwood 
130301d7584cSLiam Girdwood static int widget_in_list(struct snd_soc_dapm_widget_list *list,
130401d7584cSLiam Girdwood 		struct snd_soc_dapm_widget *widget)
130501d7584cSLiam Girdwood {
130601d7584cSLiam Girdwood 	int i;
130701d7584cSLiam Girdwood 
130801d7584cSLiam Girdwood 	for (i = 0; i < list->num_widgets; i++) {
130901d7584cSLiam Girdwood 		if (widget == list->widgets[i])
131001d7584cSLiam Girdwood 			return 1;
131101d7584cSLiam Girdwood 	}
131201d7584cSLiam Girdwood 
131301d7584cSLiam Girdwood 	return 0;
131401d7584cSLiam Girdwood }
131501d7584cSLiam Girdwood 
13165fdd022cSPiotr Stankiewicz static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
13175fdd022cSPiotr Stankiewicz 		enum snd_soc_dapm_direction dir)
13185fdd022cSPiotr Stankiewicz {
13195fdd022cSPiotr Stankiewicz 	struct snd_soc_card *card = widget->dapm->card;
13205fdd022cSPiotr Stankiewicz 	struct snd_soc_pcm_runtime *rtd;
13210b7990e3SKuninori Morimoto 	struct snd_soc_dai *dai;
13225fdd022cSPiotr Stankiewicz 	int i;
13235fdd022cSPiotr Stankiewicz 
13245fdd022cSPiotr Stankiewicz 	if (dir == SND_SOC_DAPM_DIR_OUT) {
1325bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
13265fdd022cSPiotr Stankiewicz 			if (!rtd->dai_link->no_pcm)
13275fdd022cSPiotr Stankiewicz 				continue;
13285fdd022cSPiotr Stankiewicz 
13295fdd022cSPiotr Stankiewicz 			if (rtd->cpu_dai->playback_widget == widget)
13305fdd022cSPiotr Stankiewicz 				return true;
13315fdd022cSPiotr Stankiewicz 
13320b7990e3SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, dai) {
13335fdd022cSPiotr Stankiewicz 				if (dai->playback_widget == widget)
13345fdd022cSPiotr Stankiewicz 					return true;
13355fdd022cSPiotr Stankiewicz 			}
13365fdd022cSPiotr Stankiewicz 		}
13375fdd022cSPiotr Stankiewicz 	} else { /* SND_SOC_DAPM_DIR_IN */
1338bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
13395fdd022cSPiotr Stankiewicz 			if (!rtd->dai_link->no_pcm)
13405fdd022cSPiotr Stankiewicz 				continue;
13415fdd022cSPiotr Stankiewicz 
13425fdd022cSPiotr Stankiewicz 			if (rtd->cpu_dai->capture_widget == widget)
13435fdd022cSPiotr Stankiewicz 				return true;
13445fdd022cSPiotr Stankiewicz 
13450b7990e3SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, dai) {
13465fdd022cSPiotr Stankiewicz 				if (dai->capture_widget == widget)
13475fdd022cSPiotr Stankiewicz 					return true;
13485fdd022cSPiotr Stankiewicz 			}
13495fdd022cSPiotr Stankiewicz 		}
13505fdd022cSPiotr Stankiewicz 	}
13515fdd022cSPiotr Stankiewicz 
13525fdd022cSPiotr Stankiewicz 	return false;
13535fdd022cSPiotr Stankiewicz }
13545fdd022cSPiotr Stankiewicz 
135523607025SLiam Girdwood int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
13561ce43acfSLars-Peter Clausen 	int stream, struct snd_soc_dapm_widget_list **list)
135701d7584cSLiam Girdwood {
135801d7584cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = fe->cpu_dai;
135901d7584cSLiam Girdwood 	int paths;
136001d7584cSLiam Girdwood 
136101d7584cSLiam Girdwood 	/* get number of valid DAI paths and their widgets */
13626742064aSPiotr Stankiewicz 	paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
13635fdd022cSPiotr Stankiewicz 			dpcm_end_walk_at_be);
136401d7584cSLiam Girdwood 
1365103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
136601d7584cSLiam Girdwood 			stream ? "capture" : "playback");
136701d7584cSLiam Girdwood 
136801d7584cSLiam Girdwood 	return paths;
136901d7584cSLiam Girdwood }
137001d7584cSLiam Girdwood 
137101d7584cSLiam Girdwood static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
137201d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list **list_)
137301d7584cSLiam Girdwood {
137401d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
137501d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list = *list_;
137601d7584cSLiam Girdwood 	struct snd_soc_dapm_widget *widget;
13777afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
137801d7584cSLiam Girdwood 	int prune = 0;
1379bed646dcSKuninori Morimoto 	int do_prune;
138001d7584cSLiam Girdwood 
138101d7584cSLiam Girdwood 	/* Destroy any old FE <--> BE connections */
13828d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
13832e5894d7SBenoit Cousson 		unsigned int i;
138401d7584cSLiam Girdwood 
138501d7584cSLiam Girdwood 		/* is there a valid CPU DAI widget for this BE */
138637018610SBenoit Cousson 		widget = dai_get_widget(dpcm->be->cpu_dai, stream);
138701d7584cSLiam Girdwood 
138801d7584cSLiam Girdwood 		/* prune the BE if it's no longer in our active list */
138901d7584cSLiam Girdwood 		if (widget && widget_in_list(list, widget))
139001d7584cSLiam Girdwood 			continue;
139101d7584cSLiam Girdwood 
139201d7584cSLiam Girdwood 		/* is there a valid CODEC DAI widget for this BE */
1393bed646dcSKuninori Morimoto 		do_prune = 1;
13947afecb30SKuninori Morimoto 		for_each_rtd_codec_dai(dpcm->be, i, dai) {
13952e5894d7SBenoit Cousson 			widget = dai_get_widget(dai, stream);
139601d7584cSLiam Girdwood 
139701d7584cSLiam Girdwood 			/* prune the BE if it's no longer in our active list */
139801d7584cSLiam Girdwood 			if (widget && widget_in_list(list, widget))
1399bed646dcSKuninori Morimoto 				do_prune = 0;
14002e5894d7SBenoit Cousson 		}
1401bed646dcSKuninori Morimoto 		if (!do_prune)
1402bed646dcSKuninori Morimoto 			continue;
140301d7584cSLiam Girdwood 
1404103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
140501d7584cSLiam Girdwood 			stream ? "capture" : "playback",
140601d7584cSLiam Girdwood 			dpcm->be->dai_link->name, fe->dai_link->name);
140701d7584cSLiam Girdwood 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
140801d7584cSLiam Girdwood 		dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
140901d7584cSLiam Girdwood 		prune++;
141001d7584cSLiam Girdwood 	}
141101d7584cSLiam Girdwood 
1412103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
141301d7584cSLiam Girdwood 	return prune;
141401d7584cSLiam Girdwood }
141501d7584cSLiam Girdwood 
141601d7584cSLiam Girdwood static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
141701d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list **list_)
141801d7584cSLiam Girdwood {
141901d7584cSLiam Girdwood 	struct snd_soc_card *card = fe->card;
142001d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list = *list_;
142101d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *be;
142201d7584cSLiam Girdwood 	int i, new = 0, err;
142301d7584cSLiam Girdwood 
142401d7584cSLiam Girdwood 	/* Create any new FE <--> BE connections */
142501d7584cSLiam Girdwood 	for (i = 0; i < list->num_widgets; i++) {
142601d7584cSLiam Girdwood 
14274616274dSMark Brown 		switch (list->widgets[i]->id) {
14284616274dSMark Brown 		case snd_soc_dapm_dai_in:
1429c5b8540dSKoro Chen 			if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1430c5b8540dSKoro Chen 				continue;
1431c5b8540dSKoro Chen 			break;
14324616274dSMark Brown 		case snd_soc_dapm_dai_out:
1433c5b8540dSKoro Chen 			if (stream != SNDRV_PCM_STREAM_CAPTURE)
1434c5b8540dSKoro Chen 				continue;
14354616274dSMark Brown 			break;
14364616274dSMark Brown 		default:
143701d7584cSLiam Girdwood 			continue;
14384616274dSMark Brown 		}
143901d7584cSLiam Girdwood 
144001d7584cSLiam Girdwood 		/* is there a valid BE rtd for this widget */
144101d7584cSLiam Girdwood 		be = dpcm_get_be(card, list->widgets[i], stream);
144201d7584cSLiam Girdwood 		if (!be) {
1443103d84a3SLiam Girdwood 			dev_err(fe->dev, "ASoC: no BE found for %s\n",
144401d7584cSLiam Girdwood 					list->widgets[i]->name);
144501d7584cSLiam Girdwood 			continue;
144601d7584cSLiam Girdwood 		}
144701d7584cSLiam Girdwood 
144801d7584cSLiam Girdwood 		/* make sure BE is a real BE */
144901d7584cSLiam Girdwood 		if (!be->dai_link->no_pcm)
145001d7584cSLiam Girdwood 			continue;
145101d7584cSLiam Girdwood 
145201d7584cSLiam Girdwood 		/* don't connect if FE is not running */
145323607025SLiam Girdwood 		if (!fe->dpcm[stream].runtime && !fe->fe_compr)
145401d7584cSLiam Girdwood 			continue;
145501d7584cSLiam Girdwood 
145601d7584cSLiam Girdwood 		/* newly connected FE and BE */
145701d7584cSLiam Girdwood 		err = dpcm_be_connect(fe, be, stream);
145801d7584cSLiam Girdwood 		if (err < 0) {
1459103d84a3SLiam Girdwood 			dev_err(fe->dev, "ASoC: can't connect %s\n",
146001d7584cSLiam Girdwood 				list->widgets[i]->name);
146101d7584cSLiam Girdwood 			break;
146201d7584cSLiam Girdwood 		} else if (err == 0) /* already connected */
146301d7584cSLiam Girdwood 			continue;
146401d7584cSLiam Girdwood 
146501d7584cSLiam Girdwood 		/* new */
146601d7584cSLiam Girdwood 		be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
146701d7584cSLiam Girdwood 		new++;
146801d7584cSLiam Girdwood 	}
146901d7584cSLiam Girdwood 
1470103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
147101d7584cSLiam Girdwood 	return new;
147201d7584cSLiam Girdwood }
147301d7584cSLiam Girdwood 
147401d7584cSLiam Girdwood /*
147501d7584cSLiam Girdwood  * Find the corresponding BE DAIs that source or sink audio to this
147601d7584cSLiam Girdwood  * FE substream.
147701d7584cSLiam Girdwood  */
147823607025SLiam Girdwood int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
147901d7584cSLiam Girdwood 	int stream, struct snd_soc_dapm_widget_list **list, int new)
148001d7584cSLiam Girdwood {
148101d7584cSLiam Girdwood 	if (new)
148201d7584cSLiam Girdwood 		return dpcm_add_paths(fe, stream, list);
148301d7584cSLiam Girdwood 	else
148401d7584cSLiam Girdwood 		return dpcm_prune_paths(fe, stream, list);
148501d7584cSLiam Girdwood }
148601d7584cSLiam Girdwood 
148723607025SLiam Girdwood void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
148801d7584cSLiam Girdwood {
148901d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
1490a9764869SKaiChieh Chuang 	unsigned long flags;
149101d7584cSLiam Girdwood 
1492a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
14938d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm)
149401d7584cSLiam Girdwood 		dpcm->be->dpcm[stream].runtime_update =
149501d7584cSLiam Girdwood 						SND_SOC_DPCM_UPDATE_NO;
1496a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
149701d7584cSLiam Girdwood }
149801d7584cSLiam Girdwood 
149901d7584cSLiam Girdwood static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
150001d7584cSLiam Girdwood 	int stream)
150101d7584cSLiam Girdwood {
150201d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
150301d7584cSLiam Girdwood 
150401d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
15058d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
150601d7584cSLiam Girdwood 
150701d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
150801d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
150901d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
151001d7584cSLiam Girdwood 
151101d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1512103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
151301d7584cSLiam Girdwood 				stream ? "capture" : "playback",
151401d7584cSLiam Girdwood 				be->dpcm[stream].state);
151501d7584cSLiam Girdwood 
151601d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
151701d7584cSLiam Girdwood 			continue;
151801d7584cSLiam Girdwood 
151901d7584cSLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
152001d7584cSLiam Girdwood 			continue;
152101d7584cSLiam Girdwood 
152201d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
152301d7584cSLiam Girdwood 		be_substream->runtime = NULL;
152401d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
152501d7584cSLiam Girdwood 	}
152601d7584cSLiam Girdwood }
152701d7584cSLiam Girdwood 
152823607025SLiam Girdwood int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
152901d7584cSLiam Girdwood {
153001d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
153101d7584cSLiam Girdwood 	int err, count = 0;
153201d7584cSLiam Girdwood 
153301d7584cSLiam Girdwood 	/* only startup BE DAIs that are either sinks or sources to this FE DAI */
15348d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
153501d7584cSLiam Girdwood 
153601d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
153701d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
153801d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
153901d7584cSLiam Girdwood 
15402062b4c5SRussell King - ARM Linux 		if (!be_substream) {
15412062b4c5SRussell King - ARM Linux 			dev_err(be->dev, "ASoC: no backend %s stream\n",
15422062b4c5SRussell King - ARM Linux 				stream ? "capture" : "playback");
15432062b4c5SRussell King - ARM Linux 			continue;
15442062b4c5SRussell King - ARM Linux 		}
15452062b4c5SRussell King - ARM Linux 
154601d7584cSLiam Girdwood 		/* is this op for this BE ? */
154701d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
154801d7584cSLiam Girdwood 			continue;
154901d7584cSLiam Girdwood 
155001d7584cSLiam Girdwood 		/* first time the dpcm is open ? */
155101d7584cSLiam Girdwood 		if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1552103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: too many users %s at open %d\n",
155301d7584cSLiam Girdwood 				stream ? "capture" : "playback",
155401d7584cSLiam Girdwood 				be->dpcm[stream].state);
155501d7584cSLiam Girdwood 
155601d7584cSLiam Girdwood 		if (be->dpcm[stream].users++ != 0)
155701d7584cSLiam Girdwood 			continue;
155801d7584cSLiam Girdwood 
155901d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
156001d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
156101d7584cSLiam Girdwood 			continue;
156201d7584cSLiam Girdwood 
15632062b4c5SRussell King - ARM Linux 		dev_dbg(be->dev, "ASoC: open %s BE %s\n",
15642062b4c5SRussell King - ARM Linux 			stream ? "capture" : "playback", be->dai_link->name);
156501d7584cSLiam Girdwood 
156601d7584cSLiam Girdwood 		be_substream->runtime = be->dpcm[stream].runtime;
156701d7584cSLiam Girdwood 		err = soc_pcm_open(be_substream);
156801d7584cSLiam Girdwood 		if (err < 0) {
1569103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: BE open failed %d\n", err);
157001d7584cSLiam Girdwood 			be->dpcm[stream].users--;
157101d7584cSLiam Girdwood 			if (be->dpcm[stream].users < 0)
1572103d84a3SLiam Girdwood 				dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
157301d7584cSLiam Girdwood 					stream ? "capture" : "playback",
157401d7584cSLiam Girdwood 					be->dpcm[stream].state);
157501d7584cSLiam Girdwood 
157601d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
157701d7584cSLiam Girdwood 			goto unwind;
157801d7584cSLiam Girdwood 		}
157901d7584cSLiam Girdwood 
158001d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
158101d7584cSLiam Girdwood 		count++;
158201d7584cSLiam Girdwood 	}
158301d7584cSLiam Girdwood 
158401d7584cSLiam Girdwood 	return count;
158501d7584cSLiam Girdwood 
158601d7584cSLiam Girdwood unwind:
158701d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
15888d6258a4SKuninori Morimoto 	for_each_dpcm_be_rollback(fe, stream, dpcm) {
158901d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
159001d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
159101d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
159201d7584cSLiam Girdwood 
159301d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
159401d7584cSLiam Girdwood 			continue;
159501d7584cSLiam Girdwood 
159601d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1597103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close %d\n",
159801d7584cSLiam Girdwood 				stream ? "capture" : "playback",
159901d7584cSLiam Girdwood 				be->dpcm[stream].state);
160001d7584cSLiam Girdwood 
160101d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
160201d7584cSLiam Girdwood 			continue;
160301d7584cSLiam Girdwood 
160401d7584cSLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
160501d7584cSLiam Girdwood 			continue;
160601d7584cSLiam Girdwood 
160701d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
160801d7584cSLiam Girdwood 		be_substream->runtime = NULL;
160901d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
161001d7584cSLiam Girdwood 	}
161101d7584cSLiam Girdwood 
161201d7584cSLiam Girdwood 	return err;
161301d7584cSLiam Girdwood }
161401d7584cSLiam Girdwood 
161508ae9b45SLars-Peter Clausen static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1616435ffb76SJerome Brunet 				 struct snd_soc_pcm_stream *stream)
161708ae9b45SLars-Peter Clausen {
161808ae9b45SLars-Peter Clausen 	runtime->hw.rate_min = stream->rate_min;
1619e33ffbd9SCharles Keepax 	runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
162008ae9b45SLars-Peter Clausen 	runtime->hw.channels_min = stream->channels_min;
162108ae9b45SLars-Peter Clausen 	runtime->hw.channels_max = stream->channels_max;
1622002220a9SLars-Peter Clausen 	if (runtime->hw.formats)
1623435ffb76SJerome Brunet 		runtime->hw.formats &= stream->formats;
1624002220a9SLars-Peter Clausen 	else
1625435ffb76SJerome Brunet 		runtime->hw.formats = stream->formats;
162608ae9b45SLars-Peter Clausen 	runtime->hw.rates = stream->rates;
162708ae9b45SLars-Peter Clausen }
162808ae9b45SLars-Peter Clausen 
1629435ffb76SJerome Brunet static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1630435ffb76SJerome Brunet 				      u64 *formats)
1631b073ed4eSKuninori Morimoto {
1632b073ed4eSKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1633b073ed4eSKuninori Morimoto 	struct snd_soc_dpcm *dpcm;
16347afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
1635b073ed4eSKuninori Morimoto 	int stream = substream->stream;
1636b073ed4eSKuninori Morimoto 
1637b073ed4eSKuninori Morimoto 	if (!fe->dai_link->dpcm_merged_format)
1638435ffb76SJerome Brunet 		return;
1639b073ed4eSKuninori Morimoto 
1640b073ed4eSKuninori Morimoto 	/*
1641b073ed4eSKuninori Morimoto 	 * It returns merged BE codec format
1642b073ed4eSKuninori Morimoto 	 * if FE want to use it (= dpcm_merged_format)
1643b073ed4eSKuninori Morimoto 	 */
1644b073ed4eSKuninori Morimoto 
16458d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1646b073ed4eSKuninori Morimoto 		struct snd_soc_pcm_runtime *be = dpcm->be;
1647b073ed4eSKuninori Morimoto 		struct snd_soc_dai_driver *codec_dai_drv;
1648b073ed4eSKuninori Morimoto 		struct snd_soc_pcm_stream *codec_stream;
1649b073ed4eSKuninori Morimoto 		int i;
1650b073ed4eSKuninori Morimoto 
16517afecb30SKuninori Morimoto 		for_each_rtd_codec_dai(be, i, dai) {
16524febced1SJerome Brunet 			/*
16534febced1SJerome Brunet 			 * Skip CODECs which don't support the current stream
16544febced1SJerome Brunet 			 * type. See soc_pcm_init_runtime_hw() for more details
16554febced1SJerome Brunet 			 */
16567afecb30SKuninori Morimoto 			if (!snd_soc_dai_stream_valid(dai, stream))
16574febced1SJerome Brunet 				continue;
16584febced1SJerome Brunet 
16597afecb30SKuninori Morimoto 			codec_dai_drv = dai->driver;
1660b073ed4eSKuninori Morimoto 			if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1661b073ed4eSKuninori Morimoto 				codec_stream = &codec_dai_drv->playback;
1662b073ed4eSKuninori Morimoto 			else
1663b073ed4eSKuninori Morimoto 				codec_stream = &codec_dai_drv->capture;
1664b073ed4eSKuninori Morimoto 
1665435ffb76SJerome Brunet 			*formats &= codec_stream->formats;
1666435ffb76SJerome Brunet 		}
1667b073ed4eSKuninori Morimoto 	}
1668b073ed4eSKuninori Morimoto }
1669b073ed4eSKuninori Morimoto 
1670435ffb76SJerome Brunet static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1671f4c277b8SJiada Wang 				    unsigned int *channels_min,
1672f4c277b8SJiada Wang 				    unsigned int *channels_max)
1673f4c277b8SJiada Wang {
1674f4c277b8SJiada Wang 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1675f4c277b8SJiada Wang 	struct snd_soc_dpcm *dpcm;
1676f4c277b8SJiada Wang 	int stream = substream->stream;
1677f4c277b8SJiada Wang 
1678f4c277b8SJiada Wang 	if (!fe->dai_link->dpcm_merged_chan)
1679f4c277b8SJiada Wang 		return;
1680f4c277b8SJiada Wang 
1681f4c277b8SJiada Wang 	/*
1682f4c277b8SJiada Wang 	 * It returns merged BE codec channel;
1683f4c277b8SJiada Wang 	 * if FE want to use it (= dpcm_merged_chan)
1684f4c277b8SJiada Wang 	 */
1685f4c277b8SJiada Wang 
16868d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1687f4c277b8SJiada Wang 		struct snd_soc_pcm_runtime *be = dpcm->be;
16884f2bd18bSJerome Brunet 		struct snd_soc_dai_driver *cpu_dai_drv =  be->cpu_dai->driver;
1689f4c277b8SJiada Wang 		struct snd_soc_dai_driver *codec_dai_drv;
1690f4c277b8SJiada Wang 		struct snd_soc_pcm_stream *codec_stream;
16914f2bd18bSJerome Brunet 		struct snd_soc_pcm_stream *cpu_stream;
1692f4c277b8SJiada Wang 
16934f2bd18bSJerome Brunet 		if (stream == SNDRV_PCM_STREAM_PLAYBACK)
16944f2bd18bSJerome Brunet 			cpu_stream = &cpu_dai_drv->playback;
16954f2bd18bSJerome Brunet 		else
16964f2bd18bSJerome Brunet 			cpu_stream = &cpu_dai_drv->capture;
16974f2bd18bSJerome Brunet 
16984f2bd18bSJerome Brunet 		*channels_min = max(*channels_min, cpu_stream->channels_min);
16994f2bd18bSJerome Brunet 		*channels_max = min(*channels_max, cpu_stream->channels_max);
17004f2bd18bSJerome Brunet 
17014f2bd18bSJerome Brunet 		/*
17024f2bd18bSJerome Brunet 		 * chan min/max cannot be enforced if there are multiple CODEC
17034f2bd18bSJerome Brunet 		 * DAIs connected to a single CPU DAI, use CPU DAI's directly
17044f2bd18bSJerome Brunet 		 */
17054f2bd18bSJerome Brunet 		if (be->num_codecs == 1) {
17064f2bd18bSJerome Brunet 			codec_dai_drv = be->codec_dais[0]->driver;
17074f2bd18bSJerome Brunet 
1708f4c277b8SJiada Wang 			if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1709f4c277b8SJiada Wang 				codec_stream = &codec_dai_drv->playback;
1710f4c277b8SJiada Wang 			else
1711f4c277b8SJiada Wang 				codec_stream = &codec_dai_drv->capture;
1712f4c277b8SJiada Wang 
1713f4c277b8SJiada Wang 			*channels_min = max(*channels_min,
1714f4c277b8SJiada Wang 					    codec_stream->channels_min);
1715f4c277b8SJiada Wang 			*channels_max = min(*channels_max,
1716f4c277b8SJiada Wang 					    codec_stream->channels_max);
1717f4c277b8SJiada Wang 		}
1718f4c277b8SJiada Wang 	}
1719f4c277b8SJiada Wang }
1720f4c277b8SJiada Wang 
1721baacd8d1SJerome Brunet static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1722baacd8d1SJerome Brunet 				    unsigned int *rates,
1723baacd8d1SJerome Brunet 				    unsigned int *rate_min,
1724baacd8d1SJerome Brunet 				    unsigned int *rate_max)
1725baacd8d1SJerome Brunet {
1726baacd8d1SJerome Brunet 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1727baacd8d1SJerome Brunet 	struct snd_soc_dpcm *dpcm;
1728baacd8d1SJerome Brunet 	int stream = substream->stream;
1729baacd8d1SJerome Brunet 
1730baacd8d1SJerome Brunet 	if (!fe->dai_link->dpcm_merged_rate)
1731baacd8d1SJerome Brunet 		return;
1732baacd8d1SJerome Brunet 
1733baacd8d1SJerome Brunet 	/*
1734baacd8d1SJerome Brunet 	 * It returns merged BE codec channel;
1735baacd8d1SJerome Brunet 	 * if FE want to use it (= dpcm_merged_chan)
1736baacd8d1SJerome Brunet 	 */
1737baacd8d1SJerome Brunet 
17388d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1739baacd8d1SJerome Brunet 		struct snd_soc_pcm_runtime *be = dpcm->be;
1740baacd8d1SJerome Brunet 		struct snd_soc_dai_driver *cpu_dai_drv =  be->cpu_dai->driver;
1741baacd8d1SJerome Brunet 		struct snd_soc_dai_driver *codec_dai_drv;
1742baacd8d1SJerome Brunet 		struct snd_soc_pcm_stream *codec_stream;
1743baacd8d1SJerome Brunet 		struct snd_soc_pcm_stream *cpu_stream;
17447afecb30SKuninori Morimoto 		struct snd_soc_dai *dai;
1745baacd8d1SJerome Brunet 		int i;
1746baacd8d1SJerome Brunet 
1747baacd8d1SJerome Brunet 		if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1748baacd8d1SJerome Brunet 			cpu_stream = &cpu_dai_drv->playback;
1749baacd8d1SJerome Brunet 		else
1750baacd8d1SJerome Brunet 			cpu_stream = &cpu_dai_drv->capture;
1751baacd8d1SJerome Brunet 
1752baacd8d1SJerome Brunet 		*rate_min = max(*rate_min, cpu_stream->rate_min);
1753baacd8d1SJerome Brunet 		*rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1754baacd8d1SJerome Brunet 		*rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1755baacd8d1SJerome Brunet 
17567afecb30SKuninori Morimoto 		for_each_rtd_codec_dai(be, i, dai) {
1757baacd8d1SJerome Brunet 			/*
1758baacd8d1SJerome Brunet 			 * Skip CODECs which don't support the current stream
1759baacd8d1SJerome Brunet 			 * type. See soc_pcm_init_runtime_hw() for more details
1760baacd8d1SJerome Brunet 			 */
17617afecb30SKuninori Morimoto 			if (!snd_soc_dai_stream_valid(dai, stream))
1762baacd8d1SJerome Brunet 				continue;
1763baacd8d1SJerome Brunet 
17647afecb30SKuninori Morimoto 			codec_dai_drv = dai->driver;
1765baacd8d1SJerome Brunet 			if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1766baacd8d1SJerome Brunet 				codec_stream = &codec_dai_drv->playback;
1767baacd8d1SJerome Brunet 			else
1768baacd8d1SJerome Brunet 				codec_stream = &codec_dai_drv->capture;
1769baacd8d1SJerome Brunet 
1770baacd8d1SJerome Brunet 			*rate_min = max(*rate_min, codec_stream->rate_min);
1771baacd8d1SJerome Brunet 			*rate_max = min_not_zero(*rate_max,
1772baacd8d1SJerome Brunet 						 codec_stream->rate_max);
1773baacd8d1SJerome Brunet 			*rates = snd_pcm_rate_mask_intersect(*rates,
1774baacd8d1SJerome Brunet 						codec_stream->rates);
1775baacd8d1SJerome Brunet 		}
1776baacd8d1SJerome Brunet 	}
1777baacd8d1SJerome Brunet }
1778baacd8d1SJerome Brunet 
177945c0a188SMark Brown static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
178001d7584cSLiam Girdwood {
178101d7584cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
178201d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
178301d7584cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
178401d7584cSLiam Girdwood 	struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
178501d7584cSLiam Girdwood 
178608ae9b45SLars-Peter Clausen 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1787435ffb76SJerome Brunet 		dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
178808ae9b45SLars-Peter Clausen 	else
1789435ffb76SJerome Brunet 		dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1790f4c277b8SJiada Wang 
1791435ffb76SJerome Brunet 	dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1792435ffb76SJerome Brunet 	dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1793435ffb76SJerome Brunet 				&runtime->hw.channels_max);
1794baacd8d1SJerome Brunet 	dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1795baacd8d1SJerome Brunet 				&runtime->hw.rate_min, &runtime->hw.rate_max);
179601d7584cSLiam Girdwood }
179701d7584cSLiam Girdwood 
1798ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1799ea9d0d77STakashi Iwai 
1800ea9d0d77STakashi Iwai /* Set FE's runtime_update state; the state is protected via PCM stream lock
1801ea9d0d77STakashi Iwai  * for avoiding the race with trigger callback.
1802ea9d0d77STakashi Iwai  * If the state is unset and a trigger is pending while the previous operation,
1803ea9d0d77STakashi Iwai  * process the pending trigger action here.
1804ea9d0d77STakashi Iwai  */
1805ea9d0d77STakashi Iwai static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1806ea9d0d77STakashi Iwai 				     int stream, enum snd_soc_dpcm_update state)
1807ea9d0d77STakashi Iwai {
1808ea9d0d77STakashi Iwai 	struct snd_pcm_substream *substream =
1809ea9d0d77STakashi Iwai 		snd_soc_dpcm_get_substream(fe, stream);
1810ea9d0d77STakashi Iwai 
1811ea9d0d77STakashi Iwai 	snd_pcm_stream_lock_irq(substream);
1812ea9d0d77STakashi Iwai 	if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1813ea9d0d77STakashi Iwai 		dpcm_fe_dai_do_trigger(substream,
1814ea9d0d77STakashi Iwai 				       fe->dpcm[stream].trigger_pending - 1);
1815ea9d0d77STakashi Iwai 		fe->dpcm[stream].trigger_pending = 0;
1816ea9d0d77STakashi Iwai 	}
1817ea9d0d77STakashi Iwai 	fe->dpcm[stream].runtime_update = state;
1818ea9d0d77STakashi Iwai 	snd_pcm_stream_unlock_irq(substream);
1819ea9d0d77STakashi Iwai }
1820ea9d0d77STakashi Iwai 
1821906c7d69SPC Liao static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1822906c7d69SPC Liao 			       int stream)
1823906c7d69SPC Liao {
1824906c7d69SPC Liao 	struct snd_soc_dpcm *dpcm;
1825906c7d69SPC Liao 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1826906c7d69SPC Liao 	struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1827906c7d69SPC Liao 	int err;
1828906c7d69SPC Liao 
1829906c7d69SPC Liao 	/* apply symmetry for FE */
1830906c7d69SPC Liao 	if (soc_pcm_has_symmetry(fe_substream))
1831906c7d69SPC Liao 		fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1832906c7d69SPC Liao 
1833906c7d69SPC Liao 	/* Symmetry only applies if we've got an active stream. */
1834906c7d69SPC Liao 	if (fe_cpu_dai->active) {
1835906c7d69SPC Liao 		err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1836906c7d69SPC Liao 		if (err < 0)
1837906c7d69SPC Liao 			return err;
1838906c7d69SPC Liao 	}
1839906c7d69SPC Liao 
1840906c7d69SPC Liao 	/* apply symmetry for BE */
18418d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1842906c7d69SPC Liao 		struct snd_soc_pcm_runtime *be = dpcm->be;
1843906c7d69SPC Liao 		struct snd_pcm_substream *be_substream =
1844906c7d69SPC Liao 			snd_soc_dpcm_get_substream(be, stream);
18456246f283SJerome Brunet 		struct snd_soc_pcm_runtime *rtd;
18460b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
1847906c7d69SPC Liao 		int i;
1848906c7d69SPC Liao 
18496246f283SJerome Brunet 		/* A backend may not have the requested substream */
18506246f283SJerome Brunet 		if (!be_substream)
18516246f283SJerome Brunet 			continue;
18526246f283SJerome Brunet 
18536246f283SJerome Brunet 		rtd = be_substream->private_data;
1854f1176614SJeeja KP 		if (rtd->dai_link->be_hw_params_fixup)
1855f1176614SJeeja KP 			continue;
1856f1176614SJeeja KP 
1857906c7d69SPC Liao 		if (soc_pcm_has_symmetry(be_substream))
1858906c7d69SPC Liao 			be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1859906c7d69SPC Liao 
1860906c7d69SPC Liao 		/* Symmetry only applies if we've got an active stream. */
1861906c7d69SPC Liao 		if (rtd->cpu_dai->active) {
186299bcedbdSKai Chieh Chuang 			err = soc_pcm_apply_symmetry(fe_substream,
186399bcedbdSKai Chieh Chuang 						     rtd->cpu_dai);
1864906c7d69SPC Liao 			if (err < 0)
1865906c7d69SPC Liao 				return err;
1866906c7d69SPC Liao 		}
1867906c7d69SPC Liao 
18680b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
18690b7990e3SKuninori Morimoto 			if (codec_dai->active) {
187099bcedbdSKai Chieh Chuang 				err = soc_pcm_apply_symmetry(fe_substream,
18710b7990e3SKuninori Morimoto 							     codec_dai);
1872906c7d69SPC Liao 				if (err < 0)
1873906c7d69SPC Liao 					return err;
1874906c7d69SPC Liao 			}
1875906c7d69SPC Liao 		}
1876906c7d69SPC Liao 	}
1877906c7d69SPC Liao 
1878906c7d69SPC Liao 	return 0;
1879906c7d69SPC Liao }
1880906c7d69SPC Liao 
188101d7584cSLiam Girdwood static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
188201d7584cSLiam Girdwood {
188301d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
188401d7584cSLiam Girdwood 	struct snd_pcm_runtime *runtime = fe_substream->runtime;
188501d7584cSLiam Girdwood 	int stream = fe_substream->stream, ret = 0;
188601d7584cSLiam Girdwood 
1887ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
188801d7584cSLiam Girdwood 
188901d7584cSLiam Girdwood 	ret = dpcm_be_dai_startup(fe, fe_substream->stream);
189001d7584cSLiam Girdwood 	if (ret < 0) {
1891103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
189201d7584cSLiam Girdwood 		goto be_err;
189301d7584cSLiam Girdwood 	}
189401d7584cSLiam Girdwood 
1895103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
189601d7584cSLiam Girdwood 
189701d7584cSLiam Girdwood 	/* start the DAI frontend */
189801d7584cSLiam Girdwood 	ret = soc_pcm_open(fe_substream);
189901d7584cSLiam Girdwood 	if (ret < 0) {
1900103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
190101d7584cSLiam Girdwood 		goto unwind;
190201d7584cSLiam Girdwood 	}
190301d7584cSLiam Girdwood 
190401d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
190501d7584cSLiam Girdwood 
190601d7584cSLiam Girdwood 	dpcm_set_fe_runtime(fe_substream);
190701d7584cSLiam Girdwood 	snd_pcm_limit_hw_rates(runtime);
190801d7584cSLiam Girdwood 
1909906c7d69SPC Liao 	ret = dpcm_apply_symmetry(fe_substream, stream);
1910906c7d69SPC Liao 	if (ret < 0) {
1911906c7d69SPC Liao 		dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1912906c7d69SPC Liao 			ret);
1913906c7d69SPC Liao 		goto unwind;
1914906c7d69SPC Liao 	}
1915906c7d69SPC Liao 
1916ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
191701d7584cSLiam Girdwood 	return 0;
191801d7584cSLiam Girdwood 
191901d7584cSLiam Girdwood unwind:
192001d7584cSLiam Girdwood 	dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
192101d7584cSLiam Girdwood be_err:
1922ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
192301d7584cSLiam Girdwood 	return ret;
192401d7584cSLiam Girdwood }
192501d7584cSLiam Girdwood 
192623607025SLiam Girdwood int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
192701d7584cSLiam Girdwood {
192801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
192901d7584cSLiam Girdwood 
193001d7584cSLiam Girdwood 	/* only shutdown BEs that are either sinks or sources to this FE DAI */
19318d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
193201d7584cSLiam Girdwood 
193301d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
193401d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
193501d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
193601d7584cSLiam Girdwood 
193701d7584cSLiam Girdwood 		/* is this op for this BE ? */
193801d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
193901d7584cSLiam Girdwood 			continue;
194001d7584cSLiam Girdwood 
194101d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1942103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
194301d7584cSLiam Girdwood 				stream ? "capture" : "playback",
194401d7584cSLiam Girdwood 				be->dpcm[stream].state);
194501d7584cSLiam Girdwood 
194601d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
194701d7584cSLiam Girdwood 			continue;
194801d7584cSLiam Girdwood 
194901d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
19509c0ac70aSKai Chieh Chuang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
19519c0ac70aSKai Chieh Chuang 			soc_pcm_hw_free(be_substream);
19529c0ac70aSKai Chieh Chuang 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
19539c0ac70aSKai Chieh Chuang 		}
195401d7584cSLiam Girdwood 
1955103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: close BE %s\n",
195694d215ccS彭东林 			be->dai_link->name);
195701d7584cSLiam Girdwood 
195801d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
195901d7584cSLiam Girdwood 		be_substream->runtime = NULL;
196001d7584cSLiam Girdwood 
196101d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
196201d7584cSLiam Girdwood 	}
196301d7584cSLiam Girdwood 	return 0;
196401d7584cSLiam Girdwood }
196501d7584cSLiam Girdwood 
196601d7584cSLiam Girdwood static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
196701d7584cSLiam Girdwood {
196801d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
196901d7584cSLiam Girdwood 	int stream = substream->stream;
197001d7584cSLiam Girdwood 
1971ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
197201d7584cSLiam Girdwood 
197301d7584cSLiam Girdwood 	/* shutdown the BEs */
197401d7584cSLiam Girdwood 	dpcm_be_dai_shutdown(fe, substream->stream);
197501d7584cSLiam Girdwood 
1976103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
197701d7584cSLiam Girdwood 
197801d7584cSLiam Girdwood 	/* now shutdown the frontend */
197901d7584cSLiam Girdwood 	soc_pcm_close(substream);
198001d7584cSLiam Girdwood 
198101d7584cSLiam Girdwood 	/* run the stream event for each BE */
1982b0edff42SKuninori Morimoto 	snd_soc_dapm_stream_stop(fe, stream);
198301d7584cSLiam Girdwood 
198401d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1985ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
198601d7584cSLiam Girdwood 	return 0;
198701d7584cSLiam Girdwood }
198801d7584cSLiam Girdwood 
198923607025SLiam Girdwood int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
199001d7584cSLiam Girdwood {
199101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
199201d7584cSLiam Girdwood 
199301d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
199401d7584cSLiam Girdwood 	 * to this frontend DAI */
19958d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
199601d7584cSLiam Girdwood 
199701d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
199801d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
199901d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
200001d7584cSLiam Girdwood 
200101d7584cSLiam Girdwood 		/* is this op for this BE ? */
200201d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
200301d7584cSLiam Girdwood 			continue;
200401d7584cSLiam Girdwood 
200501d7584cSLiam Girdwood 		/* only free hw when no longer used - check all FEs */
200601d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
200701d7584cSLiam Girdwood 				continue;
200801d7584cSLiam Girdwood 
200936fba62cSQiao Zhou 		/* do not free hw if this BE is used by other FE */
201036fba62cSQiao Zhou 		if (be->dpcm[stream].users > 1)
201136fba62cSQiao Zhou 			continue;
201236fba62cSQiao Zhou 
201301d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
201401d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
201501d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
201608b27848SPatrick Lai 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
20175e82d2beSVinod Koul 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
20185e82d2beSVinod Koul 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
201901d7584cSLiam Girdwood 			continue;
202001d7584cSLiam Girdwood 
2021103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
202294d215ccS彭东林 			be->dai_link->name);
202301d7584cSLiam Girdwood 
202401d7584cSLiam Girdwood 		soc_pcm_hw_free(be_substream);
202501d7584cSLiam Girdwood 
202601d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
202701d7584cSLiam Girdwood 	}
202801d7584cSLiam Girdwood 
202901d7584cSLiam Girdwood 	return 0;
203001d7584cSLiam Girdwood }
203101d7584cSLiam Girdwood 
203245c0a188SMark Brown static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
203301d7584cSLiam Girdwood {
203401d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
203501d7584cSLiam Girdwood 	int err, stream = substream->stream;
203601d7584cSLiam Girdwood 
203701d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2038ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
203901d7584cSLiam Girdwood 
2040103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
204101d7584cSLiam Girdwood 
204201d7584cSLiam Girdwood 	/* call hw_free on the frontend */
204301d7584cSLiam Girdwood 	err = soc_pcm_hw_free(substream);
204401d7584cSLiam Girdwood 	if (err < 0)
2045103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
204601d7584cSLiam Girdwood 			fe->dai_link->name);
204701d7584cSLiam Girdwood 
204801d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
204901d7584cSLiam Girdwood 	 * to this frontend DAI */
205001d7584cSLiam Girdwood 	err = dpcm_be_dai_hw_free(fe, stream);
205101d7584cSLiam Girdwood 
205201d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2053ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
205401d7584cSLiam Girdwood 
205501d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
205601d7584cSLiam Girdwood 	return 0;
205701d7584cSLiam Girdwood }
205801d7584cSLiam Girdwood 
205923607025SLiam Girdwood int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
206001d7584cSLiam Girdwood {
206101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
206201d7584cSLiam Girdwood 	int ret;
206301d7584cSLiam Girdwood 
20648d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
206501d7584cSLiam Girdwood 
206601d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
206701d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
206801d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
206901d7584cSLiam Girdwood 
207001d7584cSLiam Girdwood 		/* is this op for this BE ? */
207101d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
207201d7584cSLiam Girdwood 			continue;
207301d7584cSLiam Girdwood 
207401d7584cSLiam Girdwood 		/* copy params for each dpcm */
207501d7584cSLiam Girdwood 		memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
207601d7584cSLiam Girdwood 				sizeof(struct snd_pcm_hw_params));
207701d7584cSLiam Girdwood 
207801d7584cSLiam Girdwood 		/* perform any hw_params fixups */
207901d7584cSLiam Girdwood 		if (be->dai_link->be_hw_params_fixup) {
208001d7584cSLiam Girdwood 			ret = be->dai_link->be_hw_params_fixup(be,
208101d7584cSLiam Girdwood 					&dpcm->hw_params);
208201d7584cSLiam Girdwood 			if (ret < 0) {
208301d7584cSLiam Girdwood 				dev_err(be->dev,
2084103d84a3SLiam Girdwood 					"ASoC: hw_params BE fixup failed %d\n",
208501d7584cSLiam Girdwood 					ret);
208601d7584cSLiam Girdwood 				goto unwind;
208701d7584cSLiam Girdwood 			}
208801d7584cSLiam Girdwood 		}
208901d7584cSLiam Girdwood 
2090ae061d2aSLibin Yang 		/* copy the fixed-up hw params for BE dai */
2091ae061d2aSLibin Yang 		memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2092ae061d2aSLibin Yang 		       sizeof(struct snd_pcm_hw_params));
2093ae061d2aSLibin Yang 
2094b0639bd2SKuninori Morimoto 		/* only allow hw_params() if no connected FEs are running */
2095b0639bd2SKuninori Morimoto 		if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2096b0639bd2SKuninori Morimoto 			continue;
2097b0639bd2SKuninori Morimoto 
2098b0639bd2SKuninori Morimoto 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2099b0639bd2SKuninori Morimoto 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2100b0639bd2SKuninori Morimoto 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2101b0639bd2SKuninori Morimoto 			continue;
2102b0639bd2SKuninori Morimoto 
2103b0639bd2SKuninori Morimoto 		dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
210494d215ccS彭东林 			be->dai_link->name);
2105b0639bd2SKuninori Morimoto 
210601d7584cSLiam Girdwood 		ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
210701d7584cSLiam Girdwood 		if (ret < 0) {
210801d7584cSLiam Girdwood 			dev_err(dpcm->be->dev,
2109103d84a3SLiam Girdwood 				"ASoC: hw_params BE failed %d\n", ret);
211001d7584cSLiam Girdwood 			goto unwind;
211101d7584cSLiam Girdwood 		}
211201d7584cSLiam Girdwood 
211301d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
211401d7584cSLiam Girdwood 	}
211501d7584cSLiam Girdwood 	return 0;
211601d7584cSLiam Girdwood 
211701d7584cSLiam Girdwood unwind:
211801d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
21198d6258a4SKuninori Morimoto 	for_each_dpcm_be_rollback(fe, stream, dpcm) {
212001d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
212101d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
212201d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
212301d7584cSLiam Girdwood 
212401d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
212501d7584cSLiam Girdwood 			continue;
212601d7584cSLiam Girdwood 
212701d7584cSLiam Girdwood 		/* only allow hw_free() if no connected FEs are running */
212801d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
212901d7584cSLiam Girdwood 			continue;
213001d7584cSLiam Girdwood 
213101d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
213201d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
213301d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
213401d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
213501d7584cSLiam Girdwood 			continue;
213601d7584cSLiam Girdwood 
213701d7584cSLiam Girdwood 		soc_pcm_hw_free(be_substream);
213801d7584cSLiam Girdwood 	}
213901d7584cSLiam Girdwood 
214001d7584cSLiam Girdwood 	return ret;
214101d7584cSLiam Girdwood }
214201d7584cSLiam Girdwood 
214345c0a188SMark Brown static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
214401d7584cSLiam Girdwood 				 struct snd_pcm_hw_params *params)
214501d7584cSLiam Girdwood {
214601d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
214701d7584cSLiam Girdwood 	int ret, stream = substream->stream;
214801d7584cSLiam Girdwood 
214901d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2150ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
215101d7584cSLiam Girdwood 
215201d7584cSLiam Girdwood 	memcpy(&fe->dpcm[substream->stream].hw_params, params,
215301d7584cSLiam Girdwood 			sizeof(struct snd_pcm_hw_params));
215401d7584cSLiam Girdwood 	ret = dpcm_be_dai_hw_params(fe, substream->stream);
215501d7584cSLiam Girdwood 	if (ret < 0) {
2156103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
215701d7584cSLiam Girdwood 		goto out;
215801d7584cSLiam Girdwood 	}
215901d7584cSLiam Girdwood 
2160103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
216101d7584cSLiam Girdwood 			fe->dai_link->name, params_rate(params),
216201d7584cSLiam Girdwood 			params_channels(params), params_format(params));
216301d7584cSLiam Girdwood 
216401d7584cSLiam Girdwood 	/* call hw_params on the frontend */
216501d7584cSLiam Girdwood 	ret = soc_pcm_hw_params(substream, params);
216601d7584cSLiam Girdwood 	if (ret < 0) {
2167103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
216801d7584cSLiam Girdwood 		dpcm_be_dai_hw_free(fe, stream);
216901d7584cSLiam Girdwood 	 } else
217001d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
217101d7584cSLiam Girdwood 
217201d7584cSLiam Girdwood out:
2173ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
217401d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
217501d7584cSLiam Girdwood 	return ret;
217601d7584cSLiam Girdwood }
217701d7584cSLiam Girdwood 
217801d7584cSLiam Girdwood static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
217901d7584cSLiam Girdwood 		struct snd_pcm_substream *substream, int cmd)
218001d7584cSLiam Girdwood {
218101d7584cSLiam Girdwood 	int ret;
218201d7584cSLiam Girdwood 
2183103d84a3SLiam Girdwood 	dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
218494d215ccS彭东林 			dpcm->be->dai_link->name, cmd);
218501d7584cSLiam Girdwood 
218601d7584cSLiam Girdwood 	ret = soc_pcm_trigger(substream, cmd);
218701d7584cSLiam Girdwood 	if (ret < 0)
2188103d84a3SLiam Girdwood 		dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
218901d7584cSLiam Girdwood 
219001d7584cSLiam Girdwood 	return ret;
219101d7584cSLiam Girdwood }
219201d7584cSLiam Girdwood 
219323607025SLiam Girdwood int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
219445c0a188SMark Brown 			       int cmd)
219501d7584cSLiam Girdwood {
219601d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
219701d7584cSLiam Girdwood 	int ret = 0;
219801d7584cSLiam Girdwood 
21998d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
220001d7584cSLiam Girdwood 
220101d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
220201d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
220301d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
220401d7584cSLiam Girdwood 
220501d7584cSLiam Girdwood 		/* is this op for this BE ? */
220601d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
220701d7584cSLiam Girdwood 			continue;
220801d7584cSLiam Girdwood 
220901d7584cSLiam Girdwood 		switch (cmd) {
221001d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_START:
221101d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
221201d7584cSLiam Girdwood 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
221301d7584cSLiam Girdwood 				continue;
221401d7584cSLiam Girdwood 
221501d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
221601d7584cSLiam Girdwood 			if (ret)
221701d7584cSLiam Girdwood 				return ret;
221801d7584cSLiam Girdwood 
221901d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
222001d7584cSLiam Girdwood 			break;
222101d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_RESUME:
222201d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
222301d7584cSLiam Girdwood 				continue;
222401d7584cSLiam Girdwood 
222501d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
222601d7584cSLiam Girdwood 			if (ret)
222701d7584cSLiam Girdwood 				return ret;
222801d7584cSLiam Girdwood 
222901d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
223001d7584cSLiam Girdwood 			break;
223101d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
223201d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
223301d7584cSLiam Girdwood 				continue;
223401d7584cSLiam Girdwood 
223501d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
223601d7584cSLiam Girdwood 			if (ret)
223701d7584cSLiam Girdwood 				return ret;
223801d7584cSLiam Girdwood 
223901d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
224001d7584cSLiam Girdwood 			break;
224101d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_STOP:
224201d7584cSLiam Girdwood 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
224301d7584cSLiam Girdwood 				continue;
224401d7584cSLiam Girdwood 
224501d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
224601d7584cSLiam Girdwood 				continue;
224701d7584cSLiam Girdwood 
224801d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
224901d7584cSLiam Girdwood 			if (ret)
225001d7584cSLiam Girdwood 				return ret;
225101d7584cSLiam Girdwood 
225201d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
225301d7584cSLiam Girdwood 			break;
225401d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_SUSPEND:
2255868a6ca8SNicolin Chen 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
225601d7584cSLiam Girdwood 				continue;
225701d7584cSLiam Girdwood 
225801d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
225901d7584cSLiam Girdwood 				continue;
226001d7584cSLiam Girdwood 
226101d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
226201d7584cSLiam Girdwood 			if (ret)
226301d7584cSLiam Girdwood 				return ret;
226401d7584cSLiam Girdwood 
226501d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
226601d7584cSLiam Girdwood 			break;
226701d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
226801d7584cSLiam Girdwood 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
226901d7584cSLiam Girdwood 				continue;
227001d7584cSLiam Girdwood 
227101d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
227201d7584cSLiam Girdwood 				continue;
227301d7584cSLiam Girdwood 
227401d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
227501d7584cSLiam Girdwood 			if (ret)
227601d7584cSLiam Girdwood 				return ret;
227701d7584cSLiam Girdwood 
227801d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
227901d7584cSLiam Girdwood 			break;
228001d7584cSLiam Girdwood 		}
228101d7584cSLiam Girdwood 	}
228201d7584cSLiam Girdwood 
228301d7584cSLiam Girdwood 	return ret;
228401d7584cSLiam Girdwood }
228501d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
228601d7584cSLiam Girdwood 
2287acbf2774SRanjani Sridharan static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2288acbf2774SRanjani Sridharan 				  int cmd, bool fe_first)
2289acbf2774SRanjani Sridharan {
2290acbf2774SRanjani Sridharan 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2291acbf2774SRanjani Sridharan 	int ret;
2292acbf2774SRanjani Sridharan 
2293acbf2774SRanjani Sridharan 	/* call trigger on the frontend before the backend. */
2294acbf2774SRanjani Sridharan 	if (fe_first) {
2295acbf2774SRanjani Sridharan 		dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2296acbf2774SRanjani Sridharan 			fe->dai_link->name, cmd);
2297acbf2774SRanjani Sridharan 
2298acbf2774SRanjani Sridharan 		ret = soc_pcm_trigger(substream, cmd);
2299acbf2774SRanjani Sridharan 		if (ret < 0)
2300acbf2774SRanjani Sridharan 			return ret;
2301acbf2774SRanjani Sridharan 
2302acbf2774SRanjani Sridharan 		ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2303acbf2774SRanjani Sridharan 		return ret;
2304acbf2774SRanjani Sridharan 	}
2305acbf2774SRanjani Sridharan 
2306acbf2774SRanjani Sridharan 	/* call trigger on the frontend after the backend. */
2307acbf2774SRanjani Sridharan 	ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2308acbf2774SRanjani Sridharan 	if (ret < 0)
2309acbf2774SRanjani Sridharan 		return ret;
2310acbf2774SRanjani Sridharan 
2311acbf2774SRanjani Sridharan 	dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2312acbf2774SRanjani Sridharan 		fe->dai_link->name, cmd);
2313acbf2774SRanjani Sridharan 
2314acbf2774SRanjani Sridharan 	ret = soc_pcm_trigger(substream, cmd);
2315acbf2774SRanjani Sridharan 
2316acbf2774SRanjani Sridharan 	return ret;
2317acbf2774SRanjani Sridharan }
2318acbf2774SRanjani Sridharan 
2319ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
232001d7584cSLiam Girdwood {
232101d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2322acbf2774SRanjani Sridharan 	int stream = substream->stream;
2323acbf2774SRanjani Sridharan 	int ret = 0;
232401d7584cSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
232501d7584cSLiam Girdwood 
232601d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
232701d7584cSLiam Girdwood 
232801d7584cSLiam Girdwood 	switch (trigger) {
232901d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_PRE:
2330acbf2774SRanjani Sridharan 		switch (cmd) {
2331acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_START:
2332acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_RESUME:
2333acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2334acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2335acbf2774SRanjani Sridharan 			break;
2336acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_STOP:
2337acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_SUSPEND:
2338acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2339acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2340acbf2774SRanjani Sridharan 			break;
2341acbf2774SRanjani Sridharan 		default:
2342acbf2774SRanjani Sridharan 			ret = -EINVAL;
2343acbf2774SRanjani Sridharan 			break;
234401d7584cSLiam Girdwood 		}
234501d7584cSLiam Girdwood 		break;
234601d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_POST:
2347acbf2774SRanjani Sridharan 		switch (cmd) {
2348acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_START:
2349acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_RESUME:
2350acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2351acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2352acbf2774SRanjani Sridharan 			break;
2353acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_STOP:
2354acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_SUSPEND:
2355acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2356acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2357acbf2774SRanjani Sridharan 			break;
2358acbf2774SRanjani Sridharan 		default:
2359acbf2774SRanjani Sridharan 			ret = -EINVAL;
2360acbf2774SRanjani Sridharan 			break;
236101d7584cSLiam Girdwood 		}
236201d7584cSLiam Girdwood 		break;
236307bf84aaSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_BESPOKE:
236407bf84aaSLiam Girdwood 		/* bespoke trigger() - handles both FE and BEs */
236507bf84aaSLiam Girdwood 
2366103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
236707bf84aaSLiam Girdwood 				fe->dai_link->name, cmd);
236807bf84aaSLiam Girdwood 
236907bf84aaSLiam Girdwood 		ret = soc_pcm_bespoke_trigger(substream, cmd);
237007bf84aaSLiam Girdwood 		break;
237101d7584cSLiam Girdwood 	default:
2372103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
237301d7584cSLiam Girdwood 				fe->dai_link->name);
237401d7584cSLiam Girdwood 		ret = -EINVAL;
237501d7584cSLiam Girdwood 		goto out;
237601d7584cSLiam Girdwood 	}
237701d7584cSLiam Girdwood 
2378acbf2774SRanjani Sridharan 	if (ret < 0) {
2379acbf2774SRanjani Sridharan 		dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2380acbf2774SRanjani Sridharan 			cmd, ret);
2381acbf2774SRanjani Sridharan 		goto out;
2382acbf2774SRanjani Sridharan 	}
2383acbf2774SRanjani Sridharan 
238401d7584cSLiam Girdwood 	switch (cmd) {
238501d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
238601d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_RESUME:
238701d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
238801d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
238901d7584cSLiam Girdwood 		break;
239001d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_STOP:
239101d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_SUSPEND:
239201d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
239301d7584cSLiam Girdwood 		break;
23949f169b9fSPatrick Lai 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
23959f169b9fSPatrick Lai 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
23969f169b9fSPatrick Lai 		break;
239701d7584cSLiam Girdwood 	}
239801d7584cSLiam Girdwood 
239901d7584cSLiam Girdwood out:
240001d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
240101d7584cSLiam Girdwood 	return ret;
240201d7584cSLiam Girdwood }
240301d7584cSLiam Girdwood 
2404ea9d0d77STakashi Iwai static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2405ea9d0d77STakashi Iwai {
2406ea9d0d77STakashi Iwai 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2407ea9d0d77STakashi Iwai 	int stream = substream->stream;
2408ea9d0d77STakashi Iwai 
2409ea9d0d77STakashi Iwai 	/* if FE's runtime_update is already set, we're in race;
2410ea9d0d77STakashi Iwai 	 * process this trigger later at exit
2411ea9d0d77STakashi Iwai 	 */
2412ea9d0d77STakashi Iwai 	if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2413ea9d0d77STakashi Iwai 		fe->dpcm[stream].trigger_pending = cmd + 1;
2414ea9d0d77STakashi Iwai 		return 0; /* delayed, assuming it's successful */
2415ea9d0d77STakashi Iwai 	}
2416ea9d0d77STakashi Iwai 
2417ea9d0d77STakashi Iwai 	/* we're alone, let's trigger */
2418ea9d0d77STakashi Iwai 	return dpcm_fe_dai_do_trigger(substream, cmd);
2419ea9d0d77STakashi Iwai }
2420ea9d0d77STakashi Iwai 
242123607025SLiam Girdwood int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
242201d7584cSLiam Girdwood {
242301d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
242401d7584cSLiam Girdwood 	int ret = 0;
242501d7584cSLiam Girdwood 
24268d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
242701d7584cSLiam Girdwood 
242801d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
242901d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
243001d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
243101d7584cSLiam Girdwood 
243201d7584cSLiam Girdwood 		/* is this op for this BE ? */
243301d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
243401d7584cSLiam Girdwood 			continue;
243501d7584cSLiam Girdwood 
243601d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
243795f444dcSKoro Chen 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
24385087a8f1SLibin Yang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
24395087a8f1SLibin Yang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
244001d7584cSLiam Girdwood 			continue;
244101d7584cSLiam Girdwood 
2442103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: prepare BE %s\n",
244394d215ccS彭东林 			be->dai_link->name);
244401d7584cSLiam Girdwood 
244501d7584cSLiam Girdwood 		ret = soc_pcm_prepare(be_substream);
244601d7584cSLiam Girdwood 		if (ret < 0) {
2447103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: backend prepare failed %d\n",
244801d7584cSLiam Girdwood 				ret);
244901d7584cSLiam Girdwood 			break;
245001d7584cSLiam Girdwood 		}
245101d7584cSLiam Girdwood 
245201d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
245301d7584cSLiam Girdwood 	}
245401d7584cSLiam Girdwood 	return ret;
245501d7584cSLiam Girdwood }
245601d7584cSLiam Girdwood 
245745c0a188SMark Brown static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
245801d7584cSLiam Girdwood {
245901d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
246001d7584cSLiam Girdwood 	int stream = substream->stream, ret = 0;
246101d7584cSLiam Girdwood 
246201d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
246301d7584cSLiam Girdwood 
2464103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
246501d7584cSLiam Girdwood 
2466ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
246701d7584cSLiam Girdwood 
246801d7584cSLiam Girdwood 	/* there is no point preparing this FE if there are no BEs */
246901d7584cSLiam Girdwood 	if (list_empty(&fe->dpcm[stream].be_clients)) {
2470103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
247101d7584cSLiam Girdwood 				fe->dai_link->name);
247201d7584cSLiam Girdwood 		ret = -EINVAL;
247301d7584cSLiam Girdwood 		goto out;
247401d7584cSLiam Girdwood 	}
247501d7584cSLiam Girdwood 
247601d7584cSLiam Girdwood 	ret = dpcm_be_dai_prepare(fe, substream->stream);
247701d7584cSLiam Girdwood 	if (ret < 0)
247801d7584cSLiam Girdwood 		goto out;
247901d7584cSLiam Girdwood 
248001d7584cSLiam Girdwood 	/* call prepare on the frontend */
248101d7584cSLiam Girdwood 	ret = soc_pcm_prepare(substream);
248201d7584cSLiam Girdwood 	if (ret < 0) {
2483103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
248401d7584cSLiam Girdwood 			fe->dai_link->name);
248501d7584cSLiam Girdwood 		goto out;
248601d7584cSLiam Girdwood 	}
248701d7584cSLiam Girdwood 
248801d7584cSLiam Girdwood 	/* run the stream event for each BE */
248901d7584cSLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
249001d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
249101d7584cSLiam Girdwood 
249201d7584cSLiam Girdwood out:
2493ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
249401d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
249501d7584cSLiam Girdwood 
249601d7584cSLiam Girdwood 	return ret;
249701d7584cSLiam Girdwood }
249801d7584cSLiam Girdwood 
2499618dae11SLiam Girdwood static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2500618dae11SLiam Girdwood {
250107bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
250207bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
250307bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2504618dae11SLiam Girdwood 	int err;
250501d7584cSLiam Girdwood 
2506103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2507618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2508618dae11SLiam Girdwood 
250907bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
251007bf84aaSLiam Girdwood 		/* call bespoke trigger - FE takes care of all BE triggers */
2511103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
251207bf84aaSLiam Girdwood 				fe->dai_link->name);
251307bf84aaSLiam Girdwood 
251407bf84aaSLiam Girdwood 		err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
251507bf84aaSLiam Girdwood 		if (err < 0)
2516103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
251707bf84aaSLiam Girdwood 	} else {
2518103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
251907bf84aaSLiam Girdwood 			fe->dai_link->name);
252007bf84aaSLiam Girdwood 
2521618dae11SLiam Girdwood 		err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2522618dae11SLiam Girdwood 		if (err < 0)
2523103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
252407bf84aaSLiam Girdwood 	}
2525618dae11SLiam Girdwood 
2526618dae11SLiam Girdwood 	err = dpcm_be_dai_hw_free(fe, stream);
2527618dae11SLiam Girdwood 	if (err < 0)
2528103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2529618dae11SLiam Girdwood 
2530618dae11SLiam Girdwood 	err = dpcm_be_dai_shutdown(fe, stream);
2531618dae11SLiam Girdwood 	if (err < 0)
2532103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2533618dae11SLiam Girdwood 
2534618dae11SLiam Girdwood 	/* run the stream event for each BE */
2535618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2536618dae11SLiam Girdwood 
2537618dae11SLiam Girdwood 	return 0;
2538618dae11SLiam Girdwood }
2539618dae11SLiam Girdwood 
2540618dae11SLiam Girdwood static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2541618dae11SLiam Girdwood {
254207bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
254307bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
2544618dae11SLiam Girdwood 	struct snd_soc_dpcm *dpcm;
254507bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2546618dae11SLiam Girdwood 	int ret;
2547a9764869SKaiChieh Chuang 	unsigned long flags;
2548618dae11SLiam Girdwood 
2549103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2550618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2551618dae11SLiam Girdwood 
2552618dae11SLiam Girdwood 	/* Only start the BE if the FE is ready */
2553618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2554618dae11SLiam Girdwood 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2555618dae11SLiam Girdwood 		return -EINVAL;
2556618dae11SLiam Girdwood 
2557618dae11SLiam Girdwood 	/* startup must always be called for new BEs */
2558618dae11SLiam Girdwood 	ret = dpcm_be_dai_startup(fe, stream);
2559fffc0ca2SDan Carpenter 	if (ret < 0)
2560618dae11SLiam Girdwood 		goto disconnect;
2561618dae11SLiam Girdwood 
2562618dae11SLiam Girdwood 	/* keep going if FE state is > open */
2563618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2564618dae11SLiam Girdwood 		return 0;
2565618dae11SLiam Girdwood 
2566618dae11SLiam Girdwood 	ret = dpcm_be_dai_hw_params(fe, stream);
2567fffc0ca2SDan Carpenter 	if (ret < 0)
2568618dae11SLiam Girdwood 		goto close;
2569618dae11SLiam Girdwood 
2570618dae11SLiam Girdwood 	/* keep going if FE state is > hw_params */
2571618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2572618dae11SLiam Girdwood 		return 0;
2573618dae11SLiam Girdwood 
2574618dae11SLiam Girdwood 
2575618dae11SLiam Girdwood 	ret = dpcm_be_dai_prepare(fe, stream);
2576fffc0ca2SDan Carpenter 	if (ret < 0)
2577618dae11SLiam Girdwood 		goto hw_free;
2578618dae11SLiam Girdwood 
2579618dae11SLiam Girdwood 	/* run the stream event for each BE */
2580618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2581618dae11SLiam Girdwood 
2582618dae11SLiam Girdwood 	/* keep going if FE state is > prepare */
2583618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2584618dae11SLiam Girdwood 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2585618dae11SLiam Girdwood 		return 0;
2586618dae11SLiam Girdwood 
258707bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
258807bf84aaSLiam Girdwood 		/* call trigger on the frontend - FE takes care of all BE triggers */
2589103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
259007bf84aaSLiam Girdwood 				fe->dai_link->name);
259107bf84aaSLiam Girdwood 
259207bf84aaSLiam Girdwood 		ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
259307bf84aaSLiam Girdwood 		if (ret < 0) {
2594103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
259507bf84aaSLiam Girdwood 			goto hw_free;
259607bf84aaSLiam Girdwood 		}
259707bf84aaSLiam Girdwood 	} else {
2598103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2599618dae11SLiam Girdwood 			fe->dai_link->name);
2600618dae11SLiam Girdwood 
2601618dae11SLiam Girdwood 		ret = dpcm_be_dai_trigger(fe, stream,
2602618dae11SLiam Girdwood 					SNDRV_PCM_TRIGGER_START);
2603618dae11SLiam Girdwood 		if (ret < 0) {
2604103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2605618dae11SLiam Girdwood 			goto hw_free;
2606618dae11SLiam Girdwood 		}
260707bf84aaSLiam Girdwood 	}
2608618dae11SLiam Girdwood 
2609618dae11SLiam Girdwood 	return 0;
2610618dae11SLiam Girdwood 
2611618dae11SLiam Girdwood hw_free:
2612618dae11SLiam Girdwood 	dpcm_be_dai_hw_free(fe, stream);
2613618dae11SLiam Girdwood close:
2614618dae11SLiam Girdwood 	dpcm_be_dai_shutdown(fe, stream);
2615618dae11SLiam Girdwood disconnect:
2616618dae11SLiam Girdwood 	/* disconnect any non started BEs */
2617a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
26188d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
2619618dae11SLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
2620618dae11SLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2621618dae11SLiam Girdwood 				dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2622618dae11SLiam Girdwood 	}
2623a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2624618dae11SLiam Girdwood 
2625618dae11SLiam Girdwood 	return ret;
2626618dae11SLiam Girdwood }
2627618dae11SLiam Girdwood 
2628618dae11SLiam Girdwood static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2629618dae11SLiam Girdwood {
2630618dae11SLiam Girdwood 	int ret;
2631618dae11SLiam Girdwood 
2632ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2633618dae11SLiam Girdwood 	ret = dpcm_run_update_startup(fe, stream);
2634618dae11SLiam Girdwood 	if (ret < 0)
2635103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2636ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2637618dae11SLiam Girdwood 
2638618dae11SLiam Girdwood 	return ret;
2639618dae11SLiam Girdwood }
2640618dae11SLiam Girdwood 
2641618dae11SLiam Girdwood static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2642618dae11SLiam Girdwood {
2643618dae11SLiam Girdwood 	int ret;
2644618dae11SLiam Girdwood 
2645ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2646618dae11SLiam Girdwood 	ret = dpcm_run_update_shutdown(fe, stream);
2647618dae11SLiam Girdwood 	if (ret < 0)
2648103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2649ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2650618dae11SLiam Girdwood 
2651618dae11SLiam Girdwood 	return ret;
2652618dae11SLiam Girdwood }
2653618dae11SLiam Girdwood 
2654de15d7ffSJerome Brunet static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2655618dae11SLiam Girdwood {
2656618dae11SLiam Girdwood 	struct snd_soc_dapm_widget_list *list;
2657de15d7ffSJerome Brunet 	int count, paths;
2658618dae11SLiam Girdwood 
2659618dae11SLiam Girdwood 	if (!fe->dai_link->dynamic)
2660de15d7ffSJerome Brunet 		return 0;
2661618dae11SLiam Girdwood 
2662618dae11SLiam Girdwood 	/* only check active links */
2663618dae11SLiam Girdwood 	if (!fe->cpu_dai->active)
2664de15d7ffSJerome Brunet 		return 0;
2665618dae11SLiam Girdwood 
2666618dae11SLiam Girdwood 	/* DAPM sync will call this to update DSP paths */
2667de15d7ffSJerome Brunet 	dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2668de15d7ffSJerome Brunet 		new ? "new" : "old", fe->dai_link->name);
2669618dae11SLiam Girdwood 
2670618dae11SLiam Girdwood 	/* skip if FE doesn't have playback capability */
2671467fece8SKuninori Morimoto 	if (!snd_soc_dai_stream_valid(fe->cpu_dai,   SNDRV_PCM_STREAM_PLAYBACK) ||
2672467fece8SKuninori Morimoto 	    !snd_soc_dai_stream_valid(fe->codec_dai, SNDRV_PCM_STREAM_PLAYBACK))
2673075207d2SQiao Zhou 		goto capture;
2674075207d2SQiao Zhou 
2675075207d2SQiao Zhou 	/* skip if FE isn't currently playing */
2676de15d7ffSJerome Brunet 	if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
2677618dae11SLiam Girdwood 		goto capture;
2678618dae11SLiam Girdwood 
2679618dae11SLiam Girdwood 	paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2680618dae11SLiam Girdwood 	if (paths < 0) {
2681103d84a3SLiam Girdwood 		dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2682618dae11SLiam Girdwood 			 fe->dai_link->name,  "playback");
2683618dae11SLiam Girdwood 		return paths;
2684618dae11SLiam Girdwood 	}
2685618dae11SLiam Girdwood 
2686de15d7ffSJerome Brunet 	/* update any playback paths */
2687de15d7ffSJerome Brunet 	count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
2688de15d7ffSJerome Brunet 	if (count) {
2689de15d7ffSJerome Brunet 		if (new)
2690618dae11SLiam Girdwood 			dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2691de15d7ffSJerome Brunet 		else
2692618dae11SLiam Girdwood 			dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2693de15d7ffSJerome Brunet 
2694618dae11SLiam Girdwood 		dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2695618dae11SLiam Girdwood 		dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2696618dae11SLiam Girdwood 	}
2697618dae11SLiam Girdwood 
26987ed9de76SQiao Zhou 	dpcm_path_put(&list);
2699de15d7ffSJerome Brunet 
2700618dae11SLiam Girdwood capture:
2701618dae11SLiam Girdwood 	/* skip if FE doesn't have capture capability */
2702467fece8SKuninori Morimoto 	if (!snd_soc_dai_stream_valid(fe->cpu_dai,   SNDRV_PCM_STREAM_CAPTURE) ||
2703467fece8SKuninori Morimoto 	    !snd_soc_dai_stream_valid(fe->codec_dai, SNDRV_PCM_STREAM_CAPTURE))
2704de15d7ffSJerome Brunet 		return 0;
2705075207d2SQiao Zhou 
2706075207d2SQiao Zhou 	/* skip if FE isn't currently capturing */
2707de15d7ffSJerome Brunet 	if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
2708de15d7ffSJerome Brunet 		return 0;
2709618dae11SLiam Girdwood 
2710618dae11SLiam Girdwood 	paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2711618dae11SLiam Girdwood 	if (paths < 0) {
2712103d84a3SLiam Girdwood 		dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2713618dae11SLiam Girdwood 			 fe->dai_link->name,  "capture");
2714618dae11SLiam Girdwood 		return paths;
2715618dae11SLiam Girdwood 	}
2716618dae11SLiam Girdwood 
2717618dae11SLiam Girdwood 	/* update any old capture paths */
2718de15d7ffSJerome Brunet 	count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
2719de15d7ffSJerome Brunet 	if (count) {
2720de15d7ffSJerome Brunet 		if (new)
2721de15d7ffSJerome Brunet 			dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2722de15d7ffSJerome Brunet 		else
2723618dae11SLiam Girdwood 			dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2724de15d7ffSJerome Brunet 
2725618dae11SLiam Girdwood 		dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2726618dae11SLiam Girdwood 		dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2727618dae11SLiam Girdwood 	}
2728618dae11SLiam Girdwood 
2729618dae11SLiam Girdwood 	dpcm_path_put(&list);
2730de15d7ffSJerome Brunet 
2731de15d7ffSJerome Brunet 	return 0;
2732618dae11SLiam Girdwood }
2733618dae11SLiam Girdwood 
2734de15d7ffSJerome Brunet /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2735de15d7ffSJerome Brunet  * any DAI links.
2736de15d7ffSJerome Brunet  */
2737de15d7ffSJerome Brunet int soc_dpcm_runtime_update(struct snd_soc_card *card)
2738de15d7ffSJerome Brunet {
2739de15d7ffSJerome Brunet 	struct snd_soc_pcm_runtime *fe;
2740de15d7ffSJerome Brunet 	int ret = 0;
2741de15d7ffSJerome Brunet 
2742de15d7ffSJerome Brunet 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2743de15d7ffSJerome Brunet 	/* shutdown all old paths first */
2744bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, fe) {
2745de15d7ffSJerome Brunet 		ret = soc_dpcm_fe_runtime_update(fe, 0);
2746de15d7ffSJerome Brunet 		if (ret)
2747de15d7ffSJerome Brunet 			goto out;
2748de15d7ffSJerome Brunet 	}
2749de15d7ffSJerome Brunet 
2750de15d7ffSJerome Brunet 	/* bring new paths up */
2751bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, fe) {
2752de15d7ffSJerome Brunet 		ret = soc_dpcm_fe_runtime_update(fe, 1);
2753de15d7ffSJerome Brunet 		if (ret)
2754de15d7ffSJerome Brunet 			goto out;
2755de15d7ffSJerome Brunet 	}
2756de15d7ffSJerome Brunet 
2757de15d7ffSJerome Brunet out:
2758618dae11SLiam Girdwood 	mutex_unlock(&card->mutex);
2759de15d7ffSJerome Brunet 	return ret;
2760618dae11SLiam Girdwood }
276101d7584cSLiam Girdwood int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
276201d7584cSLiam Girdwood {
276301d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
27647afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
276501d7584cSLiam Girdwood 
27668d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
276701d7584cSLiam Girdwood 
276801d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
27692e5894d7SBenoit Cousson 		int i;
277001d7584cSLiam Girdwood 
277101d7584cSLiam Girdwood 		if (be->dai_link->ignore_suspend)
277201d7584cSLiam Girdwood 			continue;
277301d7584cSLiam Girdwood 
27747afecb30SKuninori Morimoto 		for_each_rtd_codec_dai(be, i, dai) {
27752e5894d7SBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
277601d7584cSLiam Girdwood 
27772e5894d7SBenoit Cousson 			dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
27782e5894d7SBenoit Cousson 					 be->dai_link->name);
27792e5894d7SBenoit Cousson 
27802e5894d7SBenoit Cousson 			if (drv->ops && drv->ops->digital_mute &&
27812e5894d7SBenoit Cousson 							dai->playback_active)
278201d7584cSLiam Girdwood 				drv->ops->digital_mute(dai, mute);
278301d7584cSLiam Girdwood 		}
27842e5894d7SBenoit Cousson 	}
278501d7584cSLiam Girdwood 
278601d7584cSLiam Girdwood 	return 0;
278701d7584cSLiam Girdwood }
278801d7584cSLiam Girdwood 
278945c0a188SMark Brown static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
279001d7584cSLiam Girdwood {
279101d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
279201d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
279301d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list;
279401d7584cSLiam Girdwood 	int ret;
279501d7584cSLiam Girdwood 	int stream = fe_substream->stream;
279601d7584cSLiam Girdwood 
279701d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
279801d7584cSLiam Girdwood 	fe->dpcm[stream].runtime = fe_substream->runtime;
279901d7584cSLiam Girdwood 
28008f70e515SQiao Zhou 	ret = dpcm_path_get(fe, stream, &list);
28018f70e515SQiao Zhou 	if (ret < 0) {
28028f70e515SQiao Zhou 		mutex_unlock(&fe->card->mutex);
28038f70e515SQiao Zhou 		return ret;
28048f70e515SQiao Zhou 	} else if (ret == 0) {
2805103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
280601d7584cSLiam Girdwood 			fe->dai_link->name, stream ? "capture" : "playback");
280701d7584cSLiam Girdwood 	}
280801d7584cSLiam Girdwood 
280901d7584cSLiam Girdwood 	/* calculate valid and active FE <-> BE dpcms */
281001d7584cSLiam Girdwood 	dpcm_process_paths(fe, stream, &list, 1);
281101d7584cSLiam Girdwood 
281201d7584cSLiam Girdwood 	ret = dpcm_fe_dai_startup(fe_substream);
281301d7584cSLiam Girdwood 	if (ret < 0) {
281401d7584cSLiam Girdwood 		/* clean up all links */
28158d6258a4SKuninori Morimoto 		for_each_dpcm_be(fe, stream, dpcm)
281601d7584cSLiam Girdwood 			dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
281701d7584cSLiam Girdwood 
281801d7584cSLiam Girdwood 		dpcm_be_disconnect(fe, stream);
281901d7584cSLiam Girdwood 		fe->dpcm[stream].runtime = NULL;
282001d7584cSLiam Girdwood 	}
282101d7584cSLiam Girdwood 
282201d7584cSLiam Girdwood 	dpcm_clear_pending_state(fe, stream);
282301d7584cSLiam Girdwood 	dpcm_path_put(&list);
282401d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
282501d7584cSLiam Girdwood 	return ret;
282601d7584cSLiam Girdwood }
282701d7584cSLiam Girdwood 
282845c0a188SMark Brown static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
282901d7584cSLiam Girdwood {
283001d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
283101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
283201d7584cSLiam Girdwood 	int stream = fe_substream->stream, ret;
283301d7584cSLiam Girdwood 
283401d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
283501d7584cSLiam Girdwood 	ret = dpcm_fe_dai_shutdown(fe_substream);
283601d7584cSLiam Girdwood 
283701d7584cSLiam Girdwood 	/* mark FE's links ready to prune */
28388d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm)
283901d7584cSLiam Girdwood 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
284001d7584cSLiam Girdwood 
284101d7584cSLiam Girdwood 	dpcm_be_disconnect(fe, stream);
284201d7584cSLiam Girdwood 
284301d7584cSLiam Girdwood 	fe->dpcm[stream].runtime = NULL;
284401d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
284501d7584cSLiam Girdwood 	return ret;
284601d7584cSLiam Girdwood }
284701d7584cSLiam Girdwood 
2848ddee627cSLiam Girdwood /* create a new pcm */
2849ddee627cSLiam Girdwood int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2850ddee627cSLiam Girdwood {
28512e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
2852ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
28532b544dd7SKuninori Morimoto 	struct snd_soc_component *component;
2854ddee627cSLiam Girdwood 	struct snd_pcm *pcm;
2855ddee627cSLiam Girdwood 	char new_name[64];
2856ddee627cSLiam Girdwood 	int ret = 0, playback = 0, capture = 0;
28572e5894d7SBenoit Cousson 	int i;
2858ddee627cSLiam Girdwood 
285901d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
28601e9de42fSLiam Girdwood 		playback = rtd->dai_link->dpcm_playback;
28611e9de42fSLiam Girdwood 		capture = rtd->dai_link->dpcm_capture;
286201d7584cSLiam Girdwood 	} else {
2863a342031cSJerome Brunet 		/* Adapt stream for codec2codec links */
2864a342031cSJerome Brunet 		struct snd_soc_pcm_stream *cpu_capture = rtd->dai_link->params ?
2865a342031cSJerome Brunet 			&cpu_dai->driver->playback : &cpu_dai->driver->capture;
2866a342031cSJerome Brunet 		struct snd_soc_pcm_stream *cpu_playback = rtd->dai_link->params ?
2867a342031cSJerome Brunet 			&cpu_dai->driver->capture : &cpu_dai->driver->playback;
2868a342031cSJerome Brunet 
28690b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
2870467fece8SKuninori Morimoto 			if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2871467fece8SKuninori Morimoto 			    snd_soc_dai_stream_valid(cpu_dai,   SNDRV_PCM_STREAM_PLAYBACK))
2872ddee627cSLiam Girdwood 				playback = 1;
2873467fece8SKuninori Morimoto 			if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2874467fece8SKuninori Morimoto 			    snd_soc_dai_stream_valid(cpu_dai,   SNDRV_PCM_STREAM_CAPTURE))
2875ddee627cSLiam Girdwood 				capture = 1;
287601d7584cSLiam Girdwood 		}
2877a342031cSJerome Brunet 
2878a342031cSJerome Brunet 		capture = capture && cpu_capture->channels_min;
2879a342031cSJerome Brunet 		playback = playback && cpu_playback->channels_min;
28802e5894d7SBenoit Cousson 	}
28812e5894d7SBenoit Cousson 
2882d6bead02SFabio Estevam 	if (rtd->dai_link->playback_only) {
2883d6bead02SFabio Estevam 		playback = 1;
2884d6bead02SFabio Estevam 		capture = 0;
2885d6bead02SFabio Estevam 	}
2886d6bead02SFabio Estevam 
2887d6bead02SFabio Estevam 	if (rtd->dai_link->capture_only) {
2888d6bead02SFabio Estevam 		playback = 0;
2889d6bead02SFabio Estevam 		capture = 1;
2890d6bead02SFabio Estevam 	}
2891d6bead02SFabio Estevam 
289201d7584cSLiam Girdwood 	/* create the PCM */
2893a342031cSJerome Brunet 	if (rtd->dai_link->params) {
2894a342031cSJerome Brunet 		snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2895a342031cSJerome Brunet 			 rtd->dai_link->stream_name);
2896a342031cSJerome Brunet 
2897a342031cSJerome Brunet 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2898a342031cSJerome Brunet 					   playback, capture, &pcm);
2899a342031cSJerome Brunet 	} else if (rtd->dai_link->no_pcm) {
290001d7584cSLiam Girdwood 		snprintf(new_name, sizeof(new_name), "(%s)",
290101d7584cSLiam Girdwood 			rtd->dai_link->stream_name);
290201d7584cSLiam Girdwood 
290301d7584cSLiam Girdwood 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
290401d7584cSLiam Girdwood 				playback, capture, &pcm);
290501d7584cSLiam Girdwood 	} else {
290601d7584cSLiam Girdwood 		if (rtd->dai_link->dynamic)
290701d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s (*)",
290801d7584cSLiam Girdwood 				rtd->dai_link->stream_name);
290901d7584cSLiam Girdwood 		else
291001d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s %s-%d",
29112e5894d7SBenoit Cousson 				rtd->dai_link->stream_name,
29122e5894d7SBenoit Cousson 				(rtd->num_codecs > 1) ?
29132e5894d7SBenoit Cousson 				"multicodec" : rtd->codec_dai->name, num);
291401d7584cSLiam Girdwood 
291501d7584cSLiam Girdwood 		ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
291601d7584cSLiam Girdwood 			capture, &pcm);
291701d7584cSLiam Girdwood 	}
2918ddee627cSLiam Girdwood 	if (ret < 0) {
2919103d84a3SLiam Girdwood 		dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
29205cb9b748SLiam Girdwood 			rtd->dai_link->name);
2921ddee627cSLiam Girdwood 		return ret;
2922ddee627cSLiam Girdwood 	}
2923103d84a3SLiam Girdwood 	dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2924ddee627cSLiam Girdwood 
2925ddee627cSLiam Girdwood 	/* DAPM dai link stream work */
2926a342031cSJerome Brunet 	if (rtd->dai_link->params)
29274bf2e385SCurtis Malainey 		rtd->close_delayed_work_func = codec2codec_close_delayed_work;
2928a342031cSJerome Brunet 	else
292983f94a2eSKuninori Morimoto 		rtd->close_delayed_work_func = snd_soc_close_delayed_work;
2930ddee627cSLiam Girdwood 
293148c7699fSVinod Koul 	pcm->nonatomic = rtd->dai_link->nonatomic;
2932ddee627cSLiam Girdwood 	rtd->pcm = pcm;
2933ddee627cSLiam Girdwood 	pcm->private_data = rtd;
293401d7584cSLiam Girdwood 
2935a342031cSJerome Brunet 	if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
293601d7584cSLiam Girdwood 		if (playback)
293701d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
293801d7584cSLiam Girdwood 		if (capture)
293901d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
294001d7584cSLiam Girdwood 		goto out;
294101d7584cSLiam Girdwood 	}
294201d7584cSLiam Girdwood 
294301d7584cSLiam Girdwood 	/* ASoC PCM operations */
294401d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic) {
294501d7584cSLiam Girdwood 		rtd->ops.open		= dpcm_fe_dai_open;
294601d7584cSLiam Girdwood 		rtd->ops.hw_params	= dpcm_fe_dai_hw_params;
294701d7584cSLiam Girdwood 		rtd->ops.prepare	= dpcm_fe_dai_prepare;
294801d7584cSLiam Girdwood 		rtd->ops.trigger	= dpcm_fe_dai_trigger;
294901d7584cSLiam Girdwood 		rtd->ops.hw_free	= dpcm_fe_dai_hw_free;
295001d7584cSLiam Girdwood 		rtd->ops.close		= dpcm_fe_dai_close;
295101d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
295201d7584cSLiam Girdwood 	} else {
295301d7584cSLiam Girdwood 		rtd->ops.open		= soc_pcm_open;
295401d7584cSLiam Girdwood 		rtd->ops.hw_params	= soc_pcm_hw_params;
295501d7584cSLiam Girdwood 		rtd->ops.prepare	= soc_pcm_prepare;
295601d7584cSLiam Girdwood 		rtd->ops.trigger	= soc_pcm_trigger;
295701d7584cSLiam Girdwood 		rtd->ops.hw_free	= soc_pcm_hw_free;
295801d7584cSLiam Girdwood 		rtd->ops.close		= soc_pcm_close;
295901d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
296001d7584cSLiam Girdwood 	}
296101d7584cSLiam Girdwood 
2962613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
29632b544dd7SKuninori Morimoto 		const struct snd_soc_component_driver *drv = component->driver;
2964b8135864SKuninori Morimoto 
29653b1c952cSTakashi Iwai 		if (drv->ioctl)
29663b1c952cSTakashi Iwai 			rtd->ops.ioctl		= snd_soc_pcm_component_ioctl;
29671e5ddb6bSTakashi Iwai 		if (drv->sync_stop)
29681e5ddb6bSTakashi Iwai 			rtd->ops.sync_stop	= snd_soc_pcm_component_sync_stop;
2969e9067bb5SKuninori Morimoto 		if (drv->copy_user)
297082d81f5cSKuninori Morimoto 			rtd->ops.copy_user	= snd_soc_pcm_component_copy_user;
2971e9067bb5SKuninori Morimoto 		if (drv->page)
29729c712e4fSKuninori Morimoto 			rtd->ops.page		= snd_soc_pcm_component_page;
2973e9067bb5SKuninori Morimoto 		if (drv->mmap)
2974205875e1SKuninori Morimoto 			rtd->ops.mmap		= snd_soc_pcm_component_mmap;
2975b8135864SKuninori Morimoto 	}
2976b8135864SKuninori Morimoto 
2977ddee627cSLiam Girdwood 	if (playback)
297801d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2979ddee627cSLiam Girdwood 
2980ddee627cSLiam Girdwood 	if (capture)
298101d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2982ddee627cSLiam Girdwood 
2983b2b2afbbSKuninori Morimoto 	ret = snd_soc_pcm_component_new(rtd);
2984ddee627cSLiam Girdwood 	if (ret < 0) {
29857484291eSKuninori Morimoto 		dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret);
2986ddee627cSLiam Girdwood 		return ret;
2987ddee627cSLiam Girdwood 	}
2988c641e5b2SJohan Hovold 
29893d21ef0bSTakashi Iwai 	pcm->no_device_suspend = true;
299001d7584cSLiam Girdwood out:
29912e5894d7SBenoit Cousson 	dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
29922e5894d7SBenoit Cousson 		 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2993ddee627cSLiam Girdwood 		 cpu_dai->name);
2994ddee627cSLiam Girdwood 	return ret;
2995ddee627cSLiam Girdwood }
299601d7584cSLiam Girdwood 
299701d7584cSLiam Girdwood /* is the current PCM operation for this FE ? */
299801d7584cSLiam Girdwood int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
299901d7584cSLiam Girdwood {
300001d7584cSLiam Girdwood 	if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
300101d7584cSLiam Girdwood 		return 1;
300201d7584cSLiam Girdwood 	return 0;
300301d7584cSLiam Girdwood }
300401d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
300501d7584cSLiam Girdwood 
300601d7584cSLiam Girdwood /* is the current PCM operation for this BE ? */
300701d7584cSLiam Girdwood int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
300801d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
300901d7584cSLiam Girdwood {
301001d7584cSLiam Girdwood 	if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
301101d7584cSLiam Girdwood 	   ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
301201d7584cSLiam Girdwood 		  be->dpcm[stream].runtime_update))
301301d7584cSLiam Girdwood 		return 1;
301401d7584cSLiam Girdwood 	return 0;
301501d7584cSLiam Girdwood }
301601d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
301701d7584cSLiam Girdwood 
301801d7584cSLiam Girdwood /* get the substream for this BE */
301901d7584cSLiam Girdwood struct snd_pcm_substream *
302001d7584cSLiam Girdwood 	snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
302101d7584cSLiam Girdwood {
302201d7584cSLiam Girdwood 	return be->pcm->streams[stream].substream;
302301d7584cSLiam Girdwood }
302401d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
302501d7584cSLiam Girdwood 
302601d7584cSLiam Girdwood /* get the BE runtime state */
302701d7584cSLiam Girdwood enum snd_soc_dpcm_state
302801d7584cSLiam Girdwood 	snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
302901d7584cSLiam Girdwood {
303001d7584cSLiam Girdwood 	return be->dpcm[stream].state;
303101d7584cSLiam Girdwood }
303201d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
303301d7584cSLiam Girdwood 
303401d7584cSLiam Girdwood /* set the BE runtime state */
303501d7584cSLiam Girdwood void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
303601d7584cSLiam Girdwood 		int stream, enum snd_soc_dpcm_state state)
303701d7584cSLiam Girdwood {
303801d7584cSLiam Girdwood 	be->dpcm[stream].state = state;
303901d7584cSLiam Girdwood }
304001d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
304101d7584cSLiam Girdwood 
304201d7584cSLiam Girdwood /*
304301d7584cSLiam Girdwood  * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
304401d7584cSLiam Girdwood  * are not running, paused or suspended for the specified stream direction.
304501d7584cSLiam Girdwood  */
304601d7584cSLiam Girdwood int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
304701d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
304801d7584cSLiam Girdwood {
304901d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
305001d7584cSLiam Girdwood 	int state;
3051a9764869SKaiChieh Chuang 	int ret = 1;
3052a9764869SKaiChieh Chuang 	unsigned long flags;
305301d7584cSLiam Girdwood 
3054a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3055d2e24d64SKuninori Morimoto 	for_each_dpcm_fe(be, stream, dpcm) {
305601d7584cSLiam Girdwood 
305701d7584cSLiam Girdwood 		if (dpcm->fe == fe)
305801d7584cSLiam Girdwood 			continue;
305901d7584cSLiam Girdwood 
306001d7584cSLiam Girdwood 		state = dpcm->fe->dpcm[stream].state;
306101d7584cSLiam Girdwood 		if (state == SND_SOC_DPCM_STATE_START ||
306201d7584cSLiam Girdwood 			state == SND_SOC_DPCM_STATE_PAUSED ||
3063a9764869SKaiChieh Chuang 			state == SND_SOC_DPCM_STATE_SUSPEND) {
3064a9764869SKaiChieh Chuang 			ret = 0;
3065a9764869SKaiChieh Chuang 			break;
306601d7584cSLiam Girdwood 		}
3067a9764869SKaiChieh Chuang 	}
3068a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
306901d7584cSLiam Girdwood 
307001d7584cSLiam Girdwood 	/* it's safe to free/stop this BE DAI */
3071a9764869SKaiChieh Chuang 	return ret;
307201d7584cSLiam Girdwood }
307301d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
307401d7584cSLiam Girdwood 
307501d7584cSLiam Girdwood /*
307601d7584cSLiam Girdwood  * We can only change hw params a BE DAI if any of it's FE are not prepared,
307701d7584cSLiam Girdwood  * running, paused or suspended for the specified stream direction.
307801d7584cSLiam Girdwood  */
307901d7584cSLiam Girdwood int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
308001d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
308101d7584cSLiam Girdwood {
308201d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
308301d7584cSLiam Girdwood 	int state;
3084a9764869SKaiChieh Chuang 	int ret = 1;
3085a9764869SKaiChieh Chuang 	unsigned long flags;
308601d7584cSLiam Girdwood 
3087a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3088d2e24d64SKuninori Morimoto 	for_each_dpcm_fe(be, stream, dpcm) {
308901d7584cSLiam Girdwood 
309001d7584cSLiam Girdwood 		if (dpcm->fe == fe)
309101d7584cSLiam Girdwood 			continue;
309201d7584cSLiam Girdwood 
309301d7584cSLiam Girdwood 		state = dpcm->fe->dpcm[stream].state;
309401d7584cSLiam Girdwood 		if (state == SND_SOC_DPCM_STATE_START ||
309501d7584cSLiam Girdwood 			state == SND_SOC_DPCM_STATE_PAUSED ||
309601d7584cSLiam Girdwood 			state == SND_SOC_DPCM_STATE_SUSPEND ||
3097a9764869SKaiChieh Chuang 			state == SND_SOC_DPCM_STATE_PREPARE) {
3098a9764869SKaiChieh Chuang 			ret = 0;
3099a9764869SKaiChieh Chuang 			break;
310001d7584cSLiam Girdwood 		}
3101a9764869SKaiChieh Chuang 	}
3102a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
310301d7584cSLiam Girdwood 
310401d7584cSLiam Girdwood 	/* it's safe to change hw_params */
3105a9764869SKaiChieh Chuang 	return ret;
310601d7584cSLiam Girdwood }
310701d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3108f86dcef8SLiam Girdwood 
3109f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS
311085280141SLars-Peter Clausen static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
3111f86dcef8SLiam Girdwood {
3112f86dcef8SLiam Girdwood 	switch (state) {
3113f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_NEW:
3114f86dcef8SLiam Girdwood 		return "new";
3115f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_OPEN:
3116f86dcef8SLiam Girdwood 		return "open";
3117f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_HW_PARAMS:
3118f86dcef8SLiam Girdwood 		return "hw_params";
3119f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_PREPARE:
3120f86dcef8SLiam Girdwood 		return "prepare";
3121f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_START:
3122f86dcef8SLiam Girdwood 		return "start";
3123f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_STOP:
3124f86dcef8SLiam Girdwood 		return "stop";
3125f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_SUSPEND:
3126f86dcef8SLiam Girdwood 		return "suspend";
3127f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_PAUSED:
3128f86dcef8SLiam Girdwood 		return "paused";
3129f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_HW_FREE:
3130f86dcef8SLiam Girdwood 		return "hw_free";
3131f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_CLOSE:
3132f86dcef8SLiam Girdwood 		return "close";
3133f86dcef8SLiam Girdwood 	}
3134f86dcef8SLiam Girdwood 
3135f86dcef8SLiam Girdwood 	return "unknown";
3136f86dcef8SLiam Girdwood }
3137f86dcef8SLiam Girdwood 
3138f86dcef8SLiam Girdwood static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3139f86dcef8SLiam Girdwood 				int stream, char *buf, size_t size)
3140f86dcef8SLiam Girdwood {
3141f86dcef8SLiam Girdwood 	struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3142f86dcef8SLiam Girdwood 	struct snd_soc_dpcm *dpcm;
3143f86dcef8SLiam Girdwood 	ssize_t offset = 0;
3144a9764869SKaiChieh Chuang 	unsigned long flags;
3145f86dcef8SLiam Girdwood 
3146f86dcef8SLiam Girdwood 	/* FE state */
3147f86dcef8SLiam Girdwood 	offset += snprintf(buf + offset, size - offset,
3148f86dcef8SLiam Girdwood 			"[%s - %s]\n", fe->dai_link->name,
3149f86dcef8SLiam Girdwood 			stream ? "Capture" : "Playback");
3150f86dcef8SLiam Girdwood 
3151f86dcef8SLiam Girdwood 	offset += snprintf(buf + offset, size - offset, "State: %s\n",
3152f86dcef8SLiam Girdwood 	                dpcm_state_string(fe->dpcm[stream].state));
3153f86dcef8SLiam Girdwood 
3154f86dcef8SLiam Girdwood 	if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3155f86dcef8SLiam Girdwood 	    (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3156f86dcef8SLiam Girdwood 		offset += snprintf(buf + offset, size - offset,
3157f86dcef8SLiam Girdwood 				"Hardware Params: "
3158f86dcef8SLiam Girdwood 				"Format = %s, Channels = %d, Rate = %d\n",
3159f86dcef8SLiam Girdwood 				snd_pcm_format_name(params_format(params)),
3160f86dcef8SLiam Girdwood 				params_channels(params),
3161f86dcef8SLiam Girdwood 				params_rate(params));
3162f86dcef8SLiam Girdwood 
3163f86dcef8SLiam Girdwood 	/* BEs state */
3164f86dcef8SLiam Girdwood 	offset += snprintf(buf + offset, size - offset, "Backends:\n");
3165f86dcef8SLiam Girdwood 
3166f86dcef8SLiam Girdwood 	if (list_empty(&fe->dpcm[stream].be_clients)) {
3167f86dcef8SLiam Girdwood 		offset += snprintf(buf + offset, size - offset,
3168f86dcef8SLiam Girdwood 				" No active DSP links\n");
3169f86dcef8SLiam Girdwood 		goto out;
3170f86dcef8SLiam Girdwood 	}
3171f86dcef8SLiam Girdwood 
3172a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
31738d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
3174f86dcef8SLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
3175f86dcef8SLiam Girdwood 		params = &dpcm->hw_params;
3176f86dcef8SLiam Girdwood 
3177f86dcef8SLiam Girdwood 		offset += snprintf(buf + offset, size - offset,
3178f86dcef8SLiam Girdwood 				"- %s\n", be->dai_link->name);
3179f86dcef8SLiam Girdwood 
3180f86dcef8SLiam Girdwood 		offset += snprintf(buf + offset, size - offset,
3181f86dcef8SLiam Girdwood 				"   State: %s\n",
3182f86dcef8SLiam Girdwood 				dpcm_state_string(be->dpcm[stream].state));
3183f86dcef8SLiam Girdwood 
3184f86dcef8SLiam Girdwood 		if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3185f86dcef8SLiam Girdwood 		    (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3186f86dcef8SLiam Girdwood 			offset += snprintf(buf + offset, size - offset,
3187f86dcef8SLiam Girdwood 				"   Hardware Params: "
3188f86dcef8SLiam Girdwood 				"Format = %s, Channels = %d, Rate = %d\n",
3189f86dcef8SLiam Girdwood 				snd_pcm_format_name(params_format(params)),
3190f86dcef8SLiam Girdwood 				params_channels(params),
3191f86dcef8SLiam Girdwood 				params_rate(params));
3192f86dcef8SLiam Girdwood 	}
3193a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3194f86dcef8SLiam Girdwood out:
3195f86dcef8SLiam Girdwood 	return offset;
3196f86dcef8SLiam Girdwood }
3197f86dcef8SLiam Girdwood 
3198f86dcef8SLiam Girdwood static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3199f86dcef8SLiam Girdwood 				size_t count, loff_t *ppos)
3200f86dcef8SLiam Girdwood {
3201f86dcef8SLiam Girdwood 	struct snd_soc_pcm_runtime *fe = file->private_data;
3202f86dcef8SLiam Girdwood 	ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3203f86dcef8SLiam Girdwood 	char *buf;
3204f86dcef8SLiam Girdwood 
3205f86dcef8SLiam Girdwood 	buf = kmalloc(out_count, GFP_KERNEL);
3206f86dcef8SLiam Girdwood 	if (!buf)
3207f86dcef8SLiam Girdwood 		return -ENOMEM;
3208f86dcef8SLiam Girdwood 
3209467fece8SKuninori Morimoto 	if (snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
3210f86dcef8SLiam Girdwood 		offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3211f86dcef8SLiam Girdwood 					buf + offset, out_count - offset);
3212f86dcef8SLiam Girdwood 
3213467fece8SKuninori Morimoto 	if (snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
3214f86dcef8SLiam Girdwood 		offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3215f86dcef8SLiam Girdwood 					buf + offset, out_count - offset);
3216f86dcef8SLiam Girdwood 
3217f86dcef8SLiam Girdwood 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3218f86dcef8SLiam Girdwood 
3219f86dcef8SLiam Girdwood 	kfree(buf);
3220f86dcef8SLiam Girdwood 	return ret;
3221f86dcef8SLiam Girdwood }
3222f86dcef8SLiam Girdwood 
3223f86dcef8SLiam Girdwood static const struct file_operations dpcm_state_fops = {
3224f57b8488SLiam Girdwood 	.open = simple_open,
3225f86dcef8SLiam Girdwood 	.read = dpcm_state_read_file,
3226f86dcef8SLiam Girdwood 	.llseek = default_llseek,
3227f86dcef8SLiam Girdwood };
3228f86dcef8SLiam Girdwood 
32292e55b90aSLars-Peter Clausen void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3230f86dcef8SLiam Girdwood {
3231b3bba9a1SMark Brown 	if (!rtd->dai_link)
32322e55b90aSLars-Peter Clausen 		return;
3233b3bba9a1SMark Brown 
3234596becd3SKuninori Morimoto 	if (!rtd->dai_link->dynamic)
3235596becd3SKuninori Morimoto 		return;
3236596becd3SKuninori Morimoto 
32376553bf06SLars-Peter Clausen 	if (!rtd->card->debugfs_card_root)
32386553bf06SLars-Peter Clausen 		return;
3239f86dcef8SLiam Girdwood 
3240f86dcef8SLiam Girdwood 	rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3241f86dcef8SLiam Girdwood 			rtd->card->debugfs_card_root);
3242f86dcef8SLiam Girdwood 
3243f1e3f409SFabio Estevam 	debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3244f86dcef8SLiam Girdwood 			    rtd, &dpcm_state_fops);
3245f86dcef8SLiam Girdwood }
3246f86dcef8SLiam Girdwood #endif
3247