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