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