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