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