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