xref: /openbmc/linux/sound/soc/soc-pcm.c (revision 7ec6b431)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // soc-pcm.c  --  ALSA SoC PCM
4 //
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Copyright 2005 Openedhand Ltd.
7 // Copyright (C) 2010 Slimlogic Ltd.
8 // Copyright (C) 2010 Texas Instruments Inc.
9 //
10 // Authors: Liam Girdwood <lrg@ti.com>
11 //          Mark Brown <broonie@opensource.wolfsonmicro.com>
12 
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/slab.h>
19 #include <linux/workqueue.h>
20 #include <linux/export.h>
21 #include <linux/debugfs.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dpcm.h>
27 #include <sound/initval.h>
28 
29 #define DPCM_MAX_BE_USERS	8
30 
31 /**
32  * snd_soc_runtime_activate() - Increment active count for PCM runtime components
33  * @rtd: ASoC PCM runtime that is activated
34  * @stream: Direction of the PCM stream
35  *
36  * Increments the active count for all the DAIs and components attached to a PCM
37  * runtime. Should typically be called when a stream is opened.
38  *
39  * Must be called with the rtd->card->pcm_mutex being held
40  */
41 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
42 {
43 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
44 	struct snd_soc_dai *codec_dai;
45 	int i;
46 
47 	lockdep_assert_held(&rtd->card->pcm_mutex);
48 
49 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
50 		cpu_dai->playback_active++;
51 		for_each_rtd_codec_dai(rtd, i, codec_dai)
52 			codec_dai->playback_active++;
53 	} else {
54 		cpu_dai->capture_active++;
55 		for_each_rtd_codec_dai(rtd, i, codec_dai)
56 			codec_dai->capture_active++;
57 	}
58 
59 	cpu_dai->active++;
60 	cpu_dai->component->active++;
61 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
62 		codec_dai->active++;
63 		codec_dai->component->active++;
64 	}
65 }
66 
67 /**
68  * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
69  * @rtd: ASoC PCM runtime that is deactivated
70  * @stream: Direction of the PCM stream
71  *
72  * Decrements the active count for all the DAIs and components attached to a PCM
73  * runtime. Should typically be called when a stream is closed.
74  *
75  * Must be called with the rtd->card->pcm_mutex being held
76  */
77 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
78 {
79 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
80 	struct snd_soc_dai *codec_dai;
81 	int i;
82 
83 	lockdep_assert_held(&rtd->card->pcm_mutex);
84 
85 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
86 		cpu_dai->playback_active--;
87 		for_each_rtd_codec_dai(rtd, i, codec_dai)
88 			codec_dai->playback_active--;
89 	} else {
90 		cpu_dai->capture_active--;
91 		for_each_rtd_codec_dai(rtd, i, codec_dai)
92 			codec_dai->capture_active--;
93 	}
94 
95 	cpu_dai->active--;
96 	cpu_dai->component->active--;
97 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
98 		codec_dai->component->active--;
99 		codec_dai->active--;
100 	}
101 }
102 
103 /**
104  * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
105  * @rtd: The ASoC PCM runtime that should be checked.
106  *
107  * This function checks whether the power down delay should be ignored for a
108  * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
109  * been configured to ignore the delay, or if none of the components benefits
110  * from having the delay.
111  */
112 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
113 {
114 	struct snd_soc_component *component;
115 	bool ignore = true;
116 	int i;
117 
118 	if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
119 		return true;
120 
121 	for_each_rtd_components(rtd, i, component)
122 		ignore &= !component->driver->use_pmdown_time;
123 
124 	return ignore;
125 }
126 
127 /**
128  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
129  * @substream: the pcm substream
130  * @hw: the hardware parameters
131  *
132  * Sets the substream runtime hardware parameters.
133  */
134 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
135 	const struct snd_pcm_hardware *hw)
136 {
137 	struct snd_pcm_runtime *runtime = substream->runtime;
138 	runtime->hw.info = hw->info;
139 	runtime->hw.formats = hw->formats;
140 	runtime->hw.period_bytes_min = hw->period_bytes_min;
141 	runtime->hw.period_bytes_max = hw->period_bytes_max;
142 	runtime->hw.periods_min = hw->periods_min;
143 	runtime->hw.periods_max = hw->periods_max;
144 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
145 	runtime->hw.fifo_size = hw->fifo_size;
146 	return 0;
147 }
148 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
149 
150 /* DPCM stream event, send event to FE and all active BEs. */
151 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
152 	int event)
153 {
154 	struct snd_soc_dpcm *dpcm;
155 
156 	for_each_dpcm_be(fe, dir, dpcm) {
157 
158 		struct snd_soc_pcm_runtime *be = dpcm->be;
159 
160 		dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
161 				be->dai_link->name, event, dir);
162 
163 		if ((event == SND_SOC_DAPM_STREAM_STOP) &&
164 		    (be->dpcm[dir].users >= 1))
165 			continue;
166 
167 		snd_soc_dapm_stream_event(be, dir, event);
168 	}
169 
170 	snd_soc_dapm_stream_event(fe, dir, event);
171 
172 	return 0;
173 }
174 
175 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
176 					struct snd_soc_dai *soc_dai)
177 {
178 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
179 	int ret;
180 
181 	if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
182 				rtd->dai_link->symmetric_rates)) {
183 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
184 				soc_dai->rate);
185 
186 		ret = snd_pcm_hw_constraint_single(substream->runtime,
187 						SNDRV_PCM_HW_PARAM_RATE,
188 						soc_dai->rate);
189 		if (ret < 0) {
190 			dev_err(soc_dai->dev,
191 				"ASoC: Unable to apply rate constraint: %d\n",
192 				ret);
193 			return ret;
194 		}
195 	}
196 
197 	if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
198 				rtd->dai_link->symmetric_channels)) {
199 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
200 				soc_dai->channels);
201 
202 		ret = snd_pcm_hw_constraint_single(substream->runtime,
203 						SNDRV_PCM_HW_PARAM_CHANNELS,
204 						soc_dai->channels);
205 		if (ret < 0) {
206 			dev_err(soc_dai->dev,
207 				"ASoC: Unable to apply channel symmetry constraint: %d\n",
208 				ret);
209 			return ret;
210 		}
211 	}
212 
213 	if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
214 				rtd->dai_link->symmetric_samplebits)) {
215 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
216 				soc_dai->sample_bits);
217 
218 		ret = snd_pcm_hw_constraint_single(substream->runtime,
219 						SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
220 						soc_dai->sample_bits);
221 		if (ret < 0) {
222 			dev_err(soc_dai->dev,
223 				"ASoC: Unable to apply sample bits symmetry constraint: %d\n",
224 				ret);
225 			return ret;
226 		}
227 	}
228 
229 	return 0;
230 }
231 
232 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
233 				struct snd_pcm_hw_params *params)
234 {
235 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
236 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
237 	struct snd_soc_dai *codec_dai;
238 	unsigned int rate, channels, sample_bits, symmetry, i;
239 
240 	rate = params_rate(params);
241 	channels = params_channels(params);
242 	sample_bits = snd_pcm_format_physical_width(params_format(params));
243 
244 	/* reject unmatched parameters when applying symmetry */
245 	symmetry = cpu_dai->driver->symmetric_rates ||
246 		rtd->dai_link->symmetric_rates;
247 
248 	for_each_rtd_codec_dai(rtd, i, codec_dai)
249 		symmetry |= codec_dai->driver->symmetric_rates;
250 
251 	if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
252 		dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
253 				cpu_dai->rate, rate);
254 		return -EINVAL;
255 	}
256 
257 	symmetry = cpu_dai->driver->symmetric_channels ||
258 		rtd->dai_link->symmetric_channels;
259 
260 	for_each_rtd_codec_dai(rtd, i, codec_dai)
261 		symmetry |= codec_dai->driver->symmetric_channels;
262 
263 	if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
264 		dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
265 				cpu_dai->channels, channels);
266 		return -EINVAL;
267 	}
268 
269 	symmetry = cpu_dai->driver->symmetric_samplebits ||
270 		rtd->dai_link->symmetric_samplebits;
271 
272 	for_each_rtd_codec_dai(rtd, i, codec_dai)
273 		symmetry |= codec_dai->driver->symmetric_samplebits;
274 
275 	if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
276 		dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
277 				cpu_dai->sample_bits, sample_bits);
278 		return -EINVAL;
279 	}
280 
281 	return 0;
282 }
283 
284 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
285 {
286 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
287 	struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
288 	struct snd_soc_dai_link *link = rtd->dai_link;
289 	struct snd_soc_dai *codec_dai;
290 	unsigned int symmetry, i;
291 
292 	symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
293 		cpu_driver->symmetric_channels || link->symmetric_channels ||
294 		cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
295 
296 	for_each_rtd_codec_dai(rtd, i, codec_dai)
297 		symmetry = symmetry ||
298 			codec_dai->driver->symmetric_rates ||
299 			codec_dai->driver->symmetric_channels ||
300 			codec_dai->driver->symmetric_samplebits;
301 
302 	return symmetry;
303 }
304 
305 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
306 {
307 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
308 	int ret;
309 
310 	if (!bits)
311 		return;
312 
313 	ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
314 	if (ret != 0)
315 		dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
316 				 bits, ret);
317 }
318 
319 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
320 {
321 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
322 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
323 	struct snd_soc_dai *codec_dai;
324 	int i;
325 	unsigned int bits = 0, cpu_bits;
326 
327 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
328 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
329 			if (codec_dai->driver->playback.sig_bits == 0) {
330 				bits = 0;
331 				break;
332 			}
333 			bits = max(codec_dai->driver->playback.sig_bits, bits);
334 		}
335 		cpu_bits = cpu_dai->driver->playback.sig_bits;
336 	} else {
337 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
338 			if (codec_dai->driver->capture.sig_bits == 0) {
339 				bits = 0;
340 				break;
341 			}
342 			bits = max(codec_dai->driver->capture.sig_bits, bits);
343 		}
344 		cpu_bits = cpu_dai->driver->capture.sig_bits;
345 	}
346 
347 	soc_pcm_set_msb(substream, bits);
348 	soc_pcm_set_msb(substream, cpu_bits);
349 }
350 
351 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
352 {
353 	struct snd_pcm_runtime *runtime = substream->runtime;
354 	struct snd_pcm_hardware *hw = &runtime->hw;
355 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
356 	struct snd_soc_dai *codec_dai;
357 	struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
358 	struct snd_soc_dai_driver *codec_dai_drv;
359 	struct snd_soc_pcm_stream *codec_stream;
360 	struct snd_soc_pcm_stream *cpu_stream;
361 	unsigned int chan_min = 0, chan_max = UINT_MAX;
362 	unsigned int rate_min = 0, rate_max = UINT_MAX;
363 	unsigned int rates = UINT_MAX;
364 	u64 formats = ULLONG_MAX;
365 	int i;
366 
367 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
368 		cpu_stream = &cpu_dai_drv->playback;
369 	else
370 		cpu_stream = &cpu_dai_drv->capture;
371 
372 	/* first calculate min/max only for CODECs in the DAI link */
373 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
374 
375 		/*
376 		 * Skip CODECs which don't support the current stream type.
377 		 * Otherwise, since the rate, channel, and format values will
378 		 * zero in that case, we would have no usable settings left,
379 		 * causing the resulting setup to fail.
380 		 * At least one CODEC should match, otherwise we should have
381 		 * bailed out on a higher level, since there would be no
382 		 * CODEC to support the transfer direction in that case.
383 		 */
384 		if (!snd_soc_dai_stream_valid(codec_dai,
385 					      substream->stream))
386 			continue;
387 
388 		codec_dai_drv = codec_dai->driver;
389 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
390 			codec_stream = &codec_dai_drv->playback;
391 		else
392 			codec_stream = &codec_dai_drv->capture;
393 		chan_min = max(chan_min, codec_stream->channels_min);
394 		chan_max = min(chan_max, codec_stream->channels_max);
395 		rate_min = max(rate_min, codec_stream->rate_min);
396 		rate_max = min_not_zero(rate_max, codec_stream->rate_max);
397 		formats &= codec_stream->formats;
398 		rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
399 	}
400 
401 	/*
402 	 * chan min/max cannot be enforced if there are multiple CODEC DAIs
403 	 * connected to a single CPU DAI, use CPU DAI's directly and let
404 	 * channel allocation be fixed up later
405 	 */
406 	if (rtd->num_codecs > 1) {
407 		chan_min = cpu_stream->channels_min;
408 		chan_max = cpu_stream->channels_max;
409 	}
410 
411 	hw->channels_min = max(chan_min, cpu_stream->channels_min);
412 	hw->channels_max = min(chan_max, cpu_stream->channels_max);
413 	if (hw->formats)
414 		hw->formats &= formats & cpu_stream->formats;
415 	else
416 		hw->formats = formats & cpu_stream->formats;
417 	hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
418 
419 	snd_pcm_limit_hw_rates(runtime);
420 
421 	hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
422 	hw->rate_min = max(hw->rate_min, rate_min);
423 	hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
424 	hw->rate_max = min_not_zero(hw->rate_max, rate_max);
425 }
426 
427 static int soc_pcm_components_open(struct snd_pcm_substream *substream,
428 				   struct snd_soc_component **last)
429 {
430 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
431 	struct snd_soc_component *component;
432 	int i, ret = 0;
433 
434 	for_each_rtd_components(rtd, i, component) {
435 		*last = component;
436 
437 		ret = snd_soc_component_module_get_when_open(component);
438 		if (ret < 0) {
439 			dev_err(component->dev,
440 				"ASoC: can't get module %s\n",
441 				component->name);
442 			return ret;
443 		}
444 
445 		ret = snd_soc_component_open(component, substream);
446 		if (ret < 0) {
447 			dev_err(component->dev,
448 				"ASoC: can't open component %s: %d\n",
449 				component->name, ret);
450 			return ret;
451 		}
452 	}
453 	*last = NULL;
454 	return 0;
455 }
456 
457 static int soc_pcm_components_close(struct snd_pcm_substream *substream,
458 				    struct snd_soc_component *last)
459 {
460 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
461 	struct snd_soc_component *component;
462 	int i, ret = 0;
463 
464 	for_each_rtd_components(rtd, i, component) {
465 		if (component == last)
466 			break;
467 
468 		ret |= snd_soc_component_close(component, substream);
469 		snd_soc_component_module_put_when_close(component);
470 	}
471 
472 	return ret;
473 }
474 
475 /*
476  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
477  * then initialized and any private data can be allocated. This also calls
478  * startup for the cpu DAI, component, machine and codec DAI.
479  */
480 static int soc_pcm_open(struct snd_pcm_substream *substream)
481 {
482 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
483 	struct snd_pcm_runtime *runtime = substream->runtime;
484 	struct snd_soc_component *component;
485 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
486 	struct snd_soc_dai *codec_dai;
487 	const char *codec_dai_name = "multicodec";
488 	int i, ret = 0;
489 
490 	for_each_rtd_components(rtd, i, component)
491 		pinctrl_pm_select_default_state(component->dev);
492 
493 	for_each_rtd_components(rtd, i, component)
494 		pm_runtime_get_sync(component->dev);
495 
496 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
497 
498 	/* startup the audio subsystem */
499 	ret = snd_soc_dai_startup(cpu_dai, substream);
500 	if (ret < 0) {
501 		dev_err(cpu_dai->dev, "ASoC: can't open interface %s: %d\n",
502 			cpu_dai->name, ret);
503 		goto out;
504 	}
505 
506 	ret = soc_pcm_components_open(substream, &component);
507 	if (ret < 0)
508 		goto component_err;
509 
510 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
511 		ret = snd_soc_dai_startup(codec_dai, substream);
512 		if (ret < 0) {
513 			dev_err(codec_dai->dev,
514 				"ASoC: can't open codec %s: %d\n",
515 				codec_dai->name, ret);
516 			goto codec_dai_err;
517 		}
518 
519 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
520 			codec_dai->tx_mask = 0;
521 		else
522 			codec_dai->rx_mask = 0;
523 	}
524 
525 	if (rtd->dai_link->ops->startup) {
526 		ret = rtd->dai_link->ops->startup(substream);
527 		if (ret < 0) {
528 			pr_err("ASoC: %s startup failed: %d\n",
529 			       rtd->dai_link->name, ret);
530 			goto machine_err;
531 		}
532 	}
533 
534 	/* Dynamic PCM DAI links compat checks use dynamic capabilities */
535 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
536 		goto dynamic;
537 
538 	/* Check that the codec and cpu DAIs are compatible */
539 	soc_pcm_init_runtime_hw(substream);
540 
541 	if (rtd->num_codecs == 1)
542 		codec_dai_name = rtd->codec_dai->name;
543 
544 	if (soc_pcm_has_symmetry(substream))
545 		runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
546 
547 	ret = -EINVAL;
548 	if (!runtime->hw.rates) {
549 		printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
550 			codec_dai_name, cpu_dai->name);
551 		goto config_err;
552 	}
553 	if (!runtime->hw.formats) {
554 		printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
555 			codec_dai_name, cpu_dai->name);
556 		goto config_err;
557 	}
558 	if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
559 	    runtime->hw.channels_min > runtime->hw.channels_max) {
560 		printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
561 				codec_dai_name, cpu_dai->name);
562 		goto config_err;
563 	}
564 
565 	soc_pcm_apply_msb(substream);
566 
567 	/* Symmetry only applies if we've already got an active stream. */
568 	if (cpu_dai->active) {
569 		ret = soc_pcm_apply_symmetry(substream, cpu_dai);
570 		if (ret != 0)
571 			goto config_err;
572 	}
573 
574 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
575 		if (codec_dai->active) {
576 			ret = soc_pcm_apply_symmetry(substream, codec_dai);
577 			if (ret != 0)
578 				goto config_err;
579 		}
580 	}
581 
582 	pr_debug("ASoC: %s <-> %s info:\n",
583 			codec_dai_name, cpu_dai->name);
584 	pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
585 	pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
586 		 runtime->hw.channels_max);
587 	pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
588 		 runtime->hw.rate_max);
589 
590 dynamic:
591 
592 	snd_soc_runtime_activate(rtd, substream->stream);
593 
594 	mutex_unlock(&rtd->card->pcm_mutex);
595 	return 0;
596 
597 config_err:
598 	if (rtd->dai_link->ops->shutdown)
599 		rtd->dai_link->ops->shutdown(substream);
600 
601 machine_err:
602 	i = rtd->num_codecs;
603 
604 codec_dai_err:
605 	for_each_rtd_codec_dai_rollback(rtd, i, codec_dai)
606 		snd_soc_dai_shutdown(codec_dai, substream);
607 
608 component_err:
609 	soc_pcm_components_close(substream, component);
610 
611 	snd_soc_dai_shutdown(cpu_dai, substream);
612 out:
613 	mutex_unlock(&rtd->card->pcm_mutex);
614 
615 	for_each_rtd_components(rtd, i, component) {
616 		pm_runtime_mark_last_busy(component->dev);
617 		pm_runtime_put_autosuspend(component->dev);
618 	}
619 
620 	for_each_rtd_components(rtd, i, component)
621 		if (!component->active)
622 			pinctrl_pm_select_sleep_state(component->dev);
623 
624 	return ret;
625 }
626 
627 static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
628 {
629 	/*
630 	 * Currently nothing to do for c2c links
631 	 * Since c2c links are internal nodes in the DAPM graph and
632 	 * don't interface with the outside world or application layer
633 	 * we don't have to do any special handling on close.
634 	 */
635 }
636 
637 /*
638  * Called by ALSA when a PCM substream is closed. Private data can be
639  * freed here. The cpu DAI, codec DAI, machine and components are also
640  * shutdown.
641  */
642 static int soc_pcm_close(struct snd_pcm_substream *substream)
643 {
644 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
645 	struct snd_soc_component *component;
646 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
647 	struct snd_soc_dai *codec_dai;
648 	int i;
649 
650 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
651 
652 	snd_soc_runtime_deactivate(rtd, substream->stream);
653 
654 	/* clear the corresponding DAIs rate when inactive */
655 	if (!cpu_dai->active)
656 		cpu_dai->rate = 0;
657 
658 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
659 		if (!codec_dai->active)
660 			codec_dai->rate = 0;
661 	}
662 
663 	snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
664 
665 	snd_soc_dai_shutdown(cpu_dai, substream);
666 
667 	for_each_rtd_codec_dai(rtd, i, codec_dai)
668 		snd_soc_dai_shutdown(codec_dai, substream);
669 
670 	if (rtd->dai_link->ops->shutdown)
671 		rtd->dai_link->ops->shutdown(substream);
672 
673 	soc_pcm_components_close(substream, NULL);
674 
675 	snd_soc_dapm_stream_stop(rtd, substream->stream);
676 
677 	mutex_unlock(&rtd->card->pcm_mutex);
678 
679 	for_each_rtd_components(rtd, i, component) {
680 		pm_runtime_mark_last_busy(component->dev);
681 		pm_runtime_put_autosuspend(component->dev);
682 	}
683 
684 	for_each_rtd_components(rtd, i, component)
685 		if (!component->active)
686 			pinctrl_pm_select_sleep_state(component->dev);
687 
688 	return 0;
689 }
690 
691 /*
692  * Called by ALSA when the PCM substream is prepared, can set format, sample
693  * rate, etc.  This function is non atomic and can be called multiple times,
694  * it can refer to the runtime info.
695  */
696 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
697 {
698 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
699 	struct snd_soc_component *component;
700 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
701 	struct snd_soc_dai *codec_dai;
702 	int i, ret = 0;
703 
704 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
705 
706 	if (rtd->dai_link->ops->prepare) {
707 		ret = rtd->dai_link->ops->prepare(substream);
708 		if (ret < 0) {
709 			dev_err(rtd->card->dev, "ASoC: machine prepare error:"
710 				" %d\n", ret);
711 			goto out;
712 		}
713 	}
714 
715 	for_each_rtd_components(rtd, i, component) {
716 		ret = snd_soc_component_prepare(component, substream);
717 		if (ret < 0) {
718 			dev_err(component->dev,
719 				"ASoC: platform prepare error: %d\n", ret);
720 			goto out;
721 		}
722 	}
723 
724 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
725 		ret = snd_soc_dai_prepare(codec_dai, substream);
726 		if (ret < 0) {
727 			dev_err(codec_dai->dev,
728 				"ASoC: codec DAI prepare error: %d\n",
729 				ret);
730 			goto out;
731 		}
732 	}
733 
734 	ret = snd_soc_dai_prepare(cpu_dai, substream);
735 	if (ret < 0) {
736 		dev_err(cpu_dai->dev,
737 			"ASoC: cpu DAI prepare error: %d\n", ret);
738 		goto out;
739 	}
740 
741 	/* cancel any delayed stream shutdown that is pending */
742 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
743 	    rtd->pop_wait) {
744 		rtd->pop_wait = 0;
745 		cancel_delayed_work(&rtd->delayed_work);
746 	}
747 
748 	snd_soc_dapm_stream_event(rtd, substream->stream,
749 			SND_SOC_DAPM_STREAM_START);
750 
751 	for_each_rtd_codec_dai(rtd, i, codec_dai)
752 		snd_soc_dai_digital_mute(codec_dai, 0,
753 					 substream->stream);
754 	snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
755 
756 out:
757 	mutex_unlock(&rtd->card->pcm_mutex);
758 	return ret;
759 }
760 
761 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
762 				       unsigned int mask)
763 {
764 	struct snd_interval *interval;
765 	int channels = hweight_long(mask);
766 
767 	interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
768 	interval->min = channels;
769 	interval->max = channels;
770 }
771 
772 static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
773 				      struct snd_soc_component *last)
774 {
775 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
776 	struct snd_soc_component *component;
777 	int i, ret = 0;
778 
779 	for_each_rtd_components(rtd, i, component) {
780 		if (component == last)
781 			break;
782 
783 		ret |= snd_soc_component_hw_free(component, substream);
784 	}
785 
786 	return ret;
787 }
788 
789 /*
790  * Called by ALSA when the hardware params are set by application. This
791  * function can also be called multiple times and can allocate buffers
792  * (using snd_pcm_lib_* ). It's non-atomic.
793  */
794 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
795 				struct snd_pcm_hw_params *params)
796 {
797 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
798 	struct snd_soc_component *component;
799 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
800 	struct snd_soc_dai *codec_dai;
801 	int i, ret = 0;
802 
803 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
804 
805 	ret = soc_pcm_params_symmetry(substream, params);
806 	if (ret)
807 		goto out;
808 
809 	if (rtd->dai_link->ops->hw_params) {
810 		ret = rtd->dai_link->ops->hw_params(substream, params);
811 		if (ret < 0) {
812 			dev_err(rtd->card->dev, "ASoC: machine hw_params"
813 				" failed: %d\n", ret);
814 			goto out;
815 		}
816 	}
817 
818 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
819 		struct snd_pcm_hw_params codec_params;
820 
821 		/*
822 		 * Skip CODECs which don't support the current stream type,
823 		 * the idea being that if a CODEC is not used for the currently
824 		 * set up transfer direction, it should not need to be
825 		 * configured, especially since the configuration used might
826 		 * not even be supported by that CODEC. There may be cases
827 		 * however where a CODEC needs to be set up although it is
828 		 * actually not being used for the transfer, e.g. if a
829 		 * capture-only CODEC is acting as an LRCLK and/or BCLK master
830 		 * for the DAI link including a playback-only CODEC.
831 		 * If this becomes necessary, we will have to augment the
832 		 * machine driver setup with information on how to act, so
833 		 * we can do the right thing here.
834 		 */
835 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
836 			continue;
837 
838 		/* copy params for each codec */
839 		codec_params = *params;
840 
841 		/* fixup params based on TDM slot masks */
842 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
843 		    codec_dai->tx_mask)
844 			soc_pcm_codec_params_fixup(&codec_params,
845 						   codec_dai->tx_mask);
846 
847 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
848 		    codec_dai->rx_mask)
849 			soc_pcm_codec_params_fixup(&codec_params,
850 						   codec_dai->rx_mask);
851 
852 		ret = snd_soc_dai_hw_params(codec_dai, substream,
853 					    &codec_params);
854 		if(ret < 0)
855 			goto codec_err;
856 
857 		codec_dai->rate = params_rate(&codec_params);
858 		codec_dai->channels = params_channels(&codec_params);
859 		codec_dai->sample_bits = snd_pcm_format_physical_width(
860 						params_format(&codec_params));
861 
862 		snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
863 	}
864 
865 	ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
866 	if (ret < 0)
867 		goto interface_err;
868 
869 	/* store the parameters for each DAIs */
870 	cpu_dai->rate = params_rate(params);
871 	cpu_dai->channels = params_channels(params);
872 	cpu_dai->sample_bits =
873 		snd_pcm_format_physical_width(params_format(params));
874 
875 	snd_soc_dapm_update_dai(substream, params, cpu_dai);
876 
877 	for_each_rtd_components(rtd, i, component) {
878 		ret = snd_soc_component_hw_params(component, substream, params);
879 		if (ret < 0) {
880 			dev_err(component->dev,
881 				"ASoC: %s hw params failed: %d\n",
882 				component->name, ret);
883 			goto component_err;
884 		}
885 	}
886 	component = NULL;
887 
888 out:
889 	mutex_unlock(&rtd->card->pcm_mutex);
890 	return ret;
891 
892 component_err:
893 	soc_pcm_components_hw_free(substream, component);
894 
895 	snd_soc_dai_hw_free(cpu_dai, substream);
896 	cpu_dai->rate = 0;
897 
898 interface_err:
899 	i = rtd->num_codecs;
900 
901 codec_err:
902 	for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
903 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
904 			continue;
905 
906 		snd_soc_dai_hw_free(codec_dai, substream);
907 		codec_dai->rate = 0;
908 	}
909 
910 	if (rtd->dai_link->ops->hw_free)
911 		rtd->dai_link->ops->hw_free(substream);
912 
913 	mutex_unlock(&rtd->card->pcm_mutex);
914 	return ret;
915 }
916 
917 /*
918  * Frees resources allocated by hw_params, can be called multiple times
919  */
920 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
921 {
922 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
923 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
924 	struct snd_soc_dai *codec_dai;
925 	bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
926 	int i;
927 
928 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
929 
930 	/* clear the corresponding DAIs parameters when going to be inactive */
931 	if (cpu_dai->active == 1) {
932 		cpu_dai->rate = 0;
933 		cpu_dai->channels = 0;
934 		cpu_dai->sample_bits = 0;
935 	}
936 
937 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
938 		if (codec_dai->active == 1) {
939 			codec_dai->rate = 0;
940 			codec_dai->channels = 0;
941 			codec_dai->sample_bits = 0;
942 		}
943 	}
944 
945 	/* apply codec digital mute */
946 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
947 		if ((playback && codec_dai->playback_active == 1) ||
948 		    (!playback && codec_dai->capture_active == 1))
949 			snd_soc_dai_digital_mute(codec_dai, 1,
950 						 substream->stream);
951 	}
952 
953 	/* free any machine hw params */
954 	if (rtd->dai_link->ops->hw_free)
955 		rtd->dai_link->ops->hw_free(substream);
956 
957 	/* free any component resources */
958 	soc_pcm_components_hw_free(substream, NULL);
959 
960 	/* now free hw params for the DAIs  */
961 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
962 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
963 			continue;
964 
965 		snd_soc_dai_hw_free(codec_dai, substream);
966 	}
967 
968 	snd_soc_dai_hw_free(cpu_dai, substream);
969 
970 	mutex_unlock(&rtd->card->pcm_mutex);
971 	return 0;
972 }
973 
974 static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd)
975 {
976 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
977 	struct snd_soc_component *component;
978 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
979 	struct snd_soc_dai *codec_dai;
980 	int i, ret;
981 
982 	if (rtd->dai_link->ops->trigger) {
983 		ret = rtd->dai_link->ops->trigger(substream, cmd);
984 		if (ret < 0)
985 			return ret;
986 	}
987 
988 	for_each_rtd_components(rtd, i, component) {
989 		ret = snd_soc_component_trigger(component, substream, cmd);
990 		if (ret < 0)
991 			return ret;
992 	}
993 
994 	ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
995 	if (ret < 0)
996 		return ret;
997 
998 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
999 		ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
1000 		if (ret < 0)
1001 			return ret;
1002 	}
1003 
1004 	return 0;
1005 }
1006 
1007 static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd)
1008 {
1009 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1010 	struct snd_soc_component *component;
1011 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1012 	struct snd_soc_dai *codec_dai;
1013 	int i, ret;
1014 
1015 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1016 		ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
1017 		if (ret < 0)
1018 			return ret;
1019 	}
1020 
1021 	ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
1022 	if (ret < 0)
1023 		return ret;
1024 
1025 	for_each_rtd_components(rtd, i, component) {
1026 		ret = snd_soc_component_trigger(component, substream, cmd);
1027 		if (ret < 0)
1028 			return ret;
1029 	}
1030 
1031 	if (rtd->dai_link->ops->trigger) {
1032 		ret = rtd->dai_link->ops->trigger(substream, cmd);
1033 		if (ret < 0)
1034 			return ret;
1035 	}
1036 
1037 	return 0;
1038 }
1039 
1040 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1041 {
1042 	int ret;
1043 
1044 	switch (cmd) {
1045 	case SNDRV_PCM_TRIGGER_START:
1046 	case SNDRV_PCM_TRIGGER_RESUME:
1047 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1048 		ret = soc_pcm_trigger_start(substream, cmd);
1049 		break;
1050 	case SNDRV_PCM_TRIGGER_STOP:
1051 	case SNDRV_PCM_TRIGGER_SUSPEND:
1052 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1053 		ret = soc_pcm_trigger_stop(substream, cmd);
1054 		break;
1055 	default:
1056 		return -EINVAL;
1057 	}
1058 
1059 	return ret;
1060 }
1061 
1062 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1063 				   int cmd)
1064 {
1065 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1066 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1067 	struct snd_soc_dai *codec_dai;
1068 	int i, ret;
1069 
1070 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1071 		ret = snd_soc_dai_bespoke_trigger(codec_dai, substream, cmd);
1072 		if (ret < 0)
1073 			return ret;
1074 	}
1075 
1076 	ret = snd_soc_dai_bespoke_trigger(cpu_dai, substream, cmd);
1077 	if (ret < 0)
1078 		return ret;
1079 
1080 	return 0;
1081 }
1082 /*
1083  * soc level wrapper for pointer callback
1084  * If cpu_dai, codec_dai, component driver has the delay callback, then
1085  * the runtime->delay will be updated accordingly.
1086  */
1087 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1088 {
1089 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1090 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1091 	struct snd_soc_dai *codec_dai;
1092 	struct snd_pcm_runtime *runtime = substream->runtime;
1093 	snd_pcm_uframes_t offset = 0;
1094 	snd_pcm_sframes_t delay = 0;
1095 	snd_pcm_sframes_t codec_delay = 0;
1096 	int i;
1097 
1098 	/* clearing the previous total delay */
1099 	runtime->delay = 0;
1100 
1101 	offset = snd_soc_pcm_component_pointer(substream);
1102 
1103 	/* base delay if assigned in pointer callback */
1104 	delay = runtime->delay;
1105 
1106 	delay += snd_soc_dai_delay(cpu_dai, substream);
1107 
1108 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1109 		codec_delay = max(codec_delay,
1110 				  snd_soc_dai_delay(codec_dai, substream));
1111 	}
1112 	delay += codec_delay;
1113 
1114 	runtime->delay = delay;
1115 
1116 	return offset;
1117 }
1118 
1119 /* connect a FE and BE */
1120 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1121 		struct snd_soc_pcm_runtime *be, int stream)
1122 {
1123 	struct snd_soc_dpcm *dpcm;
1124 	unsigned long flags;
1125 #ifdef CONFIG_DEBUG_FS
1126 	char *name;
1127 #endif
1128 
1129 	/* only add new dpcms */
1130 	for_each_dpcm_be(fe, stream, dpcm) {
1131 		if (dpcm->be == be && dpcm->fe == fe)
1132 			return 0;
1133 	}
1134 
1135 	dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1136 	if (!dpcm)
1137 		return -ENOMEM;
1138 
1139 	dpcm->be = be;
1140 	dpcm->fe = fe;
1141 	be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1142 	dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1143 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1144 	list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1145 	list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1146 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1147 
1148 	dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1149 			stream ? "capture" : "playback",  fe->dai_link->name,
1150 			stream ? "<-" : "->", be->dai_link->name);
1151 
1152 #ifdef CONFIG_DEBUG_FS
1153 	name = kasprintf(GFP_KERNEL, "%s:%s", be->dai_link->name,
1154 			 stream ? "capture" : "playback");
1155 	if (name) {
1156 		dpcm->debugfs_state = debugfs_create_dir(name,
1157 							 fe->debugfs_dpcm_root);
1158 		debugfs_create_u32("state", 0644, dpcm->debugfs_state,
1159 				   &dpcm->state);
1160 		kfree(name);
1161 	}
1162 #endif
1163 	return 1;
1164 }
1165 
1166 /* reparent a BE onto another FE */
1167 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1168 			struct snd_soc_pcm_runtime *be, int stream)
1169 {
1170 	struct snd_soc_dpcm *dpcm;
1171 	struct snd_pcm_substream *fe_substream, *be_substream;
1172 
1173 	/* reparent if BE is connected to other FEs */
1174 	if (!be->dpcm[stream].users)
1175 		return;
1176 
1177 	be_substream = snd_soc_dpcm_get_substream(be, stream);
1178 
1179 	for_each_dpcm_fe(be, stream, dpcm) {
1180 		if (dpcm->fe == fe)
1181 			continue;
1182 
1183 		dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1184 			stream ? "capture" : "playback",
1185 			dpcm->fe->dai_link->name,
1186 			stream ? "<-" : "->", dpcm->be->dai_link->name);
1187 
1188 		fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1189 		be_substream->runtime = fe_substream->runtime;
1190 		break;
1191 	}
1192 }
1193 
1194 /* disconnect a BE and FE */
1195 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1196 {
1197 	struct snd_soc_dpcm *dpcm, *d;
1198 	unsigned long flags;
1199 
1200 	for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1201 		dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1202 				stream ? "capture" : "playback",
1203 				dpcm->be->dai_link->name);
1204 
1205 		if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1206 			continue;
1207 
1208 		dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1209 			stream ? "capture" : "playback", fe->dai_link->name,
1210 			stream ? "<-" : "->", dpcm->be->dai_link->name);
1211 
1212 		/* BEs still alive need new FE */
1213 		dpcm_be_reparent(fe, dpcm->be, stream);
1214 
1215 #ifdef CONFIG_DEBUG_FS
1216 		debugfs_remove_recursive(dpcm->debugfs_state);
1217 #endif
1218 		spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1219 		list_del(&dpcm->list_be);
1220 		list_del(&dpcm->list_fe);
1221 		spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1222 		kfree(dpcm);
1223 	}
1224 }
1225 
1226 /* get BE for DAI widget and stream */
1227 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1228 		struct snd_soc_dapm_widget *widget, int stream)
1229 {
1230 	struct snd_soc_pcm_runtime *be;
1231 	struct snd_soc_dai *dai;
1232 	int i;
1233 
1234 	dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1235 
1236 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1237 		for_each_card_rtds(card, be) {
1238 
1239 			if (!be->dai_link->no_pcm)
1240 				continue;
1241 
1242 			dev_dbg(card->dev, "ASoC: try BE : %s\n",
1243 				be->cpu_dai->playback_widget ?
1244 				be->cpu_dai->playback_widget->name : "(not set)");
1245 
1246 			if (be->cpu_dai->playback_widget == widget)
1247 				return be;
1248 
1249 			for_each_rtd_codec_dai(be, i, dai) {
1250 				if (dai->playback_widget == widget)
1251 					return be;
1252 			}
1253 		}
1254 	} else {
1255 
1256 		for_each_card_rtds(card, be) {
1257 
1258 			if (!be->dai_link->no_pcm)
1259 				continue;
1260 
1261 			dev_dbg(card->dev, "ASoC: try BE %s\n",
1262 				be->cpu_dai->capture_widget ?
1263 				be->cpu_dai->capture_widget->name : "(not set)");
1264 
1265 			if (be->cpu_dai->capture_widget == widget)
1266 				return be;
1267 
1268 			for_each_rtd_codec_dai(be, i, dai) {
1269 				if (dai->capture_widget == widget)
1270 					return be;
1271 			}
1272 		}
1273 	}
1274 
1275 	/* dai link name and stream name set correctly ? */
1276 	dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1277 		stream ? "capture" : "playback", widget->name);
1278 	return NULL;
1279 }
1280 
1281 static inline struct snd_soc_dapm_widget *
1282 	dai_get_widget(struct snd_soc_dai *dai, int stream)
1283 {
1284 	if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1285 		return dai->playback_widget;
1286 	else
1287 		return dai->capture_widget;
1288 }
1289 
1290 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1291 		struct snd_soc_dapm_widget *widget)
1292 {
1293 	int i;
1294 
1295 	for (i = 0; i < list->num_widgets; i++) {
1296 		if (widget == list->widgets[i])
1297 			return 1;
1298 	}
1299 
1300 	return 0;
1301 }
1302 
1303 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1304 		enum snd_soc_dapm_direction dir)
1305 {
1306 	struct snd_soc_card *card = widget->dapm->card;
1307 	struct snd_soc_pcm_runtime *rtd;
1308 	struct snd_soc_dai *dai;
1309 	int i;
1310 
1311 	if (dir == SND_SOC_DAPM_DIR_OUT) {
1312 		for_each_card_rtds(card, rtd) {
1313 			if (!rtd->dai_link->no_pcm)
1314 				continue;
1315 
1316 			if (rtd->cpu_dai->playback_widget == widget)
1317 				return true;
1318 
1319 			for_each_rtd_codec_dai(rtd, i, dai) {
1320 				if (dai->playback_widget == widget)
1321 					return true;
1322 			}
1323 		}
1324 	} else { /* SND_SOC_DAPM_DIR_IN */
1325 		for_each_card_rtds(card, rtd) {
1326 			if (!rtd->dai_link->no_pcm)
1327 				continue;
1328 
1329 			if (rtd->cpu_dai->capture_widget == widget)
1330 				return true;
1331 
1332 			for_each_rtd_codec_dai(rtd, i, dai) {
1333 				if (dai->capture_widget == widget)
1334 					return true;
1335 			}
1336 		}
1337 	}
1338 
1339 	return false;
1340 }
1341 
1342 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1343 	int stream, struct snd_soc_dapm_widget_list **list)
1344 {
1345 	struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1346 	int paths;
1347 
1348 	/* get number of valid DAI paths and their widgets */
1349 	paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1350 			dpcm_end_walk_at_be);
1351 
1352 	dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1353 			stream ? "capture" : "playback");
1354 
1355 	return paths;
1356 }
1357 
1358 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1359 	struct snd_soc_dapm_widget_list **list_)
1360 {
1361 	struct snd_soc_dpcm *dpcm;
1362 	struct snd_soc_dapm_widget_list *list = *list_;
1363 	struct snd_soc_dapm_widget *widget;
1364 	struct snd_soc_dai *dai;
1365 	int prune = 0;
1366 	int do_prune;
1367 
1368 	/* Destroy any old FE <--> BE connections */
1369 	for_each_dpcm_be(fe, stream, dpcm) {
1370 		unsigned int i;
1371 
1372 		/* is there a valid CPU DAI widget for this BE */
1373 		widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1374 
1375 		/* prune the BE if it's no longer in our active list */
1376 		if (widget && widget_in_list(list, widget))
1377 			continue;
1378 
1379 		/* is there a valid CODEC DAI widget for this BE */
1380 		do_prune = 1;
1381 		for_each_rtd_codec_dai(dpcm->be, i, dai) {
1382 			widget = dai_get_widget(dai, stream);
1383 
1384 			/* prune the BE if it's no longer in our active list */
1385 			if (widget && widget_in_list(list, widget))
1386 				do_prune = 0;
1387 		}
1388 		if (!do_prune)
1389 			continue;
1390 
1391 		dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1392 			stream ? "capture" : "playback",
1393 			dpcm->be->dai_link->name, fe->dai_link->name);
1394 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1395 		dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1396 		prune++;
1397 	}
1398 
1399 	dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1400 	return prune;
1401 }
1402 
1403 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1404 	struct snd_soc_dapm_widget_list **list_)
1405 {
1406 	struct snd_soc_card *card = fe->card;
1407 	struct snd_soc_dapm_widget_list *list = *list_;
1408 	struct snd_soc_pcm_runtime *be;
1409 	int i, new = 0, err;
1410 
1411 	/* Create any new FE <--> BE connections */
1412 	for (i = 0; i < list->num_widgets; i++) {
1413 
1414 		switch (list->widgets[i]->id) {
1415 		case snd_soc_dapm_dai_in:
1416 			if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1417 				continue;
1418 			break;
1419 		case snd_soc_dapm_dai_out:
1420 			if (stream != SNDRV_PCM_STREAM_CAPTURE)
1421 				continue;
1422 			break;
1423 		default:
1424 			continue;
1425 		}
1426 
1427 		/* is there a valid BE rtd for this widget */
1428 		be = dpcm_get_be(card, list->widgets[i], stream);
1429 		if (!be) {
1430 			dev_err(fe->dev, "ASoC: no BE found for %s\n",
1431 					list->widgets[i]->name);
1432 			continue;
1433 		}
1434 
1435 		/* make sure BE is a real BE */
1436 		if (!be->dai_link->no_pcm)
1437 			continue;
1438 
1439 		/* don't connect if FE is not running */
1440 		if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1441 			continue;
1442 
1443 		/* newly connected FE and BE */
1444 		err = dpcm_be_connect(fe, be, stream);
1445 		if (err < 0) {
1446 			dev_err(fe->dev, "ASoC: can't connect %s\n",
1447 				list->widgets[i]->name);
1448 			break;
1449 		} else if (err == 0) /* already connected */
1450 			continue;
1451 
1452 		/* new */
1453 		be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1454 		new++;
1455 	}
1456 
1457 	dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1458 	return new;
1459 }
1460 
1461 /*
1462  * Find the corresponding BE DAIs that source or sink audio to this
1463  * FE substream.
1464  */
1465 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1466 	int stream, struct snd_soc_dapm_widget_list **list, int new)
1467 {
1468 	if (new)
1469 		return dpcm_add_paths(fe, stream, list);
1470 	else
1471 		return dpcm_prune_paths(fe, stream, list);
1472 }
1473 
1474 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1475 {
1476 	struct snd_soc_dpcm *dpcm;
1477 	unsigned long flags;
1478 
1479 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1480 	for_each_dpcm_be(fe, stream, dpcm)
1481 		dpcm->be->dpcm[stream].runtime_update =
1482 						SND_SOC_DPCM_UPDATE_NO;
1483 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1484 }
1485 
1486 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1487 	int stream)
1488 {
1489 	struct snd_soc_dpcm *dpcm;
1490 
1491 	/* disable any enabled and non active backends */
1492 	for_each_dpcm_be(fe, stream, dpcm) {
1493 
1494 		struct snd_soc_pcm_runtime *be = dpcm->be;
1495 		struct snd_pcm_substream *be_substream =
1496 			snd_soc_dpcm_get_substream(be, stream);
1497 
1498 		if (be->dpcm[stream].users == 0)
1499 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1500 				stream ? "capture" : "playback",
1501 				be->dpcm[stream].state);
1502 
1503 		if (--be->dpcm[stream].users != 0)
1504 			continue;
1505 
1506 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1507 			continue;
1508 
1509 		soc_pcm_close(be_substream);
1510 		be_substream->runtime = NULL;
1511 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1512 	}
1513 }
1514 
1515 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1516 {
1517 	struct snd_soc_dpcm *dpcm;
1518 	int err, count = 0;
1519 
1520 	/* only startup BE DAIs that are either sinks or sources to this FE DAI */
1521 	for_each_dpcm_be(fe, stream, dpcm) {
1522 
1523 		struct snd_soc_pcm_runtime *be = dpcm->be;
1524 		struct snd_pcm_substream *be_substream =
1525 			snd_soc_dpcm_get_substream(be, stream);
1526 
1527 		if (!be_substream) {
1528 			dev_err(be->dev, "ASoC: no backend %s stream\n",
1529 				stream ? "capture" : "playback");
1530 			continue;
1531 		}
1532 
1533 		/* is this op for this BE ? */
1534 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1535 			continue;
1536 
1537 		/* first time the dpcm is open ? */
1538 		if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1539 			dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1540 				stream ? "capture" : "playback",
1541 				be->dpcm[stream].state);
1542 
1543 		if (be->dpcm[stream].users++ != 0)
1544 			continue;
1545 
1546 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1547 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1548 			continue;
1549 
1550 		dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1551 			stream ? "capture" : "playback", be->dai_link->name);
1552 
1553 		be_substream->runtime = be->dpcm[stream].runtime;
1554 		err = soc_pcm_open(be_substream);
1555 		if (err < 0) {
1556 			dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1557 			be->dpcm[stream].users--;
1558 			if (be->dpcm[stream].users < 0)
1559 				dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1560 					stream ? "capture" : "playback",
1561 					be->dpcm[stream].state);
1562 
1563 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1564 			goto unwind;
1565 		}
1566 
1567 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1568 		count++;
1569 	}
1570 
1571 	return count;
1572 
1573 unwind:
1574 	/* disable any enabled and non active backends */
1575 	for_each_dpcm_be_rollback(fe, stream, dpcm) {
1576 		struct snd_soc_pcm_runtime *be = dpcm->be;
1577 		struct snd_pcm_substream *be_substream =
1578 			snd_soc_dpcm_get_substream(be, stream);
1579 
1580 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1581 			continue;
1582 
1583 		if (be->dpcm[stream].users == 0)
1584 			dev_err(be->dev, "ASoC: no users %s at close %d\n",
1585 				stream ? "capture" : "playback",
1586 				be->dpcm[stream].state);
1587 
1588 		if (--be->dpcm[stream].users != 0)
1589 			continue;
1590 
1591 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1592 			continue;
1593 
1594 		soc_pcm_close(be_substream);
1595 		be_substream->runtime = NULL;
1596 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1597 	}
1598 
1599 	return err;
1600 }
1601 
1602 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1603 				 struct snd_soc_pcm_stream *stream)
1604 {
1605 	runtime->hw.rate_min = stream->rate_min;
1606 	runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
1607 	runtime->hw.channels_min = stream->channels_min;
1608 	runtime->hw.channels_max = stream->channels_max;
1609 	if (runtime->hw.formats)
1610 		runtime->hw.formats &= stream->formats;
1611 	else
1612 		runtime->hw.formats = stream->formats;
1613 	runtime->hw.rates = stream->rates;
1614 }
1615 
1616 static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1617 				      u64 *formats)
1618 {
1619 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1620 	struct snd_soc_dpcm *dpcm;
1621 	struct snd_soc_dai *dai;
1622 	int stream = substream->stream;
1623 
1624 	if (!fe->dai_link->dpcm_merged_format)
1625 		return;
1626 
1627 	/*
1628 	 * It returns merged BE codec format
1629 	 * if FE want to use it (= dpcm_merged_format)
1630 	 */
1631 
1632 	for_each_dpcm_be(fe, stream, dpcm) {
1633 		struct snd_soc_pcm_runtime *be = dpcm->be;
1634 		struct snd_soc_dai_driver *codec_dai_drv;
1635 		struct snd_soc_pcm_stream *codec_stream;
1636 		int i;
1637 
1638 		for_each_rtd_codec_dai(be, i, dai) {
1639 			/*
1640 			 * Skip CODECs which don't support the current stream
1641 			 * type. See soc_pcm_init_runtime_hw() for more details
1642 			 */
1643 			if (!snd_soc_dai_stream_valid(dai, stream))
1644 				continue;
1645 
1646 			codec_dai_drv = dai->driver;
1647 			if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1648 				codec_stream = &codec_dai_drv->playback;
1649 			else
1650 				codec_stream = &codec_dai_drv->capture;
1651 
1652 			*formats &= codec_stream->formats;
1653 		}
1654 	}
1655 }
1656 
1657 static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1658 				    unsigned int *channels_min,
1659 				    unsigned int *channels_max)
1660 {
1661 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1662 	struct snd_soc_dpcm *dpcm;
1663 	int stream = substream->stream;
1664 
1665 	if (!fe->dai_link->dpcm_merged_chan)
1666 		return;
1667 
1668 	/*
1669 	 * It returns merged BE codec channel;
1670 	 * if FE want to use it (= dpcm_merged_chan)
1671 	 */
1672 
1673 	for_each_dpcm_be(fe, stream, dpcm) {
1674 		struct snd_soc_pcm_runtime *be = dpcm->be;
1675 		struct snd_soc_dai_driver *cpu_dai_drv =  be->cpu_dai->driver;
1676 		struct snd_soc_dai_driver *codec_dai_drv;
1677 		struct snd_soc_pcm_stream *codec_stream;
1678 		struct snd_soc_pcm_stream *cpu_stream;
1679 
1680 		if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1681 			cpu_stream = &cpu_dai_drv->playback;
1682 		else
1683 			cpu_stream = &cpu_dai_drv->capture;
1684 
1685 		*channels_min = max(*channels_min, cpu_stream->channels_min);
1686 		*channels_max = min(*channels_max, cpu_stream->channels_max);
1687 
1688 		/*
1689 		 * chan min/max cannot be enforced if there are multiple CODEC
1690 		 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1691 		 */
1692 		if (be->num_codecs == 1) {
1693 			codec_dai_drv = be->codec_dais[0]->driver;
1694 
1695 			if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1696 				codec_stream = &codec_dai_drv->playback;
1697 			else
1698 				codec_stream = &codec_dai_drv->capture;
1699 
1700 			*channels_min = max(*channels_min,
1701 					    codec_stream->channels_min);
1702 			*channels_max = min(*channels_max,
1703 					    codec_stream->channels_max);
1704 		}
1705 	}
1706 }
1707 
1708 static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1709 				    unsigned int *rates,
1710 				    unsigned int *rate_min,
1711 				    unsigned int *rate_max)
1712 {
1713 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1714 	struct snd_soc_dpcm *dpcm;
1715 	int stream = substream->stream;
1716 
1717 	if (!fe->dai_link->dpcm_merged_rate)
1718 		return;
1719 
1720 	/*
1721 	 * It returns merged BE codec channel;
1722 	 * if FE want to use it (= dpcm_merged_chan)
1723 	 */
1724 
1725 	for_each_dpcm_be(fe, stream, dpcm) {
1726 		struct snd_soc_pcm_runtime *be = dpcm->be;
1727 		struct snd_soc_dai_driver *cpu_dai_drv =  be->cpu_dai->driver;
1728 		struct snd_soc_dai_driver *codec_dai_drv;
1729 		struct snd_soc_pcm_stream *codec_stream;
1730 		struct snd_soc_pcm_stream *cpu_stream;
1731 		struct snd_soc_dai *dai;
1732 		int i;
1733 
1734 		if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1735 			cpu_stream = &cpu_dai_drv->playback;
1736 		else
1737 			cpu_stream = &cpu_dai_drv->capture;
1738 
1739 		*rate_min = max(*rate_min, cpu_stream->rate_min);
1740 		*rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1741 		*rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1742 
1743 		for_each_rtd_codec_dai(be, i, dai) {
1744 			/*
1745 			 * Skip CODECs which don't support the current stream
1746 			 * type. See soc_pcm_init_runtime_hw() for more details
1747 			 */
1748 			if (!snd_soc_dai_stream_valid(dai, stream))
1749 				continue;
1750 
1751 			codec_dai_drv = dai->driver;
1752 			if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1753 				codec_stream = &codec_dai_drv->playback;
1754 			else
1755 				codec_stream = &codec_dai_drv->capture;
1756 
1757 			*rate_min = max(*rate_min, codec_stream->rate_min);
1758 			*rate_max = min_not_zero(*rate_max,
1759 						 codec_stream->rate_max);
1760 			*rates = snd_pcm_rate_mask_intersect(*rates,
1761 						codec_stream->rates);
1762 		}
1763 	}
1764 }
1765 
1766 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1767 {
1768 	struct snd_pcm_runtime *runtime = substream->runtime;
1769 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1770 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1771 	struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1772 
1773 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1774 		dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1775 	else
1776 		dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1777 
1778 	dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1779 	dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1780 				&runtime->hw.channels_max);
1781 	dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1782 				&runtime->hw.rate_min, &runtime->hw.rate_max);
1783 }
1784 
1785 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1786 
1787 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1788  * for avoiding the race with trigger callback.
1789  * If the state is unset and a trigger is pending while the previous operation,
1790  * process the pending trigger action here.
1791  */
1792 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1793 				     int stream, enum snd_soc_dpcm_update state)
1794 {
1795 	struct snd_pcm_substream *substream =
1796 		snd_soc_dpcm_get_substream(fe, stream);
1797 
1798 	snd_pcm_stream_lock_irq(substream);
1799 	if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1800 		dpcm_fe_dai_do_trigger(substream,
1801 				       fe->dpcm[stream].trigger_pending - 1);
1802 		fe->dpcm[stream].trigger_pending = 0;
1803 	}
1804 	fe->dpcm[stream].runtime_update = state;
1805 	snd_pcm_stream_unlock_irq(substream);
1806 }
1807 
1808 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1809 			       int stream)
1810 {
1811 	struct snd_soc_dpcm *dpcm;
1812 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1813 	struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1814 	int err;
1815 
1816 	/* apply symmetry for FE */
1817 	if (soc_pcm_has_symmetry(fe_substream))
1818 		fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1819 
1820 	/* Symmetry only applies if we've got an active stream. */
1821 	if (fe_cpu_dai->active) {
1822 		err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1823 		if (err < 0)
1824 			return err;
1825 	}
1826 
1827 	/* apply symmetry for BE */
1828 	for_each_dpcm_be(fe, stream, dpcm) {
1829 		struct snd_soc_pcm_runtime *be = dpcm->be;
1830 		struct snd_pcm_substream *be_substream =
1831 			snd_soc_dpcm_get_substream(be, stream);
1832 		struct snd_soc_pcm_runtime *rtd;
1833 		struct snd_soc_dai *codec_dai;
1834 		int i;
1835 
1836 		/* A backend may not have the requested substream */
1837 		if (!be_substream)
1838 			continue;
1839 
1840 		rtd = be_substream->private_data;
1841 		if (rtd->dai_link->be_hw_params_fixup)
1842 			continue;
1843 
1844 		if (soc_pcm_has_symmetry(be_substream))
1845 			be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1846 
1847 		/* Symmetry only applies if we've got an active stream. */
1848 		if (rtd->cpu_dai->active) {
1849 			err = soc_pcm_apply_symmetry(fe_substream,
1850 						     rtd->cpu_dai);
1851 			if (err < 0)
1852 				return err;
1853 		}
1854 
1855 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
1856 			if (codec_dai->active) {
1857 				err = soc_pcm_apply_symmetry(fe_substream,
1858 							     codec_dai);
1859 				if (err < 0)
1860 					return err;
1861 			}
1862 		}
1863 	}
1864 
1865 	return 0;
1866 }
1867 
1868 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1869 {
1870 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1871 	struct snd_pcm_runtime *runtime = fe_substream->runtime;
1872 	int stream = fe_substream->stream, ret = 0;
1873 
1874 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1875 
1876 	ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1877 	if (ret < 0) {
1878 		dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1879 		goto be_err;
1880 	}
1881 
1882 	dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1883 
1884 	/* start the DAI frontend */
1885 	ret = soc_pcm_open(fe_substream);
1886 	if (ret < 0) {
1887 		dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1888 		goto unwind;
1889 	}
1890 
1891 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1892 
1893 	dpcm_set_fe_runtime(fe_substream);
1894 	snd_pcm_limit_hw_rates(runtime);
1895 
1896 	ret = dpcm_apply_symmetry(fe_substream, stream);
1897 	if (ret < 0) {
1898 		dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1899 			ret);
1900 		goto unwind;
1901 	}
1902 
1903 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1904 	return 0;
1905 
1906 unwind:
1907 	dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1908 be_err:
1909 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1910 	return ret;
1911 }
1912 
1913 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1914 {
1915 	struct snd_soc_dpcm *dpcm;
1916 
1917 	/* only shutdown BEs that are either sinks or sources to this FE DAI */
1918 	for_each_dpcm_be(fe, stream, dpcm) {
1919 
1920 		struct snd_soc_pcm_runtime *be = dpcm->be;
1921 		struct snd_pcm_substream *be_substream =
1922 			snd_soc_dpcm_get_substream(be, stream);
1923 
1924 		/* is this op for this BE ? */
1925 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1926 			continue;
1927 
1928 		if (be->dpcm[stream].users == 0)
1929 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1930 				stream ? "capture" : "playback",
1931 				be->dpcm[stream].state);
1932 
1933 		if (--be->dpcm[stream].users != 0)
1934 			continue;
1935 
1936 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1937 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1938 			soc_pcm_hw_free(be_substream);
1939 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1940 		}
1941 
1942 		dev_dbg(be->dev, "ASoC: close BE %s\n",
1943 			be->dai_link->name);
1944 
1945 		soc_pcm_close(be_substream);
1946 		be_substream->runtime = NULL;
1947 
1948 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1949 	}
1950 	return 0;
1951 }
1952 
1953 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1954 {
1955 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1956 	int stream = substream->stream;
1957 
1958 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1959 
1960 	/* shutdown the BEs */
1961 	dpcm_be_dai_shutdown(fe, substream->stream);
1962 
1963 	dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1964 
1965 	/* now shutdown the frontend */
1966 	soc_pcm_close(substream);
1967 
1968 	/* run the stream event for each BE */
1969 	snd_soc_dapm_stream_stop(fe, stream);
1970 
1971 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1972 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1973 	return 0;
1974 }
1975 
1976 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1977 {
1978 	struct snd_soc_dpcm *dpcm;
1979 
1980 	/* only hw_params backends that are either sinks or sources
1981 	 * to this frontend DAI */
1982 	for_each_dpcm_be(fe, stream, dpcm) {
1983 
1984 		struct snd_soc_pcm_runtime *be = dpcm->be;
1985 		struct snd_pcm_substream *be_substream =
1986 			snd_soc_dpcm_get_substream(be, stream);
1987 
1988 		/* is this op for this BE ? */
1989 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1990 			continue;
1991 
1992 		/* only free hw when no longer used - check all FEs */
1993 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1994 				continue;
1995 
1996 		/* do not free hw if this BE is used by other FE */
1997 		if (be->dpcm[stream].users > 1)
1998 			continue;
1999 
2000 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2001 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2002 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2003 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
2004 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2005 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2006 			continue;
2007 
2008 		dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
2009 			be->dai_link->name);
2010 
2011 		soc_pcm_hw_free(be_substream);
2012 
2013 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2014 	}
2015 
2016 	return 0;
2017 }
2018 
2019 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
2020 {
2021 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2022 	int err, stream = substream->stream;
2023 
2024 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2025 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2026 
2027 	dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
2028 
2029 	/* call hw_free on the frontend */
2030 	err = soc_pcm_hw_free(substream);
2031 	if (err < 0)
2032 		dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
2033 			fe->dai_link->name);
2034 
2035 	/* only hw_params backends that are either sinks or sources
2036 	 * to this frontend DAI */
2037 	err = dpcm_be_dai_hw_free(fe, stream);
2038 
2039 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2040 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2041 
2042 	mutex_unlock(&fe->card->mutex);
2043 	return 0;
2044 }
2045 
2046 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
2047 {
2048 	struct snd_soc_dpcm *dpcm;
2049 	int ret;
2050 
2051 	for_each_dpcm_be(fe, stream, dpcm) {
2052 
2053 		struct snd_soc_pcm_runtime *be = dpcm->be;
2054 		struct snd_pcm_substream *be_substream =
2055 			snd_soc_dpcm_get_substream(be, stream);
2056 
2057 		/* is this op for this BE ? */
2058 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2059 			continue;
2060 
2061 		/* copy params for each dpcm */
2062 		memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2063 				sizeof(struct snd_pcm_hw_params));
2064 
2065 		/* perform any hw_params fixups */
2066 		if (be->dai_link->be_hw_params_fixup) {
2067 			ret = be->dai_link->be_hw_params_fixup(be,
2068 					&dpcm->hw_params);
2069 			if (ret < 0) {
2070 				dev_err(be->dev,
2071 					"ASoC: hw_params BE fixup failed %d\n",
2072 					ret);
2073 				goto unwind;
2074 			}
2075 		}
2076 
2077 		/* copy the fixed-up hw params for BE dai */
2078 		memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2079 		       sizeof(struct snd_pcm_hw_params));
2080 
2081 		/* only allow hw_params() if no connected FEs are running */
2082 		if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2083 			continue;
2084 
2085 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2086 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2087 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2088 			continue;
2089 
2090 		dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2091 			be->dai_link->name);
2092 
2093 		ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2094 		if (ret < 0) {
2095 			dev_err(dpcm->be->dev,
2096 				"ASoC: hw_params BE failed %d\n", ret);
2097 			goto unwind;
2098 		}
2099 
2100 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2101 	}
2102 	return 0;
2103 
2104 unwind:
2105 	/* disable any enabled and non active backends */
2106 	for_each_dpcm_be_rollback(fe, stream, dpcm) {
2107 		struct snd_soc_pcm_runtime *be = dpcm->be;
2108 		struct snd_pcm_substream *be_substream =
2109 			snd_soc_dpcm_get_substream(be, stream);
2110 
2111 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2112 			continue;
2113 
2114 		/* only allow hw_free() if no connected FEs are running */
2115 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2116 			continue;
2117 
2118 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2119 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2120 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2121 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2122 			continue;
2123 
2124 		soc_pcm_hw_free(be_substream);
2125 	}
2126 
2127 	return ret;
2128 }
2129 
2130 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2131 				 struct snd_pcm_hw_params *params)
2132 {
2133 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2134 	int ret, stream = substream->stream;
2135 
2136 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2137 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2138 
2139 	memcpy(&fe->dpcm[substream->stream].hw_params, params,
2140 			sizeof(struct snd_pcm_hw_params));
2141 	ret = dpcm_be_dai_hw_params(fe, substream->stream);
2142 	if (ret < 0) {
2143 		dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2144 		goto out;
2145 	}
2146 
2147 	dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2148 			fe->dai_link->name, params_rate(params),
2149 			params_channels(params), params_format(params));
2150 
2151 	/* call hw_params on the frontend */
2152 	ret = soc_pcm_hw_params(substream, params);
2153 	if (ret < 0) {
2154 		dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2155 		dpcm_be_dai_hw_free(fe, stream);
2156 	 } else
2157 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2158 
2159 out:
2160 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2161 	mutex_unlock(&fe->card->mutex);
2162 	return ret;
2163 }
2164 
2165 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2166 		struct snd_pcm_substream *substream, int cmd)
2167 {
2168 	int ret;
2169 
2170 	dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2171 			dpcm->be->dai_link->name, cmd);
2172 
2173 	ret = soc_pcm_trigger(substream, cmd);
2174 	if (ret < 0)
2175 		dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2176 
2177 	return ret;
2178 }
2179 
2180 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2181 			       int cmd)
2182 {
2183 	struct snd_soc_dpcm *dpcm;
2184 	int ret = 0;
2185 
2186 	for_each_dpcm_be(fe, stream, dpcm) {
2187 
2188 		struct snd_soc_pcm_runtime *be = dpcm->be;
2189 		struct snd_pcm_substream *be_substream =
2190 			snd_soc_dpcm_get_substream(be, stream);
2191 
2192 		/* is this op for this BE ? */
2193 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2194 			continue;
2195 
2196 		switch (cmd) {
2197 		case SNDRV_PCM_TRIGGER_START:
2198 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2199 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2200 				continue;
2201 
2202 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2203 			if (ret)
2204 				return ret;
2205 
2206 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2207 			break;
2208 		case SNDRV_PCM_TRIGGER_RESUME:
2209 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2210 				continue;
2211 
2212 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2213 			if (ret)
2214 				return ret;
2215 
2216 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2217 			break;
2218 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2219 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2220 				continue;
2221 
2222 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2223 			if (ret)
2224 				return ret;
2225 
2226 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2227 			break;
2228 		case SNDRV_PCM_TRIGGER_STOP:
2229 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2230 				continue;
2231 
2232 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2233 				continue;
2234 
2235 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2236 			if (ret)
2237 				return ret;
2238 
2239 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2240 			break;
2241 		case SNDRV_PCM_TRIGGER_SUSPEND:
2242 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2243 				continue;
2244 
2245 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2246 				continue;
2247 
2248 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2249 			if (ret)
2250 				return ret;
2251 
2252 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2253 			break;
2254 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2255 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2256 				continue;
2257 
2258 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2259 				continue;
2260 
2261 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2262 			if (ret)
2263 				return ret;
2264 
2265 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2266 			break;
2267 		}
2268 	}
2269 
2270 	return ret;
2271 }
2272 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2273 
2274 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2275 				  int cmd, bool fe_first)
2276 {
2277 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2278 	int ret;
2279 
2280 	/* call trigger on the frontend before the backend. */
2281 	if (fe_first) {
2282 		dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2283 			fe->dai_link->name, cmd);
2284 
2285 		ret = soc_pcm_trigger(substream, cmd);
2286 		if (ret < 0)
2287 			return ret;
2288 
2289 		ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2290 		return ret;
2291 	}
2292 
2293 	/* call trigger on the frontend after the backend. */
2294 	ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2295 	if (ret < 0)
2296 		return ret;
2297 
2298 	dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2299 		fe->dai_link->name, cmd);
2300 
2301 	ret = soc_pcm_trigger(substream, cmd);
2302 
2303 	return ret;
2304 }
2305 
2306 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2307 {
2308 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2309 	int stream = substream->stream;
2310 	int ret = 0;
2311 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2312 
2313 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2314 
2315 	switch (trigger) {
2316 	case SND_SOC_DPCM_TRIGGER_PRE:
2317 		switch (cmd) {
2318 		case SNDRV_PCM_TRIGGER_START:
2319 		case SNDRV_PCM_TRIGGER_RESUME:
2320 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2321 			ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2322 			break;
2323 		case SNDRV_PCM_TRIGGER_STOP:
2324 		case SNDRV_PCM_TRIGGER_SUSPEND:
2325 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2326 			ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2327 			break;
2328 		default:
2329 			ret = -EINVAL;
2330 			break;
2331 		}
2332 		break;
2333 	case SND_SOC_DPCM_TRIGGER_POST:
2334 		switch (cmd) {
2335 		case SNDRV_PCM_TRIGGER_START:
2336 		case SNDRV_PCM_TRIGGER_RESUME:
2337 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2338 			ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2339 			break;
2340 		case SNDRV_PCM_TRIGGER_STOP:
2341 		case SNDRV_PCM_TRIGGER_SUSPEND:
2342 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2343 			ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2344 			break;
2345 		default:
2346 			ret = -EINVAL;
2347 			break;
2348 		}
2349 		break;
2350 	case SND_SOC_DPCM_TRIGGER_BESPOKE:
2351 		/* bespoke trigger() - handles both FE and BEs */
2352 
2353 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2354 				fe->dai_link->name, cmd);
2355 
2356 		ret = soc_pcm_bespoke_trigger(substream, cmd);
2357 		break;
2358 	default:
2359 		dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2360 				fe->dai_link->name);
2361 		ret = -EINVAL;
2362 		goto out;
2363 	}
2364 
2365 	if (ret < 0) {
2366 		dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2367 			cmd, ret);
2368 		goto out;
2369 	}
2370 
2371 	switch (cmd) {
2372 	case SNDRV_PCM_TRIGGER_START:
2373 	case SNDRV_PCM_TRIGGER_RESUME:
2374 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2375 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2376 		break;
2377 	case SNDRV_PCM_TRIGGER_STOP:
2378 	case SNDRV_PCM_TRIGGER_SUSPEND:
2379 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2380 		break;
2381 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2382 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2383 		break;
2384 	}
2385 
2386 out:
2387 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2388 	return ret;
2389 }
2390 
2391 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2392 {
2393 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2394 	int stream = substream->stream;
2395 
2396 	/* if FE's runtime_update is already set, we're in race;
2397 	 * process this trigger later at exit
2398 	 */
2399 	if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2400 		fe->dpcm[stream].trigger_pending = cmd + 1;
2401 		return 0; /* delayed, assuming it's successful */
2402 	}
2403 
2404 	/* we're alone, let's trigger */
2405 	return dpcm_fe_dai_do_trigger(substream, cmd);
2406 }
2407 
2408 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2409 {
2410 	struct snd_soc_dpcm *dpcm;
2411 	int ret = 0;
2412 
2413 	for_each_dpcm_be(fe, stream, dpcm) {
2414 
2415 		struct snd_soc_pcm_runtime *be = dpcm->be;
2416 		struct snd_pcm_substream *be_substream =
2417 			snd_soc_dpcm_get_substream(be, stream);
2418 
2419 		/* is this op for this BE ? */
2420 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2421 			continue;
2422 
2423 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2424 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2425 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2426 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2427 			continue;
2428 
2429 		dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2430 			be->dai_link->name);
2431 
2432 		ret = soc_pcm_prepare(be_substream);
2433 		if (ret < 0) {
2434 			dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2435 				ret);
2436 			break;
2437 		}
2438 
2439 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2440 	}
2441 	return ret;
2442 }
2443 
2444 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2445 {
2446 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2447 	int stream = substream->stream, ret = 0;
2448 
2449 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2450 
2451 	dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2452 
2453 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2454 
2455 	/* there is no point preparing this FE if there are no BEs */
2456 	if (list_empty(&fe->dpcm[stream].be_clients)) {
2457 		dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2458 				fe->dai_link->name);
2459 		ret = -EINVAL;
2460 		goto out;
2461 	}
2462 
2463 	ret = dpcm_be_dai_prepare(fe, substream->stream);
2464 	if (ret < 0)
2465 		goto out;
2466 
2467 	/* call prepare on the frontend */
2468 	ret = soc_pcm_prepare(substream);
2469 	if (ret < 0) {
2470 		dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2471 			fe->dai_link->name);
2472 		goto out;
2473 	}
2474 
2475 	/* run the stream event for each BE */
2476 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2477 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2478 
2479 out:
2480 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2481 	mutex_unlock(&fe->card->mutex);
2482 
2483 	return ret;
2484 }
2485 
2486 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2487 {
2488 	struct snd_pcm_substream *substream =
2489 		snd_soc_dpcm_get_substream(fe, stream);
2490 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2491 	int err;
2492 
2493 	dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2494 			stream ? "capture" : "playback", fe->dai_link->name);
2495 
2496 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2497 		/* call bespoke trigger - FE takes care of all BE triggers */
2498 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2499 				fe->dai_link->name);
2500 
2501 		err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2502 		if (err < 0)
2503 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2504 	} else {
2505 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2506 			fe->dai_link->name);
2507 
2508 		err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2509 		if (err < 0)
2510 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2511 	}
2512 
2513 	err = dpcm_be_dai_hw_free(fe, stream);
2514 	if (err < 0)
2515 		dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2516 
2517 	err = dpcm_be_dai_shutdown(fe, stream);
2518 	if (err < 0)
2519 		dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2520 
2521 	/* run the stream event for each BE */
2522 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2523 
2524 	return 0;
2525 }
2526 
2527 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2528 {
2529 	struct snd_pcm_substream *substream =
2530 		snd_soc_dpcm_get_substream(fe, stream);
2531 	struct snd_soc_dpcm *dpcm;
2532 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2533 	int ret;
2534 	unsigned long flags;
2535 
2536 	dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2537 			stream ? "capture" : "playback", fe->dai_link->name);
2538 
2539 	/* Only start the BE if the FE is ready */
2540 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2541 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2542 		return -EINVAL;
2543 
2544 	/* startup must always be called for new BEs */
2545 	ret = dpcm_be_dai_startup(fe, stream);
2546 	if (ret < 0)
2547 		goto disconnect;
2548 
2549 	/* keep going if FE state is > open */
2550 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2551 		return 0;
2552 
2553 	ret = dpcm_be_dai_hw_params(fe, stream);
2554 	if (ret < 0)
2555 		goto close;
2556 
2557 	/* keep going if FE state is > hw_params */
2558 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2559 		return 0;
2560 
2561 
2562 	ret = dpcm_be_dai_prepare(fe, stream);
2563 	if (ret < 0)
2564 		goto hw_free;
2565 
2566 	/* run the stream event for each BE */
2567 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2568 
2569 	/* keep going if FE state is > prepare */
2570 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2571 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2572 		return 0;
2573 
2574 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2575 		/* call trigger on the frontend - FE takes care of all BE triggers */
2576 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2577 				fe->dai_link->name);
2578 
2579 		ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2580 		if (ret < 0) {
2581 			dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2582 			goto hw_free;
2583 		}
2584 	} else {
2585 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2586 			fe->dai_link->name);
2587 
2588 		ret = dpcm_be_dai_trigger(fe, stream,
2589 					SNDRV_PCM_TRIGGER_START);
2590 		if (ret < 0) {
2591 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2592 			goto hw_free;
2593 		}
2594 	}
2595 
2596 	return 0;
2597 
2598 hw_free:
2599 	dpcm_be_dai_hw_free(fe, stream);
2600 close:
2601 	dpcm_be_dai_shutdown(fe, stream);
2602 disconnect:
2603 	/* disconnect any non started BEs */
2604 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2605 	for_each_dpcm_be(fe, stream, dpcm) {
2606 		struct snd_soc_pcm_runtime *be = dpcm->be;
2607 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2608 				dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2609 	}
2610 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2611 
2612 	return ret;
2613 }
2614 
2615 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2616 {
2617 	int ret;
2618 
2619 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2620 	ret = dpcm_run_update_startup(fe, stream);
2621 	if (ret < 0)
2622 		dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2623 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2624 
2625 	return ret;
2626 }
2627 
2628 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2629 {
2630 	int ret;
2631 
2632 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2633 	ret = dpcm_run_update_shutdown(fe, stream);
2634 	if (ret < 0)
2635 		dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2636 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2637 
2638 	return ret;
2639 }
2640 
2641 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2642 {
2643 	struct snd_soc_dapm_widget_list *list;
2644 	int count, paths;
2645 
2646 	if (!fe->dai_link->dynamic)
2647 		return 0;
2648 
2649 	/* only check active links */
2650 	if (!fe->cpu_dai->active)
2651 		return 0;
2652 
2653 	/* DAPM sync will call this to update DSP paths */
2654 	dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2655 		new ? "new" : "old", fe->dai_link->name);
2656 
2657 	/* skip if FE doesn't have playback capability */
2658 	if (!snd_soc_dai_stream_valid(fe->cpu_dai,   SNDRV_PCM_STREAM_PLAYBACK) ||
2659 	    !snd_soc_dai_stream_valid(fe->codec_dai, SNDRV_PCM_STREAM_PLAYBACK))
2660 		goto capture;
2661 
2662 	/* skip if FE isn't currently playing */
2663 	if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
2664 		goto capture;
2665 
2666 	paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2667 	if (paths < 0) {
2668 		dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2669 			 fe->dai_link->name,  "playback");
2670 		return paths;
2671 	}
2672 
2673 	/* update any playback paths */
2674 	count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
2675 	if (count) {
2676 		if (new)
2677 			dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2678 		else
2679 			dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2680 
2681 		dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2682 		dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2683 	}
2684 
2685 	dpcm_path_put(&list);
2686 
2687 capture:
2688 	/* skip if FE doesn't have capture capability */
2689 	if (!snd_soc_dai_stream_valid(fe->cpu_dai,   SNDRV_PCM_STREAM_CAPTURE) ||
2690 	    !snd_soc_dai_stream_valid(fe->codec_dai, SNDRV_PCM_STREAM_CAPTURE))
2691 		return 0;
2692 
2693 	/* skip if FE isn't currently capturing */
2694 	if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
2695 		return 0;
2696 
2697 	paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2698 	if (paths < 0) {
2699 		dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2700 			 fe->dai_link->name,  "capture");
2701 		return paths;
2702 	}
2703 
2704 	/* update any old capture paths */
2705 	count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
2706 	if (count) {
2707 		if (new)
2708 			dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2709 		else
2710 			dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2711 
2712 		dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2713 		dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2714 	}
2715 
2716 	dpcm_path_put(&list);
2717 
2718 	return 0;
2719 }
2720 
2721 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2722  * any DAI links.
2723  */
2724 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2725 {
2726 	struct snd_soc_pcm_runtime *fe;
2727 	int ret = 0;
2728 
2729 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2730 	/* shutdown all old paths first */
2731 	for_each_card_rtds(card, fe) {
2732 		ret = soc_dpcm_fe_runtime_update(fe, 0);
2733 		if (ret)
2734 			goto out;
2735 	}
2736 
2737 	/* bring new paths up */
2738 	for_each_card_rtds(card, fe) {
2739 		ret = soc_dpcm_fe_runtime_update(fe, 1);
2740 		if (ret)
2741 			goto out;
2742 	}
2743 
2744 out:
2745 	mutex_unlock(&card->mutex);
2746 	return ret;
2747 }
2748 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2749 {
2750 	struct snd_soc_dpcm *dpcm;
2751 	struct snd_soc_dai *dai;
2752 
2753 	for_each_dpcm_be(fe, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
2754 
2755 		struct snd_soc_pcm_runtime *be = dpcm->be;
2756 		int i;
2757 
2758 		if (be->dai_link->ignore_suspend)
2759 			continue;
2760 
2761 		for_each_rtd_codec_dai(be, i, dai) {
2762 			struct snd_soc_dai_driver *drv = dai->driver;
2763 
2764 			dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2765 					 be->dai_link->name);
2766 
2767 			if (drv->ops && drv->ops->digital_mute &&
2768 							dai->playback_active)
2769 				drv->ops->digital_mute(dai, mute);
2770 		}
2771 	}
2772 
2773 	return 0;
2774 }
2775 
2776 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2777 {
2778 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2779 	struct snd_soc_dpcm *dpcm;
2780 	struct snd_soc_dapm_widget_list *list;
2781 	int ret;
2782 	int stream = fe_substream->stream;
2783 
2784 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2785 	fe->dpcm[stream].runtime = fe_substream->runtime;
2786 
2787 	ret = dpcm_path_get(fe, stream, &list);
2788 	if (ret < 0) {
2789 		mutex_unlock(&fe->card->mutex);
2790 		return ret;
2791 	} else if (ret == 0) {
2792 		dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2793 			fe->dai_link->name, stream ? "capture" : "playback");
2794 	}
2795 
2796 	/* calculate valid and active FE <-> BE dpcms */
2797 	dpcm_process_paths(fe, stream, &list, 1);
2798 
2799 	ret = dpcm_fe_dai_startup(fe_substream);
2800 	if (ret < 0) {
2801 		/* clean up all links */
2802 		for_each_dpcm_be(fe, stream, dpcm)
2803 			dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2804 
2805 		dpcm_be_disconnect(fe, stream);
2806 		fe->dpcm[stream].runtime = NULL;
2807 	}
2808 
2809 	dpcm_clear_pending_state(fe, stream);
2810 	dpcm_path_put(&list);
2811 	mutex_unlock(&fe->card->mutex);
2812 	return ret;
2813 }
2814 
2815 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2816 {
2817 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2818 	struct snd_soc_dpcm *dpcm;
2819 	int stream = fe_substream->stream, ret;
2820 
2821 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2822 	ret = dpcm_fe_dai_shutdown(fe_substream);
2823 
2824 	/* mark FE's links ready to prune */
2825 	for_each_dpcm_be(fe, stream, dpcm)
2826 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2827 
2828 	dpcm_be_disconnect(fe, stream);
2829 
2830 	fe->dpcm[stream].runtime = NULL;
2831 	mutex_unlock(&fe->card->mutex);
2832 	return ret;
2833 }
2834 
2835 /* create a new pcm */
2836 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2837 {
2838 	struct snd_soc_dai *codec_dai;
2839 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2840 	struct snd_soc_component *component;
2841 	struct snd_pcm *pcm;
2842 	char new_name[64];
2843 	int ret = 0, playback = 0, capture = 0;
2844 	int i;
2845 
2846 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2847 		playback = rtd->dai_link->dpcm_playback;
2848 		capture = rtd->dai_link->dpcm_capture;
2849 	} else {
2850 		/* Adapt stream for codec2codec links */
2851 		struct snd_soc_pcm_stream *cpu_capture = rtd->dai_link->params ?
2852 			&cpu_dai->driver->playback : &cpu_dai->driver->capture;
2853 		struct snd_soc_pcm_stream *cpu_playback = rtd->dai_link->params ?
2854 			&cpu_dai->driver->capture : &cpu_dai->driver->playback;
2855 
2856 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
2857 			if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2858 			    snd_soc_dai_stream_valid(cpu_dai,   SNDRV_PCM_STREAM_PLAYBACK))
2859 				playback = 1;
2860 			if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2861 			    snd_soc_dai_stream_valid(cpu_dai,   SNDRV_PCM_STREAM_CAPTURE))
2862 				capture = 1;
2863 		}
2864 
2865 		capture = capture && cpu_capture->channels_min;
2866 		playback = playback && cpu_playback->channels_min;
2867 	}
2868 
2869 	if (rtd->dai_link->playback_only) {
2870 		playback = 1;
2871 		capture = 0;
2872 	}
2873 
2874 	if (rtd->dai_link->capture_only) {
2875 		playback = 0;
2876 		capture = 1;
2877 	}
2878 
2879 	/* create the PCM */
2880 	if (rtd->dai_link->params) {
2881 		snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2882 			 rtd->dai_link->stream_name);
2883 
2884 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2885 					   playback, capture, &pcm);
2886 	} else if (rtd->dai_link->no_pcm) {
2887 		snprintf(new_name, sizeof(new_name), "(%s)",
2888 			rtd->dai_link->stream_name);
2889 
2890 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2891 				playback, capture, &pcm);
2892 	} else {
2893 		if (rtd->dai_link->dynamic)
2894 			snprintf(new_name, sizeof(new_name), "%s (*)",
2895 				rtd->dai_link->stream_name);
2896 		else
2897 			snprintf(new_name, sizeof(new_name), "%s %s-%d",
2898 				rtd->dai_link->stream_name,
2899 				(rtd->num_codecs > 1) ?
2900 				"multicodec" : rtd->codec_dai->name, num);
2901 
2902 		ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2903 			capture, &pcm);
2904 	}
2905 	if (ret < 0) {
2906 		dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2907 			rtd->dai_link->name);
2908 		return ret;
2909 	}
2910 	dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2911 
2912 	/* DAPM dai link stream work */
2913 	if (rtd->dai_link->params)
2914 		rtd->close_delayed_work_func = codec2codec_close_delayed_work;
2915 	else
2916 		rtd->close_delayed_work_func = snd_soc_close_delayed_work;
2917 
2918 	pcm->nonatomic = rtd->dai_link->nonatomic;
2919 	rtd->pcm = pcm;
2920 	pcm->private_data = rtd;
2921 
2922 	if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
2923 		if (playback)
2924 			pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2925 		if (capture)
2926 			pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2927 		goto out;
2928 	}
2929 
2930 	/* ASoC PCM operations */
2931 	if (rtd->dai_link->dynamic) {
2932 		rtd->ops.open		= dpcm_fe_dai_open;
2933 		rtd->ops.hw_params	= dpcm_fe_dai_hw_params;
2934 		rtd->ops.prepare	= dpcm_fe_dai_prepare;
2935 		rtd->ops.trigger	= dpcm_fe_dai_trigger;
2936 		rtd->ops.hw_free	= dpcm_fe_dai_hw_free;
2937 		rtd->ops.close		= dpcm_fe_dai_close;
2938 		rtd->ops.pointer	= soc_pcm_pointer;
2939 	} else {
2940 		rtd->ops.open		= soc_pcm_open;
2941 		rtd->ops.hw_params	= soc_pcm_hw_params;
2942 		rtd->ops.prepare	= soc_pcm_prepare;
2943 		rtd->ops.trigger	= soc_pcm_trigger;
2944 		rtd->ops.hw_free	= soc_pcm_hw_free;
2945 		rtd->ops.close		= soc_pcm_close;
2946 		rtd->ops.pointer	= soc_pcm_pointer;
2947 	}
2948 
2949 	for_each_rtd_components(rtd, i, component) {
2950 		const struct snd_soc_component_driver *drv = component->driver;
2951 
2952 		if (drv->ioctl)
2953 			rtd->ops.ioctl		= snd_soc_pcm_component_ioctl;
2954 		if (drv->sync_stop)
2955 			rtd->ops.sync_stop	= snd_soc_pcm_component_sync_stop;
2956 		if (drv->copy_user)
2957 			rtd->ops.copy_user	= snd_soc_pcm_component_copy_user;
2958 		if (drv->page)
2959 			rtd->ops.page		= snd_soc_pcm_component_page;
2960 		if (drv->mmap)
2961 			rtd->ops.mmap		= snd_soc_pcm_component_mmap;
2962 	}
2963 
2964 	if (playback)
2965 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2966 
2967 	if (capture)
2968 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2969 
2970 	ret = snd_soc_pcm_component_new(rtd);
2971 	if (ret < 0) {
2972 		dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret);
2973 		return ret;
2974 	}
2975 
2976 	pcm->no_device_suspend = true;
2977 out:
2978 	dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2979 		 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2980 		 cpu_dai->name);
2981 	return ret;
2982 }
2983 
2984 /* is the current PCM operation for this FE ? */
2985 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2986 {
2987 	if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2988 		return 1;
2989 	return 0;
2990 }
2991 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2992 
2993 /* is the current PCM operation for this BE ? */
2994 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2995 		struct snd_soc_pcm_runtime *be, int stream)
2996 {
2997 	if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2998 	   ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2999 		  be->dpcm[stream].runtime_update))
3000 		return 1;
3001 	return 0;
3002 }
3003 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3004 
3005 /* get the substream for this BE */
3006 struct snd_pcm_substream *
3007 	snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3008 {
3009 	return be->pcm->streams[stream].substream;
3010 }
3011 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3012 
3013 /* get the BE runtime state */
3014 enum snd_soc_dpcm_state
3015 	snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3016 {
3017 	return be->dpcm[stream].state;
3018 }
3019 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3020 
3021 /* set the BE runtime state */
3022 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3023 		int stream, enum snd_soc_dpcm_state state)
3024 {
3025 	be->dpcm[stream].state = state;
3026 }
3027 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3028 
3029 /*
3030  * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3031  * are not running, paused or suspended for the specified stream direction.
3032  */
3033 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3034 		struct snd_soc_pcm_runtime *be, int stream)
3035 {
3036 	struct snd_soc_dpcm *dpcm;
3037 	int state;
3038 	int ret = 1;
3039 	unsigned long flags;
3040 
3041 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3042 	for_each_dpcm_fe(be, stream, dpcm) {
3043 
3044 		if (dpcm->fe == fe)
3045 			continue;
3046 
3047 		state = dpcm->fe->dpcm[stream].state;
3048 		if (state == SND_SOC_DPCM_STATE_START ||
3049 			state == SND_SOC_DPCM_STATE_PAUSED ||
3050 			state == SND_SOC_DPCM_STATE_SUSPEND) {
3051 			ret = 0;
3052 			break;
3053 		}
3054 	}
3055 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3056 
3057 	/* it's safe to free/stop this BE DAI */
3058 	return ret;
3059 }
3060 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3061 
3062 /*
3063  * We can only change hw params a BE DAI if any of it's FE are not prepared,
3064  * running, paused or suspended for the specified stream direction.
3065  */
3066 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3067 		struct snd_soc_pcm_runtime *be, int stream)
3068 {
3069 	struct snd_soc_dpcm *dpcm;
3070 	int state;
3071 	int ret = 1;
3072 	unsigned long flags;
3073 
3074 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3075 	for_each_dpcm_fe(be, stream, dpcm) {
3076 
3077 		if (dpcm->fe == fe)
3078 			continue;
3079 
3080 		state = dpcm->fe->dpcm[stream].state;
3081 		if (state == SND_SOC_DPCM_STATE_START ||
3082 			state == SND_SOC_DPCM_STATE_PAUSED ||
3083 			state == SND_SOC_DPCM_STATE_SUSPEND ||
3084 			state == SND_SOC_DPCM_STATE_PREPARE) {
3085 			ret = 0;
3086 			break;
3087 		}
3088 	}
3089 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3090 
3091 	/* it's safe to change hw_params */
3092 	return ret;
3093 }
3094 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3095 
3096 #ifdef CONFIG_DEBUG_FS
3097 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
3098 {
3099 	switch (state) {
3100 	case SND_SOC_DPCM_STATE_NEW:
3101 		return "new";
3102 	case SND_SOC_DPCM_STATE_OPEN:
3103 		return "open";
3104 	case SND_SOC_DPCM_STATE_HW_PARAMS:
3105 		return "hw_params";
3106 	case SND_SOC_DPCM_STATE_PREPARE:
3107 		return "prepare";
3108 	case SND_SOC_DPCM_STATE_START:
3109 		return "start";
3110 	case SND_SOC_DPCM_STATE_STOP:
3111 		return "stop";
3112 	case SND_SOC_DPCM_STATE_SUSPEND:
3113 		return "suspend";
3114 	case SND_SOC_DPCM_STATE_PAUSED:
3115 		return "paused";
3116 	case SND_SOC_DPCM_STATE_HW_FREE:
3117 		return "hw_free";
3118 	case SND_SOC_DPCM_STATE_CLOSE:
3119 		return "close";
3120 	}
3121 
3122 	return "unknown";
3123 }
3124 
3125 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3126 				int stream, char *buf, size_t size)
3127 {
3128 	struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3129 	struct snd_soc_dpcm *dpcm;
3130 	ssize_t offset = 0;
3131 	unsigned long flags;
3132 
3133 	/* FE state */
3134 	offset += snprintf(buf + offset, size - offset,
3135 			"[%s - %s]\n", fe->dai_link->name,
3136 			stream ? "Capture" : "Playback");
3137 
3138 	offset += snprintf(buf + offset, size - offset, "State: %s\n",
3139 	                dpcm_state_string(fe->dpcm[stream].state));
3140 
3141 	if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3142 	    (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3143 		offset += snprintf(buf + offset, size - offset,
3144 				"Hardware Params: "
3145 				"Format = %s, Channels = %d, Rate = %d\n",
3146 				snd_pcm_format_name(params_format(params)),
3147 				params_channels(params),
3148 				params_rate(params));
3149 
3150 	/* BEs state */
3151 	offset += snprintf(buf + offset, size - offset, "Backends:\n");
3152 
3153 	if (list_empty(&fe->dpcm[stream].be_clients)) {
3154 		offset += snprintf(buf + offset, size - offset,
3155 				" No active DSP links\n");
3156 		goto out;
3157 	}
3158 
3159 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3160 	for_each_dpcm_be(fe, stream, dpcm) {
3161 		struct snd_soc_pcm_runtime *be = dpcm->be;
3162 		params = &dpcm->hw_params;
3163 
3164 		offset += snprintf(buf + offset, size - offset,
3165 				"- %s\n", be->dai_link->name);
3166 
3167 		offset += snprintf(buf + offset, size - offset,
3168 				"   State: %s\n",
3169 				dpcm_state_string(be->dpcm[stream].state));
3170 
3171 		if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3172 		    (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3173 			offset += snprintf(buf + offset, size - offset,
3174 				"   Hardware Params: "
3175 				"Format = %s, Channels = %d, Rate = %d\n",
3176 				snd_pcm_format_name(params_format(params)),
3177 				params_channels(params),
3178 				params_rate(params));
3179 	}
3180 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3181 out:
3182 	return offset;
3183 }
3184 
3185 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3186 				size_t count, loff_t *ppos)
3187 {
3188 	struct snd_soc_pcm_runtime *fe = file->private_data;
3189 	ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3190 	char *buf;
3191 
3192 	buf = kmalloc(out_count, GFP_KERNEL);
3193 	if (!buf)
3194 		return -ENOMEM;
3195 
3196 	if (snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
3197 		offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3198 					buf + offset, out_count - offset);
3199 
3200 	if (snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
3201 		offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3202 					buf + offset, out_count - offset);
3203 
3204 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3205 
3206 	kfree(buf);
3207 	return ret;
3208 }
3209 
3210 static const struct file_operations dpcm_state_fops = {
3211 	.open = simple_open,
3212 	.read = dpcm_state_read_file,
3213 	.llseek = default_llseek,
3214 };
3215 
3216 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3217 {
3218 	if (!rtd->dai_link)
3219 		return;
3220 
3221 	if (!rtd->dai_link->dynamic)
3222 		return;
3223 
3224 	if (!rtd->card->debugfs_card_root)
3225 		return;
3226 
3227 	rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3228 			rtd->card->debugfs_card_root);
3229 
3230 	debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3231 			    rtd, &dpcm_state_fops);
3232 }
3233 #endif
3234