xref: /openbmc/linux/sound/soc/soc-pcm.c (revision c4c11dd1)
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/pm_runtime.h>
23 #include <linux/slab.h>
24 #include <linux/workqueue.h>
25 #include <linux/export.h>
26 #include <linux/debugfs.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
31 #include <sound/soc-dpcm.h>
32 #include <sound/initval.h>
33 
34 #define DPCM_MAX_BE_USERS	8
35 
36 /**
37  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
38  * @substream: the pcm substream
39  * @hw: the hardware parameters
40  *
41  * Sets the substream runtime hardware parameters.
42  */
43 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
44 	const struct snd_pcm_hardware *hw)
45 {
46 	struct snd_pcm_runtime *runtime = substream->runtime;
47 	runtime->hw.info = hw->info;
48 	runtime->hw.formats = hw->formats;
49 	runtime->hw.period_bytes_min = hw->period_bytes_min;
50 	runtime->hw.period_bytes_max = hw->period_bytes_max;
51 	runtime->hw.periods_min = hw->periods_min;
52 	runtime->hw.periods_max = hw->periods_max;
53 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
54 	runtime->hw.fifo_size = hw->fifo_size;
55 	return 0;
56 }
57 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
58 
59 /* DPCM stream event, send event to FE and all active BEs. */
60 static int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
61 	int event)
62 {
63 	struct snd_soc_dpcm *dpcm;
64 
65 	list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
66 
67 		struct snd_soc_pcm_runtime *be = dpcm->be;
68 
69 		dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
70 				be->dai_link->name, event, dir);
71 
72 		snd_soc_dapm_stream_event(be, dir, event);
73 	}
74 
75 	snd_soc_dapm_stream_event(fe, dir, event);
76 
77 	return 0;
78 }
79 
80 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
81 					struct snd_soc_dai *soc_dai)
82 {
83 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
84 	int ret;
85 
86 	if (!soc_dai->driver->symmetric_rates &&
87 	    !rtd->dai_link->symmetric_rates)
88 		return 0;
89 
90 	/* This can happen if multiple streams are starting simultaneously -
91 	 * the second can need to get its constraints before the first has
92 	 * picked a rate.  Complain and allow the application to carry on.
93 	 */
94 	if (!soc_dai->rate) {
95 		dev_warn(soc_dai->dev,
96 			 "ASoC: Not enforcing symmetric_rates due to race\n");
97 		return 0;
98 	}
99 
100 	dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n", soc_dai->rate);
101 
102 	ret = snd_pcm_hw_constraint_minmax(substream->runtime,
103 					   SNDRV_PCM_HW_PARAM_RATE,
104 					   soc_dai->rate, soc_dai->rate);
105 	if (ret < 0) {
106 		dev_err(soc_dai->dev,
107 			"ASoC: Unable to apply rate symmetry constraint: %d\n",
108 			ret);
109 		return ret;
110 	}
111 
112 	return 0;
113 }
114 
115 /*
116  * List of sample sizes that might go over the bus for parameter
117  * application.  There ought to be a wildcard sample size for things
118  * like the DAC/ADC resolution to use but there isn't right now.
119  */
120 static int sample_sizes[] = {
121 	24, 32,
122 };
123 
124 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
125 			      struct snd_soc_dai *dai)
126 {
127 	int ret, i, bits;
128 
129 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
130 		bits = dai->driver->playback.sig_bits;
131 	else
132 		bits = dai->driver->capture.sig_bits;
133 
134 	if (!bits)
135 		return;
136 
137 	for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
138 		if (bits >= sample_sizes[i])
139 			continue;
140 
141 		ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
142 						   sample_sizes[i], bits);
143 		if (ret != 0)
144 			dev_warn(dai->dev,
145 				 "ASoC: Failed to set MSB %d/%d: %d\n",
146 				 bits, sample_sizes[i], ret);
147 	}
148 }
149 
150 static void soc_pcm_init_runtime_hw(struct snd_pcm_hardware *hw,
151 	struct snd_soc_pcm_stream *codec_stream,
152 	struct snd_soc_pcm_stream *cpu_stream)
153 {
154 	hw->rate_min = max(codec_stream->rate_min, cpu_stream->rate_min);
155 	hw->rate_max = max(codec_stream->rate_max, cpu_stream->rate_max);
156 	hw->channels_min = max(codec_stream->channels_min,
157 		cpu_stream->channels_min);
158 	hw->channels_max = min(codec_stream->channels_max,
159 		cpu_stream->channels_max);
160 	hw->formats = codec_stream->formats & cpu_stream->formats;
161 	hw->rates = codec_stream->rates & cpu_stream->rates;
162 	if (codec_stream->rates
163 		& (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
164 		hw->rates |= cpu_stream->rates;
165 	if (cpu_stream->rates
166 		& (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
167 		hw->rates |= codec_stream->rates;
168 }
169 
170 /*
171  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
172  * then initialized and any private data can be allocated. This also calls
173  * startup for the cpu DAI, platform, machine and codec DAI.
174  */
175 static int soc_pcm_open(struct snd_pcm_substream *substream)
176 {
177 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
178 	struct snd_pcm_runtime *runtime = substream->runtime;
179 	struct snd_soc_platform *platform = rtd->platform;
180 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
181 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
182 	struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
183 	struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
184 	int ret = 0;
185 
186 	pm_runtime_get_sync(cpu_dai->dev);
187 	pm_runtime_get_sync(codec_dai->dev);
188 	pm_runtime_get_sync(platform->dev);
189 
190 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
191 
192 	/* startup the audio subsystem */
193 	if (cpu_dai->driver->ops->startup) {
194 		ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
195 		if (ret < 0) {
196 			dev_err(cpu_dai->dev, "ASoC: can't open interface"
197 				" %s: %d\n", cpu_dai->name, ret);
198 			goto out;
199 		}
200 	}
201 
202 	if (platform->driver->ops && platform->driver->ops->open) {
203 		ret = platform->driver->ops->open(substream);
204 		if (ret < 0) {
205 			dev_err(platform->dev, "ASoC: can't open platform"
206 				" %s: %d\n", platform->name, ret);
207 			goto platform_err;
208 		}
209 	}
210 
211 	if (codec_dai->driver->ops->startup) {
212 		ret = codec_dai->driver->ops->startup(substream, codec_dai);
213 		if (ret < 0) {
214 			dev_err(codec_dai->dev, "ASoC: can't open codec"
215 				" %s: %d\n", codec_dai->name, ret);
216 			goto codec_dai_err;
217 		}
218 	}
219 
220 	if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
221 		ret = rtd->dai_link->ops->startup(substream);
222 		if (ret < 0) {
223 			pr_err("ASoC: %s startup failed: %d\n",
224 			       rtd->dai_link->name, ret);
225 			goto machine_err;
226 		}
227 	}
228 
229 	/* Dynamic PCM DAI links compat checks use dynamic capabilities */
230 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
231 		goto dynamic;
232 
233 	/* Check that the codec and cpu DAIs are compatible */
234 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
235 		soc_pcm_init_runtime_hw(&runtime->hw, &codec_dai_drv->playback,
236 			&cpu_dai_drv->playback);
237 	} else {
238 		soc_pcm_init_runtime_hw(&runtime->hw, &codec_dai_drv->capture,
239 			&cpu_dai_drv->capture);
240 	}
241 
242 	ret = -EINVAL;
243 	snd_pcm_limit_hw_rates(runtime);
244 	if (!runtime->hw.rates) {
245 		printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
246 			codec_dai->name, cpu_dai->name);
247 		goto config_err;
248 	}
249 	if (!runtime->hw.formats) {
250 		printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
251 			codec_dai->name, cpu_dai->name);
252 		goto config_err;
253 	}
254 	if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
255 	    runtime->hw.channels_min > runtime->hw.channels_max) {
256 		printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
257 				codec_dai->name, cpu_dai->name);
258 		goto config_err;
259 	}
260 
261 	soc_pcm_apply_msb(substream, codec_dai);
262 	soc_pcm_apply_msb(substream, cpu_dai);
263 
264 	/* Symmetry only applies if we've already got an active stream. */
265 	if (cpu_dai->active) {
266 		ret = soc_pcm_apply_symmetry(substream, cpu_dai);
267 		if (ret != 0)
268 			goto config_err;
269 	}
270 
271 	if (codec_dai->active) {
272 		ret = soc_pcm_apply_symmetry(substream, codec_dai);
273 		if (ret != 0)
274 			goto config_err;
275 	}
276 
277 	pr_debug("ASoC: %s <-> %s info:\n",
278 			codec_dai->name, cpu_dai->name);
279 	pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
280 	pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
281 		 runtime->hw.channels_max);
282 	pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
283 		 runtime->hw.rate_max);
284 
285 dynamic:
286 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
287 		cpu_dai->playback_active++;
288 		codec_dai->playback_active++;
289 	} else {
290 		cpu_dai->capture_active++;
291 		codec_dai->capture_active++;
292 	}
293 	cpu_dai->active++;
294 	codec_dai->active++;
295 	rtd->codec->active++;
296 	mutex_unlock(&rtd->pcm_mutex);
297 	return 0;
298 
299 config_err:
300 	if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
301 		rtd->dai_link->ops->shutdown(substream);
302 
303 machine_err:
304 	if (codec_dai->driver->ops->shutdown)
305 		codec_dai->driver->ops->shutdown(substream, codec_dai);
306 
307 codec_dai_err:
308 	if (platform->driver->ops && platform->driver->ops->close)
309 		platform->driver->ops->close(substream);
310 
311 platform_err:
312 	if (cpu_dai->driver->ops->shutdown)
313 		cpu_dai->driver->ops->shutdown(substream, cpu_dai);
314 out:
315 	mutex_unlock(&rtd->pcm_mutex);
316 
317 	pm_runtime_put(platform->dev);
318 	pm_runtime_put(codec_dai->dev);
319 	pm_runtime_put(cpu_dai->dev);
320 
321 	return ret;
322 }
323 
324 /*
325  * Power down the audio subsystem pmdown_time msecs after close is called.
326  * This is to ensure there are no pops or clicks in between any music tracks
327  * due to DAPM power cycling.
328  */
329 static void close_delayed_work(struct work_struct *work)
330 {
331 	struct snd_soc_pcm_runtime *rtd =
332 			container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
333 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
334 
335 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
336 
337 	dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
338 		 codec_dai->driver->playback.stream_name,
339 		 codec_dai->playback_active ? "active" : "inactive",
340 		 rtd->pop_wait ? "yes" : "no");
341 
342 	/* are we waiting on this codec DAI stream */
343 	if (rtd->pop_wait == 1) {
344 		rtd->pop_wait = 0;
345 		snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
346 					  SND_SOC_DAPM_STREAM_STOP);
347 	}
348 
349 	mutex_unlock(&rtd->pcm_mutex);
350 }
351 
352 /*
353  * Called by ALSA when a PCM substream is closed. Private data can be
354  * freed here. The cpu DAI, codec DAI, machine and platform are also
355  * shutdown.
356  */
357 static int soc_pcm_close(struct snd_pcm_substream *substream)
358 {
359 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
360 	struct snd_soc_platform *platform = rtd->platform;
361 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
362 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
363 	struct snd_soc_codec *codec = rtd->codec;
364 
365 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
366 
367 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
368 		cpu_dai->playback_active--;
369 		codec_dai->playback_active--;
370 	} else {
371 		cpu_dai->capture_active--;
372 		codec_dai->capture_active--;
373 	}
374 
375 	cpu_dai->active--;
376 	codec_dai->active--;
377 	codec->active--;
378 
379 	/* clear the corresponding DAIs rate when inactive */
380 	if (!cpu_dai->active)
381 		cpu_dai->rate = 0;
382 
383 	if (!codec_dai->active)
384 		codec_dai->rate = 0;
385 
386 	/* Muting the DAC suppresses artifacts caused during digital
387 	 * shutdown, for example from stopping clocks.
388 	 */
389 	snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
390 
391 	if (cpu_dai->driver->ops->shutdown)
392 		cpu_dai->driver->ops->shutdown(substream, cpu_dai);
393 
394 	if (codec_dai->driver->ops->shutdown)
395 		codec_dai->driver->ops->shutdown(substream, codec_dai);
396 
397 	if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
398 		rtd->dai_link->ops->shutdown(substream);
399 
400 	if (platform->driver->ops && platform->driver->ops->close)
401 		platform->driver->ops->close(substream);
402 	cpu_dai->runtime = NULL;
403 
404 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
405 		if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
406 		    rtd->dai_link->ignore_pmdown_time) {
407 			/* powered down playback stream now */
408 			snd_soc_dapm_stream_event(rtd,
409 						  SNDRV_PCM_STREAM_PLAYBACK,
410 						  SND_SOC_DAPM_STREAM_STOP);
411 		} else {
412 			/* start delayed pop wq here for playback streams */
413 			rtd->pop_wait = 1;
414 			schedule_delayed_work(&rtd->delayed_work,
415 				msecs_to_jiffies(rtd->pmdown_time));
416 		}
417 	} else {
418 		/* capture streams can be powered down now */
419 		snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
420 					  SND_SOC_DAPM_STREAM_STOP);
421 	}
422 
423 	mutex_unlock(&rtd->pcm_mutex);
424 
425 	pm_runtime_put(platform->dev);
426 	pm_runtime_put(codec_dai->dev);
427 	pm_runtime_put(cpu_dai->dev);
428 
429 	return 0;
430 }
431 
432 /*
433  * Called by ALSA when the PCM substream is prepared, can set format, sample
434  * rate, etc.  This function is non atomic and can be called multiple times,
435  * it can refer to the runtime info.
436  */
437 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
438 {
439 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
440 	struct snd_soc_platform *platform = rtd->platform;
441 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
442 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
443 	int ret = 0;
444 
445 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
446 
447 	if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
448 		ret = rtd->dai_link->ops->prepare(substream);
449 		if (ret < 0) {
450 			dev_err(rtd->card->dev, "ASoC: machine prepare error:"
451 				" %d\n", ret);
452 			goto out;
453 		}
454 	}
455 
456 	if (platform->driver->ops && platform->driver->ops->prepare) {
457 		ret = platform->driver->ops->prepare(substream);
458 		if (ret < 0) {
459 			dev_err(platform->dev, "ASoC: platform prepare error:"
460 				" %d\n", ret);
461 			goto out;
462 		}
463 	}
464 
465 	if (codec_dai->driver->ops->prepare) {
466 		ret = codec_dai->driver->ops->prepare(substream, codec_dai);
467 		if (ret < 0) {
468 			dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
469 				ret);
470 			goto out;
471 		}
472 	}
473 
474 	if (cpu_dai->driver->ops->prepare) {
475 		ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
476 		if (ret < 0) {
477 			dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
478 				ret);
479 			goto out;
480 		}
481 	}
482 
483 	/* cancel any delayed stream shutdown that is pending */
484 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
485 	    rtd->pop_wait) {
486 		rtd->pop_wait = 0;
487 		cancel_delayed_work(&rtd->delayed_work);
488 	}
489 
490 	snd_soc_dapm_stream_event(rtd, substream->stream,
491 			SND_SOC_DAPM_STREAM_START);
492 
493 	snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
494 
495 out:
496 	mutex_unlock(&rtd->pcm_mutex);
497 	return ret;
498 }
499 
500 /*
501  * Called by ALSA when the hardware params are set by application. This
502  * function can also be called multiple times and can allocate buffers
503  * (using snd_pcm_lib_* ). It's non-atomic.
504  */
505 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
506 				struct snd_pcm_hw_params *params)
507 {
508 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
509 	struct snd_soc_platform *platform = rtd->platform;
510 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
511 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
512 	int ret = 0;
513 
514 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
515 
516 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
517 		ret = rtd->dai_link->ops->hw_params(substream, params);
518 		if (ret < 0) {
519 			dev_err(rtd->card->dev, "ASoC: machine hw_params"
520 				" failed: %d\n", ret);
521 			goto out;
522 		}
523 	}
524 
525 	if (codec_dai->driver->ops->hw_params) {
526 		ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
527 		if (ret < 0) {
528 			dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
529 				" %d\n", codec_dai->name, ret);
530 			goto codec_err;
531 		}
532 	}
533 
534 	if (cpu_dai->driver->ops->hw_params) {
535 		ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
536 		if (ret < 0) {
537 			dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
538 				cpu_dai->name, ret);
539 			goto interface_err;
540 		}
541 	}
542 
543 	if (platform->driver->ops && platform->driver->ops->hw_params) {
544 		ret = platform->driver->ops->hw_params(substream, params);
545 		if (ret < 0) {
546 			dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
547 			       platform->name, ret);
548 			goto platform_err;
549 		}
550 	}
551 
552 	/* store the rate for each DAIs */
553 	cpu_dai->rate = params_rate(params);
554 	codec_dai->rate = params_rate(params);
555 
556 out:
557 	mutex_unlock(&rtd->pcm_mutex);
558 	return ret;
559 
560 platform_err:
561 	if (cpu_dai->driver->ops->hw_free)
562 		cpu_dai->driver->ops->hw_free(substream, cpu_dai);
563 
564 interface_err:
565 	if (codec_dai->driver->ops->hw_free)
566 		codec_dai->driver->ops->hw_free(substream, codec_dai);
567 
568 codec_err:
569 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
570 		rtd->dai_link->ops->hw_free(substream);
571 
572 	mutex_unlock(&rtd->pcm_mutex);
573 	return ret;
574 }
575 
576 /*
577  * Frees resources allocated by hw_params, can be called multiple times
578  */
579 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
580 {
581 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
582 	struct snd_soc_platform *platform = rtd->platform;
583 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
584 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
585 	struct snd_soc_codec *codec = rtd->codec;
586 
587 	mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
588 
589 	/* apply codec digital mute */
590 	if (!codec->active)
591 		snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
592 
593 	/* free any machine hw params */
594 	if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
595 		rtd->dai_link->ops->hw_free(substream);
596 
597 	/* free any DMA resources */
598 	if (platform->driver->ops && platform->driver->ops->hw_free)
599 		platform->driver->ops->hw_free(substream);
600 
601 	/* now free hw params for the DAIs  */
602 	if (codec_dai->driver->ops->hw_free)
603 		codec_dai->driver->ops->hw_free(substream, codec_dai);
604 
605 	if (cpu_dai->driver->ops->hw_free)
606 		cpu_dai->driver->ops->hw_free(substream, cpu_dai);
607 
608 	mutex_unlock(&rtd->pcm_mutex);
609 	return 0;
610 }
611 
612 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
613 {
614 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
615 	struct snd_soc_platform *platform = rtd->platform;
616 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
617 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
618 	int ret;
619 
620 	if (codec_dai->driver->ops->trigger) {
621 		ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
622 		if (ret < 0)
623 			return ret;
624 	}
625 
626 	if (platform->driver->ops && platform->driver->ops->trigger) {
627 		ret = platform->driver->ops->trigger(substream, cmd);
628 		if (ret < 0)
629 			return ret;
630 	}
631 
632 	if (cpu_dai->driver->ops->trigger) {
633 		ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
634 		if (ret < 0)
635 			return ret;
636 	}
637 	return 0;
638 }
639 
640 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
641 				   int cmd)
642 {
643 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
644 	struct snd_soc_platform *platform = rtd->platform;
645 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
646 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
647 	int ret;
648 
649 	if (codec_dai->driver->ops->bespoke_trigger) {
650 		ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
651 		if (ret < 0)
652 			return ret;
653 	}
654 
655 	if (platform->driver->bespoke_trigger) {
656 		ret = platform->driver->bespoke_trigger(substream, cmd);
657 		if (ret < 0)
658 			return ret;
659 	}
660 
661 	if (cpu_dai->driver->ops->bespoke_trigger) {
662 		ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
663 		if (ret < 0)
664 			return ret;
665 	}
666 	return 0;
667 }
668 /*
669  * soc level wrapper for pointer callback
670  * If cpu_dai, codec_dai, platform driver has the delay callback, than
671  * the runtime->delay will be updated accordingly.
672  */
673 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
674 {
675 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
676 	struct snd_soc_platform *platform = rtd->platform;
677 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
678 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
679 	struct snd_pcm_runtime *runtime = substream->runtime;
680 	snd_pcm_uframes_t offset = 0;
681 	snd_pcm_sframes_t delay = 0;
682 
683 	if (platform->driver->ops && platform->driver->ops->pointer)
684 		offset = platform->driver->ops->pointer(substream);
685 
686 	if (cpu_dai->driver->ops->delay)
687 		delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
688 
689 	if (codec_dai->driver->ops->delay)
690 		delay += codec_dai->driver->ops->delay(substream, codec_dai);
691 
692 	if (platform->driver->delay)
693 		delay += platform->driver->delay(substream, codec_dai);
694 
695 	runtime->delay = delay;
696 
697 	return offset;
698 }
699 
700 /* connect a FE and BE */
701 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
702 		struct snd_soc_pcm_runtime *be, int stream)
703 {
704 	struct snd_soc_dpcm *dpcm;
705 
706 	/* only add new dpcms */
707 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
708 		if (dpcm->be == be && dpcm->fe == fe)
709 			return 0;
710 	}
711 
712 	dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
713 	if (!dpcm)
714 		return -ENOMEM;
715 
716 	dpcm->be = be;
717 	dpcm->fe = fe;
718 	be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
719 	dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
720 	list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
721 	list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
722 
723 	dev_dbg(fe->dev, "  connected new DPCM %s path %s %s %s\n",
724 			stream ? "capture" : "playback",  fe->dai_link->name,
725 			stream ? "<-" : "->", be->dai_link->name);
726 
727 #ifdef CONFIG_DEBUG_FS
728 	dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
729 			fe->debugfs_dpcm_root, &dpcm->state);
730 #endif
731 	return 1;
732 }
733 
734 /* reparent a BE onto another FE */
735 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
736 			struct snd_soc_pcm_runtime *be, int stream)
737 {
738 	struct snd_soc_dpcm *dpcm;
739 	struct snd_pcm_substream *fe_substream, *be_substream;
740 
741 	/* reparent if BE is connected to other FEs */
742 	if (!be->dpcm[stream].users)
743 		return;
744 
745 	be_substream = snd_soc_dpcm_get_substream(be, stream);
746 
747 	list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
748 		if (dpcm->fe == fe)
749 			continue;
750 
751 		dev_dbg(fe->dev, "  reparent %s path %s %s %s\n",
752 			stream ? "capture" : "playback",
753 			dpcm->fe->dai_link->name,
754 			stream ? "<-" : "->", dpcm->be->dai_link->name);
755 
756 		fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
757 		be_substream->runtime = fe_substream->runtime;
758 		break;
759 	}
760 }
761 
762 /* disconnect a BE and FE */
763 static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
764 {
765 	struct snd_soc_dpcm *dpcm, *d;
766 
767 	list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
768 		dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
769 				stream ? "capture" : "playback",
770 				dpcm->be->dai_link->name);
771 
772 		if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
773 			continue;
774 
775 		dev_dbg(fe->dev, "  freed DSP %s path %s %s %s\n",
776 			stream ? "capture" : "playback", fe->dai_link->name,
777 			stream ? "<-" : "->", dpcm->be->dai_link->name);
778 
779 		/* BEs still alive need new FE */
780 		dpcm_be_reparent(fe, dpcm->be, stream);
781 
782 #ifdef CONFIG_DEBUG_FS
783 		debugfs_remove(dpcm->debugfs_state);
784 #endif
785 		list_del(&dpcm->list_be);
786 		list_del(&dpcm->list_fe);
787 		kfree(dpcm);
788 	}
789 }
790 
791 /* get BE for DAI widget and stream */
792 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
793 		struct snd_soc_dapm_widget *widget, int stream)
794 {
795 	struct snd_soc_pcm_runtime *be;
796 	int i;
797 
798 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
799 		for (i = 0; i < card->num_links; i++) {
800 			be = &card->rtd[i];
801 
802 			if (!be->dai_link->no_pcm)
803 				continue;
804 
805 			if (be->cpu_dai->playback_widget == widget ||
806 				be->codec_dai->playback_widget == widget)
807 				return be;
808 		}
809 	} else {
810 
811 		for (i = 0; i < card->num_links; i++) {
812 			be = &card->rtd[i];
813 
814 			if (!be->dai_link->no_pcm)
815 				continue;
816 
817 			if (be->cpu_dai->capture_widget == widget ||
818 				be->codec_dai->capture_widget == widget)
819 				return be;
820 		}
821 	}
822 
823 	dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
824 		stream ? "capture" : "playback", widget->name);
825 	return NULL;
826 }
827 
828 static inline struct snd_soc_dapm_widget *
829 	rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
830 {
831 	if (stream == SNDRV_PCM_STREAM_PLAYBACK)
832 		return rtd->cpu_dai->playback_widget;
833 	else
834 		return rtd->cpu_dai->capture_widget;
835 }
836 
837 static inline struct snd_soc_dapm_widget *
838 	rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
839 {
840 	if (stream == SNDRV_PCM_STREAM_PLAYBACK)
841 		return rtd->codec_dai->playback_widget;
842 	else
843 		return rtd->codec_dai->capture_widget;
844 }
845 
846 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
847 		struct snd_soc_dapm_widget *widget)
848 {
849 	int i;
850 
851 	for (i = 0; i < list->num_widgets; i++) {
852 		if (widget == list->widgets[i])
853 			return 1;
854 	}
855 
856 	return 0;
857 }
858 
859 static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
860 	int stream, struct snd_soc_dapm_widget_list **list_)
861 {
862 	struct snd_soc_dai *cpu_dai = fe->cpu_dai;
863 	struct snd_soc_dapm_widget_list *list;
864 	int paths;
865 
866 	list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
867 			sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
868 	if (list == NULL)
869 		return -ENOMEM;
870 
871 	/* get number of valid DAI paths and their widgets */
872 	paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
873 
874 	dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
875 			stream ? "capture" : "playback");
876 
877 	*list_ = list;
878 	return paths;
879 }
880 
881 static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
882 {
883 	kfree(*list);
884 }
885 
886 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
887 	struct snd_soc_dapm_widget_list **list_)
888 {
889 	struct snd_soc_dpcm *dpcm;
890 	struct snd_soc_dapm_widget_list *list = *list_;
891 	struct snd_soc_dapm_widget *widget;
892 	int prune = 0;
893 
894 	/* Destroy any old FE <--> BE connections */
895 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
896 
897 		/* is there a valid CPU DAI widget for this BE */
898 		widget = rtd_get_cpu_widget(dpcm->be, stream);
899 
900 		/* prune the BE if it's no longer in our active list */
901 		if (widget && widget_in_list(list, widget))
902 			continue;
903 
904 		/* is there a valid CODEC DAI widget for this BE */
905 		widget = rtd_get_codec_widget(dpcm->be, stream);
906 
907 		/* prune the BE if it's no longer in our active list */
908 		if (widget && widget_in_list(list, widget))
909 			continue;
910 
911 		dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
912 			stream ? "capture" : "playback",
913 			dpcm->be->dai_link->name, fe->dai_link->name);
914 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
915 		dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
916 		prune++;
917 	}
918 
919 	dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
920 	return prune;
921 }
922 
923 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
924 	struct snd_soc_dapm_widget_list **list_)
925 {
926 	struct snd_soc_card *card = fe->card;
927 	struct snd_soc_dapm_widget_list *list = *list_;
928 	struct snd_soc_pcm_runtime *be;
929 	int i, new = 0, err;
930 
931 	/* Create any new FE <--> BE connections */
932 	for (i = 0; i < list->num_widgets; i++) {
933 
934 		switch (list->widgets[i]->id) {
935 		case snd_soc_dapm_dai_in:
936 		case snd_soc_dapm_dai_out:
937 			break;
938 		default:
939 			continue;
940 		}
941 
942 		/* is there a valid BE rtd for this widget */
943 		be = dpcm_get_be(card, list->widgets[i], stream);
944 		if (!be) {
945 			dev_err(fe->dev, "ASoC: no BE found for %s\n",
946 					list->widgets[i]->name);
947 			continue;
948 		}
949 
950 		/* make sure BE is a real BE */
951 		if (!be->dai_link->no_pcm)
952 			continue;
953 
954 		/* don't connect if FE is not running */
955 		if (!fe->dpcm[stream].runtime)
956 			continue;
957 
958 		/* newly connected FE and BE */
959 		err = dpcm_be_connect(fe, be, stream);
960 		if (err < 0) {
961 			dev_err(fe->dev, "ASoC: can't connect %s\n",
962 				list->widgets[i]->name);
963 			break;
964 		} else if (err == 0) /* already connected */
965 			continue;
966 
967 		/* new */
968 		be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
969 		new++;
970 	}
971 
972 	dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
973 	return new;
974 }
975 
976 /*
977  * Find the corresponding BE DAIs that source or sink audio to this
978  * FE substream.
979  */
980 static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
981 	int stream, struct snd_soc_dapm_widget_list **list, int new)
982 {
983 	if (new)
984 		return dpcm_add_paths(fe, stream, list);
985 	else
986 		return dpcm_prune_paths(fe, stream, list);
987 }
988 
989 static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
990 {
991 	struct snd_soc_dpcm *dpcm;
992 
993 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
994 		dpcm->be->dpcm[stream].runtime_update =
995 						SND_SOC_DPCM_UPDATE_NO;
996 }
997 
998 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
999 	int stream)
1000 {
1001 	struct snd_soc_dpcm *dpcm;
1002 
1003 	/* disable any enabled and non active backends */
1004 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1005 
1006 		struct snd_soc_pcm_runtime *be = dpcm->be;
1007 		struct snd_pcm_substream *be_substream =
1008 			snd_soc_dpcm_get_substream(be, stream);
1009 
1010 		if (be->dpcm[stream].users == 0)
1011 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1012 				stream ? "capture" : "playback",
1013 				be->dpcm[stream].state);
1014 
1015 		if (--be->dpcm[stream].users != 0)
1016 			continue;
1017 
1018 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1019 			continue;
1020 
1021 		soc_pcm_close(be_substream);
1022 		be_substream->runtime = NULL;
1023 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1024 	}
1025 }
1026 
1027 static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1028 {
1029 	struct snd_soc_dpcm *dpcm;
1030 	int err, count = 0;
1031 
1032 	/* only startup BE DAIs that are either sinks or sources to this FE DAI */
1033 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1034 
1035 		struct snd_soc_pcm_runtime *be = dpcm->be;
1036 		struct snd_pcm_substream *be_substream =
1037 			snd_soc_dpcm_get_substream(be, stream);
1038 
1039 		/* is this op for this BE ? */
1040 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1041 			continue;
1042 
1043 		/* first time the dpcm is open ? */
1044 		if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1045 			dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1046 				stream ? "capture" : "playback",
1047 				be->dpcm[stream].state);
1048 
1049 		if (be->dpcm[stream].users++ != 0)
1050 			continue;
1051 
1052 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1053 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1054 			continue;
1055 
1056 		dev_dbg(be->dev, "ASoC: open BE %s\n", be->dai_link->name);
1057 
1058 		be_substream->runtime = be->dpcm[stream].runtime;
1059 		err = soc_pcm_open(be_substream);
1060 		if (err < 0) {
1061 			dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1062 			be->dpcm[stream].users--;
1063 			if (be->dpcm[stream].users < 0)
1064 				dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1065 					stream ? "capture" : "playback",
1066 					be->dpcm[stream].state);
1067 
1068 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1069 			goto unwind;
1070 		}
1071 
1072 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1073 		count++;
1074 	}
1075 
1076 	return count;
1077 
1078 unwind:
1079 	/* disable any enabled and non active backends */
1080 	list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1081 		struct snd_soc_pcm_runtime *be = dpcm->be;
1082 		struct snd_pcm_substream *be_substream =
1083 			snd_soc_dpcm_get_substream(be, stream);
1084 
1085 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1086 			continue;
1087 
1088 		if (be->dpcm[stream].users == 0)
1089 			dev_err(be->dev, "ASoC: no users %s at close %d\n",
1090 				stream ? "capture" : "playback",
1091 				be->dpcm[stream].state);
1092 
1093 		if (--be->dpcm[stream].users != 0)
1094 			continue;
1095 
1096 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1097 			continue;
1098 
1099 		soc_pcm_close(be_substream);
1100 		be_substream->runtime = NULL;
1101 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1102 	}
1103 
1104 	return err;
1105 }
1106 
1107 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1108 {
1109 	struct snd_pcm_runtime *runtime = substream->runtime;
1110 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1111 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1112 	struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1113 
1114 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1115 		runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1116 		runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1117 		runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1118 		runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1119 		runtime->hw.formats &= cpu_dai_drv->playback.formats;
1120 		runtime->hw.rates = cpu_dai_drv->playback.rates;
1121 	} else {
1122 		runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1123 		runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1124 		runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1125 		runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1126 		runtime->hw.formats &= cpu_dai_drv->capture.formats;
1127 		runtime->hw.rates = cpu_dai_drv->capture.rates;
1128 	}
1129 }
1130 
1131 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1132 {
1133 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1134 	struct snd_pcm_runtime *runtime = fe_substream->runtime;
1135 	int stream = fe_substream->stream, ret = 0;
1136 
1137 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1138 
1139 	ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1140 	if (ret < 0) {
1141 		dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1142 		goto be_err;
1143 	}
1144 
1145 	dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1146 
1147 	/* start the DAI frontend */
1148 	ret = soc_pcm_open(fe_substream);
1149 	if (ret < 0) {
1150 		dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1151 		goto unwind;
1152 	}
1153 
1154 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1155 
1156 	dpcm_set_fe_runtime(fe_substream);
1157 	snd_pcm_limit_hw_rates(runtime);
1158 
1159 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1160 	return 0;
1161 
1162 unwind:
1163 	dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1164 be_err:
1165 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1166 	return ret;
1167 }
1168 
1169 static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1170 {
1171 	struct snd_soc_dpcm *dpcm;
1172 
1173 	/* only shutdown BEs that are either sinks or sources to this FE DAI */
1174 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1175 
1176 		struct snd_soc_pcm_runtime *be = dpcm->be;
1177 		struct snd_pcm_substream *be_substream =
1178 			snd_soc_dpcm_get_substream(be, stream);
1179 
1180 		/* is this op for this BE ? */
1181 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1182 			continue;
1183 
1184 		if (be->dpcm[stream].users == 0)
1185 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1186 				stream ? "capture" : "playback",
1187 				be->dpcm[stream].state);
1188 
1189 		if (--be->dpcm[stream].users != 0)
1190 			continue;
1191 
1192 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1193 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1194 			continue;
1195 
1196 		dev_dbg(be->dev, "ASoC: close BE %s\n",
1197 			dpcm->fe->dai_link->name);
1198 
1199 		soc_pcm_close(be_substream);
1200 		be_substream->runtime = NULL;
1201 
1202 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1203 	}
1204 	return 0;
1205 }
1206 
1207 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1208 {
1209 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1210 	int stream = substream->stream;
1211 
1212 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1213 
1214 	/* shutdown the BEs */
1215 	dpcm_be_dai_shutdown(fe, substream->stream);
1216 
1217 	dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1218 
1219 	/* now shutdown the frontend */
1220 	soc_pcm_close(substream);
1221 
1222 	/* run the stream event for each BE */
1223 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1224 
1225 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1226 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1227 	return 0;
1228 }
1229 
1230 static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1231 {
1232 	struct snd_soc_dpcm *dpcm;
1233 
1234 	/* only hw_params backends that are either sinks or sources
1235 	 * to this frontend DAI */
1236 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1237 
1238 		struct snd_soc_pcm_runtime *be = dpcm->be;
1239 		struct snd_pcm_substream *be_substream =
1240 			snd_soc_dpcm_get_substream(be, stream);
1241 
1242 		/* is this op for this BE ? */
1243 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1244 			continue;
1245 
1246 		/* only free hw when no longer used - check all FEs */
1247 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1248 				continue;
1249 
1250 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1251 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1252 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1253 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1254 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1255 			continue;
1256 
1257 		dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1258 			dpcm->fe->dai_link->name);
1259 
1260 		soc_pcm_hw_free(be_substream);
1261 
1262 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1263 	}
1264 
1265 	return 0;
1266 }
1267 
1268 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1269 {
1270 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1271 	int err, stream = substream->stream;
1272 
1273 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1274 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1275 
1276 	dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1277 
1278 	/* call hw_free on the frontend */
1279 	err = soc_pcm_hw_free(substream);
1280 	if (err < 0)
1281 		dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1282 			fe->dai_link->name);
1283 
1284 	/* only hw_params backends that are either sinks or sources
1285 	 * to this frontend DAI */
1286 	err = dpcm_be_dai_hw_free(fe, stream);
1287 
1288 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1289 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1290 
1291 	mutex_unlock(&fe->card->mutex);
1292 	return 0;
1293 }
1294 
1295 static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1296 {
1297 	struct snd_soc_dpcm *dpcm;
1298 	int ret;
1299 
1300 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1301 
1302 		struct snd_soc_pcm_runtime *be = dpcm->be;
1303 		struct snd_pcm_substream *be_substream =
1304 			snd_soc_dpcm_get_substream(be, stream);
1305 
1306 		/* is this op for this BE ? */
1307 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1308 			continue;
1309 
1310 		/* only allow hw_params() if no connected FEs are running */
1311 		if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1312 			continue;
1313 
1314 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1315 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1316 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1317 			continue;
1318 
1319 		dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1320 			dpcm->fe->dai_link->name);
1321 
1322 		/* copy params for each dpcm */
1323 		memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1324 				sizeof(struct snd_pcm_hw_params));
1325 
1326 		/* perform any hw_params fixups */
1327 		if (be->dai_link->be_hw_params_fixup) {
1328 			ret = be->dai_link->be_hw_params_fixup(be,
1329 					&dpcm->hw_params);
1330 			if (ret < 0) {
1331 				dev_err(be->dev,
1332 					"ASoC: hw_params BE fixup failed %d\n",
1333 					ret);
1334 				goto unwind;
1335 			}
1336 		}
1337 
1338 		ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1339 		if (ret < 0) {
1340 			dev_err(dpcm->be->dev,
1341 				"ASoC: hw_params BE failed %d\n", ret);
1342 			goto unwind;
1343 		}
1344 
1345 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1346 	}
1347 	return 0;
1348 
1349 unwind:
1350 	/* disable any enabled and non active backends */
1351 	list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1352 		struct snd_soc_pcm_runtime *be = dpcm->be;
1353 		struct snd_pcm_substream *be_substream =
1354 			snd_soc_dpcm_get_substream(be, stream);
1355 
1356 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1357 			continue;
1358 
1359 		/* only allow hw_free() if no connected FEs are running */
1360 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1361 			continue;
1362 
1363 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1364 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1365 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1366 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1367 			continue;
1368 
1369 		soc_pcm_hw_free(be_substream);
1370 	}
1371 
1372 	return ret;
1373 }
1374 
1375 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1376 				 struct snd_pcm_hw_params *params)
1377 {
1378 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1379 	int ret, stream = substream->stream;
1380 
1381 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1382 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1383 
1384 	memcpy(&fe->dpcm[substream->stream].hw_params, params,
1385 			sizeof(struct snd_pcm_hw_params));
1386 	ret = dpcm_be_dai_hw_params(fe, substream->stream);
1387 	if (ret < 0) {
1388 		dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
1389 		goto out;
1390 	}
1391 
1392 	dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1393 			fe->dai_link->name, params_rate(params),
1394 			params_channels(params), params_format(params));
1395 
1396 	/* call hw_params on the frontend */
1397 	ret = soc_pcm_hw_params(substream, params);
1398 	if (ret < 0) {
1399 		dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
1400 		dpcm_be_dai_hw_free(fe, stream);
1401 	 } else
1402 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1403 
1404 out:
1405 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1406 	mutex_unlock(&fe->card->mutex);
1407 	return ret;
1408 }
1409 
1410 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1411 		struct snd_pcm_substream *substream, int cmd)
1412 {
1413 	int ret;
1414 
1415 	dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
1416 			dpcm->fe->dai_link->name, cmd);
1417 
1418 	ret = soc_pcm_trigger(substream, cmd);
1419 	if (ret < 0)
1420 		dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
1421 
1422 	return ret;
1423 }
1424 
1425 static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1426 			       int cmd)
1427 {
1428 	struct snd_soc_dpcm *dpcm;
1429 	int ret = 0;
1430 
1431 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1432 
1433 		struct snd_soc_pcm_runtime *be = dpcm->be;
1434 		struct snd_pcm_substream *be_substream =
1435 			snd_soc_dpcm_get_substream(be, stream);
1436 
1437 		/* is this op for this BE ? */
1438 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1439 			continue;
1440 
1441 		switch (cmd) {
1442 		case SNDRV_PCM_TRIGGER_START:
1443 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1444 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1445 				continue;
1446 
1447 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1448 			if (ret)
1449 				return ret;
1450 
1451 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1452 			break;
1453 		case SNDRV_PCM_TRIGGER_RESUME:
1454 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1455 				continue;
1456 
1457 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1458 			if (ret)
1459 				return ret;
1460 
1461 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1462 			break;
1463 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1464 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1465 				continue;
1466 
1467 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1468 			if (ret)
1469 				return ret;
1470 
1471 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1472 			break;
1473 		case SNDRV_PCM_TRIGGER_STOP:
1474 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1475 				continue;
1476 
1477 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1478 				continue;
1479 
1480 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1481 			if (ret)
1482 				return ret;
1483 
1484 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1485 			break;
1486 		case SNDRV_PCM_TRIGGER_SUSPEND:
1487 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1488 				continue;
1489 
1490 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1491 				continue;
1492 
1493 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1494 			if (ret)
1495 				return ret;
1496 
1497 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1498 			break;
1499 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1500 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1501 				continue;
1502 
1503 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1504 				continue;
1505 
1506 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1507 			if (ret)
1508 				return ret;
1509 
1510 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1511 			break;
1512 		}
1513 	}
1514 
1515 	return ret;
1516 }
1517 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1518 
1519 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
1520 {
1521 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1522 	int stream = substream->stream, ret;
1523 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1524 
1525 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1526 
1527 	switch (trigger) {
1528 	case SND_SOC_DPCM_TRIGGER_PRE:
1529 		/* call trigger on the frontend before the backend. */
1530 
1531 		dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
1532 				fe->dai_link->name, cmd);
1533 
1534 		ret = soc_pcm_trigger(substream, cmd);
1535 		if (ret < 0) {
1536 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1537 			goto out;
1538 		}
1539 
1540 		ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1541 		break;
1542 	case SND_SOC_DPCM_TRIGGER_POST:
1543 		/* call trigger on the frontend after the backend. */
1544 
1545 		ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1546 		if (ret < 0) {
1547 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1548 			goto out;
1549 		}
1550 
1551 		dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
1552 				fe->dai_link->name, cmd);
1553 
1554 		ret = soc_pcm_trigger(substream, cmd);
1555 		break;
1556 	case SND_SOC_DPCM_TRIGGER_BESPOKE:
1557 		/* bespoke trigger() - handles both FE and BEs */
1558 
1559 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
1560 				fe->dai_link->name, cmd);
1561 
1562 		ret = soc_pcm_bespoke_trigger(substream, cmd);
1563 		if (ret < 0) {
1564 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1565 			goto out;
1566 		}
1567 		break;
1568 	default:
1569 		dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
1570 				fe->dai_link->name);
1571 		ret = -EINVAL;
1572 		goto out;
1573 	}
1574 
1575 	switch (cmd) {
1576 	case SNDRV_PCM_TRIGGER_START:
1577 	case SNDRV_PCM_TRIGGER_RESUME:
1578 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1579 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1580 		break;
1581 	case SNDRV_PCM_TRIGGER_STOP:
1582 	case SNDRV_PCM_TRIGGER_SUSPEND:
1583 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1584 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1585 		break;
1586 	}
1587 
1588 out:
1589 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1590 	return ret;
1591 }
1592 
1593 static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1594 {
1595 	struct snd_soc_dpcm *dpcm;
1596 	int ret = 0;
1597 
1598 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1599 
1600 		struct snd_soc_pcm_runtime *be = dpcm->be;
1601 		struct snd_pcm_substream *be_substream =
1602 			snd_soc_dpcm_get_substream(be, stream);
1603 
1604 		/* is this op for this BE ? */
1605 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1606 			continue;
1607 
1608 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1609 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1610 			continue;
1611 
1612 		dev_dbg(be->dev, "ASoC: prepare BE %s\n",
1613 			dpcm->fe->dai_link->name);
1614 
1615 		ret = soc_pcm_prepare(be_substream);
1616 		if (ret < 0) {
1617 			dev_err(be->dev, "ASoC: backend prepare failed %d\n",
1618 				ret);
1619 			break;
1620 		}
1621 
1622 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1623 	}
1624 	return ret;
1625 }
1626 
1627 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
1628 {
1629 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1630 	int stream = substream->stream, ret = 0;
1631 
1632 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1633 
1634 	dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
1635 
1636 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1637 
1638 	/* there is no point preparing this FE if there are no BEs */
1639 	if (list_empty(&fe->dpcm[stream].be_clients)) {
1640 		dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
1641 				fe->dai_link->name);
1642 		ret = -EINVAL;
1643 		goto out;
1644 	}
1645 
1646 	ret = dpcm_be_dai_prepare(fe, substream->stream);
1647 	if (ret < 0)
1648 		goto out;
1649 
1650 	/* call prepare on the frontend */
1651 	ret = soc_pcm_prepare(substream);
1652 	if (ret < 0) {
1653 		dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
1654 			fe->dai_link->name);
1655 		goto out;
1656 	}
1657 
1658 	/* run the stream event for each BE */
1659 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1660 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1661 
1662 out:
1663 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1664 	mutex_unlock(&fe->card->mutex);
1665 
1666 	return ret;
1667 }
1668 
1669 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1670 		     unsigned int cmd, void *arg)
1671 {
1672 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1673 	struct snd_soc_platform *platform = rtd->platform;
1674 
1675 	if (platform->driver->ops->ioctl)
1676 		return platform->driver->ops->ioctl(substream, cmd, arg);
1677 	return snd_pcm_lib_ioctl(substream, cmd, arg);
1678 }
1679 
1680 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1681 {
1682 	struct snd_pcm_substream *substream =
1683 		snd_soc_dpcm_get_substream(fe, stream);
1684 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1685 	int err;
1686 
1687 	dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
1688 			stream ? "capture" : "playback", fe->dai_link->name);
1689 
1690 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1691 		/* call bespoke trigger - FE takes care of all BE triggers */
1692 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
1693 				fe->dai_link->name);
1694 
1695 		err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1696 		if (err < 0)
1697 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1698 	} else {
1699 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
1700 			fe->dai_link->name);
1701 
1702 		err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1703 		if (err < 0)
1704 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1705 	}
1706 
1707 	err = dpcm_be_dai_hw_free(fe, stream);
1708 	if (err < 0)
1709 		dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
1710 
1711 	err = dpcm_be_dai_shutdown(fe, stream);
1712 	if (err < 0)
1713 		dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
1714 
1715 	/* run the stream event for each BE */
1716 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1717 
1718 	return 0;
1719 }
1720 
1721 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1722 {
1723 	struct snd_pcm_substream *substream =
1724 		snd_soc_dpcm_get_substream(fe, stream);
1725 	struct snd_soc_dpcm *dpcm;
1726 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1727 	int ret;
1728 
1729 	dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
1730 			stream ? "capture" : "playback", fe->dai_link->name);
1731 
1732 	/* Only start the BE if the FE is ready */
1733 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1734 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1735 		return -EINVAL;
1736 
1737 	/* startup must always be called for new BEs */
1738 	ret = dpcm_be_dai_startup(fe, stream);
1739 	if (ret < 0)
1740 		goto disconnect;
1741 
1742 	/* keep going if FE state is > open */
1743 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1744 		return 0;
1745 
1746 	ret = dpcm_be_dai_hw_params(fe, stream);
1747 	if (ret < 0)
1748 		goto close;
1749 
1750 	/* keep going if FE state is > hw_params */
1751 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1752 		return 0;
1753 
1754 
1755 	ret = dpcm_be_dai_prepare(fe, stream);
1756 	if (ret < 0)
1757 		goto hw_free;
1758 
1759 	/* run the stream event for each BE */
1760 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1761 
1762 	/* keep going if FE state is > prepare */
1763 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1764 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1765 		return 0;
1766 
1767 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1768 		/* call trigger on the frontend - FE takes care of all BE triggers */
1769 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
1770 				fe->dai_link->name);
1771 
1772 		ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1773 		if (ret < 0) {
1774 			dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
1775 			goto hw_free;
1776 		}
1777 	} else {
1778 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
1779 			fe->dai_link->name);
1780 
1781 		ret = dpcm_be_dai_trigger(fe, stream,
1782 					SNDRV_PCM_TRIGGER_START);
1783 		if (ret < 0) {
1784 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1785 			goto hw_free;
1786 		}
1787 	}
1788 
1789 	return 0;
1790 
1791 hw_free:
1792 	dpcm_be_dai_hw_free(fe, stream);
1793 close:
1794 	dpcm_be_dai_shutdown(fe, stream);
1795 disconnect:
1796 	/* disconnect any non started BEs */
1797 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1798 		struct snd_soc_pcm_runtime *be = dpcm->be;
1799 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1800 				dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1801 	}
1802 
1803 	return ret;
1804 }
1805 
1806 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1807 {
1808 	int ret;
1809 
1810 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1811 	ret = dpcm_run_update_startup(fe, stream);
1812 	if (ret < 0)
1813 		dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
1814 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1815 
1816 	return ret;
1817 }
1818 
1819 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1820 {
1821 	int ret;
1822 
1823 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1824 	ret = dpcm_run_update_shutdown(fe, stream);
1825 	if (ret < 0)
1826 		dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
1827 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1828 
1829 	return ret;
1830 }
1831 
1832 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1833  * any DAI links.
1834  */
1835 int soc_dpcm_runtime_update(struct snd_soc_dapm_widget *widget)
1836 {
1837 	struct snd_soc_card *card;
1838 	int i, old, new, paths;
1839 
1840 	if (widget->codec)
1841 		card = widget->codec->card;
1842 	else if (widget->platform)
1843 		card = widget->platform->card;
1844 	else
1845 		return -EINVAL;
1846 
1847 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1848 	for (i = 0; i < card->num_rtd; i++) {
1849 		struct snd_soc_dapm_widget_list *list;
1850 		struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1851 
1852 		/* make sure link is FE */
1853 		if (!fe->dai_link->dynamic)
1854 			continue;
1855 
1856 		/* only check active links */
1857 		if (!fe->cpu_dai->active)
1858 			continue;
1859 
1860 		/* DAPM sync will call this to update DSP paths */
1861 		dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
1862 			fe->dai_link->name);
1863 
1864 		/* skip if FE doesn't have playback capability */
1865 		if (!fe->cpu_dai->driver->playback.channels_min)
1866 			goto capture;
1867 
1868 		paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1869 		if (paths < 0) {
1870 			dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
1871 					fe->dai_link->name,  "playback");
1872 			mutex_unlock(&card->mutex);
1873 			return paths;
1874 		}
1875 
1876 		/* update any new playback paths */
1877 		new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1878 		if (new) {
1879 			dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1880 			dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1881 			dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1882 		}
1883 
1884 		/* update any old playback paths */
1885 		old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1886 		if (old) {
1887 			dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1888 			dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1889 			dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1890 		}
1891 
1892 capture:
1893 		/* skip if FE doesn't have capture capability */
1894 		if (!fe->cpu_dai->driver->capture.channels_min)
1895 			continue;
1896 
1897 		paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1898 		if (paths < 0) {
1899 			dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
1900 					fe->dai_link->name,  "capture");
1901 			mutex_unlock(&card->mutex);
1902 			return paths;
1903 		}
1904 
1905 		/* update any new capture paths */
1906 		new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1907 		if (new) {
1908 			dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1909 			dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1910 			dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1911 		}
1912 
1913 		/* update any old capture paths */
1914 		old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1915 		if (old) {
1916 			dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1917 			dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1918 			dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1919 		}
1920 
1921 		dpcm_path_put(&list);
1922 	}
1923 
1924 	mutex_unlock(&card->mutex);
1925 	return 0;
1926 }
1927 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
1928 {
1929 	struct snd_soc_dpcm *dpcm;
1930 	struct list_head *clients =
1931 		&fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
1932 
1933 	list_for_each_entry(dpcm, clients, list_be) {
1934 
1935 		struct snd_soc_pcm_runtime *be = dpcm->be;
1936 		struct snd_soc_dai *dai = be->codec_dai;
1937 		struct snd_soc_dai_driver *drv = dai->driver;
1938 
1939 		if (be->dai_link->ignore_suspend)
1940 			continue;
1941 
1942 		dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
1943 
1944 		if (drv->ops->digital_mute && dai->playback_active)
1945 				drv->ops->digital_mute(dai, mute);
1946 	}
1947 
1948 	return 0;
1949 }
1950 
1951 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
1952 {
1953 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1954 	struct snd_soc_dpcm *dpcm;
1955 	struct snd_soc_dapm_widget_list *list;
1956 	int ret;
1957 	int stream = fe_substream->stream;
1958 
1959 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1960 	fe->dpcm[stream].runtime = fe_substream->runtime;
1961 
1962 	if (dpcm_path_get(fe, stream, &list) <= 0) {
1963 		dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
1964 			fe->dai_link->name, stream ? "capture" : "playback");
1965 	}
1966 
1967 	/* calculate valid and active FE <-> BE dpcms */
1968 	dpcm_process_paths(fe, stream, &list, 1);
1969 
1970 	ret = dpcm_fe_dai_startup(fe_substream);
1971 	if (ret < 0) {
1972 		/* clean up all links */
1973 		list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1974 			dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1975 
1976 		dpcm_be_disconnect(fe, stream);
1977 		fe->dpcm[stream].runtime = NULL;
1978 	}
1979 
1980 	dpcm_clear_pending_state(fe, stream);
1981 	dpcm_path_put(&list);
1982 	mutex_unlock(&fe->card->mutex);
1983 	return ret;
1984 }
1985 
1986 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
1987 {
1988 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1989 	struct snd_soc_dpcm *dpcm;
1990 	int stream = fe_substream->stream, ret;
1991 
1992 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1993 	ret = dpcm_fe_dai_shutdown(fe_substream);
1994 
1995 	/* mark FE's links ready to prune */
1996 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1997 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1998 
1999 	dpcm_be_disconnect(fe, stream);
2000 
2001 	fe->dpcm[stream].runtime = NULL;
2002 	mutex_unlock(&fe->card->mutex);
2003 	return ret;
2004 }
2005 
2006 /* create a new pcm */
2007 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2008 {
2009 	struct snd_soc_platform *platform = rtd->platform;
2010 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
2011 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2012 	struct snd_pcm *pcm;
2013 	char new_name[64];
2014 	int ret = 0, playback = 0, capture = 0;
2015 
2016 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2017 		if (cpu_dai->driver->playback.channels_min)
2018 			playback = 1;
2019 		if (cpu_dai->driver->capture.channels_min)
2020 			capture = 1;
2021 	} else {
2022 		if (codec_dai->driver->playback.channels_min &&
2023 		    cpu_dai->driver->playback.channels_min)
2024 			playback = 1;
2025 		if (codec_dai->driver->capture.channels_min &&
2026 		    cpu_dai->driver->capture.channels_min)
2027 			capture = 1;
2028 	}
2029 
2030 	/* create the PCM */
2031 	if (rtd->dai_link->no_pcm) {
2032 		snprintf(new_name, sizeof(new_name), "(%s)",
2033 			rtd->dai_link->stream_name);
2034 
2035 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2036 				playback, capture, &pcm);
2037 	} else {
2038 		if (rtd->dai_link->dynamic)
2039 			snprintf(new_name, sizeof(new_name), "%s (*)",
2040 				rtd->dai_link->stream_name);
2041 		else
2042 			snprintf(new_name, sizeof(new_name), "%s %s-%d",
2043 				rtd->dai_link->stream_name, codec_dai->name, num);
2044 
2045 		ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2046 			capture, &pcm);
2047 	}
2048 	if (ret < 0) {
2049 		dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2050 			rtd->dai_link->name);
2051 		return ret;
2052 	}
2053 	dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2054 
2055 	/* DAPM dai link stream work */
2056 	INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2057 
2058 	rtd->pcm = pcm;
2059 	pcm->private_data = rtd;
2060 
2061 	if (rtd->dai_link->no_pcm) {
2062 		if (playback)
2063 			pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2064 		if (capture)
2065 			pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2066 		goto out;
2067 	}
2068 
2069 	/* ASoC PCM operations */
2070 	if (rtd->dai_link->dynamic) {
2071 		rtd->ops.open		= dpcm_fe_dai_open;
2072 		rtd->ops.hw_params	= dpcm_fe_dai_hw_params;
2073 		rtd->ops.prepare	= dpcm_fe_dai_prepare;
2074 		rtd->ops.trigger	= dpcm_fe_dai_trigger;
2075 		rtd->ops.hw_free	= dpcm_fe_dai_hw_free;
2076 		rtd->ops.close		= dpcm_fe_dai_close;
2077 		rtd->ops.pointer	= soc_pcm_pointer;
2078 		rtd->ops.ioctl		= soc_pcm_ioctl;
2079 	} else {
2080 		rtd->ops.open		= soc_pcm_open;
2081 		rtd->ops.hw_params	= soc_pcm_hw_params;
2082 		rtd->ops.prepare	= soc_pcm_prepare;
2083 		rtd->ops.trigger	= soc_pcm_trigger;
2084 		rtd->ops.hw_free	= soc_pcm_hw_free;
2085 		rtd->ops.close		= soc_pcm_close;
2086 		rtd->ops.pointer	= soc_pcm_pointer;
2087 		rtd->ops.ioctl		= soc_pcm_ioctl;
2088 	}
2089 
2090 	if (platform->driver->ops) {
2091 		rtd->ops.ack		= platform->driver->ops->ack;
2092 		rtd->ops.copy		= platform->driver->ops->copy;
2093 		rtd->ops.silence	= platform->driver->ops->silence;
2094 		rtd->ops.page		= platform->driver->ops->page;
2095 		rtd->ops.mmap		= platform->driver->ops->mmap;
2096 	}
2097 
2098 	if (playback)
2099 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2100 
2101 	if (capture)
2102 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2103 
2104 	if (platform->driver->pcm_new) {
2105 		ret = platform->driver->pcm_new(rtd);
2106 		if (ret < 0) {
2107 			dev_err(platform->dev,
2108 				"ASoC: pcm constructor failed: %d\n",
2109 				ret);
2110 			return ret;
2111 		}
2112 	}
2113 
2114 	pcm->private_free = platform->driver->pcm_free;
2115 out:
2116 	dev_info(rtd->card->dev, " %s <-> %s mapping ok\n", codec_dai->name,
2117 		cpu_dai->name);
2118 	return ret;
2119 }
2120 
2121 /* is the current PCM operation for this FE ? */
2122 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2123 {
2124 	if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2125 		return 1;
2126 	return 0;
2127 }
2128 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2129 
2130 /* is the current PCM operation for this BE ? */
2131 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2132 		struct snd_soc_pcm_runtime *be, int stream)
2133 {
2134 	if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2135 	   ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2136 		  be->dpcm[stream].runtime_update))
2137 		return 1;
2138 	return 0;
2139 }
2140 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2141 
2142 /* get the substream for this BE */
2143 struct snd_pcm_substream *
2144 	snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2145 {
2146 	return be->pcm->streams[stream].substream;
2147 }
2148 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2149 
2150 /* get the BE runtime state */
2151 enum snd_soc_dpcm_state
2152 	snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2153 {
2154 	return be->dpcm[stream].state;
2155 }
2156 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2157 
2158 /* set the BE runtime state */
2159 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2160 		int stream, enum snd_soc_dpcm_state state)
2161 {
2162 	be->dpcm[stream].state = state;
2163 }
2164 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2165 
2166 /*
2167  * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2168  * are not running, paused or suspended for the specified stream direction.
2169  */
2170 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2171 		struct snd_soc_pcm_runtime *be, int stream)
2172 {
2173 	struct snd_soc_dpcm *dpcm;
2174 	int state;
2175 
2176 	list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2177 
2178 		if (dpcm->fe == fe)
2179 			continue;
2180 
2181 		state = dpcm->fe->dpcm[stream].state;
2182 		if (state == SND_SOC_DPCM_STATE_START ||
2183 			state == SND_SOC_DPCM_STATE_PAUSED ||
2184 			state == SND_SOC_DPCM_STATE_SUSPEND)
2185 			return 0;
2186 	}
2187 
2188 	/* it's safe to free/stop this BE DAI */
2189 	return 1;
2190 }
2191 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2192 
2193 /*
2194  * We can only change hw params a BE DAI if any of it's FE are not prepared,
2195  * running, paused or suspended for the specified stream direction.
2196  */
2197 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2198 		struct snd_soc_pcm_runtime *be, int stream)
2199 {
2200 	struct snd_soc_dpcm *dpcm;
2201 	int state;
2202 
2203 	list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2204 
2205 		if (dpcm->fe == fe)
2206 			continue;
2207 
2208 		state = dpcm->fe->dpcm[stream].state;
2209 		if (state == SND_SOC_DPCM_STATE_START ||
2210 			state == SND_SOC_DPCM_STATE_PAUSED ||
2211 			state == SND_SOC_DPCM_STATE_SUSPEND ||
2212 			state == SND_SOC_DPCM_STATE_PREPARE)
2213 			return 0;
2214 	}
2215 
2216 	/* it's safe to change hw_params */
2217 	return 1;
2218 }
2219 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2220 
2221 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2222 		int cmd, struct snd_soc_platform *platform)
2223 {
2224 	if (platform->driver->ops->trigger)
2225 		return platform->driver->ops->trigger(substream, cmd);
2226 	return 0;
2227 }
2228 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2229 
2230 #ifdef CONFIG_DEBUG_FS
2231 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2232 {
2233 	switch (state) {
2234 	case SND_SOC_DPCM_STATE_NEW:
2235 		return "new";
2236 	case SND_SOC_DPCM_STATE_OPEN:
2237 		return "open";
2238 	case SND_SOC_DPCM_STATE_HW_PARAMS:
2239 		return "hw_params";
2240 	case SND_SOC_DPCM_STATE_PREPARE:
2241 		return "prepare";
2242 	case SND_SOC_DPCM_STATE_START:
2243 		return "start";
2244 	case SND_SOC_DPCM_STATE_STOP:
2245 		return "stop";
2246 	case SND_SOC_DPCM_STATE_SUSPEND:
2247 		return "suspend";
2248 	case SND_SOC_DPCM_STATE_PAUSED:
2249 		return "paused";
2250 	case SND_SOC_DPCM_STATE_HW_FREE:
2251 		return "hw_free";
2252 	case SND_SOC_DPCM_STATE_CLOSE:
2253 		return "close";
2254 	}
2255 
2256 	return "unknown";
2257 }
2258 
2259 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2260 				int stream, char *buf, size_t size)
2261 {
2262 	struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2263 	struct snd_soc_dpcm *dpcm;
2264 	ssize_t offset = 0;
2265 
2266 	/* FE state */
2267 	offset += snprintf(buf + offset, size - offset,
2268 			"[%s - %s]\n", fe->dai_link->name,
2269 			stream ? "Capture" : "Playback");
2270 
2271 	offset += snprintf(buf + offset, size - offset, "State: %s\n",
2272 	                dpcm_state_string(fe->dpcm[stream].state));
2273 
2274 	if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2275 	    (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2276 		offset += snprintf(buf + offset, size - offset,
2277 				"Hardware Params: "
2278 				"Format = %s, Channels = %d, Rate = %d\n",
2279 				snd_pcm_format_name(params_format(params)),
2280 				params_channels(params),
2281 				params_rate(params));
2282 
2283 	/* BEs state */
2284 	offset += snprintf(buf + offset, size - offset, "Backends:\n");
2285 
2286 	if (list_empty(&fe->dpcm[stream].be_clients)) {
2287 		offset += snprintf(buf + offset, size - offset,
2288 				" No active DSP links\n");
2289 		goto out;
2290 	}
2291 
2292 	list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2293 		struct snd_soc_pcm_runtime *be = dpcm->be;
2294 		params = &dpcm->hw_params;
2295 
2296 		offset += snprintf(buf + offset, size - offset,
2297 				"- %s\n", be->dai_link->name);
2298 
2299 		offset += snprintf(buf + offset, size - offset,
2300 				"   State: %s\n",
2301 				dpcm_state_string(be->dpcm[stream].state));
2302 
2303 		if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2304 		    (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2305 			offset += snprintf(buf + offset, size - offset,
2306 				"   Hardware Params: "
2307 				"Format = %s, Channels = %d, Rate = %d\n",
2308 				snd_pcm_format_name(params_format(params)),
2309 				params_channels(params),
2310 				params_rate(params));
2311 	}
2312 
2313 out:
2314 	return offset;
2315 }
2316 
2317 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2318 				size_t count, loff_t *ppos)
2319 {
2320 	struct snd_soc_pcm_runtime *fe = file->private_data;
2321 	ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2322 	char *buf;
2323 
2324 	buf = kmalloc(out_count, GFP_KERNEL);
2325 	if (!buf)
2326 		return -ENOMEM;
2327 
2328 	if (fe->cpu_dai->driver->playback.channels_min)
2329 		offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2330 					buf + offset, out_count - offset);
2331 
2332 	if (fe->cpu_dai->driver->capture.channels_min)
2333 		offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2334 					buf + offset, out_count - offset);
2335 
2336 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2337 
2338 	kfree(buf);
2339 	return ret;
2340 }
2341 
2342 static const struct file_operations dpcm_state_fops = {
2343 	.open = simple_open,
2344 	.read = dpcm_state_read_file,
2345 	.llseek = default_llseek,
2346 };
2347 
2348 int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2349 {
2350 	if (!rtd->dai_link)
2351 		return 0;
2352 
2353 	rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2354 			rtd->card->debugfs_card_root);
2355 	if (!rtd->debugfs_dpcm_root) {
2356 		dev_dbg(rtd->dev,
2357 			 "ASoC: Failed to create dpcm debugfs directory %s\n",
2358 			 rtd->dai_link->name);
2359 		return -EINVAL;
2360 	}
2361 
2362 	rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2363 						rtd->debugfs_dpcm_root,
2364 						rtd, &dpcm_state_fops);
2365 
2366 	return 0;
2367 }
2368 #endif
2369