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