xref: /openbmc/linux/sound/soc/soc-pcm.c (revision ae11601b80b984859deda1f2d430a23bfabd3bea)
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 
3790996f43SLars-Peter Clausen /**
3824894b76SLars-Peter Clausen  * snd_soc_runtime_activate() - Increment active count for PCM runtime components
3924894b76SLars-Peter Clausen  * @rtd: ASoC PCM runtime that is activated
4024894b76SLars-Peter Clausen  * @stream: Direction of the PCM stream
4124894b76SLars-Peter Clausen  *
4224894b76SLars-Peter Clausen  * Increments the active count for all the DAIs and components attached to a PCM
4324894b76SLars-Peter Clausen  * runtime. Should typically be called when a stream is opened.
4424894b76SLars-Peter Clausen  *
4524894b76SLars-Peter Clausen  * Must be called with the rtd->pcm_mutex being held
4624894b76SLars-Peter Clausen  */
4724894b76SLars-Peter Clausen void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
4824894b76SLars-Peter Clausen {
4924894b76SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
502e5894d7SBenoit Cousson 	int i;
5124894b76SLars-Peter Clausen 
5224894b76SLars-Peter Clausen 	lockdep_assert_held(&rtd->pcm_mutex);
5324894b76SLars-Peter Clausen 
5424894b76SLars-Peter Clausen 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
5524894b76SLars-Peter Clausen 		cpu_dai->playback_active++;
562e5894d7SBenoit Cousson 		for (i = 0; i < rtd->num_codecs; i++)
572e5894d7SBenoit Cousson 			rtd->codec_dais[i]->playback_active++;
5824894b76SLars-Peter Clausen 	} else {
5924894b76SLars-Peter Clausen 		cpu_dai->capture_active++;
602e5894d7SBenoit Cousson 		for (i = 0; i < rtd->num_codecs; i++)
612e5894d7SBenoit Cousson 			rtd->codec_dais[i]->capture_active++;
6224894b76SLars-Peter Clausen 	}
6324894b76SLars-Peter Clausen 
6424894b76SLars-Peter Clausen 	cpu_dai->active++;
65cdde4ccbSLars-Peter Clausen 	cpu_dai->component->active++;
662e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
672e5894d7SBenoit Cousson 		rtd->codec_dais[i]->active++;
682e5894d7SBenoit Cousson 		rtd->codec_dais[i]->component->active++;
692e5894d7SBenoit Cousson 	}
7024894b76SLars-Peter Clausen }
7124894b76SLars-Peter Clausen 
7224894b76SLars-Peter Clausen /**
7324894b76SLars-Peter Clausen  * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
7424894b76SLars-Peter Clausen  * @rtd: ASoC PCM runtime that is deactivated
7524894b76SLars-Peter Clausen  * @stream: Direction of the PCM stream
7624894b76SLars-Peter Clausen  *
7724894b76SLars-Peter Clausen  * Decrements the active count for all the DAIs and components attached to a PCM
7824894b76SLars-Peter Clausen  * runtime. Should typically be called when a stream is closed.
7924894b76SLars-Peter Clausen  *
8024894b76SLars-Peter Clausen  * Must be called with the rtd->pcm_mutex being held
8124894b76SLars-Peter Clausen  */
8224894b76SLars-Peter Clausen void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
8324894b76SLars-Peter Clausen {
8424894b76SLars-Peter Clausen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
852e5894d7SBenoit Cousson 	int i;
8624894b76SLars-Peter Clausen 
8724894b76SLars-Peter Clausen 	lockdep_assert_held(&rtd->pcm_mutex);
8824894b76SLars-Peter Clausen 
8924894b76SLars-Peter Clausen 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
9024894b76SLars-Peter Clausen 		cpu_dai->playback_active--;
912e5894d7SBenoit Cousson 		for (i = 0; i < rtd->num_codecs; i++)
922e5894d7SBenoit Cousson 			rtd->codec_dais[i]->playback_active--;
9324894b76SLars-Peter Clausen 	} else {
9424894b76SLars-Peter Clausen 		cpu_dai->capture_active--;
952e5894d7SBenoit Cousson 		for (i = 0; i < rtd->num_codecs; i++)
962e5894d7SBenoit Cousson 			rtd->codec_dais[i]->capture_active--;
9724894b76SLars-Peter Clausen 	}
9824894b76SLars-Peter Clausen 
9924894b76SLars-Peter Clausen 	cpu_dai->active--;
100cdde4ccbSLars-Peter Clausen 	cpu_dai->component->active--;
1012e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
1022e5894d7SBenoit Cousson 		rtd->codec_dais[i]->component->active--;
1032e5894d7SBenoit Cousson 		rtd->codec_dais[i]->active--;
1042e5894d7SBenoit Cousson 	}
10524894b76SLars-Peter Clausen }
10624894b76SLars-Peter Clausen 
10724894b76SLars-Peter Clausen /**
108208a1589SLars-Peter Clausen  * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
109208a1589SLars-Peter Clausen  * @rtd: The ASoC PCM runtime that should be checked.
110208a1589SLars-Peter Clausen  *
111208a1589SLars-Peter Clausen  * This function checks whether the power down delay should be ignored for a
112208a1589SLars-Peter Clausen  * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
113208a1589SLars-Peter Clausen  * been configured to ignore the delay, or if none of the components benefits
114208a1589SLars-Peter Clausen  * from having the delay.
115208a1589SLars-Peter Clausen  */
116208a1589SLars-Peter Clausen bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
117208a1589SLars-Peter Clausen {
1182e5894d7SBenoit Cousson 	int i;
1192e5894d7SBenoit Cousson 	bool ignore = true;
1202e5894d7SBenoit Cousson 
121208a1589SLars-Peter Clausen 	if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
122208a1589SLars-Peter Clausen 		return true;
123208a1589SLars-Peter Clausen 
1242e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
1252e5894d7SBenoit Cousson 		ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
1262e5894d7SBenoit Cousson 
1272e5894d7SBenoit Cousson 	return rtd->cpu_dai->component->ignore_pmdown_time && ignore;
128208a1589SLars-Peter Clausen }
129208a1589SLars-Peter Clausen 
130208a1589SLars-Peter Clausen /**
13190996f43SLars-Peter Clausen  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
13290996f43SLars-Peter Clausen  * @substream: the pcm substream
13390996f43SLars-Peter Clausen  * @hw: the hardware parameters
13490996f43SLars-Peter Clausen  *
13590996f43SLars-Peter Clausen  * Sets the substream runtime hardware parameters.
13690996f43SLars-Peter Clausen  */
13790996f43SLars-Peter Clausen int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
13890996f43SLars-Peter Clausen 	const struct snd_pcm_hardware *hw)
13990996f43SLars-Peter Clausen {
14090996f43SLars-Peter Clausen 	struct snd_pcm_runtime *runtime = substream->runtime;
14190996f43SLars-Peter Clausen 	runtime->hw.info = hw->info;
14290996f43SLars-Peter Clausen 	runtime->hw.formats = hw->formats;
14390996f43SLars-Peter Clausen 	runtime->hw.period_bytes_min = hw->period_bytes_min;
14490996f43SLars-Peter Clausen 	runtime->hw.period_bytes_max = hw->period_bytes_max;
14590996f43SLars-Peter Clausen 	runtime->hw.periods_min = hw->periods_min;
14690996f43SLars-Peter Clausen 	runtime->hw.periods_max = hw->periods_max;
14790996f43SLars-Peter Clausen 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
14890996f43SLars-Peter Clausen 	runtime->hw.fifo_size = hw->fifo_size;
14990996f43SLars-Peter Clausen 	return 0;
15090996f43SLars-Peter Clausen }
15190996f43SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
15290996f43SLars-Peter Clausen 
15301d7584cSLiam Girdwood /* DPCM stream event, send event to FE and all active BEs. */
15423607025SLiam Girdwood int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
15501d7584cSLiam Girdwood 	int event)
15601d7584cSLiam Girdwood {
15701d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
15801d7584cSLiam Girdwood 
15901d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
16001d7584cSLiam Girdwood 
16101d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
16201d7584cSLiam Girdwood 
163103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
16401d7584cSLiam Girdwood 				be->dai_link->name, event, dir);
16501d7584cSLiam Girdwood 
16601d7584cSLiam Girdwood 		snd_soc_dapm_stream_event(be, dir, event);
16701d7584cSLiam Girdwood 	}
16801d7584cSLiam Girdwood 
16901d7584cSLiam Girdwood 	snd_soc_dapm_stream_event(fe, dir, event);
17001d7584cSLiam Girdwood 
17101d7584cSLiam Girdwood 	return 0;
17201d7584cSLiam Girdwood }
17301d7584cSLiam Girdwood 
17417841020SDong Aisheng static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
17517841020SDong Aisheng 					struct snd_soc_dai *soc_dai)
176ddee627cSLiam Girdwood {
177ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
178ddee627cSLiam Girdwood 	int ret;
179ddee627cSLiam Girdwood 
1803635bf09SNicolin Chen 	if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
1813635bf09SNicolin Chen 				rtd->dai_link->symmetric_rates)) {
1823635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
1833635bf09SNicolin Chen 				soc_dai->rate);
184ddee627cSLiam Girdwood 
185ddee627cSLiam Girdwood 		ret = snd_pcm_hw_constraint_minmax(substream->runtime,
186ddee627cSLiam Girdwood 						SNDRV_PCM_HW_PARAM_RATE,
18717841020SDong Aisheng 						soc_dai->rate, soc_dai->rate);
188ddee627cSLiam Girdwood 		if (ret < 0) {
18917841020SDong Aisheng 			dev_err(soc_dai->dev,
1903635bf09SNicolin Chen 				"ASoC: Unable to apply rate constraint: %d\n",
191103d84a3SLiam Girdwood 				ret);
192ddee627cSLiam Girdwood 			return ret;
193ddee627cSLiam Girdwood 		}
1943635bf09SNicolin Chen 	}
1953635bf09SNicolin Chen 
1963635bf09SNicolin Chen 	if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
1973635bf09SNicolin Chen 				rtd->dai_link->symmetric_channels)) {
1983635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
1993635bf09SNicolin Chen 				soc_dai->channels);
2003635bf09SNicolin Chen 
2013635bf09SNicolin Chen 		ret = snd_pcm_hw_constraint_minmax(substream->runtime,
2023635bf09SNicolin Chen 						SNDRV_PCM_HW_PARAM_CHANNELS,
2033635bf09SNicolin Chen 						soc_dai->channels,
2043635bf09SNicolin Chen 						soc_dai->channels);
2053635bf09SNicolin Chen 		if (ret < 0) {
2063635bf09SNicolin Chen 			dev_err(soc_dai->dev,
2073635bf09SNicolin Chen 				"ASoC: Unable to apply channel symmetry constraint: %d\n",
2083635bf09SNicolin Chen 				ret);
2093635bf09SNicolin Chen 			return ret;
2103635bf09SNicolin Chen 		}
2113635bf09SNicolin Chen 	}
2123635bf09SNicolin Chen 
2133635bf09SNicolin Chen 	if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
2143635bf09SNicolin Chen 				rtd->dai_link->symmetric_samplebits)) {
2153635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
2163635bf09SNicolin Chen 				soc_dai->sample_bits);
2173635bf09SNicolin Chen 
2183635bf09SNicolin Chen 		ret = snd_pcm_hw_constraint_minmax(substream->runtime,
2193635bf09SNicolin Chen 						SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2203635bf09SNicolin Chen 						soc_dai->sample_bits,
2213635bf09SNicolin Chen 						soc_dai->sample_bits);
2223635bf09SNicolin Chen 		if (ret < 0) {
2233635bf09SNicolin Chen 			dev_err(soc_dai->dev,
2243635bf09SNicolin Chen 				"ASoC: Unable to apply sample bits symmetry constraint: %d\n",
2253635bf09SNicolin Chen 				ret);
2263635bf09SNicolin Chen 			return ret;
2273635bf09SNicolin Chen 		}
2283635bf09SNicolin Chen 	}
229ddee627cSLiam Girdwood 
230ddee627cSLiam Girdwood 	return 0;
231ddee627cSLiam Girdwood }
232ddee627cSLiam Girdwood 
2333635bf09SNicolin Chen static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
2343635bf09SNicolin Chen 				struct snd_pcm_hw_params *params)
2353635bf09SNicolin Chen {
2363635bf09SNicolin Chen 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
2373635bf09SNicolin Chen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2382e5894d7SBenoit Cousson 	unsigned int rate, channels, sample_bits, symmetry, i;
2393635bf09SNicolin Chen 
2403635bf09SNicolin Chen 	rate = params_rate(params);
2413635bf09SNicolin Chen 	channels = params_channels(params);
2423635bf09SNicolin Chen 	sample_bits = snd_pcm_format_physical_width(params_format(params));
2433635bf09SNicolin Chen 
2443635bf09SNicolin Chen 	/* reject unmatched parameters when applying symmetry */
2453635bf09SNicolin Chen 	symmetry = cpu_dai->driver->symmetric_rates ||
2463635bf09SNicolin Chen 		rtd->dai_link->symmetric_rates;
2472e5894d7SBenoit Cousson 
2482e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
2492e5894d7SBenoit Cousson 		symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
2502e5894d7SBenoit Cousson 
2513635bf09SNicolin Chen 	if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
2523635bf09SNicolin Chen 		dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
2533635bf09SNicolin Chen 				cpu_dai->rate, rate);
2543635bf09SNicolin Chen 		return -EINVAL;
2553635bf09SNicolin Chen 	}
2563635bf09SNicolin Chen 
2573635bf09SNicolin Chen 	symmetry = cpu_dai->driver->symmetric_channels ||
2583635bf09SNicolin Chen 		rtd->dai_link->symmetric_channels;
2592e5894d7SBenoit Cousson 
2602e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
2612e5894d7SBenoit Cousson 		symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
2622e5894d7SBenoit Cousson 
2633635bf09SNicolin Chen 	if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
2643635bf09SNicolin Chen 		dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
2653635bf09SNicolin Chen 				cpu_dai->channels, channels);
2663635bf09SNicolin Chen 		return -EINVAL;
2673635bf09SNicolin Chen 	}
2683635bf09SNicolin Chen 
2693635bf09SNicolin Chen 	symmetry = cpu_dai->driver->symmetric_samplebits ||
2703635bf09SNicolin Chen 		rtd->dai_link->symmetric_samplebits;
2712e5894d7SBenoit Cousson 
2722e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
2732e5894d7SBenoit Cousson 		symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
2742e5894d7SBenoit Cousson 
2753635bf09SNicolin Chen 	if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
2763635bf09SNicolin Chen 		dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
2773635bf09SNicolin Chen 				cpu_dai->sample_bits, sample_bits);
2783635bf09SNicolin Chen 		return -EINVAL;
2793635bf09SNicolin Chen 	}
280ddee627cSLiam Girdwood 
281ddee627cSLiam Girdwood 	return 0;
282ddee627cSLiam Girdwood }
283ddee627cSLiam Girdwood 
28462e5f676SLars-Peter Clausen static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
28562e5f676SLars-Peter Clausen {
28662e5f676SLars-Peter Clausen 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
28762e5f676SLars-Peter Clausen 	struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
28862e5f676SLars-Peter Clausen 	struct snd_soc_dai_link *link = rtd->dai_link;
2892e5894d7SBenoit Cousson 	unsigned int symmetry, i;
29062e5f676SLars-Peter Clausen 
2912e5894d7SBenoit Cousson 	symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
2922e5894d7SBenoit Cousson 		cpu_driver->symmetric_channels || link->symmetric_channels ||
2932e5894d7SBenoit Cousson 		cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
2942e5894d7SBenoit Cousson 
2952e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
2962e5894d7SBenoit Cousson 		symmetry = symmetry ||
2972e5894d7SBenoit Cousson 			rtd->codec_dais[i]->driver->symmetric_rates ||
2982e5894d7SBenoit Cousson 			rtd->codec_dais[i]->driver->symmetric_channels ||
2992e5894d7SBenoit Cousson 			rtd->codec_dais[i]->driver->symmetric_samplebits;
3002e5894d7SBenoit Cousson 
3012e5894d7SBenoit Cousson 	return symmetry;
30262e5f676SLars-Peter Clausen }
30362e5f676SLars-Peter Clausen 
304ddee627cSLiam Girdwood /*
30558ba9b25SMark Brown  * List of sample sizes that might go over the bus for parameter
30658ba9b25SMark Brown  * application.  There ought to be a wildcard sample size for things
30758ba9b25SMark Brown  * like the DAC/ADC resolution to use but there isn't right now.
30858ba9b25SMark Brown  */
30958ba9b25SMark Brown static int sample_sizes[] = {
31088e33954SPeter Ujfalusi 	24, 32,
31158ba9b25SMark Brown };
31258ba9b25SMark Brown 
3132e5894d7SBenoit Cousson static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
31458ba9b25SMark Brown {
3152e5894d7SBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
316c8dd1fecSBenoit Cousson 	int ret, i;
31758ba9b25SMark Brown 
31858ba9b25SMark Brown 	if (!bits)
31958ba9b25SMark Brown 		return;
32058ba9b25SMark Brown 
32158ba9b25SMark Brown 	for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
322278047fdSMark Brown 		if (bits >= sample_sizes[i])
323278047fdSMark Brown 			continue;
324278047fdSMark Brown 
325278047fdSMark Brown 		ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
326278047fdSMark Brown 						   sample_sizes[i], bits);
32758ba9b25SMark Brown 		if (ret != 0)
3282e5894d7SBenoit Cousson 			dev_warn(rtd->dev,
329103d84a3SLiam Girdwood 				 "ASoC: Failed to set MSB %d/%d: %d\n",
33058ba9b25SMark Brown 				 bits, sample_sizes[i], ret);
33158ba9b25SMark Brown 	}
33258ba9b25SMark Brown }
33358ba9b25SMark Brown 
334c8dd1fecSBenoit Cousson static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
335bd477c31SLars-Peter Clausen {
336c8dd1fecSBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
337c8dd1fecSBenoit Cousson 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3382e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
3392e5894d7SBenoit Cousson 	int i;
340c8dd1fecSBenoit Cousson 	unsigned int bits = 0, cpu_bits;
341c8dd1fecSBenoit Cousson 
342c8dd1fecSBenoit Cousson 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3432e5894d7SBenoit Cousson 		for (i = 0; i < rtd->num_codecs; i++) {
3442e5894d7SBenoit Cousson 			codec_dai = rtd->codec_dais[i];
3452e5894d7SBenoit Cousson 			if (codec_dai->driver->playback.sig_bits == 0) {
3462e5894d7SBenoit Cousson 				bits = 0;
3472e5894d7SBenoit Cousson 				break;
3482e5894d7SBenoit Cousson 			}
3492e5894d7SBenoit Cousson 			bits = max(codec_dai->driver->playback.sig_bits, bits);
3502e5894d7SBenoit Cousson 		}
351c8dd1fecSBenoit Cousson 		cpu_bits = cpu_dai->driver->playback.sig_bits;
352c8dd1fecSBenoit Cousson 	} else {
3532e5894d7SBenoit Cousson 		for (i = 0; i < rtd->num_codecs; i++) {
3542e5894d7SBenoit Cousson 			codec_dai = rtd->codec_dais[i];
3555e63dfccSDaniel Mack 			if (codec_dai->driver->capture.sig_bits == 0) {
3562e5894d7SBenoit Cousson 				bits = 0;
3572e5894d7SBenoit Cousson 				break;
3582e5894d7SBenoit Cousson 			}
3592e5894d7SBenoit Cousson 			bits = max(codec_dai->driver->capture.sig_bits, bits);
3602e5894d7SBenoit Cousson 		}
361c8dd1fecSBenoit Cousson 		cpu_bits = cpu_dai->driver->capture.sig_bits;
362c8dd1fecSBenoit Cousson 	}
363c8dd1fecSBenoit Cousson 
3642e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, bits);
3652e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, cpu_bits);
366c8dd1fecSBenoit Cousson }
367c8dd1fecSBenoit Cousson 
3682e5894d7SBenoit Cousson static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
369bd477c31SLars-Peter Clausen {
3702e5894d7SBenoit Cousson 	struct snd_pcm_runtime *runtime = substream->runtime;
37178e45c99SLars-Peter Clausen 	struct snd_pcm_hardware *hw = &runtime->hw;
3722e5894d7SBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
3732e5894d7SBenoit Cousson 	struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
3742e5894d7SBenoit Cousson 	struct snd_soc_dai_driver *codec_dai_drv;
3752e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *codec_stream;
3762e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *cpu_stream;
3772e5894d7SBenoit Cousson 	unsigned int chan_min = 0, chan_max = UINT_MAX;
3782e5894d7SBenoit Cousson 	unsigned int rate_min = 0, rate_max = UINT_MAX;
3792e5894d7SBenoit Cousson 	unsigned int rates = UINT_MAX;
3802e5894d7SBenoit Cousson 	u64 formats = ULLONG_MAX;
3812e5894d7SBenoit Cousson 	int i;
38278e45c99SLars-Peter Clausen 
3832e5894d7SBenoit Cousson 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3842e5894d7SBenoit Cousson 		cpu_stream = &cpu_dai_drv->playback;
38516d7ea91SLars-Peter Clausen 	else
3862e5894d7SBenoit Cousson 		cpu_stream = &cpu_dai_drv->capture;
38778e45c99SLars-Peter Clausen 
3882e5894d7SBenoit Cousson 	/* first calculate min/max only for CODECs in the DAI link */
3892e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
3902e5894d7SBenoit Cousson 		codec_dai_drv = rtd->codec_dais[i]->driver;
3912e5894d7SBenoit Cousson 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3922e5894d7SBenoit Cousson 			codec_stream = &codec_dai_drv->playback;
3932e5894d7SBenoit Cousson 		else
3942e5894d7SBenoit Cousson 			codec_stream = &codec_dai_drv->capture;
3952e5894d7SBenoit Cousson 		chan_min = max(chan_min, codec_stream->channels_min);
3962e5894d7SBenoit Cousson 		chan_max = min(chan_max, codec_stream->channels_max);
3972e5894d7SBenoit Cousson 		rate_min = max(rate_min, codec_stream->rate_min);
3982e5894d7SBenoit Cousson 		rate_max = min_not_zero(rate_max, codec_stream->rate_max);
3992e5894d7SBenoit Cousson 		formats &= codec_stream->formats;
4002e5894d7SBenoit Cousson 		rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
4012e5894d7SBenoit Cousson 	}
4022e5894d7SBenoit Cousson 
4032e5894d7SBenoit Cousson 	/*
4042e5894d7SBenoit Cousson 	 * chan min/max cannot be enforced if there are multiple CODEC DAIs
4052e5894d7SBenoit Cousson 	 * connected to a single CPU DAI, use CPU DAI's directly and let
4062e5894d7SBenoit Cousson 	 * channel allocation be fixed up later
4072e5894d7SBenoit Cousson 	 */
4082e5894d7SBenoit Cousson 	if (rtd->num_codecs > 1) {
4092e5894d7SBenoit Cousson 		chan_min = cpu_stream->channels_min;
4102e5894d7SBenoit Cousson 		chan_max = cpu_stream->channels_max;
4112e5894d7SBenoit Cousson 	}
4122e5894d7SBenoit Cousson 
4132e5894d7SBenoit Cousson 	hw->channels_min = max(chan_min, cpu_stream->channels_min);
4142e5894d7SBenoit Cousson 	hw->channels_max = min(chan_max, cpu_stream->channels_max);
4152e5894d7SBenoit Cousson 	if (hw->formats)
4162e5894d7SBenoit Cousson 		hw->formats &= formats & cpu_stream->formats;
4172e5894d7SBenoit Cousson 	else
4182e5894d7SBenoit Cousson 		hw->formats = formats & cpu_stream->formats;
4192e5894d7SBenoit Cousson 	hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
42078e45c99SLars-Peter Clausen 
42178e45c99SLars-Peter Clausen 	snd_pcm_limit_hw_rates(runtime);
42278e45c99SLars-Peter Clausen 
42378e45c99SLars-Peter Clausen 	hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
4242e5894d7SBenoit Cousson 	hw->rate_min = max(hw->rate_min, rate_min);
42578e45c99SLars-Peter Clausen 	hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
4262e5894d7SBenoit Cousson 	hw->rate_max = min_not_zero(hw->rate_max, rate_max);
427bd477c31SLars-Peter Clausen }
428bd477c31SLars-Peter Clausen 
42958ba9b25SMark Brown /*
430ddee627cSLiam Girdwood  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
431ddee627cSLiam Girdwood  * then initialized and any private data can be allocated. This also calls
432ddee627cSLiam Girdwood  * startup for the cpu DAI, platform, machine and codec DAI.
433ddee627cSLiam Girdwood  */
434ddee627cSLiam Girdwood static int soc_pcm_open(struct snd_pcm_substream *substream)
435ddee627cSLiam Girdwood {
436ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
437ddee627cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
438ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
439ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
4402e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
4412e5894d7SBenoit Cousson 	const char *codec_dai_name = "multicodec";
4422e5894d7SBenoit Cousson 	int i, ret = 0;
443ddee627cSLiam Girdwood 
444988e8cc4SNicolin Chen 	pinctrl_pm_select_default_state(cpu_dai->dev);
4452e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
4462e5894d7SBenoit Cousson 		pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
447d6652ef8SMark Brown 	pm_runtime_get_sync(cpu_dai->dev);
4482e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
4492e5894d7SBenoit Cousson 		pm_runtime_get_sync(rtd->codec_dais[i]->dev);
450d6652ef8SMark Brown 	pm_runtime_get_sync(platform->dev);
451d6652ef8SMark Brown 
452b8c0dab9SLiam Girdwood 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
453ddee627cSLiam Girdwood 
454ddee627cSLiam Girdwood 	/* startup the audio subsystem */
455c5914b0aSMark Brown 	if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
456ddee627cSLiam Girdwood 		ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
457ddee627cSLiam Girdwood 		if (ret < 0) {
458103d84a3SLiam Girdwood 			dev_err(cpu_dai->dev, "ASoC: can't open interface"
459103d84a3SLiam Girdwood 				" %s: %d\n", cpu_dai->name, ret);
460ddee627cSLiam Girdwood 			goto out;
461ddee627cSLiam Girdwood 		}
462ddee627cSLiam Girdwood 	}
463ddee627cSLiam Girdwood 
464ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->open) {
465ddee627cSLiam Girdwood 		ret = platform->driver->ops->open(substream);
466ddee627cSLiam Girdwood 		if (ret < 0) {
467103d84a3SLiam Girdwood 			dev_err(platform->dev, "ASoC: can't open platform"
468f4333203SLars-Peter Clausen 				" %s: %d\n", platform->component.name, ret);
469ddee627cSLiam Girdwood 			goto platform_err;
470ddee627cSLiam Girdwood 		}
471ddee627cSLiam Girdwood 	}
472ddee627cSLiam Girdwood 
4732e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
4742e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
475c5914b0aSMark Brown 		if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
4762e5894d7SBenoit Cousson 			ret = codec_dai->driver->ops->startup(substream,
4772e5894d7SBenoit Cousson 							      codec_dai);
478ddee627cSLiam Girdwood 			if (ret < 0) {
4792e5894d7SBenoit Cousson 				dev_err(codec_dai->dev,
4802e5894d7SBenoit Cousson 					"ASoC: can't open codec %s: %d\n",
4812e5894d7SBenoit Cousson 					codec_dai->name, ret);
482ddee627cSLiam Girdwood 				goto codec_dai_err;
483ddee627cSLiam Girdwood 			}
484ddee627cSLiam Girdwood 		}
485ddee627cSLiam Girdwood 
4862e5894d7SBenoit Cousson 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4872e5894d7SBenoit Cousson 			codec_dai->tx_mask = 0;
4882e5894d7SBenoit Cousson 		else
4892e5894d7SBenoit Cousson 			codec_dai->rx_mask = 0;
4902e5894d7SBenoit Cousson 	}
4912e5894d7SBenoit Cousson 
492ddee627cSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
493ddee627cSLiam Girdwood 		ret = rtd->dai_link->ops->startup(substream);
494ddee627cSLiam Girdwood 		if (ret < 0) {
495103d84a3SLiam Girdwood 			pr_err("ASoC: %s startup failed: %d\n",
49625bfe662SMark Brown 			       rtd->dai_link->name, ret);
497ddee627cSLiam Girdwood 			goto machine_err;
498ddee627cSLiam Girdwood 		}
499ddee627cSLiam Girdwood 	}
500ddee627cSLiam Girdwood 
50101d7584cSLiam Girdwood 	/* Dynamic PCM DAI links compat checks use dynamic capabilities */
50201d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
50301d7584cSLiam Girdwood 		goto dynamic;
50401d7584cSLiam Girdwood 
505ddee627cSLiam Girdwood 	/* Check that the codec and cpu DAIs are compatible */
5062e5894d7SBenoit Cousson 	soc_pcm_init_runtime_hw(substream);
5072e5894d7SBenoit Cousson 
5082e5894d7SBenoit Cousson 	if (rtd->num_codecs == 1)
5092e5894d7SBenoit Cousson 		codec_dai_name = rtd->codec_dai->name;
510ddee627cSLiam Girdwood 
51162e5f676SLars-Peter Clausen 	if (soc_pcm_has_symmetry(substream))
51262e5f676SLars-Peter Clausen 		runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
51362e5f676SLars-Peter Clausen 
514ddee627cSLiam Girdwood 	ret = -EINVAL;
515ddee627cSLiam Girdwood 	if (!runtime->hw.rates) {
516103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
5172e5894d7SBenoit Cousson 			codec_dai_name, cpu_dai->name);
518ddee627cSLiam Girdwood 		goto config_err;
519ddee627cSLiam Girdwood 	}
520ddee627cSLiam Girdwood 	if (!runtime->hw.formats) {
521103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
5222e5894d7SBenoit Cousson 			codec_dai_name, cpu_dai->name);
523ddee627cSLiam Girdwood 		goto config_err;
524ddee627cSLiam Girdwood 	}
525ddee627cSLiam Girdwood 	if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
526ddee627cSLiam Girdwood 	    runtime->hw.channels_min > runtime->hw.channels_max) {
527103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
5282e5894d7SBenoit Cousson 				codec_dai_name, cpu_dai->name);
529ddee627cSLiam Girdwood 		goto config_err;
530ddee627cSLiam Girdwood 	}
531ddee627cSLiam Girdwood 
532c8dd1fecSBenoit Cousson 	soc_pcm_apply_msb(substream);
53358ba9b25SMark Brown 
534ddee627cSLiam Girdwood 	/* Symmetry only applies if we've already got an active stream. */
53517841020SDong Aisheng 	if (cpu_dai->active) {
53617841020SDong Aisheng 		ret = soc_pcm_apply_symmetry(substream, cpu_dai);
53717841020SDong Aisheng 		if (ret != 0)
53817841020SDong Aisheng 			goto config_err;
53917841020SDong Aisheng 	}
54017841020SDong Aisheng 
5412e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
5422e5894d7SBenoit Cousson 		if (rtd->codec_dais[i]->active) {
5432e5894d7SBenoit Cousson 			ret = soc_pcm_apply_symmetry(substream,
5442e5894d7SBenoit Cousson 						     rtd->codec_dais[i]);
545ddee627cSLiam Girdwood 			if (ret != 0)
546ddee627cSLiam Girdwood 				goto config_err;
547ddee627cSLiam Girdwood 		}
5482e5894d7SBenoit Cousson 	}
549ddee627cSLiam Girdwood 
550103d84a3SLiam Girdwood 	pr_debug("ASoC: %s <-> %s info:\n",
5512e5894d7SBenoit Cousson 			codec_dai_name, cpu_dai->name);
552103d84a3SLiam Girdwood 	pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
553103d84a3SLiam Girdwood 	pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
554ddee627cSLiam Girdwood 		 runtime->hw.channels_max);
555103d84a3SLiam Girdwood 	pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
556ddee627cSLiam Girdwood 		 runtime->hw.rate_max);
557ddee627cSLiam Girdwood 
55801d7584cSLiam Girdwood dynamic:
55924894b76SLars-Peter Clausen 
56024894b76SLars-Peter Clausen 	snd_soc_runtime_activate(rtd, substream->stream);
56124894b76SLars-Peter Clausen 
562b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
563ddee627cSLiam Girdwood 	return 0;
564ddee627cSLiam Girdwood 
565ddee627cSLiam Girdwood config_err:
566ddee627cSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
567ddee627cSLiam Girdwood 		rtd->dai_link->ops->shutdown(substream);
568ddee627cSLiam Girdwood 
569ddee627cSLiam Girdwood machine_err:
5702e5894d7SBenoit Cousson 	i = rtd->num_codecs;
571ddee627cSLiam Girdwood 
572ddee627cSLiam Girdwood codec_dai_err:
5732e5894d7SBenoit Cousson 	while (--i >= 0) {
5742e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
5752e5894d7SBenoit Cousson 		if (codec_dai->driver->ops->shutdown)
5762e5894d7SBenoit Cousson 			codec_dai->driver->ops->shutdown(substream, codec_dai);
5772e5894d7SBenoit Cousson 	}
5782e5894d7SBenoit Cousson 
579ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->close)
580ddee627cSLiam Girdwood 		platform->driver->ops->close(substream);
581ddee627cSLiam Girdwood 
582ddee627cSLiam Girdwood platform_err:
583ddee627cSLiam Girdwood 	if (cpu_dai->driver->ops->shutdown)
584ddee627cSLiam Girdwood 		cpu_dai->driver->ops->shutdown(substream, cpu_dai);
585ddee627cSLiam Girdwood out:
586b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
587d6652ef8SMark Brown 
588d6652ef8SMark Brown 	pm_runtime_put(platform->dev);
5892e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
5902e5894d7SBenoit Cousson 		pm_runtime_put(rtd->codec_dais[i]->dev);
591d6652ef8SMark Brown 	pm_runtime_put(cpu_dai->dev);
5922e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
5932e5894d7SBenoit Cousson 		if (!rtd->codec_dais[i]->active)
5942e5894d7SBenoit Cousson 			pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
5952e5894d7SBenoit Cousson 	}
596988e8cc4SNicolin Chen 	if (!cpu_dai->active)
597988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
598d6652ef8SMark Brown 
599ddee627cSLiam Girdwood 	return ret;
600ddee627cSLiam Girdwood }
601ddee627cSLiam Girdwood 
602ddee627cSLiam Girdwood /*
603ddee627cSLiam Girdwood  * Power down the audio subsystem pmdown_time msecs after close is called.
604ddee627cSLiam Girdwood  * This is to ensure there are no pops or clicks in between any music tracks
605ddee627cSLiam Girdwood  * due to DAPM power cycling.
606ddee627cSLiam Girdwood  */
607ddee627cSLiam Girdwood static void close_delayed_work(struct work_struct *work)
608ddee627cSLiam Girdwood {
609ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd =
610ddee627cSLiam Girdwood 			container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
6112e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
612ddee627cSLiam Girdwood 
613b8c0dab9SLiam Girdwood 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
614ddee627cSLiam Girdwood 
615103d84a3SLiam Girdwood 	dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
616ddee627cSLiam Girdwood 		 codec_dai->driver->playback.stream_name,
617ddee627cSLiam Girdwood 		 codec_dai->playback_active ? "active" : "inactive",
6189bffb1fbSMisael Lopez Cruz 		 rtd->pop_wait ? "yes" : "no");
619ddee627cSLiam Girdwood 
620ddee627cSLiam Girdwood 	/* are we waiting on this codec DAI stream */
6219bffb1fbSMisael Lopez Cruz 	if (rtd->pop_wait == 1) {
6229bffb1fbSMisael Lopez Cruz 		rtd->pop_wait = 0;
6237bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
624d9b0951bSLiam Girdwood 					  SND_SOC_DAPM_STREAM_STOP);
625ddee627cSLiam Girdwood 	}
626ddee627cSLiam Girdwood 
627b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
628ddee627cSLiam Girdwood }
629ddee627cSLiam Girdwood 
630ddee627cSLiam Girdwood /*
631ddee627cSLiam Girdwood  * Called by ALSA when a PCM substream is closed. Private data can be
632ddee627cSLiam Girdwood  * freed here. The cpu DAI, codec DAI, machine and platform are also
633ddee627cSLiam Girdwood  * shutdown.
634ddee627cSLiam Girdwood  */
63591d5e6b4SLiam Girdwood static int soc_pcm_close(struct snd_pcm_substream *substream)
636ddee627cSLiam Girdwood {
637ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
638ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
639ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6402e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
6412e5894d7SBenoit Cousson 	int i;
642ddee627cSLiam Girdwood 
643b8c0dab9SLiam Girdwood 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
644ddee627cSLiam Girdwood 
64524894b76SLars-Peter Clausen 	snd_soc_runtime_deactivate(rtd, substream->stream);
646ddee627cSLiam Girdwood 
64717841020SDong Aisheng 	/* clear the corresponding DAIs rate when inactive */
64817841020SDong Aisheng 	if (!cpu_dai->active)
64917841020SDong Aisheng 		cpu_dai->rate = 0;
65017841020SDong Aisheng 
6512e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
6522e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
65317841020SDong Aisheng 		if (!codec_dai->active)
65417841020SDong Aisheng 			codec_dai->rate = 0;
6552e5894d7SBenoit Cousson 	}
65625b76791SSascha Hauer 
657*ae11601bSRamesh Babu 	snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
658*ae11601bSRamesh Babu 
659ddee627cSLiam Girdwood 	if (cpu_dai->driver->ops->shutdown)
660ddee627cSLiam Girdwood 		cpu_dai->driver->ops->shutdown(substream, cpu_dai);
661ddee627cSLiam Girdwood 
6622e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
6632e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
664ddee627cSLiam Girdwood 		if (codec_dai->driver->ops->shutdown)
665ddee627cSLiam Girdwood 			codec_dai->driver->ops->shutdown(substream, codec_dai);
6662e5894d7SBenoit Cousson 	}
667ddee627cSLiam Girdwood 
668ddee627cSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
669ddee627cSLiam Girdwood 		rtd->dai_link->ops->shutdown(substream);
670ddee627cSLiam Girdwood 
671ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->close)
672ddee627cSLiam Girdwood 		platform->driver->ops->close(substream);
673ddee627cSLiam Girdwood 
674ddee627cSLiam Girdwood 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
675208a1589SLars-Peter Clausen 		if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
6761d69c5c5SPeter Ujfalusi 			/* powered down playback stream now */
6771d69c5c5SPeter Ujfalusi 			snd_soc_dapm_stream_event(rtd,
6787bd3a6f3SMark Brown 						  SNDRV_PCM_STREAM_PLAYBACK,
6791d69c5c5SPeter Ujfalusi 						  SND_SOC_DAPM_STREAM_STOP);
6801d69c5c5SPeter Ujfalusi 		} else {
681ddee627cSLiam Girdwood 			/* start delayed pop wq here for playback streams */
6829bffb1fbSMisael Lopez Cruz 			rtd->pop_wait = 1;
683d4e1a73aSMark Brown 			queue_delayed_work(system_power_efficient_wq,
684d4e1a73aSMark Brown 					   &rtd->delayed_work,
685ddee627cSLiam Girdwood 					   msecs_to_jiffies(rtd->pmdown_time));
6861d69c5c5SPeter Ujfalusi 		}
687ddee627cSLiam Girdwood 	} else {
688ddee627cSLiam Girdwood 		/* capture streams can be powered down now */
6897bd3a6f3SMark Brown 		snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
690d9b0951bSLiam Girdwood 					  SND_SOC_DAPM_STREAM_STOP);
691ddee627cSLiam Girdwood 	}
692ddee627cSLiam Girdwood 
693b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
694d6652ef8SMark Brown 
695d6652ef8SMark Brown 	pm_runtime_put(platform->dev);
6962e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
6972e5894d7SBenoit Cousson 		pm_runtime_put(rtd->codec_dais[i]->dev);
698d6652ef8SMark Brown 	pm_runtime_put(cpu_dai->dev);
6992e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
7002e5894d7SBenoit Cousson 		if (!rtd->codec_dais[i]->active)
7012e5894d7SBenoit Cousson 			pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
7022e5894d7SBenoit Cousson 	}
703988e8cc4SNicolin Chen 	if (!cpu_dai->active)
704988e8cc4SNicolin Chen 		pinctrl_pm_select_sleep_state(cpu_dai->dev);
705d6652ef8SMark Brown 
706ddee627cSLiam Girdwood 	return 0;
707ddee627cSLiam Girdwood }
708ddee627cSLiam Girdwood 
709ddee627cSLiam Girdwood /*
710ddee627cSLiam Girdwood  * Called by ALSA when the PCM substream is prepared, can set format, sample
711ddee627cSLiam Girdwood  * rate, etc.  This function is non atomic and can be called multiple times,
712ddee627cSLiam Girdwood  * it can refer to the runtime info.
713ddee627cSLiam Girdwood  */
714ddee627cSLiam Girdwood static int soc_pcm_prepare(struct snd_pcm_substream *substream)
715ddee627cSLiam Girdwood {
716ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
717ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
718ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7192e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
7202e5894d7SBenoit Cousson 	int i, ret = 0;
721ddee627cSLiam Girdwood 
722b8c0dab9SLiam Girdwood 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
723ddee627cSLiam Girdwood 
724ddee627cSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
725ddee627cSLiam Girdwood 		ret = rtd->dai_link->ops->prepare(substream);
726ddee627cSLiam Girdwood 		if (ret < 0) {
727103d84a3SLiam Girdwood 			dev_err(rtd->card->dev, "ASoC: machine prepare error:"
728103d84a3SLiam Girdwood 				" %d\n", ret);
729ddee627cSLiam Girdwood 			goto out;
730ddee627cSLiam Girdwood 		}
731ddee627cSLiam Girdwood 	}
732ddee627cSLiam Girdwood 
733ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->prepare) {
734ddee627cSLiam Girdwood 		ret = platform->driver->ops->prepare(substream);
735ddee627cSLiam Girdwood 		if (ret < 0) {
736103d84a3SLiam Girdwood 			dev_err(platform->dev, "ASoC: platform prepare error:"
737103d84a3SLiam Girdwood 				" %d\n", ret);
738ddee627cSLiam Girdwood 			goto out;
739ddee627cSLiam Girdwood 		}
740ddee627cSLiam Girdwood 	}
741ddee627cSLiam Girdwood 
7422e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
7432e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
744c5914b0aSMark Brown 		if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
7452e5894d7SBenoit Cousson 			ret = codec_dai->driver->ops->prepare(substream,
7462e5894d7SBenoit Cousson 							      codec_dai);
747ddee627cSLiam Girdwood 			if (ret < 0) {
7482e5894d7SBenoit Cousson 				dev_err(codec_dai->dev,
7492e5894d7SBenoit Cousson 					"ASoC: DAI prepare error: %d\n", ret);
750ddee627cSLiam Girdwood 				goto out;
751ddee627cSLiam Girdwood 			}
752ddee627cSLiam Girdwood 		}
7532e5894d7SBenoit Cousson 	}
754ddee627cSLiam Girdwood 
755c5914b0aSMark Brown 	if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
756ddee627cSLiam Girdwood 		ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
757ddee627cSLiam Girdwood 		if (ret < 0) {
758103d84a3SLiam Girdwood 			dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
75925bfe662SMark Brown 				ret);
760ddee627cSLiam Girdwood 			goto out;
761ddee627cSLiam Girdwood 		}
762ddee627cSLiam Girdwood 	}
763ddee627cSLiam Girdwood 
764ddee627cSLiam Girdwood 	/* cancel any delayed stream shutdown that is pending */
765ddee627cSLiam Girdwood 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
7669bffb1fbSMisael Lopez Cruz 	    rtd->pop_wait) {
7679bffb1fbSMisael Lopez Cruz 		rtd->pop_wait = 0;
768ddee627cSLiam Girdwood 		cancel_delayed_work(&rtd->delayed_work);
769ddee627cSLiam Girdwood 	}
770ddee627cSLiam Girdwood 
771d9b0951bSLiam Girdwood 	snd_soc_dapm_stream_event(rtd, substream->stream,
772ddee627cSLiam Girdwood 			SND_SOC_DAPM_STREAM_START);
773ddee627cSLiam Girdwood 
7742e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++)
7752e5894d7SBenoit Cousson 		snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
7762e5894d7SBenoit Cousson 					 substream->stream);
777*ae11601bSRamesh Babu 	snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
778ddee627cSLiam Girdwood 
779ddee627cSLiam Girdwood out:
780b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
781ddee627cSLiam Girdwood 	return ret;
782ddee627cSLiam Girdwood }
783ddee627cSLiam Girdwood 
7842e5894d7SBenoit Cousson static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
7852e5894d7SBenoit Cousson 				       unsigned int mask)
7862e5894d7SBenoit Cousson {
7872e5894d7SBenoit Cousson 	struct snd_interval *interval;
7882e5894d7SBenoit Cousson 	int channels = hweight_long(mask);
7892e5894d7SBenoit Cousson 
7902e5894d7SBenoit Cousson 	interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
7912e5894d7SBenoit Cousson 	interval->min = channels;
7922e5894d7SBenoit Cousson 	interval->max = channels;
7932e5894d7SBenoit Cousson }
7942e5894d7SBenoit Cousson 
79593e6958aSBenoit Cousson int soc_dai_hw_params(struct snd_pcm_substream *substream,
79693e6958aSBenoit Cousson 		      struct snd_pcm_hw_params *params,
79793e6958aSBenoit Cousson 		      struct snd_soc_dai *dai)
79893e6958aSBenoit Cousson {
79993e6958aSBenoit Cousson 	int ret;
80093e6958aSBenoit Cousson 
80193e6958aSBenoit Cousson 	if (dai->driver->ops && dai->driver->ops->hw_params) {
80293e6958aSBenoit Cousson 		ret = dai->driver->ops->hw_params(substream, params, dai);
80393e6958aSBenoit Cousson 		if (ret < 0) {
80493e6958aSBenoit Cousson 			dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
80593e6958aSBenoit Cousson 				dai->name, ret);
80693e6958aSBenoit Cousson 			return ret;
80793e6958aSBenoit Cousson 		}
80893e6958aSBenoit Cousson 	}
80993e6958aSBenoit Cousson 
81093e6958aSBenoit Cousson 	return 0;
81193e6958aSBenoit Cousson }
81293e6958aSBenoit Cousson 
813ddee627cSLiam Girdwood /*
814ddee627cSLiam Girdwood  * Called by ALSA when the hardware params are set by application. This
815ddee627cSLiam Girdwood  * function can also be called multiple times and can allocate buffers
816ddee627cSLiam Girdwood  * (using snd_pcm_lib_* ). It's non-atomic.
817ddee627cSLiam Girdwood  */
818ddee627cSLiam Girdwood static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
819ddee627cSLiam Girdwood 				struct snd_pcm_hw_params *params)
820ddee627cSLiam Girdwood {
821ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
822ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
823ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
8242e5894d7SBenoit Cousson 	int i, ret = 0;
825ddee627cSLiam Girdwood 
826b8c0dab9SLiam Girdwood 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
827ddee627cSLiam Girdwood 
8283635bf09SNicolin Chen 	ret = soc_pcm_params_symmetry(substream, params);
8293635bf09SNicolin Chen 	if (ret)
8303635bf09SNicolin Chen 		goto out;
8313635bf09SNicolin Chen 
832ddee627cSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
833ddee627cSLiam Girdwood 		ret = rtd->dai_link->ops->hw_params(substream, params);
834ddee627cSLiam Girdwood 		if (ret < 0) {
835103d84a3SLiam Girdwood 			dev_err(rtd->card->dev, "ASoC: machine hw_params"
836103d84a3SLiam Girdwood 				" failed: %d\n", ret);
837ddee627cSLiam Girdwood 			goto out;
838ddee627cSLiam Girdwood 		}
839ddee627cSLiam Girdwood 	}
840ddee627cSLiam Girdwood 
8412e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
8422e5894d7SBenoit Cousson 		struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
8432e5894d7SBenoit Cousson 		struct snd_pcm_hw_params codec_params;
8442e5894d7SBenoit Cousson 
8452e5894d7SBenoit Cousson 		/* copy params for each codec */
8462e5894d7SBenoit Cousson 		codec_params = *params;
8472e5894d7SBenoit Cousson 
8482e5894d7SBenoit Cousson 		/* fixup params based on TDM slot masks */
8492e5894d7SBenoit Cousson 		if (codec_dai->tx_mask)
8502e5894d7SBenoit Cousson 			soc_pcm_codec_params_fixup(&codec_params,
8512e5894d7SBenoit Cousson 						   codec_dai->tx_mask);
8522e5894d7SBenoit Cousson 		if (codec_dai->rx_mask)
8532e5894d7SBenoit Cousson 			soc_pcm_codec_params_fixup(&codec_params,
8542e5894d7SBenoit Cousson 						   codec_dai->rx_mask);
8552e5894d7SBenoit Cousson 
85693e6958aSBenoit Cousson 		ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
85793e6958aSBenoit Cousson 		if(ret < 0)
858ddee627cSLiam Girdwood 			goto codec_err;
859ddee627cSLiam Girdwood 
8602e5894d7SBenoit Cousson 		codec_dai->rate = params_rate(&codec_params);
8612e5894d7SBenoit Cousson 		codec_dai->channels = params_channels(&codec_params);
8622e5894d7SBenoit Cousson 		codec_dai->sample_bits = snd_pcm_format_physical_width(
8632e5894d7SBenoit Cousson 						params_format(&codec_params));
864ddee627cSLiam Girdwood 	}
865ddee627cSLiam Girdwood 
86693e6958aSBenoit Cousson 	ret = soc_dai_hw_params(substream, params, cpu_dai);
86793e6958aSBenoit Cousson 	if (ret < 0)
868ddee627cSLiam Girdwood 		goto interface_err;
869ddee627cSLiam Girdwood 
870ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->hw_params) {
871ddee627cSLiam Girdwood 		ret = platform->driver->ops->hw_params(substream, params);
872ddee627cSLiam Girdwood 		if (ret < 0) {
873103d84a3SLiam Girdwood 			dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
874f4333203SLars-Peter Clausen 			       platform->component.name, ret);
875ddee627cSLiam Girdwood 			goto platform_err;
876ddee627cSLiam Girdwood 		}
877ddee627cSLiam Girdwood 	}
878ddee627cSLiam Girdwood 
8793635bf09SNicolin Chen 	/* store the parameters for each DAIs */
88017841020SDong Aisheng 	cpu_dai->rate = params_rate(params);
8813635bf09SNicolin Chen 	cpu_dai->channels = params_channels(params);
8823635bf09SNicolin Chen 	cpu_dai->sample_bits =
8833635bf09SNicolin Chen 		snd_pcm_format_physical_width(params_format(params));
8843635bf09SNicolin Chen 
885ddee627cSLiam Girdwood out:
886b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
887ddee627cSLiam Girdwood 	return ret;
888ddee627cSLiam Girdwood 
889ddee627cSLiam Girdwood platform_err:
890c5914b0aSMark Brown 	if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
891ddee627cSLiam Girdwood 		cpu_dai->driver->ops->hw_free(substream, cpu_dai);
892ddee627cSLiam Girdwood 
893ddee627cSLiam Girdwood interface_err:
8942e5894d7SBenoit Cousson 	i = rtd->num_codecs;
895ddee627cSLiam Girdwood 
896ddee627cSLiam Girdwood codec_err:
8972e5894d7SBenoit Cousson 	while (--i >= 0) {
8982e5894d7SBenoit Cousson 		struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
8992e5894d7SBenoit Cousson 		if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
9002e5894d7SBenoit Cousson 			codec_dai->driver->ops->hw_free(substream, codec_dai);
9012e5894d7SBenoit Cousson 		codec_dai->rate = 0;
9022e5894d7SBenoit Cousson 	}
9032e5894d7SBenoit Cousson 
904ddee627cSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
905ddee627cSLiam Girdwood 		rtd->dai_link->ops->hw_free(substream);
906ddee627cSLiam Girdwood 
907b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
908ddee627cSLiam Girdwood 	return ret;
909ddee627cSLiam Girdwood }
910ddee627cSLiam Girdwood 
911ddee627cSLiam Girdwood /*
912ddee627cSLiam Girdwood  * Frees resources allocated by hw_params, can be called multiple times
913ddee627cSLiam Girdwood  */
914ddee627cSLiam Girdwood static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
915ddee627cSLiam Girdwood {
916ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
917ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
918ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
9192e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
9207f62b6eeSNicolin Chen 	bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
9212e5894d7SBenoit Cousson 	int i;
922ddee627cSLiam Girdwood 
923b8c0dab9SLiam Girdwood 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
924ddee627cSLiam Girdwood 
925d3383420SNicolin Chen 	/* clear the corresponding DAIs parameters when going to be inactive */
926d3383420SNicolin Chen 	if (cpu_dai->active == 1) {
927d3383420SNicolin Chen 		cpu_dai->rate = 0;
928d3383420SNicolin Chen 		cpu_dai->channels = 0;
929d3383420SNicolin Chen 		cpu_dai->sample_bits = 0;
930d3383420SNicolin Chen 	}
931d3383420SNicolin Chen 
9322e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
9332e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
934d3383420SNicolin Chen 		if (codec_dai->active == 1) {
935d3383420SNicolin Chen 			codec_dai->rate = 0;
936d3383420SNicolin Chen 			codec_dai->channels = 0;
937d3383420SNicolin Chen 			codec_dai->sample_bits = 0;
938d3383420SNicolin Chen 		}
9392e5894d7SBenoit Cousson 	}
940d3383420SNicolin Chen 
941ddee627cSLiam Girdwood 	/* apply codec digital mute */
9422e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
9432e5894d7SBenoit Cousson 		if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
9442e5894d7SBenoit Cousson 		    (!playback && rtd->codec_dais[i]->capture_active == 1))
9452e5894d7SBenoit Cousson 			snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
9462e5894d7SBenoit Cousson 						 substream->stream);
9472e5894d7SBenoit Cousson 	}
948ddee627cSLiam Girdwood 
949ddee627cSLiam Girdwood 	/* free any machine hw params */
950ddee627cSLiam Girdwood 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
951ddee627cSLiam Girdwood 		rtd->dai_link->ops->hw_free(substream);
952ddee627cSLiam Girdwood 
953ddee627cSLiam Girdwood 	/* free any DMA resources */
954ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->hw_free)
955ddee627cSLiam Girdwood 		platform->driver->ops->hw_free(substream);
956ddee627cSLiam Girdwood 
957ddee627cSLiam Girdwood 	/* now free hw params for the DAIs  */
9582e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
9592e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
960c5914b0aSMark Brown 		if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
961ddee627cSLiam Girdwood 			codec_dai->driver->ops->hw_free(substream, codec_dai);
9622e5894d7SBenoit Cousson 	}
963ddee627cSLiam Girdwood 
964c5914b0aSMark Brown 	if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
965ddee627cSLiam Girdwood 		cpu_dai->driver->ops->hw_free(substream, cpu_dai);
966ddee627cSLiam Girdwood 
967b8c0dab9SLiam Girdwood 	mutex_unlock(&rtd->pcm_mutex);
968ddee627cSLiam Girdwood 	return 0;
969ddee627cSLiam Girdwood }
970ddee627cSLiam Girdwood 
971ddee627cSLiam Girdwood static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
972ddee627cSLiam Girdwood {
973ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
974ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
975ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
9762e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
9772e5894d7SBenoit Cousson 	int i, ret;
978ddee627cSLiam Girdwood 
9792e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
9802e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
981c5914b0aSMark Brown 		if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
9822e5894d7SBenoit Cousson 			ret = codec_dai->driver->ops->trigger(substream,
9832e5894d7SBenoit Cousson 							      cmd, codec_dai);
984ddee627cSLiam Girdwood 			if (ret < 0)
985ddee627cSLiam Girdwood 				return ret;
986ddee627cSLiam Girdwood 		}
9872e5894d7SBenoit Cousson 	}
988ddee627cSLiam Girdwood 
989ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->trigger) {
990ddee627cSLiam Girdwood 		ret = platform->driver->ops->trigger(substream, cmd);
991ddee627cSLiam Girdwood 		if (ret < 0)
992ddee627cSLiam Girdwood 			return ret;
993ddee627cSLiam Girdwood 	}
994ddee627cSLiam Girdwood 
995c5914b0aSMark Brown 	if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
996ddee627cSLiam Girdwood 		ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
997ddee627cSLiam Girdwood 		if (ret < 0)
998ddee627cSLiam Girdwood 			return ret;
999ddee627cSLiam Girdwood 	}
10004792b0dbSJarkko Nikula 
10014792b0dbSJarkko Nikula 	if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
10024792b0dbSJarkko Nikula 		ret = rtd->dai_link->ops->trigger(substream, cmd);
10034792b0dbSJarkko Nikula 		if (ret < 0)
10044792b0dbSJarkko Nikula 			return ret;
10054792b0dbSJarkko Nikula 	}
10064792b0dbSJarkko Nikula 
1007ddee627cSLiam Girdwood 	return 0;
1008ddee627cSLiam Girdwood }
1009ddee627cSLiam Girdwood 
101045c0a188SMark Brown static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
101145c0a188SMark Brown 				   int cmd)
101207bf84aaSLiam Girdwood {
101307bf84aaSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
101407bf84aaSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
101507bf84aaSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
10162e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
10172e5894d7SBenoit Cousson 	int i, ret;
101807bf84aaSLiam Girdwood 
10192e5894d7SBenoit Cousson 	for (i = 0; i < rtd->num_codecs; i++) {
10202e5894d7SBenoit Cousson 		codec_dai = rtd->codec_dais[i];
1021c5914b0aSMark Brown 		if (codec_dai->driver->ops &&
1022c5914b0aSMark Brown 		    codec_dai->driver->ops->bespoke_trigger) {
10232e5894d7SBenoit Cousson 			ret = codec_dai->driver->ops->bespoke_trigger(substream,
10242e5894d7SBenoit Cousson 								cmd, codec_dai);
102507bf84aaSLiam Girdwood 			if (ret < 0)
102607bf84aaSLiam Girdwood 				return ret;
102707bf84aaSLiam Girdwood 		}
10282e5894d7SBenoit Cousson 	}
102907bf84aaSLiam Girdwood 
1030dcf0fa27SJean-Francois Moine 	if (platform->driver->bespoke_trigger) {
103107bf84aaSLiam Girdwood 		ret = platform->driver->bespoke_trigger(substream, cmd);
103207bf84aaSLiam Girdwood 		if (ret < 0)
103307bf84aaSLiam Girdwood 			return ret;
103407bf84aaSLiam Girdwood 	}
103507bf84aaSLiam Girdwood 
1036c5914b0aSMark Brown 	if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
103707bf84aaSLiam Girdwood 		ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
103807bf84aaSLiam Girdwood 		if (ret < 0)
103907bf84aaSLiam Girdwood 			return ret;
104007bf84aaSLiam Girdwood 	}
104107bf84aaSLiam Girdwood 	return 0;
104207bf84aaSLiam Girdwood }
1043ddee627cSLiam Girdwood /*
1044ddee627cSLiam Girdwood  * soc level wrapper for pointer callback
1045ddee627cSLiam Girdwood  * If cpu_dai, codec_dai, platform driver has the delay callback, than
1046ddee627cSLiam Girdwood  * the runtime->delay will be updated accordingly.
1047ddee627cSLiam Girdwood  */
1048ddee627cSLiam Girdwood static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1049ddee627cSLiam Girdwood {
1050ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1051ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
1052ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
10532e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
1054ddee627cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
1055ddee627cSLiam Girdwood 	snd_pcm_uframes_t offset = 0;
1056ddee627cSLiam Girdwood 	snd_pcm_sframes_t delay = 0;
10572e5894d7SBenoit Cousson 	snd_pcm_sframes_t codec_delay = 0;
10582e5894d7SBenoit Cousson 	int i;
1059ddee627cSLiam Girdwood 
1060ddee627cSLiam Girdwood 	if (platform->driver->ops && platform->driver->ops->pointer)
1061ddee627cSLiam Girdwood 		offset = platform->driver->ops->pointer(substream);
1062ddee627cSLiam Girdwood 
1063c5914b0aSMark Brown 	if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
1064ddee627cSLiam Girdwood 		delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1065ddee627cSLiam 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 && codec_dai->driver->ops->delay)
10692e5894d7SBenoit Cousson 			codec_delay = max(codec_delay,
10702e5894d7SBenoit Cousson 					codec_dai->driver->ops->delay(substream,
10712e5894d7SBenoit Cousson 								    codec_dai));
10722e5894d7SBenoit Cousson 	}
10732e5894d7SBenoit Cousson 	delay += codec_delay;
1074ddee627cSLiam Girdwood 
10752e5894d7SBenoit Cousson 	/*
10762e5894d7SBenoit Cousson 	 * None of the existing platform drivers implement delay(), so
10772e5894d7SBenoit Cousson 	 * for now the codec_dai of first multicodec entry is used
10782e5894d7SBenoit Cousson 	 */
1079ddee627cSLiam Girdwood 	if (platform->driver->delay)
10802e5894d7SBenoit Cousson 		delay += platform->driver->delay(substream, rtd->codec_dais[0]);
1081ddee627cSLiam Girdwood 
1082ddee627cSLiam Girdwood 	runtime->delay = delay;
1083ddee627cSLiam Girdwood 
1084ddee627cSLiam Girdwood 	return offset;
1085ddee627cSLiam Girdwood }
1086ddee627cSLiam Girdwood 
108701d7584cSLiam Girdwood /* connect a FE and BE */
108801d7584cSLiam Girdwood static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
108901d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
109001d7584cSLiam Girdwood {
109101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
109201d7584cSLiam Girdwood 
109301d7584cSLiam Girdwood 	/* only add new dpcms */
109401d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
109501d7584cSLiam Girdwood 		if (dpcm->be == be && dpcm->fe == fe)
109601d7584cSLiam Girdwood 			return 0;
109701d7584cSLiam Girdwood 	}
109801d7584cSLiam Girdwood 
109901d7584cSLiam Girdwood 	dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
110001d7584cSLiam Girdwood 	if (!dpcm)
110101d7584cSLiam Girdwood 		return -ENOMEM;
110201d7584cSLiam Girdwood 
110301d7584cSLiam Girdwood 	dpcm->be = be;
110401d7584cSLiam Girdwood 	dpcm->fe = fe;
110501d7584cSLiam Girdwood 	be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
110601d7584cSLiam Girdwood 	dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
110701d7584cSLiam Girdwood 	list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
110801d7584cSLiam Girdwood 	list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
110901d7584cSLiam Girdwood 
111001d7584cSLiam Girdwood 	dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
111101d7584cSLiam Girdwood 			stream ? "capture" : "playback",  fe->dai_link->name,
111201d7584cSLiam Girdwood 			stream ? "<-" : "->", be->dai_link->name);
111301d7584cSLiam Girdwood 
1114f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS
1115f86dcef8SLiam Girdwood 	dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1116f86dcef8SLiam Girdwood 			fe->debugfs_dpcm_root, &dpcm->state);
1117f86dcef8SLiam Girdwood #endif
111801d7584cSLiam Girdwood 	return 1;
111901d7584cSLiam Girdwood }
112001d7584cSLiam Girdwood 
112101d7584cSLiam Girdwood /* reparent a BE onto another FE */
112201d7584cSLiam Girdwood static void dpcm_be_reparent(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 	struct snd_pcm_substream *fe_substream, *be_substream;
112701d7584cSLiam Girdwood 
112801d7584cSLiam Girdwood 	/* reparent if BE is connected to other FEs */
112901d7584cSLiam Girdwood 	if (!be->dpcm[stream].users)
113001d7584cSLiam Girdwood 		return;
113101d7584cSLiam Girdwood 
113201d7584cSLiam Girdwood 	be_substream = snd_soc_dpcm_get_substream(be, stream);
113301d7584cSLiam Girdwood 
113401d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
113501d7584cSLiam Girdwood 		if (dpcm->fe == fe)
113601d7584cSLiam Girdwood 			continue;
113701d7584cSLiam Girdwood 
113801d7584cSLiam Girdwood 		dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
113901d7584cSLiam Girdwood 			stream ? "capture" : "playback",
114001d7584cSLiam Girdwood 			dpcm->fe->dai_link->name,
114101d7584cSLiam Girdwood 			stream ? "<-" : "->", dpcm->be->dai_link->name);
114201d7584cSLiam Girdwood 
114301d7584cSLiam Girdwood 		fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
114401d7584cSLiam Girdwood 		be_substream->runtime = fe_substream->runtime;
114501d7584cSLiam Girdwood 		break;
114601d7584cSLiam Girdwood 	}
114701d7584cSLiam Girdwood }
114801d7584cSLiam Girdwood 
114901d7584cSLiam Girdwood /* disconnect a BE and FE */
115023607025SLiam Girdwood void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
115101d7584cSLiam Girdwood {
115201d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm, *d;
115301d7584cSLiam Girdwood 
115401d7584cSLiam Girdwood 	list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1155103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
115601d7584cSLiam Girdwood 				stream ? "capture" : "playback",
115701d7584cSLiam Girdwood 				dpcm->be->dai_link->name);
115801d7584cSLiam Girdwood 
115901d7584cSLiam Girdwood 		if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
116001d7584cSLiam Girdwood 			continue;
116101d7584cSLiam Girdwood 
116201d7584cSLiam Girdwood 		dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
116301d7584cSLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name,
116401d7584cSLiam Girdwood 			stream ? "<-" : "->", dpcm->be->dai_link->name);
116501d7584cSLiam Girdwood 
116601d7584cSLiam Girdwood 		/* BEs still alive need new FE */
116701d7584cSLiam Girdwood 		dpcm_be_reparent(fe, dpcm->be, stream);
116801d7584cSLiam Girdwood 
1169f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS
1170f86dcef8SLiam Girdwood 		debugfs_remove(dpcm->debugfs_state);
1171f86dcef8SLiam Girdwood #endif
117201d7584cSLiam Girdwood 		list_del(&dpcm->list_be);
117301d7584cSLiam Girdwood 		list_del(&dpcm->list_fe);
117401d7584cSLiam Girdwood 		kfree(dpcm);
117501d7584cSLiam Girdwood 	}
117601d7584cSLiam Girdwood }
117701d7584cSLiam Girdwood 
117801d7584cSLiam Girdwood /* get BE for DAI widget and stream */
117901d7584cSLiam Girdwood static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
118001d7584cSLiam Girdwood 		struct snd_soc_dapm_widget *widget, int stream)
118101d7584cSLiam Girdwood {
118201d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *be;
11832e5894d7SBenoit Cousson 	int i, j;
118401d7584cSLiam Girdwood 
118501d7584cSLiam Girdwood 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
118601d7584cSLiam Girdwood 		for (i = 0; i < card->num_links; i++) {
118701d7584cSLiam Girdwood 			be = &card->rtd[i];
118801d7584cSLiam Girdwood 
118935ea0655SLiam Girdwood 			if (!be->dai_link->no_pcm)
119035ea0655SLiam Girdwood 				continue;
119135ea0655SLiam Girdwood 
11922e5894d7SBenoit Cousson 			if (be->cpu_dai->playback_widget == widget)
119301d7584cSLiam Girdwood 				return be;
11942e5894d7SBenoit Cousson 
11952e5894d7SBenoit Cousson 			for (j = 0; j < be->num_codecs; j++) {
11962e5894d7SBenoit Cousson 				struct snd_soc_dai *dai = be->codec_dais[j];
11972e5894d7SBenoit Cousson 				if (dai->playback_widget == widget)
11982e5894d7SBenoit Cousson 					return be;
11992e5894d7SBenoit Cousson 			}
120001d7584cSLiam Girdwood 		}
120101d7584cSLiam Girdwood 	} else {
120201d7584cSLiam Girdwood 
120301d7584cSLiam Girdwood 		for (i = 0; i < card->num_links; i++) {
120401d7584cSLiam Girdwood 			be = &card->rtd[i];
120501d7584cSLiam Girdwood 
120635ea0655SLiam Girdwood 			if (!be->dai_link->no_pcm)
120735ea0655SLiam Girdwood 				continue;
120835ea0655SLiam Girdwood 
12092e5894d7SBenoit Cousson 			if (be->cpu_dai->capture_widget == widget)
121001d7584cSLiam Girdwood 				return be;
12112e5894d7SBenoit Cousson 
12122e5894d7SBenoit Cousson 			for (j = 0; j < be->num_codecs; j++) {
12132e5894d7SBenoit Cousson 				struct snd_soc_dai *dai = be->codec_dais[j];
12142e5894d7SBenoit Cousson 				if (dai->capture_widget == widget)
12152e5894d7SBenoit Cousson 					return be;
12162e5894d7SBenoit Cousson 			}
121701d7584cSLiam Girdwood 		}
121801d7584cSLiam Girdwood 	}
121901d7584cSLiam Girdwood 
1220103d84a3SLiam Girdwood 	dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
122101d7584cSLiam Girdwood 		stream ? "capture" : "playback", widget->name);
122201d7584cSLiam Girdwood 	return NULL;
122301d7584cSLiam Girdwood }
122401d7584cSLiam Girdwood 
122501d7584cSLiam Girdwood static inline struct snd_soc_dapm_widget *
122637018610SBenoit Cousson 	dai_get_widget(struct snd_soc_dai *dai, int stream)
122701d7584cSLiam Girdwood {
122801d7584cSLiam Girdwood 	if (stream == SNDRV_PCM_STREAM_PLAYBACK)
122937018610SBenoit Cousson 		return dai->playback_widget;
123001d7584cSLiam Girdwood 	else
123137018610SBenoit Cousson 		return dai->capture_widget;
123201d7584cSLiam Girdwood }
123301d7584cSLiam Girdwood 
123401d7584cSLiam Girdwood static int widget_in_list(struct snd_soc_dapm_widget_list *list,
123501d7584cSLiam Girdwood 		struct snd_soc_dapm_widget *widget)
123601d7584cSLiam Girdwood {
123701d7584cSLiam Girdwood 	int i;
123801d7584cSLiam Girdwood 
123901d7584cSLiam Girdwood 	for (i = 0; i < list->num_widgets; i++) {
124001d7584cSLiam Girdwood 		if (widget == list->widgets[i])
124101d7584cSLiam Girdwood 			return 1;
124201d7584cSLiam Girdwood 	}
124301d7584cSLiam Girdwood 
124401d7584cSLiam Girdwood 	return 0;
124501d7584cSLiam Girdwood }
124601d7584cSLiam Girdwood 
124723607025SLiam Girdwood int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
124801d7584cSLiam Girdwood 	int stream, struct snd_soc_dapm_widget_list **list_)
124901d7584cSLiam Girdwood {
125001d7584cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = fe->cpu_dai;
125101d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list;
125201d7584cSLiam Girdwood 	int paths;
125301d7584cSLiam Girdwood 
125401d7584cSLiam Girdwood 	list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
125501d7584cSLiam Girdwood 			sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
125601d7584cSLiam Girdwood 	if (list == NULL)
125701d7584cSLiam Girdwood 		return -ENOMEM;
125801d7584cSLiam Girdwood 
125901d7584cSLiam Girdwood 	/* get number of valid DAI paths and their widgets */
126001d7584cSLiam Girdwood 	paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
126101d7584cSLiam Girdwood 
1262103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
126301d7584cSLiam Girdwood 			stream ? "capture" : "playback");
126401d7584cSLiam Girdwood 
126501d7584cSLiam Girdwood 	*list_ = list;
126601d7584cSLiam Girdwood 	return paths;
126701d7584cSLiam Girdwood }
126801d7584cSLiam Girdwood 
126901d7584cSLiam Girdwood static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
127001d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list **list_)
127101d7584cSLiam Girdwood {
127201d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
127301d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list = *list_;
127401d7584cSLiam Girdwood 	struct snd_soc_dapm_widget *widget;
127501d7584cSLiam Girdwood 	int prune = 0;
127601d7584cSLiam Girdwood 
127701d7584cSLiam Girdwood 	/* Destroy any old FE <--> BE connections */
127801d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
12792e5894d7SBenoit Cousson 		unsigned int i;
128001d7584cSLiam Girdwood 
128101d7584cSLiam Girdwood 		/* is there a valid CPU DAI widget for this BE */
128237018610SBenoit Cousson 		widget = dai_get_widget(dpcm->be->cpu_dai, stream);
128301d7584cSLiam Girdwood 
128401d7584cSLiam Girdwood 		/* prune the BE if it's no longer in our active list */
128501d7584cSLiam Girdwood 		if (widget && widget_in_list(list, widget))
128601d7584cSLiam Girdwood 			continue;
128701d7584cSLiam Girdwood 
128801d7584cSLiam Girdwood 		/* is there a valid CODEC DAI widget for this BE */
12892e5894d7SBenoit Cousson 		for (i = 0; i < dpcm->be->num_codecs; i++) {
12902e5894d7SBenoit Cousson 			struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
12912e5894d7SBenoit Cousson 			widget = dai_get_widget(dai, stream);
129201d7584cSLiam Girdwood 
129301d7584cSLiam Girdwood 			/* prune the BE if it's no longer in our active list */
129401d7584cSLiam Girdwood 			if (widget && widget_in_list(list, widget))
129501d7584cSLiam Girdwood 				continue;
12962e5894d7SBenoit Cousson 		}
129701d7584cSLiam Girdwood 
1298103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
129901d7584cSLiam Girdwood 			stream ? "capture" : "playback",
130001d7584cSLiam Girdwood 			dpcm->be->dai_link->name, fe->dai_link->name);
130101d7584cSLiam Girdwood 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
130201d7584cSLiam Girdwood 		dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
130301d7584cSLiam Girdwood 		prune++;
130401d7584cSLiam Girdwood 	}
130501d7584cSLiam Girdwood 
1306103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
130701d7584cSLiam Girdwood 	return prune;
130801d7584cSLiam Girdwood }
130901d7584cSLiam Girdwood 
131001d7584cSLiam Girdwood static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
131101d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list **list_)
131201d7584cSLiam Girdwood {
131301d7584cSLiam Girdwood 	struct snd_soc_card *card = fe->card;
131401d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list = *list_;
131501d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *be;
131601d7584cSLiam Girdwood 	int i, new = 0, err;
131701d7584cSLiam Girdwood 
131801d7584cSLiam Girdwood 	/* Create any new FE <--> BE connections */
131901d7584cSLiam Girdwood 	for (i = 0; i < list->num_widgets; i++) {
132001d7584cSLiam Girdwood 
13214616274dSMark Brown 		switch (list->widgets[i]->id) {
13224616274dSMark Brown 		case snd_soc_dapm_dai_in:
13234616274dSMark Brown 		case snd_soc_dapm_dai_out:
13244616274dSMark Brown 			break;
13254616274dSMark Brown 		default:
132601d7584cSLiam Girdwood 			continue;
13274616274dSMark Brown 		}
132801d7584cSLiam Girdwood 
132901d7584cSLiam Girdwood 		/* is there a valid BE rtd for this widget */
133001d7584cSLiam Girdwood 		be = dpcm_get_be(card, list->widgets[i], stream);
133101d7584cSLiam Girdwood 		if (!be) {
1332103d84a3SLiam Girdwood 			dev_err(fe->dev, "ASoC: no BE found for %s\n",
133301d7584cSLiam Girdwood 					list->widgets[i]->name);
133401d7584cSLiam Girdwood 			continue;
133501d7584cSLiam Girdwood 		}
133601d7584cSLiam Girdwood 
133701d7584cSLiam Girdwood 		/* make sure BE is a real BE */
133801d7584cSLiam Girdwood 		if (!be->dai_link->no_pcm)
133901d7584cSLiam Girdwood 			continue;
134001d7584cSLiam Girdwood 
134101d7584cSLiam Girdwood 		/* don't connect if FE is not running */
134223607025SLiam Girdwood 		if (!fe->dpcm[stream].runtime && !fe->fe_compr)
134301d7584cSLiam Girdwood 			continue;
134401d7584cSLiam Girdwood 
134501d7584cSLiam Girdwood 		/* newly connected FE and BE */
134601d7584cSLiam Girdwood 		err = dpcm_be_connect(fe, be, stream);
134701d7584cSLiam Girdwood 		if (err < 0) {
1348103d84a3SLiam Girdwood 			dev_err(fe->dev, "ASoC: can't connect %s\n",
134901d7584cSLiam Girdwood 				list->widgets[i]->name);
135001d7584cSLiam Girdwood 			break;
135101d7584cSLiam Girdwood 		} else if (err == 0) /* already connected */
135201d7584cSLiam Girdwood 			continue;
135301d7584cSLiam Girdwood 
135401d7584cSLiam Girdwood 		/* new */
135501d7584cSLiam Girdwood 		be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
135601d7584cSLiam Girdwood 		new++;
135701d7584cSLiam Girdwood 	}
135801d7584cSLiam Girdwood 
1359103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
136001d7584cSLiam Girdwood 	return new;
136101d7584cSLiam Girdwood }
136201d7584cSLiam Girdwood 
136301d7584cSLiam Girdwood /*
136401d7584cSLiam Girdwood  * Find the corresponding BE DAIs that source or sink audio to this
136501d7584cSLiam Girdwood  * FE substream.
136601d7584cSLiam Girdwood  */
136723607025SLiam Girdwood int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
136801d7584cSLiam Girdwood 	int stream, struct snd_soc_dapm_widget_list **list, int new)
136901d7584cSLiam Girdwood {
137001d7584cSLiam Girdwood 	if (new)
137101d7584cSLiam Girdwood 		return dpcm_add_paths(fe, stream, list);
137201d7584cSLiam Girdwood 	else
137301d7584cSLiam Girdwood 		return dpcm_prune_paths(fe, stream, list);
137401d7584cSLiam Girdwood }
137501d7584cSLiam Girdwood 
137623607025SLiam Girdwood void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
137701d7584cSLiam Girdwood {
137801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
137901d7584cSLiam Girdwood 
138001d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
138101d7584cSLiam Girdwood 		dpcm->be->dpcm[stream].runtime_update =
138201d7584cSLiam Girdwood 						SND_SOC_DPCM_UPDATE_NO;
138301d7584cSLiam Girdwood }
138401d7584cSLiam Girdwood 
138501d7584cSLiam Girdwood static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
138601d7584cSLiam Girdwood 	int stream)
138701d7584cSLiam Girdwood {
138801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
138901d7584cSLiam Girdwood 
139001d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
139101d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
139201d7584cSLiam Girdwood 
139301d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
139401d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
139501d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
139601d7584cSLiam Girdwood 
139701d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1398103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
139901d7584cSLiam Girdwood 				stream ? "capture" : "playback",
140001d7584cSLiam Girdwood 				be->dpcm[stream].state);
140101d7584cSLiam Girdwood 
140201d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
140301d7584cSLiam Girdwood 			continue;
140401d7584cSLiam Girdwood 
140501d7584cSLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
140601d7584cSLiam Girdwood 			continue;
140701d7584cSLiam Girdwood 
140801d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
140901d7584cSLiam Girdwood 		be_substream->runtime = NULL;
141001d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
141101d7584cSLiam Girdwood 	}
141201d7584cSLiam Girdwood }
141301d7584cSLiam Girdwood 
141423607025SLiam Girdwood int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
141501d7584cSLiam Girdwood {
141601d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
141701d7584cSLiam Girdwood 	int err, count = 0;
141801d7584cSLiam Girdwood 
141901d7584cSLiam Girdwood 	/* only startup BE DAIs that are either sinks or sources to this FE DAI */
142001d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
142101d7584cSLiam Girdwood 
142201d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
142301d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
142401d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
142501d7584cSLiam Girdwood 
14262062b4c5SRussell King - ARM Linux 		if (!be_substream) {
14272062b4c5SRussell King - ARM Linux 			dev_err(be->dev, "ASoC: no backend %s stream\n",
14282062b4c5SRussell King - ARM Linux 				stream ? "capture" : "playback");
14292062b4c5SRussell King - ARM Linux 			continue;
14302062b4c5SRussell King - ARM Linux 		}
14312062b4c5SRussell King - ARM Linux 
143201d7584cSLiam Girdwood 		/* is this op for this BE ? */
143301d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
143401d7584cSLiam Girdwood 			continue;
143501d7584cSLiam Girdwood 
143601d7584cSLiam Girdwood 		/* first time the dpcm is open ? */
143701d7584cSLiam Girdwood 		if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1438103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: too many users %s at open %d\n",
143901d7584cSLiam Girdwood 				stream ? "capture" : "playback",
144001d7584cSLiam Girdwood 				be->dpcm[stream].state);
144101d7584cSLiam Girdwood 
144201d7584cSLiam Girdwood 		if (be->dpcm[stream].users++ != 0)
144301d7584cSLiam Girdwood 			continue;
144401d7584cSLiam Girdwood 
144501d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
144601d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
144701d7584cSLiam Girdwood 			continue;
144801d7584cSLiam Girdwood 
14492062b4c5SRussell King - ARM Linux 		dev_dbg(be->dev, "ASoC: open %s BE %s\n",
14502062b4c5SRussell King - ARM Linux 			stream ? "capture" : "playback", be->dai_link->name);
145101d7584cSLiam Girdwood 
145201d7584cSLiam Girdwood 		be_substream->runtime = be->dpcm[stream].runtime;
145301d7584cSLiam Girdwood 		err = soc_pcm_open(be_substream);
145401d7584cSLiam Girdwood 		if (err < 0) {
1455103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: BE open failed %d\n", err);
145601d7584cSLiam Girdwood 			be->dpcm[stream].users--;
145701d7584cSLiam Girdwood 			if (be->dpcm[stream].users < 0)
1458103d84a3SLiam Girdwood 				dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
145901d7584cSLiam Girdwood 					stream ? "capture" : "playback",
146001d7584cSLiam Girdwood 					be->dpcm[stream].state);
146101d7584cSLiam Girdwood 
146201d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
146301d7584cSLiam Girdwood 			goto unwind;
146401d7584cSLiam Girdwood 		}
146501d7584cSLiam Girdwood 
146601d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
146701d7584cSLiam Girdwood 		count++;
146801d7584cSLiam Girdwood 	}
146901d7584cSLiam Girdwood 
147001d7584cSLiam Girdwood 	return count;
147101d7584cSLiam Girdwood 
147201d7584cSLiam Girdwood unwind:
147301d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
147401d7584cSLiam Girdwood 	list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
147501d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
147601d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
147701d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
147801d7584cSLiam Girdwood 
147901d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
148001d7584cSLiam Girdwood 			continue;
148101d7584cSLiam Girdwood 
148201d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1483103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close %d\n",
148401d7584cSLiam Girdwood 				stream ? "capture" : "playback",
148501d7584cSLiam Girdwood 				be->dpcm[stream].state);
148601d7584cSLiam Girdwood 
148701d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
148801d7584cSLiam Girdwood 			continue;
148901d7584cSLiam Girdwood 
149001d7584cSLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
149101d7584cSLiam Girdwood 			continue;
149201d7584cSLiam Girdwood 
149301d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
149401d7584cSLiam Girdwood 		be_substream->runtime = NULL;
149501d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
149601d7584cSLiam Girdwood 	}
149701d7584cSLiam Girdwood 
149801d7584cSLiam Girdwood 	return err;
149901d7584cSLiam Girdwood }
150001d7584cSLiam Girdwood 
150108ae9b45SLars-Peter Clausen static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
150208ae9b45SLars-Peter Clausen 	struct snd_soc_pcm_stream *stream)
150308ae9b45SLars-Peter Clausen {
150408ae9b45SLars-Peter Clausen 	runtime->hw.rate_min = stream->rate_min;
150508ae9b45SLars-Peter Clausen 	runtime->hw.rate_max = stream->rate_max;
150608ae9b45SLars-Peter Clausen 	runtime->hw.channels_min = stream->channels_min;
150708ae9b45SLars-Peter Clausen 	runtime->hw.channels_max = stream->channels_max;
1508002220a9SLars-Peter Clausen 	if (runtime->hw.formats)
150908ae9b45SLars-Peter Clausen 		runtime->hw.formats &= stream->formats;
1510002220a9SLars-Peter Clausen 	else
1511002220a9SLars-Peter Clausen 		runtime->hw.formats = stream->formats;
151208ae9b45SLars-Peter Clausen 	runtime->hw.rates = stream->rates;
151308ae9b45SLars-Peter Clausen }
151408ae9b45SLars-Peter Clausen 
151545c0a188SMark Brown static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
151601d7584cSLiam Girdwood {
151701d7584cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
151801d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
151901d7584cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
152001d7584cSLiam Girdwood 	struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
152101d7584cSLiam Girdwood 
152208ae9b45SLars-Peter Clausen 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
152308ae9b45SLars-Peter Clausen 		dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
152408ae9b45SLars-Peter Clausen 	else
152508ae9b45SLars-Peter Clausen 		dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
152601d7584cSLiam Girdwood }
152701d7584cSLiam Girdwood 
152801d7584cSLiam Girdwood static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
152901d7584cSLiam Girdwood {
153001d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
153101d7584cSLiam Girdwood 	struct snd_pcm_runtime *runtime = fe_substream->runtime;
153201d7584cSLiam Girdwood 	int stream = fe_substream->stream, ret = 0;
153301d7584cSLiam Girdwood 
153401d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
153501d7584cSLiam Girdwood 
153601d7584cSLiam Girdwood 	ret = dpcm_be_dai_startup(fe, fe_substream->stream);
153701d7584cSLiam Girdwood 	if (ret < 0) {
1538103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
153901d7584cSLiam Girdwood 		goto be_err;
154001d7584cSLiam Girdwood 	}
154101d7584cSLiam Girdwood 
1542103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
154301d7584cSLiam Girdwood 
154401d7584cSLiam Girdwood 	/* start the DAI frontend */
154501d7584cSLiam Girdwood 	ret = soc_pcm_open(fe_substream);
154601d7584cSLiam Girdwood 	if (ret < 0) {
1547103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
154801d7584cSLiam Girdwood 		goto unwind;
154901d7584cSLiam Girdwood 	}
155001d7584cSLiam Girdwood 
155101d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
155201d7584cSLiam Girdwood 
155301d7584cSLiam Girdwood 	dpcm_set_fe_runtime(fe_substream);
155401d7584cSLiam Girdwood 	snd_pcm_limit_hw_rates(runtime);
155501d7584cSLiam Girdwood 
155601d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
155701d7584cSLiam Girdwood 	return 0;
155801d7584cSLiam Girdwood 
155901d7584cSLiam Girdwood unwind:
156001d7584cSLiam Girdwood 	dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
156101d7584cSLiam Girdwood be_err:
156201d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
156301d7584cSLiam Girdwood 	return ret;
156401d7584cSLiam Girdwood }
156501d7584cSLiam Girdwood 
156623607025SLiam Girdwood int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
156701d7584cSLiam Girdwood {
156801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
156901d7584cSLiam Girdwood 
157001d7584cSLiam Girdwood 	/* only shutdown BEs that are either sinks or sources to this FE DAI */
157101d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
157201d7584cSLiam Girdwood 
157301d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
157401d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
157501d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
157601d7584cSLiam Girdwood 
157701d7584cSLiam Girdwood 		/* is this op for this BE ? */
157801d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
157901d7584cSLiam Girdwood 			continue;
158001d7584cSLiam Girdwood 
158101d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1582103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
158301d7584cSLiam Girdwood 				stream ? "capture" : "playback",
158401d7584cSLiam Girdwood 				be->dpcm[stream].state);
158501d7584cSLiam Girdwood 
158601d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
158701d7584cSLiam Girdwood 			continue;
158801d7584cSLiam Girdwood 
158901d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
159001d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
159101d7584cSLiam Girdwood 			continue;
159201d7584cSLiam Girdwood 
1593103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: close BE %s\n",
159401d7584cSLiam Girdwood 			dpcm->fe->dai_link->name);
159501d7584cSLiam Girdwood 
159601d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
159701d7584cSLiam Girdwood 		be_substream->runtime = NULL;
159801d7584cSLiam Girdwood 
159901d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
160001d7584cSLiam Girdwood 	}
160101d7584cSLiam Girdwood 	return 0;
160201d7584cSLiam Girdwood }
160301d7584cSLiam Girdwood 
160401d7584cSLiam Girdwood static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
160501d7584cSLiam Girdwood {
160601d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
160701d7584cSLiam Girdwood 	int stream = substream->stream;
160801d7584cSLiam Girdwood 
160901d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
161001d7584cSLiam Girdwood 
161101d7584cSLiam Girdwood 	/* shutdown the BEs */
161201d7584cSLiam Girdwood 	dpcm_be_dai_shutdown(fe, substream->stream);
161301d7584cSLiam Girdwood 
1614103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
161501d7584cSLiam Girdwood 
161601d7584cSLiam Girdwood 	/* now shutdown the frontend */
161701d7584cSLiam Girdwood 	soc_pcm_close(substream);
161801d7584cSLiam Girdwood 
161901d7584cSLiam Girdwood 	/* run the stream event for each BE */
162001d7584cSLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
162101d7584cSLiam Girdwood 
162201d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
162301d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
162401d7584cSLiam Girdwood 	return 0;
162501d7584cSLiam Girdwood }
162601d7584cSLiam Girdwood 
162723607025SLiam Girdwood int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
162801d7584cSLiam Girdwood {
162901d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
163001d7584cSLiam Girdwood 
163101d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
163201d7584cSLiam Girdwood 	 * to this frontend DAI */
163301d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
163401d7584cSLiam Girdwood 
163501d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
163601d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
163701d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
163801d7584cSLiam Girdwood 
163901d7584cSLiam Girdwood 		/* is this op for this BE ? */
164001d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
164101d7584cSLiam Girdwood 			continue;
164201d7584cSLiam Girdwood 
164301d7584cSLiam Girdwood 		/* only free hw when no longer used - check all FEs */
164401d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
164501d7584cSLiam Girdwood 				continue;
164601d7584cSLiam Girdwood 
164701d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
164801d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
164901d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
165008b27848SPatrick Lai 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
165101d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
165201d7584cSLiam Girdwood 			continue;
165301d7584cSLiam Girdwood 
1654103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
165501d7584cSLiam Girdwood 			dpcm->fe->dai_link->name);
165601d7584cSLiam Girdwood 
165701d7584cSLiam Girdwood 		soc_pcm_hw_free(be_substream);
165801d7584cSLiam Girdwood 
165901d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
166001d7584cSLiam Girdwood 	}
166101d7584cSLiam Girdwood 
166201d7584cSLiam Girdwood 	return 0;
166301d7584cSLiam Girdwood }
166401d7584cSLiam Girdwood 
166545c0a188SMark Brown static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
166601d7584cSLiam Girdwood {
166701d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
166801d7584cSLiam Girdwood 	int err, stream = substream->stream;
166901d7584cSLiam Girdwood 
167001d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
167101d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
167201d7584cSLiam Girdwood 
1673103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
167401d7584cSLiam Girdwood 
167501d7584cSLiam Girdwood 	/* call hw_free on the frontend */
167601d7584cSLiam Girdwood 	err = soc_pcm_hw_free(substream);
167701d7584cSLiam Girdwood 	if (err < 0)
1678103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
167901d7584cSLiam Girdwood 			fe->dai_link->name);
168001d7584cSLiam Girdwood 
168101d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
168201d7584cSLiam Girdwood 	 * to this frontend DAI */
168301d7584cSLiam Girdwood 	err = dpcm_be_dai_hw_free(fe, stream);
168401d7584cSLiam Girdwood 
168501d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
168601d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
168701d7584cSLiam Girdwood 
168801d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
168901d7584cSLiam Girdwood 	return 0;
169001d7584cSLiam Girdwood }
169101d7584cSLiam Girdwood 
169223607025SLiam Girdwood int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
169301d7584cSLiam Girdwood {
169401d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
169501d7584cSLiam Girdwood 	int ret;
169601d7584cSLiam Girdwood 
169701d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
169801d7584cSLiam Girdwood 
169901d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
170001d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
170101d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
170201d7584cSLiam Girdwood 
170301d7584cSLiam Girdwood 		/* is this op for this BE ? */
170401d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
170501d7584cSLiam Girdwood 			continue;
170601d7584cSLiam Girdwood 
170701d7584cSLiam Girdwood 		/* only allow hw_params() if no connected FEs are running */
170801d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_params(fe, be, stream))
170901d7584cSLiam Girdwood 			continue;
171001d7584cSLiam Girdwood 
171101d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
171201d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
171301d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
171401d7584cSLiam Girdwood 			continue;
171501d7584cSLiam Girdwood 
1716103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
171701d7584cSLiam Girdwood 			dpcm->fe->dai_link->name);
171801d7584cSLiam Girdwood 
171901d7584cSLiam Girdwood 		/* copy params for each dpcm */
172001d7584cSLiam Girdwood 		memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
172101d7584cSLiam Girdwood 				sizeof(struct snd_pcm_hw_params));
172201d7584cSLiam Girdwood 
172301d7584cSLiam Girdwood 		/* perform any hw_params fixups */
172401d7584cSLiam Girdwood 		if (be->dai_link->be_hw_params_fixup) {
172501d7584cSLiam Girdwood 			ret = be->dai_link->be_hw_params_fixup(be,
172601d7584cSLiam Girdwood 					&dpcm->hw_params);
172701d7584cSLiam Girdwood 			if (ret < 0) {
172801d7584cSLiam Girdwood 				dev_err(be->dev,
1729103d84a3SLiam Girdwood 					"ASoC: hw_params BE fixup failed %d\n",
173001d7584cSLiam Girdwood 					ret);
173101d7584cSLiam Girdwood 				goto unwind;
173201d7584cSLiam Girdwood 			}
173301d7584cSLiam Girdwood 		}
173401d7584cSLiam Girdwood 
173501d7584cSLiam Girdwood 		ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
173601d7584cSLiam Girdwood 		if (ret < 0) {
173701d7584cSLiam Girdwood 			dev_err(dpcm->be->dev,
1738103d84a3SLiam Girdwood 				"ASoC: hw_params BE failed %d\n", ret);
173901d7584cSLiam Girdwood 			goto unwind;
174001d7584cSLiam Girdwood 		}
174101d7584cSLiam Girdwood 
174201d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
174301d7584cSLiam Girdwood 	}
174401d7584cSLiam Girdwood 	return 0;
174501d7584cSLiam Girdwood 
174601d7584cSLiam Girdwood unwind:
174701d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
174801d7584cSLiam Girdwood 	list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
174901d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
175001d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
175101d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
175201d7584cSLiam Girdwood 
175301d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
175401d7584cSLiam Girdwood 			continue;
175501d7584cSLiam Girdwood 
175601d7584cSLiam Girdwood 		/* only allow hw_free() if no connected FEs are running */
175701d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
175801d7584cSLiam Girdwood 			continue;
175901d7584cSLiam Girdwood 
176001d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
176101d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
176201d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
176301d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
176401d7584cSLiam Girdwood 			continue;
176501d7584cSLiam Girdwood 
176601d7584cSLiam Girdwood 		soc_pcm_hw_free(be_substream);
176701d7584cSLiam Girdwood 	}
176801d7584cSLiam Girdwood 
176901d7584cSLiam Girdwood 	return ret;
177001d7584cSLiam Girdwood }
177101d7584cSLiam Girdwood 
177245c0a188SMark Brown static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
177301d7584cSLiam Girdwood 				 struct snd_pcm_hw_params *params)
177401d7584cSLiam Girdwood {
177501d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
177601d7584cSLiam Girdwood 	int ret, stream = substream->stream;
177701d7584cSLiam Girdwood 
177801d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
177901d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
178001d7584cSLiam Girdwood 
178101d7584cSLiam Girdwood 	memcpy(&fe->dpcm[substream->stream].hw_params, params,
178201d7584cSLiam Girdwood 			sizeof(struct snd_pcm_hw_params));
178301d7584cSLiam Girdwood 	ret = dpcm_be_dai_hw_params(fe, substream->stream);
178401d7584cSLiam Girdwood 	if (ret < 0) {
1785103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
178601d7584cSLiam Girdwood 		goto out;
178701d7584cSLiam Girdwood 	}
178801d7584cSLiam Girdwood 
1789103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
179001d7584cSLiam Girdwood 			fe->dai_link->name, params_rate(params),
179101d7584cSLiam Girdwood 			params_channels(params), params_format(params));
179201d7584cSLiam Girdwood 
179301d7584cSLiam Girdwood 	/* call hw_params on the frontend */
179401d7584cSLiam Girdwood 	ret = soc_pcm_hw_params(substream, params);
179501d7584cSLiam Girdwood 	if (ret < 0) {
1796103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
179701d7584cSLiam Girdwood 		dpcm_be_dai_hw_free(fe, stream);
179801d7584cSLiam Girdwood 	 } else
179901d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
180001d7584cSLiam Girdwood 
180101d7584cSLiam Girdwood out:
180201d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
180301d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
180401d7584cSLiam Girdwood 	return ret;
180501d7584cSLiam Girdwood }
180601d7584cSLiam Girdwood 
180701d7584cSLiam Girdwood static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
180801d7584cSLiam Girdwood 		struct snd_pcm_substream *substream, int cmd)
180901d7584cSLiam Girdwood {
181001d7584cSLiam Girdwood 	int ret;
181101d7584cSLiam Girdwood 
1812103d84a3SLiam Girdwood 	dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
181301d7584cSLiam Girdwood 			dpcm->fe->dai_link->name, cmd);
181401d7584cSLiam Girdwood 
181501d7584cSLiam Girdwood 	ret = soc_pcm_trigger(substream, cmd);
181601d7584cSLiam Girdwood 	if (ret < 0)
1817103d84a3SLiam Girdwood 		dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
181801d7584cSLiam Girdwood 
181901d7584cSLiam Girdwood 	return ret;
182001d7584cSLiam Girdwood }
182101d7584cSLiam Girdwood 
182223607025SLiam Girdwood int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
182345c0a188SMark Brown 			       int cmd)
182401d7584cSLiam Girdwood {
182501d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
182601d7584cSLiam Girdwood 	int ret = 0;
182701d7584cSLiam Girdwood 
182801d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
182901d7584cSLiam Girdwood 
183001d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
183101d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
183201d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
183301d7584cSLiam Girdwood 
183401d7584cSLiam Girdwood 		/* is this op for this BE ? */
183501d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
183601d7584cSLiam Girdwood 			continue;
183701d7584cSLiam Girdwood 
183801d7584cSLiam Girdwood 		switch (cmd) {
183901d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_START:
184001d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
184101d7584cSLiam Girdwood 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
184201d7584cSLiam Girdwood 				continue;
184301d7584cSLiam Girdwood 
184401d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
184501d7584cSLiam Girdwood 			if (ret)
184601d7584cSLiam Girdwood 				return ret;
184701d7584cSLiam Girdwood 
184801d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
184901d7584cSLiam Girdwood 			break;
185001d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_RESUME:
185101d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
185201d7584cSLiam Girdwood 				continue;
185301d7584cSLiam Girdwood 
185401d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
185501d7584cSLiam Girdwood 			if (ret)
185601d7584cSLiam Girdwood 				return ret;
185701d7584cSLiam Girdwood 
185801d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
185901d7584cSLiam Girdwood 			break;
186001d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
186101d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
186201d7584cSLiam Girdwood 				continue;
186301d7584cSLiam Girdwood 
186401d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
186501d7584cSLiam Girdwood 			if (ret)
186601d7584cSLiam Girdwood 				return ret;
186701d7584cSLiam Girdwood 
186801d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
186901d7584cSLiam Girdwood 			break;
187001d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_STOP:
187101d7584cSLiam Girdwood 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
187201d7584cSLiam Girdwood 				continue;
187301d7584cSLiam Girdwood 
187401d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
187501d7584cSLiam Girdwood 				continue;
187601d7584cSLiam Girdwood 
187701d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
187801d7584cSLiam Girdwood 			if (ret)
187901d7584cSLiam Girdwood 				return ret;
188001d7584cSLiam Girdwood 
188101d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
188201d7584cSLiam Girdwood 			break;
188301d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_SUSPEND:
1884868a6ca8SNicolin Chen 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
188501d7584cSLiam Girdwood 				continue;
188601d7584cSLiam Girdwood 
188701d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
188801d7584cSLiam Girdwood 				continue;
188901d7584cSLiam Girdwood 
189001d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
189101d7584cSLiam Girdwood 			if (ret)
189201d7584cSLiam Girdwood 				return ret;
189301d7584cSLiam Girdwood 
189401d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
189501d7584cSLiam Girdwood 			break;
189601d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
189701d7584cSLiam Girdwood 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
189801d7584cSLiam Girdwood 				continue;
189901d7584cSLiam Girdwood 
190001d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
190101d7584cSLiam Girdwood 				continue;
190201d7584cSLiam Girdwood 
190301d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
190401d7584cSLiam Girdwood 			if (ret)
190501d7584cSLiam Girdwood 				return ret;
190601d7584cSLiam Girdwood 
190701d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
190801d7584cSLiam Girdwood 			break;
190901d7584cSLiam Girdwood 		}
191001d7584cSLiam Girdwood 	}
191101d7584cSLiam Girdwood 
191201d7584cSLiam Girdwood 	return ret;
191301d7584cSLiam Girdwood }
191401d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
191501d7584cSLiam Girdwood 
191645c0a188SMark Brown static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
191701d7584cSLiam Girdwood {
191801d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
191901d7584cSLiam Girdwood 	int stream = substream->stream, ret;
192001d7584cSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
192101d7584cSLiam Girdwood 
192201d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
192301d7584cSLiam Girdwood 
192401d7584cSLiam Girdwood 	switch (trigger) {
192501d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_PRE:
192601d7584cSLiam Girdwood 		/* call trigger on the frontend before the backend. */
192701d7584cSLiam Girdwood 
1928103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
192901d7584cSLiam Girdwood 				fe->dai_link->name, cmd);
193001d7584cSLiam Girdwood 
193101d7584cSLiam Girdwood 		ret = soc_pcm_trigger(substream, cmd);
193201d7584cSLiam Girdwood 		if (ret < 0) {
1933103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
193401d7584cSLiam Girdwood 			goto out;
193501d7584cSLiam Girdwood 		}
193601d7584cSLiam Girdwood 
193701d7584cSLiam Girdwood 		ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
193801d7584cSLiam Girdwood 		break;
193901d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_POST:
194001d7584cSLiam Girdwood 		/* call trigger on the frontend after the backend. */
194101d7584cSLiam Girdwood 
194201d7584cSLiam Girdwood 		ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
194301d7584cSLiam Girdwood 		if (ret < 0) {
1944103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
194501d7584cSLiam Girdwood 			goto out;
194601d7584cSLiam Girdwood 		}
194701d7584cSLiam Girdwood 
1948103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
194901d7584cSLiam Girdwood 				fe->dai_link->name, cmd);
195001d7584cSLiam Girdwood 
195101d7584cSLiam Girdwood 		ret = soc_pcm_trigger(substream, cmd);
195201d7584cSLiam Girdwood 		break;
195307bf84aaSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_BESPOKE:
195407bf84aaSLiam Girdwood 		/* bespoke trigger() - handles both FE and BEs */
195507bf84aaSLiam Girdwood 
1956103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
195707bf84aaSLiam Girdwood 				fe->dai_link->name, cmd);
195807bf84aaSLiam Girdwood 
195907bf84aaSLiam Girdwood 		ret = soc_pcm_bespoke_trigger(substream, cmd);
196007bf84aaSLiam Girdwood 		if (ret < 0) {
1961103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
196207bf84aaSLiam Girdwood 			goto out;
196307bf84aaSLiam Girdwood 		}
196407bf84aaSLiam Girdwood 		break;
196501d7584cSLiam Girdwood 	default:
1966103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
196701d7584cSLiam Girdwood 				fe->dai_link->name);
196801d7584cSLiam Girdwood 		ret = -EINVAL;
196901d7584cSLiam Girdwood 		goto out;
197001d7584cSLiam Girdwood 	}
197101d7584cSLiam Girdwood 
197201d7584cSLiam Girdwood 	switch (cmd) {
197301d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
197401d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_RESUME:
197501d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
197601d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
197701d7584cSLiam Girdwood 		break;
197801d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_STOP:
197901d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_SUSPEND:
198001d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
198101d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
198201d7584cSLiam Girdwood 		break;
198301d7584cSLiam Girdwood 	}
198401d7584cSLiam Girdwood 
198501d7584cSLiam Girdwood out:
198601d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
198701d7584cSLiam Girdwood 	return ret;
198801d7584cSLiam Girdwood }
198901d7584cSLiam Girdwood 
199023607025SLiam Girdwood int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
199101d7584cSLiam Girdwood {
199201d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
199301d7584cSLiam Girdwood 	int ret = 0;
199401d7584cSLiam Girdwood 
199501d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
199601d7584cSLiam Girdwood 
199701d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
199801d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
199901d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
200001d7584cSLiam Girdwood 
200101d7584cSLiam Girdwood 		/* is this op for this BE ? */
200201d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
200301d7584cSLiam Girdwood 			continue;
200401d7584cSLiam Girdwood 
200501d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
200601d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
200701d7584cSLiam Girdwood 			continue;
200801d7584cSLiam Girdwood 
2009103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: prepare BE %s\n",
201001d7584cSLiam Girdwood 			dpcm->fe->dai_link->name);
201101d7584cSLiam Girdwood 
201201d7584cSLiam Girdwood 		ret = soc_pcm_prepare(be_substream);
201301d7584cSLiam Girdwood 		if (ret < 0) {
2014103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: backend prepare failed %d\n",
201501d7584cSLiam Girdwood 				ret);
201601d7584cSLiam Girdwood 			break;
201701d7584cSLiam Girdwood 		}
201801d7584cSLiam Girdwood 
201901d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
202001d7584cSLiam Girdwood 	}
202101d7584cSLiam Girdwood 	return ret;
202201d7584cSLiam Girdwood }
202301d7584cSLiam Girdwood 
202445c0a188SMark Brown static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
202501d7584cSLiam Girdwood {
202601d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
202701d7584cSLiam Girdwood 	int stream = substream->stream, ret = 0;
202801d7584cSLiam Girdwood 
202901d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
203001d7584cSLiam Girdwood 
2031103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
203201d7584cSLiam Girdwood 
203301d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
203401d7584cSLiam Girdwood 
203501d7584cSLiam Girdwood 	/* there is no point preparing this FE if there are no BEs */
203601d7584cSLiam Girdwood 	if (list_empty(&fe->dpcm[stream].be_clients)) {
2037103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
203801d7584cSLiam Girdwood 				fe->dai_link->name);
203901d7584cSLiam Girdwood 		ret = -EINVAL;
204001d7584cSLiam Girdwood 		goto out;
204101d7584cSLiam Girdwood 	}
204201d7584cSLiam Girdwood 
204301d7584cSLiam Girdwood 	ret = dpcm_be_dai_prepare(fe, substream->stream);
204401d7584cSLiam Girdwood 	if (ret < 0)
204501d7584cSLiam Girdwood 		goto out;
204601d7584cSLiam Girdwood 
204701d7584cSLiam Girdwood 	/* call prepare on the frontend */
204801d7584cSLiam Girdwood 	ret = soc_pcm_prepare(substream);
204901d7584cSLiam Girdwood 	if (ret < 0) {
2050103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
205101d7584cSLiam Girdwood 			fe->dai_link->name);
205201d7584cSLiam Girdwood 		goto out;
205301d7584cSLiam Girdwood 	}
205401d7584cSLiam Girdwood 
205501d7584cSLiam Girdwood 	/* run the stream event for each BE */
205601d7584cSLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
205701d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
205801d7584cSLiam Girdwood 
205901d7584cSLiam Girdwood out:
206001d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
206101d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
206201d7584cSLiam Girdwood 
206301d7584cSLiam Girdwood 	return ret;
206401d7584cSLiam Girdwood }
206501d7584cSLiam Girdwood 
2066be3f3f2cSLiam Girdwood static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2067be3f3f2cSLiam Girdwood 		     unsigned int cmd, void *arg)
2068be3f3f2cSLiam Girdwood {
2069be3f3f2cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
2070be3f3f2cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
2071be3f3f2cSLiam Girdwood 
2072c5914b0aSMark Brown 	if (platform->driver->ops && platform->driver->ops->ioctl)
2073be3f3f2cSLiam Girdwood 		return platform->driver->ops->ioctl(substream, cmd, arg);
2074be3f3f2cSLiam Girdwood 	return snd_pcm_lib_ioctl(substream, cmd, arg);
2075be3f3f2cSLiam Girdwood }
2076be3f3f2cSLiam Girdwood 
2077618dae11SLiam Girdwood static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2078618dae11SLiam Girdwood {
207907bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
208007bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
208107bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2082618dae11SLiam Girdwood 	int err;
208301d7584cSLiam Girdwood 
2084103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2085618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2086618dae11SLiam Girdwood 
208707bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
208807bf84aaSLiam Girdwood 		/* call bespoke trigger - FE takes care of all BE triggers */
2089103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
209007bf84aaSLiam Girdwood 				fe->dai_link->name);
209107bf84aaSLiam Girdwood 
209207bf84aaSLiam Girdwood 		err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
209307bf84aaSLiam Girdwood 		if (err < 0)
2094103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
209507bf84aaSLiam Girdwood 	} else {
2096103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
209707bf84aaSLiam Girdwood 			fe->dai_link->name);
209807bf84aaSLiam Girdwood 
2099618dae11SLiam Girdwood 		err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2100618dae11SLiam Girdwood 		if (err < 0)
2101103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
210207bf84aaSLiam Girdwood 	}
2103618dae11SLiam Girdwood 
2104618dae11SLiam Girdwood 	err = dpcm_be_dai_hw_free(fe, stream);
2105618dae11SLiam Girdwood 	if (err < 0)
2106103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2107618dae11SLiam Girdwood 
2108618dae11SLiam Girdwood 	err = dpcm_be_dai_shutdown(fe, stream);
2109618dae11SLiam Girdwood 	if (err < 0)
2110103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2111618dae11SLiam Girdwood 
2112618dae11SLiam Girdwood 	/* run the stream event for each BE */
2113618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2114618dae11SLiam Girdwood 
2115618dae11SLiam Girdwood 	return 0;
2116618dae11SLiam Girdwood }
2117618dae11SLiam Girdwood 
2118618dae11SLiam Girdwood static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2119618dae11SLiam Girdwood {
212007bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
212107bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
2122618dae11SLiam Girdwood 	struct snd_soc_dpcm *dpcm;
212307bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2124618dae11SLiam Girdwood 	int ret;
2125618dae11SLiam Girdwood 
2126103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2127618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2128618dae11SLiam Girdwood 
2129618dae11SLiam Girdwood 	/* Only start the BE if the FE is ready */
2130618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2131618dae11SLiam Girdwood 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2132618dae11SLiam Girdwood 		return -EINVAL;
2133618dae11SLiam Girdwood 
2134618dae11SLiam Girdwood 	/* startup must always be called for new BEs */
2135618dae11SLiam Girdwood 	ret = dpcm_be_dai_startup(fe, stream);
2136fffc0ca2SDan Carpenter 	if (ret < 0)
2137618dae11SLiam Girdwood 		goto disconnect;
2138618dae11SLiam Girdwood 
2139618dae11SLiam Girdwood 	/* keep going if FE state is > open */
2140618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2141618dae11SLiam Girdwood 		return 0;
2142618dae11SLiam Girdwood 
2143618dae11SLiam Girdwood 	ret = dpcm_be_dai_hw_params(fe, stream);
2144fffc0ca2SDan Carpenter 	if (ret < 0)
2145618dae11SLiam Girdwood 		goto close;
2146618dae11SLiam Girdwood 
2147618dae11SLiam Girdwood 	/* keep going if FE state is > hw_params */
2148618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2149618dae11SLiam Girdwood 		return 0;
2150618dae11SLiam Girdwood 
2151618dae11SLiam Girdwood 
2152618dae11SLiam Girdwood 	ret = dpcm_be_dai_prepare(fe, stream);
2153fffc0ca2SDan Carpenter 	if (ret < 0)
2154618dae11SLiam Girdwood 		goto hw_free;
2155618dae11SLiam Girdwood 
2156618dae11SLiam Girdwood 	/* run the stream event for each BE */
2157618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2158618dae11SLiam Girdwood 
2159618dae11SLiam Girdwood 	/* keep going if FE state is > prepare */
2160618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2161618dae11SLiam Girdwood 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2162618dae11SLiam Girdwood 		return 0;
2163618dae11SLiam Girdwood 
216407bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
216507bf84aaSLiam Girdwood 		/* call trigger on the frontend - FE takes care of all BE triggers */
2166103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
216707bf84aaSLiam Girdwood 				fe->dai_link->name);
216807bf84aaSLiam Girdwood 
216907bf84aaSLiam Girdwood 		ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
217007bf84aaSLiam Girdwood 		if (ret < 0) {
2171103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
217207bf84aaSLiam Girdwood 			goto hw_free;
217307bf84aaSLiam Girdwood 		}
217407bf84aaSLiam Girdwood 	} else {
2175103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2176618dae11SLiam Girdwood 			fe->dai_link->name);
2177618dae11SLiam Girdwood 
2178618dae11SLiam Girdwood 		ret = dpcm_be_dai_trigger(fe, stream,
2179618dae11SLiam Girdwood 					SNDRV_PCM_TRIGGER_START);
2180618dae11SLiam Girdwood 		if (ret < 0) {
2181103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2182618dae11SLiam Girdwood 			goto hw_free;
2183618dae11SLiam Girdwood 		}
218407bf84aaSLiam Girdwood 	}
2185618dae11SLiam Girdwood 
2186618dae11SLiam Girdwood 	return 0;
2187618dae11SLiam Girdwood 
2188618dae11SLiam Girdwood hw_free:
2189618dae11SLiam Girdwood 	dpcm_be_dai_hw_free(fe, stream);
2190618dae11SLiam Girdwood close:
2191618dae11SLiam Girdwood 	dpcm_be_dai_shutdown(fe, stream);
2192618dae11SLiam Girdwood disconnect:
2193618dae11SLiam Girdwood 	/* disconnect any non started BEs */
2194618dae11SLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2195618dae11SLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
2196618dae11SLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2197618dae11SLiam Girdwood 				dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2198618dae11SLiam Girdwood 	}
2199618dae11SLiam Girdwood 
2200618dae11SLiam Girdwood 	return ret;
2201618dae11SLiam Girdwood }
2202618dae11SLiam Girdwood 
2203618dae11SLiam Girdwood static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2204618dae11SLiam Girdwood {
2205618dae11SLiam Girdwood 	int ret;
2206618dae11SLiam Girdwood 
2207618dae11SLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
2208618dae11SLiam Girdwood 	ret = dpcm_run_update_startup(fe, stream);
2209618dae11SLiam Girdwood 	if (ret < 0)
2210103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2211618dae11SLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2212618dae11SLiam Girdwood 
2213618dae11SLiam Girdwood 	return ret;
2214618dae11SLiam Girdwood }
2215618dae11SLiam Girdwood 
2216618dae11SLiam Girdwood static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2217618dae11SLiam Girdwood {
2218618dae11SLiam Girdwood 	int ret;
2219618dae11SLiam Girdwood 
2220618dae11SLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
2221618dae11SLiam Girdwood 	ret = dpcm_run_update_shutdown(fe, stream);
2222618dae11SLiam Girdwood 	if (ret < 0)
2223103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2224618dae11SLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2225618dae11SLiam Girdwood 
2226618dae11SLiam Girdwood 	return ret;
2227618dae11SLiam Girdwood }
2228618dae11SLiam Girdwood 
2229618dae11SLiam Girdwood /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2230618dae11SLiam Girdwood  * any DAI links.
2231618dae11SLiam Girdwood  */
2232c3f48ae6SLars-Peter Clausen int soc_dpcm_runtime_update(struct snd_soc_card *card)
2233618dae11SLiam Girdwood {
2234618dae11SLiam Girdwood 	int i, old, new, paths;
2235618dae11SLiam Girdwood 
2236618dae11SLiam Girdwood 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2237618dae11SLiam Girdwood 	for (i = 0; i < card->num_rtd; i++) {
2238618dae11SLiam Girdwood 		struct snd_soc_dapm_widget_list *list;
2239618dae11SLiam Girdwood 		struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2240618dae11SLiam Girdwood 
2241618dae11SLiam Girdwood 		/* make sure link is FE */
2242618dae11SLiam Girdwood 		if (!fe->dai_link->dynamic)
2243618dae11SLiam Girdwood 			continue;
2244618dae11SLiam Girdwood 
2245618dae11SLiam Girdwood 		/* only check active links */
2246618dae11SLiam Girdwood 		if (!fe->cpu_dai->active)
2247618dae11SLiam Girdwood 			continue;
2248618dae11SLiam Girdwood 
2249618dae11SLiam Girdwood 		/* DAPM sync will call this to update DSP paths */
2250103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
2251618dae11SLiam Girdwood 			fe->dai_link->name);
2252618dae11SLiam Girdwood 
2253618dae11SLiam Girdwood 		/* skip if FE doesn't have playback capability */
2254618dae11SLiam Girdwood 		if (!fe->cpu_dai->driver->playback.channels_min)
2255618dae11SLiam Girdwood 			goto capture;
2256618dae11SLiam Girdwood 
2257618dae11SLiam Girdwood 		paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2258618dae11SLiam Girdwood 		if (paths < 0) {
2259103d84a3SLiam Girdwood 			dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2260618dae11SLiam Girdwood 					fe->dai_link->name,  "playback");
2261618dae11SLiam Girdwood 			mutex_unlock(&card->mutex);
2262618dae11SLiam Girdwood 			return paths;
2263618dae11SLiam Girdwood 		}
2264618dae11SLiam Girdwood 
2265618dae11SLiam Girdwood 		/* update any new playback paths */
2266618dae11SLiam Girdwood 		new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2267618dae11SLiam Girdwood 		if (new) {
2268618dae11SLiam Girdwood 			dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2269618dae11SLiam Girdwood 			dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2270618dae11SLiam Girdwood 			dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2271618dae11SLiam Girdwood 		}
2272618dae11SLiam Girdwood 
2273618dae11SLiam Girdwood 		/* update any old playback paths */
2274618dae11SLiam Girdwood 		old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2275618dae11SLiam Girdwood 		if (old) {
2276618dae11SLiam Girdwood 			dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2277618dae11SLiam Girdwood 			dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2278618dae11SLiam Girdwood 			dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2279618dae11SLiam Girdwood 		}
2280618dae11SLiam Girdwood 
22817ed9de76SQiao Zhou 		dpcm_path_put(&list);
2282618dae11SLiam Girdwood capture:
2283618dae11SLiam Girdwood 		/* skip if FE doesn't have capture capability */
2284618dae11SLiam Girdwood 		if (!fe->cpu_dai->driver->capture.channels_min)
2285618dae11SLiam Girdwood 			continue;
2286618dae11SLiam Girdwood 
2287618dae11SLiam Girdwood 		paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2288618dae11SLiam Girdwood 		if (paths < 0) {
2289103d84a3SLiam Girdwood 			dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2290618dae11SLiam Girdwood 					fe->dai_link->name,  "capture");
2291618dae11SLiam Girdwood 			mutex_unlock(&card->mutex);
2292618dae11SLiam Girdwood 			return paths;
2293618dae11SLiam Girdwood 		}
2294618dae11SLiam Girdwood 
2295618dae11SLiam Girdwood 		/* update any new capture paths */
2296618dae11SLiam Girdwood 		new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2297618dae11SLiam Girdwood 		if (new) {
2298618dae11SLiam Girdwood 			dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2299618dae11SLiam Girdwood 			dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2300618dae11SLiam Girdwood 			dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2301618dae11SLiam Girdwood 		}
2302618dae11SLiam Girdwood 
2303618dae11SLiam Girdwood 		/* update any old capture paths */
2304618dae11SLiam Girdwood 		old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2305618dae11SLiam Girdwood 		if (old) {
2306618dae11SLiam Girdwood 			dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2307618dae11SLiam Girdwood 			dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2308618dae11SLiam Girdwood 			dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2309618dae11SLiam Girdwood 		}
2310618dae11SLiam Girdwood 
2311618dae11SLiam Girdwood 		dpcm_path_put(&list);
2312618dae11SLiam Girdwood 	}
2313618dae11SLiam Girdwood 
2314618dae11SLiam Girdwood 	mutex_unlock(&card->mutex);
2315618dae11SLiam Girdwood 	return 0;
2316618dae11SLiam Girdwood }
231701d7584cSLiam Girdwood int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
231801d7584cSLiam Girdwood {
231901d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
232001d7584cSLiam Girdwood 	struct list_head *clients =
232101d7584cSLiam Girdwood 		&fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
232201d7584cSLiam Girdwood 
232301d7584cSLiam Girdwood 	list_for_each_entry(dpcm, clients, list_be) {
232401d7584cSLiam Girdwood 
232501d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
23262e5894d7SBenoit Cousson 		int i;
232701d7584cSLiam Girdwood 
232801d7584cSLiam Girdwood 		if (be->dai_link->ignore_suspend)
232901d7584cSLiam Girdwood 			continue;
233001d7584cSLiam Girdwood 
23312e5894d7SBenoit Cousson 		for (i = 0; i < be->num_codecs; i++) {
23322e5894d7SBenoit Cousson 			struct snd_soc_dai *dai = be->codec_dais[i];
23332e5894d7SBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
233401d7584cSLiam Girdwood 
23352e5894d7SBenoit Cousson 			dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
23362e5894d7SBenoit Cousson 					 be->dai_link->name);
23372e5894d7SBenoit Cousson 
23382e5894d7SBenoit Cousson 			if (drv->ops && drv->ops->digital_mute &&
23392e5894d7SBenoit Cousson 							dai->playback_active)
234001d7584cSLiam Girdwood 				drv->ops->digital_mute(dai, mute);
234101d7584cSLiam Girdwood 		}
23422e5894d7SBenoit Cousson 	}
234301d7584cSLiam Girdwood 
234401d7584cSLiam Girdwood 	return 0;
234501d7584cSLiam Girdwood }
234601d7584cSLiam Girdwood 
234745c0a188SMark Brown static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
234801d7584cSLiam Girdwood {
234901d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
235001d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
235101d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list;
235201d7584cSLiam Girdwood 	int ret;
235301d7584cSLiam Girdwood 	int stream = fe_substream->stream;
235401d7584cSLiam Girdwood 
235501d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
235601d7584cSLiam Girdwood 	fe->dpcm[stream].runtime = fe_substream->runtime;
235701d7584cSLiam Girdwood 
23588f70e515SQiao Zhou 	ret = dpcm_path_get(fe, stream, &list);
23598f70e515SQiao Zhou 	if (ret < 0) {
23608f70e515SQiao Zhou 		mutex_unlock(&fe->card->mutex);
23618f70e515SQiao Zhou 		return ret;
23628f70e515SQiao Zhou 	} else if (ret == 0) {
2363103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
236401d7584cSLiam Girdwood 			fe->dai_link->name, stream ? "capture" : "playback");
236501d7584cSLiam Girdwood 	}
236601d7584cSLiam Girdwood 
236701d7584cSLiam Girdwood 	/* calculate valid and active FE <-> BE dpcms */
236801d7584cSLiam Girdwood 	dpcm_process_paths(fe, stream, &list, 1);
236901d7584cSLiam Girdwood 
237001d7584cSLiam Girdwood 	ret = dpcm_fe_dai_startup(fe_substream);
237101d7584cSLiam Girdwood 	if (ret < 0) {
237201d7584cSLiam Girdwood 		/* clean up all links */
237301d7584cSLiam Girdwood 		list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
237401d7584cSLiam Girdwood 			dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
237501d7584cSLiam Girdwood 
237601d7584cSLiam Girdwood 		dpcm_be_disconnect(fe, stream);
237701d7584cSLiam Girdwood 		fe->dpcm[stream].runtime = NULL;
237801d7584cSLiam Girdwood 	}
237901d7584cSLiam Girdwood 
238001d7584cSLiam Girdwood 	dpcm_clear_pending_state(fe, stream);
238101d7584cSLiam Girdwood 	dpcm_path_put(&list);
238201d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
238301d7584cSLiam Girdwood 	return ret;
238401d7584cSLiam Girdwood }
238501d7584cSLiam Girdwood 
238645c0a188SMark Brown static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
238701d7584cSLiam Girdwood {
238801d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
238901d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
239001d7584cSLiam Girdwood 	int stream = fe_substream->stream, ret;
239101d7584cSLiam Girdwood 
239201d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
239301d7584cSLiam Girdwood 	ret = dpcm_fe_dai_shutdown(fe_substream);
239401d7584cSLiam Girdwood 
239501d7584cSLiam Girdwood 	/* mark FE's links ready to prune */
239601d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
239701d7584cSLiam Girdwood 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
239801d7584cSLiam Girdwood 
239901d7584cSLiam Girdwood 	dpcm_be_disconnect(fe, stream);
240001d7584cSLiam Girdwood 
240101d7584cSLiam Girdwood 	fe->dpcm[stream].runtime = NULL;
240201d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
240301d7584cSLiam Girdwood 	return ret;
240401d7584cSLiam Girdwood }
240501d7584cSLiam Girdwood 
2406ddee627cSLiam Girdwood /* create a new pcm */
2407ddee627cSLiam Girdwood int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2408ddee627cSLiam Girdwood {
2409ddee627cSLiam Girdwood 	struct snd_soc_platform *platform = rtd->platform;
24102e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
2411ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2412ddee627cSLiam Girdwood 	struct snd_pcm *pcm;
2413ddee627cSLiam Girdwood 	char new_name[64];
2414ddee627cSLiam Girdwood 	int ret = 0, playback = 0, capture = 0;
24152e5894d7SBenoit Cousson 	int i;
2416ddee627cSLiam Girdwood 
241701d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
24181e9de42fSLiam Girdwood 		playback = rtd->dai_link->dpcm_playback;
24191e9de42fSLiam Girdwood 		capture = rtd->dai_link->dpcm_capture;
242001d7584cSLiam Girdwood 	} else {
24212e5894d7SBenoit Cousson 		for (i = 0; i < rtd->num_codecs; i++) {
24222e5894d7SBenoit Cousson 			codec_dai = rtd->codec_dais[i];
24232e5894d7SBenoit Cousson 			if (codec_dai->driver->playback.channels_min)
2424ddee627cSLiam Girdwood 				playback = 1;
24252e5894d7SBenoit Cousson 			if (codec_dai->driver->capture.channels_min)
2426ddee627cSLiam Girdwood 				capture = 1;
242701d7584cSLiam Girdwood 		}
2428ddee627cSLiam Girdwood 
24292e5894d7SBenoit Cousson 		capture = capture && cpu_dai->driver->capture.channels_min;
24302e5894d7SBenoit Cousson 		playback = playback && cpu_dai->driver->playback.channels_min;
24312e5894d7SBenoit Cousson 	}
24322e5894d7SBenoit Cousson 
2433d6bead02SFabio Estevam 	if (rtd->dai_link->playback_only) {
2434d6bead02SFabio Estevam 		playback = 1;
2435d6bead02SFabio Estevam 		capture = 0;
2436d6bead02SFabio Estevam 	}
2437d6bead02SFabio Estevam 
2438d6bead02SFabio Estevam 	if (rtd->dai_link->capture_only) {
2439d6bead02SFabio Estevam 		playback = 0;
2440d6bead02SFabio Estevam 		capture = 1;
2441d6bead02SFabio Estevam 	}
2442d6bead02SFabio Estevam 
244301d7584cSLiam Girdwood 	/* create the PCM */
244401d7584cSLiam Girdwood 	if (rtd->dai_link->no_pcm) {
244501d7584cSLiam Girdwood 		snprintf(new_name, sizeof(new_name), "(%s)",
244601d7584cSLiam Girdwood 			rtd->dai_link->stream_name);
244701d7584cSLiam Girdwood 
244801d7584cSLiam Girdwood 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
244901d7584cSLiam Girdwood 				playback, capture, &pcm);
245001d7584cSLiam Girdwood 	} else {
245101d7584cSLiam Girdwood 		if (rtd->dai_link->dynamic)
245201d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s (*)",
245301d7584cSLiam Girdwood 				rtd->dai_link->stream_name);
245401d7584cSLiam Girdwood 		else
245501d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s %s-%d",
24562e5894d7SBenoit Cousson 				rtd->dai_link->stream_name,
24572e5894d7SBenoit Cousson 				(rtd->num_codecs > 1) ?
24582e5894d7SBenoit Cousson 				"multicodec" : rtd->codec_dai->name, num);
245901d7584cSLiam Girdwood 
246001d7584cSLiam Girdwood 		ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
246101d7584cSLiam Girdwood 			capture, &pcm);
246201d7584cSLiam Girdwood 	}
2463ddee627cSLiam Girdwood 	if (ret < 0) {
2464103d84a3SLiam Girdwood 		dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
24655cb9b748SLiam Girdwood 			rtd->dai_link->name);
2466ddee627cSLiam Girdwood 		return ret;
2467ddee627cSLiam Girdwood 	}
2468103d84a3SLiam Girdwood 	dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2469ddee627cSLiam Girdwood 
2470ddee627cSLiam Girdwood 	/* DAPM dai link stream work */
2471ddee627cSLiam Girdwood 	INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2472ddee627cSLiam Girdwood 
2473ddee627cSLiam Girdwood 	rtd->pcm = pcm;
2474ddee627cSLiam Girdwood 	pcm->private_data = rtd;
247501d7584cSLiam Girdwood 
247601d7584cSLiam Girdwood 	if (rtd->dai_link->no_pcm) {
247701d7584cSLiam Girdwood 		if (playback)
247801d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
247901d7584cSLiam Girdwood 		if (capture)
248001d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
248101d7584cSLiam Girdwood 		goto out;
248201d7584cSLiam Girdwood 	}
248301d7584cSLiam Girdwood 
248401d7584cSLiam Girdwood 	/* ASoC PCM operations */
248501d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic) {
248601d7584cSLiam Girdwood 		rtd->ops.open		= dpcm_fe_dai_open;
248701d7584cSLiam Girdwood 		rtd->ops.hw_params	= dpcm_fe_dai_hw_params;
248801d7584cSLiam Girdwood 		rtd->ops.prepare	= dpcm_fe_dai_prepare;
248901d7584cSLiam Girdwood 		rtd->ops.trigger	= dpcm_fe_dai_trigger;
249001d7584cSLiam Girdwood 		rtd->ops.hw_free	= dpcm_fe_dai_hw_free;
249101d7584cSLiam Girdwood 		rtd->ops.close		= dpcm_fe_dai_close;
249201d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
2493be3f3f2cSLiam Girdwood 		rtd->ops.ioctl		= soc_pcm_ioctl;
249401d7584cSLiam Girdwood 	} else {
249501d7584cSLiam Girdwood 		rtd->ops.open		= soc_pcm_open;
249601d7584cSLiam Girdwood 		rtd->ops.hw_params	= soc_pcm_hw_params;
249701d7584cSLiam Girdwood 		rtd->ops.prepare	= soc_pcm_prepare;
249801d7584cSLiam Girdwood 		rtd->ops.trigger	= soc_pcm_trigger;
249901d7584cSLiam Girdwood 		rtd->ops.hw_free	= soc_pcm_hw_free;
250001d7584cSLiam Girdwood 		rtd->ops.close		= soc_pcm_close;
250101d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
2502be3f3f2cSLiam Girdwood 		rtd->ops.ioctl		= soc_pcm_ioctl;
250301d7584cSLiam Girdwood 	}
250401d7584cSLiam Girdwood 
2505ddee627cSLiam Girdwood 	if (platform->driver->ops) {
250601d7584cSLiam Girdwood 		rtd->ops.ack		= platform->driver->ops->ack;
250701d7584cSLiam Girdwood 		rtd->ops.copy		= platform->driver->ops->copy;
250801d7584cSLiam Girdwood 		rtd->ops.silence	= platform->driver->ops->silence;
250901d7584cSLiam Girdwood 		rtd->ops.page		= platform->driver->ops->page;
251001d7584cSLiam Girdwood 		rtd->ops.mmap		= platform->driver->ops->mmap;
2511ddee627cSLiam Girdwood 	}
2512ddee627cSLiam Girdwood 
2513ddee627cSLiam Girdwood 	if (playback)
251401d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2515ddee627cSLiam Girdwood 
2516ddee627cSLiam Girdwood 	if (capture)
251701d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2518ddee627cSLiam Girdwood 
2519ddee627cSLiam Girdwood 	if (platform->driver->pcm_new) {
2520ddee627cSLiam Girdwood 		ret = platform->driver->pcm_new(rtd);
2521ddee627cSLiam Girdwood 		if (ret < 0) {
2522b1bc7b3cSMark Brown 			dev_err(platform->dev,
2523b1bc7b3cSMark Brown 				"ASoC: pcm constructor failed: %d\n",
2524b1bc7b3cSMark Brown 				ret);
2525ddee627cSLiam Girdwood 			return ret;
2526ddee627cSLiam Girdwood 		}
2527ddee627cSLiam Girdwood 	}
2528ddee627cSLiam Girdwood 
2529ddee627cSLiam Girdwood 	pcm->private_free = platform->driver->pcm_free;
253001d7584cSLiam Girdwood out:
25312e5894d7SBenoit Cousson 	dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
25322e5894d7SBenoit Cousson 		 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2533ddee627cSLiam Girdwood 		 cpu_dai->name);
2534ddee627cSLiam Girdwood 	return ret;
2535ddee627cSLiam Girdwood }
253601d7584cSLiam Girdwood 
253701d7584cSLiam Girdwood /* is the current PCM operation for this FE ? */
253801d7584cSLiam Girdwood int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
253901d7584cSLiam Girdwood {
254001d7584cSLiam Girdwood 	if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
254101d7584cSLiam Girdwood 		return 1;
254201d7584cSLiam Girdwood 	return 0;
254301d7584cSLiam Girdwood }
254401d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
254501d7584cSLiam Girdwood 
254601d7584cSLiam Girdwood /* is the current PCM operation for this BE ? */
254701d7584cSLiam Girdwood int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
254801d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
254901d7584cSLiam Girdwood {
255001d7584cSLiam Girdwood 	if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
255101d7584cSLiam Girdwood 	   ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
255201d7584cSLiam Girdwood 		  be->dpcm[stream].runtime_update))
255301d7584cSLiam Girdwood 		return 1;
255401d7584cSLiam Girdwood 	return 0;
255501d7584cSLiam Girdwood }
255601d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
255701d7584cSLiam Girdwood 
255801d7584cSLiam Girdwood /* get the substream for this BE */
255901d7584cSLiam Girdwood struct snd_pcm_substream *
256001d7584cSLiam Girdwood 	snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
256101d7584cSLiam Girdwood {
256201d7584cSLiam Girdwood 	return be->pcm->streams[stream].substream;
256301d7584cSLiam Girdwood }
256401d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
256501d7584cSLiam Girdwood 
256601d7584cSLiam Girdwood /* get the BE runtime state */
256701d7584cSLiam Girdwood enum snd_soc_dpcm_state
256801d7584cSLiam Girdwood 	snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
256901d7584cSLiam Girdwood {
257001d7584cSLiam Girdwood 	return be->dpcm[stream].state;
257101d7584cSLiam Girdwood }
257201d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
257301d7584cSLiam Girdwood 
257401d7584cSLiam Girdwood /* set the BE runtime state */
257501d7584cSLiam Girdwood void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
257601d7584cSLiam Girdwood 		int stream, enum snd_soc_dpcm_state state)
257701d7584cSLiam Girdwood {
257801d7584cSLiam Girdwood 	be->dpcm[stream].state = state;
257901d7584cSLiam Girdwood }
258001d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
258101d7584cSLiam Girdwood 
258201d7584cSLiam Girdwood /*
258301d7584cSLiam Girdwood  * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
258401d7584cSLiam Girdwood  * are not running, paused or suspended for the specified stream direction.
258501d7584cSLiam Girdwood  */
258601d7584cSLiam Girdwood int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
258701d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
258801d7584cSLiam Girdwood {
258901d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
259001d7584cSLiam Girdwood 	int state;
259101d7584cSLiam Girdwood 
259201d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
259301d7584cSLiam Girdwood 
259401d7584cSLiam Girdwood 		if (dpcm->fe == fe)
259501d7584cSLiam Girdwood 			continue;
259601d7584cSLiam Girdwood 
259701d7584cSLiam Girdwood 		state = dpcm->fe->dpcm[stream].state;
259801d7584cSLiam Girdwood 		if (state == SND_SOC_DPCM_STATE_START ||
259901d7584cSLiam Girdwood 			state == SND_SOC_DPCM_STATE_PAUSED ||
260001d7584cSLiam Girdwood 			state == SND_SOC_DPCM_STATE_SUSPEND)
260101d7584cSLiam Girdwood 			return 0;
260201d7584cSLiam Girdwood 	}
260301d7584cSLiam Girdwood 
260401d7584cSLiam Girdwood 	/* it's safe to free/stop this BE DAI */
260501d7584cSLiam Girdwood 	return 1;
260601d7584cSLiam Girdwood }
260701d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
260801d7584cSLiam Girdwood 
260901d7584cSLiam Girdwood /*
261001d7584cSLiam Girdwood  * We can only change hw params a BE DAI if any of it's FE are not prepared,
261101d7584cSLiam Girdwood  * running, paused or suspended for the specified stream direction.
261201d7584cSLiam Girdwood  */
261301d7584cSLiam Girdwood int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
261401d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
261501d7584cSLiam Girdwood {
261601d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
261701d7584cSLiam Girdwood 	int state;
261801d7584cSLiam Girdwood 
261901d7584cSLiam Girdwood 	list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
262001d7584cSLiam Girdwood 
262101d7584cSLiam Girdwood 		if (dpcm->fe == fe)
262201d7584cSLiam Girdwood 			continue;
262301d7584cSLiam Girdwood 
262401d7584cSLiam Girdwood 		state = dpcm->fe->dpcm[stream].state;
262501d7584cSLiam Girdwood 		if (state == SND_SOC_DPCM_STATE_START ||
262601d7584cSLiam Girdwood 			state == SND_SOC_DPCM_STATE_PAUSED ||
262701d7584cSLiam Girdwood 			state == SND_SOC_DPCM_STATE_SUSPEND ||
262801d7584cSLiam Girdwood 			state == SND_SOC_DPCM_STATE_PREPARE)
262901d7584cSLiam Girdwood 			return 0;
263001d7584cSLiam Girdwood 	}
263101d7584cSLiam Girdwood 
263201d7584cSLiam Girdwood 	/* it's safe to change hw_params */
263301d7584cSLiam Girdwood 	return 1;
263401d7584cSLiam Girdwood }
263501d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2636f86dcef8SLiam Girdwood 
263707bf84aaSLiam Girdwood int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
263807bf84aaSLiam Girdwood 		int cmd, struct snd_soc_platform *platform)
263907bf84aaSLiam Girdwood {
2640c5914b0aSMark Brown 	if (platform->driver->ops && platform->driver->ops->trigger)
264107bf84aaSLiam Girdwood 		return platform->driver->ops->trigger(substream, cmd);
264207bf84aaSLiam Girdwood 	return 0;
264307bf84aaSLiam Girdwood }
264407bf84aaSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
264507bf84aaSLiam Girdwood 
2646f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS
2647f86dcef8SLiam Girdwood static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2648f86dcef8SLiam Girdwood {
2649f86dcef8SLiam Girdwood 	switch (state) {
2650f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_NEW:
2651f86dcef8SLiam Girdwood 		return "new";
2652f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_OPEN:
2653f86dcef8SLiam Girdwood 		return "open";
2654f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_HW_PARAMS:
2655f86dcef8SLiam Girdwood 		return "hw_params";
2656f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_PREPARE:
2657f86dcef8SLiam Girdwood 		return "prepare";
2658f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_START:
2659f86dcef8SLiam Girdwood 		return "start";
2660f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_STOP:
2661f86dcef8SLiam Girdwood 		return "stop";
2662f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_SUSPEND:
2663f86dcef8SLiam Girdwood 		return "suspend";
2664f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_PAUSED:
2665f86dcef8SLiam Girdwood 		return "paused";
2666f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_HW_FREE:
2667f86dcef8SLiam Girdwood 		return "hw_free";
2668f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_CLOSE:
2669f86dcef8SLiam Girdwood 		return "close";
2670f86dcef8SLiam Girdwood 	}
2671f86dcef8SLiam Girdwood 
2672f86dcef8SLiam Girdwood 	return "unknown";
2673f86dcef8SLiam Girdwood }
2674f86dcef8SLiam Girdwood 
2675f86dcef8SLiam Girdwood static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2676f86dcef8SLiam Girdwood 				int stream, char *buf, size_t size)
2677f86dcef8SLiam Girdwood {
2678f86dcef8SLiam Girdwood 	struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2679f86dcef8SLiam Girdwood 	struct snd_soc_dpcm *dpcm;
2680f86dcef8SLiam Girdwood 	ssize_t offset = 0;
2681f86dcef8SLiam Girdwood 
2682f86dcef8SLiam Girdwood 	/* FE state */
2683f86dcef8SLiam Girdwood 	offset += snprintf(buf + offset, size - offset,
2684f86dcef8SLiam Girdwood 			"[%s - %s]\n", fe->dai_link->name,
2685f86dcef8SLiam Girdwood 			stream ? "Capture" : "Playback");
2686f86dcef8SLiam Girdwood 
2687f86dcef8SLiam Girdwood 	offset += snprintf(buf + offset, size - offset, "State: %s\n",
2688f86dcef8SLiam Girdwood 	                dpcm_state_string(fe->dpcm[stream].state));
2689f86dcef8SLiam Girdwood 
2690f86dcef8SLiam Girdwood 	if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2691f86dcef8SLiam Girdwood 	    (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2692f86dcef8SLiam Girdwood 		offset += snprintf(buf + offset, size - offset,
2693f86dcef8SLiam Girdwood 				"Hardware Params: "
2694f86dcef8SLiam Girdwood 				"Format = %s, Channels = %d, Rate = %d\n",
2695f86dcef8SLiam Girdwood 				snd_pcm_format_name(params_format(params)),
2696f86dcef8SLiam Girdwood 				params_channels(params),
2697f86dcef8SLiam Girdwood 				params_rate(params));
2698f86dcef8SLiam Girdwood 
2699f86dcef8SLiam Girdwood 	/* BEs state */
2700f86dcef8SLiam Girdwood 	offset += snprintf(buf + offset, size - offset, "Backends:\n");
2701f86dcef8SLiam Girdwood 
2702f86dcef8SLiam Girdwood 	if (list_empty(&fe->dpcm[stream].be_clients)) {
2703f86dcef8SLiam Girdwood 		offset += snprintf(buf + offset, size - offset,
2704f86dcef8SLiam Girdwood 				" No active DSP links\n");
2705f86dcef8SLiam Girdwood 		goto out;
2706f86dcef8SLiam Girdwood 	}
2707f86dcef8SLiam Girdwood 
2708f86dcef8SLiam Girdwood 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2709f86dcef8SLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
2710f86dcef8SLiam Girdwood 		params = &dpcm->hw_params;
2711f86dcef8SLiam Girdwood 
2712f86dcef8SLiam Girdwood 		offset += snprintf(buf + offset, size - offset,
2713f86dcef8SLiam Girdwood 				"- %s\n", be->dai_link->name);
2714f86dcef8SLiam Girdwood 
2715f86dcef8SLiam Girdwood 		offset += snprintf(buf + offset, size - offset,
2716f86dcef8SLiam Girdwood 				"   State: %s\n",
2717f86dcef8SLiam Girdwood 				dpcm_state_string(be->dpcm[stream].state));
2718f86dcef8SLiam Girdwood 
2719f86dcef8SLiam Girdwood 		if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2720f86dcef8SLiam Girdwood 		    (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2721f86dcef8SLiam Girdwood 			offset += snprintf(buf + offset, size - offset,
2722f86dcef8SLiam Girdwood 				"   Hardware Params: "
2723f86dcef8SLiam Girdwood 				"Format = %s, Channels = %d, Rate = %d\n",
2724f86dcef8SLiam Girdwood 				snd_pcm_format_name(params_format(params)),
2725f86dcef8SLiam Girdwood 				params_channels(params),
2726f86dcef8SLiam Girdwood 				params_rate(params));
2727f86dcef8SLiam Girdwood 	}
2728f86dcef8SLiam Girdwood 
2729f86dcef8SLiam Girdwood out:
2730f86dcef8SLiam Girdwood 	return offset;
2731f86dcef8SLiam Girdwood }
2732f86dcef8SLiam Girdwood 
2733f86dcef8SLiam Girdwood static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2734f86dcef8SLiam Girdwood 				size_t count, loff_t *ppos)
2735f86dcef8SLiam Girdwood {
2736f86dcef8SLiam Girdwood 	struct snd_soc_pcm_runtime *fe = file->private_data;
2737f86dcef8SLiam Girdwood 	ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2738f86dcef8SLiam Girdwood 	char *buf;
2739f86dcef8SLiam Girdwood 
2740f86dcef8SLiam Girdwood 	buf = kmalloc(out_count, GFP_KERNEL);
2741f86dcef8SLiam Girdwood 	if (!buf)
2742f86dcef8SLiam Girdwood 		return -ENOMEM;
2743f86dcef8SLiam Girdwood 
2744f86dcef8SLiam Girdwood 	if (fe->cpu_dai->driver->playback.channels_min)
2745f86dcef8SLiam Girdwood 		offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2746f86dcef8SLiam Girdwood 					buf + offset, out_count - offset);
2747f86dcef8SLiam Girdwood 
2748f86dcef8SLiam Girdwood 	if (fe->cpu_dai->driver->capture.channels_min)
2749f86dcef8SLiam Girdwood 		offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2750f86dcef8SLiam Girdwood 					buf + offset, out_count - offset);
2751f86dcef8SLiam Girdwood 
2752f86dcef8SLiam Girdwood 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2753f86dcef8SLiam Girdwood 
2754f86dcef8SLiam Girdwood 	kfree(buf);
2755f86dcef8SLiam Girdwood 	return ret;
2756f86dcef8SLiam Girdwood }
2757f86dcef8SLiam Girdwood 
2758f86dcef8SLiam Girdwood static const struct file_operations dpcm_state_fops = {
2759f57b8488SLiam Girdwood 	.open = simple_open,
2760f86dcef8SLiam Girdwood 	.read = dpcm_state_read_file,
2761f86dcef8SLiam Girdwood 	.llseek = default_llseek,
2762f86dcef8SLiam Girdwood };
2763f86dcef8SLiam Girdwood 
2764f86dcef8SLiam Girdwood int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2765f86dcef8SLiam Girdwood {
2766b3bba9a1SMark Brown 	if (!rtd->dai_link)
2767b3bba9a1SMark Brown 		return 0;
2768b3bba9a1SMark Brown 
2769f86dcef8SLiam Girdwood 	rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2770f86dcef8SLiam Girdwood 			rtd->card->debugfs_card_root);
2771f86dcef8SLiam Girdwood 	if (!rtd->debugfs_dpcm_root) {
2772f86dcef8SLiam Girdwood 		dev_dbg(rtd->dev,
2773f86dcef8SLiam Girdwood 			 "ASoC: Failed to create dpcm debugfs directory %s\n",
2774f86dcef8SLiam Girdwood 			 rtd->dai_link->name);
2775f86dcef8SLiam Girdwood 		return -EINVAL;
2776f86dcef8SLiam Girdwood 	}
2777f86dcef8SLiam Girdwood 
2778f57b8488SLiam Girdwood 	rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2779f86dcef8SLiam Girdwood 						rtd->debugfs_dpcm_root,
2780f86dcef8SLiam Girdwood 						rtd, &dpcm_state_fops);
2781f86dcef8SLiam Girdwood 
2782f86dcef8SLiam Girdwood 	return 0;
2783f86dcef8SLiam Girdwood }
2784f86dcef8SLiam Girdwood #endif
2785