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