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