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