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