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