xref: /openbmc/linux/sound/soc/soc-pcm.c (revision cde79035c6cf578dd33dfea3e39666efc27cbcf2)
1ddee627cSLiam Girdwood /*
2ddee627cSLiam Girdwood  * soc-pcm.c  --  ALSA SoC PCM
3ddee627cSLiam Girdwood  *
4ddee627cSLiam Girdwood  * Copyright 2005 Wolfson Microelectronics PLC.
5ddee627cSLiam Girdwood  * Copyright 2005 Openedhand Ltd.
6ddee627cSLiam Girdwood  * Copyright (C) 2010 Slimlogic Ltd.
7ddee627cSLiam Girdwood  * Copyright (C) 2010 Texas Instruments Inc.
8ddee627cSLiam Girdwood  *
9ddee627cSLiam Girdwood  * Authors: Liam Girdwood <lrg@ti.com>
10ddee627cSLiam Girdwood  *          Mark Brown <broonie@opensource.wolfsonmicro.com>
11ddee627cSLiam Girdwood  *
12ddee627cSLiam Girdwood  *  This program is free software; you can redistribute  it and/or modify it
13ddee627cSLiam Girdwood  *  under  the terms of  the GNU General  Public License as published by the
14ddee627cSLiam Girdwood  *  Free Software Foundation;  either version 2 of the  License, or (at your
15ddee627cSLiam Girdwood  *  option) any later version.
16ddee627cSLiam Girdwood  *
17ddee627cSLiam Girdwood  */
18ddee627cSLiam Girdwood 
19ddee627cSLiam Girdwood #include <linux/kernel.h>
20ddee627cSLiam Girdwood #include <linux/init.h>
21ddee627cSLiam Girdwood #include <linux/delay.h>
22988e8cc4SNicolin Chen #include <linux/pinctrl/consumer.h>
23d6652ef8SMark Brown #include <linux/pm_runtime.h>
24ddee627cSLiam Girdwood #include <linux/slab.h>
25ddee627cSLiam Girdwood #include <linux/workqueue.h>
2601d7584cSLiam Girdwood #include <linux/export.h>
27f86dcef8SLiam Girdwood #include <linux/debugfs.h>
28ddee627cSLiam Girdwood #include <sound/core.h>
29ddee627cSLiam Girdwood #include <sound/pcm.h>
30ddee627cSLiam Girdwood #include <sound/pcm_params.h>
31ddee627cSLiam Girdwood #include <sound/soc.h>
3201d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
33ddee627cSLiam Girdwood #include <sound/initval.h>
34ddee627cSLiam Girdwood 
3501d7584cSLiam Girdwood #define DPCM_MAX_BE_USERS	8
3601d7584cSLiam Girdwood 
37*cde79035SRicard Wanderlof /*
38*cde79035SRicard Wanderlof  * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
39*cde79035SRicard Wanderlof  *
40*cde79035SRicard Wanderlof  * Returns true if the DAI supports the indicated stream type.
41*cde79035SRicard Wanderlof  */
42*cde79035SRicard Wanderlof static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
43*cde79035SRicard Wanderlof {
44*cde79035SRicard Wanderlof 	struct snd_soc_pcm_stream *codec_stream;
45*cde79035SRicard Wanderlof 
46*cde79035SRicard Wanderlof 	if (stream == SNDRV_PCM_STREAM_PLAYBACK)
47*cde79035SRicard Wanderlof 		codec_stream = &dai->driver->playback;
48*cde79035SRicard Wanderlof 	else
49*cde79035SRicard Wanderlof 		codec_stream = &dai->driver->capture;
50*cde79035SRicard Wanderlof 
51*cde79035SRicard Wanderlof 	/* If the codec specifies any rate at all, it supports the stream. */
52*cde79035SRicard Wanderlof 	return codec_stream->rates;
53*cde79035SRicard Wanderlof }
54*cde79035SRicard Wanderlof 
5590996f43SLars-Peter Clausen /**
5624894b76SLars-Peter Clausen  * snd_soc_runtime_activate() - Increment active count for PCM runtime components
5724894b76SLars-Peter Clausen  * @rtd: ASoC PCM runtime that is activated
5824894b76SLars-Peter Clausen  * @stream: Direction of the PCM stream
5924894b76SLars-Peter Clausen  *
6024894b76SLars-Peter Clausen  * Increments the active count for all the DAIs and components attached to a PCM
6124894b76SLars-Peter Clausen  * runtime. Should typically be called when a stream is opened.
6224894b76SLars-Peter Clausen  *
6324894b76SLars-Peter Clausen  * Must be called with the rtd->pcm_mutex being held
6424894b76SLars-Peter Clausen  */
6524894b76SLars-Peter Clausen void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
6624894b76SLars-Peter Clausen {
6724894b76SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
682e5894d7SBenoit Cousson 	int i;
6924894b76SLars-Peter Clausen 
7024894b76SLars-Peter Clausen 	lockdep_assert_held(&rtd->pcm_mutex);
7124894b76SLars-Peter Clausen 
7224894b76SLars-Peter Clausen 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
7324894b76SLars-Peter Clausen 		cpu_dai->playback_active++;
742e5894d7SBenoit Cousson 		for (i = 0; i < rtd->num_codecs; i++)
752e5894d7SBenoit Cousson 			rtd->codec_dais[i]->playback_active++;
7624894b76SLars-Peter Clausen 	} else {
7724894b76SLars-Peter Clausen 		cpu_dai->capture_active++;
782e5894d7SBenoit Cousson 		for (i = 0; i < rtd->num_codecs; i++)
792e5894d7SBenoit Cousson 			rtd->codec_dais[i]->capture_active++;
8024894b76SLars-Peter Clausen 	}
8124894b76SLars-Peter Clausen 
8224894b76SLars-Peter Clausen 	cpu_dai->active++;
83cdde4ccbSLars-Peter Clausen 	cpu_dai->component->active++;
842e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
852e5894d7SBenoit Cousson 		rtd->codec_dais[i]->active++;
862e5894d7SBenoit Cousson 		rtd->codec_dais[i]->component->active++;
872e5894d7SBenoit Cousson 	}
8824894b76SLars-Peter Clausen }
8924894b76SLars-Peter Clausen 
9024894b76SLars-Peter Clausen /**
9124894b76SLars-Peter Clausen  * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
9224894b76SLars-Peter Clausen  * @rtd: ASoC PCM runtime that is deactivated
9324894b76SLars-Peter Clausen  * @stream: Direction of the PCM stream
9424894b76SLars-Peter Clausen  *
9524894b76SLars-Peter Clausen  * Decrements the active count for all the DAIs and components attached to a PCM
9624894b76SLars-Peter Clausen  * runtime. Should typically be called when a stream is closed.
9724894b76SLars-Peter Clausen  *
9824894b76SLars-Peter Clausen  * Must be called with the rtd->pcm_mutex being held
9924894b76SLars-Peter Clausen  */
10024894b76SLars-Peter Clausen void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
10124894b76SLars-Peter Clausen {
10224894b76SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1032e5894d7SBenoit Cousson 	int i;
10424894b76SLars-Peter Clausen 
10524894b76SLars-Peter Clausen 	lockdep_assert_held(&rtd->pcm_mutex);
10624894b76SLars-Peter Clausen 
10724894b76SLars-Peter Clausen 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
10824894b76SLars-Peter Clausen 		cpu_dai->playback_active--;
1092e5894d7SBenoit Cousson 		for (i = 0; i < rtd->num_codecs; i++)
1102e5894d7SBenoit Cousson 			rtd->codec_dais[i]->playback_active--;
11124894b76SLars-Peter Clausen 	} else {
11224894b76SLars-Peter Clausen 		cpu_dai->capture_active--;
1132e5894d7SBenoit Cousson 		for (i = 0; i < rtd->num_codecs; i++)
1142e5894d7SBenoit Cousson 			rtd->codec_dais[i]->capture_active--;
11524894b76SLars-Peter Clausen 	}
11624894b76SLars-Peter Clausen 
11724894b76SLars-Peter Clausen 	cpu_dai->active--;
118cdde4ccbSLars-Peter Clausen 	cpu_dai->component->active--;
1192e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
1202e5894d7SBenoit Cousson 		rtd->codec_dais[i]->component->active--;
1212e5894d7SBenoit Cousson 		rtd->codec_dais[i]->active--;
1222e5894d7SBenoit Cousson 	}
12324894b76SLars-Peter Clausen }
12424894b76SLars-Peter Clausen 
12524894b76SLars-Peter Clausen /**
126208a1589SLars-Peter Clausen  * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
127208a1589SLars-Peter Clausen  * @rtd: The ASoC PCM runtime that should be checked.
128208a1589SLars-Peter Clausen  *
129208a1589SLars-Peter Clausen  * This function checks whether the power down delay should be ignored for a
130208a1589SLars-Peter Clausen  * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
131208a1589SLars-Peter Clausen  * been configured to ignore the delay, or if none of the components benefits
132208a1589SLars-Peter Clausen  * from having the delay.
133208a1589SLars-Peter Clausen  */
134208a1589SLars-Peter Clausen bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
135208a1589SLars-Peter Clausen {
1362e5894d7SBenoit Cousson 	int i;
1372e5894d7SBenoit Cousson 	bool ignore = true;
1382e5894d7SBenoit Cousson 
139208a1589SLars-Peter Clausen 	if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
140208a1589SLars-Peter Clausen 		return true;
141208a1589SLars-Peter Clausen 
1422e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
1432e5894d7SBenoit Cousson 		ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
1442e5894d7SBenoit Cousson 
1452e5894d7SBenoit Cousson 	return rtd->cpu_dai->component->ignore_pmdown_time && ignore;
146208a1589SLars-Peter Clausen }
147208a1589SLars-Peter Clausen 
148208a1589SLars-Peter Clausen /**
14990996f43SLars-Peter Clausen  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
15090996f43SLars-Peter Clausen  * @substream: the pcm substream
15190996f43SLars-Peter Clausen  * @hw: the hardware parameters
15290996f43SLars-Peter Clausen  *
15390996f43SLars-Peter Clausen  * Sets the substream runtime hardware parameters.
15490996f43SLars-Peter Clausen  */
15590996f43SLars-Peter Clausen int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
15690996f43SLars-Peter Clausen 	const struct snd_pcm_hardware *hw)
15790996f43SLars-Peter Clausen {
15890996f43SLars-Peter Clausen 	struct snd_pcm_runtime *runtime = substream->runtime;
15990996f43SLars-Peter Clausen 	runtime->hw.info = hw->info;
16090996f43SLars-Peter Clausen 	runtime->hw.formats = hw->formats;
16190996f43SLars-Peter Clausen 	runtime->hw.period_bytes_min = hw->period_bytes_min;
16290996f43SLars-Peter Clausen 	runtime->hw.period_bytes_max = hw->period_bytes_max;
16390996f43SLars-Peter Clausen 	runtime->hw.periods_min = hw->periods_min;
16490996f43SLars-Peter Clausen 	runtime->hw.periods_max = hw->periods_max;
16590996f43SLars-Peter Clausen 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
16690996f43SLars-Peter Clausen 	runtime->hw.fifo_size = hw->fifo_size;
16790996f43SLars-Peter Clausen 	return 0;
16890996f43SLars-Peter Clausen }
16990996f43SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
17090996f43SLars-Peter Clausen 
17101d7584cSLiam Girdwood /* DPCM stream event, send event to FE and all active BEs. */
17223607025SLiam Girdwood int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
17301d7584cSLiam Girdwood 	int event)
17401d7584cSLiam Girdwood {
17501d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
17601d7584cSLiam Girdwood 
17701d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
17801d7584cSLiam Girdwood 
17901d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
18001d7584cSLiam Girdwood 
181103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
18201d7584cSLiam Girdwood 				be->dai_link->name, event, dir);
18301d7584cSLiam Girdwood 
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 
203ddee627cSLiam Girdwood 		ret = snd_pcm_hw_constraint_minmax(substream->runtime,
204ddee627cSLiam Girdwood 						SNDRV_PCM_HW_PARAM_RATE,
20517841020SDong Aisheng 						soc_dai->rate, 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 
2193635bf09SNicolin Chen 		ret = snd_pcm_hw_constraint_minmax(substream->runtime,
2203635bf09SNicolin Chen 						SNDRV_PCM_HW_PARAM_CHANNELS,
2213635bf09SNicolin Chen 						soc_dai->channels,
2223635bf09SNicolin Chen 						soc_dai->channels);
2233635bf09SNicolin Chen 		if (ret < 0) {
2243635bf09SNicolin Chen 			dev_err(soc_dai->dev,
2253635bf09SNicolin Chen 				"ASoC: Unable to apply channel symmetry constraint: %d\n",
2263635bf09SNicolin Chen 				ret);
2273635bf09SNicolin Chen 			return ret;
2283635bf09SNicolin Chen 		}
2293635bf09SNicolin Chen 	}
2303635bf09SNicolin Chen 
2313635bf09SNicolin Chen 	if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
2323635bf09SNicolin Chen 				rtd->dai_link->symmetric_samplebits)) {
2333635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
2343635bf09SNicolin Chen 				soc_dai->sample_bits);
2353635bf09SNicolin Chen 
2363635bf09SNicolin Chen 		ret = snd_pcm_hw_constraint_minmax(substream->runtime,
2373635bf09SNicolin Chen 						SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2383635bf09SNicolin Chen 						soc_dai->sample_bits,
2393635bf09SNicolin Chen 						soc_dai->sample_bits);
2403635bf09SNicolin Chen 		if (ret < 0) {
2413635bf09SNicolin Chen 			dev_err(soc_dai->dev,
2423635bf09SNicolin Chen 				"ASoC: Unable to apply sample bits symmetry constraint: %d\n",
2433635bf09SNicolin Chen 				ret);
2443635bf09SNicolin Chen 			return ret;
2453635bf09SNicolin Chen 		}
2463635bf09SNicolin Chen 	}
247ddee627cSLiam Girdwood 
248ddee627cSLiam Girdwood 	return 0;
249ddee627cSLiam Girdwood }
250ddee627cSLiam Girdwood 
2513635bf09SNicolin Chen static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
2523635bf09SNicolin Chen 				struct snd_pcm_hw_params *params)
2533635bf09SNicolin Chen {
2543635bf09SNicolin Chen 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
2553635bf09SNicolin Chen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2562e5894d7SBenoit Cousson 	unsigned int rate, channels, sample_bits, symmetry, i;
2573635bf09SNicolin Chen 
2583635bf09SNicolin Chen 	rate = params_rate(params);
2593635bf09SNicolin Chen 	channels = params_channels(params);
2603635bf09SNicolin Chen 	sample_bits = snd_pcm_format_physical_width(params_format(params));
2613635bf09SNicolin Chen 
2623635bf09SNicolin Chen 	/* reject unmatched parameters when applying symmetry */
2633635bf09SNicolin Chen 	symmetry = cpu_dai->driver->symmetric_rates ||
2643635bf09SNicolin Chen 		rtd->dai_link->symmetric_rates;
2652e5894d7SBenoit Cousson 
2662e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
2672e5894d7SBenoit Cousson 		symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
2682e5894d7SBenoit Cousson 
2693635bf09SNicolin Chen 	if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
2703635bf09SNicolin Chen 		dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
2713635bf09SNicolin Chen 				cpu_dai->rate, rate);
2723635bf09SNicolin Chen 		return -EINVAL;
2733635bf09SNicolin Chen 	}
2743635bf09SNicolin Chen 
2753635bf09SNicolin Chen 	symmetry = cpu_dai->driver->symmetric_channels ||
2763635bf09SNicolin Chen 		rtd->dai_link->symmetric_channels;
2772e5894d7SBenoit Cousson 
2782e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
2792e5894d7SBenoit Cousson 		symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
2802e5894d7SBenoit Cousson 
2813635bf09SNicolin Chen 	if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
2823635bf09SNicolin Chen 		dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
2833635bf09SNicolin Chen 				cpu_dai->channels, channels);
2843635bf09SNicolin Chen 		return -EINVAL;
2853635bf09SNicolin Chen 	}
2863635bf09SNicolin Chen 
2873635bf09SNicolin Chen 	symmetry = cpu_dai->driver->symmetric_samplebits ||
2883635bf09SNicolin Chen 		rtd->dai_link->symmetric_samplebits;
2892e5894d7SBenoit Cousson 
2902e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
2912e5894d7SBenoit Cousson 		symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
2922e5894d7SBenoit Cousson 
2933635bf09SNicolin Chen 	if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
2943635bf09SNicolin Chen 		dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
2953635bf09SNicolin Chen 				cpu_dai->sample_bits, sample_bits);
2963635bf09SNicolin Chen 		return -EINVAL;
2973635bf09SNicolin Chen 	}
298ddee627cSLiam Girdwood 
299ddee627cSLiam Girdwood 	return 0;
300ddee627cSLiam Girdwood }
301ddee627cSLiam Girdwood 
30262e5f676SLars-Peter Clausen static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
30362e5f676SLars-Peter Clausen {
30462e5f676SLars-Peter Clausen 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
30562e5f676SLars-Peter Clausen 	struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
30662e5f676SLars-Peter Clausen 	struct snd_soc_dai_link *link = rtd->dai_link;
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 
3132e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
3142e5894d7SBenoit Cousson 		symmetry = symmetry ||
3152e5894d7SBenoit Cousson 			rtd->codec_dais[i]->driver->symmetric_rates ||
3162e5894d7SBenoit Cousson 			rtd->codec_dais[i]->driver->symmetric_channels ||
3172e5894d7SBenoit Cousson 			rtd->codec_dais[i]->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) {
3452e5894d7SBenoit Cousson 		for (i = 0; i < rtd->num_codecs; i++) {
3462e5894d7SBenoit Cousson 			codec_dai = rtd->codec_dais[i];
3472e5894d7SBenoit Cousson 			if (codec_dai->driver->playback.sig_bits == 0) {
3482e5894d7SBenoit Cousson 				bits = 0;
3492e5894d7SBenoit Cousson 				break;
3502e5894d7SBenoit Cousson 			}
3512e5894d7SBenoit Cousson 			bits = max(codec_dai->driver->playback.sig_bits, bits);
3522e5894d7SBenoit Cousson 		}
353c8dd1fecSBenoit Cousson 		cpu_bits = cpu_dai->driver->playback.sig_bits;
354c8dd1fecSBenoit Cousson 	} else {
3552e5894d7SBenoit Cousson 		for (i = 0; i < rtd->num_codecs; i++) {
3562e5894d7SBenoit Cousson 			codec_dai = rtd->codec_dais[i];
3575e63dfccSDaniel Mack 			if (codec_dai->driver->capture.sig_bits == 0) {
3582e5894d7SBenoit Cousson 				bits = 0;
3592e5894d7SBenoit Cousson 				break;
3602e5894d7SBenoit Cousson 			}
3612e5894d7SBenoit Cousson 			bits = max(codec_dai->driver->capture.sig_bits, bits);
3622e5894d7SBenoit Cousson 		}
363c8dd1fecSBenoit Cousson 		cpu_bits = cpu_dai->driver->capture.sig_bits;
364c8dd1fecSBenoit Cousson 	}
365c8dd1fecSBenoit Cousson 
3662e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, bits);
3672e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, cpu_bits);
368c8dd1fecSBenoit Cousson }
369c8dd1fecSBenoit Cousson 
3702e5894d7SBenoit Cousson static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
371bd477c31SLars-Peter Clausen {
3722e5894d7SBenoit Cousson 	struct snd_pcm_runtime *runtime = substream->runtime;
37378e45c99SLars-Peter Clausen 	struct snd_pcm_hardware *hw = &runtime->hw;
3742e5894d7SBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
3752e5894d7SBenoit Cousson 	struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
3762e5894d7SBenoit Cousson 	struct snd_soc_dai_driver *codec_dai_drv;
3772e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *codec_stream;
3782e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *cpu_stream;
3792e5894d7SBenoit Cousson 	unsigned int chan_min = 0, chan_max = UINT_MAX;
3802e5894d7SBenoit Cousson 	unsigned int rate_min = 0, rate_max = UINT_MAX;
3812e5894d7SBenoit Cousson 	unsigned int rates = UINT_MAX;
3822e5894d7SBenoit Cousson 	u64 formats = ULLONG_MAX;
3832e5894d7SBenoit Cousson 	int i;
38478e45c99SLars-Peter Clausen 
3852e5894d7SBenoit Cousson 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3862e5894d7SBenoit Cousson 		cpu_stream = &cpu_dai_drv->playback;
38716d7ea91SLars-Peter Clausen 	else
3882e5894d7SBenoit Cousson 		cpu_stream = &cpu_dai_drv->capture;
38978e45c99SLars-Peter Clausen 
3902e5894d7SBenoit Cousson 	/* first calculate min/max only for CODECs in the DAI link */
3912e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
392*cde79035SRicard Wanderlof 
393*cde79035SRicard Wanderlof 		/*
394*cde79035SRicard Wanderlof 		 * Skip CODECs which don't support the current stream type.
395*cde79035SRicard Wanderlof 		 * Otherwise, since the rate, channel, and format values will
396*cde79035SRicard Wanderlof 		 * zero in that case, we would have no usable settings left,
397*cde79035SRicard Wanderlof 		 * causing the resulting setup to fail.
398*cde79035SRicard Wanderlof 		 * At least one CODEC should match, otherwise we should have
399*cde79035SRicard Wanderlof 		 * bailed out on a higher level, since there would be no
400*cde79035SRicard Wanderlof 		 * CODEC to support the transfer direction in that case.
401*cde79035SRicard Wanderlof 		 */
402*cde79035SRicard Wanderlof 		if (!snd_soc_dai_stream_valid(rtd->codec_dais[i],
403*cde79035SRicard Wanderlof 					      substream->stream))
404*cde79035SRicard Wanderlof 			continue;
405*cde79035SRicard Wanderlof 
4062e5894d7SBenoit Cousson 		codec_dai_drv = rtd->codec_dais[i]->driver;
4072e5894d7SBenoit Cousson 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4082e5894d7SBenoit Cousson 			codec_stream = &codec_dai_drv->playback;
4092e5894d7SBenoit Cousson 		else
4102e5894d7SBenoit Cousson 			codec_stream = &codec_dai_drv->capture;
4112e5894d7SBenoit Cousson 		chan_min = max(chan_min, codec_stream->channels_min);
4122e5894d7SBenoit Cousson 		chan_max = min(chan_max, codec_stream->channels_max);
4132e5894d7SBenoit Cousson 		rate_min = max(rate_min, codec_stream->rate_min);
4142e5894d7SBenoit Cousson 		rate_max = min_not_zero(rate_max, codec_stream->rate_max);
4152e5894d7SBenoit Cousson 		formats &= codec_stream->formats;
4162e5894d7SBenoit Cousson 		rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
4172e5894d7SBenoit Cousson 	}
4182e5894d7SBenoit Cousson 
4192e5894d7SBenoit Cousson 	/*
4202e5894d7SBenoit Cousson 	 * chan min/max cannot be enforced if there are multiple CODEC DAIs
4212e5894d7SBenoit Cousson 	 * connected to a single CPU DAI, use CPU DAI's directly and let
4222e5894d7SBenoit Cousson 	 * channel allocation be fixed up later
4232e5894d7SBenoit Cousson 	 */
4242e5894d7SBenoit Cousson 	if (rtd->num_codecs > 1) {
4252e5894d7SBenoit Cousson 		chan_min = cpu_stream->channels_min;
4262e5894d7SBenoit Cousson 		chan_max = cpu_stream->channels_max;
4272e5894d7SBenoit Cousson 	}
4282e5894d7SBenoit Cousson 
4292e5894d7SBenoit Cousson 	hw->channels_min = max(chan_min, cpu_stream->channels_min);
4302e5894d7SBenoit Cousson 	hw->channels_max = min(chan_max, cpu_stream->channels_max);
4312e5894d7SBenoit Cousson 	if (hw->formats)
4322e5894d7SBenoit Cousson 		hw->formats &= formats & cpu_stream->formats;
4332e5894d7SBenoit Cousson 	else
4342e5894d7SBenoit Cousson 		hw->formats = formats & cpu_stream->formats;
4352e5894d7SBenoit Cousson 	hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
43678e45c99SLars-Peter Clausen 
43778e45c99SLars-Peter Clausen 	snd_pcm_limit_hw_rates(runtime);
43878e45c99SLars-Peter Clausen 
43978e45c99SLars-Peter Clausen 	hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
4402e5894d7SBenoit Cousson 	hw->rate_min = max(hw->rate_min, rate_min);
44178e45c99SLars-Peter Clausen 	hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
4422e5894d7SBenoit Cousson 	hw->rate_max = min_not_zero(hw->rate_max, rate_max);
443bd477c31SLars-Peter Clausen }
444bd477c31SLars-Peter Clausen 
44558ba9b25SMark Brown /*
446ddee627cSLiam Girdwood  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
447ddee627cSLiam Girdwood  * then initialized and any private data can be allocated. This also calls
448ddee627cSLiam Girdwood  * startup for the cpu DAI, platform, machine and codec DAI.
449ddee627cSLiam Girdwood  */
450ddee627cSLiam Girdwood static int soc_pcm_open(struct snd_pcm_substream *substream)
451ddee627cSLiam Girdwood {
452ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
453ddee627cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
454ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
455ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4562e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
4572e5894d7SBenoit Cousson 	const char *codec_dai_name = "multicodec";
4582e5894d7SBenoit Cousson 	int i, ret = 0;
459ddee627cSLiam Girdwood 
460988e8cc4SNicolin Chen 	pinctrl_pm_select_default_state(cpu_dai->dev);
4612e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
4622e5894d7SBenoit Cousson 		pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
463d6652ef8SMark Brown 	pm_runtime_get_sync(cpu_dai->dev);
4642e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
4652e5894d7SBenoit Cousson 		pm_runtime_get_sync(rtd->codec_dais[i]->dev);
466d6652ef8SMark Brown 	pm_runtime_get_sync(platform->dev);
467d6652ef8SMark Brown 
468b8c0dab9SLiam Girdwood 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
469ddee627cSLiam Girdwood 
470ddee627cSLiam Girdwood 	/* startup the audio subsystem */
471c5914b0aSMark Brown 	if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
472ddee627cSLiam Girdwood 		ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
473ddee627cSLiam Girdwood 		if (ret < 0) {
474103d84a3SLiam Girdwood 			dev_err(cpu_dai->dev, "ASoC: can't open interface"
475103d84a3SLiam Girdwood 				" %s: %d\n", cpu_dai->name, ret);
476ddee627cSLiam Girdwood 			goto out;
477ddee627cSLiam Girdwood 		}
478ddee627cSLiam Girdwood 	}
479ddee627cSLiam Girdwood 
480ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->open) {
481ddee627cSLiam Girdwood 		ret = platform->driver->ops->open(substream);
482ddee627cSLiam Girdwood 		if (ret < 0) {
483103d84a3SLiam Girdwood 			dev_err(platform->dev, "ASoC: can't open platform"
484f4333203SLars-Peter Clausen 				" %s: %d\n", platform->component.name, ret);
485ddee627cSLiam Girdwood 			goto platform_err;
486ddee627cSLiam Girdwood 		}
487ddee627cSLiam Girdwood 	}
488ddee627cSLiam Girdwood 
4892e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
4902e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
491c5914b0aSMark Brown 		if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
4922e5894d7SBenoit Cousson 			ret = codec_dai->driver->ops->startup(substream,
4932e5894d7SBenoit Cousson 							      codec_dai);
494ddee627cSLiam Girdwood 			if (ret < 0) {
4952e5894d7SBenoit Cousson 				dev_err(codec_dai->dev,
4962e5894d7SBenoit Cousson 					"ASoC: can't open codec %s: %d\n",
4972e5894d7SBenoit Cousson 					codec_dai->name, ret);
498ddee627cSLiam Girdwood 				goto codec_dai_err;
499ddee627cSLiam Girdwood 			}
500ddee627cSLiam Girdwood 		}
501ddee627cSLiam Girdwood 
5022e5894d7SBenoit Cousson 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5032e5894d7SBenoit Cousson 			codec_dai->tx_mask = 0;
5042e5894d7SBenoit Cousson 		else
5052e5894d7SBenoit Cousson 			codec_dai->rx_mask = 0;
5062e5894d7SBenoit Cousson 	}
5072e5894d7SBenoit Cousson 
508ddee627cSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
509ddee627cSLiam Girdwood 		ret = rtd->dai_link->ops->startup(substream);
510ddee627cSLiam Girdwood 		if (ret < 0) {
511103d84a3SLiam Girdwood 			pr_err("ASoC: %s startup failed: %d\n",
51225bfe662SMark Brown 			       rtd->dai_link->name, ret);
513ddee627cSLiam Girdwood 			goto machine_err;
514ddee627cSLiam Girdwood 		}
515ddee627cSLiam Girdwood 	}
516ddee627cSLiam Girdwood 
51701d7584cSLiam Girdwood 	/* Dynamic PCM DAI links compat checks use dynamic capabilities */
51801d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
51901d7584cSLiam Girdwood 		goto dynamic;
52001d7584cSLiam Girdwood 
521ddee627cSLiam Girdwood 	/* Check that the codec and cpu DAIs are compatible */
5222e5894d7SBenoit Cousson 	soc_pcm_init_runtime_hw(substream);
5232e5894d7SBenoit Cousson 
5242e5894d7SBenoit Cousson 	if (rtd->num_codecs == 1)
5252e5894d7SBenoit Cousson 		codec_dai_name = rtd->codec_dai->name;
526ddee627cSLiam Girdwood 
52762e5f676SLars-Peter Clausen 	if (soc_pcm_has_symmetry(substream))
52862e5f676SLars-Peter Clausen 		runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
52962e5f676SLars-Peter Clausen 
530ddee627cSLiam Girdwood 	ret = -EINVAL;
531ddee627cSLiam Girdwood 	if (!runtime->hw.rates) {
532103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
5332e5894d7SBenoit Cousson 			codec_dai_name, cpu_dai->name);
534ddee627cSLiam Girdwood 		goto config_err;
535ddee627cSLiam Girdwood 	}
536ddee627cSLiam Girdwood 	if (!runtime->hw.formats) {
537103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
5382e5894d7SBenoit Cousson 			codec_dai_name, cpu_dai->name);
539ddee627cSLiam Girdwood 		goto config_err;
540ddee627cSLiam Girdwood 	}
541ddee627cSLiam Girdwood 	if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
542ddee627cSLiam Girdwood 	    runtime->hw.channels_min > runtime->hw.channels_max) {
543103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
5442e5894d7SBenoit Cousson 				codec_dai_name, cpu_dai->name);
545ddee627cSLiam Girdwood 		goto config_err;
546ddee627cSLiam Girdwood 	}
547ddee627cSLiam Girdwood 
548c8dd1fecSBenoit Cousson 	soc_pcm_apply_msb(substream);
54958ba9b25SMark Brown 
550ddee627cSLiam Girdwood 	/* Symmetry only applies if we've already got an active stream. */
55117841020SDong Aisheng 	if (cpu_dai->active) {
55217841020SDong Aisheng 		ret = soc_pcm_apply_symmetry(substream, cpu_dai);
55317841020SDong Aisheng 		if (ret != 0)
55417841020SDong Aisheng 			goto config_err;
55517841020SDong Aisheng 	}
55617841020SDong Aisheng 
5572e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
5582e5894d7SBenoit Cousson 		if (rtd->codec_dais[i]->active) {
5592e5894d7SBenoit Cousson 			ret = soc_pcm_apply_symmetry(substream,
5602e5894d7SBenoit Cousson 						     rtd->codec_dais[i]);
561ddee627cSLiam Girdwood 			if (ret != 0)
562ddee627cSLiam Girdwood 				goto config_err;
563ddee627cSLiam Girdwood 		}
5642e5894d7SBenoit Cousson 	}
565ddee627cSLiam Girdwood 
566103d84a3SLiam Girdwood 	pr_debug("ASoC: %s <-> %s info:\n",
5672e5894d7SBenoit Cousson 			codec_dai_name, cpu_dai->name);
568103d84a3SLiam Girdwood 	pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
569103d84a3SLiam Girdwood 	pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
570ddee627cSLiam Girdwood 		 runtime->hw.channels_max);
571103d84a3SLiam Girdwood 	pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
572ddee627cSLiam Girdwood 		 runtime->hw.rate_max);
573ddee627cSLiam Girdwood 
57401d7584cSLiam Girdwood dynamic:
57524894b76SLars-Peter Clausen 
57624894b76SLars-Peter Clausen 	snd_soc_runtime_activate(rtd, substream->stream);
57724894b76SLars-Peter Clausen 
578b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
579ddee627cSLiam Girdwood 	return 0;
580ddee627cSLiam Girdwood 
581ddee627cSLiam Girdwood config_err:
582ddee627cSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
583ddee627cSLiam Girdwood 		rtd->dai_link->ops->shutdown(substream);
584ddee627cSLiam Girdwood 
585ddee627cSLiam Girdwood machine_err:
5862e5894d7SBenoit Cousson 	i = rtd->num_codecs;
587ddee627cSLiam Girdwood 
588ddee627cSLiam Girdwood codec_dai_err:
5892e5894d7SBenoit Cousson 	while (--i >= 0) {
5902e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
5912e5894d7SBenoit Cousson 		if (codec_dai->driver->ops->shutdown)
5922e5894d7SBenoit Cousson 			codec_dai->driver->ops->shutdown(substream, codec_dai);
5932e5894d7SBenoit Cousson 	}
5942e5894d7SBenoit Cousson 
595ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->close)
596ddee627cSLiam Girdwood 		platform->driver->ops->close(substream);
597ddee627cSLiam Girdwood 
598ddee627cSLiam Girdwood platform_err:
599ddee627cSLiam Girdwood 	if (cpu_dai->driver->ops->shutdown)
600ddee627cSLiam Girdwood 		cpu_dai->driver->ops->shutdown(substream, cpu_dai);
601ddee627cSLiam Girdwood out:
602b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
603d6652ef8SMark Brown 
604d6652ef8SMark Brown 	pm_runtime_put(platform->dev);
6052e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
6062e5894d7SBenoit Cousson 		pm_runtime_put(rtd->codec_dais[i]->dev);
607d6652ef8SMark Brown 	pm_runtime_put(cpu_dai->dev);
6082e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
6092e5894d7SBenoit Cousson 		if (!rtd->codec_dais[i]->active)
6102e5894d7SBenoit Cousson 			pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
6112e5894d7SBenoit Cousson 	}
612988e8cc4SNicolin Chen 	if (!cpu_dai->active)
613988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
614d6652ef8SMark Brown 
615ddee627cSLiam Girdwood 	return ret;
616ddee627cSLiam Girdwood }
617ddee627cSLiam Girdwood 
618ddee627cSLiam Girdwood /*
619ddee627cSLiam Girdwood  * Power down the audio subsystem pmdown_time msecs after close is called.
620ddee627cSLiam Girdwood  * This is to ensure there are no pops or clicks in between any music tracks
621ddee627cSLiam Girdwood  * due to DAPM power cycling.
622ddee627cSLiam Girdwood  */
623ddee627cSLiam Girdwood static void close_delayed_work(struct work_struct *work)
624ddee627cSLiam Girdwood {
625ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
626ddee627cSLiam Girdwood 			container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
6272e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
628ddee627cSLiam Girdwood 
629b8c0dab9SLiam Girdwood 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
630ddee627cSLiam Girdwood 
631103d84a3SLiam Girdwood 	dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
632ddee627cSLiam Girdwood 		 codec_dai->driver->playback.stream_name,
633ddee627cSLiam Girdwood 		 codec_dai->playback_active ? "active" : "inactive",
6349bffb1fbSMisael Lopez Cruz 		 rtd->pop_wait ? "yes" : "no");
635ddee627cSLiam Girdwood 
636ddee627cSLiam Girdwood 	/* are we waiting on this codec DAI stream */
6379bffb1fbSMisael Lopez Cruz 	if (rtd->pop_wait == 1) {
6389bffb1fbSMisael Lopez Cruz 		rtd->pop_wait = 0;
6397bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
640d9b0951bSLiam Girdwood 					  SND_SOC_DAPM_STREAM_STOP);
641ddee627cSLiam Girdwood 	}
642ddee627cSLiam Girdwood 
643b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
644ddee627cSLiam Girdwood }
645ddee627cSLiam Girdwood 
646ddee627cSLiam Girdwood /*
647ddee627cSLiam Girdwood  * Called by ALSA when a PCM substream is closed. Private data can be
648ddee627cSLiam Girdwood  * freed here. The cpu DAI, codec DAI, machine and platform are also
649ddee627cSLiam Girdwood  * shutdown.
650ddee627cSLiam Girdwood  */
65191d5e6b4SLiam Girdwood static int soc_pcm_close(struct snd_pcm_substream *substream)
652ddee627cSLiam Girdwood {
653ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
654ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
655ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6562e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
6572e5894d7SBenoit Cousson 	int i;
658ddee627cSLiam Girdwood 
659b8c0dab9SLiam Girdwood 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
660ddee627cSLiam Girdwood 
66124894b76SLars-Peter Clausen 	snd_soc_runtime_deactivate(rtd, substream->stream);
662ddee627cSLiam Girdwood 
66317841020SDong Aisheng 	/* clear the corresponding DAIs rate when inactive */
66417841020SDong Aisheng 	if (!cpu_dai->active)
66517841020SDong Aisheng 		cpu_dai->rate = 0;
66617841020SDong Aisheng 
6672e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
6682e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
66917841020SDong Aisheng 		if (!codec_dai->active)
67017841020SDong Aisheng 			codec_dai->rate = 0;
6712e5894d7SBenoit Cousson 	}
67225b76791SSascha Hauer 
673ae11601bSRamesh Babu 	snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
674ae11601bSRamesh Babu 
675ddee627cSLiam Girdwood 	if (cpu_dai->driver->ops->shutdown)
676ddee627cSLiam Girdwood 		cpu_dai->driver->ops->shutdown(substream, cpu_dai);
677ddee627cSLiam Girdwood 
6782e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
6792e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
680ddee627cSLiam Girdwood 		if (codec_dai->driver->ops->shutdown)
681ddee627cSLiam Girdwood 			codec_dai->driver->ops->shutdown(substream, codec_dai);
6822e5894d7SBenoit Cousson 	}
683ddee627cSLiam Girdwood 
684ddee627cSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
685ddee627cSLiam Girdwood 		rtd->dai_link->ops->shutdown(substream);
686ddee627cSLiam Girdwood 
687ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->close)
688ddee627cSLiam Girdwood 		platform->driver->ops->close(substream);
689ddee627cSLiam Girdwood 
690ddee627cSLiam Girdwood 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
691208a1589SLars-Peter Clausen 		if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
6921d69c5c5SPeter Ujfalusi 			/* powered down playback stream now */
6931d69c5c5SPeter Ujfalusi 			snd_soc_dapm_stream_event(rtd,
6947bd3a6f3SMark Brown 						  SNDRV_PCM_STREAM_PLAYBACK,
6951d69c5c5SPeter Ujfalusi 						  SND_SOC_DAPM_STREAM_STOP);
6961d69c5c5SPeter Ujfalusi 		} else {
697ddee627cSLiam Girdwood 			/* start delayed pop wq here for playback streams */
6989bffb1fbSMisael Lopez Cruz 			rtd->pop_wait = 1;
699d4e1a73aSMark Brown 			queue_delayed_work(system_power_efficient_wq,
700d4e1a73aSMark Brown 					   &rtd->delayed_work,
701ddee627cSLiam Girdwood 					   msecs_to_jiffies(rtd->pmdown_time));
7021d69c5c5SPeter Ujfalusi 		}
703ddee627cSLiam Girdwood 	} else {
704ddee627cSLiam Girdwood 		/* capture streams can be powered down now */
7057bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
706d9b0951bSLiam Girdwood 					  SND_SOC_DAPM_STREAM_STOP);
707ddee627cSLiam Girdwood 	}
708ddee627cSLiam Girdwood 
709b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
710d6652ef8SMark Brown 
711d6652ef8SMark Brown 	pm_runtime_put(platform->dev);
7122e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
7132e5894d7SBenoit Cousson 		pm_runtime_put(rtd->codec_dais[i]->dev);
714d6652ef8SMark Brown 	pm_runtime_put(cpu_dai->dev);
7152e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
7162e5894d7SBenoit Cousson 		if (!rtd->codec_dais[i]->active)
7172e5894d7SBenoit Cousson 			pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
7182e5894d7SBenoit Cousson 	}
719988e8cc4SNicolin Chen 	if (!cpu_dai->active)
720988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
721d6652ef8SMark Brown 
722ddee627cSLiam Girdwood 	return 0;
723ddee627cSLiam Girdwood }
724ddee627cSLiam Girdwood 
725ddee627cSLiam Girdwood /*
726ddee627cSLiam Girdwood  * Called by ALSA when the PCM substream is prepared, can set format, sample
727ddee627cSLiam Girdwood  * rate, etc.  This function is non atomic and can be called multiple times,
728ddee627cSLiam Girdwood  * it can refer to the runtime info.
729ddee627cSLiam Girdwood  */
730ddee627cSLiam Girdwood static int soc_pcm_prepare(struct snd_pcm_substream *substream)
731ddee627cSLiam Girdwood {
732ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
733ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
734ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7352e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
7362e5894d7SBenoit Cousson 	int i, ret = 0;
737ddee627cSLiam Girdwood 
738b8c0dab9SLiam Girdwood 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
739ddee627cSLiam Girdwood 
740ddee627cSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
741ddee627cSLiam Girdwood 		ret = rtd->dai_link->ops->prepare(substream);
742ddee627cSLiam Girdwood 		if (ret < 0) {
743103d84a3SLiam Girdwood 			dev_err(rtd->card->dev, "ASoC: machine prepare error:"
744103d84a3SLiam Girdwood 				" %d\n", ret);
745ddee627cSLiam Girdwood 			goto out;
746ddee627cSLiam Girdwood 		}
747ddee627cSLiam Girdwood 	}
748ddee627cSLiam Girdwood 
749ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->prepare) {
750ddee627cSLiam Girdwood 		ret = platform->driver->ops->prepare(substream);
751ddee627cSLiam Girdwood 		if (ret < 0) {
752103d84a3SLiam Girdwood 			dev_err(platform->dev, "ASoC: platform prepare error:"
753103d84a3SLiam Girdwood 				" %d\n", ret);
754ddee627cSLiam Girdwood 			goto out;
755ddee627cSLiam Girdwood 		}
756ddee627cSLiam Girdwood 	}
757ddee627cSLiam Girdwood 
7582e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
7592e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
760c5914b0aSMark Brown 		if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
7612e5894d7SBenoit Cousson 			ret = codec_dai->driver->ops->prepare(substream,
7622e5894d7SBenoit Cousson 							      codec_dai);
763ddee627cSLiam Girdwood 			if (ret < 0) {
7642e5894d7SBenoit Cousson 				dev_err(codec_dai->dev,
76590cc7f1cSJarkko Nikula 					"ASoC: codec DAI prepare error: %d\n",
76690cc7f1cSJarkko Nikula 					ret);
767ddee627cSLiam Girdwood 				goto out;
768ddee627cSLiam Girdwood 			}
769ddee627cSLiam Girdwood 		}
7702e5894d7SBenoit Cousson 	}
771ddee627cSLiam Girdwood 
772c5914b0aSMark Brown 	if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
773ddee627cSLiam Girdwood 		ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
774ddee627cSLiam Girdwood 		if (ret < 0) {
77590cc7f1cSJarkko Nikula 			dev_err(cpu_dai->dev,
77690cc7f1cSJarkko Nikula 				"ASoC: cpu DAI prepare error: %d\n", ret);
777ddee627cSLiam Girdwood 			goto out;
778ddee627cSLiam Girdwood 		}
779ddee627cSLiam Girdwood 	}
780ddee627cSLiam Girdwood 
781ddee627cSLiam Girdwood 	/* cancel any delayed stream shutdown that is pending */
782ddee627cSLiam Girdwood 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
7839bffb1fbSMisael Lopez Cruz 	    rtd->pop_wait) {
7849bffb1fbSMisael Lopez Cruz 		rtd->pop_wait = 0;
785ddee627cSLiam Girdwood 		cancel_delayed_work(&rtd->delayed_work);
786ddee627cSLiam Girdwood 	}
787ddee627cSLiam Girdwood 
788d9b0951bSLiam Girdwood 	snd_soc_dapm_stream_event(rtd, substream->stream,
789ddee627cSLiam Girdwood 			SND_SOC_DAPM_STREAM_START);
790ddee627cSLiam Girdwood 
7912e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
7922e5894d7SBenoit Cousson 		snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
7932e5894d7SBenoit Cousson 					 substream->stream);
794ae11601bSRamesh Babu 	snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
795ddee627cSLiam Girdwood 
796ddee627cSLiam Girdwood out:
797b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
798ddee627cSLiam Girdwood 	return ret;
799ddee627cSLiam Girdwood }
800ddee627cSLiam Girdwood 
8012e5894d7SBenoit Cousson static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
8022e5894d7SBenoit Cousson 				       unsigned int mask)
8032e5894d7SBenoit Cousson {
8042e5894d7SBenoit Cousson 	struct snd_interval *interval;
8052e5894d7SBenoit Cousson 	int channels = hweight_long(mask);
8062e5894d7SBenoit Cousson 
8072e5894d7SBenoit Cousson 	interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
8082e5894d7SBenoit Cousson 	interval->min = channels;
8092e5894d7SBenoit Cousson 	interval->max = channels;
8102e5894d7SBenoit Cousson }
8112e5894d7SBenoit Cousson 
81293e6958aSBenoit Cousson int soc_dai_hw_params(struct snd_pcm_substream *substream,
81393e6958aSBenoit Cousson 		      struct snd_pcm_hw_params *params,
81493e6958aSBenoit Cousson 		      struct snd_soc_dai *dai)
81593e6958aSBenoit Cousson {
81693e6958aSBenoit Cousson 	int ret;
81793e6958aSBenoit Cousson 
81893e6958aSBenoit Cousson 	if (dai->driver->ops && dai->driver->ops->hw_params) {
81993e6958aSBenoit Cousson 		ret = dai->driver->ops->hw_params(substream, params, dai);
82093e6958aSBenoit Cousson 		if (ret < 0) {
82193e6958aSBenoit Cousson 			dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
82293e6958aSBenoit Cousson 				dai->name, ret);
82393e6958aSBenoit Cousson 			return ret;
82493e6958aSBenoit Cousson 		}
82593e6958aSBenoit Cousson 	}
82693e6958aSBenoit Cousson 
82793e6958aSBenoit Cousson 	return 0;
82893e6958aSBenoit Cousson }
82993e6958aSBenoit Cousson 
830ddee627cSLiam Girdwood /*
831ddee627cSLiam Girdwood  * Called by ALSA when the hardware params are set by application. This
832ddee627cSLiam Girdwood  * function can also be called multiple times and can allocate buffers
833ddee627cSLiam Girdwood  * (using snd_pcm_lib_* ). It's non-atomic.
834ddee627cSLiam Girdwood  */
835ddee627cSLiam Girdwood static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
836ddee627cSLiam Girdwood 				struct snd_pcm_hw_params *params)
837ddee627cSLiam Girdwood {
838ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
839ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
840ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
8412e5894d7SBenoit Cousson 	int i, ret = 0;
842ddee627cSLiam Girdwood 
843b8c0dab9SLiam Girdwood 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
844ddee627cSLiam Girdwood 
8453635bf09SNicolin Chen 	ret = soc_pcm_params_symmetry(substream, params);
8463635bf09SNicolin Chen 	if (ret)
8473635bf09SNicolin Chen 		goto out;
8483635bf09SNicolin Chen 
849ddee627cSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
850ddee627cSLiam Girdwood 		ret = rtd->dai_link->ops->hw_params(substream, params);
851ddee627cSLiam Girdwood 		if (ret < 0) {
852103d84a3SLiam Girdwood 			dev_err(rtd->card->dev, "ASoC: machine hw_params"
853103d84a3SLiam Girdwood 				" failed: %d\n", ret);
854ddee627cSLiam Girdwood 			goto out;
855ddee627cSLiam Girdwood 		}
856ddee627cSLiam Girdwood 	}
857ddee627cSLiam Girdwood 
8582e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
8592e5894d7SBenoit Cousson 		struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
8602e5894d7SBenoit Cousson 		struct snd_pcm_hw_params codec_params;
8612e5894d7SBenoit Cousson 
862*cde79035SRicard Wanderlof 		/*
863*cde79035SRicard Wanderlof 		 * Skip CODECs which don't support the current stream type,
864*cde79035SRicard Wanderlof 		 * the idea being that if a CODEC is not used for the currently
865*cde79035SRicard Wanderlof 		 * set up transfer direction, it should not need to be
866*cde79035SRicard Wanderlof 		 * configured, especially since the configuration used might
867*cde79035SRicard Wanderlof 		 * not even be supported by that CODEC. There may be cases
868*cde79035SRicard Wanderlof 		 * however where a CODEC needs to be set up although it is
869*cde79035SRicard Wanderlof 		 * actually not being used for the transfer, e.g. if a
870*cde79035SRicard Wanderlof 		 * capture-only CODEC is acting as an LRCLK and/or BCLK master
871*cde79035SRicard Wanderlof 		 * for the DAI link including a playback-only CODEC.
872*cde79035SRicard Wanderlof 		 * If this becomes necessary, we will have to augment the
873*cde79035SRicard Wanderlof 		 * machine driver setup with information on how to act, so
874*cde79035SRicard Wanderlof 		 * we can do the right thing here.
875*cde79035SRicard Wanderlof 		 */
876*cde79035SRicard Wanderlof 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
877*cde79035SRicard Wanderlof 			continue;
878*cde79035SRicard Wanderlof 
8792e5894d7SBenoit Cousson 		/* copy params for each codec */
8802e5894d7SBenoit Cousson 		codec_params = *params;
8812e5894d7SBenoit Cousson 
8822e5894d7SBenoit Cousson 		/* fixup params based on TDM slot masks */
8832e5894d7SBenoit Cousson 		if (codec_dai->tx_mask)
8842e5894d7SBenoit Cousson 			soc_pcm_codec_params_fixup(&codec_params,
8852e5894d7SBenoit Cousson 						   codec_dai->tx_mask);
8862e5894d7SBenoit Cousson 		if (codec_dai->rx_mask)
8872e5894d7SBenoit Cousson 			soc_pcm_codec_params_fixup(&codec_params,
8882e5894d7SBenoit Cousson 						   codec_dai->rx_mask);
8892e5894d7SBenoit Cousson 
89093e6958aSBenoit Cousson 		ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
89193e6958aSBenoit Cousson 		if(ret < 0)
892ddee627cSLiam Girdwood 			goto codec_err;
893ddee627cSLiam Girdwood 
8942e5894d7SBenoit Cousson 		codec_dai->rate = params_rate(&codec_params);
8952e5894d7SBenoit Cousson 		codec_dai->channels = params_channels(&codec_params);
8962e5894d7SBenoit Cousson 		codec_dai->sample_bits = snd_pcm_format_physical_width(
8972e5894d7SBenoit Cousson 						params_format(&codec_params));
898ddee627cSLiam Girdwood 	}
899ddee627cSLiam Girdwood 
90093e6958aSBenoit Cousson 	ret = soc_dai_hw_params(substream, params, cpu_dai);
90193e6958aSBenoit Cousson 	if (ret < 0)
902ddee627cSLiam Girdwood 		goto interface_err;
903ddee627cSLiam Girdwood 
904ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->hw_params) {
905ddee627cSLiam Girdwood 		ret = platform->driver->ops->hw_params(substream, params);
906ddee627cSLiam Girdwood 		if (ret < 0) {
907103d84a3SLiam Girdwood 			dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
908f4333203SLars-Peter Clausen 			       platform->component.name, ret);
909ddee627cSLiam Girdwood 			goto platform_err;
910ddee627cSLiam Girdwood 		}
911ddee627cSLiam Girdwood 	}
912ddee627cSLiam Girdwood 
9133635bf09SNicolin Chen 	/* store the parameters for each DAIs */
91417841020SDong Aisheng 	cpu_dai->rate = params_rate(params);
9153635bf09SNicolin Chen 	cpu_dai->channels = params_channels(params);
9163635bf09SNicolin Chen 	cpu_dai->sample_bits =
9173635bf09SNicolin Chen 		snd_pcm_format_physical_width(params_format(params));
9183635bf09SNicolin Chen 
919ddee627cSLiam Girdwood out:
920b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
921ddee627cSLiam Girdwood 	return ret;
922ddee627cSLiam Girdwood 
923ddee627cSLiam Girdwood platform_err:
924c5914b0aSMark Brown 	if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
925ddee627cSLiam Girdwood 		cpu_dai->driver->ops->hw_free(substream, cpu_dai);
926ddee627cSLiam Girdwood 
927ddee627cSLiam Girdwood interface_err:
9282e5894d7SBenoit Cousson 	i = rtd->num_codecs;
929ddee627cSLiam Girdwood 
930ddee627cSLiam Girdwood codec_err:
9312e5894d7SBenoit Cousson 	while (--i >= 0) {
9322e5894d7SBenoit Cousson 		struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
9332e5894d7SBenoit Cousson 		if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
9342e5894d7SBenoit Cousson 			codec_dai->driver->ops->hw_free(substream, codec_dai);
9352e5894d7SBenoit Cousson 		codec_dai->rate = 0;
9362e5894d7SBenoit Cousson 	}
9372e5894d7SBenoit Cousson 
938ddee627cSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
939ddee627cSLiam Girdwood 		rtd->dai_link->ops->hw_free(substream);
940ddee627cSLiam Girdwood 
941b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
942ddee627cSLiam Girdwood 	return ret;
943ddee627cSLiam Girdwood }
944ddee627cSLiam Girdwood 
945ddee627cSLiam Girdwood /*
946ddee627cSLiam Girdwood  * Frees resources allocated by hw_params, can be called multiple times
947ddee627cSLiam Girdwood  */
948ddee627cSLiam Girdwood static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
949ddee627cSLiam Girdwood {
950ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
951ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
952ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
9532e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
9547f62b6eeSNicolin Chen 	bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
9552e5894d7SBenoit Cousson 	int i;
956ddee627cSLiam Girdwood 
957b8c0dab9SLiam Girdwood 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
958ddee627cSLiam Girdwood 
959d3383420SNicolin Chen 	/* clear the corresponding DAIs parameters when going to be inactive */
960d3383420SNicolin Chen 	if (cpu_dai->active == 1) {
961d3383420SNicolin Chen 		cpu_dai->rate = 0;
962d3383420SNicolin Chen 		cpu_dai->channels = 0;
963d3383420SNicolin Chen 		cpu_dai->sample_bits = 0;
964d3383420SNicolin Chen 	}
965d3383420SNicolin Chen 
9662e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
9672e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
968d3383420SNicolin Chen 		if (codec_dai->active == 1) {
969d3383420SNicolin Chen 			codec_dai->rate = 0;
970d3383420SNicolin Chen 			codec_dai->channels = 0;
971d3383420SNicolin Chen 			codec_dai->sample_bits = 0;
972d3383420SNicolin Chen 		}
9732e5894d7SBenoit Cousson 	}
974d3383420SNicolin Chen 
975ddee627cSLiam Girdwood 	/* apply codec digital mute */
9762e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
9772e5894d7SBenoit Cousson 		if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
9782e5894d7SBenoit Cousson 		    (!playback && rtd->codec_dais[i]->capture_active == 1))
9792e5894d7SBenoit Cousson 			snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
9802e5894d7SBenoit Cousson 						 substream->stream);
9812e5894d7SBenoit Cousson 	}
982ddee627cSLiam Girdwood 
983ddee627cSLiam Girdwood 	/* free any machine hw params */
984ddee627cSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
985ddee627cSLiam Girdwood 		rtd->dai_link->ops->hw_free(substream);
986ddee627cSLiam Girdwood 
987ddee627cSLiam Girdwood 	/* free any DMA resources */
988ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->hw_free)
989ddee627cSLiam Girdwood 		platform->driver->ops->hw_free(substream);
990ddee627cSLiam Girdwood 
991ddee627cSLiam Girdwood 	/* now free hw params for the DAIs  */
9922e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
9932e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
994c5914b0aSMark Brown 		if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
995ddee627cSLiam Girdwood 			codec_dai->driver->ops->hw_free(substream, codec_dai);
9962e5894d7SBenoit Cousson 	}
997ddee627cSLiam Girdwood 
998c5914b0aSMark Brown 	if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
999ddee627cSLiam Girdwood 		cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1000ddee627cSLiam Girdwood 
1001b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
1002ddee627cSLiam Girdwood 	return 0;
1003ddee627cSLiam Girdwood }
1004ddee627cSLiam Girdwood 
1005ddee627cSLiam Girdwood static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1006ddee627cSLiam Girdwood {
1007ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1008ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1009ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
10102e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
10112e5894d7SBenoit Cousson 	int i, ret;
1012ddee627cSLiam Girdwood 
10132e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
10142e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
1015c5914b0aSMark Brown 		if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
10162e5894d7SBenoit Cousson 			ret = codec_dai->driver->ops->trigger(substream,
10172e5894d7SBenoit Cousson 							      cmd, codec_dai);
1018ddee627cSLiam Girdwood 			if (ret < 0)
1019ddee627cSLiam Girdwood 				return ret;
1020ddee627cSLiam Girdwood 		}
10212e5894d7SBenoit Cousson 	}
1022ddee627cSLiam Girdwood 
1023ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->trigger) {
1024ddee627cSLiam Girdwood 		ret = platform->driver->ops->trigger(substream, cmd);
1025ddee627cSLiam Girdwood 		if (ret < 0)
1026ddee627cSLiam Girdwood 			return ret;
1027ddee627cSLiam Girdwood 	}
1028ddee627cSLiam Girdwood 
1029c5914b0aSMark Brown 	if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
1030ddee627cSLiam Girdwood 		ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1031ddee627cSLiam Girdwood 		if (ret < 0)
1032ddee627cSLiam Girdwood 			return ret;
1033ddee627cSLiam Girdwood 	}
10344792b0dbSJarkko Nikula 
10354792b0dbSJarkko Nikula 	if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
10364792b0dbSJarkko Nikula 		ret = rtd->dai_link->ops->trigger(substream, cmd);
10374792b0dbSJarkko Nikula 		if (ret < 0)
10384792b0dbSJarkko Nikula 			return ret;
10394792b0dbSJarkko Nikula 	}
10404792b0dbSJarkko Nikula 
1041ddee627cSLiam Girdwood 	return 0;
1042ddee627cSLiam Girdwood }
1043ddee627cSLiam Girdwood 
104445c0a188SMark Brown static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
104545c0a188SMark Brown 				   int cmd)
104607bf84aaSLiam Girdwood {
104707bf84aaSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
104807bf84aaSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
104907bf84aaSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
10502e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
10512e5894d7SBenoit Cousson 	int i, ret;
105207bf84aaSLiam Girdwood 
10532e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
10542e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
1055c5914b0aSMark Brown 		if (codec_dai->driver->ops &&
1056c5914b0aSMark Brown 		    codec_dai->driver->ops->bespoke_trigger) {
10572e5894d7SBenoit Cousson 			ret = codec_dai->driver->ops->bespoke_trigger(substream,
10582e5894d7SBenoit Cousson 								cmd, codec_dai);
105907bf84aaSLiam Girdwood 			if (ret < 0)
106007bf84aaSLiam Girdwood 				return ret;
106107bf84aaSLiam Girdwood 		}
10622e5894d7SBenoit Cousson 	}
106307bf84aaSLiam Girdwood 
1064dcf0fa27SJean-Francois Moine 	if (platform->driver->bespoke_trigger) {
106507bf84aaSLiam Girdwood 		ret = platform->driver->bespoke_trigger(substream, cmd);
106607bf84aaSLiam Girdwood 		if (ret < 0)
106707bf84aaSLiam Girdwood 			return ret;
106807bf84aaSLiam Girdwood 	}
106907bf84aaSLiam Girdwood 
1070c5914b0aSMark Brown 	if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
107107bf84aaSLiam Girdwood 		ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
107207bf84aaSLiam Girdwood 		if (ret < 0)
107307bf84aaSLiam Girdwood 			return ret;
107407bf84aaSLiam Girdwood 	}
107507bf84aaSLiam Girdwood 	return 0;
107607bf84aaSLiam Girdwood }
1077ddee627cSLiam Girdwood /*
1078ddee627cSLiam Girdwood  * soc level wrapper for pointer callback
1079ddee627cSLiam Girdwood  * If cpu_dai, codec_dai, platform driver has the delay callback, than
1080ddee627cSLiam Girdwood  * the runtime->delay will be updated accordingly.
1081ddee627cSLiam Girdwood  */
1082ddee627cSLiam Girdwood static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1083ddee627cSLiam Girdwood {
1084ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1085ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1086ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
10872e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
1088ddee627cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
1089ddee627cSLiam Girdwood 	snd_pcm_uframes_t offset = 0;
1090ddee627cSLiam Girdwood 	snd_pcm_sframes_t delay = 0;
10912e5894d7SBenoit Cousson 	snd_pcm_sframes_t codec_delay = 0;
10922e5894d7SBenoit Cousson 	int i;
1093ddee627cSLiam Girdwood 
1094ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->pointer)
1095ddee627cSLiam Girdwood 		offset = platform->driver->ops->pointer(substream);
1096ddee627cSLiam Girdwood 
1097c5914b0aSMark Brown 	if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
1098ddee627cSLiam Girdwood 		delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1099ddee627cSLiam Girdwood 
11002e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
11012e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
1102c5914b0aSMark Brown 		if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
11032e5894d7SBenoit Cousson 			codec_delay = max(codec_delay,
11042e5894d7SBenoit Cousson 					codec_dai->driver->ops->delay(substream,
11052e5894d7SBenoit Cousson 								    codec_dai));
11062e5894d7SBenoit Cousson 	}
11072e5894d7SBenoit Cousson 	delay += codec_delay;
1108ddee627cSLiam Girdwood 
11092e5894d7SBenoit Cousson 	/*
11102e5894d7SBenoit Cousson 	 * None of the existing platform drivers implement delay(), so
11112e5894d7SBenoit Cousson 	 * for now the codec_dai of first multicodec entry is used
11122e5894d7SBenoit Cousson 	 */
1113ddee627cSLiam Girdwood 	if (platform->driver->delay)
11142e5894d7SBenoit Cousson 		delay += platform->driver->delay(substream, rtd->codec_dais[0]);
1115ddee627cSLiam Girdwood 
1116ddee627cSLiam Girdwood 	runtime->delay = delay;
1117ddee627cSLiam Girdwood 
1118ddee627cSLiam Girdwood 	return offset;
1119ddee627cSLiam Girdwood }
1120ddee627cSLiam Girdwood 
112101d7584cSLiam Girdwood /* connect a FE and BE */
112201d7584cSLiam Girdwood static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
112301d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
112401d7584cSLiam Girdwood {
112501d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
112601d7584cSLiam Girdwood 
112701d7584cSLiam Girdwood 	/* only add new dpcms */
112801d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
112901d7584cSLiam Girdwood 		if (dpcm->be == be && dpcm->fe == fe)
113001d7584cSLiam Girdwood 			return 0;
113101d7584cSLiam Girdwood 	}
113201d7584cSLiam Girdwood 
113301d7584cSLiam Girdwood 	dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
113401d7584cSLiam Girdwood 	if (!dpcm)
113501d7584cSLiam Girdwood 		return -ENOMEM;
113601d7584cSLiam Girdwood 
113701d7584cSLiam Girdwood 	dpcm->be = be;
113801d7584cSLiam Girdwood 	dpcm->fe = fe;
113901d7584cSLiam Girdwood 	be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
114001d7584cSLiam Girdwood 	dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
114101d7584cSLiam Girdwood 	list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
114201d7584cSLiam Girdwood 	list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
114301d7584cSLiam Girdwood 
114401d7584cSLiam Girdwood 	dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
114501d7584cSLiam Girdwood 			stream ? "capture" : "playback",  fe->dai_link->name,
114601d7584cSLiam Girdwood 			stream ? "<-" : "->", be->dai_link->name);
114701d7584cSLiam Girdwood 
1148f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS
11496553bf06SLars-Peter Clausen 	if (fe->debugfs_dpcm_root)
1150f86dcef8SLiam Girdwood 		dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1151f86dcef8SLiam Girdwood 				fe->debugfs_dpcm_root, &dpcm->state);
1152f86dcef8SLiam Girdwood #endif
115301d7584cSLiam Girdwood 	return 1;
115401d7584cSLiam Girdwood }
115501d7584cSLiam Girdwood 
115601d7584cSLiam Girdwood /* reparent a BE onto another FE */
115701d7584cSLiam Girdwood static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
115801d7584cSLiam Girdwood 			struct snd_soc_pcm_runtime *be, int stream)
115901d7584cSLiam Girdwood {
116001d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
116101d7584cSLiam Girdwood 	struct snd_pcm_substream *fe_substream, *be_substream;
116201d7584cSLiam Girdwood 
116301d7584cSLiam Girdwood 	/* reparent if BE is connected to other FEs */
116401d7584cSLiam Girdwood 	if (!be->dpcm[stream].users)
116501d7584cSLiam Girdwood 		return;
116601d7584cSLiam Girdwood 
116701d7584cSLiam Girdwood 	be_substream = snd_soc_dpcm_get_substream(be, stream);
116801d7584cSLiam Girdwood 
116901d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
117001d7584cSLiam Girdwood 		if (dpcm->fe == fe)
117101d7584cSLiam Girdwood 			continue;
117201d7584cSLiam Girdwood 
117301d7584cSLiam Girdwood 		dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
117401d7584cSLiam Girdwood 			stream ? "capture" : "playback",
117501d7584cSLiam Girdwood 			dpcm->fe->dai_link->name,
117601d7584cSLiam Girdwood 			stream ? "<-" : "->", dpcm->be->dai_link->name);
117701d7584cSLiam Girdwood 
117801d7584cSLiam Girdwood 		fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
117901d7584cSLiam Girdwood 		be_substream->runtime = fe_substream->runtime;
118001d7584cSLiam Girdwood 		break;
118101d7584cSLiam Girdwood 	}
118201d7584cSLiam Girdwood }
118301d7584cSLiam Girdwood 
118401d7584cSLiam Girdwood /* disconnect a BE and FE */
118523607025SLiam Girdwood void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
118601d7584cSLiam Girdwood {
118701d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm, *d;
118801d7584cSLiam Girdwood 
118901d7584cSLiam Girdwood 	list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1190103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
119101d7584cSLiam Girdwood 				stream ? "capture" : "playback",
119201d7584cSLiam Girdwood 				dpcm->be->dai_link->name);
119301d7584cSLiam Girdwood 
119401d7584cSLiam Girdwood 		if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
119501d7584cSLiam Girdwood 			continue;
119601d7584cSLiam Girdwood 
119701d7584cSLiam Girdwood 		dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
119801d7584cSLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name,
119901d7584cSLiam Girdwood 			stream ? "<-" : "->", dpcm->be->dai_link->name);
120001d7584cSLiam Girdwood 
120101d7584cSLiam Girdwood 		/* BEs still alive need new FE */
120201d7584cSLiam Girdwood 		dpcm_be_reparent(fe, dpcm->be, stream);
120301d7584cSLiam Girdwood 
1204f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS
1205f86dcef8SLiam Girdwood 		debugfs_remove(dpcm->debugfs_state);
1206f86dcef8SLiam Girdwood #endif
120701d7584cSLiam Girdwood 		list_del(&dpcm->list_be);
120801d7584cSLiam Girdwood 		list_del(&dpcm->list_fe);
120901d7584cSLiam Girdwood 		kfree(dpcm);
121001d7584cSLiam Girdwood 	}
121101d7584cSLiam Girdwood }
121201d7584cSLiam Girdwood 
121301d7584cSLiam Girdwood /* get BE for DAI widget and stream */
121401d7584cSLiam Girdwood static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
121501d7584cSLiam Girdwood 		struct snd_soc_dapm_widget *widget, int stream)
121601d7584cSLiam Girdwood {
121701d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *be;
12182e5894d7SBenoit Cousson 	int i, j;
121901d7584cSLiam Girdwood 
122001d7584cSLiam Girdwood 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
122101d7584cSLiam Girdwood 		for (i = 0; i < card->num_links; i++) {
122201d7584cSLiam Girdwood 			be = &card->rtd[i];
122301d7584cSLiam Girdwood 
122435ea0655SLiam Girdwood 			if (!be->dai_link->no_pcm)
122535ea0655SLiam Girdwood 				continue;
122635ea0655SLiam Girdwood 
12272e5894d7SBenoit Cousson 			if (be->cpu_dai->playback_widget == widget)
122801d7584cSLiam Girdwood 				return be;
12292e5894d7SBenoit Cousson 
12302e5894d7SBenoit Cousson 			for (j = 0; j < be->num_codecs; j++) {
12312e5894d7SBenoit Cousson 				struct snd_soc_dai *dai = be->codec_dais[j];
12322e5894d7SBenoit Cousson 				if (dai->playback_widget == widget)
12332e5894d7SBenoit Cousson 					return be;
12342e5894d7SBenoit Cousson 			}
123501d7584cSLiam Girdwood 		}
123601d7584cSLiam Girdwood 	} else {
123701d7584cSLiam Girdwood 
123801d7584cSLiam Girdwood 		for (i = 0; i < card->num_links; i++) {
123901d7584cSLiam Girdwood 			be = &card->rtd[i];
124001d7584cSLiam Girdwood 
124135ea0655SLiam Girdwood 			if (!be->dai_link->no_pcm)
124235ea0655SLiam Girdwood 				continue;
124335ea0655SLiam Girdwood 
12442e5894d7SBenoit Cousson 			if (be->cpu_dai->capture_widget == widget)
124501d7584cSLiam Girdwood 				return be;
12462e5894d7SBenoit Cousson 
12472e5894d7SBenoit Cousson 			for (j = 0; j < be->num_codecs; j++) {
12482e5894d7SBenoit Cousson 				struct snd_soc_dai *dai = be->codec_dais[j];
12492e5894d7SBenoit Cousson 				if (dai->capture_widget == widget)
12502e5894d7SBenoit Cousson 					return be;
12512e5894d7SBenoit Cousson 			}
125201d7584cSLiam Girdwood 		}
125301d7584cSLiam Girdwood 	}
125401d7584cSLiam Girdwood 
1255103d84a3SLiam Girdwood 	dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
125601d7584cSLiam Girdwood 		stream ? "capture" : "playback", widget->name);
125701d7584cSLiam Girdwood 	return NULL;
125801d7584cSLiam Girdwood }
125901d7584cSLiam Girdwood 
126001d7584cSLiam Girdwood static inline struct snd_soc_dapm_widget *
126137018610SBenoit Cousson 	dai_get_widget(struct snd_soc_dai *dai, int stream)
126201d7584cSLiam Girdwood {
126301d7584cSLiam Girdwood 	if (stream == SNDRV_PCM_STREAM_PLAYBACK)
126437018610SBenoit Cousson 		return dai->playback_widget;
126501d7584cSLiam Girdwood 	else
126637018610SBenoit Cousson 		return dai->capture_widget;
126701d7584cSLiam Girdwood }
126801d7584cSLiam Girdwood 
126901d7584cSLiam Girdwood static int widget_in_list(struct snd_soc_dapm_widget_list *list,
127001d7584cSLiam Girdwood 		struct snd_soc_dapm_widget *widget)
127101d7584cSLiam Girdwood {
127201d7584cSLiam Girdwood 	int i;
127301d7584cSLiam Girdwood 
127401d7584cSLiam Girdwood 	for (i = 0; i < list->num_widgets; i++) {
127501d7584cSLiam Girdwood 		if (widget == list->widgets[i])
127601d7584cSLiam Girdwood 			return 1;
127701d7584cSLiam Girdwood 	}
127801d7584cSLiam Girdwood 
127901d7584cSLiam Girdwood 	return 0;
128001d7584cSLiam Girdwood }
128101d7584cSLiam Girdwood 
128223607025SLiam Girdwood int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
12831ce43acfSLars-Peter Clausen 	int stream, struct snd_soc_dapm_widget_list **list)
128401d7584cSLiam Girdwood {
128501d7584cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = fe->cpu_dai;
128601d7584cSLiam Girdwood 	int paths;
128701d7584cSLiam Girdwood 
128801d7584cSLiam Girdwood 	/* get number of valid DAI paths and their widgets */
12891ce43acfSLars-Peter Clausen 	paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list);
129001d7584cSLiam Girdwood 
1291103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
129201d7584cSLiam Girdwood 			stream ? "capture" : "playback");
129301d7584cSLiam Girdwood 
129401d7584cSLiam Girdwood 	return paths;
129501d7584cSLiam Girdwood }
129601d7584cSLiam Girdwood 
129701d7584cSLiam Girdwood static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
129801d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list **list_)
129901d7584cSLiam Girdwood {
130001d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
130101d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list = *list_;
130201d7584cSLiam Girdwood 	struct snd_soc_dapm_widget *widget;
130301d7584cSLiam Girdwood 	int prune = 0;
130401d7584cSLiam Girdwood 
130501d7584cSLiam Girdwood 	/* Destroy any old FE <--> BE connections */
130601d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
13072e5894d7SBenoit Cousson 		unsigned int i;
130801d7584cSLiam Girdwood 
130901d7584cSLiam Girdwood 		/* is there a valid CPU DAI widget for this BE */
131037018610SBenoit Cousson 		widget = dai_get_widget(dpcm->be->cpu_dai, stream);
131101d7584cSLiam Girdwood 
131201d7584cSLiam Girdwood 		/* prune the BE if it's no longer in our active list */
131301d7584cSLiam Girdwood 		if (widget && widget_in_list(list, widget))
131401d7584cSLiam Girdwood 			continue;
131501d7584cSLiam Girdwood 
131601d7584cSLiam Girdwood 		/* is there a valid CODEC DAI widget for this BE */
13172e5894d7SBenoit Cousson 		for (i = 0; i < dpcm->be->num_codecs; i++) {
13182e5894d7SBenoit Cousson 			struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
13192e5894d7SBenoit Cousson 			widget = dai_get_widget(dai, stream);
132001d7584cSLiam Girdwood 
132101d7584cSLiam Girdwood 			/* prune the BE if it's no longer in our active list */
132201d7584cSLiam Girdwood 			if (widget && widget_in_list(list, widget))
132301d7584cSLiam Girdwood 				continue;
13242e5894d7SBenoit Cousson 		}
132501d7584cSLiam Girdwood 
1326103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
132701d7584cSLiam Girdwood 			stream ? "capture" : "playback",
132801d7584cSLiam Girdwood 			dpcm->be->dai_link->name, fe->dai_link->name);
132901d7584cSLiam Girdwood 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
133001d7584cSLiam Girdwood 		dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
133101d7584cSLiam Girdwood 		prune++;
133201d7584cSLiam Girdwood 	}
133301d7584cSLiam Girdwood 
1334103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
133501d7584cSLiam Girdwood 	return prune;
133601d7584cSLiam Girdwood }
133701d7584cSLiam Girdwood 
133801d7584cSLiam Girdwood static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
133901d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list **list_)
134001d7584cSLiam Girdwood {
134101d7584cSLiam Girdwood 	struct snd_soc_card *card = fe->card;
134201d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list = *list_;
134301d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *be;
134401d7584cSLiam Girdwood 	int i, new = 0, err;
134501d7584cSLiam Girdwood 
134601d7584cSLiam Girdwood 	/* Create any new FE <--> BE connections */
134701d7584cSLiam Girdwood 	for (i = 0; i < list->num_widgets; i++) {
134801d7584cSLiam Girdwood 
13494616274dSMark Brown 		switch (list->widgets[i]->id) {
13504616274dSMark Brown 		case snd_soc_dapm_dai_in:
1351c5b8540dSKoro Chen 			if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1352c5b8540dSKoro Chen 				continue;
1353c5b8540dSKoro Chen 			break;
13544616274dSMark Brown 		case snd_soc_dapm_dai_out:
1355c5b8540dSKoro Chen 			if (stream != SNDRV_PCM_STREAM_CAPTURE)
1356c5b8540dSKoro Chen 				continue;
13574616274dSMark Brown 			break;
13584616274dSMark Brown 		default:
135901d7584cSLiam Girdwood 			continue;
13604616274dSMark Brown 		}
136101d7584cSLiam Girdwood 
136201d7584cSLiam Girdwood 		/* is there a valid BE rtd for this widget */
136301d7584cSLiam Girdwood 		be = dpcm_get_be(card, list->widgets[i], stream);
136401d7584cSLiam Girdwood 		if (!be) {
1365103d84a3SLiam Girdwood 			dev_err(fe->dev, "ASoC: no BE found for %s\n",
136601d7584cSLiam Girdwood 					list->widgets[i]->name);
136701d7584cSLiam Girdwood 			continue;
136801d7584cSLiam Girdwood 		}
136901d7584cSLiam Girdwood 
137001d7584cSLiam Girdwood 		/* make sure BE is a real BE */
137101d7584cSLiam Girdwood 		if (!be->dai_link->no_pcm)
137201d7584cSLiam Girdwood 			continue;
137301d7584cSLiam Girdwood 
137401d7584cSLiam Girdwood 		/* don't connect if FE is not running */
137523607025SLiam Girdwood 		if (!fe->dpcm[stream].runtime && !fe->fe_compr)
137601d7584cSLiam Girdwood 			continue;
137701d7584cSLiam Girdwood 
137801d7584cSLiam Girdwood 		/* newly connected FE and BE */
137901d7584cSLiam Girdwood 		err = dpcm_be_connect(fe, be, stream);
138001d7584cSLiam Girdwood 		if (err < 0) {
1381103d84a3SLiam Girdwood 			dev_err(fe->dev, "ASoC: can't connect %s\n",
138201d7584cSLiam Girdwood 				list->widgets[i]->name);
138301d7584cSLiam Girdwood 			break;
138401d7584cSLiam Girdwood 		} else if (err == 0) /* already connected */
138501d7584cSLiam Girdwood 			continue;
138601d7584cSLiam Girdwood 
138701d7584cSLiam Girdwood 		/* new */
138801d7584cSLiam Girdwood 		be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
138901d7584cSLiam Girdwood 		new++;
139001d7584cSLiam Girdwood 	}
139101d7584cSLiam Girdwood 
1392103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
139301d7584cSLiam Girdwood 	return new;
139401d7584cSLiam Girdwood }
139501d7584cSLiam Girdwood 
139601d7584cSLiam Girdwood /*
139701d7584cSLiam Girdwood  * Find the corresponding BE DAIs that source or sink audio to this
139801d7584cSLiam Girdwood  * FE substream.
139901d7584cSLiam Girdwood  */
140023607025SLiam Girdwood int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
140101d7584cSLiam Girdwood 	int stream, struct snd_soc_dapm_widget_list **list, int new)
140201d7584cSLiam Girdwood {
140301d7584cSLiam Girdwood 	if (new)
140401d7584cSLiam Girdwood 		return dpcm_add_paths(fe, stream, list);
140501d7584cSLiam Girdwood 	else
140601d7584cSLiam Girdwood 		return dpcm_prune_paths(fe, stream, list);
140701d7584cSLiam Girdwood }
140801d7584cSLiam Girdwood 
140923607025SLiam Girdwood void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
141001d7584cSLiam Girdwood {
141101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
141201d7584cSLiam Girdwood 
141301d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
141401d7584cSLiam Girdwood 		dpcm->be->dpcm[stream].runtime_update =
141501d7584cSLiam Girdwood 						SND_SOC_DPCM_UPDATE_NO;
141601d7584cSLiam Girdwood }
141701d7584cSLiam Girdwood 
141801d7584cSLiam Girdwood static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
141901d7584cSLiam Girdwood 	int stream)
142001d7584cSLiam Girdwood {
142101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
142201d7584cSLiam Girdwood 
142301d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
142401d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
142501d7584cSLiam Girdwood 
142601d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
142701d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
142801d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
142901d7584cSLiam Girdwood 
143001d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1431103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
143201d7584cSLiam Girdwood 				stream ? "capture" : "playback",
143301d7584cSLiam Girdwood 				be->dpcm[stream].state);
143401d7584cSLiam Girdwood 
143501d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
143601d7584cSLiam Girdwood 			continue;
143701d7584cSLiam Girdwood 
143801d7584cSLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
143901d7584cSLiam Girdwood 			continue;
144001d7584cSLiam Girdwood 
144101d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
144201d7584cSLiam Girdwood 		be_substream->runtime = NULL;
144301d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
144401d7584cSLiam Girdwood 	}
144501d7584cSLiam Girdwood }
144601d7584cSLiam Girdwood 
144723607025SLiam Girdwood int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
144801d7584cSLiam Girdwood {
144901d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
145001d7584cSLiam Girdwood 	int err, count = 0;
145101d7584cSLiam Girdwood 
145201d7584cSLiam Girdwood 	/* only startup BE DAIs that are either sinks or sources to this FE DAI */
145301d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
145401d7584cSLiam Girdwood 
145501d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
145601d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
145701d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
145801d7584cSLiam Girdwood 
14592062b4c5SRussell King - ARM Linux 		if (!be_substream) {
14602062b4c5SRussell King - ARM Linux 			dev_err(be->dev, "ASoC: no backend %s stream\n",
14612062b4c5SRussell King - ARM Linux 				stream ? "capture" : "playback");
14622062b4c5SRussell King - ARM Linux 			continue;
14632062b4c5SRussell King - ARM Linux 		}
14642062b4c5SRussell King - ARM Linux 
146501d7584cSLiam Girdwood 		/* is this op for this BE ? */
146601d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
146701d7584cSLiam Girdwood 			continue;
146801d7584cSLiam Girdwood 
146901d7584cSLiam Girdwood 		/* first time the dpcm is open ? */
147001d7584cSLiam Girdwood 		if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1471103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: too many users %s at open %d\n",
147201d7584cSLiam Girdwood 				stream ? "capture" : "playback",
147301d7584cSLiam Girdwood 				be->dpcm[stream].state);
147401d7584cSLiam Girdwood 
147501d7584cSLiam Girdwood 		if (be->dpcm[stream].users++ != 0)
147601d7584cSLiam Girdwood 			continue;
147701d7584cSLiam Girdwood 
147801d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
147901d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
148001d7584cSLiam Girdwood 			continue;
148101d7584cSLiam Girdwood 
14822062b4c5SRussell King - ARM Linux 		dev_dbg(be->dev, "ASoC: open %s BE %s\n",
14832062b4c5SRussell King - ARM Linux 			stream ? "capture" : "playback", be->dai_link->name);
148401d7584cSLiam Girdwood 
148501d7584cSLiam Girdwood 		be_substream->runtime = be->dpcm[stream].runtime;
148601d7584cSLiam Girdwood 		err = soc_pcm_open(be_substream);
148701d7584cSLiam Girdwood 		if (err < 0) {
1488103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: BE open failed %d\n", err);
148901d7584cSLiam Girdwood 			be->dpcm[stream].users--;
149001d7584cSLiam Girdwood 			if (be->dpcm[stream].users < 0)
1491103d84a3SLiam Girdwood 				dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
149201d7584cSLiam Girdwood 					stream ? "capture" : "playback",
149301d7584cSLiam Girdwood 					be->dpcm[stream].state);
149401d7584cSLiam Girdwood 
149501d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
149601d7584cSLiam Girdwood 			goto unwind;
149701d7584cSLiam Girdwood 		}
149801d7584cSLiam Girdwood 
149901d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
150001d7584cSLiam Girdwood 		count++;
150101d7584cSLiam Girdwood 	}
150201d7584cSLiam Girdwood 
150301d7584cSLiam Girdwood 	return count;
150401d7584cSLiam Girdwood 
150501d7584cSLiam Girdwood unwind:
150601d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
150701d7584cSLiam Girdwood 	list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
150801d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
150901d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
151001d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
151101d7584cSLiam Girdwood 
151201d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
151301d7584cSLiam Girdwood 			continue;
151401d7584cSLiam Girdwood 
151501d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1516103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close %d\n",
151701d7584cSLiam Girdwood 				stream ? "capture" : "playback",
151801d7584cSLiam Girdwood 				be->dpcm[stream].state);
151901d7584cSLiam Girdwood 
152001d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
152101d7584cSLiam Girdwood 			continue;
152201d7584cSLiam Girdwood 
152301d7584cSLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
152401d7584cSLiam Girdwood 			continue;
152501d7584cSLiam Girdwood 
152601d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
152701d7584cSLiam Girdwood 		be_substream->runtime = NULL;
152801d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
152901d7584cSLiam Girdwood 	}
153001d7584cSLiam Girdwood 
153101d7584cSLiam Girdwood 	return err;
153201d7584cSLiam Girdwood }
153301d7584cSLiam Girdwood 
153408ae9b45SLars-Peter Clausen static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1535b073ed4eSKuninori Morimoto 				 struct snd_soc_pcm_stream *stream,
1536b073ed4eSKuninori Morimoto 				 u64 formats)
153708ae9b45SLars-Peter Clausen {
153808ae9b45SLars-Peter Clausen 	runtime->hw.rate_min = stream->rate_min;
153908ae9b45SLars-Peter Clausen 	runtime->hw.rate_max = stream->rate_max;
154008ae9b45SLars-Peter Clausen 	runtime->hw.channels_min = stream->channels_min;
154108ae9b45SLars-Peter Clausen 	runtime->hw.channels_max = stream->channels_max;
1542002220a9SLars-Peter Clausen 	if (runtime->hw.formats)
1543b073ed4eSKuninori Morimoto 		runtime->hw.formats &= formats & stream->formats;
1544002220a9SLars-Peter Clausen 	else
1545b073ed4eSKuninori Morimoto 		runtime->hw.formats = formats & stream->formats;
154608ae9b45SLars-Peter Clausen 	runtime->hw.rates = stream->rates;
154708ae9b45SLars-Peter Clausen }
154808ae9b45SLars-Peter Clausen 
1549b073ed4eSKuninori Morimoto static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1550b073ed4eSKuninori Morimoto {
1551b073ed4eSKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1552b073ed4eSKuninori Morimoto 	struct snd_soc_dpcm *dpcm;
1553b073ed4eSKuninori Morimoto 	u64 formats = ULLONG_MAX;
1554b073ed4eSKuninori Morimoto 	int stream = substream->stream;
1555b073ed4eSKuninori Morimoto 
1556b073ed4eSKuninori Morimoto 	if (!fe->dai_link->dpcm_merged_format)
1557b073ed4eSKuninori Morimoto 		return formats;
1558b073ed4eSKuninori Morimoto 
1559b073ed4eSKuninori Morimoto 	/*
1560b073ed4eSKuninori Morimoto 	 * It returns merged BE codec format
1561b073ed4eSKuninori Morimoto 	 * if FE want to use it (= dpcm_merged_format)
1562b073ed4eSKuninori Morimoto 	 */
1563b073ed4eSKuninori Morimoto 
1564b073ed4eSKuninori Morimoto 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1565b073ed4eSKuninori Morimoto 		struct snd_soc_pcm_runtime *be = dpcm->be;
1566b073ed4eSKuninori Morimoto 		struct snd_soc_dai_driver *codec_dai_drv;
1567b073ed4eSKuninori Morimoto 		struct snd_soc_pcm_stream *codec_stream;
1568b073ed4eSKuninori Morimoto 		int i;
1569b073ed4eSKuninori Morimoto 
1570b073ed4eSKuninori Morimoto 		for (i = 0; i < be->num_codecs; i++) {
1571b073ed4eSKuninori Morimoto 			codec_dai_drv = be->codec_dais[i]->driver;
1572b073ed4eSKuninori Morimoto 			if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1573b073ed4eSKuninori Morimoto 				codec_stream = &codec_dai_drv->playback;
1574b073ed4eSKuninori Morimoto 			else
1575b073ed4eSKuninori Morimoto 				codec_stream = &codec_dai_drv->capture;
1576b073ed4eSKuninori Morimoto 
1577b073ed4eSKuninori Morimoto 			formats &= codec_stream->formats;
1578b073ed4eSKuninori Morimoto 		}
1579b073ed4eSKuninori Morimoto 	}
1580b073ed4eSKuninori Morimoto 
1581b073ed4eSKuninori Morimoto 	return formats;
1582b073ed4eSKuninori Morimoto }
1583b073ed4eSKuninori Morimoto 
158445c0a188SMark Brown static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
158501d7584cSLiam Girdwood {
158601d7584cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
158701d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
158801d7584cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
158901d7584cSLiam Girdwood 	struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1590b073ed4eSKuninori Morimoto 	u64 format = dpcm_runtime_base_format(substream);
159101d7584cSLiam Girdwood 
159208ae9b45SLars-Peter Clausen 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1593b073ed4eSKuninori Morimoto 		dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
159408ae9b45SLars-Peter Clausen 	else
1595b073ed4eSKuninori Morimoto 		dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
159601d7584cSLiam Girdwood }
159701d7584cSLiam Girdwood 
1598ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1599ea9d0d77STakashi Iwai 
1600ea9d0d77STakashi Iwai /* Set FE's runtime_update state; the state is protected via PCM stream lock
1601ea9d0d77STakashi Iwai  * for avoiding the race with trigger callback.
1602ea9d0d77STakashi Iwai  * If the state is unset and a trigger is pending while the previous operation,
1603ea9d0d77STakashi Iwai  * process the pending trigger action here.
1604ea9d0d77STakashi Iwai  */
1605ea9d0d77STakashi Iwai static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1606ea9d0d77STakashi Iwai 				     int stream, enum snd_soc_dpcm_update state)
1607ea9d0d77STakashi Iwai {
1608ea9d0d77STakashi Iwai 	struct snd_pcm_substream *substream =
1609ea9d0d77STakashi Iwai 		snd_soc_dpcm_get_substream(fe, stream);
1610ea9d0d77STakashi Iwai 
1611ea9d0d77STakashi Iwai 	snd_pcm_stream_lock_irq(substream);
1612ea9d0d77STakashi Iwai 	if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1613ea9d0d77STakashi Iwai 		dpcm_fe_dai_do_trigger(substream,
1614ea9d0d77STakashi Iwai 				       fe->dpcm[stream].trigger_pending - 1);
1615ea9d0d77STakashi Iwai 		fe->dpcm[stream].trigger_pending = 0;
1616ea9d0d77STakashi Iwai 	}
1617ea9d0d77STakashi Iwai 	fe->dpcm[stream].runtime_update = state;
1618ea9d0d77STakashi Iwai 	snd_pcm_stream_unlock_irq(substream);
1619ea9d0d77STakashi Iwai }
1620ea9d0d77STakashi Iwai 
162101d7584cSLiam Girdwood static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
162201d7584cSLiam Girdwood {
162301d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
162401d7584cSLiam Girdwood 	struct snd_pcm_runtime *runtime = fe_substream->runtime;
162501d7584cSLiam Girdwood 	int stream = fe_substream->stream, ret = 0;
162601d7584cSLiam Girdwood 
1627ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
162801d7584cSLiam Girdwood 
162901d7584cSLiam Girdwood 	ret = dpcm_be_dai_startup(fe, fe_substream->stream);
163001d7584cSLiam Girdwood 	if (ret < 0) {
1631103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
163201d7584cSLiam Girdwood 		goto be_err;
163301d7584cSLiam Girdwood 	}
163401d7584cSLiam Girdwood 
1635103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
163601d7584cSLiam Girdwood 
163701d7584cSLiam Girdwood 	/* start the DAI frontend */
163801d7584cSLiam Girdwood 	ret = soc_pcm_open(fe_substream);
163901d7584cSLiam Girdwood 	if (ret < 0) {
1640103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
164101d7584cSLiam Girdwood 		goto unwind;
164201d7584cSLiam Girdwood 	}
164301d7584cSLiam Girdwood 
164401d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
164501d7584cSLiam Girdwood 
164601d7584cSLiam Girdwood 	dpcm_set_fe_runtime(fe_substream);
164701d7584cSLiam Girdwood 	snd_pcm_limit_hw_rates(runtime);
164801d7584cSLiam Girdwood 
1649ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
165001d7584cSLiam Girdwood 	return 0;
165101d7584cSLiam Girdwood 
165201d7584cSLiam Girdwood unwind:
165301d7584cSLiam Girdwood 	dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
165401d7584cSLiam Girdwood be_err:
1655ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
165601d7584cSLiam Girdwood 	return ret;
165701d7584cSLiam Girdwood }
165801d7584cSLiam Girdwood 
165923607025SLiam Girdwood int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
166001d7584cSLiam Girdwood {
166101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
166201d7584cSLiam Girdwood 
166301d7584cSLiam Girdwood 	/* only shutdown BEs that are either sinks or sources to this FE DAI */
166401d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
166501d7584cSLiam Girdwood 
166601d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
166701d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
166801d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
166901d7584cSLiam Girdwood 
167001d7584cSLiam Girdwood 		/* is this op for this BE ? */
167101d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
167201d7584cSLiam Girdwood 			continue;
167301d7584cSLiam Girdwood 
167401d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1675103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
167601d7584cSLiam Girdwood 				stream ? "capture" : "playback",
167701d7584cSLiam Girdwood 				be->dpcm[stream].state);
167801d7584cSLiam Girdwood 
167901d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
168001d7584cSLiam Girdwood 			continue;
168101d7584cSLiam Girdwood 
168201d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
168301d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
168401d7584cSLiam Girdwood 			continue;
168501d7584cSLiam Girdwood 
1686103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: close BE %s\n",
168701d7584cSLiam Girdwood 			dpcm->fe->dai_link->name);
168801d7584cSLiam Girdwood 
168901d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
169001d7584cSLiam Girdwood 		be_substream->runtime = NULL;
169101d7584cSLiam Girdwood 
169201d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
169301d7584cSLiam Girdwood 	}
169401d7584cSLiam Girdwood 	return 0;
169501d7584cSLiam Girdwood }
169601d7584cSLiam Girdwood 
169701d7584cSLiam Girdwood static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
169801d7584cSLiam Girdwood {
169901d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
170001d7584cSLiam Girdwood 	int stream = substream->stream;
170101d7584cSLiam Girdwood 
1702ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
170301d7584cSLiam Girdwood 
170401d7584cSLiam Girdwood 	/* shutdown the BEs */
170501d7584cSLiam Girdwood 	dpcm_be_dai_shutdown(fe, substream->stream);
170601d7584cSLiam Girdwood 
1707103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
170801d7584cSLiam Girdwood 
170901d7584cSLiam Girdwood 	/* now shutdown the frontend */
171001d7584cSLiam Girdwood 	soc_pcm_close(substream);
171101d7584cSLiam Girdwood 
171201d7584cSLiam Girdwood 	/* run the stream event for each BE */
171301d7584cSLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
171401d7584cSLiam Girdwood 
171501d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1716ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
171701d7584cSLiam Girdwood 	return 0;
171801d7584cSLiam Girdwood }
171901d7584cSLiam Girdwood 
172023607025SLiam Girdwood int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
172101d7584cSLiam Girdwood {
172201d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
172301d7584cSLiam Girdwood 
172401d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
172501d7584cSLiam Girdwood 	 * to this frontend DAI */
172601d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
172701d7584cSLiam Girdwood 
172801d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
172901d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
173001d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
173101d7584cSLiam Girdwood 
173201d7584cSLiam Girdwood 		/* is this op for this BE ? */
173301d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
173401d7584cSLiam Girdwood 			continue;
173501d7584cSLiam Girdwood 
173601d7584cSLiam Girdwood 		/* only free hw when no longer used - check all FEs */
173701d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
173801d7584cSLiam Girdwood 				continue;
173901d7584cSLiam Girdwood 
174036fba62cSQiao Zhou 		/* do not free hw if this BE is used by other FE */
174136fba62cSQiao Zhou 		if (be->dpcm[stream].users > 1)
174236fba62cSQiao Zhou 			continue;
174336fba62cSQiao Zhou 
174401d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
174501d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
174601d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
174708b27848SPatrick Lai 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
174801d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
174901d7584cSLiam Girdwood 			continue;
175001d7584cSLiam Girdwood 
1751103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
175201d7584cSLiam Girdwood 			dpcm->fe->dai_link->name);
175301d7584cSLiam Girdwood 
175401d7584cSLiam Girdwood 		soc_pcm_hw_free(be_substream);
175501d7584cSLiam Girdwood 
175601d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
175701d7584cSLiam Girdwood 	}
175801d7584cSLiam Girdwood 
175901d7584cSLiam Girdwood 	return 0;
176001d7584cSLiam Girdwood }
176101d7584cSLiam Girdwood 
176245c0a188SMark Brown static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
176301d7584cSLiam Girdwood {
176401d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
176501d7584cSLiam Girdwood 	int err, stream = substream->stream;
176601d7584cSLiam Girdwood 
176701d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1768ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
176901d7584cSLiam Girdwood 
1770103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
177101d7584cSLiam Girdwood 
177201d7584cSLiam Girdwood 	/* call hw_free on the frontend */
177301d7584cSLiam Girdwood 	err = soc_pcm_hw_free(substream);
177401d7584cSLiam Girdwood 	if (err < 0)
1775103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
177601d7584cSLiam Girdwood 			fe->dai_link->name);
177701d7584cSLiam Girdwood 
177801d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
177901d7584cSLiam Girdwood 	 * to this frontend DAI */
178001d7584cSLiam Girdwood 	err = dpcm_be_dai_hw_free(fe, stream);
178101d7584cSLiam Girdwood 
178201d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1783ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
178401d7584cSLiam Girdwood 
178501d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
178601d7584cSLiam Girdwood 	return 0;
178701d7584cSLiam Girdwood }
178801d7584cSLiam Girdwood 
178923607025SLiam Girdwood int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
179001d7584cSLiam Girdwood {
179101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
179201d7584cSLiam Girdwood 	int ret;
179301d7584cSLiam Girdwood 
179401d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
179501d7584cSLiam Girdwood 
179601d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
179701d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
179801d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
179901d7584cSLiam Girdwood 
180001d7584cSLiam Girdwood 		/* is this op for this BE ? */
180101d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
180201d7584cSLiam Girdwood 			continue;
180301d7584cSLiam Girdwood 
180401d7584cSLiam Girdwood 		/* only allow hw_params() if no connected FEs are running */
180501d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_params(fe, be, stream))
180601d7584cSLiam Girdwood 			continue;
180701d7584cSLiam Girdwood 
180801d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
180901d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
181001d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
181101d7584cSLiam Girdwood 			continue;
181201d7584cSLiam Girdwood 
1813103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
181401d7584cSLiam Girdwood 			dpcm->fe->dai_link->name);
181501d7584cSLiam Girdwood 
181601d7584cSLiam Girdwood 		/* copy params for each dpcm */
181701d7584cSLiam Girdwood 		memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
181801d7584cSLiam Girdwood 				sizeof(struct snd_pcm_hw_params));
181901d7584cSLiam Girdwood 
182001d7584cSLiam Girdwood 		/* perform any hw_params fixups */
182101d7584cSLiam Girdwood 		if (be->dai_link->be_hw_params_fixup) {
182201d7584cSLiam Girdwood 			ret = be->dai_link->be_hw_params_fixup(be,
182301d7584cSLiam Girdwood 					&dpcm->hw_params);
182401d7584cSLiam Girdwood 			if (ret < 0) {
182501d7584cSLiam Girdwood 				dev_err(be->dev,
1826103d84a3SLiam Girdwood 					"ASoC: hw_params BE fixup failed %d\n",
182701d7584cSLiam Girdwood 					ret);
182801d7584cSLiam Girdwood 				goto unwind;
182901d7584cSLiam Girdwood 			}
183001d7584cSLiam Girdwood 		}
183101d7584cSLiam Girdwood 
183201d7584cSLiam Girdwood 		ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
183301d7584cSLiam Girdwood 		if (ret < 0) {
183401d7584cSLiam Girdwood 			dev_err(dpcm->be->dev,
1835103d84a3SLiam Girdwood 				"ASoC: hw_params BE failed %d\n", ret);
183601d7584cSLiam Girdwood 			goto unwind;
183701d7584cSLiam Girdwood 		}
183801d7584cSLiam Girdwood 
183901d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
184001d7584cSLiam Girdwood 	}
184101d7584cSLiam Girdwood 	return 0;
184201d7584cSLiam Girdwood 
184301d7584cSLiam Girdwood unwind:
184401d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
184501d7584cSLiam Girdwood 	list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
184601d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
184701d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
184801d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
184901d7584cSLiam Girdwood 
185001d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
185101d7584cSLiam Girdwood 			continue;
185201d7584cSLiam Girdwood 
185301d7584cSLiam Girdwood 		/* only allow hw_free() if no connected FEs are running */
185401d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
185501d7584cSLiam Girdwood 			continue;
185601d7584cSLiam Girdwood 
185701d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
185801d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
185901d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
186001d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
186101d7584cSLiam Girdwood 			continue;
186201d7584cSLiam Girdwood 
186301d7584cSLiam Girdwood 		soc_pcm_hw_free(be_substream);
186401d7584cSLiam Girdwood 	}
186501d7584cSLiam Girdwood 
186601d7584cSLiam Girdwood 	return ret;
186701d7584cSLiam Girdwood }
186801d7584cSLiam Girdwood 
186945c0a188SMark Brown static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
187001d7584cSLiam Girdwood 				 struct snd_pcm_hw_params *params)
187101d7584cSLiam Girdwood {
187201d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
187301d7584cSLiam Girdwood 	int ret, stream = substream->stream;
187401d7584cSLiam Girdwood 
187501d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1876ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
187701d7584cSLiam Girdwood 
187801d7584cSLiam Girdwood 	memcpy(&fe->dpcm[substream->stream].hw_params, params,
187901d7584cSLiam Girdwood 			sizeof(struct snd_pcm_hw_params));
188001d7584cSLiam Girdwood 	ret = dpcm_be_dai_hw_params(fe, substream->stream);
188101d7584cSLiam Girdwood 	if (ret < 0) {
1882103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
188301d7584cSLiam Girdwood 		goto out;
188401d7584cSLiam Girdwood 	}
188501d7584cSLiam Girdwood 
1886103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
188701d7584cSLiam Girdwood 			fe->dai_link->name, params_rate(params),
188801d7584cSLiam Girdwood 			params_channels(params), params_format(params));
188901d7584cSLiam Girdwood 
189001d7584cSLiam Girdwood 	/* call hw_params on the frontend */
189101d7584cSLiam Girdwood 	ret = soc_pcm_hw_params(substream, params);
189201d7584cSLiam Girdwood 	if (ret < 0) {
1893103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
189401d7584cSLiam Girdwood 		dpcm_be_dai_hw_free(fe, stream);
189501d7584cSLiam Girdwood 	 } else
189601d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
189701d7584cSLiam Girdwood 
189801d7584cSLiam Girdwood out:
1899ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
190001d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
190101d7584cSLiam Girdwood 	return ret;
190201d7584cSLiam Girdwood }
190301d7584cSLiam Girdwood 
190401d7584cSLiam Girdwood static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
190501d7584cSLiam Girdwood 		struct snd_pcm_substream *substream, int cmd)
190601d7584cSLiam Girdwood {
190701d7584cSLiam Girdwood 	int ret;
190801d7584cSLiam Girdwood 
1909103d84a3SLiam Girdwood 	dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
191001d7584cSLiam Girdwood 			dpcm->fe->dai_link->name, cmd);
191101d7584cSLiam Girdwood 
191201d7584cSLiam Girdwood 	ret = soc_pcm_trigger(substream, cmd);
191301d7584cSLiam Girdwood 	if (ret < 0)
1914103d84a3SLiam Girdwood 		dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
191501d7584cSLiam Girdwood 
191601d7584cSLiam Girdwood 	return ret;
191701d7584cSLiam Girdwood }
191801d7584cSLiam Girdwood 
191923607025SLiam Girdwood int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
192045c0a188SMark Brown 			       int cmd)
192101d7584cSLiam Girdwood {
192201d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
192301d7584cSLiam Girdwood 	int ret = 0;
192401d7584cSLiam Girdwood 
192501d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
192601d7584cSLiam Girdwood 
192701d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
192801d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
192901d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
193001d7584cSLiam Girdwood 
193101d7584cSLiam Girdwood 		/* is this op for this BE ? */
193201d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
193301d7584cSLiam Girdwood 			continue;
193401d7584cSLiam Girdwood 
193501d7584cSLiam Girdwood 		switch (cmd) {
193601d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_START:
193701d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
193801d7584cSLiam Girdwood 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
193901d7584cSLiam Girdwood 				continue;
194001d7584cSLiam Girdwood 
194101d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
194201d7584cSLiam Girdwood 			if (ret)
194301d7584cSLiam Girdwood 				return ret;
194401d7584cSLiam Girdwood 
194501d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
194601d7584cSLiam Girdwood 			break;
194701d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_RESUME:
194801d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
194901d7584cSLiam Girdwood 				continue;
195001d7584cSLiam Girdwood 
195101d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
195201d7584cSLiam Girdwood 			if (ret)
195301d7584cSLiam Girdwood 				return ret;
195401d7584cSLiam Girdwood 
195501d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
195601d7584cSLiam Girdwood 			break;
195701d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
195801d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
195901d7584cSLiam Girdwood 				continue;
196001d7584cSLiam Girdwood 
196101d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
196201d7584cSLiam Girdwood 			if (ret)
196301d7584cSLiam Girdwood 				return ret;
196401d7584cSLiam Girdwood 
196501d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
196601d7584cSLiam Girdwood 			break;
196701d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_STOP:
196801d7584cSLiam Girdwood 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
196901d7584cSLiam Girdwood 				continue;
197001d7584cSLiam Girdwood 
197101d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
197201d7584cSLiam Girdwood 				continue;
197301d7584cSLiam Girdwood 
197401d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
197501d7584cSLiam Girdwood 			if (ret)
197601d7584cSLiam Girdwood 				return ret;
197701d7584cSLiam Girdwood 
197801d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
197901d7584cSLiam Girdwood 			break;
198001d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_SUSPEND:
1981868a6ca8SNicolin Chen 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
198201d7584cSLiam Girdwood 				continue;
198301d7584cSLiam Girdwood 
198401d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
198501d7584cSLiam Girdwood 				continue;
198601d7584cSLiam Girdwood 
198701d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
198801d7584cSLiam Girdwood 			if (ret)
198901d7584cSLiam Girdwood 				return ret;
199001d7584cSLiam Girdwood 
199101d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
199201d7584cSLiam Girdwood 			break;
199301d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
199401d7584cSLiam Girdwood 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
199501d7584cSLiam Girdwood 				continue;
199601d7584cSLiam Girdwood 
199701d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
199801d7584cSLiam Girdwood 				continue;
199901d7584cSLiam Girdwood 
200001d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
200101d7584cSLiam Girdwood 			if (ret)
200201d7584cSLiam Girdwood 				return ret;
200301d7584cSLiam Girdwood 
200401d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
200501d7584cSLiam Girdwood 			break;
200601d7584cSLiam Girdwood 		}
200701d7584cSLiam Girdwood 	}
200801d7584cSLiam Girdwood 
200901d7584cSLiam Girdwood 	return ret;
201001d7584cSLiam Girdwood }
201101d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
201201d7584cSLiam Girdwood 
2013ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
201401d7584cSLiam Girdwood {
201501d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
201601d7584cSLiam Girdwood 	int stream = substream->stream, ret;
201701d7584cSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
201801d7584cSLiam Girdwood 
201901d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
202001d7584cSLiam Girdwood 
202101d7584cSLiam Girdwood 	switch (trigger) {
202201d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_PRE:
202301d7584cSLiam Girdwood 		/* call trigger on the frontend before the backend. */
202401d7584cSLiam Girdwood 
2025103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
202601d7584cSLiam Girdwood 				fe->dai_link->name, cmd);
202701d7584cSLiam Girdwood 
202801d7584cSLiam Girdwood 		ret = soc_pcm_trigger(substream, cmd);
202901d7584cSLiam Girdwood 		if (ret < 0) {
2030103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
203101d7584cSLiam Girdwood 			goto out;
203201d7584cSLiam Girdwood 		}
203301d7584cSLiam Girdwood 
203401d7584cSLiam Girdwood 		ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
203501d7584cSLiam Girdwood 		break;
203601d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_POST:
203701d7584cSLiam Girdwood 		/* call trigger on the frontend after the backend. */
203801d7584cSLiam Girdwood 
203901d7584cSLiam Girdwood 		ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
204001d7584cSLiam Girdwood 		if (ret < 0) {
2041103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
204201d7584cSLiam Girdwood 			goto out;
204301d7584cSLiam Girdwood 		}
204401d7584cSLiam Girdwood 
2045103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
204601d7584cSLiam Girdwood 				fe->dai_link->name, cmd);
204701d7584cSLiam Girdwood 
204801d7584cSLiam Girdwood 		ret = soc_pcm_trigger(substream, cmd);
204901d7584cSLiam Girdwood 		break;
205007bf84aaSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_BESPOKE:
205107bf84aaSLiam Girdwood 		/* bespoke trigger() - handles both FE and BEs */
205207bf84aaSLiam Girdwood 
2053103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
205407bf84aaSLiam Girdwood 				fe->dai_link->name, cmd);
205507bf84aaSLiam Girdwood 
205607bf84aaSLiam Girdwood 		ret = soc_pcm_bespoke_trigger(substream, cmd);
205707bf84aaSLiam Girdwood 		if (ret < 0) {
2058103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
205907bf84aaSLiam Girdwood 			goto out;
206007bf84aaSLiam Girdwood 		}
206107bf84aaSLiam Girdwood 		break;
206201d7584cSLiam Girdwood 	default:
2063103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
206401d7584cSLiam Girdwood 				fe->dai_link->name);
206501d7584cSLiam Girdwood 		ret = -EINVAL;
206601d7584cSLiam Girdwood 		goto out;
206701d7584cSLiam Girdwood 	}
206801d7584cSLiam Girdwood 
206901d7584cSLiam Girdwood 	switch (cmd) {
207001d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
207101d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_RESUME:
207201d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
207301d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
207401d7584cSLiam Girdwood 		break;
207501d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_STOP:
207601d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_SUSPEND:
207701d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
207801d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
207901d7584cSLiam Girdwood 		break;
208001d7584cSLiam Girdwood 	}
208101d7584cSLiam Girdwood 
208201d7584cSLiam Girdwood out:
208301d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
208401d7584cSLiam Girdwood 	return ret;
208501d7584cSLiam Girdwood }
208601d7584cSLiam Girdwood 
2087ea9d0d77STakashi Iwai static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2088ea9d0d77STakashi Iwai {
2089ea9d0d77STakashi Iwai 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2090ea9d0d77STakashi Iwai 	int stream = substream->stream;
2091ea9d0d77STakashi Iwai 
2092ea9d0d77STakashi Iwai 	/* if FE's runtime_update is already set, we're in race;
2093ea9d0d77STakashi Iwai 	 * process this trigger later at exit
2094ea9d0d77STakashi Iwai 	 */
2095ea9d0d77STakashi Iwai 	if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2096ea9d0d77STakashi Iwai 		fe->dpcm[stream].trigger_pending = cmd + 1;
2097ea9d0d77STakashi Iwai 		return 0; /* delayed, assuming it's successful */
2098ea9d0d77STakashi Iwai 	}
2099ea9d0d77STakashi Iwai 
2100ea9d0d77STakashi Iwai 	/* we're alone, let's trigger */
2101ea9d0d77STakashi Iwai 	return dpcm_fe_dai_do_trigger(substream, cmd);
2102ea9d0d77STakashi Iwai }
2103ea9d0d77STakashi Iwai 
210423607025SLiam Girdwood int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
210501d7584cSLiam Girdwood {
210601d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
210701d7584cSLiam Girdwood 	int ret = 0;
210801d7584cSLiam Girdwood 
210901d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
211001d7584cSLiam Girdwood 
211101d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
211201d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
211301d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
211401d7584cSLiam Girdwood 
211501d7584cSLiam Girdwood 		/* is this op for this BE ? */
211601d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
211701d7584cSLiam Girdwood 			continue;
211801d7584cSLiam Girdwood 
211901d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
212001d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
212101d7584cSLiam Girdwood 			continue;
212201d7584cSLiam Girdwood 
2123103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: prepare BE %s\n",
212401d7584cSLiam Girdwood 			dpcm->fe->dai_link->name);
212501d7584cSLiam Girdwood 
212601d7584cSLiam Girdwood 		ret = soc_pcm_prepare(be_substream);
212701d7584cSLiam Girdwood 		if (ret < 0) {
2128103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: backend prepare failed %d\n",
212901d7584cSLiam Girdwood 				ret);
213001d7584cSLiam Girdwood 			break;
213101d7584cSLiam Girdwood 		}
213201d7584cSLiam Girdwood 
213301d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
213401d7584cSLiam Girdwood 	}
213501d7584cSLiam Girdwood 	return ret;
213601d7584cSLiam Girdwood }
213701d7584cSLiam Girdwood 
213845c0a188SMark Brown static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
213901d7584cSLiam Girdwood {
214001d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
214101d7584cSLiam Girdwood 	int stream = substream->stream, ret = 0;
214201d7584cSLiam Girdwood 
214301d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
214401d7584cSLiam Girdwood 
2145103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
214601d7584cSLiam Girdwood 
2147ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
214801d7584cSLiam Girdwood 
214901d7584cSLiam Girdwood 	/* there is no point preparing this FE if there are no BEs */
215001d7584cSLiam Girdwood 	if (list_empty(&fe->dpcm[stream].be_clients)) {
2151103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
215201d7584cSLiam Girdwood 				fe->dai_link->name);
215301d7584cSLiam Girdwood 		ret = -EINVAL;
215401d7584cSLiam Girdwood 		goto out;
215501d7584cSLiam Girdwood 	}
215601d7584cSLiam Girdwood 
215701d7584cSLiam Girdwood 	ret = dpcm_be_dai_prepare(fe, substream->stream);
215801d7584cSLiam Girdwood 	if (ret < 0)
215901d7584cSLiam Girdwood 		goto out;
216001d7584cSLiam Girdwood 
216101d7584cSLiam Girdwood 	/* call prepare on the frontend */
216201d7584cSLiam Girdwood 	ret = soc_pcm_prepare(substream);
216301d7584cSLiam Girdwood 	if (ret < 0) {
2164103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
216501d7584cSLiam Girdwood 			fe->dai_link->name);
216601d7584cSLiam Girdwood 		goto out;
216701d7584cSLiam Girdwood 	}
216801d7584cSLiam Girdwood 
216901d7584cSLiam Girdwood 	/* run the stream event for each BE */
217001d7584cSLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
217101d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
217201d7584cSLiam Girdwood 
217301d7584cSLiam Girdwood out:
2174ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
217501d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
217601d7584cSLiam Girdwood 
217701d7584cSLiam Girdwood 	return ret;
217801d7584cSLiam Girdwood }
217901d7584cSLiam Girdwood 
2180be3f3f2cSLiam Girdwood static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2181be3f3f2cSLiam Girdwood 		     unsigned int cmd, void *arg)
2182be3f3f2cSLiam Girdwood {
2183be3f3f2cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
2184be3f3f2cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
2185be3f3f2cSLiam Girdwood 
2186c5914b0aSMark Brown 	if (platform->driver->ops && platform->driver->ops->ioctl)
2187be3f3f2cSLiam Girdwood 		return platform->driver->ops->ioctl(substream, cmd, arg);
2188be3f3f2cSLiam Girdwood 	return snd_pcm_lib_ioctl(substream, cmd, arg);
2189be3f3f2cSLiam Girdwood }
2190be3f3f2cSLiam Girdwood 
2191618dae11SLiam Girdwood static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2192618dae11SLiam Girdwood {
219307bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
219407bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
219507bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2196618dae11SLiam Girdwood 	int err;
219701d7584cSLiam Girdwood 
2198103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2199618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2200618dae11SLiam Girdwood 
220107bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
220207bf84aaSLiam Girdwood 		/* call bespoke trigger - FE takes care of all BE triggers */
2203103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
220407bf84aaSLiam Girdwood 				fe->dai_link->name);
220507bf84aaSLiam Girdwood 
220607bf84aaSLiam Girdwood 		err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
220707bf84aaSLiam Girdwood 		if (err < 0)
2208103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
220907bf84aaSLiam Girdwood 	} else {
2210103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
221107bf84aaSLiam Girdwood 			fe->dai_link->name);
221207bf84aaSLiam Girdwood 
2213618dae11SLiam Girdwood 		err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2214618dae11SLiam Girdwood 		if (err < 0)
2215103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
221607bf84aaSLiam Girdwood 	}
2217618dae11SLiam Girdwood 
2218618dae11SLiam Girdwood 	err = dpcm_be_dai_hw_free(fe, stream);
2219618dae11SLiam Girdwood 	if (err < 0)
2220103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2221618dae11SLiam Girdwood 
2222618dae11SLiam Girdwood 	err = dpcm_be_dai_shutdown(fe, stream);
2223618dae11SLiam Girdwood 	if (err < 0)
2224103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2225618dae11SLiam Girdwood 
2226618dae11SLiam Girdwood 	/* run the stream event for each BE */
2227618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2228618dae11SLiam Girdwood 
2229618dae11SLiam Girdwood 	return 0;
2230618dae11SLiam Girdwood }
2231618dae11SLiam Girdwood 
2232618dae11SLiam Girdwood static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2233618dae11SLiam Girdwood {
223407bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
223507bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
2236618dae11SLiam Girdwood 	struct snd_soc_dpcm *dpcm;
223707bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2238618dae11SLiam Girdwood 	int ret;
2239618dae11SLiam Girdwood 
2240103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2241618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2242618dae11SLiam Girdwood 
2243618dae11SLiam Girdwood 	/* Only start the BE if the FE is ready */
2244618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2245618dae11SLiam Girdwood 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2246618dae11SLiam Girdwood 		return -EINVAL;
2247618dae11SLiam Girdwood 
2248618dae11SLiam Girdwood 	/* startup must always be called for new BEs */
2249618dae11SLiam Girdwood 	ret = dpcm_be_dai_startup(fe, stream);
2250fffc0ca2SDan Carpenter 	if (ret < 0)
2251618dae11SLiam Girdwood 		goto disconnect;
2252618dae11SLiam Girdwood 
2253618dae11SLiam Girdwood 	/* keep going if FE state is > open */
2254618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2255618dae11SLiam Girdwood 		return 0;
2256618dae11SLiam Girdwood 
2257618dae11SLiam Girdwood 	ret = dpcm_be_dai_hw_params(fe, stream);
2258fffc0ca2SDan Carpenter 	if (ret < 0)
2259618dae11SLiam Girdwood 		goto close;
2260618dae11SLiam Girdwood 
2261618dae11SLiam Girdwood 	/* keep going if FE state is > hw_params */
2262618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2263618dae11SLiam Girdwood 		return 0;
2264618dae11SLiam Girdwood 
2265618dae11SLiam Girdwood 
2266618dae11SLiam Girdwood 	ret = dpcm_be_dai_prepare(fe, stream);
2267fffc0ca2SDan Carpenter 	if (ret < 0)
2268618dae11SLiam Girdwood 		goto hw_free;
2269618dae11SLiam Girdwood 
2270618dae11SLiam Girdwood 	/* run the stream event for each BE */
2271618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2272618dae11SLiam Girdwood 
2273618dae11SLiam Girdwood 	/* keep going if FE state is > prepare */
2274618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2275618dae11SLiam Girdwood 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2276618dae11SLiam Girdwood 		return 0;
2277618dae11SLiam Girdwood 
227807bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
227907bf84aaSLiam Girdwood 		/* call trigger on the frontend - FE takes care of all BE triggers */
2280103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
228107bf84aaSLiam Girdwood 				fe->dai_link->name);
228207bf84aaSLiam Girdwood 
228307bf84aaSLiam Girdwood 		ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
228407bf84aaSLiam Girdwood 		if (ret < 0) {
2285103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
228607bf84aaSLiam Girdwood 			goto hw_free;
228707bf84aaSLiam Girdwood 		}
228807bf84aaSLiam Girdwood 	} else {
2289103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2290618dae11SLiam Girdwood 			fe->dai_link->name);
2291618dae11SLiam Girdwood 
2292618dae11SLiam Girdwood 		ret = dpcm_be_dai_trigger(fe, stream,
2293618dae11SLiam Girdwood 					SNDRV_PCM_TRIGGER_START);
2294618dae11SLiam Girdwood 		if (ret < 0) {
2295103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2296618dae11SLiam Girdwood 			goto hw_free;
2297618dae11SLiam Girdwood 		}
229807bf84aaSLiam Girdwood 	}
2299618dae11SLiam Girdwood 
2300618dae11SLiam Girdwood 	return 0;
2301618dae11SLiam Girdwood 
2302618dae11SLiam Girdwood hw_free:
2303618dae11SLiam Girdwood 	dpcm_be_dai_hw_free(fe, stream);
2304618dae11SLiam Girdwood close:
2305618dae11SLiam Girdwood 	dpcm_be_dai_shutdown(fe, stream);
2306618dae11SLiam Girdwood disconnect:
2307618dae11SLiam Girdwood 	/* disconnect any non started BEs */
2308618dae11SLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2309618dae11SLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
2310618dae11SLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2311618dae11SLiam Girdwood 				dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2312618dae11SLiam Girdwood 	}
2313618dae11SLiam Girdwood 
2314618dae11SLiam Girdwood 	return ret;
2315618dae11SLiam Girdwood }
2316618dae11SLiam Girdwood 
2317618dae11SLiam Girdwood static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2318618dae11SLiam Girdwood {
2319618dae11SLiam Girdwood 	int ret;
2320618dae11SLiam Girdwood 
2321ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2322618dae11SLiam Girdwood 	ret = dpcm_run_update_startup(fe, stream);
2323618dae11SLiam Girdwood 	if (ret < 0)
2324103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2325ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2326618dae11SLiam Girdwood 
2327618dae11SLiam Girdwood 	return ret;
2328618dae11SLiam Girdwood }
2329618dae11SLiam Girdwood 
2330618dae11SLiam Girdwood static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2331618dae11SLiam Girdwood {
2332618dae11SLiam Girdwood 	int ret;
2333618dae11SLiam Girdwood 
2334ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2335618dae11SLiam Girdwood 	ret = dpcm_run_update_shutdown(fe, stream);
2336618dae11SLiam Girdwood 	if (ret < 0)
2337103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2338ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2339618dae11SLiam Girdwood 
2340618dae11SLiam Girdwood 	return ret;
2341618dae11SLiam Girdwood }
2342618dae11SLiam Girdwood 
2343618dae11SLiam Girdwood /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2344618dae11SLiam Girdwood  * any DAI links.
2345618dae11SLiam Girdwood  */
2346c3f48ae6SLars-Peter Clausen int soc_dpcm_runtime_update(struct snd_soc_card *card)
2347618dae11SLiam Girdwood {
2348618dae11SLiam Girdwood 	int i, old, new, paths;
2349618dae11SLiam Girdwood 
2350618dae11SLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2351618dae11SLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
2352618dae11SLiam Girdwood 		struct snd_soc_dapm_widget_list *list;
2353618dae11SLiam Girdwood 		struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2354618dae11SLiam Girdwood 
2355618dae11SLiam Girdwood 		/* make sure link is FE */
2356618dae11SLiam Girdwood 		if (!fe->dai_link->dynamic)
2357618dae11SLiam Girdwood 			continue;
2358618dae11SLiam Girdwood 
2359618dae11SLiam Girdwood 		/* only check active links */
2360618dae11SLiam Girdwood 		if (!fe->cpu_dai->active)
2361618dae11SLiam Girdwood 			continue;
2362618dae11SLiam Girdwood 
2363618dae11SLiam Girdwood 		/* DAPM sync will call this to update DSP paths */
2364103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
2365618dae11SLiam Girdwood 			fe->dai_link->name);
2366618dae11SLiam Girdwood 
2367618dae11SLiam Girdwood 		/* skip if FE doesn't have playback capability */
2368075207d2SQiao Zhou 		if (!fe->cpu_dai->driver->playback.channels_min
2369075207d2SQiao Zhou 		    || !fe->codec_dai->driver->playback.channels_min)
2370075207d2SQiao Zhou 			goto capture;
2371075207d2SQiao Zhou 
2372075207d2SQiao Zhou 		/* skip if FE isn't currently playing */
2373075207d2SQiao Zhou 		if (!fe->cpu_dai->playback_active
2374075207d2SQiao Zhou 		    || !fe->codec_dai->playback_active)
2375618dae11SLiam Girdwood 			goto capture;
2376618dae11SLiam Girdwood 
2377618dae11SLiam Girdwood 		paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2378618dae11SLiam Girdwood 		if (paths < 0) {
2379103d84a3SLiam Girdwood 			dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2380618dae11SLiam Girdwood 					fe->dai_link->name,  "playback");
2381618dae11SLiam Girdwood 			mutex_unlock(&card->mutex);
2382618dae11SLiam Girdwood 			return paths;
2383618dae11SLiam Girdwood 		}
2384618dae11SLiam Girdwood 
2385618dae11SLiam Girdwood 		/* update any new playback paths */
2386618dae11SLiam Girdwood 		new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2387618dae11SLiam Girdwood 		if (new) {
2388618dae11SLiam Girdwood 			dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2389618dae11SLiam Girdwood 			dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2390618dae11SLiam Girdwood 			dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2391618dae11SLiam Girdwood 		}
2392618dae11SLiam Girdwood 
2393618dae11SLiam Girdwood 		/* update any old playback paths */
2394618dae11SLiam Girdwood 		old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2395618dae11SLiam Girdwood 		if (old) {
2396618dae11SLiam Girdwood 			dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2397618dae11SLiam Girdwood 			dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2398618dae11SLiam Girdwood 			dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2399618dae11SLiam Girdwood 		}
2400618dae11SLiam Girdwood 
24017ed9de76SQiao Zhou 		dpcm_path_put(&list);
2402618dae11SLiam Girdwood capture:
2403618dae11SLiam Girdwood 		/* skip if FE doesn't have capture capability */
2404075207d2SQiao Zhou 		if (!fe->cpu_dai->driver->capture.channels_min
2405075207d2SQiao Zhou 		    || !fe->codec_dai->driver->capture.channels_min)
2406075207d2SQiao Zhou 			continue;
2407075207d2SQiao Zhou 
2408075207d2SQiao Zhou 		/* skip if FE isn't currently capturing */
2409075207d2SQiao Zhou 		if (!fe->cpu_dai->capture_active
2410075207d2SQiao Zhou 		    || !fe->codec_dai->capture_active)
2411618dae11SLiam Girdwood 			continue;
2412618dae11SLiam Girdwood 
2413618dae11SLiam Girdwood 		paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2414618dae11SLiam Girdwood 		if (paths < 0) {
2415103d84a3SLiam Girdwood 			dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2416618dae11SLiam Girdwood 					fe->dai_link->name,  "capture");
2417618dae11SLiam Girdwood 			mutex_unlock(&card->mutex);
2418618dae11SLiam Girdwood 			return paths;
2419618dae11SLiam Girdwood 		}
2420618dae11SLiam Girdwood 
2421618dae11SLiam Girdwood 		/* update any new capture paths */
2422618dae11SLiam Girdwood 		new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2423618dae11SLiam Girdwood 		if (new) {
2424618dae11SLiam Girdwood 			dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2425618dae11SLiam Girdwood 			dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2426618dae11SLiam Girdwood 			dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2427618dae11SLiam Girdwood 		}
2428618dae11SLiam Girdwood 
2429618dae11SLiam Girdwood 		/* update any old capture paths */
2430618dae11SLiam Girdwood 		old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2431618dae11SLiam Girdwood 		if (old) {
2432618dae11SLiam Girdwood 			dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2433618dae11SLiam Girdwood 			dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2434618dae11SLiam Girdwood 			dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2435618dae11SLiam Girdwood 		}
2436618dae11SLiam Girdwood 
2437618dae11SLiam Girdwood 		dpcm_path_put(&list);
2438618dae11SLiam Girdwood 	}
2439618dae11SLiam Girdwood 
2440618dae11SLiam Girdwood 	mutex_unlock(&card->mutex);
2441618dae11SLiam Girdwood 	return 0;
2442618dae11SLiam Girdwood }
244301d7584cSLiam Girdwood int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
244401d7584cSLiam Girdwood {
244501d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
244601d7584cSLiam Girdwood 	struct list_head *clients =
244701d7584cSLiam Girdwood 		&fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
244801d7584cSLiam Girdwood 
244901d7584cSLiam Girdwood 	list_for_each_entry(dpcm, clients, list_be) {
245001d7584cSLiam Girdwood 
245101d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
24522e5894d7SBenoit Cousson 		int i;
245301d7584cSLiam Girdwood 
245401d7584cSLiam Girdwood 		if (be->dai_link->ignore_suspend)
245501d7584cSLiam Girdwood 			continue;
245601d7584cSLiam Girdwood 
24572e5894d7SBenoit Cousson 		for (i = 0; i < be->num_codecs; i++) {
24582e5894d7SBenoit Cousson 			struct snd_soc_dai *dai = be->codec_dais[i];
24592e5894d7SBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
246001d7584cSLiam Girdwood 
24612e5894d7SBenoit Cousson 			dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
24622e5894d7SBenoit Cousson 					 be->dai_link->name);
24632e5894d7SBenoit Cousson 
24642e5894d7SBenoit Cousson 			if (drv->ops && drv->ops->digital_mute &&
24652e5894d7SBenoit Cousson 							dai->playback_active)
246601d7584cSLiam Girdwood 				drv->ops->digital_mute(dai, mute);
246701d7584cSLiam Girdwood 		}
24682e5894d7SBenoit Cousson 	}
246901d7584cSLiam Girdwood 
247001d7584cSLiam Girdwood 	return 0;
247101d7584cSLiam Girdwood }
247201d7584cSLiam Girdwood 
247345c0a188SMark Brown static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
247401d7584cSLiam Girdwood {
247501d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
247601d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
247701d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list;
247801d7584cSLiam Girdwood 	int ret;
247901d7584cSLiam Girdwood 	int stream = fe_substream->stream;
248001d7584cSLiam Girdwood 
248101d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
248201d7584cSLiam Girdwood 	fe->dpcm[stream].runtime = fe_substream->runtime;
248301d7584cSLiam Girdwood 
24848f70e515SQiao Zhou 	ret = dpcm_path_get(fe, stream, &list);
24858f70e515SQiao Zhou 	if (ret < 0) {
24868f70e515SQiao Zhou 		mutex_unlock(&fe->card->mutex);
24878f70e515SQiao Zhou 		return ret;
24888f70e515SQiao Zhou 	} else if (ret == 0) {
2489103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
249001d7584cSLiam Girdwood 			fe->dai_link->name, stream ? "capture" : "playback");
249101d7584cSLiam Girdwood 	}
249201d7584cSLiam Girdwood 
249301d7584cSLiam Girdwood 	/* calculate valid and active FE <-> BE dpcms */
249401d7584cSLiam Girdwood 	dpcm_process_paths(fe, stream, &list, 1);
249501d7584cSLiam Girdwood 
249601d7584cSLiam Girdwood 	ret = dpcm_fe_dai_startup(fe_substream);
249701d7584cSLiam Girdwood 	if (ret < 0) {
249801d7584cSLiam Girdwood 		/* clean up all links */
249901d7584cSLiam Girdwood 		list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
250001d7584cSLiam Girdwood 			dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
250101d7584cSLiam Girdwood 
250201d7584cSLiam Girdwood 		dpcm_be_disconnect(fe, stream);
250301d7584cSLiam Girdwood 		fe->dpcm[stream].runtime = NULL;
250401d7584cSLiam Girdwood 	}
250501d7584cSLiam Girdwood 
250601d7584cSLiam Girdwood 	dpcm_clear_pending_state(fe, stream);
250701d7584cSLiam Girdwood 	dpcm_path_put(&list);
250801d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
250901d7584cSLiam Girdwood 	return ret;
251001d7584cSLiam Girdwood }
251101d7584cSLiam Girdwood 
251245c0a188SMark Brown static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
251301d7584cSLiam Girdwood {
251401d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
251501d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
251601d7584cSLiam Girdwood 	int stream = fe_substream->stream, ret;
251701d7584cSLiam Girdwood 
251801d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
251901d7584cSLiam Girdwood 	ret = dpcm_fe_dai_shutdown(fe_substream);
252001d7584cSLiam Girdwood 
252101d7584cSLiam Girdwood 	/* mark FE's links ready to prune */
252201d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
252301d7584cSLiam Girdwood 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
252401d7584cSLiam Girdwood 
252501d7584cSLiam Girdwood 	dpcm_be_disconnect(fe, stream);
252601d7584cSLiam Girdwood 
252701d7584cSLiam Girdwood 	fe->dpcm[stream].runtime = NULL;
252801d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
252901d7584cSLiam Girdwood 	return ret;
253001d7584cSLiam Girdwood }
253101d7584cSLiam Girdwood 
2532ddee627cSLiam Girdwood /* create a new pcm */
2533ddee627cSLiam Girdwood int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2534ddee627cSLiam Girdwood {
2535ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
25362e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
2537ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2538ddee627cSLiam Girdwood 	struct snd_pcm *pcm;
2539ddee627cSLiam Girdwood 	char new_name[64];
2540ddee627cSLiam Girdwood 	int ret = 0, playback = 0, capture = 0;
25412e5894d7SBenoit Cousson 	int i;
2542ddee627cSLiam Girdwood 
254301d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
25441e9de42fSLiam Girdwood 		playback = rtd->dai_link->dpcm_playback;
25451e9de42fSLiam Girdwood 		capture = rtd->dai_link->dpcm_capture;
254601d7584cSLiam Girdwood 	} else {
25472e5894d7SBenoit Cousson 		for (i = 0; i < rtd->num_codecs; i++) {
25482e5894d7SBenoit Cousson 			codec_dai = rtd->codec_dais[i];
25492e5894d7SBenoit Cousson 			if (codec_dai->driver->playback.channels_min)
2550ddee627cSLiam Girdwood 				playback = 1;
25512e5894d7SBenoit Cousson 			if (codec_dai->driver->capture.channels_min)
2552ddee627cSLiam Girdwood 				capture = 1;
255301d7584cSLiam Girdwood 		}
2554ddee627cSLiam Girdwood 
25552e5894d7SBenoit Cousson 		capture = capture && cpu_dai->driver->capture.channels_min;
25562e5894d7SBenoit Cousson 		playback = playback && cpu_dai->driver->playback.channels_min;
25572e5894d7SBenoit Cousson 	}
25582e5894d7SBenoit Cousson 
2559d6bead02SFabio Estevam 	if (rtd->dai_link->playback_only) {
2560d6bead02SFabio Estevam 		playback = 1;
2561d6bead02SFabio Estevam 		capture = 0;
2562d6bead02SFabio Estevam 	}
2563d6bead02SFabio Estevam 
2564d6bead02SFabio Estevam 	if (rtd->dai_link->capture_only) {
2565d6bead02SFabio Estevam 		playback = 0;
2566d6bead02SFabio Estevam 		capture = 1;
2567d6bead02SFabio Estevam 	}
2568d6bead02SFabio Estevam 
256901d7584cSLiam Girdwood 	/* create the PCM */
257001d7584cSLiam Girdwood 	if (rtd->dai_link->no_pcm) {
257101d7584cSLiam Girdwood 		snprintf(new_name, sizeof(new_name), "(%s)",
257201d7584cSLiam Girdwood 			rtd->dai_link->stream_name);
257301d7584cSLiam Girdwood 
257401d7584cSLiam Girdwood 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
257501d7584cSLiam Girdwood 				playback, capture, &pcm);
257601d7584cSLiam Girdwood 	} else {
257701d7584cSLiam Girdwood 		if (rtd->dai_link->dynamic)
257801d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s (*)",
257901d7584cSLiam Girdwood 				rtd->dai_link->stream_name);
258001d7584cSLiam Girdwood 		else
258101d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s %s-%d",
25822e5894d7SBenoit Cousson 				rtd->dai_link->stream_name,
25832e5894d7SBenoit Cousson 				(rtd->num_codecs > 1) ?
25842e5894d7SBenoit Cousson 				"multicodec" : rtd->codec_dai->name, num);
258501d7584cSLiam Girdwood 
258601d7584cSLiam Girdwood 		ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
258701d7584cSLiam Girdwood 			capture, &pcm);
258801d7584cSLiam Girdwood 	}
2589ddee627cSLiam Girdwood 	if (ret < 0) {
2590103d84a3SLiam Girdwood 		dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
25915cb9b748SLiam Girdwood 			rtd->dai_link->name);
2592ddee627cSLiam Girdwood 		return ret;
2593ddee627cSLiam Girdwood 	}
2594103d84a3SLiam Girdwood 	dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2595ddee627cSLiam Girdwood 
2596ddee627cSLiam Girdwood 	/* DAPM dai link stream work */
2597ddee627cSLiam Girdwood 	INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2598ddee627cSLiam Girdwood 
259948c7699fSVinod Koul 	pcm->nonatomic = rtd->dai_link->nonatomic;
2600ddee627cSLiam Girdwood 	rtd->pcm = pcm;
2601ddee627cSLiam Girdwood 	pcm->private_data = rtd;
260201d7584cSLiam Girdwood 
260301d7584cSLiam Girdwood 	if (rtd->dai_link->no_pcm) {
260401d7584cSLiam Girdwood 		if (playback)
260501d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
260601d7584cSLiam Girdwood 		if (capture)
260701d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
260801d7584cSLiam Girdwood 		goto out;
260901d7584cSLiam Girdwood 	}
261001d7584cSLiam Girdwood 
261101d7584cSLiam Girdwood 	/* ASoC PCM operations */
261201d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic) {
261301d7584cSLiam Girdwood 		rtd->ops.open		= dpcm_fe_dai_open;
261401d7584cSLiam Girdwood 		rtd->ops.hw_params	= dpcm_fe_dai_hw_params;
261501d7584cSLiam Girdwood 		rtd->ops.prepare	= dpcm_fe_dai_prepare;
261601d7584cSLiam Girdwood 		rtd->ops.trigger	= dpcm_fe_dai_trigger;
261701d7584cSLiam Girdwood 		rtd->ops.hw_free	= dpcm_fe_dai_hw_free;
261801d7584cSLiam Girdwood 		rtd->ops.close		= dpcm_fe_dai_close;
261901d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
2620be3f3f2cSLiam Girdwood 		rtd->ops.ioctl		= soc_pcm_ioctl;
262101d7584cSLiam Girdwood 	} else {
262201d7584cSLiam Girdwood 		rtd->ops.open		= soc_pcm_open;
262301d7584cSLiam Girdwood 		rtd->ops.hw_params	= soc_pcm_hw_params;
262401d7584cSLiam Girdwood 		rtd->ops.prepare	= soc_pcm_prepare;
262501d7584cSLiam Girdwood 		rtd->ops.trigger	= soc_pcm_trigger;
262601d7584cSLiam Girdwood 		rtd->ops.hw_free	= soc_pcm_hw_free;
262701d7584cSLiam Girdwood 		rtd->ops.close		= soc_pcm_close;
262801d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
2629be3f3f2cSLiam Girdwood 		rtd->ops.ioctl		= soc_pcm_ioctl;
263001d7584cSLiam Girdwood 	}
263101d7584cSLiam Girdwood 
2632ddee627cSLiam Girdwood 	if (platform->driver->ops) {
263301d7584cSLiam Girdwood 		rtd->ops.ack		= platform->driver->ops->ack;
263401d7584cSLiam Girdwood 		rtd->ops.copy		= platform->driver->ops->copy;
263501d7584cSLiam Girdwood 		rtd->ops.silence	= platform->driver->ops->silence;
263601d7584cSLiam Girdwood 		rtd->ops.page		= platform->driver->ops->page;
263701d7584cSLiam Girdwood 		rtd->ops.mmap		= platform->driver->ops->mmap;
2638ddee627cSLiam Girdwood 	}
2639ddee627cSLiam Girdwood 
2640ddee627cSLiam Girdwood 	if (playback)
264101d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2642ddee627cSLiam Girdwood 
2643ddee627cSLiam Girdwood 	if (capture)
264401d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2645ddee627cSLiam Girdwood 
2646ddee627cSLiam Girdwood 	if (platform->driver->pcm_new) {
2647ddee627cSLiam Girdwood 		ret = platform->driver->pcm_new(rtd);
2648ddee627cSLiam Girdwood 		if (ret < 0) {
2649b1bc7b3cSMark Brown 			dev_err(platform->dev,
2650b1bc7b3cSMark Brown 				"ASoC: pcm constructor failed: %d\n",
2651b1bc7b3cSMark Brown 				ret);
2652ddee627cSLiam Girdwood 			return ret;
2653ddee627cSLiam Girdwood 		}
2654ddee627cSLiam Girdwood 	}
2655ddee627cSLiam Girdwood 
2656ddee627cSLiam Girdwood 	pcm->private_free = platform->driver->pcm_free;
265701d7584cSLiam Girdwood out:
26582e5894d7SBenoit Cousson 	dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
26592e5894d7SBenoit Cousson 		 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2660ddee627cSLiam Girdwood 		 cpu_dai->name);
2661ddee627cSLiam Girdwood 	return ret;
2662ddee627cSLiam Girdwood }
266301d7584cSLiam Girdwood 
266401d7584cSLiam Girdwood /* is the current PCM operation for this FE ? */
266501d7584cSLiam Girdwood int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
266601d7584cSLiam Girdwood {
266701d7584cSLiam Girdwood 	if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
266801d7584cSLiam Girdwood 		return 1;
266901d7584cSLiam Girdwood 	return 0;
267001d7584cSLiam Girdwood }
267101d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
267201d7584cSLiam Girdwood 
267301d7584cSLiam Girdwood /* is the current PCM operation for this BE ? */
267401d7584cSLiam Girdwood int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
267501d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
267601d7584cSLiam Girdwood {
267701d7584cSLiam Girdwood 	if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
267801d7584cSLiam Girdwood 	   ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
267901d7584cSLiam Girdwood 		  be->dpcm[stream].runtime_update))
268001d7584cSLiam Girdwood 		return 1;
268101d7584cSLiam Girdwood 	return 0;
268201d7584cSLiam Girdwood }
268301d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
268401d7584cSLiam Girdwood 
268501d7584cSLiam Girdwood /* get the substream for this BE */
268601d7584cSLiam Girdwood struct snd_pcm_substream *
268701d7584cSLiam Girdwood 	snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
268801d7584cSLiam Girdwood {
268901d7584cSLiam Girdwood 	return be->pcm->streams[stream].substream;
269001d7584cSLiam Girdwood }
269101d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
269201d7584cSLiam Girdwood 
269301d7584cSLiam Girdwood /* get the BE runtime state */
269401d7584cSLiam Girdwood enum snd_soc_dpcm_state
269501d7584cSLiam Girdwood 	snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
269601d7584cSLiam Girdwood {
269701d7584cSLiam Girdwood 	return be->dpcm[stream].state;
269801d7584cSLiam Girdwood }
269901d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
270001d7584cSLiam Girdwood 
270101d7584cSLiam Girdwood /* set the BE runtime state */
270201d7584cSLiam Girdwood void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
270301d7584cSLiam Girdwood 		int stream, enum snd_soc_dpcm_state state)
270401d7584cSLiam Girdwood {
270501d7584cSLiam Girdwood 	be->dpcm[stream].state = state;
270601d7584cSLiam Girdwood }
270701d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
270801d7584cSLiam Girdwood 
270901d7584cSLiam Girdwood /*
271001d7584cSLiam Girdwood  * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
271101d7584cSLiam Girdwood  * are not running, paused or suspended for the specified stream direction.
271201d7584cSLiam Girdwood  */
271301d7584cSLiam Girdwood int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
271401d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
271501d7584cSLiam Girdwood {
271601d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
271701d7584cSLiam Girdwood 	int state;
271801d7584cSLiam Girdwood 
271901d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
272001d7584cSLiam Girdwood 
272101d7584cSLiam Girdwood 		if (dpcm->fe == fe)
272201d7584cSLiam Girdwood 			continue;
272301d7584cSLiam Girdwood 
272401d7584cSLiam Girdwood 		state = dpcm->fe->dpcm[stream].state;
272501d7584cSLiam Girdwood 		if (state == SND_SOC_DPCM_STATE_START ||
272601d7584cSLiam Girdwood 			state == SND_SOC_DPCM_STATE_PAUSED ||
272701d7584cSLiam Girdwood 			state == SND_SOC_DPCM_STATE_SUSPEND)
272801d7584cSLiam Girdwood 			return 0;
272901d7584cSLiam Girdwood 	}
273001d7584cSLiam Girdwood 
273101d7584cSLiam Girdwood 	/* it's safe to free/stop this BE DAI */
273201d7584cSLiam Girdwood 	return 1;
273301d7584cSLiam Girdwood }
273401d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
273501d7584cSLiam Girdwood 
273601d7584cSLiam Girdwood /*
273701d7584cSLiam Girdwood  * We can only change hw params a BE DAI if any of it's FE are not prepared,
273801d7584cSLiam Girdwood  * running, paused or suspended for the specified stream direction.
273901d7584cSLiam Girdwood  */
274001d7584cSLiam Girdwood int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
274101d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
274201d7584cSLiam Girdwood {
274301d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
274401d7584cSLiam Girdwood 	int state;
274501d7584cSLiam Girdwood 
274601d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
274701d7584cSLiam Girdwood 
274801d7584cSLiam Girdwood 		if (dpcm->fe == fe)
274901d7584cSLiam Girdwood 			continue;
275001d7584cSLiam Girdwood 
275101d7584cSLiam Girdwood 		state = dpcm->fe->dpcm[stream].state;
275201d7584cSLiam Girdwood 		if (state == SND_SOC_DPCM_STATE_START ||
275301d7584cSLiam Girdwood 			state == SND_SOC_DPCM_STATE_PAUSED ||
275401d7584cSLiam Girdwood 			state == SND_SOC_DPCM_STATE_SUSPEND ||
275501d7584cSLiam Girdwood 			state == SND_SOC_DPCM_STATE_PREPARE)
275601d7584cSLiam Girdwood 			return 0;
275701d7584cSLiam Girdwood 	}
275801d7584cSLiam Girdwood 
275901d7584cSLiam Girdwood 	/* it's safe to change hw_params */
276001d7584cSLiam Girdwood 	return 1;
276101d7584cSLiam Girdwood }
276201d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2763f86dcef8SLiam Girdwood 
276407bf84aaSLiam Girdwood int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
276507bf84aaSLiam Girdwood 		int cmd, struct snd_soc_platform *platform)
276607bf84aaSLiam Girdwood {
2767c5914b0aSMark Brown 	if (platform->driver->ops && platform->driver->ops->trigger)
276807bf84aaSLiam Girdwood 		return platform->driver->ops->trigger(substream, cmd);
276907bf84aaSLiam Girdwood 	return 0;
277007bf84aaSLiam Girdwood }
277107bf84aaSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
277207bf84aaSLiam Girdwood 
2773f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS
2774f86dcef8SLiam Girdwood static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2775f86dcef8SLiam Girdwood {
2776f86dcef8SLiam Girdwood 	switch (state) {
2777f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_NEW:
2778f86dcef8SLiam Girdwood 		return "new";
2779f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_OPEN:
2780f86dcef8SLiam Girdwood 		return "open";
2781f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_HW_PARAMS:
2782f86dcef8SLiam Girdwood 		return "hw_params";
2783f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_PREPARE:
2784f86dcef8SLiam Girdwood 		return "prepare";
2785f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_START:
2786f86dcef8SLiam Girdwood 		return "start";
2787f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_STOP:
2788f86dcef8SLiam Girdwood 		return "stop";
2789f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_SUSPEND:
2790f86dcef8SLiam Girdwood 		return "suspend";
2791f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_PAUSED:
2792f86dcef8SLiam Girdwood 		return "paused";
2793f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_HW_FREE:
2794f86dcef8SLiam Girdwood 		return "hw_free";
2795f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_CLOSE:
2796f86dcef8SLiam Girdwood 		return "close";
2797f86dcef8SLiam Girdwood 	}
2798f86dcef8SLiam Girdwood 
2799f86dcef8SLiam Girdwood 	return "unknown";
2800f86dcef8SLiam Girdwood }
2801f86dcef8SLiam Girdwood 
2802f86dcef8SLiam Girdwood static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2803f86dcef8SLiam Girdwood 				int stream, char *buf, size_t size)
2804f86dcef8SLiam Girdwood {
2805f86dcef8SLiam Girdwood 	struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2806f86dcef8SLiam Girdwood 	struct snd_soc_dpcm *dpcm;
2807f86dcef8SLiam Girdwood 	ssize_t offset = 0;
2808f86dcef8SLiam Girdwood 
2809f86dcef8SLiam Girdwood 	/* FE state */
2810f86dcef8SLiam Girdwood 	offset += snprintf(buf + offset, size - offset,
2811f86dcef8SLiam Girdwood 			"[%s - %s]\n", fe->dai_link->name,
2812f86dcef8SLiam Girdwood 			stream ? "Capture" : "Playback");
2813f86dcef8SLiam Girdwood 
2814f86dcef8SLiam Girdwood 	offset += snprintf(buf + offset, size - offset, "State: %s\n",
2815f86dcef8SLiam Girdwood 	                dpcm_state_string(fe->dpcm[stream].state));
2816f86dcef8SLiam Girdwood 
2817f86dcef8SLiam Girdwood 	if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2818f86dcef8SLiam Girdwood 	    (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2819f86dcef8SLiam Girdwood 		offset += snprintf(buf + offset, size - offset,
2820f86dcef8SLiam Girdwood 				"Hardware Params: "
2821f86dcef8SLiam Girdwood 				"Format = %s, Channels = %d, Rate = %d\n",
2822f86dcef8SLiam Girdwood 				snd_pcm_format_name(params_format(params)),
2823f86dcef8SLiam Girdwood 				params_channels(params),
2824f86dcef8SLiam Girdwood 				params_rate(params));
2825f86dcef8SLiam Girdwood 
2826f86dcef8SLiam Girdwood 	/* BEs state */
2827f86dcef8SLiam Girdwood 	offset += snprintf(buf + offset, size - offset, "Backends:\n");
2828f86dcef8SLiam Girdwood 
2829f86dcef8SLiam Girdwood 	if (list_empty(&fe->dpcm[stream].be_clients)) {
2830f86dcef8SLiam Girdwood 		offset += snprintf(buf + offset, size - offset,
2831f86dcef8SLiam Girdwood 				" No active DSP links\n");
2832f86dcef8SLiam Girdwood 		goto out;
2833f86dcef8SLiam Girdwood 	}
2834f86dcef8SLiam Girdwood 
2835f86dcef8SLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2836f86dcef8SLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
2837f86dcef8SLiam Girdwood 		params = &dpcm->hw_params;
2838f86dcef8SLiam Girdwood 
2839f86dcef8SLiam Girdwood 		offset += snprintf(buf + offset, size - offset,
2840f86dcef8SLiam Girdwood 				"- %s\n", be->dai_link->name);
2841f86dcef8SLiam Girdwood 
2842f86dcef8SLiam Girdwood 		offset += snprintf(buf + offset, size - offset,
2843f86dcef8SLiam Girdwood 				"   State: %s\n",
2844f86dcef8SLiam Girdwood 				dpcm_state_string(be->dpcm[stream].state));
2845f86dcef8SLiam Girdwood 
2846f86dcef8SLiam Girdwood 		if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2847f86dcef8SLiam Girdwood 		    (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2848f86dcef8SLiam Girdwood 			offset += snprintf(buf + offset, size - offset,
2849f86dcef8SLiam Girdwood 				"   Hardware Params: "
2850f86dcef8SLiam Girdwood 				"Format = %s, Channels = %d, Rate = %d\n",
2851f86dcef8SLiam Girdwood 				snd_pcm_format_name(params_format(params)),
2852f86dcef8SLiam Girdwood 				params_channels(params),
2853f86dcef8SLiam Girdwood 				params_rate(params));
2854f86dcef8SLiam Girdwood 	}
2855f86dcef8SLiam Girdwood 
2856f86dcef8SLiam Girdwood out:
2857f86dcef8SLiam Girdwood 	return offset;
2858f86dcef8SLiam Girdwood }
2859f86dcef8SLiam Girdwood 
2860f86dcef8SLiam Girdwood static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2861f86dcef8SLiam Girdwood 				size_t count, loff_t *ppos)
2862f86dcef8SLiam Girdwood {
2863f86dcef8SLiam Girdwood 	struct snd_soc_pcm_runtime *fe = file->private_data;
2864f86dcef8SLiam Girdwood 	ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2865f86dcef8SLiam Girdwood 	char *buf;
2866f86dcef8SLiam Girdwood 
2867f86dcef8SLiam Girdwood 	buf = kmalloc(out_count, GFP_KERNEL);
2868f86dcef8SLiam Girdwood 	if (!buf)
2869f86dcef8SLiam Girdwood 		return -ENOMEM;
2870f86dcef8SLiam Girdwood 
2871f86dcef8SLiam Girdwood 	if (fe->cpu_dai->driver->playback.channels_min)
2872f86dcef8SLiam Girdwood 		offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2873f86dcef8SLiam Girdwood 					buf + offset, out_count - offset);
2874f86dcef8SLiam Girdwood 
2875f86dcef8SLiam Girdwood 	if (fe->cpu_dai->driver->capture.channels_min)
2876f86dcef8SLiam Girdwood 		offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2877f86dcef8SLiam Girdwood 					buf + offset, out_count - offset);
2878f86dcef8SLiam Girdwood 
2879f86dcef8SLiam Girdwood 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2880f86dcef8SLiam Girdwood 
2881f86dcef8SLiam Girdwood 	kfree(buf);
2882f86dcef8SLiam Girdwood 	return ret;
2883f86dcef8SLiam Girdwood }
2884f86dcef8SLiam Girdwood 
2885f86dcef8SLiam Girdwood static const struct file_operations dpcm_state_fops = {
2886f57b8488SLiam Girdwood 	.open = simple_open,
2887f86dcef8SLiam Girdwood 	.read = dpcm_state_read_file,
2888f86dcef8SLiam Girdwood 	.llseek = default_llseek,
2889f86dcef8SLiam Girdwood };
2890f86dcef8SLiam Girdwood 
28912e55b90aSLars-Peter Clausen void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2892f86dcef8SLiam Girdwood {
2893b3bba9a1SMark Brown 	if (!rtd->dai_link)
28942e55b90aSLars-Peter Clausen 		return;
2895b3bba9a1SMark Brown 
28966553bf06SLars-Peter Clausen 	if (!rtd->card->debugfs_card_root)
28976553bf06SLars-Peter Clausen 		return;
2898f86dcef8SLiam Girdwood 
2899f86dcef8SLiam Girdwood 	rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2900f86dcef8SLiam Girdwood 			rtd->card->debugfs_card_root);
2901f86dcef8SLiam Girdwood 	if (!rtd->debugfs_dpcm_root) {
2902f86dcef8SLiam Girdwood 		dev_dbg(rtd->dev,
2903f86dcef8SLiam Girdwood 			 "ASoC: Failed to create dpcm debugfs directory %s\n",
2904f86dcef8SLiam Girdwood 			 rtd->dai_link->name);
29052e55b90aSLars-Peter Clausen 		return;
2906f86dcef8SLiam Girdwood 	}
2907f86dcef8SLiam Girdwood 
2908f57b8488SLiam Girdwood 	rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2909f86dcef8SLiam Girdwood 						rtd->debugfs_dpcm_root,
2910f86dcef8SLiam Girdwood 						rtd, &dpcm_state_fops);
2911f86dcef8SLiam Girdwood }
2912f86dcef8SLiam Girdwood #endif
2913