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