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