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