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