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