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