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