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