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