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