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