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