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