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