xref: /linux/sound/soc/soc-pcm.c (revision 6b382bdfe26a2232091bf743e454e6794295783e)
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 ch_mask = snd_soc_dai_tdm_mask_get(codec_dai, substream->stream);
1210 		struct snd_soc_dai_link_ch_map *ch_maps;
1211 		int j;
1212 
1213 		/*
1214 		 * Skip CODECs which don't support the current stream type,
1215 		 * the idea being that if a CODEC is not used for the currently
1216 		 * set up transfer direction, it should not need to be
1217 		 * configured, especially since the configuration used might
1218 		 * not even be supported by that CODEC. There may be cases
1219 		 * however where a CODEC needs to be set up although it is
1220 		 * actually not being used for the transfer, e.g. if a
1221 		 * capture-only CODEC is acting as an LRCLK and/or BCLK master
1222 		 * for the DAI link including a playback-only CODEC.
1223 		 * If this becomes necessary, we will have to augment the
1224 		 * machine driver setup with information on how to act, so
1225 		 * we can do the right thing here.
1226 		 */
1227 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1228 			continue;
1229 
1230 		/* copy params for each codec */
1231 		tmp_params = *params;
1232 
1233 		/* fixup params based on TDM or ch_map masks */
1234 		if (!ch_mask) {
1235 			for_each_rtd_ch_maps(rtd, j, ch_maps)
1236 				if (ch_maps->codec == i)
1237 					ch_mask |= ch_maps->codec_ch_mask;
1238 		}
1239 
1240 		if (ch_mask)
1241 			soc_pcm_codec_params_fixup(&tmp_params, ch_mask);
1242 
1243 		ret = snd_soc_dai_hw_params(codec_dai, substream,
1244 					    &tmp_params);
1245 		if(ret < 0)
1246 			goto out;
1247 
1248 		soc_pcm_set_dai_params(codec_dai, &tmp_params);
1249 		snd_soc_dapm_update_dai(substream, &tmp_params, codec_dai);
1250 	}
1251 
1252 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1253 		struct snd_soc_dai_link_ch_map *ch_maps;
1254 		unsigned int ch_mask = 0;
1255 		int j;
1256 
1257 		/*
1258 		 * Skip CPUs which don't support the current stream
1259 		 * type. See soc_pcm_init_runtime_hw() for more details
1260 		 */
1261 		if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
1262 			continue;
1263 
1264 		/* copy params for each cpu */
1265 		tmp_params = *params;
1266 
1267 		/*
1268 		 * construct cpu channel mask by combining ch_mask of each
1269 		 * codec which maps to the cpu.
1270 		 * see
1271 		 *	soc.h :: [dai_link->ch_maps Image sample]
1272 		 */
1273 		for_each_rtd_ch_maps(rtd, j, ch_maps)
1274 			if (ch_maps->cpu == i)
1275 				ch_mask |= ch_maps->cpu_ch_mask;
1276 
1277 		/* fixup cpu channel number */
1278 		if (ch_mask)
1279 			soc_pcm_codec_params_fixup(&tmp_params, ch_mask);
1280 
1281 		ret = snd_soc_dai_hw_params(cpu_dai, substream, &tmp_params);
1282 		if (ret < 0)
1283 			goto out;
1284 
1285 		/* store the parameters for each DAI */
1286 		soc_pcm_set_dai_params(cpu_dai, &tmp_params);
1287 		snd_soc_dapm_update_dai(substream, &tmp_params, cpu_dai);
1288 	}
1289 
1290 	ret = snd_soc_pcm_component_hw_params(substream, params);
1291 out:
1292 	if (ret < 0)
1293 		soc_pcm_hw_clean(rtd, substream, 1);
1294 
1295 	return soc_pcm_ret(rtd, ret);
1296 }
1297 
1298 /* hw_params PCM ops for non-DPCM streams */
1299 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
1300 			     struct snd_pcm_hw_params *params)
1301 {
1302 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1303 	int ret;
1304 
1305 	snd_soc_dpcm_mutex_lock(rtd);
1306 	ret = __soc_pcm_hw_params(substream, params);
1307 	snd_soc_dpcm_mutex_unlock(rtd);
1308 	return ret;
1309 }
1310 
1311 #define TRIGGER_MAX 3
1312 static int (* const trigger[][TRIGGER_MAX])(struct snd_pcm_substream *substream, int cmd, int rollback) = {
1313 	[SND_SOC_TRIGGER_ORDER_DEFAULT] = {
1314 		snd_soc_link_trigger,
1315 		snd_soc_pcm_component_trigger,
1316 		snd_soc_pcm_dai_trigger,
1317 	},
1318 	[SND_SOC_TRIGGER_ORDER_LDC] = {
1319 		snd_soc_link_trigger,
1320 		snd_soc_pcm_dai_trigger,
1321 		snd_soc_pcm_component_trigger,
1322 	},
1323 };
1324 
1325 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1326 {
1327 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1328 	struct snd_soc_component *component;
1329 	int ret = 0, r = 0, i;
1330 	int rollback = 0;
1331 	int start = 0, stop = 0;
1332 
1333 	/*
1334 	 * select START/STOP sequence
1335 	 */
1336 	for_each_rtd_components(rtd, i, component) {
1337 		if (component->driver->trigger_start)
1338 			start = component->driver->trigger_start;
1339 		if (component->driver->trigger_stop)
1340 			stop = component->driver->trigger_stop;
1341 	}
1342 	if (rtd->dai_link->trigger_start)
1343 		start = rtd->dai_link->trigger_start;
1344 	if (rtd->dai_link->trigger_stop)
1345 		stop  = rtd->dai_link->trigger_stop;
1346 
1347 	if (start < 0 || start >= SND_SOC_TRIGGER_ORDER_MAX ||
1348 	    stop  < 0 || stop  >= SND_SOC_TRIGGER_ORDER_MAX)
1349 		return -EINVAL;
1350 
1351 	/*
1352 	 * START
1353 	 */
1354 	switch (cmd) {
1355 	case SNDRV_PCM_TRIGGER_START:
1356 	case SNDRV_PCM_TRIGGER_RESUME:
1357 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1358 		for (i = 0; i < TRIGGER_MAX; i++) {
1359 			r = trigger[start][i](substream, cmd, 0);
1360 			if (r < 0)
1361 				break;
1362 		}
1363 	}
1364 
1365 	/*
1366 	 * Rollback if START failed
1367 	 * find correspond STOP command
1368 	 */
1369 	if (r < 0) {
1370 		rollback = 1;
1371 		ret = r;
1372 		switch (cmd) {
1373 		case SNDRV_PCM_TRIGGER_START:
1374 			cmd = SNDRV_PCM_TRIGGER_STOP;
1375 			break;
1376 		case SNDRV_PCM_TRIGGER_RESUME:
1377 			cmd = SNDRV_PCM_TRIGGER_SUSPEND;
1378 			break;
1379 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1380 			cmd = SNDRV_PCM_TRIGGER_PAUSE_PUSH;
1381 			break;
1382 		}
1383 	}
1384 
1385 	/*
1386 	 * STOP
1387 	 */
1388 	switch (cmd) {
1389 	case SNDRV_PCM_TRIGGER_STOP:
1390 	case SNDRV_PCM_TRIGGER_SUSPEND:
1391 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1392 		for (i = TRIGGER_MAX; i > 0; i--) {
1393 			r = trigger[stop][i - 1](substream, cmd, rollback);
1394 			if (r < 0)
1395 				ret = r;
1396 		}
1397 	}
1398 
1399 	return ret;
1400 }
1401 
1402 /*
1403  * soc level wrapper for pointer callback
1404  * If cpu_dai, codec_dai, component driver has the delay callback, then
1405  * the runtime->delay will be updated via snd_soc_pcm_component/dai_delay().
1406  */
1407 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1408 {
1409 	struct snd_pcm_runtime *runtime = substream->runtime;
1410 	snd_pcm_uframes_t offset = 0;
1411 	snd_pcm_sframes_t codec_delay = 0;
1412 	snd_pcm_sframes_t cpu_delay = 0;
1413 
1414 	offset = snd_soc_pcm_component_pointer(substream);
1415 
1416 	/* should be called *after* snd_soc_pcm_component_pointer() */
1417 	snd_soc_pcm_dai_delay(substream, &cpu_delay, &codec_delay);
1418 	snd_soc_pcm_component_delay(substream, &cpu_delay, &codec_delay);
1419 
1420 	runtime->delay = cpu_delay + codec_delay;
1421 
1422 	return offset;
1423 }
1424 
1425 /* connect a FE and BE */
1426 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1427 		struct snd_soc_pcm_runtime *be, int stream)
1428 {
1429 	struct snd_pcm_substream *fe_substream;
1430 	struct snd_pcm_substream *be_substream;
1431 	struct snd_soc_dpcm *dpcm;
1432 
1433 	snd_soc_dpcm_mutex_assert_held(fe);
1434 
1435 	/* only add new dpcms */
1436 	for_each_dpcm_be(fe, stream, dpcm)
1437 		if (dpcm->be == be)
1438 			return 0;
1439 
1440 	fe_substream = snd_soc_dpcm_get_substream(fe, stream);
1441 	be_substream = snd_soc_dpcm_get_substream(be, stream);
1442 
1443 	if (!fe_substream->pcm->nonatomic && be_substream->pcm->nonatomic)
1444 		return snd_soc_ret(be->dev, -EINVAL,
1445 			"%s: %s is atomic but %s is nonatomic, invalid configuration\n",
1446 				   __func__, fe->dai_link->name, be->dai_link->name);
1447 
1448 	if (fe_substream->pcm->nonatomic && !be_substream->pcm->nonatomic) {
1449 		dev_dbg(be->dev, "FE is nonatomic but BE is not, forcing BE as nonatomic\n");
1450 		be_substream->pcm->nonatomic = 1;
1451 	}
1452 
1453 	dpcm = kzalloc_obj(struct snd_soc_dpcm);
1454 	if (!dpcm)
1455 		return -ENOMEM;
1456 
1457 	dpcm->be = be;
1458 	dpcm->fe = fe;
1459 	dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1460 	snd_pcm_stream_lock_irq(fe_substream);
1461 	list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1462 	list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1463 	snd_pcm_stream_unlock_irq(fe_substream);
1464 
1465 	dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1466 			snd_pcm_direction_name(stream),  fe->dai_link->name,
1467 			stream ? "<-" : "->", be->dai_link->name);
1468 
1469 	dpcm_create_debugfs_state(dpcm, stream);
1470 
1471 	return 1;
1472 }
1473 
1474 /* reparent a BE onto another FE */
1475 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1476 			struct snd_soc_pcm_runtime *be, int stream)
1477 {
1478 	struct snd_soc_dpcm *dpcm;
1479 	struct snd_pcm_substream *fe_substream, *be_substream;
1480 
1481 	/* reparent if BE is connected to other FEs */
1482 	if (!be->dpcm[stream].users)
1483 		return;
1484 
1485 	be_substream = snd_soc_dpcm_get_substream(be, stream);
1486 	if (!be_substream)
1487 		return;
1488 
1489 	for_each_dpcm_fe(be, stream, dpcm) {
1490 		if (dpcm->fe == fe)
1491 			continue;
1492 
1493 		dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1494 			snd_pcm_direction_name(stream),
1495 			dpcm->fe->dai_link->name,
1496 			stream ? "<-" : "->", dpcm->be->dai_link->name);
1497 
1498 		fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1499 		be_substream->runtime = fe_substream->runtime;
1500 		break;
1501 	}
1502 }
1503 
1504 /* disconnect a BE and FE */
1505 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1506 {
1507 	struct snd_soc_dpcm *dpcm, *d;
1508 	struct snd_pcm_substream *substream = snd_soc_dpcm_get_substream(fe, stream);
1509 	LIST_HEAD(deleted_dpcms);
1510 
1511 	snd_soc_dpcm_mutex_assert_held(fe);
1512 
1513 	snd_pcm_stream_lock_irq(substream);
1514 	for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1515 		dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1516 				snd_pcm_direction_name(stream),
1517 				dpcm->be->dai_link->name);
1518 
1519 		if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1520 			continue;
1521 
1522 		dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1523 			snd_pcm_direction_name(stream), fe->dai_link->name,
1524 			stream ? "<-" : "->", dpcm->be->dai_link->name);
1525 
1526 		/* BEs still alive need new FE */
1527 		dpcm_be_reparent(fe, dpcm->be, stream);
1528 
1529 		list_del(&dpcm->list_be);
1530 		list_move(&dpcm->list_fe, &deleted_dpcms);
1531 	}
1532 	snd_pcm_stream_unlock_irq(substream);
1533 
1534 	while (!list_empty(&deleted_dpcms)) {
1535 		dpcm = list_first_entry(&deleted_dpcms, struct snd_soc_dpcm,
1536 					list_fe);
1537 		list_del(&dpcm->list_fe);
1538 		dpcm_remove_debugfs_state(dpcm);
1539 		kfree(dpcm);
1540 	}
1541 }
1542 
1543 /* get BE for DAI widget and stream */
1544 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1545 		struct snd_soc_dapm_widget *widget, int stream)
1546 {
1547 	struct snd_soc_pcm_runtime *be;
1548 	struct snd_soc_dapm_widget *w;
1549 	struct snd_soc_dai *dai;
1550 	int i;
1551 
1552 	dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1553 
1554 	for_each_card_rtds(card, be) {
1555 
1556 		if (!be->dai_link->no_pcm)
1557 			continue;
1558 
1559 		if (!snd_soc_dpcm_get_substream(be, stream))
1560 			continue;
1561 
1562 		for_each_rtd_dais(be, i, dai) {
1563 			w = snd_soc_dai_get_widget(dai, stream);
1564 
1565 			dev_dbg(card->dev, "ASoC: try BE : %s\n",
1566 				w ? w->name : "(not set)");
1567 
1568 			if (w == widget)
1569 				return be;
1570 		}
1571 	}
1572 
1573 	/* Widget provided is not a BE */
1574 	return NULL;
1575 }
1576 
1577 int widget_in_list(struct snd_soc_dapm_widget_list *list,
1578 		struct snd_soc_dapm_widget *widget)
1579 {
1580 	struct snd_soc_dapm_widget *w;
1581 	int i;
1582 
1583 	for_each_dapm_widgets(list, i, w)
1584 		if (widget == w)
1585 			return 1;
1586 
1587 	return 0;
1588 }
1589 EXPORT_SYMBOL_GPL(widget_in_list);
1590 
1591 bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget, enum snd_soc_dapm_direction dir)
1592 {
1593 	struct snd_soc_card *card = snd_soc_dapm_to_card(widget->dapm);
1594 	struct snd_soc_pcm_runtime *rtd;
1595 	int stream;
1596 
1597 	/* adjust dir to stream */
1598 	if (dir == SND_SOC_DAPM_DIR_OUT)
1599 		stream = SNDRV_PCM_STREAM_PLAYBACK;
1600 	else
1601 		stream = SNDRV_PCM_STREAM_CAPTURE;
1602 
1603 	rtd = dpcm_get_be(card, widget, stream);
1604 	if (rtd)
1605 		return true;
1606 
1607 	return false;
1608 }
1609 EXPORT_SYMBOL_GPL(dpcm_end_walk_at_be);
1610 
1611 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1612 	int stream, struct snd_soc_dapm_widget_list **list)
1613 {
1614 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(fe, 0);
1615 	int paths;
1616 
1617 	if (fe->dai_link->num_cpus > 1)
1618 		return snd_soc_ret(fe->dev, -EINVAL,
1619 			"%s doesn't support Multi CPU yet\n", __func__);
1620 
1621 	/* get number of valid DAI paths and their widgets */
1622 	paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1623 			fe->card->component_chaining ?
1624 				NULL : dpcm_end_walk_at_be);
1625 
1626 	if (paths > 0)
1627 		dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1628 			snd_pcm_direction_name(stream));
1629 	else if (paths == 0)
1630 		dev_dbg(fe->dev, "ASoC: %s no valid %s path\n", fe->dai_link->name,
1631 			snd_pcm_direction_name(stream));
1632 
1633 	return paths;
1634 }
1635 
1636 void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1637 {
1638 	snd_soc_dapm_dai_free_widgets(list);
1639 }
1640 
1641 static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
1642 			      struct snd_soc_dapm_widget_list *list)
1643 {
1644 	struct snd_soc_dai *dai;
1645 	unsigned int i;
1646 
1647 	/* is there a valid DAI widget for this BE */
1648 	for_each_rtd_dais(dpcm->be, i, dai) {
1649 		struct snd_soc_dapm_widget *widget = snd_soc_dai_get_widget(dai, stream);
1650 
1651 		/*
1652 		 * The BE is pruned only if none of the dai
1653 		 * widgets are in the active list.
1654 		 */
1655 		if (widget && widget_in_list(list, widget))
1656 			return true;
1657 	}
1658 
1659 	return false;
1660 }
1661 
1662 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1663 			    struct snd_soc_dapm_widget_list **list_)
1664 {
1665 	struct snd_soc_dpcm *dpcm;
1666 	int prune = 0;
1667 
1668 	/* Destroy any old FE <--> BE connections */
1669 	for_each_dpcm_be(fe, stream, dpcm) {
1670 		if (dpcm_be_is_active(dpcm, stream, *list_))
1671 			continue;
1672 
1673 		dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1674 			snd_pcm_direction_name(stream),
1675 			dpcm->be->dai_link->name, fe->dai_link->name);
1676 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1677 		dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_BE);
1678 		prune++;
1679 	}
1680 
1681 	dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1682 	return prune;
1683 }
1684 
1685 int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1686 		   struct snd_soc_dapm_widget_list **list_)
1687 {
1688 	struct snd_soc_card *card = fe->card;
1689 	struct snd_soc_dapm_widget_list *list = *list_;
1690 	struct snd_soc_pcm_runtime *be;
1691 	struct snd_soc_dapm_widget *widget;
1692 	struct snd_pcm_substream *fe_substream = snd_soc_dpcm_get_substream(fe, stream);
1693 	int i, new = 0, err;
1694 
1695 	/* don't connect if FE is not running */
1696 	if (!fe_substream->runtime && !fe->fe_compr)
1697 		return new;
1698 
1699 	/* Create any new FE <--> BE connections */
1700 	for_each_dapm_widgets(list, i, widget) {
1701 
1702 		switch (widget->id) {
1703 		case snd_soc_dapm_dai_in:
1704 			if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1705 				continue;
1706 			break;
1707 		case snd_soc_dapm_dai_out:
1708 			if (stream != SNDRV_PCM_STREAM_CAPTURE)
1709 				continue;
1710 			break;
1711 		default:
1712 			continue;
1713 		}
1714 
1715 		/* is there a valid BE rtd for this widget */
1716 		be = dpcm_get_be(card, widget, stream);
1717 		if (!be) {
1718 			dev_dbg(fe->dev, "ASoC: no BE found for %s\n",
1719 				widget->name);
1720 			continue;
1721 		}
1722 
1723 		/*
1724 		 * Filter for systems with 'component_chaining' enabled.
1725 		 * This helps to avoid unnecessary re-configuration of an
1726 		 * already active BE on such systems and ensures the BE DAI
1727 		 * widget is powered ON after hw_params() BE DAI callback.
1728 		 */
1729 		if (fe->card->component_chaining &&
1730 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1731 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1732 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1733 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1734 			continue;
1735 
1736 		/* newly connected FE and BE */
1737 		err = dpcm_be_connect(fe, be, stream);
1738 		if (err < 0) {
1739 			dev_err(fe->dev, "ASoC: can't connect %s\n",
1740 				widget->name);
1741 			break;
1742 		} else if (err == 0) /* already connected */
1743 			continue;
1744 
1745 		/* new */
1746 		dpcm_set_be_update_state(be, stream, SND_SOC_DPCM_UPDATE_BE);
1747 		new++;
1748 	}
1749 
1750 	dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1751 	return new;
1752 }
1753 
1754 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1755 {
1756 	struct snd_soc_dpcm *dpcm;
1757 
1758 	for_each_dpcm_be(fe, stream, dpcm)
1759 		dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_NO);
1760 }
1761 
1762 void dpcm_be_dai_stop(struct snd_soc_pcm_runtime *fe, int stream,
1763 		      int do_hw_free, struct snd_soc_dpcm *last)
1764 {
1765 	struct snd_soc_dpcm *dpcm;
1766 
1767 	/* disable any enabled and non active backends */
1768 	for_each_dpcm_be(fe, stream, dpcm) {
1769 		struct snd_soc_pcm_runtime *be = dpcm->be;
1770 		struct snd_pcm_substream *be_substream =
1771 			snd_soc_dpcm_get_substream(be, stream);
1772 
1773 		if (dpcm == last)
1774 			return;
1775 
1776 		/* is this op for this BE ? */
1777 		if (!snd_soc_dpcm_can_be_update(fe, be, stream))
1778 			continue;
1779 
1780 		if (be->dpcm[stream].users == 0) {
1781 			dev_err(be->dev, "ASoC: no users %s at close - state %s\n",
1782 				snd_pcm_direction_name(stream),
1783 				dpcm_state_string(be->dpcm[stream].state));
1784 			continue;
1785 		}
1786 
1787 		if (--be->dpcm[stream].users != 0)
1788 			continue;
1789 
1790 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) {
1791 			if (!do_hw_free)
1792 				continue;
1793 
1794 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) {
1795 				__soc_pcm_hw_free(be, be_substream);
1796 				be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1797 			}
1798 		}
1799 
1800 		__soc_pcm_close(be, be_substream);
1801 		be_substream->runtime = NULL;
1802 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1803 	}
1804 }
1805 
1806 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1807 {
1808 	struct snd_pcm_substream *fe_substream = snd_soc_dpcm_get_substream(fe, stream);
1809 	struct snd_soc_pcm_runtime *be;
1810 	struct snd_soc_dpcm *dpcm;
1811 	int err, count = 0;
1812 
1813 	/* only startup BE DAIs that are either sinks or sources to this FE DAI */
1814 	for_each_dpcm_be(fe, stream, dpcm) {
1815 		struct snd_pcm_substream *be_substream;
1816 
1817 		be = dpcm->be;
1818 		be_substream = snd_soc_dpcm_get_substream(be, stream);
1819 
1820 		if (!be_substream) {
1821 			dev_err(be->dev, "ASoC: no backend %s stream\n",
1822 				snd_pcm_direction_name(stream));
1823 			continue;
1824 		}
1825 
1826 		/* is this op for this BE ? */
1827 		if (!snd_soc_dpcm_can_be_update(fe, be, stream))
1828 			continue;
1829 
1830 		/* first time the dpcm is open ? */
1831 		if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) {
1832 			dev_err(be->dev, "ASoC: too many users %s at open %s\n",
1833 				snd_pcm_direction_name(stream),
1834 				dpcm_state_string(be->dpcm[stream].state));
1835 			continue;
1836 		}
1837 
1838 		if (be->dpcm[stream].users++ != 0)
1839 			continue;
1840 
1841 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1842 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1843 			continue;
1844 
1845 		dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1846 			snd_pcm_direction_name(stream), be->dai_link->name);
1847 
1848 		be_substream->runtime = fe_substream->runtime;
1849 		err = __soc_pcm_open(be, be_substream);
1850 		if (err < 0) {
1851 			be->dpcm[stream].users--;
1852 			if (be->dpcm[stream].users < 0)
1853 				dev_err(be->dev, "ASoC: no users %s at unwind %s\n",
1854 					snd_pcm_direction_name(stream),
1855 					dpcm_state_string(be->dpcm[stream].state));
1856 
1857 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1858 			goto unwind;
1859 		}
1860 		be->dpcm[stream].be_start = 0;
1861 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1862 		count++;
1863 	}
1864 
1865 	return count;
1866 
1867 unwind:
1868 	dpcm_be_dai_startup_rollback(fe, stream, dpcm);
1869 
1870 	return soc_pcm_ret(fe, err);
1871 }
1872 
1873 static void dpcm_runtime_setup_fe(struct snd_pcm_substream *substream)
1874 {
1875 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
1876 	struct snd_pcm_runtime *runtime = substream->runtime;
1877 	struct snd_pcm_hardware *hw = &runtime->hw;
1878 	struct snd_soc_dai *dai;
1879 	int stream = substream->stream;
1880 	int i;
1881 
1882 	soc_pcm_hw_init(hw, false);
1883 
1884 	for_each_rtd_cpu_dais(fe, i, dai) {
1885 		const struct snd_soc_pcm_stream *cpu_stream;
1886 
1887 		/*
1888 		 * Skip CPUs which don't support the current stream
1889 		 * type. See soc_pcm_init_runtime_hw() for more details
1890 		 */
1891 		if (!snd_soc_dai_stream_valid(dai, stream))
1892 			continue;
1893 
1894 		cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1895 
1896 		soc_pcm_hw_update_rate(hw, cpu_stream);
1897 		soc_pcm_hw_update_chan(hw, cpu_stream);
1898 		soc_pcm_hw_update_format(hw, cpu_stream);
1899 	}
1900 
1901 }
1902 
1903 static void dpcm_runtime_setup_be_format(struct snd_pcm_substream *substream)
1904 {
1905 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
1906 	struct snd_pcm_runtime *runtime = substream->runtime;
1907 	struct snd_pcm_hardware *hw = &runtime->hw;
1908 	struct snd_soc_dpcm *dpcm;
1909 	struct snd_soc_dai *dai;
1910 	int stream = substream->stream;
1911 
1912 	if (!fe->dai_link->dpcm_merged_format)
1913 		return;
1914 
1915 	/*
1916 	 * It returns merged BE codec format
1917 	 * if FE want to use it (= dpcm_merged_format)
1918 	 */
1919 
1920 	for_each_dpcm_be(fe, stream, dpcm) {
1921 		struct snd_soc_pcm_runtime *be = dpcm->be;
1922 		const struct snd_soc_pcm_stream *codec_stream;
1923 		int i;
1924 
1925 		for_each_rtd_codec_dais(be, i, dai) {
1926 			/*
1927 			 * Skip CODECs which don't support the current stream
1928 			 * type. See soc_pcm_init_runtime_hw() for more details
1929 			 */
1930 			if (!snd_soc_dai_stream_valid(dai, stream))
1931 				continue;
1932 
1933 			codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1934 
1935 			soc_pcm_hw_update_format(hw, codec_stream);
1936 		}
1937 	}
1938 }
1939 
1940 static void dpcm_runtime_setup_be_chan(struct snd_pcm_substream *substream)
1941 {
1942 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
1943 	struct snd_pcm_runtime *runtime = substream->runtime;
1944 	struct snd_pcm_hardware *hw = &runtime->hw;
1945 	struct snd_soc_dpcm *dpcm;
1946 	int stream = substream->stream;
1947 
1948 	if (!fe->dai_link->dpcm_merged_chan)
1949 		return;
1950 
1951 	/*
1952 	 * It returns merged BE codec channel;
1953 	 * if FE want to use it (= dpcm_merged_chan)
1954 	 */
1955 
1956 	for_each_dpcm_be(fe, stream, dpcm) {
1957 		struct snd_soc_pcm_runtime *be = dpcm->be;
1958 		const struct snd_soc_pcm_stream *cpu_stream;
1959 		struct snd_soc_dai *dai;
1960 		int i;
1961 
1962 		for_each_rtd_cpu_dais(be, i, dai) {
1963 			/*
1964 			 * Skip CPUs which don't support the current stream
1965 			 * type. See soc_pcm_init_runtime_hw() for more details
1966 			 */
1967 			if (!snd_soc_dai_stream_valid(dai, stream))
1968 				continue;
1969 
1970 			cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1971 
1972 			soc_pcm_hw_update_chan(hw, cpu_stream);
1973 		}
1974 
1975 		/*
1976 		 * chan min/max cannot be enforced if there are multiple CODEC
1977 		 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1978 		 */
1979 		if (be->dai_link->num_codecs == 1) {
1980 			const struct snd_soc_pcm_stream *codec_stream = snd_soc_dai_get_pcm_stream(
1981 				snd_soc_rtd_to_codec(be, 0), stream);
1982 
1983 			soc_pcm_hw_update_chan(hw, codec_stream);
1984 		}
1985 	}
1986 }
1987 
1988 static void dpcm_runtime_setup_be_rate(struct snd_pcm_substream *substream)
1989 {
1990 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
1991 	struct snd_pcm_runtime *runtime = substream->runtime;
1992 	struct snd_pcm_hardware *hw = &runtime->hw;
1993 	struct snd_soc_dpcm *dpcm;
1994 	int stream = substream->stream;
1995 
1996 	if (!fe->dai_link->dpcm_merged_rate)
1997 		return;
1998 
1999 	/*
2000 	 * It returns merged BE codec channel;
2001 	 * if FE want to use it (= dpcm_merged_chan)
2002 	 */
2003 
2004 	for_each_dpcm_be(fe, stream, dpcm) {
2005 		struct snd_soc_pcm_runtime *be = dpcm->be;
2006 		const struct snd_soc_pcm_stream *pcm;
2007 		struct snd_soc_dai *dai;
2008 		int i;
2009 
2010 		for_each_rtd_dais(be, i, dai) {
2011 			/*
2012 			 * Skip DAIs which don't support the current stream
2013 			 * type. See soc_pcm_init_runtime_hw() for more details
2014 			 */
2015 			if (!snd_soc_dai_stream_valid(dai, stream))
2016 				continue;
2017 
2018 			pcm = snd_soc_dai_get_pcm_stream(dai, stream);
2019 
2020 			soc_pcm_hw_update_rate(hw, pcm);
2021 		}
2022 	}
2023 }
2024 
2025 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
2026 			       int stream)
2027 {
2028 	struct snd_soc_dpcm *dpcm;
2029 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream);
2030 	struct snd_soc_dai *fe_cpu_dai;
2031 	int err = 0;
2032 	int i;
2033 
2034 	/* apply symmetry for FE */
2035 	soc_pcm_update_symmetry(fe_substream);
2036 
2037 	for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
2038 		/* Symmetry only applies if we've got an active stream. */
2039 		err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
2040 		if (err < 0)
2041 			goto error;
2042 	}
2043 
2044 	/* apply symmetry for BE */
2045 	for_each_dpcm_be(fe, stream, dpcm) {
2046 		struct snd_soc_pcm_runtime *be = dpcm->be;
2047 		struct snd_pcm_substream *be_substream =
2048 			snd_soc_dpcm_get_substream(be, stream);
2049 		struct snd_soc_pcm_runtime *rtd;
2050 		struct snd_soc_dai *dai;
2051 
2052 		/* A backend may not have the requested substream */
2053 		if (!be_substream)
2054 			continue;
2055 
2056 		rtd = snd_soc_substream_to_rtd(be_substream);
2057 		if (rtd->dai_link->be_hw_params_fixup)
2058 			continue;
2059 
2060 		soc_pcm_update_symmetry(be_substream);
2061 
2062 		/* Symmetry only applies if we've got an active stream. */
2063 		for_each_rtd_dais(rtd, i, dai) {
2064 			err = soc_pcm_apply_symmetry(fe_substream, dai);
2065 			if (err < 0)
2066 				goto error;
2067 		}
2068 	}
2069 error:
2070 	return soc_pcm_ret(fe, err);
2071 }
2072 
2073 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
2074 {
2075 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream);
2076 	int stream = fe_substream->stream, ret = 0;
2077 
2078 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2079 
2080 	ret = dpcm_be_dai_startup(fe, stream);
2081 	if (ret < 0)
2082 		goto be_err;
2083 
2084 	dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
2085 
2086 	/* start the DAI frontend */
2087 	ret = __soc_pcm_open(fe, fe_substream);
2088 	if (ret < 0)
2089 		goto unwind;
2090 
2091 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
2092 
2093 	dpcm_runtime_setup_fe(fe_substream);
2094 
2095 	dpcm_runtime_setup_be_format(fe_substream);
2096 	dpcm_runtime_setup_be_chan(fe_substream);
2097 	dpcm_runtime_setup_be_rate(fe_substream);
2098 
2099 	ret = dpcm_apply_symmetry(fe_substream, stream);
2100 
2101 unwind:
2102 	if (ret < 0)
2103 		dpcm_be_dai_startup_unwind(fe, stream);
2104 be_err:
2105 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2106 
2107 	return soc_pcm_ret(fe, ret);
2108 }
2109 
2110 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
2111 {
2112 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
2113 	int stream = substream->stream;
2114 
2115 	snd_soc_dpcm_mutex_assert_held(fe);
2116 
2117 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2118 
2119 	/* shutdown the BEs */
2120 	dpcm_be_dai_shutdown(fe, stream);
2121 
2122 	dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
2123 
2124 	/* now shutdown the frontend */
2125 	__soc_pcm_close(fe, substream);
2126 
2127 	/* run the stream stop event */
2128 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
2129 
2130 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2131 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2132 	return 0;
2133 }
2134 
2135 void dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
2136 {
2137 	struct snd_soc_dpcm *dpcm;
2138 
2139 	/* only hw_params backends that are either sinks or sources
2140 	 * to this frontend DAI */
2141 	for_each_dpcm_be(fe, stream, dpcm) {
2142 
2143 		struct snd_soc_pcm_runtime *be = dpcm->be;
2144 		struct snd_pcm_substream *be_substream =
2145 			snd_soc_dpcm_get_substream(be, stream);
2146 
2147 		/* is this op for this BE ? */
2148 		if (!snd_soc_dpcm_can_be_update(fe, be, stream))
2149 			continue;
2150 
2151 		/* only free hw when no longer used - check all FEs */
2152 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2153 				continue;
2154 
2155 		/* do not free hw if this BE is used by other FE */
2156 		if (be->dpcm[stream].users > 1)
2157 			continue;
2158 
2159 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2160 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2161 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2162 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
2163 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2164 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2165 			continue;
2166 
2167 		dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
2168 			be->dai_link->name);
2169 
2170 		__soc_pcm_hw_free(be, be_substream);
2171 
2172 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2173 	}
2174 }
2175 
2176 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
2177 {
2178 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
2179 	int stream = substream->stream;
2180 
2181 	snd_soc_dpcm_mutex_lock(fe);
2182 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2183 
2184 	dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
2185 
2186 	/* call hw_free on the frontend */
2187 	soc_pcm_hw_clean(fe, substream, 0);
2188 
2189 	/* only hw_params backends that are either sinks or sources
2190 	 * to this frontend DAI */
2191 	dpcm_be_dai_hw_free(fe, stream);
2192 
2193 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2194 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2195 
2196 	snd_soc_dpcm_mutex_unlock(fe);
2197 	return 0;
2198 }
2199 
2200 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
2201 {
2202 	struct snd_soc_pcm_runtime *be;
2203 	struct snd_pcm_substream *be_substream;
2204 	struct snd_soc_dpcm *dpcm;
2205 	int ret;
2206 
2207 	for_each_dpcm_be(fe, stream, dpcm) {
2208 		struct snd_pcm_hw_params hw_params;
2209 
2210 		be = dpcm->be;
2211 		be_substream = snd_soc_dpcm_get_substream(be, stream);
2212 
2213 		/* is this op for this BE ? */
2214 		if (!snd_soc_dpcm_can_be_update(fe, be, stream))
2215 			continue;
2216 
2217 		/* copy params for each dpcm */
2218 		memcpy(&hw_params, &fe->dpcm[stream].hw_params,
2219 				sizeof(struct snd_pcm_hw_params));
2220 
2221 		/* perform any hw_params fixups */
2222 		ret = snd_soc_link_be_hw_params_fixup(be, &hw_params);
2223 		if (ret < 0)
2224 			goto unwind;
2225 
2226 		/* copy the fixed-up hw params for BE dai */
2227 		memcpy(&be->dpcm[stream].hw_params, &hw_params,
2228 		       sizeof(struct snd_pcm_hw_params));
2229 
2230 		/* only allow hw_params() if no connected FEs are running */
2231 		if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2232 			continue;
2233 
2234 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2235 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2236 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2237 			continue;
2238 
2239 		dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2240 			be->dai_link->name);
2241 
2242 		ret = __soc_pcm_hw_params(be_substream, &hw_params);
2243 		if (ret < 0)
2244 			goto unwind;
2245 
2246 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2247 	}
2248 	return 0;
2249 
2250 unwind:
2251 	dev_dbg(fe->dev, "ASoC: %s() failed at %s (%d)\n",
2252 		__func__, be->dai_link->name, ret);
2253 
2254 	/* disable any enabled and non active backends */
2255 	for_each_dpcm_be_rollback(fe, stream, dpcm) {
2256 		be = dpcm->be;
2257 		be_substream = snd_soc_dpcm_get_substream(be, stream);
2258 
2259 		if (!snd_soc_dpcm_can_be_update(fe, be, stream))
2260 			continue;
2261 
2262 		/* only allow hw_free() if no connected FEs are running */
2263 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2264 			continue;
2265 
2266 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2267 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2268 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2269 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2270 			continue;
2271 
2272 		__soc_pcm_hw_free(be, be_substream);
2273 	}
2274 
2275 	return ret;
2276 }
2277 
2278 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2279 				 struct snd_pcm_hw_params *params)
2280 {
2281 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
2282 	int ret, stream = substream->stream;
2283 
2284 	snd_soc_dpcm_mutex_lock(fe);
2285 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2286 
2287 	memcpy(&fe->dpcm[stream].hw_params, params,
2288 			sizeof(struct snd_pcm_hw_params));
2289 	ret = dpcm_be_dai_hw_params(fe, stream);
2290 	if (ret < 0)
2291 		goto out;
2292 
2293 	dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2294 			fe->dai_link->name, params_rate(params),
2295 			params_channels(params), params_format(params));
2296 
2297 	/* call hw_params on the frontend */
2298 	ret = __soc_pcm_hw_params(substream, params);
2299 	if (ret < 0)
2300 		dpcm_be_dai_hw_free(fe, stream);
2301 	else
2302 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2303 
2304 out:
2305 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2306 	snd_soc_dpcm_mutex_unlock(fe);
2307 
2308 	return soc_pcm_ret(fe, ret);
2309 }
2310 
2311 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2312 			       int cmd)
2313 {
2314 	struct snd_soc_pcm_runtime *be;
2315 	bool pause_stop_transition;
2316 	struct snd_soc_dpcm *dpcm;
2317 	unsigned long flags;
2318 	int ret = 0;
2319 
2320 	for_each_dpcm_be(fe, stream, dpcm) {
2321 		struct snd_pcm_substream *be_substream;
2322 
2323 		be = dpcm->be;
2324 		be_substream = snd_soc_dpcm_get_substream(be, stream);
2325 
2326 		snd_pcm_stream_lock_irqsave_nested(be_substream, flags);
2327 
2328 		/* is this op for this BE ? */
2329 		if (!snd_soc_dpcm_can_be_update(fe, be, stream))
2330 			goto next;
2331 
2332 		dev_dbg(be->dev, "ASoC: trigger BE %s cmd %d\n",
2333 			be->dai_link->name, cmd);
2334 
2335 		switch (cmd) {
2336 		case SNDRV_PCM_TRIGGER_START:
2337 			if (!be->dpcm[stream].be_start &&
2338 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2339 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2340 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2341 				goto next;
2342 
2343 			be->dpcm[stream].be_start++;
2344 			if (be->dpcm[stream].be_start != 1)
2345 				goto next;
2346 
2347 			if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_PAUSED)
2348 				ret = soc_pcm_trigger(be_substream,
2349 						      SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
2350 			else
2351 				ret = soc_pcm_trigger(be_substream,
2352 						      SNDRV_PCM_TRIGGER_START);
2353 			if (ret) {
2354 				be->dpcm[stream].be_start--;
2355 				goto next;
2356 			}
2357 
2358 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2359 			break;
2360 		case SNDRV_PCM_TRIGGER_RESUME:
2361 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2362 				goto next;
2363 
2364 			be->dpcm[stream].be_start++;
2365 			if (be->dpcm[stream].be_start != 1)
2366 				goto next;
2367 
2368 			ret = soc_pcm_trigger(be_substream, cmd);
2369 			if (ret) {
2370 				be->dpcm[stream].be_start--;
2371 				goto next;
2372 			}
2373 
2374 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2375 			break;
2376 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2377 			if (!be->dpcm[stream].be_start &&
2378 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2379 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2380 				goto next;
2381 
2382 			fe->dpcm[stream].fe_pause = false;
2383 			be->dpcm[stream].be_pause--;
2384 
2385 			be->dpcm[stream].be_start++;
2386 			if (be->dpcm[stream].be_start != 1)
2387 				goto next;
2388 
2389 			ret = soc_pcm_trigger(be_substream, cmd);
2390 			if (ret) {
2391 				be->dpcm[stream].be_start--;
2392 				goto next;
2393 			}
2394 
2395 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2396 			break;
2397 		case SNDRV_PCM_TRIGGER_STOP:
2398 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2399 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2400 				goto next;
2401 
2402 			if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START)
2403 				be->dpcm[stream].be_start--;
2404 
2405 			if (be->dpcm[stream].be_start != 0)
2406 				goto next;
2407 
2408 			pause_stop_transition = false;
2409 			if (fe->dpcm[stream].fe_pause) {
2410 				pause_stop_transition = true;
2411 				fe->dpcm[stream].fe_pause = false;
2412 				be->dpcm[stream].be_pause--;
2413 			}
2414 
2415 			if (be->dpcm[stream].be_pause != 0)
2416 				ret = soc_pcm_trigger(be_substream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
2417 			else
2418 				ret = soc_pcm_trigger(be_substream, SNDRV_PCM_TRIGGER_STOP);
2419 
2420 			if (ret) {
2421 				if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START)
2422 					be->dpcm[stream].be_start++;
2423 				if (pause_stop_transition) {
2424 					fe->dpcm[stream].fe_pause = true;
2425 					be->dpcm[stream].be_pause++;
2426 				}
2427 				goto next;
2428 			}
2429 
2430 			if (be->dpcm[stream].be_pause != 0)
2431 				be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2432 			else
2433 				be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2434 
2435 			break;
2436 		case SNDRV_PCM_TRIGGER_SUSPEND:
2437 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2438 				goto next;
2439 
2440 			be->dpcm[stream].be_start--;
2441 			if (be->dpcm[stream].be_start != 0)
2442 				goto next;
2443 
2444 			ret = soc_pcm_trigger(be_substream, cmd);
2445 			if (ret) {
2446 				be->dpcm[stream].be_start++;
2447 				goto next;
2448 			}
2449 
2450 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2451 			break;
2452 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2453 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2454 				goto next;
2455 
2456 			fe->dpcm[stream].fe_pause = true;
2457 			be->dpcm[stream].be_pause++;
2458 
2459 			be->dpcm[stream].be_start--;
2460 			if (be->dpcm[stream].be_start != 0)
2461 				goto next;
2462 
2463 			ret = soc_pcm_trigger(be_substream, cmd);
2464 			if (ret) {
2465 				be->dpcm[stream].be_start++;
2466 				goto next;
2467 			}
2468 
2469 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2470 			break;
2471 		}
2472 next:
2473 		snd_pcm_stream_unlock_irqrestore(be_substream, flags);
2474 		if (ret)
2475 			break;
2476 	}
2477 	return soc_pcm_ret(fe, ret);
2478 }
2479 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2480 
2481 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2482 				  int cmd, bool fe_first)
2483 {
2484 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
2485 	int ret;
2486 
2487 	/* call trigger on the frontend before the backend. */
2488 	if (fe_first) {
2489 		dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2490 			fe->dai_link->name, cmd);
2491 
2492 		ret = soc_pcm_trigger(substream, cmd);
2493 		if (ret < 0)
2494 			goto end;
2495 
2496 		ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2497 	}
2498 	/* call trigger on the frontend after the backend. */
2499 	else {
2500 		ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2501 		if (ret < 0)
2502 			goto end;
2503 
2504 		dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2505 			fe->dai_link->name, cmd);
2506 
2507 		ret = soc_pcm_trigger(substream, cmd);
2508 	}
2509 end:
2510 	return snd_soc_ret(fe->dev, ret, "trigger FE cmd: %d failed\n", cmd);
2511 }
2512 
2513 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2514 {
2515 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
2516 	int stream = substream->stream;
2517 	int ret = 0;
2518 	int fe_first;
2519 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2520 
2521 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2522 
2523 	switch (trigger) {
2524 	case SND_SOC_DPCM_TRIGGER_PRE:
2525 		fe_first = true;
2526 		break;
2527 	case SND_SOC_DPCM_TRIGGER_POST:
2528 		fe_first = false;
2529 		break;
2530 	default:
2531 		dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2532 				fe->dai_link->name);
2533 		ret = -EINVAL;
2534 		goto out;
2535 	}
2536 
2537 	switch (cmd) {
2538 	case SNDRV_PCM_TRIGGER_START:
2539 	case SNDRV_PCM_TRIGGER_RESUME:
2540 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2541 	case SNDRV_PCM_TRIGGER_DRAIN:
2542 		ret = dpcm_dai_trigger_fe_be(substream, cmd, fe_first);
2543 		break;
2544 	case SNDRV_PCM_TRIGGER_STOP:
2545 	case SNDRV_PCM_TRIGGER_SUSPEND:
2546 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2547 		ret = dpcm_dai_trigger_fe_be(substream, cmd, !fe_first);
2548 		break;
2549 	default:
2550 		ret = -EINVAL;
2551 		break;
2552 	}
2553 
2554 	if (ret < 0)
2555 		goto out;
2556 
2557 	switch (cmd) {
2558 	case SNDRV_PCM_TRIGGER_START:
2559 	case SNDRV_PCM_TRIGGER_RESUME:
2560 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2561 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2562 		break;
2563 	case SNDRV_PCM_TRIGGER_STOP:
2564 	case SNDRV_PCM_TRIGGER_SUSPEND:
2565 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2566 		break;
2567 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2568 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2569 		break;
2570 	}
2571 
2572 out:
2573 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2574 	return ret;
2575 }
2576 
2577 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2578 {
2579 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
2580 	int stream = substream->stream;
2581 
2582 	/* if FE's runtime_update is already set, we're in race;
2583 	 * process this trigger later at exit
2584 	 */
2585 	if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2586 		fe->dpcm[stream].trigger_pending = cmd + 1;
2587 		return 0; /* delayed, assuming it's successful */
2588 	}
2589 
2590 	/* we're alone, let's trigger */
2591 	return dpcm_fe_dai_do_trigger(substream, cmd);
2592 }
2593 
2594 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2595 {
2596 	struct snd_soc_dpcm *dpcm;
2597 	int ret = 0;
2598 
2599 	for_each_dpcm_be(fe, stream, dpcm) {
2600 
2601 		struct snd_soc_pcm_runtime *be = dpcm->be;
2602 		struct snd_pcm_substream *be_substream =
2603 			snd_soc_dpcm_get_substream(be, stream);
2604 
2605 		/* is this op for this BE ? */
2606 		if (!snd_soc_dpcm_can_be_update(fe, be, stream))
2607 			continue;
2608 
2609 		if (!snd_soc_dpcm_can_be_prepared(fe, be, stream))
2610 			continue;
2611 
2612 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2613 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2614 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2615 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2616 			continue;
2617 
2618 		dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2619 			be->dai_link->name);
2620 
2621 		ret = __soc_pcm_prepare(be, be_substream);
2622 		if (ret < 0)
2623 			break;
2624 
2625 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2626 	}
2627 
2628 	/*
2629 	 * Don't use soc_pcm_ret() on .prepare callback to lower error log severity
2630 	 *
2631 	 * We don't want to log an error since we do not want to give userspace a way to do a
2632 	 * denial-of-service attack on the syslog / diskspace.
2633 	 */
2634 	return ret;
2635 }
2636 
2637 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2638 {
2639 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
2640 	int stream = substream->stream, ret = 0;
2641 
2642 	snd_soc_dpcm_mutex_lock(fe);
2643 
2644 	dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2645 
2646 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2647 
2648 	ret = dpcm_be_dai_prepare(fe, stream);
2649 	if (ret < 0)
2650 		goto out;
2651 
2652 	/* call prepare on the frontend */
2653 	ret = __soc_pcm_prepare(fe, substream);
2654 	if (ret < 0)
2655 		goto out;
2656 
2657 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2658 
2659 out:
2660 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2661 	snd_soc_dpcm_mutex_unlock(fe);
2662 
2663 	/*
2664 	 * Don't use soc_pcm_ret() on .prepare callback to lower error log severity
2665 	 *
2666 	 * We don't want to log an error since we do not want to give userspace a way to do a
2667 	 * denial-of-service attack on the syslog / diskspace.
2668 	 */
2669 	return ret;
2670 }
2671 
2672 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2673 {
2674 	int err;
2675 
2676 	dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2677 		snd_pcm_direction_name(stream), fe->dai_link->name);
2678 
2679 	err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2680 
2681 	dpcm_be_dai_hw_free(fe, stream);
2682 
2683 	dpcm_be_dai_shutdown(fe, stream);
2684 
2685 	/* run the stream event for each BE */
2686 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2687 
2688 	return soc_pcm_ret(fe, err);
2689 }
2690 
2691 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2692 {
2693 	struct snd_soc_dpcm *dpcm;
2694 	int ret = 0;
2695 
2696 	dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2697 		snd_pcm_direction_name(stream), fe->dai_link->name);
2698 
2699 	/* Only start the BE if the FE is ready */
2700 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2701 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) {
2702 		dev_err(fe->dev, "ASoC: FE %s is not ready %s\n",
2703 			fe->dai_link->name, dpcm_state_string(fe->dpcm[stream].state));
2704 		ret = -EINVAL;
2705 		goto disconnect;
2706 	}
2707 
2708 	/* startup must always be called for new BEs */
2709 	ret = dpcm_be_dai_startup(fe, stream);
2710 	if (ret < 0)
2711 		goto disconnect;
2712 
2713 	/* keep going if FE state is > open */
2714 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2715 		return 0;
2716 
2717 	ret = dpcm_be_dai_hw_params(fe, stream);
2718 	if (ret < 0)
2719 		goto close;
2720 
2721 	/* keep going if FE state is > hw_params */
2722 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2723 		return 0;
2724 
2725 	ret = dpcm_be_dai_prepare(fe, stream);
2726 	if (ret < 0)
2727 		goto hw_free;
2728 
2729 	/* run the stream event for each BE */
2730 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2731 
2732 	/* keep going if FE state is > prepare */
2733 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2734 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2735 		return 0;
2736 
2737 	ret = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_START);
2738 	if (ret < 0)
2739 		goto hw_free;
2740 
2741 	return 0;
2742 
2743 hw_free:
2744 	dpcm_be_dai_hw_free(fe, stream);
2745 close:
2746 	dpcm_be_dai_shutdown(fe, stream);
2747 disconnect:
2748 	/* disconnect any pending BEs */
2749 	for_each_dpcm_be(fe, stream, dpcm) {
2750 		struct snd_soc_pcm_runtime *be = dpcm->be;
2751 
2752 		/* is this op for this BE ? */
2753 		if (!snd_soc_dpcm_can_be_update(fe, be, stream))
2754 			continue;
2755 
2756 		if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE ||
2757 			be->dpcm[stream].state == SND_SOC_DPCM_STATE_NEW)
2758 				dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2759 	}
2760 
2761 	return soc_pcm_ret(fe, ret);
2762 }
2763 
2764 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2765 {
2766 	struct snd_soc_dapm_widget_list *list;
2767 	int stream;
2768 	int count, paths;
2769 
2770 	if (!fe->dai_link->dynamic)
2771 		return 0;
2772 
2773 	if (fe->dai_link->num_cpus > 1)
2774 		return snd_soc_ret(fe->dev, -EINVAL,
2775 			"%s doesn't support Multi CPU yet\n", __func__);
2776 
2777 	/* only check active links */
2778 	if (!snd_soc_dai_active(snd_soc_rtd_to_cpu(fe, 0)))
2779 		return 0;
2780 
2781 	/* DAPM sync will call this to update DSP paths */
2782 	dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2783 		new ? "new" : "old", fe->dai_link->name);
2784 
2785 	for_each_pcm_streams(stream) {
2786 
2787 		/* skip if FE doesn't have playback/capture capability */
2788 		if (!snd_soc_dai_stream_valid(snd_soc_rtd_to_cpu(fe, 0),   stream) ||
2789 		    !snd_soc_dai_stream_valid(snd_soc_rtd_to_codec(fe, 0), stream))
2790 			continue;
2791 
2792 		/* skip if FE isn't currently playing/capturing */
2793 		if (!snd_soc_dai_stream_active(snd_soc_rtd_to_cpu(fe, 0), stream) ||
2794 		    !snd_soc_dai_stream_active(snd_soc_rtd_to_codec(fe, 0), stream))
2795 			continue;
2796 
2797 		paths = dpcm_path_get(fe, stream, &list);
2798 		if (paths < 0)
2799 			return paths;
2800 
2801 		/* update any playback/capture paths */
2802 		/*
2803 		 * Find the corresponding BE DAIs that source or sink audio to this
2804 		 * FE substream.
2805 		 */
2806 		if (new)
2807 			count = dpcm_add_paths(fe, stream, &list);
2808 		else
2809 			count = dpcm_prune_paths(fe, stream, &list);
2810 		if (count) {
2811 			dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2812 			if (new)
2813 				dpcm_run_update_startup(fe, stream);
2814 			else
2815 				dpcm_run_update_shutdown(fe, stream);
2816 			dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2817 
2818 			dpcm_clear_pending_state(fe, stream);
2819 			dpcm_be_disconnect(fe, stream);
2820 		}
2821 
2822 		dpcm_path_put(&list);
2823 	}
2824 
2825 	return 0;
2826 }
2827 
2828 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2829  * any DAI links.
2830  */
2831 int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
2832 {
2833 	struct snd_soc_pcm_runtime *fe;
2834 	int ret = 0;
2835 
2836 	snd_soc_dpcm_mutex_lock(card);
2837 	/* shutdown all old paths first */
2838 	for_each_card_rtds(card, fe) {
2839 		ret = soc_dpcm_fe_runtime_update(fe, 0);
2840 		if (ret)
2841 			goto out;
2842 	}
2843 
2844 	/* bring new paths up */
2845 	for_each_card_rtds(card, fe) {
2846 		ret = soc_dpcm_fe_runtime_update(fe, 1);
2847 		if (ret)
2848 			goto out;
2849 	}
2850 
2851 out:
2852 	snd_soc_dpcm_mutex_unlock(card);
2853 
2854 	return snd_soc_ret(card->dev, ret, "%s() failed\n", __func__);
2855 }
2856 EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
2857 
2858 static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
2859 {
2860 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream);
2861 	struct snd_soc_dpcm *dpcm;
2862 	int stream = fe_substream->stream;
2863 
2864 	snd_soc_dpcm_mutex_assert_held(fe);
2865 
2866 	/* mark FE's links ready to prune */
2867 	for_each_dpcm_be(fe, stream, dpcm)
2868 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2869 
2870 	dpcm_be_disconnect(fe, stream);
2871 }
2872 
2873 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2874 {
2875 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream);
2876 	int ret;
2877 
2878 	snd_soc_dpcm_mutex_lock(fe);
2879 	ret = dpcm_fe_dai_shutdown(fe_substream);
2880 
2881 	dpcm_fe_dai_cleanup(fe_substream);
2882 
2883 	snd_soc_dpcm_mutex_unlock(fe);
2884 	return ret;
2885 }
2886 
2887 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2888 {
2889 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream);
2890 	struct snd_soc_dapm_widget_list *list;
2891 	int ret;
2892 	int stream = fe_substream->stream;
2893 
2894 	snd_soc_dpcm_mutex_lock(fe);
2895 
2896 	ret = dpcm_path_get(fe, stream, &list);
2897 	if (ret < 0)
2898 		goto open_end;
2899 
2900 	/* calculate valid and active FE <-> BE dpcms */
2901 	dpcm_add_paths(fe, stream, &list);
2902 
2903 	/* There is no point starting up this FE if there are no BEs. */
2904 	if (list_empty(&fe->dpcm[stream].be_clients)) {
2905 		/* dev_err_once() for visibility, dev_dbg() for debugging UCM profiles. */
2906 		dev_err_once(fe->dev, "ASoC: no backend DAIs enabled for %s, possibly missing ALSA mixer-based routing or UCM profile\n",
2907 			     fe->dai_link->name);
2908 		dev_dbg(fe->dev, "ASoC: no backend DAIs enabled for %s\n", fe->dai_link->name);
2909 
2910 		ret = -EINVAL;
2911 		goto put_path;
2912 	}
2913 
2914 	ret = dpcm_fe_dai_startup(fe_substream);
2915 	if (ret < 0)
2916 		dpcm_fe_dai_cleanup(fe_substream);
2917 
2918 	dpcm_clear_pending_state(fe, stream);
2919 put_path:
2920 	dpcm_path_put(&list);
2921 open_end:
2922 	snd_soc_dpcm_mutex_unlock(fe);
2923 	return ret;
2924 }
2925 
2926 static int soc_get_playback_capture(struct snd_soc_pcm_runtime *rtd,
2927 				    int *playback, int *capture)
2928 {
2929 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
2930 	struct snd_soc_dai *cpu_dai;
2931 	struct snd_soc_dai *codec_dai;
2932 	struct snd_soc_dai_link_ch_map *ch_maps;
2933 	struct snd_soc_dai *dummy_dai = snd_soc_find_dai(&snd_soc_dummy_dlc);
2934 	int cpu_capture;
2935 	int cpu_playback;
2936 	int has_playback = 0;
2937 	int has_capture  = 0;
2938 	int i;
2939 
2940 	if (dai_link->dynamic && dai_link->num_cpus > 1)
2941 		return snd_soc_ret(rtd->dev, -EINVAL,
2942 				"DPCM doesn't support Multi CPU for Front-Ends yet\n");
2943 
2944 	/* Adapt stream for codec2codec links */
2945 	cpu_capture  = snd_soc_get_stream_cpu(dai_link, SNDRV_PCM_STREAM_CAPTURE);
2946 	cpu_playback = snd_soc_get_stream_cpu(dai_link, SNDRV_PCM_STREAM_PLAYBACK);
2947 
2948 	/*
2949 	 * see
2950 	 *	soc.h :: [dai_link->ch_maps Image sample]
2951 	 */
2952 	for_each_rtd_ch_maps(rtd, i, ch_maps) {
2953 		cpu_dai	  = snd_soc_rtd_to_cpu(rtd,   ch_maps->cpu);
2954 		codec_dai = snd_soc_rtd_to_codec(rtd, ch_maps->codec);
2955 
2956 		/*
2957 		 * FIXME
2958 		 *
2959 		 * DPCM Codec has been no checked before.
2960 		 * It should be checked, but it breaks compatibility.
2961 		 *
2962 		 * For example there is a case that CPU have loopback capabilities which is used
2963 		 * for tests on boards where the Codec has no capture capabilities. In this case,
2964 		 * Codec capture validation check will be fail, but system should allow capture
2965 		 * capabilities. We have no solution for it today.
2966 		 */
2967 		if (dai_link->dynamic || dai_link->no_pcm)
2968 			codec_dai = dummy_dai;
2969 
2970 		if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2971 		    snd_soc_dai_stream_valid(cpu_dai,   cpu_playback))
2972 			has_playback = 1;
2973 		if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2974 		    snd_soc_dai_stream_valid(cpu_dai,   cpu_capture))
2975 			has_capture = 1;
2976 	}
2977 
2978 	if (dai_link->playback_only)
2979 		has_capture = 0;
2980 
2981 	if (dai_link->capture_only)
2982 		has_playback = 0;
2983 
2984 	if (!has_playback && !has_capture)
2985 		return snd_soc_ret(rtd->dev, -EINVAL,
2986 			"substream %s has no playback, no capture\n", dai_link->stream_name);
2987 
2988 	*playback = has_playback;
2989 	*capture  = has_capture;
2990 
2991 	return 0;
2992 }
2993 
2994 static int soc_create_pcm(struct snd_pcm **pcm,
2995 			  struct snd_soc_pcm_runtime *rtd,
2996 			  int playback, int capture)
2997 {
2998 	char new_name[64];
2999 	int ret;
3000 
3001 	/* create the PCM */
3002 	if (rtd->dai_link->c2c_params) {
3003 		snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
3004 			 rtd->dai_link->stream_name);
3005 
3006 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, rtd->id,
3007 					   playback, capture, pcm);
3008 	} else if (rtd->dai_link->no_pcm) {
3009 		snprintf(new_name, sizeof(new_name), "(%s)",
3010 			rtd->dai_link->stream_name);
3011 
3012 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, rtd->id,
3013 				playback, capture, pcm);
3014 	} else {
3015 		if (rtd->dai_link->dynamic)
3016 			snprintf(new_name, sizeof(new_name), "%s (*)",
3017 				rtd->dai_link->stream_name);
3018 		else
3019 			snprintf(new_name, sizeof(new_name), "%s %s-%d",
3020 				rtd->dai_link->stream_name,
3021 				soc_codec_dai_name(rtd), rtd->id);
3022 
3023 		ret = snd_pcm_new(rtd->card->snd_card, new_name, rtd->id, playback,
3024 			capture, pcm);
3025 	}
3026 	if (ret < 0)
3027 		return snd_soc_ret(rtd->dev, ret,
3028 			"can't create pcm %s for dailink %s\n", new_name, rtd->dai_link->name);
3029 
3030 	dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n", rtd->id, new_name);
3031 
3032 	return 0;
3033 }
3034 
3035 /* create a new pcm */
3036 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd)
3037 {
3038 	struct snd_soc_component *component;
3039 	struct snd_pcm *pcm;
3040 	int ret = 0, playback = 0, capture = 0;
3041 	int i;
3042 
3043 	ret = soc_get_playback_capture(rtd, &playback, &capture);
3044 	if (ret < 0)
3045 		return ret;
3046 
3047 	ret = soc_create_pcm(&pcm, rtd, playback, capture);
3048 	if (ret < 0)
3049 		return ret;
3050 
3051 	/* DAPM dai link stream work */
3052 	/*
3053 	 * Currently nothing to do for c2c links
3054 	 * Since c2c links are internal nodes in the DAPM graph and
3055 	 * don't interface with the outside world or application layer
3056 	 * we don't have to do any special handling on close.
3057 	 */
3058 	if (!rtd->dai_link->c2c_params)
3059 		rtd->close_delayed_work_func = snd_soc_close_delayed_work;
3060 
3061 	rtd->pcm = pcm;
3062 	pcm->nonatomic = rtd->dai_link->nonatomic;
3063 	pcm->private_data = rtd;
3064 	pcm->no_device_suspend = true;
3065 
3066 	if (rtd->dai_link->no_pcm || rtd->dai_link->c2c_params) {
3067 		if (playback)
3068 			pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
3069 		if (capture)
3070 			pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
3071 		goto out;
3072 	}
3073 
3074 	/* ASoC PCM operations */
3075 	if (rtd->dai_link->dynamic) {
3076 		rtd->ops.open		= dpcm_fe_dai_open;
3077 		rtd->ops.hw_params	= dpcm_fe_dai_hw_params;
3078 		rtd->ops.prepare	= dpcm_fe_dai_prepare;
3079 		rtd->ops.trigger	= dpcm_fe_dai_trigger;
3080 		rtd->ops.hw_free	= dpcm_fe_dai_hw_free;
3081 		rtd->ops.close		= dpcm_fe_dai_close;
3082 		rtd->ops.pointer	= soc_pcm_pointer;
3083 	} else {
3084 		rtd->ops.open		= soc_pcm_open;
3085 		rtd->ops.hw_params	= soc_pcm_hw_params;
3086 		rtd->ops.prepare	= soc_pcm_prepare;
3087 		rtd->ops.trigger	= soc_pcm_trigger;
3088 		rtd->ops.hw_free	= soc_pcm_hw_free;
3089 		rtd->ops.close		= soc_pcm_close;
3090 		rtd->ops.pointer	= soc_pcm_pointer;
3091 	}
3092 
3093 	for_each_rtd_components(rtd, i, component) {
3094 		const struct snd_soc_component_driver *drv = component->driver;
3095 
3096 		if (drv->ioctl)
3097 			rtd->ops.ioctl		= snd_soc_pcm_component_ioctl;
3098 		if (drv->sync_stop)
3099 			rtd->ops.sync_stop	= snd_soc_pcm_component_sync_stop;
3100 		if (drv->copy)
3101 			rtd->ops.copy		= snd_soc_pcm_component_copy;
3102 		if (drv->page)
3103 			rtd->ops.page		= snd_soc_pcm_component_page;
3104 		if (drv->mmap)
3105 			rtd->ops.mmap		= snd_soc_pcm_component_mmap;
3106 		if (drv->ack)
3107 			rtd->ops.ack            = snd_soc_pcm_component_ack;
3108 	}
3109 
3110 	if (playback)
3111 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3112 
3113 	if (capture)
3114 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3115 
3116 	ret = snd_soc_pcm_component_new(rtd);
3117 	if (ret < 0)
3118 		return ret;
3119 out:
3120 	dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
3121 		soc_codec_dai_name(rtd), soc_cpu_dai_name(rtd));
3122 	return ret;
3123 }
3124 
3125 /* get the substream for this BE */
3126 struct snd_pcm_substream *
3127 	snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3128 {
3129 	return be->pcm->streams[stream].substream;
3130 }
3131 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3132