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