xref: /openbmc/linux/sound/soc/soc-pcm.c (revision b1cd2e34c69a2f3988786af451b6e17967c293a0)
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 
37cde79035SRicard Wanderlof /*
38cde79035SRicard Wanderlof  * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
39cde79035SRicard Wanderlof  *
40cde79035SRicard Wanderlof  * Returns true if the DAI supports the indicated stream type.
41cde79035SRicard Wanderlof  */
42cde79035SRicard Wanderlof static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
43cde79035SRicard Wanderlof {
44cde79035SRicard Wanderlof 	struct snd_soc_pcm_stream *codec_stream;
45cde79035SRicard Wanderlof 
46cde79035SRicard Wanderlof 	if (stream == SNDRV_PCM_STREAM_PLAYBACK)
47cde79035SRicard Wanderlof 		codec_stream = &dai->driver->playback;
48cde79035SRicard Wanderlof 	else
49cde79035SRicard Wanderlof 		codec_stream = &dai->driver->capture;
50cde79035SRicard Wanderlof 
51cde79035SRicard Wanderlof 	/* If the codec specifies any rate at all, it supports the stream. */
52cde79035SRicard Wanderlof 	return codec_stream->rates;
53cde79035SRicard Wanderlof }
54cde79035SRicard 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 
184*b1cd2e34SBanajit Goswami 		if ((event == SND_SOC_DAPM_STREAM_STOP) &&
185*b1cd2e34SBanajit Goswami 		    (be->dpcm[dir].users >= 1))
186*b1cd2e34SBanajit Goswami 			continue;
187*b1cd2e34SBanajit Goswami 
18801d7584cSLiam Girdwood 		snd_soc_dapm_stream_event(be, dir, event);
18901d7584cSLiam Girdwood 	}
19001d7584cSLiam Girdwood 
19101d7584cSLiam Girdwood 	snd_soc_dapm_stream_event(fe, dir, event);
19201d7584cSLiam Girdwood 
19301d7584cSLiam Girdwood 	return 0;
19401d7584cSLiam Girdwood }
19501d7584cSLiam Girdwood 
19617841020SDong Aisheng static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
19717841020SDong Aisheng 					struct snd_soc_dai *soc_dai)
198ddee627cSLiam Girdwood {
199ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
200ddee627cSLiam Girdwood 	int ret;
201ddee627cSLiam Girdwood 
2023635bf09SNicolin Chen 	if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
2033635bf09SNicolin Chen 				rtd->dai_link->symmetric_rates)) {
2043635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
2053635bf09SNicolin Chen 				soc_dai->rate);
206ddee627cSLiam Girdwood 
2074dcdd43bSLars-Peter Clausen 		ret = snd_pcm_hw_constraint_single(substream->runtime,
208ddee627cSLiam Girdwood 						SNDRV_PCM_HW_PARAM_RATE,
2094dcdd43bSLars-Peter Clausen 						soc_dai->rate);
210ddee627cSLiam Girdwood 		if (ret < 0) {
21117841020SDong Aisheng 			dev_err(soc_dai->dev,
2123635bf09SNicolin Chen 				"ASoC: Unable to apply rate constraint: %d\n",
213103d84a3SLiam Girdwood 				ret);
214ddee627cSLiam Girdwood 			return ret;
215ddee627cSLiam Girdwood 		}
2163635bf09SNicolin Chen 	}
2173635bf09SNicolin Chen 
2183635bf09SNicolin Chen 	if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
2193635bf09SNicolin Chen 				rtd->dai_link->symmetric_channels)) {
2203635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
2213635bf09SNicolin Chen 				soc_dai->channels);
2223635bf09SNicolin Chen 
2234dcdd43bSLars-Peter Clausen 		ret = snd_pcm_hw_constraint_single(substream->runtime,
2243635bf09SNicolin Chen 						SNDRV_PCM_HW_PARAM_CHANNELS,
2253635bf09SNicolin Chen 						soc_dai->channels);
2263635bf09SNicolin Chen 		if (ret < 0) {
2273635bf09SNicolin Chen 			dev_err(soc_dai->dev,
2283635bf09SNicolin Chen 				"ASoC: Unable to apply channel symmetry constraint: %d\n",
2293635bf09SNicolin Chen 				ret);
2303635bf09SNicolin Chen 			return ret;
2313635bf09SNicolin Chen 		}
2323635bf09SNicolin Chen 	}
2333635bf09SNicolin Chen 
2343635bf09SNicolin Chen 	if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
2353635bf09SNicolin Chen 				rtd->dai_link->symmetric_samplebits)) {
2363635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
2373635bf09SNicolin Chen 				soc_dai->sample_bits);
2383635bf09SNicolin Chen 
2394dcdd43bSLars-Peter Clausen 		ret = snd_pcm_hw_constraint_single(substream->runtime,
2403635bf09SNicolin Chen 						SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2413635bf09SNicolin Chen 						soc_dai->sample_bits);
2423635bf09SNicolin Chen 		if (ret < 0) {
2433635bf09SNicolin Chen 			dev_err(soc_dai->dev,
2443635bf09SNicolin Chen 				"ASoC: Unable to apply sample bits symmetry constraint: %d\n",
2453635bf09SNicolin Chen 				ret);
2463635bf09SNicolin Chen 			return ret;
2473635bf09SNicolin Chen 		}
2483635bf09SNicolin Chen 	}
249ddee627cSLiam Girdwood 
250ddee627cSLiam Girdwood 	return 0;
251ddee627cSLiam Girdwood }
252ddee627cSLiam Girdwood 
2533635bf09SNicolin Chen static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
2543635bf09SNicolin Chen 				struct snd_pcm_hw_params *params)
2553635bf09SNicolin Chen {
2563635bf09SNicolin Chen 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
2573635bf09SNicolin Chen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2582e5894d7SBenoit Cousson 	unsigned int rate, channels, sample_bits, symmetry, i;
2593635bf09SNicolin Chen 
2603635bf09SNicolin Chen 	rate = params_rate(params);
2613635bf09SNicolin Chen 	channels = params_channels(params);
2623635bf09SNicolin Chen 	sample_bits = snd_pcm_format_physical_width(params_format(params));
2633635bf09SNicolin Chen 
2643635bf09SNicolin Chen 	/* reject unmatched parameters when applying symmetry */
2653635bf09SNicolin Chen 	symmetry = cpu_dai->driver->symmetric_rates ||
2663635bf09SNicolin Chen 		rtd->dai_link->symmetric_rates;
2672e5894d7SBenoit Cousson 
2682e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
2692e5894d7SBenoit Cousson 		symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
2702e5894d7SBenoit Cousson 
2713635bf09SNicolin Chen 	if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
2723635bf09SNicolin Chen 		dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
2733635bf09SNicolin Chen 				cpu_dai->rate, rate);
2743635bf09SNicolin Chen 		return -EINVAL;
2753635bf09SNicolin Chen 	}
2763635bf09SNicolin Chen 
2773635bf09SNicolin Chen 	symmetry = cpu_dai->driver->symmetric_channels ||
2783635bf09SNicolin Chen 		rtd->dai_link->symmetric_channels;
2792e5894d7SBenoit Cousson 
2802e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
2812e5894d7SBenoit Cousson 		symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
2822e5894d7SBenoit Cousson 
2833635bf09SNicolin Chen 	if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
2843635bf09SNicolin Chen 		dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
2853635bf09SNicolin Chen 				cpu_dai->channels, channels);
2863635bf09SNicolin Chen 		return -EINVAL;
2873635bf09SNicolin Chen 	}
2883635bf09SNicolin Chen 
2893635bf09SNicolin Chen 	symmetry = cpu_dai->driver->symmetric_samplebits ||
2903635bf09SNicolin Chen 		rtd->dai_link->symmetric_samplebits;
2912e5894d7SBenoit Cousson 
2922e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
2932e5894d7SBenoit Cousson 		symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
2942e5894d7SBenoit Cousson 
2953635bf09SNicolin Chen 	if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
2963635bf09SNicolin Chen 		dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
2973635bf09SNicolin Chen 				cpu_dai->sample_bits, sample_bits);
2983635bf09SNicolin Chen 		return -EINVAL;
2993635bf09SNicolin Chen 	}
300ddee627cSLiam Girdwood 
301ddee627cSLiam Girdwood 	return 0;
302ddee627cSLiam Girdwood }
303ddee627cSLiam Girdwood 
30462e5f676SLars-Peter Clausen static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
30562e5f676SLars-Peter Clausen {
30662e5f676SLars-Peter Clausen 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
30762e5f676SLars-Peter Clausen 	struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
30862e5f676SLars-Peter Clausen 	struct snd_soc_dai_link *link = rtd->dai_link;
3092e5894d7SBenoit Cousson 	unsigned int symmetry, i;
31062e5f676SLars-Peter Clausen 
3112e5894d7SBenoit Cousson 	symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
3122e5894d7SBenoit Cousson 		cpu_driver->symmetric_channels || link->symmetric_channels ||
3132e5894d7SBenoit Cousson 		cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
3142e5894d7SBenoit Cousson 
3152e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
3162e5894d7SBenoit Cousson 		symmetry = symmetry ||
3172e5894d7SBenoit Cousson 			rtd->codec_dais[i]->driver->symmetric_rates ||
3182e5894d7SBenoit Cousson 			rtd->codec_dais[i]->driver->symmetric_channels ||
3192e5894d7SBenoit Cousson 			rtd->codec_dais[i]->driver->symmetric_samplebits;
3202e5894d7SBenoit Cousson 
3212e5894d7SBenoit Cousson 	return symmetry;
32262e5f676SLars-Peter Clausen }
32362e5f676SLars-Peter Clausen 
3242e5894d7SBenoit Cousson static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
32558ba9b25SMark Brown {
3262e5894d7SBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
327c6068d3aSTakashi Iwai 	int ret;
32858ba9b25SMark Brown 
32958ba9b25SMark Brown 	if (!bits)
33058ba9b25SMark Brown 		return;
33158ba9b25SMark Brown 
3320e2a3751SLars-Peter Clausen 	ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
33358ba9b25SMark Brown 	if (ret != 0)
3340e2a3751SLars-Peter Clausen 		dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
3350e2a3751SLars-Peter Clausen 				 bits, ret);
33658ba9b25SMark Brown }
33758ba9b25SMark Brown 
338c8dd1fecSBenoit Cousson static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
339bd477c31SLars-Peter Clausen {
340c8dd1fecSBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
341c8dd1fecSBenoit Cousson 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3422e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
3432e5894d7SBenoit Cousson 	int i;
344c8dd1fecSBenoit Cousson 	unsigned int bits = 0, cpu_bits;
345c8dd1fecSBenoit Cousson 
346c8dd1fecSBenoit Cousson 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3472e5894d7SBenoit Cousson 		for (i = 0; i < rtd->num_codecs; i++) {
3482e5894d7SBenoit Cousson 			codec_dai = rtd->codec_dais[i];
3492e5894d7SBenoit Cousson 			if (codec_dai->driver->playback.sig_bits == 0) {
3502e5894d7SBenoit Cousson 				bits = 0;
3512e5894d7SBenoit Cousson 				break;
3522e5894d7SBenoit Cousson 			}
3532e5894d7SBenoit Cousson 			bits = max(codec_dai->driver->playback.sig_bits, bits);
3542e5894d7SBenoit Cousson 		}
355c8dd1fecSBenoit Cousson 		cpu_bits = cpu_dai->driver->playback.sig_bits;
356c8dd1fecSBenoit Cousson 	} else {
3572e5894d7SBenoit Cousson 		for (i = 0; i < rtd->num_codecs; i++) {
3582e5894d7SBenoit Cousson 			codec_dai = rtd->codec_dais[i];
3595e63dfccSDaniel Mack 			if (codec_dai->driver->capture.sig_bits == 0) {
3602e5894d7SBenoit Cousson 				bits = 0;
3612e5894d7SBenoit Cousson 				break;
3622e5894d7SBenoit Cousson 			}
3632e5894d7SBenoit Cousson 			bits = max(codec_dai->driver->capture.sig_bits, bits);
3642e5894d7SBenoit Cousson 		}
365c8dd1fecSBenoit Cousson 		cpu_bits = cpu_dai->driver->capture.sig_bits;
366c8dd1fecSBenoit Cousson 	}
367c8dd1fecSBenoit Cousson 
3682e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, bits);
3692e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, cpu_bits);
370c8dd1fecSBenoit Cousson }
371c8dd1fecSBenoit Cousson 
3722e5894d7SBenoit Cousson static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
373bd477c31SLars-Peter Clausen {
3742e5894d7SBenoit Cousson 	struct snd_pcm_runtime *runtime = substream->runtime;
37578e45c99SLars-Peter Clausen 	struct snd_pcm_hardware *hw = &runtime->hw;
3762e5894d7SBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
3772e5894d7SBenoit Cousson 	struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
3782e5894d7SBenoit Cousson 	struct snd_soc_dai_driver *codec_dai_drv;
3792e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *codec_stream;
3802e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *cpu_stream;
3812e5894d7SBenoit Cousson 	unsigned int chan_min = 0, chan_max = UINT_MAX;
3822e5894d7SBenoit Cousson 	unsigned int rate_min = 0, rate_max = UINT_MAX;
3832e5894d7SBenoit Cousson 	unsigned int rates = UINT_MAX;
3842e5894d7SBenoit Cousson 	u64 formats = ULLONG_MAX;
3852e5894d7SBenoit Cousson 	int i;
38678e45c99SLars-Peter Clausen 
3872e5894d7SBenoit Cousson 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3882e5894d7SBenoit Cousson 		cpu_stream = &cpu_dai_drv->playback;
38916d7ea91SLars-Peter Clausen 	else
3902e5894d7SBenoit Cousson 		cpu_stream = &cpu_dai_drv->capture;
39178e45c99SLars-Peter Clausen 
3922e5894d7SBenoit Cousson 	/* first calculate min/max only for CODECs in the DAI link */
3932e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
394cde79035SRicard Wanderlof 
395cde79035SRicard Wanderlof 		/*
396cde79035SRicard Wanderlof 		 * Skip CODECs which don't support the current stream type.
397cde79035SRicard Wanderlof 		 * Otherwise, since the rate, channel, and format values will
398cde79035SRicard Wanderlof 		 * zero in that case, we would have no usable settings left,
399cde79035SRicard Wanderlof 		 * causing the resulting setup to fail.
400cde79035SRicard Wanderlof 		 * At least one CODEC should match, otherwise we should have
401cde79035SRicard Wanderlof 		 * bailed out on a higher level, since there would be no
402cde79035SRicard Wanderlof 		 * CODEC to support the transfer direction in that case.
403cde79035SRicard Wanderlof 		 */
404cde79035SRicard Wanderlof 		if (!snd_soc_dai_stream_valid(rtd->codec_dais[i],
405cde79035SRicard Wanderlof 					      substream->stream))
406cde79035SRicard Wanderlof 			continue;
407cde79035SRicard Wanderlof 
4082e5894d7SBenoit Cousson 		codec_dai_drv = rtd->codec_dais[i]->driver;
4092e5894d7SBenoit Cousson 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4102e5894d7SBenoit Cousson 			codec_stream = &codec_dai_drv->playback;
4112e5894d7SBenoit Cousson 		else
4122e5894d7SBenoit Cousson 			codec_stream = &codec_dai_drv->capture;
4132e5894d7SBenoit Cousson 		chan_min = max(chan_min, codec_stream->channels_min);
4142e5894d7SBenoit Cousson 		chan_max = min(chan_max, codec_stream->channels_max);
4152e5894d7SBenoit Cousson 		rate_min = max(rate_min, codec_stream->rate_min);
4162e5894d7SBenoit Cousson 		rate_max = min_not_zero(rate_max, codec_stream->rate_max);
4172e5894d7SBenoit Cousson 		formats &= codec_stream->formats;
4182e5894d7SBenoit Cousson 		rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
4192e5894d7SBenoit Cousson 	}
4202e5894d7SBenoit Cousson 
4212e5894d7SBenoit Cousson 	/*
4222e5894d7SBenoit Cousson 	 * chan min/max cannot be enforced if there are multiple CODEC DAIs
4232e5894d7SBenoit Cousson 	 * connected to a single CPU DAI, use CPU DAI's directly and let
4242e5894d7SBenoit Cousson 	 * channel allocation be fixed up later
4252e5894d7SBenoit Cousson 	 */
4262e5894d7SBenoit Cousson 	if (rtd->num_codecs > 1) {
4272e5894d7SBenoit Cousson 		chan_min = cpu_stream->channels_min;
4282e5894d7SBenoit Cousson 		chan_max = cpu_stream->channels_max;
4292e5894d7SBenoit Cousson 	}
4302e5894d7SBenoit Cousson 
4312e5894d7SBenoit Cousson 	hw->channels_min = max(chan_min, cpu_stream->channels_min);
4322e5894d7SBenoit Cousson 	hw->channels_max = min(chan_max, cpu_stream->channels_max);
4332e5894d7SBenoit Cousson 	if (hw->formats)
4342e5894d7SBenoit Cousson 		hw->formats &= formats & cpu_stream->formats;
4352e5894d7SBenoit Cousson 	else
4362e5894d7SBenoit Cousson 		hw->formats = formats & cpu_stream->formats;
4372e5894d7SBenoit Cousson 	hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
43878e45c99SLars-Peter Clausen 
43978e45c99SLars-Peter Clausen 	snd_pcm_limit_hw_rates(runtime);
44078e45c99SLars-Peter Clausen 
44178e45c99SLars-Peter Clausen 	hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
4422e5894d7SBenoit Cousson 	hw->rate_min = max(hw->rate_min, rate_min);
44378e45c99SLars-Peter Clausen 	hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
4442e5894d7SBenoit Cousson 	hw->rate_max = min_not_zero(hw->rate_max, rate_max);
445bd477c31SLars-Peter Clausen }
446bd477c31SLars-Peter Clausen 
44758ba9b25SMark Brown /*
448ddee627cSLiam Girdwood  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
449ddee627cSLiam Girdwood  * then initialized and any private data can be allocated. This also calls
450ddee627cSLiam Girdwood  * startup for the cpu DAI, platform, machine and codec DAI.
451ddee627cSLiam Girdwood  */
452ddee627cSLiam Girdwood static int soc_pcm_open(struct snd_pcm_substream *substream)
453ddee627cSLiam Girdwood {
454ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
455ddee627cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
456ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
457ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4582e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
4592e5894d7SBenoit Cousson 	const char *codec_dai_name = "multicodec";
4602e5894d7SBenoit Cousson 	int i, ret = 0;
461ddee627cSLiam Girdwood 
462988e8cc4SNicolin Chen 	pinctrl_pm_select_default_state(cpu_dai->dev);
4632e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
4642e5894d7SBenoit Cousson 		pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
465d6652ef8SMark Brown 	pm_runtime_get_sync(cpu_dai->dev);
4662e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
4672e5894d7SBenoit Cousson 		pm_runtime_get_sync(rtd->codec_dais[i]->dev);
468d6652ef8SMark Brown 	pm_runtime_get_sync(platform->dev);
469d6652ef8SMark Brown 
470b8c0dab9SLiam Girdwood 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
471ddee627cSLiam Girdwood 
472ddee627cSLiam Girdwood 	/* startup the audio subsystem */
473c5914b0aSMark Brown 	if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
474ddee627cSLiam Girdwood 		ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
475ddee627cSLiam Girdwood 		if (ret < 0) {
476103d84a3SLiam Girdwood 			dev_err(cpu_dai->dev, "ASoC: can't open interface"
477103d84a3SLiam Girdwood 				" %s: %d\n", cpu_dai->name, ret);
478ddee627cSLiam Girdwood 			goto out;
479ddee627cSLiam Girdwood 		}
480ddee627cSLiam Girdwood 	}
481ddee627cSLiam Girdwood 
482ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->open) {
483ddee627cSLiam Girdwood 		ret = platform->driver->ops->open(substream);
484ddee627cSLiam Girdwood 		if (ret < 0) {
485103d84a3SLiam Girdwood 			dev_err(platform->dev, "ASoC: can't open platform"
486f4333203SLars-Peter Clausen 				" %s: %d\n", platform->component.name, ret);
487ddee627cSLiam Girdwood 			goto platform_err;
488ddee627cSLiam Girdwood 		}
489ddee627cSLiam Girdwood 	}
490ddee627cSLiam Girdwood 
4912e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
4922e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
493c5914b0aSMark Brown 		if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
4942e5894d7SBenoit Cousson 			ret = codec_dai->driver->ops->startup(substream,
4952e5894d7SBenoit Cousson 							      codec_dai);
496ddee627cSLiam Girdwood 			if (ret < 0) {
4972e5894d7SBenoit Cousson 				dev_err(codec_dai->dev,
4982e5894d7SBenoit Cousson 					"ASoC: can't open codec %s: %d\n",
4992e5894d7SBenoit Cousson 					codec_dai->name, ret);
500ddee627cSLiam Girdwood 				goto codec_dai_err;
501ddee627cSLiam Girdwood 			}
502ddee627cSLiam Girdwood 		}
503ddee627cSLiam Girdwood 
5042e5894d7SBenoit Cousson 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5052e5894d7SBenoit Cousson 			codec_dai->tx_mask = 0;
5062e5894d7SBenoit Cousson 		else
5072e5894d7SBenoit Cousson 			codec_dai->rx_mask = 0;
5082e5894d7SBenoit Cousson 	}
5092e5894d7SBenoit Cousson 
510ddee627cSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
511ddee627cSLiam Girdwood 		ret = rtd->dai_link->ops->startup(substream);
512ddee627cSLiam Girdwood 		if (ret < 0) {
513103d84a3SLiam Girdwood 			pr_err("ASoC: %s startup failed: %d\n",
51425bfe662SMark Brown 			       rtd->dai_link->name, ret);
515ddee627cSLiam Girdwood 			goto machine_err;
516ddee627cSLiam Girdwood 		}
517ddee627cSLiam Girdwood 	}
518ddee627cSLiam Girdwood 
51901d7584cSLiam Girdwood 	/* Dynamic PCM DAI links compat checks use dynamic capabilities */
52001d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
52101d7584cSLiam Girdwood 		goto dynamic;
52201d7584cSLiam Girdwood 
523ddee627cSLiam Girdwood 	/* Check that the codec and cpu DAIs are compatible */
5242e5894d7SBenoit Cousson 	soc_pcm_init_runtime_hw(substream);
5252e5894d7SBenoit Cousson 
5262e5894d7SBenoit Cousson 	if (rtd->num_codecs == 1)
5272e5894d7SBenoit Cousson 		codec_dai_name = rtd->codec_dai->name;
528ddee627cSLiam Girdwood 
52962e5f676SLars-Peter Clausen 	if (soc_pcm_has_symmetry(substream))
53062e5f676SLars-Peter Clausen 		runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
53162e5f676SLars-Peter Clausen 
532ddee627cSLiam Girdwood 	ret = -EINVAL;
533ddee627cSLiam Girdwood 	if (!runtime->hw.rates) {
534103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
5352e5894d7SBenoit Cousson 			codec_dai_name, cpu_dai->name);
536ddee627cSLiam Girdwood 		goto config_err;
537ddee627cSLiam Girdwood 	}
538ddee627cSLiam Girdwood 	if (!runtime->hw.formats) {
539103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
5402e5894d7SBenoit Cousson 			codec_dai_name, cpu_dai->name);
541ddee627cSLiam Girdwood 		goto config_err;
542ddee627cSLiam Girdwood 	}
543ddee627cSLiam Girdwood 	if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
544ddee627cSLiam Girdwood 	    runtime->hw.channels_min > runtime->hw.channels_max) {
545103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
5462e5894d7SBenoit Cousson 				codec_dai_name, cpu_dai->name);
547ddee627cSLiam Girdwood 		goto config_err;
548ddee627cSLiam Girdwood 	}
549ddee627cSLiam Girdwood 
550c8dd1fecSBenoit Cousson 	soc_pcm_apply_msb(substream);
55158ba9b25SMark Brown 
552ddee627cSLiam Girdwood 	/* Symmetry only applies if we've already got an active stream. */
55317841020SDong Aisheng 	if (cpu_dai->active) {
55417841020SDong Aisheng 		ret = soc_pcm_apply_symmetry(substream, cpu_dai);
55517841020SDong Aisheng 		if (ret != 0)
55617841020SDong Aisheng 			goto config_err;
55717841020SDong Aisheng 	}
55817841020SDong Aisheng 
5592e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
5602e5894d7SBenoit Cousson 		if (rtd->codec_dais[i]->active) {
5612e5894d7SBenoit Cousson 			ret = soc_pcm_apply_symmetry(substream,
5622e5894d7SBenoit Cousson 						     rtd->codec_dais[i]);
563ddee627cSLiam Girdwood 			if (ret != 0)
564ddee627cSLiam Girdwood 				goto config_err;
565ddee627cSLiam Girdwood 		}
5662e5894d7SBenoit Cousson 	}
567ddee627cSLiam Girdwood 
568103d84a3SLiam Girdwood 	pr_debug("ASoC: %s <-> %s info:\n",
5692e5894d7SBenoit Cousson 			codec_dai_name, cpu_dai->name);
570103d84a3SLiam Girdwood 	pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
571103d84a3SLiam Girdwood 	pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
572ddee627cSLiam Girdwood 		 runtime->hw.channels_max);
573103d84a3SLiam Girdwood 	pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
574ddee627cSLiam Girdwood 		 runtime->hw.rate_max);
575ddee627cSLiam Girdwood 
57601d7584cSLiam Girdwood dynamic:
57724894b76SLars-Peter Clausen 
57824894b76SLars-Peter Clausen 	snd_soc_runtime_activate(rtd, substream->stream);
57924894b76SLars-Peter Clausen 
580b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
581ddee627cSLiam Girdwood 	return 0;
582ddee627cSLiam Girdwood 
583ddee627cSLiam Girdwood config_err:
584ddee627cSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
585ddee627cSLiam Girdwood 		rtd->dai_link->ops->shutdown(substream);
586ddee627cSLiam Girdwood 
587ddee627cSLiam Girdwood machine_err:
5882e5894d7SBenoit Cousson 	i = rtd->num_codecs;
589ddee627cSLiam Girdwood 
590ddee627cSLiam Girdwood codec_dai_err:
5912e5894d7SBenoit Cousson 	while (--i >= 0) {
5922e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
5932e5894d7SBenoit Cousson 		if (codec_dai->driver->ops->shutdown)
5942e5894d7SBenoit Cousson 			codec_dai->driver->ops->shutdown(substream, codec_dai);
5952e5894d7SBenoit Cousson 	}
5962e5894d7SBenoit Cousson 
597ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->close)
598ddee627cSLiam Girdwood 		platform->driver->ops->close(substream);
599ddee627cSLiam Girdwood 
600ddee627cSLiam Girdwood platform_err:
601ddee627cSLiam Girdwood 	if (cpu_dai->driver->ops->shutdown)
602ddee627cSLiam Girdwood 		cpu_dai->driver->ops->shutdown(substream, cpu_dai);
603ddee627cSLiam Girdwood out:
604b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
605d6652ef8SMark Brown 
6063f809783SSanyog Kale 	pm_runtime_mark_last_busy(platform->dev);
6073f809783SSanyog Kale 	pm_runtime_put_autosuspend(platform->dev);
6083f809783SSanyog Kale 	for (i = 0; i < rtd->num_codecs; i++) {
6093f809783SSanyog Kale 		pm_runtime_mark_last_busy(rtd->codec_dais[i]->dev);
6103f809783SSanyog Kale 		pm_runtime_put_autosuspend(rtd->codec_dais[i]->dev);
6113f809783SSanyog Kale 	}
6123f809783SSanyog Kale 
6133f809783SSanyog Kale 	pm_runtime_mark_last_busy(cpu_dai->dev);
6143f809783SSanyog Kale 	pm_runtime_put_autosuspend(cpu_dai->dev);
6152e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
6162e5894d7SBenoit Cousson 		if (!rtd->codec_dais[i]->active)
6172e5894d7SBenoit Cousson 			pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
6182e5894d7SBenoit Cousson 	}
619988e8cc4SNicolin Chen 	if (!cpu_dai->active)
620988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
621d6652ef8SMark Brown 
622ddee627cSLiam Girdwood 	return ret;
623ddee627cSLiam Girdwood }
624ddee627cSLiam Girdwood 
625ddee627cSLiam Girdwood /*
626ddee627cSLiam Girdwood  * Power down the audio subsystem pmdown_time msecs after close is called.
627ddee627cSLiam Girdwood  * This is to ensure there are no pops or clicks in between any music tracks
628ddee627cSLiam Girdwood  * due to DAPM power cycling.
629ddee627cSLiam Girdwood  */
630ddee627cSLiam Girdwood static void close_delayed_work(struct work_struct *work)
631ddee627cSLiam Girdwood {
632ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
633ddee627cSLiam Girdwood 			container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
6342e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
635ddee627cSLiam Girdwood 
636b8c0dab9SLiam Girdwood 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
637ddee627cSLiam Girdwood 
638103d84a3SLiam Girdwood 	dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
639ddee627cSLiam Girdwood 		 codec_dai->driver->playback.stream_name,
640ddee627cSLiam Girdwood 		 codec_dai->playback_active ? "active" : "inactive",
6419bffb1fbSMisael Lopez Cruz 		 rtd->pop_wait ? "yes" : "no");
642ddee627cSLiam Girdwood 
643ddee627cSLiam Girdwood 	/* are we waiting on this codec DAI stream */
6449bffb1fbSMisael Lopez Cruz 	if (rtd->pop_wait == 1) {
6459bffb1fbSMisael Lopez Cruz 		rtd->pop_wait = 0;
6467bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
647d9b0951bSLiam Girdwood 					  SND_SOC_DAPM_STREAM_STOP);
648ddee627cSLiam Girdwood 	}
649ddee627cSLiam Girdwood 
650b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
651ddee627cSLiam Girdwood }
652ddee627cSLiam Girdwood 
653ddee627cSLiam Girdwood /*
654ddee627cSLiam Girdwood  * Called by ALSA when a PCM substream is closed. Private data can be
655ddee627cSLiam Girdwood  * freed here. The cpu DAI, codec DAI, machine and platform are also
656ddee627cSLiam Girdwood  * shutdown.
657ddee627cSLiam Girdwood  */
65891d5e6b4SLiam Girdwood static int soc_pcm_close(struct snd_pcm_substream *substream)
659ddee627cSLiam Girdwood {
660ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
661ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
662ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6632e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
6642e5894d7SBenoit Cousson 	int i;
665ddee627cSLiam Girdwood 
666b8c0dab9SLiam Girdwood 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
667ddee627cSLiam Girdwood 
66824894b76SLars-Peter Clausen 	snd_soc_runtime_deactivate(rtd, substream->stream);
669ddee627cSLiam Girdwood 
67017841020SDong Aisheng 	/* clear the corresponding DAIs rate when inactive */
67117841020SDong Aisheng 	if (!cpu_dai->active)
67217841020SDong Aisheng 		cpu_dai->rate = 0;
67317841020SDong Aisheng 
6742e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
6752e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
67617841020SDong Aisheng 		if (!codec_dai->active)
67717841020SDong Aisheng 			codec_dai->rate = 0;
6782e5894d7SBenoit Cousson 	}
67925b76791SSascha Hauer 
680ae11601bSRamesh Babu 	snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
681ae11601bSRamesh Babu 
682ddee627cSLiam Girdwood 	if (cpu_dai->driver->ops->shutdown)
683ddee627cSLiam Girdwood 		cpu_dai->driver->ops->shutdown(substream, cpu_dai);
684ddee627cSLiam Girdwood 
6852e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
6862e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
687ddee627cSLiam Girdwood 		if (codec_dai->driver->ops->shutdown)
688ddee627cSLiam Girdwood 			codec_dai->driver->ops->shutdown(substream, codec_dai);
6892e5894d7SBenoit Cousson 	}
690ddee627cSLiam Girdwood 
691ddee627cSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
692ddee627cSLiam Girdwood 		rtd->dai_link->ops->shutdown(substream);
693ddee627cSLiam Girdwood 
694ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->close)
695ddee627cSLiam Girdwood 		platform->driver->ops->close(substream);
696ddee627cSLiam Girdwood 
697ddee627cSLiam Girdwood 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
698208a1589SLars-Peter Clausen 		if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
6991d69c5c5SPeter Ujfalusi 			/* powered down playback stream now */
7001d69c5c5SPeter Ujfalusi 			snd_soc_dapm_stream_event(rtd,
7017bd3a6f3SMark Brown 						  SNDRV_PCM_STREAM_PLAYBACK,
7021d69c5c5SPeter Ujfalusi 						  SND_SOC_DAPM_STREAM_STOP);
7031d69c5c5SPeter Ujfalusi 		} else {
704ddee627cSLiam Girdwood 			/* start delayed pop wq here for playback streams */
7059bffb1fbSMisael Lopez Cruz 			rtd->pop_wait = 1;
706d4e1a73aSMark Brown 			queue_delayed_work(system_power_efficient_wq,
707d4e1a73aSMark Brown 					   &rtd->delayed_work,
708ddee627cSLiam Girdwood 					   msecs_to_jiffies(rtd->pmdown_time));
7091d69c5c5SPeter Ujfalusi 		}
710ddee627cSLiam Girdwood 	} else {
711ddee627cSLiam Girdwood 		/* capture streams can be powered down now */
7127bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
713d9b0951bSLiam Girdwood 					  SND_SOC_DAPM_STREAM_STOP);
714ddee627cSLiam Girdwood 	}
715ddee627cSLiam Girdwood 
716b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
717d6652ef8SMark Brown 
7183f809783SSanyog Kale 	pm_runtime_mark_last_busy(platform->dev);
7193f809783SSanyog Kale 	pm_runtime_put_autosuspend(platform->dev);
7203f809783SSanyog Kale 
7213f809783SSanyog Kale 	for (i = 0; i < rtd->num_codecs; i++) {
7223f809783SSanyog Kale 		pm_runtime_mark_last_busy(rtd->codec_dais[i]->dev);
7233f809783SSanyog Kale 		pm_runtime_put_autosuspend(rtd->codec_dais[i]->dev);
7243f809783SSanyog Kale 	}
7253f809783SSanyog Kale 
7263f809783SSanyog Kale 	pm_runtime_mark_last_busy(cpu_dai->dev);
7273f809783SSanyog Kale 	pm_runtime_put_autosuspend(cpu_dai->dev);
7283f809783SSanyog Kale 
7292e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
7302e5894d7SBenoit Cousson 		if (!rtd->codec_dais[i]->active)
7312e5894d7SBenoit Cousson 			pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
7322e5894d7SBenoit Cousson 	}
733988e8cc4SNicolin Chen 	if (!cpu_dai->active)
734988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
735d6652ef8SMark Brown 
736ddee627cSLiam Girdwood 	return 0;
737ddee627cSLiam Girdwood }
738ddee627cSLiam Girdwood 
739ddee627cSLiam Girdwood /*
740ddee627cSLiam Girdwood  * Called by ALSA when the PCM substream is prepared, can set format, sample
741ddee627cSLiam Girdwood  * rate, etc.  This function is non atomic and can be called multiple times,
742ddee627cSLiam Girdwood  * it can refer to the runtime info.
743ddee627cSLiam Girdwood  */
744ddee627cSLiam Girdwood static int soc_pcm_prepare(struct snd_pcm_substream *substream)
745ddee627cSLiam Girdwood {
746ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
747ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
748ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7492e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
7502e5894d7SBenoit Cousson 	int i, ret = 0;
751ddee627cSLiam Girdwood 
752b8c0dab9SLiam Girdwood 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
753ddee627cSLiam Girdwood 
754ddee627cSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
755ddee627cSLiam Girdwood 		ret = rtd->dai_link->ops->prepare(substream);
756ddee627cSLiam Girdwood 		if (ret < 0) {
757103d84a3SLiam Girdwood 			dev_err(rtd->card->dev, "ASoC: machine prepare error:"
758103d84a3SLiam Girdwood 				" %d\n", ret);
759ddee627cSLiam Girdwood 			goto out;
760ddee627cSLiam Girdwood 		}
761ddee627cSLiam Girdwood 	}
762ddee627cSLiam Girdwood 
763ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->prepare) {
764ddee627cSLiam Girdwood 		ret = platform->driver->ops->prepare(substream);
765ddee627cSLiam Girdwood 		if (ret < 0) {
766103d84a3SLiam Girdwood 			dev_err(platform->dev, "ASoC: platform prepare error:"
767103d84a3SLiam Girdwood 				" %d\n", ret);
768ddee627cSLiam Girdwood 			goto out;
769ddee627cSLiam Girdwood 		}
770ddee627cSLiam Girdwood 	}
771ddee627cSLiam Girdwood 
7722e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
7732e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
774c5914b0aSMark Brown 		if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
7752e5894d7SBenoit Cousson 			ret = codec_dai->driver->ops->prepare(substream,
7762e5894d7SBenoit Cousson 							      codec_dai);
777ddee627cSLiam Girdwood 			if (ret < 0) {
7782e5894d7SBenoit Cousson 				dev_err(codec_dai->dev,
77990cc7f1cSJarkko Nikula 					"ASoC: codec DAI prepare error: %d\n",
78090cc7f1cSJarkko Nikula 					ret);
781ddee627cSLiam Girdwood 				goto out;
782ddee627cSLiam Girdwood 			}
783ddee627cSLiam Girdwood 		}
7842e5894d7SBenoit Cousson 	}
785ddee627cSLiam Girdwood 
786c5914b0aSMark Brown 	if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
787ddee627cSLiam Girdwood 		ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
788ddee627cSLiam Girdwood 		if (ret < 0) {
78990cc7f1cSJarkko Nikula 			dev_err(cpu_dai->dev,
79090cc7f1cSJarkko Nikula 				"ASoC: cpu DAI prepare error: %d\n", ret);
791ddee627cSLiam Girdwood 			goto out;
792ddee627cSLiam Girdwood 		}
793ddee627cSLiam Girdwood 	}
794ddee627cSLiam Girdwood 
795ddee627cSLiam Girdwood 	/* cancel any delayed stream shutdown that is pending */
796ddee627cSLiam Girdwood 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
7979bffb1fbSMisael Lopez Cruz 	    rtd->pop_wait) {
7989bffb1fbSMisael Lopez Cruz 		rtd->pop_wait = 0;
799ddee627cSLiam Girdwood 		cancel_delayed_work(&rtd->delayed_work);
800ddee627cSLiam Girdwood 	}
801ddee627cSLiam Girdwood 
802d9b0951bSLiam Girdwood 	snd_soc_dapm_stream_event(rtd, substream->stream,
803ddee627cSLiam Girdwood 			SND_SOC_DAPM_STREAM_START);
804ddee627cSLiam Girdwood 
8052e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
8062e5894d7SBenoit Cousson 		snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
8072e5894d7SBenoit Cousson 					 substream->stream);
808ae11601bSRamesh Babu 	snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
809ddee627cSLiam Girdwood 
810ddee627cSLiam Girdwood out:
811b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
812ddee627cSLiam Girdwood 	return ret;
813ddee627cSLiam Girdwood }
814ddee627cSLiam Girdwood 
8152e5894d7SBenoit Cousson static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
8162e5894d7SBenoit Cousson 				       unsigned int mask)
8172e5894d7SBenoit Cousson {
8182e5894d7SBenoit Cousson 	struct snd_interval *interval;
8192e5894d7SBenoit Cousson 	int channels = hweight_long(mask);
8202e5894d7SBenoit Cousson 
8212e5894d7SBenoit Cousson 	interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
8222e5894d7SBenoit Cousson 	interval->min = channels;
8232e5894d7SBenoit Cousson 	interval->max = channels;
8242e5894d7SBenoit Cousson }
8252e5894d7SBenoit Cousson 
82693e6958aSBenoit Cousson int soc_dai_hw_params(struct snd_pcm_substream *substream,
82793e6958aSBenoit Cousson 		      struct snd_pcm_hw_params *params,
82893e6958aSBenoit Cousson 		      struct snd_soc_dai *dai)
82993e6958aSBenoit Cousson {
83093e6958aSBenoit Cousson 	int ret;
83193e6958aSBenoit Cousson 
83293e6958aSBenoit Cousson 	if (dai->driver->ops && dai->driver->ops->hw_params) {
83393e6958aSBenoit Cousson 		ret = dai->driver->ops->hw_params(substream, params, dai);
83493e6958aSBenoit Cousson 		if (ret < 0) {
83593e6958aSBenoit Cousson 			dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
83693e6958aSBenoit Cousson 				dai->name, ret);
83793e6958aSBenoit Cousson 			return ret;
83893e6958aSBenoit Cousson 		}
83993e6958aSBenoit Cousson 	}
84093e6958aSBenoit Cousson 
84193e6958aSBenoit Cousson 	return 0;
84293e6958aSBenoit Cousson }
84393e6958aSBenoit Cousson 
844ddee627cSLiam Girdwood /*
845ddee627cSLiam Girdwood  * Called by ALSA when the hardware params are set by application. This
846ddee627cSLiam Girdwood  * function can also be called multiple times and can allocate buffers
847ddee627cSLiam Girdwood  * (using snd_pcm_lib_* ). It's non-atomic.
848ddee627cSLiam Girdwood  */
849ddee627cSLiam Girdwood static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
850ddee627cSLiam Girdwood 				struct snd_pcm_hw_params *params)
851ddee627cSLiam Girdwood {
852ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
853ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
854ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
8552e5894d7SBenoit Cousson 	int i, ret = 0;
856ddee627cSLiam Girdwood 
857b8c0dab9SLiam Girdwood 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
858ddee627cSLiam Girdwood 
8593635bf09SNicolin Chen 	ret = soc_pcm_params_symmetry(substream, params);
8603635bf09SNicolin Chen 	if (ret)
8613635bf09SNicolin Chen 		goto out;
8623635bf09SNicolin Chen 
863ddee627cSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
864ddee627cSLiam Girdwood 		ret = rtd->dai_link->ops->hw_params(substream, params);
865ddee627cSLiam Girdwood 		if (ret < 0) {
866103d84a3SLiam Girdwood 			dev_err(rtd->card->dev, "ASoC: machine hw_params"
867103d84a3SLiam Girdwood 				" failed: %d\n", ret);
868ddee627cSLiam Girdwood 			goto out;
869ddee627cSLiam Girdwood 		}
870ddee627cSLiam Girdwood 	}
871ddee627cSLiam Girdwood 
8722e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
8732e5894d7SBenoit Cousson 		struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
8742e5894d7SBenoit Cousson 		struct snd_pcm_hw_params codec_params;
8752e5894d7SBenoit Cousson 
876cde79035SRicard Wanderlof 		/*
877cde79035SRicard Wanderlof 		 * Skip CODECs which don't support the current stream type,
878cde79035SRicard Wanderlof 		 * the idea being that if a CODEC is not used for the currently
879cde79035SRicard Wanderlof 		 * set up transfer direction, it should not need to be
880cde79035SRicard Wanderlof 		 * configured, especially since the configuration used might
881cde79035SRicard Wanderlof 		 * not even be supported by that CODEC. There may be cases
882cde79035SRicard Wanderlof 		 * however where a CODEC needs to be set up although it is
883cde79035SRicard Wanderlof 		 * actually not being used for the transfer, e.g. if a
884cde79035SRicard Wanderlof 		 * capture-only CODEC is acting as an LRCLK and/or BCLK master
885cde79035SRicard Wanderlof 		 * for the DAI link including a playback-only CODEC.
886cde79035SRicard Wanderlof 		 * If this becomes necessary, we will have to augment the
887cde79035SRicard Wanderlof 		 * machine driver setup with information on how to act, so
888cde79035SRicard Wanderlof 		 * we can do the right thing here.
889cde79035SRicard Wanderlof 		 */
890cde79035SRicard Wanderlof 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
891cde79035SRicard Wanderlof 			continue;
892cde79035SRicard Wanderlof 
8932e5894d7SBenoit Cousson 		/* copy params for each codec */
8942e5894d7SBenoit Cousson 		codec_params = *params;
8952e5894d7SBenoit Cousson 
8962e5894d7SBenoit Cousson 		/* fixup params based on TDM slot masks */
8972e5894d7SBenoit Cousson 		if (codec_dai->tx_mask)
8982e5894d7SBenoit Cousson 			soc_pcm_codec_params_fixup(&codec_params,
8992e5894d7SBenoit Cousson 						   codec_dai->tx_mask);
9002e5894d7SBenoit Cousson 		if (codec_dai->rx_mask)
9012e5894d7SBenoit Cousson 			soc_pcm_codec_params_fixup(&codec_params,
9022e5894d7SBenoit Cousson 						   codec_dai->rx_mask);
9032e5894d7SBenoit Cousson 
90493e6958aSBenoit Cousson 		ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
90593e6958aSBenoit Cousson 		if(ret < 0)
906ddee627cSLiam Girdwood 			goto codec_err;
907ddee627cSLiam Girdwood 
9082e5894d7SBenoit Cousson 		codec_dai->rate = params_rate(&codec_params);
9092e5894d7SBenoit Cousson 		codec_dai->channels = params_channels(&codec_params);
9102e5894d7SBenoit Cousson 		codec_dai->sample_bits = snd_pcm_format_physical_width(
9112e5894d7SBenoit Cousson 						params_format(&codec_params));
912ddee627cSLiam Girdwood 	}
913ddee627cSLiam Girdwood 
91493e6958aSBenoit Cousson 	ret = soc_dai_hw_params(substream, params, cpu_dai);
91593e6958aSBenoit Cousson 	if (ret < 0)
916ddee627cSLiam Girdwood 		goto interface_err;
917ddee627cSLiam Girdwood 
918ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->hw_params) {
919ddee627cSLiam Girdwood 		ret = platform->driver->ops->hw_params(substream, params);
920ddee627cSLiam Girdwood 		if (ret < 0) {
921103d84a3SLiam Girdwood 			dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
922f4333203SLars-Peter Clausen 			       platform->component.name, ret);
923ddee627cSLiam Girdwood 			goto platform_err;
924ddee627cSLiam Girdwood 		}
925ddee627cSLiam Girdwood 	}
926ddee627cSLiam Girdwood 
9273635bf09SNicolin Chen 	/* store the parameters for each DAIs */
92817841020SDong Aisheng 	cpu_dai->rate = params_rate(params);
9293635bf09SNicolin Chen 	cpu_dai->channels = params_channels(params);
9303635bf09SNicolin Chen 	cpu_dai->sample_bits =
9313635bf09SNicolin Chen 		snd_pcm_format_physical_width(params_format(params));
9323635bf09SNicolin Chen 
933ddee627cSLiam Girdwood out:
934b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
935ddee627cSLiam Girdwood 	return ret;
936ddee627cSLiam Girdwood 
937ddee627cSLiam Girdwood platform_err:
938c5914b0aSMark Brown 	if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
939ddee627cSLiam Girdwood 		cpu_dai->driver->ops->hw_free(substream, cpu_dai);
940ddee627cSLiam Girdwood 
941ddee627cSLiam Girdwood interface_err:
9422e5894d7SBenoit Cousson 	i = rtd->num_codecs;
943ddee627cSLiam Girdwood 
944ddee627cSLiam Girdwood codec_err:
9452e5894d7SBenoit Cousson 	while (--i >= 0) {
9462e5894d7SBenoit Cousson 		struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
9472e5894d7SBenoit Cousson 		if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
9482e5894d7SBenoit Cousson 			codec_dai->driver->ops->hw_free(substream, codec_dai);
9492e5894d7SBenoit Cousson 		codec_dai->rate = 0;
9502e5894d7SBenoit Cousson 	}
9512e5894d7SBenoit Cousson 
952ddee627cSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
953ddee627cSLiam Girdwood 		rtd->dai_link->ops->hw_free(substream);
954ddee627cSLiam Girdwood 
955b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
956ddee627cSLiam Girdwood 	return ret;
957ddee627cSLiam Girdwood }
958ddee627cSLiam Girdwood 
959ddee627cSLiam Girdwood /*
960ddee627cSLiam Girdwood  * Frees resources allocated by hw_params, can be called multiple times
961ddee627cSLiam Girdwood  */
962ddee627cSLiam Girdwood static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
963ddee627cSLiam Girdwood {
964ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
965ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
966ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
9672e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
9687f62b6eeSNicolin Chen 	bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
9692e5894d7SBenoit Cousson 	int i;
970ddee627cSLiam Girdwood 
971b8c0dab9SLiam Girdwood 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
972ddee627cSLiam Girdwood 
973d3383420SNicolin Chen 	/* clear the corresponding DAIs parameters when going to be inactive */
974d3383420SNicolin Chen 	if (cpu_dai->active == 1) {
975d3383420SNicolin Chen 		cpu_dai->rate = 0;
976d3383420SNicolin Chen 		cpu_dai->channels = 0;
977d3383420SNicolin Chen 		cpu_dai->sample_bits = 0;
978d3383420SNicolin Chen 	}
979d3383420SNicolin Chen 
9802e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
9812e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
982d3383420SNicolin Chen 		if (codec_dai->active == 1) {
983d3383420SNicolin Chen 			codec_dai->rate = 0;
984d3383420SNicolin Chen 			codec_dai->channels = 0;
985d3383420SNicolin Chen 			codec_dai->sample_bits = 0;
986d3383420SNicolin Chen 		}
9872e5894d7SBenoit Cousson 	}
988d3383420SNicolin Chen 
989ddee627cSLiam Girdwood 	/* apply codec digital mute */
9902e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
9912e5894d7SBenoit Cousson 		if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
9922e5894d7SBenoit Cousson 		    (!playback && rtd->codec_dais[i]->capture_active == 1))
9932e5894d7SBenoit Cousson 			snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
9942e5894d7SBenoit Cousson 						 substream->stream);
9952e5894d7SBenoit Cousson 	}
996ddee627cSLiam Girdwood 
997ddee627cSLiam Girdwood 	/* free any machine hw params */
998ddee627cSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
999ddee627cSLiam Girdwood 		rtd->dai_link->ops->hw_free(substream);
1000ddee627cSLiam Girdwood 
1001ddee627cSLiam Girdwood 	/* free any DMA resources */
1002ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->hw_free)
1003ddee627cSLiam Girdwood 		platform->driver->ops->hw_free(substream);
1004ddee627cSLiam Girdwood 
1005ddee627cSLiam Girdwood 	/* now free hw params for the DAIs  */
10062e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
10072e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
1008c5914b0aSMark Brown 		if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
1009ddee627cSLiam Girdwood 			codec_dai->driver->ops->hw_free(substream, codec_dai);
10102e5894d7SBenoit Cousson 	}
1011ddee627cSLiam Girdwood 
1012c5914b0aSMark Brown 	if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
1013ddee627cSLiam Girdwood 		cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1014ddee627cSLiam Girdwood 
1015b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
1016ddee627cSLiam Girdwood 	return 0;
1017ddee627cSLiam Girdwood }
1018ddee627cSLiam Girdwood 
1019ddee627cSLiam Girdwood static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1020ddee627cSLiam Girdwood {
1021ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1022ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1023ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
10242e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
10252e5894d7SBenoit Cousson 	int i, ret;
1026ddee627cSLiam Girdwood 
10272e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
10282e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
1029c5914b0aSMark Brown 		if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
10302e5894d7SBenoit Cousson 			ret = codec_dai->driver->ops->trigger(substream,
10312e5894d7SBenoit Cousson 							      cmd, codec_dai);
1032ddee627cSLiam Girdwood 			if (ret < 0)
1033ddee627cSLiam Girdwood 				return ret;
1034ddee627cSLiam Girdwood 		}
10352e5894d7SBenoit Cousson 	}
1036ddee627cSLiam Girdwood 
1037ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->trigger) {
1038ddee627cSLiam Girdwood 		ret = platform->driver->ops->trigger(substream, cmd);
1039ddee627cSLiam Girdwood 		if (ret < 0)
1040ddee627cSLiam Girdwood 			return ret;
1041ddee627cSLiam Girdwood 	}
1042ddee627cSLiam Girdwood 
1043c5914b0aSMark Brown 	if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
1044ddee627cSLiam Girdwood 		ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1045ddee627cSLiam Girdwood 		if (ret < 0)
1046ddee627cSLiam Girdwood 			return ret;
1047ddee627cSLiam Girdwood 	}
10484792b0dbSJarkko Nikula 
10494792b0dbSJarkko Nikula 	if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
10504792b0dbSJarkko Nikula 		ret = rtd->dai_link->ops->trigger(substream, cmd);
10514792b0dbSJarkko Nikula 		if (ret < 0)
10524792b0dbSJarkko Nikula 			return ret;
10534792b0dbSJarkko Nikula 	}
10544792b0dbSJarkko Nikula 
1055ddee627cSLiam Girdwood 	return 0;
1056ddee627cSLiam Girdwood }
1057ddee627cSLiam Girdwood 
105845c0a188SMark Brown static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
105945c0a188SMark Brown 				   int cmd)
106007bf84aaSLiam Girdwood {
106107bf84aaSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
106207bf84aaSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
10632e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
10642e5894d7SBenoit Cousson 	int i, ret;
106507bf84aaSLiam Girdwood 
10662e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
10672e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
1068c5914b0aSMark Brown 		if (codec_dai->driver->ops &&
1069c5914b0aSMark Brown 		    codec_dai->driver->ops->bespoke_trigger) {
10702e5894d7SBenoit Cousson 			ret = codec_dai->driver->ops->bespoke_trigger(substream,
10712e5894d7SBenoit Cousson 								cmd, codec_dai);
107207bf84aaSLiam Girdwood 			if (ret < 0)
107307bf84aaSLiam Girdwood 				return ret;
107407bf84aaSLiam Girdwood 		}
10752e5894d7SBenoit Cousson 	}
107607bf84aaSLiam Girdwood 
1077c5914b0aSMark Brown 	if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
107807bf84aaSLiam Girdwood 		ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
107907bf84aaSLiam Girdwood 		if (ret < 0)
108007bf84aaSLiam Girdwood 			return ret;
108107bf84aaSLiam Girdwood 	}
108207bf84aaSLiam Girdwood 	return 0;
108307bf84aaSLiam Girdwood }
1084ddee627cSLiam Girdwood /*
1085ddee627cSLiam Girdwood  * soc level wrapper for pointer callback
1086ddee627cSLiam Girdwood  * If cpu_dai, codec_dai, platform driver has the delay callback, than
1087ddee627cSLiam Girdwood  * the runtime->delay will be updated accordingly.
1088ddee627cSLiam Girdwood  */
1089ddee627cSLiam Girdwood static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1090ddee627cSLiam Girdwood {
1091ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1092ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1093ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
10942e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
1095ddee627cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
1096ddee627cSLiam Girdwood 	snd_pcm_uframes_t offset = 0;
1097ddee627cSLiam Girdwood 	snd_pcm_sframes_t delay = 0;
10982e5894d7SBenoit Cousson 	snd_pcm_sframes_t codec_delay = 0;
10992e5894d7SBenoit Cousson 	int i;
1100ddee627cSLiam Girdwood 
1101ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->pointer)
1102ddee627cSLiam Girdwood 		offset = platform->driver->ops->pointer(substream);
1103ddee627cSLiam Girdwood 
1104c5914b0aSMark Brown 	if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
1105ddee627cSLiam Girdwood 		delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1106ddee627cSLiam Girdwood 
11072e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
11082e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
1109c5914b0aSMark Brown 		if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
11102e5894d7SBenoit Cousson 			codec_delay = max(codec_delay,
11112e5894d7SBenoit Cousson 					codec_dai->driver->ops->delay(substream,
11122e5894d7SBenoit Cousson 								    codec_dai));
11132e5894d7SBenoit Cousson 	}
11142e5894d7SBenoit Cousson 	delay += codec_delay;
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;
12181a497983SMengdong Lin 	int i;
121901d7584cSLiam Girdwood 
122001d7584cSLiam Girdwood 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
12211a497983SMengdong Lin 		list_for_each_entry(be, &card->rtd_list, list) {
122201d7584cSLiam Girdwood 
122335ea0655SLiam Girdwood 			if (!be->dai_link->no_pcm)
122435ea0655SLiam Girdwood 				continue;
122535ea0655SLiam Girdwood 
12262e5894d7SBenoit Cousson 			if (be->cpu_dai->playback_widget == widget)
122701d7584cSLiam Girdwood 				return be;
12282e5894d7SBenoit Cousson 
12291a497983SMengdong Lin 			for (i = 0; i < be->num_codecs; i++) {
12301a497983SMengdong Lin 				struct snd_soc_dai *dai = be->codec_dais[i];
12312e5894d7SBenoit Cousson 				if (dai->playback_widget == widget)
12322e5894d7SBenoit Cousson 					return be;
12332e5894d7SBenoit Cousson 			}
123401d7584cSLiam Girdwood 		}
123501d7584cSLiam Girdwood 	} else {
123601d7584cSLiam Girdwood 
12371a497983SMengdong Lin 		list_for_each_entry(be, &card->rtd_list, list) {
123801d7584cSLiam Girdwood 
123935ea0655SLiam Girdwood 			if (!be->dai_link->no_pcm)
124035ea0655SLiam Girdwood 				continue;
124135ea0655SLiam Girdwood 
12422e5894d7SBenoit Cousson 			if (be->cpu_dai->capture_widget == widget)
124301d7584cSLiam Girdwood 				return be;
12442e5894d7SBenoit Cousson 
12451a497983SMengdong Lin 			for (i = 0; i < be->num_codecs; i++) {
12461a497983SMengdong Lin 				struct snd_soc_dai *dai = be->codec_dais[i];
12472e5894d7SBenoit Cousson 				if (dai->capture_widget == widget)
12482e5894d7SBenoit Cousson 					return be;
12492e5894d7SBenoit Cousson 			}
125001d7584cSLiam Girdwood 		}
125101d7584cSLiam Girdwood 	}
125201d7584cSLiam Girdwood 
1253103d84a3SLiam Girdwood 	dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
125401d7584cSLiam Girdwood 		stream ? "capture" : "playback", widget->name);
125501d7584cSLiam Girdwood 	return NULL;
125601d7584cSLiam Girdwood }
125701d7584cSLiam Girdwood 
125801d7584cSLiam Girdwood static inline struct snd_soc_dapm_widget *
125937018610SBenoit Cousson 	dai_get_widget(struct snd_soc_dai *dai, int stream)
126001d7584cSLiam Girdwood {
126101d7584cSLiam Girdwood 	if (stream == SNDRV_PCM_STREAM_PLAYBACK)
126237018610SBenoit Cousson 		return dai->playback_widget;
126301d7584cSLiam Girdwood 	else
126437018610SBenoit Cousson 		return dai->capture_widget;
126501d7584cSLiam Girdwood }
126601d7584cSLiam Girdwood 
126701d7584cSLiam Girdwood static int widget_in_list(struct snd_soc_dapm_widget_list *list,
126801d7584cSLiam Girdwood 		struct snd_soc_dapm_widget *widget)
126901d7584cSLiam Girdwood {
127001d7584cSLiam Girdwood 	int i;
127101d7584cSLiam Girdwood 
127201d7584cSLiam Girdwood 	for (i = 0; i < list->num_widgets; i++) {
127301d7584cSLiam Girdwood 		if (widget == list->widgets[i])
127401d7584cSLiam Girdwood 			return 1;
127501d7584cSLiam Girdwood 	}
127601d7584cSLiam Girdwood 
127701d7584cSLiam Girdwood 	return 0;
127801d7584cSLiam Girdwood }
127901d7584cSLiam Girdwood 
12805fdd022cSPiotr Stankiewicz static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
12815fdd022cSPiotr Stankiewicz 		enum snd_soc_dapm_direction dir)
12825fdd022cSPiotr Stankiewicz {
12835fdd022cSPiotr Stankiewicz 	struct snd_soc_card *card = widget->dapm->card;
12845fdd022cSPiotr Stankiewicz 	struct snd_soc_pcm_runtime *rtd;
12855fdd022cSPiotr Stankiewicz 	int i;
12865fdd022cSPiotr Stankiewicz 
12875fdd022cSPiotr Stankiewicz 	if (dir == SND_SOC_DAPM_DIR_OUT) {
12885fdd022cSPiotr Stankiewicz 		list_for_each_entry(rtd, &card->rtd_list, list) {
12895fdd022cSPiotr Stankiewicz 			if (!rtd->dai_link->no_pcm)
12905fdd022cSPiotr Stankiewicz 				continue;
12915fdd022cSPiotr Stankiewicz 
12925fdd022cSPiotr Stankiewicz 			if (rtd->cpu_dai->playback_widget == widget)
12935fdd022cSPiotr Stankiewicz 				return true;
12945fdd022cSPiotr Stankiewicz 
12955fdd022cSPiotr Stankiewicz 			for (i = 0; i < rtd->num_codecs; ++i) {
12965fdd022cSPiotr Stankiewicz 				struct snd_soc_dai *dai = rtd->codec_dais[i];
12975fdd022cSPiotr Stankiewicz 				if (dai->playback_widget == widget)
12985fdd022cSPiotr Stankiewicz 					return true;
12995fdd022cSPiotr Stankiewicz 			}
13005fdd022cSPiotr Stankiewicz 		}
13015fdd022cSPiotr Stankiewicz 	} else { /* SND_SOC_DAPM_DIR_IN */
13025fdd022cSPiotr Stankiewicz 		list_for_each_entry(rtd, &card->rtd_list, list) {
13035fdd022cSPiotr Stankiewicz 			if (!rtd->dai_link->no_pcm)
13045fdd022cSPiotr Stankiewicz 				continue;
13055fdd022cSPiotr Stankiewicz 
13065fdd022cSPiotr Stankiewicz 			if (rtd->cpu_dai->capture_widget == widget)
13075fdd022cSPiotr Stankiewicz 				return true;
13085fdd022cSPiotr Stankiewicz 
13095fdd022cSPiotr Stankiewicz 			for (i = 0; i < rtd->num_codecs; ++i) {
13105fdd022cSPiotr Stankiewicz 				struct snd_soc_dai *dai = rtd->codec_dais[i];
13115fdd022cSPiotr Stankiewicz 				if (dai->capture_widget == widget)
13125fdd022cSPiotr Stankiewicz 					return true;
13135fdd022cSPiotr Stankiewicz 			}
13145fdd022cSPiotr Stankiewicz 		}
13155fdd022cSPiotr Stankiewicz 	}
13165fdd022cSPiotr Stankiewicz 
13175fdd022cSPiotr Stankiewicz 	return false;
13185fdd022cSPiotr Stankiewicz }
13195fdd022cSPiotr Stankiewicz 
132023607025SLiam Girdwood int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
13211ce43acfSLars-Peter Clausen 	int stream, struct snd_soc_dapm_widget_list **list)
132201d7584cSLiam Girdwood {
132301d7584cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = fe->cpu_dai;
132401d7584cSLiam Girdwood 	int paths;
132501d7584cSLiam Girdwood 
132601d7584cSLiam Girdwood 	/* get number of valid DAI paths and their widgets */
13276742064aSPiotr Stankiewicz 	paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
13285fdd022cSPiotr Stankiewicz 			dpcm_end_walk_at_be);
132901d7584cSLiam Girdwood 
1330103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
133101d7584cSLiam Girdwood 			stream ? "capture" : "playback");
133201d7584cSLiam Girdwood 
133301d7584cSLiam Girdwood 	return paths;
133401d7584cSLiam Girdwood }
133501d7584cSLiam Girdwood 
133601d7584cSLiam Girdwood static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
133701d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list **list_)
133801d7584cSLiam Girdwood {
133901d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
134001d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list = *list_;
134101d7584cSLiam Girdwood 	struct snd_soc_dapm_widget *widget;
134201d7584cSLiam Girdwood 	int prune = 0;
134301d7584cSLiam Girdwood 
134401d7584cSLiam Girdwood 	/* Destroy any old FE <--> BE connections */
134501d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
13462e5894d7SBenoit Cousson 		unsigned int i;
134701d7584cSLiam Girdwood 
134801d7584cSLiam Girdwood 		/* is there a valid CPU DAI widget for this BE */
134937018610SBenoit Cousson 		widget = dai_get_widget(dpcm->be->cpu_dai, stream);
135001d7584cSLiam Girdwood 
135101d7584cSLiam Girdwood 		/* prune the BE if it's no longer in our active list */
135201d7584cSLiam Girdwood 		if (widget && widget_in_list(list, widget))
135301d7584cSLiam Girdwood 			continue;
135401d7584cSLiam Girdwood 
135501d7584cSLiam Girdwood 		/* is there a valid CODEC DAI widget for this BE */
13562e5894d7SBenoit Cousson 		for (i = 0; i < dpcm->be->num_codecs; i++) {
13572e5894d7SBenoit Cousson 			struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
13582e5894d7SBenoit Cousson 			widget = dai_get_widget(dai, stream);
135901d7584cSLiam Girdwood 
136001d7584cSLiam Girdwood 			/* prune the BE if it's no longer in our active list */
136101d7584cSLiam Girdwood 			if (widget && widget_in_list(list, widget))
136201d7584cSLiam Girdwood 				continue;
13632e5894d7SBenoit Cousson 		}
136401d7584cSLiam Girdwood 
1365103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
136601d7584cSLiam Girdwood 			stream ? "capture" : "playback",
136701d7584cSLiam Girdwood 			dpcm->be->dai_link->name, fe->dai_link->name);
136801d7584cSLiam Girdwood 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
136901d7584cSLiam Girdwood 		dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
137001d7584cSLiam Girdwood 		prune++;
137101d7584cSLiam Girdwood 	}
137201d7584cSLiam Girdwood 
1373103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
137401d7584cSLiam Girdwood 	return prune;
137501d7584cSLiam Girdwood }
137601d7584cSLiam Girdwood 
137701d7584cSLiam Girdwood static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
137801d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list **list_)
137901d7584cSLiam Girdwood {
138001d7584cSLiam Girdwood 	struct snd_soc_card *card = fe->card;
138101d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list = *list_;
138201d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *be;
138301d7584cSLiam Girdwood 	int i, new = 0, err;
138401d7584cSLiam Girdwood 
138501d7584cSLiam Girdwood 	/* Create any new FE <--> BE connections */
138601d7584cSLiam Girdwood 	for (i = 0; i < list->num_widgets; i++) {
138701d7584cSLiam Girdwood 
13884616274dSMark Brown 		switch (list->widgets[i]->id) {
13894616274dSMark Brown 		case snd_soc_dapm_dai_in:
1390c5b8540dSKoro Chen 			if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1391c5b8540dSKoro Chen 				continue;
1392c5b8540dSKoro Chen 			break;
13934616274dSMark Brown 		case snd_soc_dapm_dai_out:
1394c5b8540dSKoro Chen 			if (stream != SNDRV_PCM_STREAM_CAPTURE)
1395c5b8540dSKoro Chen 				continue;
13964616274dSMark Brown 			break;
13974616274dSMark Brown 		default:
139801d7584cSLiam Girdwood 			continue;
13994616274dSMark Brown 		}
140001d7584cSLiam Girdwood 
140101d7584cSLiam Girdwood 		/* is there a valid BE rtd for this widget */
140201d7584cSLiam Girdwood 		be = dpcm_get_be(card, list->widgets[i], stream);
140301d7584cSLiam Girdwood 		if (!be) {
1404103d84a3SLiam Girdwood 			dev_err(fe->dev, "ASoC: no BE found for %s\n",
140501d7584cSLiam Girdwood 					list->widgets[i]->name);
140601d7584cSLiam Girdwood 			continue;
140701d7584cSLiam Girdwood 		}
140801d7584cSLiam Girdwood 
140901d7584cSLiam Girdwood 		/* make sure BE is a real BE */
141001d7584cSLiam Girdwood 		if (!be->dai_link->no_pcm)
141101d7584cSLiam Girdwood 			continue;
141201d7584cSLiam Girdwood 
141301d7584cSLiam Girdwood 		/* don't connect if FE is not running */
141423607025SLiam Girdwood 		if (!fe->dpcm[stream].runtime && !fe->fe_compr)
141501d7584cSLiam Girdwood 			continue;
141601d7584cSLiam Girdwood 
141701d7584cSLiam Girdwood 		/* newly connected FE and BE */
141801d7584cSLiam Girdwood 		err = dpcm_be_connect(fe, be, stream);
141901d7584cSLiam Girdwood 		if (err < 0) {
1420103d84a3SLiam Girdwood 			dev_err(fe->dev, "ASoC: can't connect %s\n",
142101d7584cSLiam Girdwood 				list->widgets[i]->name);
142201d7584cSLiam Girdwood 			break;
142301d7584cSLiam Girdwood 		} else if (err == 0) /* already connected */
142401d7584cSLiam Girdwood 			continue;
142501d7584cSLiam Girdwood 
142601d7584cSLiam Girdwood 		/* new */
142701d7584cSLiam Girdwood 		be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
142801d7584cSLiam Girdwood 		new++;
142901d7584cSLiam Girdwood 	}
143001d7584cSLiam Girdwood 
1431103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
143201d7584cSLiam Girdwood 	return new;
143301d7584cSLiam Girdwood }
143401d7584cSLiam Girdwood 
143501d7584cSLiam Girdwood /*
143601d7584cSLiam Girdwood  * Find the corresponding BE DAIs that source or sink audio to this
143701d7584cSLiam Girdwood  * FE substream.
143801d7584cSLiam Girdwood  */
143923607025SLiam Girdwood int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
144001d7584cSLiam Girdwood 	int stream, struct snd_soc_dapm_widget_list **list, int new)
144101d7584cSLiam Girdwood {
144201d7584cSLiam Girdwood 	if (new)
144301d7584cSLiam Girdwood 		return dpcm_add_paths(fe, stream, list);
144401d7584cSLiam Girdwood 	else
144501d7584cSLiam Girdwood 		return dpcm_prune_paths(fe, stream, list);
144601d7584cSLiam Girdwood }
144701d7584cSLiam Girdwood 
144823607025SLiam Girdwood void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
144901d7584cSLiam Girdwood {
145001d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
145101d7584cSLiam Girdwood 
145201d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
145301d7584cSLiam Girdwood 		dpcm->be->dpcm[stream].runtime_update =
145401d7584cSLiam Girdwood 						SND_SOC_DPCM_UPDATE_NO;
145501d7584cSLiam Girdwood }
145601d7584cSLiam Girdwood 
145701d7584cSLiam Girdwood static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
145801d7584cSLiam Girdwood 	int stream)
145901d7584cSLiam Girdwood {
146001d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
146101d7584cSLiam Girdwood 
146201d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
146301d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
146401d7584cSLiam Girdwood 
146501d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
146601d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
146701d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
146801d7584cSLiam Girdwood 
146901d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1470103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
147101d7584cSLiam Girdwood 				stream ? "capture" : "playback",
147201d7584cSLiam Girdwood 				be->dpcm[stream].state);
147301d7584cSLiam Girdwood 
147401d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
147501d7584cSLiam Girdwood 			continue;
147601d7584cSLiam Girdwood 
147701d7584cSLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
147801d7584cSLiam Girdwood 			continue;
147901d7584cSLiam Girdwood 
148001d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
148101d7584cSLiam Girdwood 		be_substream->runtime = NULL;
148201d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
148301d7584cSLiam Girdwood 	}
148401d7584cSLiam Girdwood }
148501d7584cSLiam Girdwood 
148623607025SLiam Girdwood int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
148701d7584cSLiam Girdwood {
148801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
148901d7584cSLiam Girdwood 	int err, count = 0;
149001d7584cSLiam Girdwood 
149101d7584cSLiam Girdwood 	/* only startup BE DAIs that are either sinks or sources to this FE DAI */
149201d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
149301d7584cSLiam Girdwood 
149401d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
149501d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
149601d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
149701d7584cSLiam Girdwood 
14982062b4c5SRussell King - ARM Linux 		if (!be_substream) {
14992062b4c5SRussell King - ARM Linux 			dev_err(be->dev, "ASoC: no backend %s stream\n",
15002062b4c5SRussell King - ARM Linux 				stream ? "capture" : "playback");
15012062b4c5SRussell King - ARM Linux 			continue;
15022062b4c5SRussell King - ARM Linux 		}
15032062b4c5SRussell King - ARM Linux 
150401d7584cSLiam Girdwood 		/* is this op for this BE ? */
150501d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
150601d7584cSLiam Girdwood 			continue;
150701d7584cSLiam Girdwood 
150801d7584cSLiam Girdwood 		/* first time the dpcm is open ? */
150901d7584cSLiam Girdwood 		if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1510103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: too many users %s at open %d\n",
151101d7584cSLiam Girdwood 				stream ? "capture" : "playback",
151201d7584cSLiam Girdwood 				be->dpcm[stream].state);
151301d7584cSLiam Girdwood 
151401d7584cSLiam Girdwood 		if (be->dpcm[stream].users++ != 0)
151501d7584cSLiam Girdwood 			continue;
151601d7584cSLiam Girdwood 
151701d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
151801d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
151901d7584cSLiam Girdwood 			continue;
152001d7584cSLiam Girdwood 
15212062b4c5SRussell King - ARM Linux 		dev_dbg(be->dev, "ASoC: open %s BE %s\n",
15222062b4c5SRussell King - ARM Linux 			stream ? "capture" : "playback", be->dai_link->name);
152301d7584cSLiam Girdwood 
152401d7584cSLiam Girdwood 		be_substream->runtime = be->dpcm[stream].runtime;
152501d7584cSLiam Girdwood 		err = soc_pcm_open(be_substream);
152601d7584cSLiam Girdwood 		if (err < 0) {
1527103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: BE open failed %d\n", err);
152801d7584cSLiam Girdwood 			be->dpcm[stream].users--;
152901d7584cSLiam Girdwood 			if (be->dpcm[stream].users < 0)
1530103d84a3SLiam Girdwood 				dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
153101d7584cSLiam Girdwood 					stream ? "capture" : "playback",
153201d7584cSLiam Girdwood 					be->dpcm[stream].state);
153301d7584cSLiam Girdwood 
153401d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
153501d7584cSLiam Girdwood 			goto unwind;
153601d7584cSLiam Girdwood 		}
153701d7584cSLiam Girdwood 
153801d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
153901d7584cSLiam Girdwood 		count++;
154001d7584cSLiam Girdwood 	}
154101d7584cSLiam Girdwood 
154201d7584cSLiam Girdwood 	return count;
154301d7584cSLiam Girdwood 
154401d7584cSLiam Girdwood unwind:
154501d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
154601d7584cSLiam Girdwood 	list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
154701d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
154801d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
154901d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
155001d7584cSLiam Girdwood 
155101d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
155201d7584cSLiam Girdwood 			continue;
155301d7584cSLiam Girdwood 
155401d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1555103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close %d\n",
155601d7584cSLiam Girdwood 				stream ? "capture" : "playback",
155701d7584cSLiam Girdwood 				be->dpcm[stream].state);
155801d7584cSLiam Girdwood 
155901d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
156001d7584cSLiam Girdwood 			continue;
156101d7584cSLiam Girdwood 
156201d7584cSLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
156301d7584cSLiam Girdwood 			continue;
156401d7584cSLiam Girdwood 
156501d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
156601d7584cSLiam Girdwood 		be_substream->runtime = NULL;
156701d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
156801d7584cSLiam Girdwood 	}
156901d7584cSLiam Girdwood 
157001d7584cSLiam Girdwood 	return err;
157101d7584cSLiam Girdwood }
157201d7584cSLiam Girdwood 
157308ae9b45SLars-Peter Clausen static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1574b073ed4eSKuninori Morimoto 				 struct snd_soc_pcm_stream *stream,
1575b073ed4eSKuninori Morimoto 				 u64 formats)
157608ae9b45SLars-Peter Clausen {
157708ae9b45SLars-Peter Clausen 	runtime->hw.rate_min = stream->rate_min;
157808ae9b45SLars-Peter Clausen 	runtime->hw.rate_max = stream->rate_max;
157908ae9b45SLars-Peter Clausen 	runtime->hw.channels_min = stream->channels_min;
158008ae9b45SLars-Peter Clausen 	runtime->hw.channels_max = stream->channels_max;
1581002220a9SLars-Peter Clausen 	if (runtime->hw.formats)
1582b073ed4eSKuninori Morimoto 		runtime->hw.formats &= formats & stream->formats;
1583002220a9SLars-Peter Clausen 	else
1584b073ed4eSKuninori Morimoto 		runtime->hw.formats = formats & stream->formats;
158508ae9b45SLars-Peter Clausen 	runtime->hw.rates = stream->rates;
158608ae9b45SLars-Peter Clausen }
158708ae9b45SLars-Peter Clausen 
1588b073ed4eSKuninori Morimoto static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1589b073ed4eSKuninori Morimoto {
1590b073ed4eSKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1591b073ed4eSKuninori Morimoto 	struct snd_soc_dpcm *dpcm;
1592b073ed4eSKuninori Morimoto 	u64 formats = ULLONG_MAX;
1593b073ed4eSKuninori Morimoto 	int stream = substream->stream;
1594b073ed4eSKuninori Morimoto 
1595b073ed4eSKuninori Morimoto 	if (!fe->dai_link->dpcm_merged_format)
1596b073ed4eSKuninori Morimoto 		return formats;
1597b073ed4eSKuninori Morimoto 
1598b073ed4eSKuninori Morimoto 	/*
1599b073ed4eSKuninori Morimoto 	 * It returns merged BE codec format
1600b073ed4eSKuninori Morimoto 	 * if FE want to use it (= dpcm_merged_format)
1601b073ed4eSKuninori Morimoto 	 */
1602b073ed4eSKuninori Morimoto 
1603b073ed4eSKuninori Morimoto 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1604b073ed4eSKuninori Morimoto 		struct snd_soc_pcm_runtime *be = dpcm->be;
1605b073ed4eSKuninori Morimoto 		struct snd_soc_dai_driver *codec_dai_drv;
1606b073ed4eSKuninori Morimoto 		struct snd_soc_pcm_stream *codec_stream;
1607b073ed4eSKuninori Morimoto 		int i;
1608b073ed4eSKuninori Morimoto 
1609b073ed4eSKuninori Morimoto 		for (i = 0; i < be->num_codecs; i++) {
1610b073ed4eSKuninori Morimoto 			codec_dai_drv = be->codec_dais[i]->driver;
1611b073ed4eSKuninori Morimoto 			if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1612b073ed4eSKuninori Morimoto 				codec_stream = &codec_dai_drv->playback;
1613b073ed4eSKuninori Morimoto 			else
1614b073ed4eSKuninori Morimoto 				codec_stream = &codec_dai_drv->capture;
1615b073ed4eSKuninori Morimoto 
1616b073ed4eSKuninori Morimoto 			formats &= codec_stream->formats;
1617b073ed4eSKuninori Morimoto 		}
1618b073ed4eSKuninori Morimoto 	}
1619b073ed4eSKuninori Morimoto 
1620b073ed4eSKuninori Morimoto 	return formats;
1621b073ed4eSKuninori Morimoto }
1622b073ed4eSKuninori Morimoto 
162345c0a188SMark Brown static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
162401d7584cSLiam Girdwood {
162501d7584cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
162601d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
162701d7584cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
162801d7584cSLiam Girdwood 	struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1629b073ed4eSKuninori Morimoto 	u64 format = dpcm_runtime_base_format(substream);
163001d7584cSLiam Girdwood 
163108ae9b45SLars-Peter Clausen 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1632b073ed4eSKuninori Morimoto 		dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
163308ae9b45SLars-Peter Clausen 	else
1634b073ed4eSKuninori Morimoto 		dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
163501d7584cSLiam Girdwood }
163601d7584cSLiam Girdwood 
1637ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1638ea9d0d77STakashi Iwai 
1639ea9d0d77STakashi Iwai /* Set FE's runtime_update state; the state is protected via PCM stream lock
1640ea9d0d77STakashi Iwai  * for avoiding the race with trigger callback.
1641ea9d0d77STakashi Iwai  * If the state is unset and a trigger is pending while the previous operation,
1642ea9d0d77STakashi Iwai  * process the pending trigger action here.
1643ea9d0d77STakashi Iwai  */
1644ea9d0d77STakashi Iwai static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1645ea9d0d77STakashi Iwai 				     int stream, enum snd_soc_dpcm_update state)
1646ea9d0d77STakashi Iwai {
1647ea9d0d77STakashi Iwai 	struct snd_pcm_substream *substream =
1648ea9d0d77STakashi Iwai 		snd_soc_dpcm_get_substream(fe, stream);
1649ea9d0d77STakashi Iwai 
1650ea9d0d77STakashi Iwai 	snd_pcm_stream_lock_irq(substream);
1651ea9d0d77STakashi Iwai 	if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1652ea9d0d77STakashi Iwai 		dpcm_fe_dai_do_trigger(substream,
1653ea9d0d77STakashi Iwai 				       fe->dpcm[stream].trigger_pending - 1);
1654ea9d0d77STakashi Iwai 		fe->dpcm[stream].trigger_pending = 0;
1655ea9d0d77STakashi Iwai 	}
1656ea9d0d77STakashi Iwai 	fe->dpcm[stream].runtime_update = state;
1657ea9d0d77STakashi Iwai 	snd_pcm_stream_unlock_irq(substream);
1658ea9d0d77STakashi Iwai }
1659ea9d0d77STakashi Iwai 
1660906c7d69SPC Liao static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1661906c7d69SPC Liao 			       int stream)
1662906c7d69SPC Liao {
1663906c7d69SPC Liao 	struct snd_soc_dpcm *dpcm;
1664906c7d69SPC Liao 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1665906c7d69SPC Liao 	struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1666906c7d69SPC Liao 	int err;
1667906c7d69SPC Liao 
1668906c7d69SPC Liao 	/* apply symmetry for FE */
1669906c7d69SPC Liao 	if (soc_pcm_has_symmetry(fe_substream))
1670906c7d69SPC Liao 		fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1671906c7d69SPC Liao 
1672906c7d69SPC Liao 	/* Symmetry only applies if we've got an active stream. */
1673906c7d69SPC Liao 	if (fe_cpu_dai->active) {
1674906c7d69SPC Liao 		err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1675906c7d69SPC Liao 		if (err < 0)
1676906c7d69SPC Liao 			return err;
1677906c7d69SPC Liao 	}
1678906c7d69SPC Liao 
1679906c7d69SPC Liao 	/* apply symmetry for BE */
1680906c7d69SPC Liao 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1681906c7d69SPC Liao 		struct snd_soc_pcm_runtime *be = dpcm->be;
1682906c7d69SPC Liao 		struct snd_pcm_substream *be_substream =
1683906c7d69SPC Liao 			snd_soc_dpcm_get_substream(be, stream);
1684906c7d69SPC Liao 		struct snd_soc_pcm_runtime *rtd = be_substream->private_data;
1685906c7d69SPC Liao 		int i;
1686906c7d69SPC Liao 
1687f1176614SJeeja KP 		if (rtd->dai_link->be_hw_params_fixup)
1688f1176614SJeeja KP 			continue;
1689f1176614SJeeja KP 
1690906c7d69SPC Liao 		if (soc_pcm_has_symmetry(be_substream))
1691906c7d69SPC Liao 			be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1692906c7d69SPC Liao 
1693906c7d69SPC Liao 		/* Symmetry only applies if we've got an active stream. */
1694906c7d69SPC Liao 		if (rtd->cpu_dai->active) {
1695906c7d69SPC Liao 			err = soc_pcm_apply_symmetry(be_substream, rtd->cpu_dai);
1696906c7d69SPC Liao 			if (err < 0)
1697906c7d69SPC Liao 				return err;
1698906c7d69SPC Liao 		}
1699906c7d69SPC Liao 
1700906c7d69SPC Liao 		for (i = 0; i < rtd->num_codecs; i++) {
1701906c7d69SPC Liao 			if (rtd->codec_dais[i]->active) {
1702906c7d69SPC Liao 				err = soc_pcm_apply_symmetry(be_substream,
1703906c7d69SPC Liao 							     rtd->codec_dais[i]);
1704906c7d69SPC Liao 				if (err < 0)
1705906c7d69SPC Liao 					return err;
1706906c7d69SPC Liao 			}
1707906c7d69SPC Liao 		}
1708906c7d69SPC Liao 	}
1709906c7d69SPC Liao 
1710906c7d69SPC Liao 	return 0;
1711906c7d69SPC Liao }
1712906c7d69SPC Liao 
171301d7584cSLiam Girdwood static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
171401d7584cSLiam Girdwood {
171501d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
171601d7584cSLiam Girdwood 	struct snd_pcm_runtime *runtime = fe_substream->runtime;
171701d7584cSLiam Girdwood 	int stream = fe_substream->stream, ret = 0;
171801d7584cSLiam Girdwood 
1719ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
172001d7584cSLiam Girdwood 
172101d7584cSLiam Girdwood 	ret = dpcm_be_dai_startup(fe, fe_substream->stream);
172201d7584cSLiam Girdwood 	if (ret < 0) {
1723103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
172401d7584cSLiam Girdwood 		goto be_err;
172501d7584cSLiam Girdwood 	}
172601d7584cSLiam Girdwood 
1727103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
172801d7584cSLiam Girdwood 
172901d7584cSLiam Girdwood 	/* start the DAI frontend */
173001d7584cSLiam Girdwood 	ret = soc_pcm_open(fe_substream);
173101d7584cSLiam Girdwood 	if (ret < 0) {
1732103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
173301d7584cSLiam Girdwood 		goto unwind;
173401d7584cSLiam Girdwood 	}
173501d7584cSLiam Girdwood 
173601d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
173701d7584cSLiam Girdwood 
173801d7584cSLiam Girdwood 	dpcm_set_fe_runtime(fe_substream);
173901d7584cSLiam Girdwood 	snd_pcm_limit_hw_rates(runtime);
174001d7584cSLiam Girdwood 
1741906c7d69SPC Liao 	ret = dpcm_apply_symmetry(fe_substream, stream);
1742906c7d69SPC Liao 	if (ret < 0) {
1743906c7d69SPC Liao 		dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1744906c7d69SPC Liao 			ret);
1745906c7d69SPC Liao 		goto unwind;
1746906c7d69SPC Liao 	}
1747906c7d69SPC Liao 
1748ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
174901d7584cSLiam Girdwood 	return 0;
175001d7584cSLiam Girdwood 
175101d7584cSLiam Girdwood unwind:
175201d7584cSLiam Girdwood 	dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
175301d7584cSLiam Girdwood be_err:
1754ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
175501d7584cSLiam Girdwood 	return ret;
175601d7584cSLiam Girdwood }
175701d7584cSLiam Girdwood 
175823607025SLiam Girdwood int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
175901d7584cSLiam Girdwood {
176001d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
176101d7584cSLiam Girdwood 
176201d7584cSLiam Girdwood 	/* only shutdown BEs that are either sinks or sources to this FE DAI */
176301d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
176401d7584cSLiam Girdwood 
176501d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
176601d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
176701d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
176801d7584cSLiam Girdwood 
176901d7584cSLiam Girdwood 		/* is this op for this BE ? */
177001d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
177101d7584cSLiam Girdwood 			continue;
177201d7584cSLiam Girdwood 
177301d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1774103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
177501d7584cSLiam Girdwood 				stream ? "capture" : "playback",
177601d7584cSLiam Girdwood 				be->dpcm[stream].state);
177701d7584cSLiam Girdwood 
177801d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
177901d7584cSLiam Girdwood 			continue;
178001d7584cSLiam Girdwood 
178101d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
178201d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
178301d7584cSLiam Girdwood 			continue;
178401d7584cSLiam Girdwood 
1785103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: close BE %s\n",
178694d215ccS彭东林 			be->dai_link->name);
178701d7584cSLiam Girdwood 
178801d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
178901d7584cSLiam Girdwood 		be_substream->runtime = NULL;
179001d7584cSLiam Girdwood 
179101d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
179201d7584cSLiam Girdwood 	}
179301d7584cSLiam Girdwood 	return 0;
179401d7584cSLiam Girdwood }
179501d7584cSLiam Girdwood 
179601d7584cSLiam Girdwood static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
179701d7584cSLiam Girdwood {
179801d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
179901d7584cSLiam Girdwood 	int stream = substream->stream;
180001d7584cSLiam Girdwood 
1801ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
180201d7584cSLiam Girdwood 
180301d7584cSLiam Girdwood 	/* shutdown the BEs */
180401d7584cSLiam Girdwood 	dpcm_be_dai_shutdown(fe, substream->stream);
180501d7584cSLiam Girdwood 
1806103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
180701d7584cSLiam Girdwood 
180801d7584cSLiam Girdwood 	/* now shutdown the frontend */
180901d7584cSLiam Girdwood 	soc_pcm_close(substream);
181001d7584cSLiam Girdwood 
181101d7584cSLiam Girdwood 	/* run the stream event for each BE */
181201d7584cSLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
181301d7584cSLiam Girdwood 
181401d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1815ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
181601d7584cSLiam Girdwood 	return 0;
181701d7584cSLiam Girdwood }
181801d7584cSLiam Girdwood 
181923607025SLiam Girdwood int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
182001d7584cSLiam Girdwood {
182101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
182201d7584cSLiam Girdwood 
182301d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
182401d7584cSLiam Girdwood 	 * to this frontend DAI */
182501d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
182601d7584cSLiam Girdwood 
182701d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
182801d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
182901d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
183001d7584cSLiam Girdwood 
183101d7584cSLiam Girdwood 		/* is this op for this BE ? */
183201d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
183301d7584cSLiam Girdwood 			continue;
183401d7584cSLiam Girdwood 
183501d7584cSLiam Girdwood 		/* only free hw when no longer used - check all FEs */
183601d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
183701d7584cSLiam Girdwood 				continue;
183801d7584cSLiam Girdwood 
183936fba62cSQiao Zhou 		/* do not free hw if this BE is used by other FE */
184036fba62cSQiao Zhou 		if (be->dpcm[stream].users > 1)
184136fba62cSQiao Zhou 			continue;
184236fba62cSQiao Zhou 
184301d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
184401d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
184501d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
184608b27848SPatrick Lai 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
18475e82d2beSVinod Koul 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
18485e82d2beSVinod Koul 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
184901d7584cSLiam Girdwood 			continue;
185001d7584cSLiam Girdwood 
1851103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
185294d215ccS彭东林 			be->dai_link->name);
185301d7584cSLiam Girdwood 
185401d7584cSLiam Girdwood 		soc_pcm_hw_free(be_substream);
185501d7584cSLiam Girdwood 
185601d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
185701d7584cSLiam Girdwood 	}
185801d7584cSLiam Girdwood 
185901d7584cSLiam Girdwood 	return 0;
186001d7584cSLiam Girdwood }
186101d7584cSLiam Girdwood 
186245c0a188SMark Brown static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
186301d7584cSLiam Girdwood {
186401d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
186501d7584cSLiam Girdwood 	int err, stream = substream->stream;
186601d7584cSLiam Girdwood 
186701d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1868ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
186901d7584cSLiam Girdwood 
1870103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
187101d7584cSLiam Girdwood 
187201d7584cSLiam Girdwood 	/* call hw_free on the frontend */
187301d7584cSLiam Girdwood 	err = soc_pcm_hw_free(substream);
187401d7584cSLiam Girdwood 	if (err < 0)
1875103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
187601d7584cSLiam Girdwood 			fe->dai_link->name);
187701d7584cSLiam Girdwood 
187801d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
187901d7584cSLiam Girdwood 	 * to this frontend DAI */
188001d7584cSLiam Girdwood 	err = dpcm_be_dai_hw_free(fe, stream);
188101d7584cSLiam Girdwood 
188201d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1883ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
188401d7584cSLiam Girdwood 
188501d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
188601d7584cSLiam Girdwood 	return 0;
188701d7584cSLiam Girdwood }
188801d7584cSLiam Girdwood 
188923607025SLiam Girdwood int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
189001d7584cSLiam Girdwood {
189101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
189201d7584cSLiam Girdwood 	int ret;
189301d7584cSLiam Girdwood 
189401d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
189501d7584cSLiam Girdwood 
189601d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
189701d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
189801d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
189901d7584cSLiam Girdwood 
190001d7584cSLiam Girdwood 		/* is this op for this BE ? */
190101d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
190201d7584cSLiam Girdwood 			continue;
190301d7584cSLiam Girdwood 
190401d7584cSLiam Girdwood 		/* copy params for each dpcm */
190501d7584cSLiam Girdwood 		memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
190601d7584cSLiam Girdwood 				sizeof(struct snd_pcm_hw_params));
190701d7584cSLiam Girdwood 
190801d7584cSLiam Girdwood 		/* perform any hw_params fixups */
190901d7584cSLiam Girdwood 		if (be->dai_link->be_hw_params_fixup) {
191001d7584cSLiam Girdwood 			ret = be->dai_link->be_hw_params_fixup(be,
191101d7584cSLiam Girdwood 					&dpcm->hw_params);
191201d7584cSLiam Girdwood 			if (ret < 0) {
191301d7584cSLiam Girdwood 				dev_err(be->dev,
1914103d84a3SLiam Girdwood 					"ASoC: hw_params BE fixup failed %d\n",
191501d7584cSLiam Girdwood 					ret);
191601d7584cSLiam Girdwood 				goto unwind;
191701d7584cSLiam Girdwood 			}
191801d7584cSLiam Girdwood 		}
191901d7584cSLiam Girdwood 
1920b0639bd2SKuninori Morimoto 		/* only allow hw_params() if no connected FEs are running */
1921b0639bd2SKuninori Morimoto 		if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1922b0639bd2SKuninori Morimoto 			continue;
1923b0639bd2SKuninori Morimoto 
1924b0639bd2SKuninori Morimoto 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1925b0639bd2SKuninori Morimoto 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1926b0639bd2SKuninori Morimoto 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1927b0639bd2SKuninori Morimoto 			continue;
1928b0639bd2SKuninori Morimoto 
1929b0639bd2SKuninori Morimoto 		dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
193094d215ccS彭东林 			be->dai_link->name);
1931b0639bd2SKuninori Morimoto 
193201d7584cSLiam Girdwood 		ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
193301d7584cSLiam Girdwood 		if (ret < 0) {
193401d7584cSLiam Girdwood 			dev_err(dpcm->be->dev,
1935103d84a3SLiam Girdwood 				"ASoC: hw_params BE failed %d\n", ret);
193601d7584cSLiam Girdwood 			goto unwind;
193701d7584cSLiam Girdwood 		}
193801d7584cSLiam Girdwood 
193901d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
194001d7584cSLiam Girdwood 	}
194101d7584cSLiam Girdwood 	return 0;
194201d7584cSLiam Girdwood 
194301d7584cSLiam Girdwood unwind:
194401d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
194501d7584cSLiam Girdwood 	list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
194601d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
194701d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
194801d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
194901d7584cSLiam Girdwood 
195001d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
195101d7584cSLiam Girdwood 			continue;
195201d7584cSLiam Girdwood 
195301d7584cSLiam Girdwood 		/* only allow hw_free() if no connected FEs are running */
195401d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
195501d7584cSLiam Girdwood 			continue;
195601d7584cSLiam Girdwood 
195701d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
195801d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
195901d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
196001d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
196101d7584cSLiam Girdwood 			continue;
196201d7584cSLiam Girdwood 
196301d7584cSLiam Girdwood 		soc_pcm_hw_free(be_substream);
196401d7584cSLiam Girdwood 	}
196501d7584cSLiam Girdwood 
196601d7584cSLiam Girdwood 	return ret;
196701d7584cSLiam Girdwood }
196801d7584cSLiam Girdwood 
196945c0a188SMark Brown static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
197001d7584cSLiam Girdwood 				 struct snd_pcm_hw_params *params)
197101d7584cSLiam Girdwood {
197201d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
197301d7584cSLiam Girdwood 	int ret, stream = substream->stream;
197401d7584cSLiam Girdwood 
197501d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1976ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
197701d7584cSLiam Girdwood 
197801d7584cSLiam Girdwood 	memcpy(&fe->dpcm[substream->stream].hw_params, params,
197901d7584cSLiam Girdwood 			sizeof(struct snd_pcm_hw_params));
198001d7584cSLiam Girdwood 	ret = dpcm_be_dai_hw_params(fe, substream->stream);
198101d7584cSLiam Girdwood 	if (ret < 0) {
1982103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
198301d7584cSLiam Girdwood 		goto out;
198401d7584cSLiam Girdwood 	}
198501d7584cSLiam Girdwood 
1986103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
198701d7584cSLiam Girdwood 			fe->dai_link->name, params_rate(params),
198801d7584cSLiam Girdwood 			params_channels(params), params_format(params));
198901d7584cSLiam Girdwood 
199001d7584cSLiam Girdwood 	/* call hw_params on the frontend */
199101d7584cSLiam Girdwood 	ret = soc_pcm_hw_params(substream, params);
199201d7584cSLiam Girdwood 	if (ret < 0) {
1993103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
199401d7584cSLiam Girdwood 		dpcm_be_dai_hw_free(fe, stream);
199501d7584cSLiam Girdwood 	 } else
199601d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
199701d7584cSLiam Girdwood 
199801d7584cSLiam Girdwood out:
1999ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
200001d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
200101d7584cSLiam Girdwood 	return ret;
200201d7584cSLiam Girdwood }
200301d7584cSLiam Girdwood 
200401d7584cSLiam Girdwood static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
200501d7584cSLiam Girdwood 		struct snd_pcm_substream *substream, int cmd)
200601d7584cSLiam Girdwood {
200701d7584cSLiam Girdwood 	int ret;
200801d7584cSLiam Girdwood 
2009103d84a3SLiam Girdwood 	dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
201094d215ccS彭东林 			dpcm->be->dai_link->name, cmd);
201101d7584cSLiam Girdwood 
201201d7584cSLiam Girdwood 	ret = soc_pcm_trigger(substream, cmd);
201301d7584cSLiam Girdwood 	if (ret < 0)
2014103d84a3SLiam Girdwood 		dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
201501d7584cSLiam Girdwood 
201601d7584cSLiam Girdwood 	return ret;
201701d7584cSLiam Girdwood }
201801d7584cSLiam Girdwood 
201923607025SLiam Girdwood int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
202045c0a188SMark Brown 			       int cmd)
202101d7584cSLiam Girdwood {
202201d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
202301d7584cSLiam Girdwood 	int ret = 0;
202401d7584cSLiam Girdwood 
202501d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
202601d7584cSLiam Girdwood 
202701d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
202801d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
202901d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
203001d7584cSLiam Girdwood 
203101d7584cSLiam Girdwood 		/* is this op for this BE ? */
203201d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
203301d7584cSLiam Girdwood 			continue;
203401d7584cSLiam Girdwood 
203501d7584cSLiam Girdwood 		switch (cmd) {
203601d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_START:
203701d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
203801d7584cSLiam Girdwood 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
203901d7584cSLiam Girdwood 				continue;
204001d7584cSLiam Girdwood 
204101d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
204201d7584cSLiam Girdwood 			if (ret)
204301d7584cSLiam Girdwood 				return ret;
204401d7584cSLiam Girdwood 
204501d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
204601d7584cSLiam Girdwood 			break;
204701d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_RESUME:
204801d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
204901d7584cSLiam Girdwood 				continue;
205001d7584cSLiam Girdwood 
205101d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
205201d7584cSLiam Girdwood 			if (ret)
205301d7584cSLiam Girdwood 				return ret;
205401d7584cSLiam Girdwood 
205501d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
205601d7584cSLiam Girdwood 			break;
205701d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
205801d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
205901d7584cSLiam Girdwood 				continue;
206001d7584cSLiam Girdwood 
206101d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
206201d7584cSLiam Girdwood 			if (ret)
206301d7584cSLiam Girdwood 				return ret;
206401d7584cSLiam Girdwood 
206501d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
206601d7584cSLiam Girdwood 			break;
206701d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_STOP:
206801d7584cSLiam Girdwood 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
206901d7584cSLiam Girdwood 				continue;
207001d7584cSLiam Girdwood 
207101d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
207201d7584cSLiam Girdwood 				continue;
207301d7584cSLiam Girdwood 
207401d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
207501d7584cSLiam Girdwood 			if (ret)
207601d7584cSLiam Girdwood 				return ret;
207701d7584cSLiam Girdwood 
207801d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
207901d7584cSLiam Girdwood 			break;
208001d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_SUSPEND:
2081868a6ca8SNicolin Chen 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
208201d7584cSLiam Girdwood 				continue;
208301d7584cSLiam Girdwood 
208401d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
208501d7584cSLiam Girdwood 				continue;
208601d7584cSLiam Girdwood 
208701d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
208801d7584cSLiam Girdwood 			if (ret)
208901d7584cSLiam Girdwood 				return ret;
209001d7584cSLiam Girdwood 
209101d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
209201d7584cSLiam Girdwood 			break;
209301d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
209401d7584cSLiam Girdwood 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
209501d7584cSLiam Girdwood 				continue;
209601d7584cSLiam Girdwood 
209701d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
209801d7584cSLiam Girdwood 				continue;
209901d7584cSLiam Girdwood 
210001d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
210101d7584cSLiam Girdwood 			if (ret)
210201d7584cSLiam Girdwood 				return ret;
210301d7584cSLiam Girdwood 
210401d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
210501d7584cSLiam Girdwood 			break;
210601d7584cSLiam Girdwood 		}
210701d7584cSLiam Girdwood 	}
210801d7584cSLiam Girdwood 
210901d7584cSLiam Girdwood 	return ret;
211001d7584cSLiam Girdwood }
211101d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
211201d7584cSLiam Girdwood 
2113ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
211401d7584cSLiam Girdwood {
211501d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
211601d7584cSLiam Girdwood 	int stream = substream->stream, ret;
211701d7584cSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
211801d7584cSLiam Girdwood 
211901d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
212001d7584cSLiam Girdwood 
212101d7584cSLiam Girdwood 	switch (trigger) {
212201d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_PRE:
212301d7584cSLiam Girdwood 		/* call trigger on the frontend before the backend. */
212401d7584cSLiam Girdwood 
2125103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
212601d7584cSLiam Girdwood 				fe->dai_link->name, cmd);
212701d7584cSLiam Girdwood 
212801d7584cSLiam Girdwood 		ret = soc_pcm_trigger(substream, cmd);
212901d7584cSLiam Girdwood 		if (ret < 0) {
2130103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
213101d7584cSLiam Girdwood 			goto out;
213201d7584cSLiam Girdwood 		}
213301d7584cSLiam Girdwood 
213401d7584cSLiam Girdwood 		ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
213501d7584cSLiam Girdwood 		break;
213601d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_POST:
213701d7584cSLiam Girdwood 		/* call trigger on the frontend after the backend. */
213801d7584cSLiam Girdwood 
213901d7584cSLiam Girdwood 		ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
214001d7584cSLiam Girdwood 		if (ret < 0) {
2141103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
214201d7584cSLiam Girdwood 			goto out;
214301d7584cSLiam Girdwood 		}
214401d7584cSLiam Girdwood 
2145103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
214601d7584cSLiam Girdwood 				fe->dai_link->name, cmd);
214701d7584cSLiam Girdwood 
214801d7584cSLiam Girdwood 		ret = soc_pcm_trigger(substream, cmd);
214901d7584cSLiam Girdwood 		break;
215007bf84aaSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_BESPOKE:
215107bf84aaSLiam Girdwood 		/* bespoke trigger() - handles both FE and BEs */
215207bf84aaSLiam Girdwood 
2153103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
215407bf84aaSLiam Girdwood 				fe->dai_link->name, cmd);
215507bf84aaSLiam Girdwood 
215607bf84aaSLiam Girdwood 		ret = soc_pcm_bespoke_trigger(substream, cmd);
215707bf84aaSLiam Girdwood 		if (ret < 0) {
2158103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
215907bf84aaSLiam Girdwood 			goto out;
216007bf84aaSLiam Girdwood 		}
216107bf84aaSLiam Girdwood 		break;
216201d7584cSLiam Girdwood 	default:
2163103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
216401d7584cSLiam Girdwood 				fe->dai_link->name);
216501d7584cSLiam Girdwood 		ret = -EINVAL;
216601d7584cSLiam Girdwood 		goto out;
216701d7584cSLiam Girdwood 	}
216801d7584cSLiam Girdwood 
216901d7584cSLiam Girdwood 	switch (cmd) {
217001d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
217101d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_RESUME:
217201d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
217301d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
217401d7584cSLiam Girdwood 		break;
217501d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_STOP:
217601d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_SUSPEND:
217701d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
217801d7584cSLiam Girdwood 		break;
21799f169b9fSPatrick Lai 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
21809f169b9fSPatrick Lai 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
21819f169b9fSPatrick Lai 		break;
218201d7584cSLiam Girdwood 	}
218301d7584cSLiam Girdwood 
218401d7584cSLiam Girdwood out:
218501d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
218601d7584cSLiam Girdwood 	return ret;
218701d7584cSLiam Girdwood }
218801d7584cSLiam Girdwood 
2189ea9d0d77STakashi Iwai static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2190ea9d0d77STakashi Iwai {
2191ea9d0d77STakashi Iwai 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2192ea9d0d77STakashi Iwai 	int stream = substream->stream;
2193ea9d0d77STakashi Iwai 
2194ea9d0d77STakashi Iwai 	/* if FE's runtime_update is already set, we're in race;
2195ea9d0d77STakashi Iwai 	 * process this trigger later at exit
2196ea9d0d77STakashi Iwai 	 */
2197ea9d0d77STakashi Iwai 	if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2198ea9d0d77STakashi Iwai 		fe->dpcm[stream].trigger_pending = cmd + 1;
2199ea9d0d77STakashi Iwai 		return 0; /* delayed, assuming it's successful */
2200ea9d0d77STakashi Iwai 	}
2201ea9d0d77STakashi Iwai 
2202ea9d0d77STakashi Iwai 	/* we're alone, let's trigger */
2203ea9d0d77STakashi Iwai 	return dpcm_fe_dai_do_trigger(substream, cmd);
2204ea9d0d77STakashi Iwai }
2205ea9d0d77STakashi Iwai 
220623607025SLiam Girdwood int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
220701d7584cSLiam Girdwood {
220801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
220901d7584cSLiam Girdwood 	int ret = 0;
221001d7584cSLiam Girdwood 
221101d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
221201d7584cSLiam Girdwood 
221301d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
221401d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
221501d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
221601d7584cSLiam Girdwood 
221701d7584cSLiam Girdwood 		/* is this op for this BE ? */
221801d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
221901d7584cSLiam Girdwood 			continue;
222001d7584cSLiam Girdwood 
222101d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
222295f444dcSKoro Chen 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
222395f444dcSKoro Chen 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
222401d7584cSLiam Girdwood 			continue;
222501d7584cSLiam Girdwood 
2226103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: prepare BE %s\n",
222794d215ccS彭东林 			be->dai_link->name);
222801d7584cSLiam Girdwood 
222901d7584cSLiam Girdwood 		ret = soc_pcm_prepare(be_substream);
223001d7584cSLiam Girdwood 		if (ret < 0) {
2231103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: backend prepare failed %d\n",
223201d7584cSLiam Girdwood 				ret);
223301d7584cSLiam Girdwood 			break;
223401d7584cSLiam Girdwood 		}
223501d7584cSLiam Girdwood 
223601d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
223701d7584cSLiam Girdwood 	}
223801d7584cSLiam Girdwood 	return ret;
223901d7584cSLiam Girdwood }
224001d7584cSLiam Girdwood 
224145c0a188SMark Brown static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
224201d7584cSLiam Girdwood {
224301d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
224401d7584cSLiam Girdwood 	int stream = substream->stream, ret = 0;
224501d7584cSLiam Girdwood 
224601d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
224701d7584cSLiam Girdwood 
2248103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
224901d7584cSLiam Girdwood 
2250ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
225101d7584cSLiam Girdwood 
225201d7584cSLiam Girdwood 	/* there is no point preparing this FE if there are no BEs */
225301d7584cSLiam Girdwood 	if (list_empty(&fe->dpcm[stream].be_clients)) {
2254103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
225501d7584cSLiam Girdwood 				fe->dai_link->name);
225601d7584cSLiam Girdwood 		ret = -EINVAL;
225701d7584cSLiam Girdwood 		goto out;
225801d7584cSLiam Girdwood 	}
225901d7584cSLiam Girdwood 
226001d7584cSLiam Girdwood 	ret = dpcm_be_dai_prepare(fe, substream->stream);
226101d7584cSLiam Girdwood 	if (ret < 0)
226201d7584cSLiam Girdwood 		goto out;
226301d7584cSLiam Girdwood 
226401d7584cSLiam Girdwood 	/* call prepare on the frontend */
226501d7584cSLiam Girdwood 	ret = soc_pcm_prepare(substream);
226601d7584cSLiam Girdwood 	if (ret < 0) {
2267103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
226801d7584cSLiam Girdwood 			fe->dai_link->name);
226901d7584cSLiam Girdwood 		goto out;
227001d7584cSLiam Girdwood 	}
227101d7584cSLiam Girdwood 
227201d7584cSLiam Girdwood 	/* run the stream event for each BE */
227301d7584cSLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
227401d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
227501d7584cSLiam Girdwood 
227601d7584cSLiam Girdwood out:
2277ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
227801d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
227901d7584cSLiam Girdwood 
228001d7584cSLiam Girdwood 	return ret;
228101d7584cSLiam Girdwood }
228201d7584cSLiam Girdwood 
2283be3f3f2cSLiam Girdwood static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2284be3f3f2cSLiam Girdwood 		     unsigned int cmd, void *arg)
2285be3f3f2cSLiam Girdwood {
2286be3f3f2cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
2287be3f3f2cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
2288be3f3f2cSLiam Girdwood 
2289c5914b0aSMark Brown 	if (platform->driver->ops && platform->driver->ops->ioctl)
2290be3f3f2cSLiam Girdwood 		return platform->driver->ops->ioctl(substream, cmd, arg);
2291be3f3f2cSLiam Girdwood 	return snd_pcm_lib_ioctl(substream, cmd, arg);
2292be3f3f2cSLiam Girdwood }
2293be3f3f2cSLiam Girdwood 
2294618dae11SLiam Girdwood static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2295618dae11SLiam Girdwood {
229607bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
229707bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
229807bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2299618dae11SLiam Girdwood 	int err;
230001d7584cSLiam Girdwood 
2301103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2302618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2303618dae11SLiam Girdwood 
230407bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
230507bf84aaSLiam Girdwood 		/* call bespoke trigger - FE takes care of all BE triggers */
2306103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
230707bf84aaSLiam Girdwood 				fe->dai_link->name);
230807bf84aaSLiam Girdwood 
230907bf84aaSLiam Girdwood 		err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
231007bf84aaSLiam Girdwood 		if (err < 0)
2311103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
231207bf84aaSLiam Girdwood 	} else {
2313103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
231407bf84aaSLiam Girdwood 			fe->dai_link->name);
231507bf84aaSLiam Girdwood 
2316618dae11SLiam Girdwood 		err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2317618dae11SLiam Girdwood 		if (err < 0)
2318103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
231907bf84aaSLiam Girdwood 	}
2320618dae11SLiam Girdwood 
2321618dae11SLiam Girdwood 	err = dpcm_be_dai_hw_free(fe, stream);
2322618dae11SLiam Girdwood 	if (err < 0)
2323103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2324618dae11SLiam Girdwood 
2325618dae11SLiam Girdwood 	err = dpcm_be_dai_shutdown(fe, stream);
2326618dae11SLiam Girdwood 	if (err < 0)
2327103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2328618dae11SLiam Girdwood 
2329618dae11SLiam Girdwood 	/* run the stream event for each BE */
2330618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2331618dae11SLiam Girdwood 
2332618dae11SLiam Girdwood 	return 0;
2333618dae11SLiam Girdwood }
2334618dae11SLiam Girdwood 
2335618dae11SLiam Girdwood static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2336618dae11SLiam Girdwood {
233707bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
233807bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
2339618dae11SLiam Girdwood 	struct snd_soc_dpcm *dpcm;
234007bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2341618dae11SLiam Girdwood 	int ret;
2342618dae11SLiam Girdwood 
2343103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2344618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2345618dae11SLiam Girdwood 
2346618dae11SLiam Girdwood 	/* Only start the BE if the FE is ready */
2347618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2348618dae11SLiam Girdwood 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2349618dae11SLiam Girdwood 		return -EINVAL;
2350618dae11SLiam Girdwood 
2351618dae11SLiam Girdwood 	/* startup must always be called for new BEs */
2352618dae11SLiam Girdwood 	ret = dpcm_be_dai_startup(fe, stream);
2353fffc0ca2SDan Carpenter 	if (ret < 0)
2354618dae11SLiam Girdwood 		goto disconnect;
2355618dae11SLiam Girdwood 
2356618dae11SLiam Girdwood 	/* keep going if FE state is > open */
2357618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2358618dae11SLiam Girdwood 		return 0;
2359618dae11SLiam Girdwood 
2360618dae11SLiam Girdwood 	ret = dpcm_be_dai_hw_params(fe, stream);
2361fffc0ca2SDan Carpenter 	if (ret < 0)
2362618dae11SLiam Girdwood 		goto close;
2363618dae11SLiam Girdwood 
2364618dae11SLiam Girdwood 	/* keep going if FE state is > hw_params */
2365618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2366618dae11SLiam Girdwood 		return 0;
2367618dae11SLiam Girdwood 
2368618dae11SLiam Girdwood 
2369618dae11SLiam Girdwood 	ret = dpcm_be_dai_prepare(fe, stream);
2370fffc0ca2SDan Carpenter 	if (ret < 0)
2371618dae11SLiam Girdwood 		goto hw_free;
2372618dae11SLiam Girdwood 
2373618dae11SLiam Girdwood 	/* run the stream event for each BE */
2374618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2375618dae11SLiam Girdwood 
2376618dae11SLiam Girdwood 	/* keep going if FE state is > prepare */
2377618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2378618dae11SLiam Girdwood 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2379618dae11SLiam Girdwood 		return 0;
2380618dae11SLiam Girdwood 
238107bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
238207bf84aaSLiam Girdwood 		/* call trigger on the frontend - FE takes care of all BE triggers */
2383103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
238407bf84aaSLiam Girdwood 				fe->dai_link->name);
238507bf84aaSLiam Girdwood 
238607bf84aaSLiam Girdwood 		ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
238707bf84aaSLiam Girdwood 		if (ret < 0) {
2388103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
238907bf84aaSLiam Girdwood 			goto hw_free;
239007bf84aaSLiam Girdwood 		}
239107bf84aaSLiam Girdwood 	} else {
2392103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2393618dae11SLiam Girdwood 			fe->dai_link->name);
2394618dae11SLiam Girdwood 
2395618dae11SLiam Girdwood 		ret = dpcm_be_dai_trigger(fe, stream,
2396618dae11SLiam Girdwood 					SNDRV_PCM_TRIGGER_START);
2397618dae11SLiam Girdwood 		if (ret < 0) {
2398103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2399618dae11SLiam Girdwood 			goto hw_free;
2400618dae11SLiam Girdwood 		}
240107bf84aaSLiam Girdwood 	}
2402618dae11SLiam Girdwood 
2403618dae11SLiam Girdwood 	return 0;
2404618dae11SLiam Girdwood 
2405618dae11SLiam Girdwood hw_free:
2406618dae11SLiam Girdwood 	dpcm_be_dai_hw_free(fe, stream);
2407618dae11SLiam Girdwood close:
2408618dae11SLiam Girdwood 	dpcm_be_dai_shutdown(fe, stream);
2409618dae11SLiam Girdwood disconnect:
2410618dae11SLiam Girdwood 	/* disconnect any non started BEs */
2411618dae11SLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2412618dae11SLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
2413618dae11SLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2414618dae11SLiam Girdwood 				dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2415618dae11SLiam Girdwood 	}
2416618dae11SLiam Girdwood 
2417618dae11SLiam Girdwood 	return ret;
2418618dae11SLiam Girdwood }
2419618dae11SLiam Girdwood 
2420618dae11SLiam Girdwood static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2421618dae11SLiam Girdwood {
2422618dae11SLiam Girdwood 	int ret;
2423618dae11SLiam Girdwood 
2424ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2425618dae11SLiam Girdwood 	ret = dpcm_run_update_startup(fe, stream);
2426618dae11SLiam Girdwood 	if (ret < 0)
2427103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2428ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2429618dae11SLiam Girdwood 
2430618dae11SLiam Girdwood 	return ret;
2431618dae11SLiam Girdwood }
2432618dae11SLiam Girdwood 
2433618dae11SLiam Girdwood static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2434618dae11SLiam Girdwood {
2435618dae11SLiam Girdwood 	int ret;
2436618dae11SLiam Girdwood 
2437ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2438618dae11SLiam Girdwood 	ret = dpcm_run_update_shutdown(fe, stream);
2439618dae11SLiam Girdwood 	if (ret < 0)
2440103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2441ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2442618dae11SLiam Girdwood 
2443618dae11SLiam Girdwood 	return ret;
2444618dae11SLiam Girdwood }
2445618dae11SLiam Girdwood 
2446618dae11SLiam Girdwood /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2447618dae11SLiam Girdwood  * any DAI links.
2448618dae11SLiam Girdwood  */
2449c3f48ae6SLars-Peter Clausen int soc_dpcm_runtime_update(struct snd_soc_card *card)
2450618dae11SLiam Girdwood {
24511a497983SMengdong Lin 	struct snd_soc_pcm_runtime *fe;
24521a497983SMengdong Lin 	int old, new, paths;
2453618dae11SLiam Girdwood 
2454618dae11SLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
24551a497983SMengdong Lin 	list_for_each_entry(fe, &card->rtd_list, list) {
2456618dae11SLiam Girdwood 		struct snd_soc_dapm_widget_list *list;
2457618dae11SLiam Girdwood 
2458618dae11SLiam Girdwood 		/* make sure link is FE */
2459618dae11SLiam Girdwood 		if (!fe->dai_link->dynamic)
2460618dae11SLiam Girdwood 			continue;
2461618dae11SLiam Girdwood 
2462618dae11SLiam Girdwood 		/* only check active links */
2463618dae11SLiam Girdwood 		if (!fe->cpu_dai->active)
2464618dae11SLiam Girdwood 			continue;
2465618dae11SLiam Girdwood 
2466618dae11SLiam Girdwood 		/* DAPM sync will call this to update DSP paths */
2467103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
2468618dae11SLiam Girdwood 			fe->dai_link->name);
2469618dae11SLiam Girdwood 
2470618dae11SLiam Girdwood 		/* skip if FE doesn't have playback capability */
2471075207d2SQiao Zhou 		if (!fe->cpu_dai->driver->playback.channels_min
2472075207d2SQiao Zhou 		    || !fe->codec_dai->driver->playback.channels_min)
2473075207d2SQiao Zhou 			goto capture;
2474075207d2SQiao Zhou 
2475075207d2SQiao Zhou 		/* skip if FE isn't currently playing */
2476075207d2SQiao Zhou 		if (!fe->cpu_dai->playback_active
2477075207d2SQiao Zhou 		    || !fe->codec_dai->playback_active)
2478618dae11SLiam Girdwood 			goto capture;
2479618dae11SLiam Girdwood 
2480618dae11SLiam Girdwood 		paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2481618dae11SLiam Girdwood 		if (paths < 0) {
2482103d84a3SLiam Girdwood 			dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2483618dae11SLiam Girdwood 					fe->dai_link->name,  "playback");
2484618dae11SLiam Girdwood 			mutex_unlock(&card->mutex);
2485618dae11SLiam Girdwood 			return paths;
2486618dae11SLiam Girdwood 		}
2487618dae11SLiam Girdwood 
2488618dae11SLiam Girdwood 		/* update any new playback paths */
2489618dae11SLiam Girdwood 		new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2490618dae11SLiam Girdwood 		if (new) {
2491618dae11SLiam Girdwood 			dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2492618dae11SLiam Girdwood 			dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2493618dae11SLiam Girdwood 			dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2494618dae11SLiam Girdwood 		}
2495618dae11SLiam Girdwood 
2496618dae11SLiam Girdwood 		/* update any old playback paths */
2497618dae11SLiam Girdwood 		old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2498618dae11SLiam Girdwood 		if (old) {
2499618dae11SLiam Girdwood 			dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2500618dae11SLiam Girdwood 			dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2501618dae11SLiam Girdwood 			dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2502618dae11SLiam Girdwood 		}
2503618dae11SLiam Girdwood 
25047ed9de76SQiao Zhou 		dpcm_path_put(&list);
2505618dae11SLiam Girdwood capture:
2506618dae11SLiam Girdwood 		/* skip if FE doesn't have capture capability */
2507075207d2SQiao Zhou 		if (!fe->cpu_dai->driver->capture.channels_min
2508075207d2SQiao Zhou 		    || !fe->codec_dai->driver->capture.channels_min)
2509075207d2SQiao Zhou 			continue;
2510075207d2SQiao Zhou 
2511075207d2SQiao Zhou 		/* skip if FE isn't currently capturing */
2512075207d2SQiao Zhou 		if (!fe->cpu_dai->capture_active
2513075207d2SQiao Zhou 		    || !fe->codec_dai->capture_active)
2514618dae11SLiam Girdwood 			continue;
2515618dae11SLiam Girdwood 
2516618dae11SLiam Girdwood 		paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2517618dae11SLiam Girdwood 		if (paths < 0) {
2518103d84a3SLiam Girdwood 			dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2519618dae11SLiam Girdwood 					fe->dai_link->name,  "capture");
2520618dae11SLiam Girdwood 			mutex_unlock(&card->mutex);
2521618dae11SLiam Girdwood 			return paths;
2522618dae11SLiam Girdwood 		}
2523618dae11SLiam Girdwood 
2524618dae11SLiam Girdwood 		/* update any new capture paths */
2525618dae11SLiam Girdwood 		new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2526618dae11SLiam Girdwood 		if (new) {
2527618dae11SLiam Girdwood 			dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2528618dae11SLiam Girdwood 			dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2529618dae11SLiam Girdwood 			dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2530618dae11SLiam Girdwood 		}
2531618dae11SLiam Girdwood 
2532618dae11SLiam Girdwood 		/* update any old capture paths */
2533618dae11SLiam Girdwood 		old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2534618dae11SLiam Girdwood 		if (old) {
2535618dae11SLiam Girdwood 			dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2536618dae11SLiam Girdwood 			dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2537618dae11SLiam Girdwood 			dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2538618dae11SLiam Girdwood 		}
2539618dae11SLiam Girdwood 
2540618dae11SLiam Girdwood 		dpcm_path_put(&list);
2541618dae11SLiam Girdwood 	}
2542618dae11SLiam Girdwood 
2543618dae11SLiam Girdwood 	mutex_unlock(&card->mutex);
2544618dae11SLiam Girdwood 	return 0;
2545618dae11SLiam Girdwood }
254601d7584cSLiam Girdwood int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
254701d7584cSLiam Girdwood {
254801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
254901d7584cSLiam Girdwood 	struct list_head *clients =
255001d7584cSLiam Girdwood 		&fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
255101d7584cSLiam Girdwood 
255201d7584cSLiam Girdwood 	list_for_each_entry(dpcm, clients, list_be) {
255301d7584cSLiam Girdwood 
255401d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
25552e5894d7SBenoit Cousson 		int i;
255601d7584cSLiam Girdwood 
255701d7584cSLiam Girdwood 		if (be->dai_link->ignore_suspend)
255801d7584cSLiam Girdwood 			continue;
255901d7584cSLiam Girdwood 
25602e5894d7SBenoit Cousson 		for (i = 0; i < be->num_codecs; i++) {
25612e5894d7SBenoit Cousson 			struct snd_soc_dai *dai = be->codec_dais[i];
25622e5894d7SBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
256301d7584cSLiam Girdwood 
25642e5894d7SBenoit Cousson 			dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
25652e5894d7SBenoit Cousson 					 be->dai_link->name);
25662e5894d7SBenoit Cousson 
25672e5894d7SBenoit Cousson 			if (drv->ops && drv->ops->digital_mute &&
25682e5894d7SBenoit Cousson 							dai->playback_active)
256901d7584cSLiam Girdwood 				drv->ops->digital_mute(dai, mute);
257001d7584cSLiam Girdwood 		}
25712e5894d7SBenoit Cousson 	}
257201d7584cSLiam Girdwood 
257301d7584cSLiam Girdwood 	return 0;
257401d7584cSLiam Girdwood }
257501d7584cSLiam Girdwood 
257645c0a188SMark Brown static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
257701d7584cSLiam Girdwood {
257801d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
257901d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
258001d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list;
258101d7584cSLiam Girdwood 	int ret;
258201d7584cSLiam Girdwood 	int stream = fe_substream->stream;
258301d7584cSLiam Girdwood 
258401d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
258501d7584cSLiam Girdwood 	fe->dpcm[stream].runtime = fe_substream->runtime;
258601d7584cSLiam Girdwood 
25878f70e515SQiao Zhou 	ret = dpcm_path_get(fe, stream, &list);
25888f70e515SQiao Zhou 	if (ret < 0) {
25898f70e515SQiao Zhou 		mutex_unlock(&fe->card->mutex);
25908f70e515SQiao Zhou 		return ret;
25918f70e515SQiao Zhou 	} else if (ret == 0) {
2592103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
259301d7584cSLiam Girdwood 			fe->dai_link->name, stream ? "capture" : "playback");
259401d7584cSLiam Girdwood 	}
259501d7584cSLiam Girdwood 
259601d7584cSLiam Girdwood 	/* calculate valid and active FE <-> BE dpcms */
259701d7584cSLiam Girdwood 	dpcm_process_paths(fe, stream, &list, 1);
259801d7584cSLiam Girdwood 
259901d7584cSLiam Girdwood 	ret = dpcm_fe_dai_startup(fe_substream);
260001d7584cSLiam Girdwood 	if (ret < 0) {
260101d7584cSLiam Girdwood 		/* clean up all links */
260201d7584cSLiam Girdwood 		list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
260301d7584cSLiam Girdwood 			dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
260401d7584cSLiam Girdwood 
260501d7584cSLiam Girdwood 		dpcm_be_disconnect(fe, stream);
260601d7584cSLiam Girdwood 		fe->dpcm[stream].runtime = NULL;
260701d7584cSLiam Girdwood 	}
260801d7584cSLiam Girdwood 
260901d7584cSLiam Girdwood 	dpcm_clear_pending_state(fe, stream);
261001d7584cSLiam Girdwood 	dpcm_path_put(&list);
261101d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
261201d7584cSLiam Girdwood 	return ret;
261301d7584cSLiam Girdwood }
261401d7584cSLiam Girdwood 
261545c0a188SMark Brown static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
261601d7584cSLiam Girdwood {
261701d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
261801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
261901d7584cSLiam Girdwood 	int stream = fe_substream->stream, ret;
262001d7584cSLiam Girdwood 
262101d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
262201d7584cSLiam Girdwood 	ret = dpcm_fe_dai_shutdown(fe_substream);
262301d7584cSLiam Girdwood 
262401d7584cSLiam Girdwood 	/* mark FE's links ready to prune */
262501d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
262601d7584cSLiam Girdwood 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
262701d7584cSLiam Girdwood 
262801d7584cSLiam Girdwood 	dpcm_be_disconnect(fe, stream);
262901d7584cSLiam Girdwood 
263001d7584cSLiam Girdwood 	fe->dpcm[stream].runtime = NULL;
263101d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
263201d7584cSLiam Girdwood 	return ret;
263301d7584cSLiam Girdwood }
263401d7584cSLiam Girdwood 
263599b04f4cSKuninori Morimoto static void soc_pcm_free(struct snd_pcm *pcm)
263699b04f4cSKuninori Morimoto {
263799b04f4cSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = pcm->private_data;
263899b04f4cSKuninori Morimoto 	struct snd_soc_component *component;
263999b04f4cSKuninori Morimoto 
264099b04f4cSKuninori Morimoto 	list_for_each_entry(component, &rtd->card->component_dev_list,
264199b04f4cSKuninori Morimoto 			    card_list) {
264299b04f4cSKuninori Morimoto 		if (component->pcm_free)
264399b04f4cSKuninori Morimoto 			component->pcm_free(pcm);
264499b04f4cSKuninori Morimoto 	}
264599b04f4cSKuninori Morimoto }
264699b04f4cSKuninori Morimoto 
2647ddee627cSLiam Girdwood /* create a new pcm */
2648ddee627cSLiam Girdwood int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2649ddee627cSLiam Girdwood {
2650ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
26512e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
2652ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
265399b04f4cSKuninori Morimoto 	struct snd_soc_component *component;
2654ddee627cSLiam Girdwood 	struct snd_pcm *pcm;
2655ddee627cSLiam Girdwood 	char new_name[64];
2656ddee627cSLiam Girdwood 	int ret = 0, playback = 0, capture = 0;
26572e5894d7SBenoit Cousson 	int i;
2658ddee627cSLiam Girdwood 
265901d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
26601e9de42fSLiam Girdwood 		playback = rtd->dai_link->dpcm_playback;
26611e9de42fSLiam Girdwood 		capture = rtd->dai_link->dpcm_capture;
266201d7584cSLiam Girdwood 	} else {
26632e5894d7SBenoit Cousson 		for (i = 0; i < rtd->num_codecs; i++) {
26642e5894d7SBenoit Cousson 			codec_dai = rtd->codec_dais[i];
26652e5894d7SBenoit Cousson 			if (codec_dai->driver->playback.channels_min)
2666ddee627cSLiam Girdwood 				playback = 1;
26672e5894d7SBenoit Cousson 			if (codec_dai->driver->capture.channels_min)
2668ddee627cSLiam Girdwood 				capture = 1;
266901d7584cSLiam Girdwood 		}
2670ddee627cSLiam Girdwood 
26712e5894d7SBenoit Cousson 		capture = capture && cpu_dai->driver->capture.channels_min;
26722e5894d7SBenoit Cousson 		playback = playback && cpu_dai->driver->playback.channels_min;
26732e5894d7SBenoit Cousson 	}
26742e5894d7SBenoit Cousson 
2675d6bead02SFabio Estevam 	if (rtd->dai_link->playback_only) {
2676d6bead02SFabio Estevam 		playback = 1;
2677d6bead02SFabio Estevam 		capture = 0;
2678d6bead02SFabio Estevam 	}
2679d6bead02SFabio Estevam 
2680d6bead02SFabio Estevam 	if (rtd->dai_link->capture_only) {
2681d6bead02SFabio Estevam 		playback = 0;
2682d6bead02SFabio Estevam 		capture = 1;
2683d6bead02SFabio Estevam 	}
2684d6bead02SFabio Estevam 
268501d7584cSLiam Girdwood 	/* create the PCM */
268601d7584cSLiam Girdwood 	if (rtd->dai_link->no_pcm) {
268701d7584cSLiam Girdwood 		snprintf(new_name, sizeof(new_name), "(%s)",
268801d7584cSLiam Girdwood 			rtd->dai_link->stream_name);
268901d7584cSLiam Girdwood 
269001d7584cSLiam Girdwood 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
269101d7584cSLiam Girdwood 				playback, capture, &pcm);
269201d7584cSLiam Girdwood 	} else {
269301d7584cSLiam Girdwood 		if (rtd->dai_link->dynamic)
269401d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s (*)",
269501d7584cSLiam Girdwood 				rtd->dai_link->stream_name);
269601d7584cSLiam Girdwood 		else
269701d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s %s-%d",
26982e5894d7SBenoit Cousson 				rtd->dai_link->stream_name,
26992e5894d7SBenoit Cousson 				(rtd->num_codecs > 1) ?
27002e5894d7SBenoit Cousson 				"multicodec" : rtd->codec_dai->name, num);
270101d7584cSLiam Girdwood 
270201d7584cSLiam Girdwood 		ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
270301d7584cSLiam Girdwood 			capture, &pcm);
270401d7584cSLiam Girdwood 	}
2705ddee627cSLiam Girdwood 	if (ret < 0) {
2706103d84a3SLiam Girdwood 		dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
27075cb9b748SLiam Girdwood 			rtd->dai_link->name);
2708ddee627cSLiam Girdwood 		return ret;
2709ddee627cSLiam Girdwood 	}
2710103d84a3SLiam Girdwood 	dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2711ddee627cSLiam Girdwood 
2712ddee627cSLiam Girdwood 	/* DAPM dai link stream work */
2713ddee627cSLiam Girdwood 	INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2714ddee627cSLiam Girdwood 
271548c7699fSVinod Koul 	pcm->nonatomic = rtd->dai_link->nonatomic;
2716ddee627cSLiam Girdwood 	rtd->pcm = pcm;
2717ddee627cSLiam Girdwood 	pcm->private_data = rtd;
271801d7584cSLiam Girdwood 
271901d7584cSLiam Girdwood 	if (rtd->dai_link->no_pcm) {
272001d7584cSLiam Girdwood 		if (playback)
272101d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
272201d7584cSLiam Girdwood 		if (capture)
272301d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
272401d7584cSLiam Girdwood 		goto out;
272501d7584cSLiam Girdwood 	}
272601d7584cSLiam Girdwood 
272701d7584cSLiam Girdwood 	/* ASoC PCM operations */
272801d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic) {
272901d7584cSLiam Girdwood 		rtd->ops.open		= dpcm_fe_dai_open;
273001d7584cSLiam Girdwood 		rtd->ops.hw_params	= dpcm_fe_dai_hw_params;
273101d7584cSLiam Girdwood 		rtd->ops.prepare	= dpcm_fe_dai_prepare;
273201d7584cSLiam Girdwood 		rtd->ops.trigger	= dpcm_fe_dai_trigger;
273301d7584cSLiam Girdwood 		rtd->ops.hw_free	= dpcm_fe_dai_hw_free;
273401d7584cSLiam Girdwood 		rtd->ops.close		= dpcm_fe_dai_close;
273501d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
2736be3f3f2cSLiam Girdwood 		rtd->ops.ioctl		= soc_pcm_ioctl;
273701d7584cSLiam Girdwood 	} else {
273801d7584cSLiam Girdwood 		rtd->ops.open		= soc_pcm_open;
273901d7584cSLiam Girdwood 		rtd->ops.hw_params	= soc_pcm_hw_params;
274001d7584cSLiam Girdwood 		rtd->ops.prepare	= soc_pcm_prepare;
274101d7584cSLiam Girdwood 		rtd->ops.trigger	= soc_pcm_trigger;
274201d7584cSLiam Girdwood 		rtd->ops.hw_free	= soc_pcm_hw_free;
274301d7584cSLiam Girdwood 		rtd->ops.close		= soc_pcm_close;
274401d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
2745be3f3f2cSLiam Girdwood 		rtd->ops.ioctl		= soc_pcm_ioctl;
274601d7584cSLiam Girdwood 	}
274701d7584cSLiam Girdwood 
2748ddee627cSLiam Girdwood 	if (platform->driver->ops) {
274901d7584cSLiam Girdwood 		rtd->ops.ack		= platform->driver->ops->ack;
275029d1a873STakashi Iwai 		rtd->ops.copy_user	= platform->driver->ops->copy_user;
275129d1a873STakashi Iwai 		rtd->ops.copy_kernel	= platform->driver->ops->copy_kernel;
275229d1a873STakashi Iwai 		rtd->ops.fill_silence	= platform->driver->ops->fill_silence;
275301d7584cSLiam Girdwood 		rtd->ops.page		= platform->driver->ops->page;
275401d7584cSLiam Girdwood 		rtd->ops.mmap		= platform->driver->ops->mmap;
2755ddee627cSLiam Girdwood 	}
2756ddee627cSLiam Girdwood 
2757ddee627cSLiam Girdwood 	if (playback)
275801d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2759ddee627cSLiam Girdwood 
2760ddee627cSLiam Girdwood 	if (capture)
276101d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2762ddee627cSLiam Girdwood 
276399b04f4cSKuninori Morimoto 	list_for_each_entry(component, &rtd->card->component_dev_list, card_list) {
276499b04f4cSKuninori Morimoto 		if (component->pcm_new) {
276599b04f4cSKuninori Morimoto 			ret = component->pcm_new(rtd);
2766ddee627cSLiam Girdwood 			if (ret < 0) {
276799b04f4cSKuninori Morimoto 				dev_err(component->dev,
2768b1bc7b3cSMark Brown 					"ASoC: pcm constructor failed: %d\n",
2769b1bc7b3cSMark Brown 					ret);
2770ddee627cSLiam Girdwood 				return ret;
2771ddee627cSLiam Girdwood 			}
2772ddee627cSLiam Girdwood 		}
277399b04f4cSKuninori Morimoto 	}
277499b04f4cSKuninori Morimoto 	pcm->private_free = soc_pcm_free;
277501d7584cSLiam Girdwood out:
27762e5894d7SBenoit Cousson 	dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
27772e5894d7SBenoit Cousson 		 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2778ddee627cSLiam Girdwood 		 cpu_dai->name);
2779ddee627cSLiam Girdwood 	return ret;
2780ddee627cSLiam Girdwood }
278101d7584cSLiam Girdwood 
278201d7584cSLiam Girdwood /* is the current PCM operation for this FE ? */
278301d7584cSLiam Girdwood int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
278401d7584cSLiam Girdwood {
278501d7584cSLiam Girdwood 	if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
278601d7584cSLiam Girdwood 		return 1;
278701d7584cSLiam Girdwood 	return 0;
278801d7584cSLiam Girdwood }
278901d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
279001d7584cSLiam Girdwood 
279101d7584cSLiam Girdwood /* is the current PCM operation for this BE ? */
279201d7584cSLiam Girdwood int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
279301d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
279401d7584cSLiam Girdwood {
279501d7584cSLiam Girdwood 	if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
279601d7584cSLiam Girdwood 	   ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
279701d7584cSLiam Girdwood 		  be->dpcm[stream].runtime_update))
279801d7584cSLiam Girdwood 		return 1;
279901d7584cSLiam Girdwood 	return 0;
280001d7584cSLiam Girdwood }
280101d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
280201d7584cSLiam Girdwood 
280301d7584cSLiam Girdwood /* get the substream for this BE */
280401d7584cSLiam Girdwood struct snd_pcm_substream *
280501d7584cSLiam Girdwood 	snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
280601d7584cSLiam Girdwood {
280701d7584cSLiam Girdwood 	return be->pcm->streams[stream].substream;
280801d7584cSLiam Girdwood }
280901d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
281001d7584cSLiam Girdwood 
281101d7584cSLiam Girdwood /* get the BE runtime state */
281201d7584cSLiam Girdwood enum snd_soc_dpcm_state
281301d7584cSLiam Girdwood 	snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
281401d7584cSLiam Girdwood {
281501d7584cSLiam Girdwood 	return be->dpcm[stream].state;
281601d7584cSLiam Girdwood }
281701d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
281801d7584cSLiam Girdwood 
281901d7584cSLiam Girdwood /* set the BE runtime state */
282001d7584cSLiam Girdwood void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
282101d7584cSLiam Girdwood 		int stream, enum snd_soc_dpcm_state state)
282201d7584cSLiam Girdwood {
282301d7584cSLiam Girdwood 	be->dpcm[stream].state = state;
282401d7584cSLiam Girdwood }
282501d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
282601d7584cSLiam Girdwood 
282701d7584cSLiam Girdwood /*
282801d7584cSLiam Girdwood  * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
282901d7584cSLiam Girdwood  * are not running, paused or suspended for the specified stream direction.
283001d7584cSLiam Girdwood  */
283101d7584cSLiam Girdwood int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
283201d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
283301d7584cSLiam Girdwood {
283401d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
283501d7584cSLiam Girdwood 	int state;
283601d7584cSLiam Girdwood 
283701d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
283801d7584cSLiam Girdwood 
283901d7584cSLiam Girdwood 		if (dpcm->fe == fe)
284001d7584cSLiam Girdwood 			continue;
284101d7584cSLiam Girdwood 
284201d7584cSLiam Girdwood 		state = dpcm->fe->dpcm[stream].state;
284301d7584cSLiam Girdwood 		if (state == SND_SOC_DPCM_STATE_START ||
284401d7584cSLiam Girdwood 			state == SND_SOC_DPCM_STATE_PAUSED ||
284501d7584cSLiam Girdwood 			state == SND_SOC_DPCM_STATE_SUSPEND)
284601d7584cSLiam Girdwood 			return 0;
284701d7584cSLiam Girdwood 	}
284801d7584cSLiam Girdwood 
284901d7584cSLiam Girdwood 	/* it's safe to free/stop this BE DAI */
285001d7584cSLiam Girdwood 	return 1;
285101d7584cSLiam Girdwood }
285201d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
285301d7584cSLiam Girdwood 
285401d7584cSLiam Girdwood /*
285501d7584cSLiam Girdwood  * We can only change hw params a BE DAI if any of it's FE are not prepared,
285601d7584cSLiam Girdwood  * running, paused or suspended for the specified stream direction.
285701d7584cSLiam Girdwood  */
285801d7584cSLiam Girdwood int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
285901d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
286001d7584cSLiam Girdwood {
286101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
286201d7584cSLiam Girdwood 	int state;
286301d7584cSLiam Girdwood 
286401d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
286501d7584cSLiam Girdwood 
286601d7584cSLiam Girdwood 		if (dpcm->fe == fe)
286701d7584cSLiam Girdwood 			continue;
286801d7584cSLiam Girdwood 
286901d7584cSLiam Girdwood 		state = dpcm->fe->dpcm[stream].state;
287001d7584cSLiam Girdwood 		if (state == SND_SOC_DPCM_STATE_START ||
287101d7584cSLiam Girdwood 			state == SND_SOC_DPCM_STATE_PAUSED ||
287201d7584cSLiam Girdwood 			state == SND_SOC_DPCM_STATE_SUSPEND ||
287301d7584cSLiam Girdwood 			state == SND_SOC_DPCM_STATE_PREPARE)
287401d7584cSLiam Girdwood 			return 0;
287501d7584cSLiam Girdwood 	}
287601d7584cSLiam Girdwood 
287701d7584cSLiam Girdwood 	/* it's safe to change hw_params */
287801d7584cSLiam Girdwood 	return 1;
287901d7584cSLiam Girdwood }
288001d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2881f86dcef8SLiam Girdwood 
2882f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS
288385280141SLars-Peter Clausen static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
2884f86dcef8SLiam Girdwood {
2885f86dcef8SLiam Girdwood 	switch (state) {
2886f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_NEW:
2887f86dcef8SLiam Girdwood 		return "new";
2888f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_OPEN:
2889f86dcef8SLiam Girdwood 		return "open";
2890f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_HW_PARAMS:
2891f86dcef8SLiam Girdwood 		return "hw_params";
2892f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_PREPARE:
2893f86dcef8SLiam Girdwood 		return "prepare";
2894f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_START:
2895f86dcef8SLiam Girdwood 		return "start";
2896f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_STOP:
2897f86dcef8SLiam Girdwood 		return "stop";
2898f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_SUSPEND:
2899f86dcef8SLiam Girdwood 		return "suspend";
2900f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_PAUSED:
2901f86dcef8SLiam Girdwood 		return "paused";
2902f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_HW_FREE:
2903f86dcef8SLiam Girdwood 		return "hw_free";
2904f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_CLOSE:
2905f86dcef8SLiam Girdwood 		return "close";
2906f86dcef8SLiam Girdwood 	}
2907f86dcef8SLiam Girdwood 
2908f86dcef8SLiam Girdwood 	return "unknown";
2909f86dcef8SLiam Girdwood }
2910f86dcef8SLiam Girdwood 
2911f86dcef8SLiam Girdwood static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2912f86dcef8SLiam Girdwood 				int stream, char *buf, size_t size)
2913f86dcef8SLiam Girdwood {
2914f86dcef8SLiam Girdwood 	struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2915f86dcef8SLiam Girdwood 	struct snd_soc_dpcm *dpcm;
2916f86dcef8SLiam Girdwood 	ssize_t offset = 0;
2917f86dcef8SLiam Girdwood 
2918f86dcef8SLiam Girdwood 	/* FE state */
2919f86dcef8SLiam Girdwood 	offset += snprintf(buf + offset, size - offset,
2920f86dcef8SLiam Girdwood 			"[%s - %s]\n", fe->dai_link->name,
2921f86dcef8SLiam Girdwood 			stream ? "Capture" : "Playback");
2922f86dcef8SLiam Girdwood 
2923f86dcef8SLiam Girdwood 	offset += snprintf(buf + offset, size - offset, "State: %s\n",
2924f86dcef8SLiam Girdwood 	                dpcm_state_string(fe->dpcm[stream].state));
2925f86dcef8SLiam Girdwood 
2926f86dcef8SLiam Girdwood 	if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2927f86dcef8SLiam Girdwood 	    (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2928f86dcef8SLiam Girdwood 		offset += snprintf(buf + offset, size - offset,
2929f86dcef8SLiam Girdwood 				"Hardware Params: "
2930f86dcef8SLiam Girdwood 				"Format = %s, Channels = %d, Rate = %d\n",
2931f86dcef8SLiam Girdwood 				snd_pcm_format_name(params_format(params)),
2932f86dcef8SLiam Girdwood 				params_channels(params),
2933f86dcef8SLiam Girdwood 				params_rate(params));
2934f86dcef8SLiam Girdwood 
2935f86dcef8SLiam Girdwood 	/* BEs state */
2936f86dcef8SLiam Girdwood 	offset += snprintf(buf + offset, size - offset, "Backends:\n");
2937f86dcef8SLiam Girdwood 
2938f86dcef8SLiam Girdwood 	if (list_empty(&fe->dpcm[stream].be_clients)) {
2939f86dcef8SLiam Girdwood 		offset += snprintf(buf + offset, size - offset,
2940f86dcef8SLiam Girdwood 				" No active DSP links\n");
2941f86dcef8SLiam Girdwood 		goto out;
2942f86dcef8SLiam Girdwood 	}
2943f86dcef8SLiam Girdwood 
2944f86dcef8SLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2945f86dcef8SLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
2946f86dcef8SLiam Girdwood 		params = &dpcm->hw_params;
2947f86dcef8SLiam Girdwood 
2948f86dcef8SLiam Girdwood 		offset += snprintf(buf + offset, size - offset,
2949f86dcef8SLiam Girdwood 				"- %s\n", be->dai_link->name);
2950f86dcef8SLiam Girdwood 
2951f86dcef8SLiam Girdwood 		offset += snprintf(buf + offset, size - offset,
2952f86dcef8SLiam Girdwood 				"   State: %s\n",
2953f86dcef8SLiam Girdwood 				dpcm_state_string(be->dpcm[stream].state));
2954f86dcef8SLiam Girdwood 
2955f86dcef8SLiam Girdwood 		if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2956f86dcef8SLiam Girdwood 		    (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2957f86dcef8SLiam Girdwood 			offset += snprintf(buf + offset, size - offset,
2958f86dcef8SLiam Girdwood 				"   Hardware Params: "
2959f86dcef8SLiam Girdwood 				"Format = %s, Channels = %d, Rate = %d\n",
2960f86dcef8SLiam Girdwood 				snd_pcm_format_name(params_format(params)),
2961f86dcef8SLiam Girdwood 				params_channels(params),
2962f86dcef8SLiam Girdwood 				params_rate(params));
2963f86dcef8SLiam Girdwood 	}
2964f86dcef8SLiam Girdwood 
2965f86dcef8SLiam Girdwood out:
2966f86dcef8SLiam Girdwood 	return offset;
2967f86dcef8SLiam Girdwood }
2968f86dcef8SLiam Girdwood 
2969f86dcef8SLiam Girdwood static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2970f86dcef8SLiam Girdwood 				size_t count, loff_t *ppos)
2971f86dcef8SLiam Girdwood {
2972f86dcef8SLiam Girdwood 	struct snd_soc_pcm_runtime *fe = file->private_data;
2973f86dcef8SLiam Girdwood 	ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2974f86dcef8SLiam Girdwood 	char *buf;
2975f86dcef8SLiam Girdwood 
2976f86dcef8SLiam Girdwood 	buf = kmalloc(out_count, GFP_KERNEL);
2977f86dcef8SLiam Girdwood 	if (!buf)
2978f86dcef8SLiam Girdwood 		return -ENOMEM;
2979f86dcef8SLiam Girdwood 
2980f86dcef8SLiam Girdwood 	if (fe->cpu_dai->driver->playback.channels_min)
2981f86dcef8SLiam Girdwood 		offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2982f86dcef8SLiam Girdwood 					buf + offset, out_count - offset);
2983f86dcef8SLiam Girdwood 
2984f86dcef8SLiam Girdwood 	if (fe->cpu_dai->driver->capture.channels_min)
2985f86dcef8SLiam Girdwood 		offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2986f86dcef8SLiam Girdwood 					buf + offset, out_count - offset);
2987f86dcef8SLiam Girdwood 
2988f86dcef8SLiam Girdwood 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2989f86dcef8SLiam Girdwood 
2990f86dcef8SLiam Girdwood 	kfree(buf);
2991f86dcef8SLiam Girdwood 	return ret;
2992f86dcef8SLiam Girdwood }
2993f86dcef8SLiam Girdwood 
2994f86dcef8SLiam Girdwood static const struct file_operations dpcm_state_fops = {
2995f57b8488SLiam Girdwood 	.open = simple_open,
2996f86dcef8SLiam Girdwood 	.read = dpcm_state_read_file,
2997f86dcef8SLiam Girdwood 	.llseek = default_llseek,
2998f86dcef8SLiam Girdwood };
2999f86dcef8SLiam Girdwood 
30002e55b90aSLars-Peter Clausen void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3001f86dcef8SLiam Girdwood {
3002b3bba9a1SMark Brown 	if (!rtd->dai_link)
30032e55b90aSLars-Peter Clausen 		return;
3004b3bba9a1SMark Brown 
30056553bf06SLars-Peter Clausen 	if (!rtd->card->debugfs_card_root)
30066553bf06SLars-Peter Clausen 		return;
3007f86dcef8SLiam Girdwood 
3008f86dcef8SLiam Girdwood 	rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3009f86dcef8SLiam Girdwood 			rtd->card->debugfs_card_root);
3010f86dcef8SLiam Girdwood 	if (!rtd->debugfs_dpcm_root) {
3011f86dcef8SLiam Girdwood 		dev_dbg(rtd->dev,
3012f86dcef8SLiam Girdwood 			 "ASoC: Failed to create dpcm debugfs directory %s\n",
3013f86dcef8SLiam Girdwood 			 rtd->dai_link->name);
30142e55b90aSLars-Peter Clausen 		return;
3015f86dcef8SLiam Girdwood 	}
3016f86dcef8SLiam Girdwood 
3017f57b8488SLiam Girdwood 	rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
3018f86dcef8SLiam Girdwood 						rtd->debugfs_dpcm_root,
3019f86dcef8SLiam Girdwood 						rtd, &dpcm_state_fops);
3020f86dcef8SLiam Girdwood }
3021f86dcef8SLiam Girdwood #endif
3022