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