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