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