blob: b3dadd64c2f7665b2d6bb55b8e805e7e77709b21 [file] [log] [blame]
Liam Girdwoodddee6272011-06-09 14:45:53 +01001/*
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>
Mark Brownd6652ef2011-12-03 20:14:31 +000022#include <linux/pm_runtime.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010023#include <linux/slab.h>
24#include <linux/workqueue.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070025#include <linux/debugfs.h>
26#include <linux/dma-mapping.h>
27#include <linux/export.h>
Iliyan Malchev734dc212012-10-02 09:22:36 -070028#include <linux/bug.h>
SathishKumar Manief4feb32012-10-09 13:31:13 -070029#include <linux/ratelimit.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010030#include <sound/core.h>
31#include <sound/pcm.h>
32#include <sound/pcm_params.h>
33#include <sound/soc.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070034#include <sound/soc-dpcm.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010035#include <sound/initval.h>
36
Steve Mucklef132c6c2012-06-06 18:30:57 -070037#define MAX_BE_USERS 8 /* adjust if too low for everday use */
38
39static int soc_dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream);
40
41/* ASoC no host IO hardware.
42 * TODO: fine tune these values for all host less transfers.
43 */
44static const struct snd_pcm_hardware no_host_hardware = {
45 .info = SNDRV_PCM_INFO_MMAP |
46 SNDRV_PCM_INFO_MMAP_VALID |
47 SNDRV_PCM_INFO_INTERLEAVED |
48 SNDRV_PCM_INFO_PAUSE |
49 SNDRV_PCM_INFO_RESUME,
50 .formats = SNDRV_PCM_FMTBIT_S16_LE |
51 SNDRV_PCM_FMTBIT_S32_LE,
52 .period_bytes_min = PAGE_SIZE >> 2,
53 .period_bytes_max = PAGE_SIZE >> 1,
54 .periods_min = 2,
55 .periods_max = 4,
56 .buffer_bytes_max = PAGE_SIZE,
57};
58
59/*
60 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
61 * are not running, paused or suspended for the specified stream direction.
62 */
63int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
64 struct snd_soc_pcm_runtime *be, int stream)
65{
66 struct snd_soc_dpcm_params *dpcm_params;
67
68 list_for_each_entry(dpcm_params, &be->dpcm[stream].fe_clients, list_fe) {
69
70 if (dpcm_params->fe == fe)
71 continue;
72
73 if (dpcm_params->fe->dpcm[stream].state == SND_SOC_DPCM_STATE_START ||
74 dpcm_params->fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PAUSED ||
75 dpcm_params->fe->dpcm[stream].state == SND_SOC_DPCM_STATE_SUSPEND)
76 return 0;
77 }
78 return 1;
79}
80EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
81
82/*
83 * We can only change hw params a BE DAI if any of it's FE are not prepared,
84 * running, paused or suspended for the specified stream direction.
85 */
86static int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
87 struct snd_soc_pcm_runtime *be, int stream)
88{
89 struct snd_soc_dpcm_params *dpcm_params;
90
91 list_for_each_entry(dpcm_params, &be->dpcm[stream].fe_clients, list_fe) {
92
93 if (dpcm_params->fe == fe)
94 continue;
95
96 if (dpcm_params->fe->dpcm[stream].state == SND_SOC_DPCM_STATE_START ||
97 dpcm_params->fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PAUSED ||
98 dpcm_params->fe->dpcm[stream].state == SND_SOC_DPCM_STATE_SUSPEND ||
99 dpcm_params->fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE)
100 return 0;
101 }
102 return 1;
103}
104
Dong Aisheng17841022011-08-29 17:15:14 +0800105static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
106 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100107{
108 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100109 int ret;
110
Dong Aisheng17841022011-08-29 17:15:14 +0800111 if (!soc_dai->driver->symmetric_rates &&
Liam Girdwoodddee6272011-06-09 14:45:53 +0100112 !rtd->dai_link->symmetric_rates)
113 return 0;
114
115 /* This can happen if multiple streams are starting simultaneously -
116 * the second can need to get its constraints before the first has
117 * picked a rate. Complain and allow the application to carry on.
118 */
Dong Aisheng17841022011-08-29 17:15:14 +0800119 if (!soc_dai->rate) {
120 dev_warn(soc_dai->dev,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100121 "Not enforcing symmetric_rates due to race\n");
122 return 0;
123 }
124
Dong Aisheng17841022011-08-29 17:15:14 +0800125 dev_dbg(soc_dai->dev, "Symmetry forces %dHz rate\n", soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100126
127 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
128 SNDRV_PCM_HW_PARAM_RATE,
Dong Aisheng17841022011-08-29 17:15:14 +0800129 soc_dai->rate, soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100130 if (ret < 0) {
Dong Aisheng17841022011-08-29 17:15:14 +0800131 dev_err(soc_dai->dev,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100132 "Unable to apply rate symmetry constraint: %d\n", ret);
133 return ret;
134 }
135
136 return 0;
137}
138
139/*
Mark Brown58ba9b22012-01-16 18:38:51 +0000140 * List of sample sizes that might go over the bus for parameter
141 * application. There ought to be a wildcard sample size for things
142 * like the DAC/ADC resolution to use but there isn't right now.
143 */
144static int sample_sizes[] = {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700145 8, 16, 24, 32,
Mark Brown58ba9b22012-01-16 18:38:51 +0000146};
147
148static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
149 struct snd_soc_dai *dai)
150{
151 int ret, i, bits;
152
153 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
154 bits = dai->driver->playback.sig_bits;
155 else
156 bits = dai->driver->capture.sig_bits;
157
158 if (!bits)
159 return;
160
161 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
Mark Brown278047f2012-01-19 18:04:18 +0000162 if (bits >= sample_sizes[i])
163 continue;
164
165 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
166 sample_sizes[i], bits);
Mark Brown58ba9b22012-01-16 18:38:51 +0000167 if (ret != 0)
168 dev_warn(dai->dev,
169 "Failed to set MSB %d/%d: %d\n",
170 bits, sample_sizes[i], ret);
171 }
172}
173
174/*
Steve Mucklef132c6c2012-06-06 18:30:57 -0700175 * stream event, send event to FE and all active BEs.
176 */
177int soc_dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe,
178 int dir, const char *stream, int event)
179{
180 struct snd_soc_dpcm_params *dpcm_params;
181
182 snd_soc_dapm_rtd_stream_event(fe, dir, event);
183
184 list_for_each_entry(dpcm_params, &fe->dpcm[dir].be_clients, list_be) {
185
186 struct snd_soc_pcm_runtime *be = dpcm_params->be;
187
188 dev_dbg(be->dev, "pm: BE %s stream %s event %d dir %d\n",
189 be->dai_link->name, stream, event, dir);
190
191 snd_soc_dapm_rtd_stream_event(be, dir, event);
192 }
193
194 return 0;
195}
196
197/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100198 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
199 * then initialized and any private data can be allocated. This also calls
200 * startup for the cpu DAI, platform, machine and codec DAI.
201 */
202static int soc_pcm_open(struct snd_pcm_substream *substream)
203{
204 struct snd_soc_pcm_runtime *rtd = substream->private_data;
205 struct snd_pcm_runtime *runtime = substream->runtime;
206 struct snd_soc_platform *platform = rtd->platform;
207 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
208 struct snd_soc_dai *codec_dai = rtd->codec_dai;
209 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
210 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
211 int ret = 0;
212
Mark Brownd6652ef2011-12-03 20:14:31 +0000213 pm_runtime_get_sync(cpu_dai->dev);
214 pm_runtime_get_sync(codec_dai->dev);
215 pm_runtime_get_sync(platform->dev);
216
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100217 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100218
Steve Mucklef132c6c2012-06-06 18:30:57 -0700219 if (rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST)
220 snd_soc_set_runtime_hwparams(substream, &no_host_hardware);
221
Liam Girdwoodddee6272011-06-09 14:45:53 +0100222 /* startup the audio subsystem */
223 if (cpu_dai->driver->ops->startup) {
224 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
225 if (ret < 0) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700226 printk(KERN_ERR "asoc: can't open interface %s\n",
227 cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100228 goto out;
229 }
230 }
231
232 if (platform->driver->ops && platform->driver->ops->open) {
233 ret = platform->driver->ops->open(substream);
234 if (ret < 0) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700235 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100236 goto platform_err;
237 }
238 }
239
240 if (codec_dai->driver->ops->startup) {
Helen Zeng5749b092012-06-10 11:50:29 -0700241 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
242 ret = codec_dai->driver->ops->startup(substream,
243 codec_dai);
244 if (ret < 0) {
245 printk(KERN_ERR "asoc: can't open codec %s\n",
246 codec_dai->name);
247 goto codec_dai_err;
248 }
249 } else {
250 if (!codec_dai->capture_active) {
251 ret = codec_dai->driver->ops->startup(substream,
252 codec_dai);
253 if (ret < 0) {
254 printk(KERN_ERR "can't open codec %s\n",
255 codec_dai->name);
256 goto codec_dai_err;
257 }
258 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100259 }
260 }
261
262 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
263 ret = rtd->dai_link->ops->startup(substream);
264 if (ret < 0) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700265 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100266 goto machine_err;
267 }
268 }
269
Steve Mucklef132c6c2012-06-06 18:30:57 -0700270 /* DSP DAI links compat checks are different */
271 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
272 goto dynamic;
273
Liam Girdwoodddee6272011-06-09 14:45:53 +0100274 /* Check that the codec and cpu DAIs are compatible */
275 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
276 runtime->hw.rate_min =
277 max(codec_dai_drv->playback.rate_min,
278 cpu_dai_drv->playback.rate_min);
279 runtime->hw.rate_max =
280 min(codec_dai_drv->playback.rate_max,
281 cpu_dai_drv->playback.rate_max);
282 runtime->hw.channels_min =
283 max(codec_dai_drv->playback.channels_min,
284 cpu_dai_drv->playback.channels_min);
285 runtime->hw.channels_max =
286 min(codec_dai_drv->playback.channels_max,
287 cpu_dai_drv->playback.channels_max);
288 runtime->hw.formats =
289 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
290 runtime->hw.rates =
291 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
292 if (codec_dai_drv->playback.rates
293 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
294 runtime->hw.rates |= cpu_dai_drv->playback.rates;
295 if (cpu_dai_drv->playback.rates
296 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
297 runtime->hw.rates |= codec_dai_drv->playback.rates;
298 } else {
299 runtime->hw.rate_min =
300 max(codec_dai_drv->capture.rate_min,
301 cpu_dai_drv->capture.rate_min);
302 runtime->hw.rate_max =
303 min(codec_dai_drv->capture.rate_max,
304 cpu_dai_drv->capture.rate_max);
305 runtime->hw.channels_min =
306 max(codec_dai_drv->capture.channels_min,
307 cpu_dai_drv->capture.channels_min);
308 runtime->hw.channels_max =
309 min(codec_dai_drv->capture.channels_max,
310 cpu_dai_drv->capture.channels_max);
311 runtime->hw.formats =
312 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
313 runtime->hw.rates =
314 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
315 if (codec_dai_drv->capture.rates
316 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
317 runtime->hw.rates |= cpu_dai_drv->capture.rates;
318 if (cpu_dai_drv->capture.rates
319 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
320 runtime->hw.rates |= codec_dai_drv->capture.rates;
321 }
322
323 ret = -EINVAL;
324 snd_pcm_limit_hw_rates(runtime);
325 if (!runtime->hw.rates) {
326 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
327 codec_dai->name, cpu_dai->name);
328 goto config_err;
329 }
330 if (!runtime->hw.formats) {
331 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
332 codec_dai->name, cpu_dai->name);
333 goto config_err;
334 }
335 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
336 runtime->hw.channels_min > runtime->hw.channels_max) {
337 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
338 codec_dai->name, cpu_dai->name);
339 goto config_err;
340 }
341
Mark Brown58ba9b22012-01-16 18:38:51 +0000342 soc_pcm_apply_msb(substream, codec_dai);
343 soc_pcm_apply_msb(substream, cpu_dai);
344
Liam Girdwoodddee6272011-06-09 14:45:53 +0100345 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800346 if (cpu_dai->active) {
347 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
348 if (ret != 0)
349 goto config_err;
350 }
351
352 if (codec_dai->active) {
353 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100354 if (ret != 0)
355 goto config_err;
356 }
357
358 pr_debug("asoc: %s <-> %s info:\n",
359 codec_dai->name, cpu_dai->name);
360 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
361 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
362 runtime->hw.channels_max);
363 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
364 runtime->hw.rate_max);
365
Steve Mucklef132c6c2012-06-06 18:30:57 -0700366dynamic:
Liam Girdwoodddee6272011-06-09 14:45:53 +0100367 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
368 cpu_dai->playback_active++;
369 codec_dai->playback_active++;
370 } else {
371 cpu_dai->capture_active++;
372 codec_dai->capture_active++;
373 }
374 cpu_dai->active++;
375 codec_dai->active++;
376 rtd->codec->active++;
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100377 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100378 return 0;
379
380config_err:
381 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
382 rtd->dai_link->ops->shutdown(substream);
383
384machine_err:
385 if (codec_dai->driver->ops->shutdown)
386 codec_dai->driver->ops->shutdown(substream, codec_dai);
387
388codec_dai_err:
389 if (platform->driver->ops && platform->driver->ops->close)
390 platform->driver->ops->close(substream);
391
392platform_err:
393 if (cpu_dai->driver->ops->shutdown)
394 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
395out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100396 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000397
398 pm_runtime_put(platform->dev);
399 pm_runtime_put(codec_dai->dev);
400 pm_runtime_put(cpu_dai->dev);
401
Liam Girdwoodddee6272011-06-09 14:45:53 +0100402 return ret;
403}
404
405/*
406 * Power down the audio subsystem pmdown_time msecs after close is called.
407 * This is to ensure there are no pops or clicks in between any music tracks
408 * due to DAPM power cycling.
409 */
410static void close_delayed_work(struct work_struct *work)
411{
412 struct snd_soc_pcm_runtime *rtd =
413 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
414 struct snd_soc_dai *codec_dai = rtd->codec_dai;
415
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100416 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100417
418 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
419 codec_dai->driver->playback.stream_name,
420 codec_dai->playback_active ? "active" : "inactive",
421 codec_dai->pop_wait ? "yes" : "no");
422
423 /* are we waiting on this codec DAI stream */
424 if (codec_dai->pop_wait == 1) {
425 codec_dai->pop_wait = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700426 snd_soc_dapm_stream_event(rtd,
427 codec_dai->driver->playback.stream_name,
428 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100429 }
430
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100431 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100432}
433
434/*
435 * Called by ALSA when a PCM substream is closed. Private data can be
436 * freed here. The cpu DAI, codec DAI, machine and platform are also
437 * shutdown.
438 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100439static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100440{
441 struct snd_soc_pcm_runtime *rtd = substream->private_data;
442 struct snd_soc_platform *platform = rtd->platform;
443 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
444 struct snd_soc_dai *codec_dai = rtd->codec_dai;
445 struct snd_soc_codec *codec = rtd->codec;
446
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100447 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100448
449 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
450 cpu_dai->playback_active--;
451 codec_dai->playback_active--;
452 } else {
453 cpu_dai->capture_active--;
454 codec_dai->capture_active--;
455 }
456
457 cpu_dai->active--;
458 codec_dai->active--;
459 codec->active--;
460
Dong Aisheng17841022011-08-29 17:15:14 +0800461 /* clear the corresponding DAIs rate when inactive */
462 if (!cpu_dai->active)
463 cpu_dai->rate = 0;
464
465 if (!codec_dai->active)
466 codec_dai->rate = 0;
Sascha Hauer25b76792011-08-17 09:20:01 +0200467
Liam Girdwoodddee6272011-06-09 14:45:53 +0100468 /* Muting the DAC suppresses artifacts caused during digital
469 * shutdown, for example from stopping clocks.
470 */
471 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
472 snd_soc_dai_digital_mute(codec_dai, 1);
473
Helen Zengd5131792012-06-28 11:24:11 -0700474 if (cpu_dai->driver->ops->shutdown)
475 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
476
477 if (codec_dai->driver->ops->shutdown) {
Helen Zeng5749b092012-06-10 11:50:29 -0700478 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
479 codec_dai->driver->ops->shutdown(substream, codec_dai);
480 } else {
481 if (!codec_dai->capture_active)
482 codec_dai->driver->ops->shutdown(substream,
483 codec_dai);
484 }
485 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100486
Liam Girdwoodddee6272011-06-09 14:45:53 +0100487 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
488 rtd->dai_link->ops->shutdown(substream);
489
490 if (platform->driver->ops && platform->driver->ops->close)
491 platform->driver->ops->close(substream);
492 cpu_dai->runtime = NULL;
493
494 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700495 if (codec->ignore_pmdown_time ||
496 rtd->dai_link->ignore_pmdown_time ||
497 !rtd->pmdown_time) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300498 /* powered down playback stream now */
499 snd_soc_dapm_stream_event(rtd,
Steve Mucklef132c6c2012-06-06 18:30:57 -0700500 codec_dai->driver->playback.stream_name,
501 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300502 } else {
503 /* start delayed pop wq here for playback streams */
504 codec_dai->pop_wait = 1;
505 schedule_delayed_work(&rtd->delayed_work,
506 msecs_to_jiffies(rtd->pmdown_time));
507 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100508 } else {
509 /* capture streams can be powered down now */
Helen Zeng5749b092012-06-10 11:50:29 -0700510 if (!codec_dai->capture_active)
511 snd_soc_dapm_stream_event(rtd,
Steve Mucklef132c6c2012-06-06 18:30:57 -0700512 codec_dai->driver->capture.stream_name,
513 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100514 }
515
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100516 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000517
518 pm_runtime_put(platform->dev);
519 pm_runtime_put(codec_dai->dev);
520 pm_runtime_put(cpu_dai->dev);
521
Liam Girdwoodddee6272011-06-09 14:45:53 +0100522 return 0;
523}
524
525/*
526 * Called by ALSA when the PCM substream is prepared, can set format, sample
527 * rate, etc. This function is non atomic and can be called multiple times,
528 * it can refer to the runtime info.
529 */
530static int soc_pcm_prepare(struct snd_pcm_substream *substream)
531{
532 struct snd_soc_pcm_runtime *rtd = substream->private_data;
533 struct snd_soc_platform *platform = rtd->platform;
534 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
535 struct snd_soc_dai *codec_dai = rtd->codec_dai;
536 int ret = 0;
537
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100538 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100539
540 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
541 ret = rtd->dai_link->ops->prepare(substream);
542 if (ret < 0) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700543 printk(KERN_ERR "asoc: machine prepare error\n");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100544 goto out;
545 }
546 }
547
548 if (platform->driver->ops && platform->driver->ops->prepare) {
549 ret = platform->driver->ops->prepare(substream);
550 if (ret < 0) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700551 printk(KERN_ERR "asoc: platform prepare error\n");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100552 goto out;
553 }
554 }
555
556 if (codec_dai->driver->ops->prepare) {
557 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
558 if (ret < 0) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700559 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100560 goto out;
561 }
562 }
563
564 if (cpu_dai->driver->ops->prepare) {
565 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
566 if (ret < 0) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700567 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100568 goto out;
569 }
570 }
571
572 /* cancel any delayed stream shutdown that is pending */
573 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
574 codec_dai->pop_wait) {
575 codec_dai->pop_wait = 0;
576 cancel_delayed_work(&rtd->delayed_work);
577 }
578
Steve Mucklef132c6c2012-06-06 18:30:57 -0700579 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
580 snd_soc_dapm_stream_event(rtd,
581 codec_dai->driver->playback.stream_name,
582 SND_SOC_DAPM_STREAM_START);
Helen Zeng5749b092012-06-10 11:50:29 -0700583 else {
584 if (codec_dai->capture_active == 1)
585 snd_soc_dapm_stream_event(rtd,
Steve Mucklef132c6c2012-06-06 18:30:57 -0700586 codec_dai->driver->capture.stream_name,
587 SND_SOC_DAPM_STREAM_START);
Helen Zeng5749b092012-06-10 11:50:29 -0700588 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100589 snd_soc_dai_digital_mute(codec_dai, 0);
590
591out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100592 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100593 return ret;
594}
595
596/*
597 * Called by ALSA when the hardware params are set by application. This
598 * function can also be called multiple times and can allocate buffers
599 * (using snd_pcm_lib_* ). It's non-atomic.
600 */
601static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
602 struct snd_pcm_hw_params *params)
603{
604 struct snd_soc_pcm_runtime *rtd = substream->private_data;
605 struct snd_soc_platform *platform = rtd->platform;
606 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
607 struct snd_soc_dai *codec_dai = rtd->codec_dai;
608 int ret = 0;
609
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100610 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100611
612 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
613 ret = rtd->dai_link->ops->hw_params(substream, params);
614 if (ret < 0) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700615 printk(KERN_ERR "asoc: machine hw_params failed\n");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100616 goto out;
617 }
618 }
619
620 if (codec_dai->driver->ops->hw_params) {
Helen Zeng5749b092012-06-10 11:50:29 -0700621 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
622 ret = codec_dai->driver->ops->hw_params(substream,
623 params, codec_dai);
624 if (ret < 0) {
625 printk(KERN_ERR "not set codec %s hw params\n",
626 codec_dai->name);
627 goto codec_err;
628 }
629 } else {
630 if (codec_dai->capture_active == 1) {
631 ret = codec_dai->driver->ops->hw_params(
632 substream, params, codec_dai);
633 if (ret < 0) {
634 printk(KERN_ERR "fail: %s hw params\n",
635 codec_dai->name);
636 goto codec_err;
637 }
638 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100639 }
640 }
641
642 if (cpu_dai->driver->ops->hw_params) {
643 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
644 if (ret < 0) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700645 printk(KERN_ERR "asoc: interface %s hw params failed\n",
646 cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100647 goto interface_err;
648 }
649 }
650
651 if (platform->driver->ops && platform->driver->ops->hw_params) {
652 ret = platform->driver->ops->hw_params(substream, params);
653 if (ret < 0) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700654 printk(KERN_ERR "asoc: platform %s hw params failed\n",
655 platform->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100656 goto platform_err;
657 }
658 }
659
Dong Aisheng17841022011-08-29 17:15:14 +0800660 /* store the rate for each DAIs */
661 cpu_dai->rate = params_rate(params);
662 codec_dai->rate = params_rate(params);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100663
Steve Mucklef132c6c2012-06-06 18:30:57 -0700664 /* malloc a page for hostless IO.
665 * FIXME: rework with alsa-lib changes so that this malloc is not required.
666 */
667 if (rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST) {
668 substream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV;
669 substream->dma_buffer.dev.dev = rtd->dev;
670 substream->dma_buffer.dev.dev->coherent_dma_mask = DMA_BIT_MASK(32);
671 substream->dma_buffer.private_data = NULL;
672
673 ret = snd_pcm_lib_malloc_pages(substream, PAGE_SIZE);
674 if (ret < 0)
675 goto platform_err;
676 }
677
Liam Girdwoodddee6272011-06-09 14:45:53 +0100678out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100679 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100680 return ret;
681
682platform_err:
683 if (cpu_dai->driver->ops->hw_free)
684 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
685
686interface_err:
687 if (codec_dai->driver->ops->hw_free)
688 codec_dai->driver->ops->hw_free(substream, codec_dai);
689
690codec_err:
691 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
692 rtd->dai_link->ops->hw_free(substream);
693
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100694 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100695 return ret;
696}
697
698/*
699 * Frees resources allocated by hw_params, can be called multiple times
700 */
701static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
702{
703 struct snd_soc_pcm_runtime *rtd = substream->private_data;
704 struct snd_soc_platform *platform = rtd->platform;
705 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
706 struct snd_soc_dai *codec_dai = rtd->codec_dai;
707 struct snd_soc_codec *codec = rtd->codec;
708
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100709 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100710
711 /* apply codec digital mute */
712 if (!codec->active)
713 snd_soc_dai_digital_mute(codec_dai, 1);
714
715 /* free any machine hw params */
716 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
717 rtd->dai_link->ops->hw_free(substream);
718
719 /* free any DMA resources */
720 if (platform->driver->ops && platform->driver->ops->hw_free)
721 platform->driver->ops->hw_free(substream);
722
723 /* now free hw params for the DAIs */
724 if (codec_dai->driver->ops->hw_free)
725 codec_dai->driver->ops->hw_free(substream, codec_dai);
726
727 if (cpu_dai->driver->ops->hw_free)
728 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
729
Steve Mucklef132c6c2012-06-06 18:30:57 -0700730 if (rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST)
731 snd_pcm_lib_free_pages(substream);
732
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100733 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100734 return 0;
735}
736
737static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
738{
739 struct snd_soc_pcm_runtime *rtd = substream->private_data;
740 struct snd_soc_platform *platform = rtd->platform;
741 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
742 struct snd_soc_dai *codec_dai = rtd->codec_dai;
743 int ret;
744
745 if (codec_dai->driver->ops->trigger) {
Helen Zeng5749b092012-06-10 11:50:29 -0700746 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
747 ret = codec_dai->driver->ops->trigger(substream,
748 cmd, codec_dai);
749 if (ret < 0)
750 return ret;
751 } else {
752 if (codec_dai->capture_active == 1) {
753 ret = codec_dai->driver->ops->trigger(
754 substream, cmd, codec_dai);
755 if (ret < 0)
756 return ret;
757 }
758 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100759 }
760
761 if (platform->driver->ops && platform->driver->ops->trigger) {
762 ret = platform->driver->ops->trigger(substream, cmd);
763 if (ret < 0)
764 return ret;
765 }
766
767 if (cpu_dai->driver->ops->trigger) {
768 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
769 if (ret < 0)
770 return ret;
771 }
772 return 0;
773}
774
Steve Mucklef132c6c2012-06-06 18:30:57 -0700775int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream, int cmd)
776{
777 struct snd_soc_pcm_runtime *rtd = substream->private_data;
778 struct snd_soc_platform *platform = rtd->platform;
779 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
780 struct snd_soc_dai *codec_dai = rtd->codec_dai;
781 int ret;
782
783 if (codec_dai->driver->ops->bespoke_trigger) {
784 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
785 if (ret < 0)
786 return ret;
787 }
788
789 if (platform->driver->bespoke_trigger) {
790 ret = platform->driver->bespoke_trigger(substream, cmd);
791 if (ret < 0)
792 return ret;
793 }
794
795 if (cpu_dai->driver->ops->bespoke_trigger) {
796 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
797 if (ret < 0)
798 return ret;
799 }
800 return 0;
801}
802
Liam Girdwoodddee6272011-06-09 14:45:53 +0100803/*
804 * soc level wrapper for pointer callback
805 * If cpu_dai, codec_dai, platform driver has the delay callback, than
806 * the runtime->delay will be updated accordingly.
807 */
808static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
809{
810 struct snd_soc_pcm_runtime *rtd = substream->private_data;
811 struct snd_soc_platform *platform = rtd->platform;
812 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
813 struct snd_soc_dai *codec_dai = rtd->codec_dai;
814 struct snd_pcm_runtime *runtime = substream->runtime;
815 snd_pcm_uframes_t offset = 0;
816 snd_pcm_sframes_t delay = 0;
817
818 if (platform->driver->ops && platform->driver->ops->pointer)
819 offset = platform->driver->ops->pointer(substream);
820
821 if (cpu_dai->driver->ops->delay)
822 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
823
824 if (codec_dai->driver->ops->delay)
825 delay += codec_dai->driver->ops->delay(substream, codec_dai);
826
827 if (platform->driver->delay)
828 delay += platform->driver->delay(substream, codec_dai);
829
830 runtime->delay = delay;
831
832 return offset;
833}
834
Steve Mucklef132c6c2012-06-06 18:30:57 -0700835static inline int be_connect(struct snd_soc_pcm_runtime *fe,
836 struct snd_soc_pcm_runtime *be, int stream)
837{
838 struct snd_soc_dpcm_params *dpcm_params;
839
840 if (!fe->dpcm[stream].runtime) {
841 dev_err(fe->dev, "%s no runtime\n", fe->dai_link->name);
842 return -ENODEV;
843 }
844
845 /* only add new dpcm_paramss */
846 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
847 if (dpcm_params->be == be && dpcm_params->fe == fe)
848 return 0;
849 }
850
851 dpcm_params = kzalloc(sizeof(struct snd_soc_dpcm_params), GFP_KERNEL);
852 if (!dpcm_params)
853 return -ENOMEM;
854
855 dpcm_params->be = be;
856 dpcm_params->fe = fe;
857 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
858 dpcm_params->state = SND_SOC_DPCM_LINK_STATE_NEW;
859 list_add(&dpcm_params->list_be, &fe->dpcm[stream].be_clients);
860 list_add(&dpcm_params->list_fe, &be->dpcm[stream].fe_clients);
861
862 dev_dbg(fe->dev, " connected new DSP %s path %s %s %s\n",
863 stream ? "capture" : "playback", fe->dai_link->name,
864 stream ? "<-" : "->", be->dai_link->name);
865
866#ifdef CONFIG_DEBUG_FS
867 dpcm_params->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
868 fe->debugfs_dpcm_root, &dpcm_params->state);
869#endif
870
871 return 1;
872}
873
874static inline void be_reparent(struct snd_soc_pcm_runtime *fe,
875 struct snd_soc_pcm_runtime *be, int stream)
876{
877 struct snd_soc_dpcm_params *dpcm_params;
878 struct snd_pcm_substream *fe_substream, *be_substream;
879
880 /* reparent if BE is connected to other FEs */
881 if (!be->dpcm[stream].users)
882 return;
883
884 be_substream = snd_soc_dpcm_get_substream(be, stream);
885
886 list_for_each_entry(dpcm_params, &be->dpcm[stream].fe_clients, list_fe) {
887 if (dpcm_params->fe != fe) {
888
889 dev_dbg(fe->dev, " reparent %s path %s %s %s\n",
890 stream ? "capture" : "playback",
891 dpcm_params->fe->dai_link->name,
892 stream ? "<-" : "->", dpcm_params->be->dai_link->name);
893
894 fe_substream = snd_soc_dpcm_get_substream(dpcm_params->fe,
895 stream);
896 be_substream->runtime = fe_substream->runtime;
897 break;
898 }
899 }
900}
901
902static inline void be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
903{
904 struct snd_soc_dpcm_params *dpcm_params, *d;
905
906 list_for_each_entry_safe(dpcm_params, d, &fe->dpcm[stream].be_clients, list_be) {
907 dev_dbg(fe->dev, "BE %s disconnect check for %s\n",
908 stream ? "capture" : "playback",
909 dpcm_params->be->dai_link->name);
910
911 if (dpcm_params->state == SND_SOC_DPCM_LINK_STATE_FREE) {
912 dev_dbg(fe->dev, " freed DSP %s path %s %s %s\n",
913 stream ? "capture" : "playback", fe->dai_link->name,
914 stream ? "<-" : "->", dpcm_params->be->dai_link->name);
915
916 /* BEs still alive need new FE */
917 be_reparent(fe, dpcm_params->be, stream);
918
919#ifdef CONFIG_DEBUG_FS
920 debugfs_remove(dpcm_params->debugfs_state);
921#endif
922
923 list_del(&dpcm_params->list_be);
924 list_del(&dpcm_params->list_fe);
925 kfree(dpcm_params);
926 }
927 }
928}
929
930static struct snd_soc_pcm_runtime *be_get_rtd(struct snd_soc_card *card,
931 struct snd_soc_dapm_widget *widget)
932{
933 struct snd_soc_pcm_runtime *be;
934 int i;
935
936 if (!widget->sname) {
937 dev_err(card->dev, "widget %s has no stream\n", widget->name);
938 return NULL;
939 }
940
941 for (i = 0; i < card->num_links; i++) {
942 be = &card->rtd[i];
943
944 if (!strcmp(widget->sname, be->dai_link->stream_name))
945 return be;
946 }
947
948 dev_err(card->dev, "can't get BE for %s\n", widget->name);
949 return NULL;
950}
951
952static struct snd_soc_dapm_widget *be_get_widget(struct snd_soc_card *card,
953 struct snd_soc_pcm_runtime *rtd)
954{
955 struct snd_soc_dapm_widget *widget;
956
957 list_for_each_entry(widget, &card->widgets, list) {
958
959 if (!widget->sname)
960 continue;
961
962 if (!strcmp(widget->sname, rtd->dai_link->stream_name))
963 return widget;
964 }
965
966 dev_err(card->dev, "can't get widget for %s\n",
967 rtd->dai_link->stream_name);
968 return NULL;
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
984static int fe_path_get(struct snd_soc_pcm_runtime *fe,
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
999 dev_dbg(fe->dev, "found %d audio %s paths\n", paths,
1000 stream ? "capture" : "playback");
1001
1002 *list_ = list;
1003 return paths;
1004}
1005
1006static inline void fe_path_put(struct snd_soc_dapm_widget_list **list)
1007{
1008 kfree(*list);
1009}
1010
1011static int be_prune_old(struct snd_soc_pcm_runtime *fe, int stream,
1012 struct snd_soc_dapm_widget_list **list_)
1013{
1014 struct snd_soc_card *card = fe->card;
1015 struct snd_soc_dpcm_params *dpcm_params;
1016 struct snd_soc_dapm_widget_list *list = *list_;
1017 struct snd_soc_dapm_widget *widget;
1018 int old = 0;
1019
1020 /* Destroy any old FE <--> BE connections */
1021 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1022
1023 /* is there a valid widget for this BE */
1024 widget = be_get_widget(card, dpcm_params->be);
1025 if (!widget) {
1026 dev_err(fe->dev, "no widget found for %s\n",
1027 dpcm_params->be->dai_link->name);
1028 continue;
1029 }
1030
1031 /* prune the BE if it's no longer in our active list */
1032 if (widget_in_list(list, widget))
1033 continue;
1034
1035 dev_dbg(fe->dev, "pruning %s BE %s for %s\n",
1036 stream ? "capture" : "playback", dpcm_params->be->dai_link->name,
1037 fe->dai_link->name);
1038 dpcm_params->state = SND_SOC_DPCM_LINK_STATE_FREE;
1039 dpcm_params->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1040 old++;
1041 }
1042
1043 dev_dbg(fe->dev, "found %d old BEs\n", old);
1044 return old;
1045}
1046
1047static int be_add_new(struct snd_soc_pcm_runtime *fe, int stream,
1048 struct snd_soc_dapm_widget_list **list_)
1049{
1050 struct snd_soc_card *card = fe->card;
1051 struct snd_soc_dapm_widget_list *list = *list_;
1052 enum snd_soc_dapm_type be_type;
1053 int i, new = 0, err;
1054
1055 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1056 be_type = snd_soc_dapm_aif_out;
1057 else
1058 be_type = snd_soc_dapm_aif_in;
1059
1060 /* Create any new FE <--> BE connections */
1061 for (i = 0; i < list->num_widgets; i++) {
1062
1063 if (list->widgets[i]->id == be_type) {
1064 struct snd_soc_pcm_runtime *be;
1065
1066 /* is there a valid BE rtd for this widget */
1067 be = be_get_rtd(card, list->widgets[i]);
1068 if (!be) {
1069 dev_err(fe->dev, "no BE found for %s\n",
1070 list->widgets[i]->name);
1071 continue;
1072 }
1073
1074 /* don't connect if FE is not running */
1075 if (!fe->dpcm[stream].runtime)
1076 continue;
1077
1078 /* newly connected FE and BE */
1079 err = be_connect(fe, be, stream);
1080 if (err < 0) {
1081 dev_err(fe->dev, "can't connect %s\n", list->widgets[i]->name);
1082 break;
1083 } else if (err == 0) /* already connected */
1084 continue;
1085
1086 /* new */
1087 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1088 new++;
1089 }
1090 }
1091
1092 dev_dbg(fe->dev, "found %d new BEs\n", new);
1093 return new;
1094}
1095
1096/*
1097 * Find the corresponding BE DAIs that source or sink audio to this
1098 * FE substream.
1099 */
1100static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1101 int stream, struct snd_soc_dapm_widget_list **list, int new)
1102{
1103 if (new)
1104 return be_add_new(fe, stream, list);
1105 else
1106 return be_prune_old(fe, stream, list);
1107 return 0;
1108}
1109
1110/*
1111 * Clear the runtime pending state of all BE's.
1112 */
1113static void fe_clear_pending(struct snd_soc_pcm_runtime *fe, int stream)
1114{
1115 struct snd_soc_dpcm_params *dpcm_params;
1116
1117 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be)
1118 dpcm_params->be->dpcm[stream].runtime_update =
1119 SND_SOC_DPCM_UPDATE_NO;
1120}
1121
1122/* Unwind the BE startup */
1123static void soc_dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe, int stream)
1124{
1125 struct snd_soc_dpcm_params *dpcm_params;
1126
1127 /* disable any enabled and non active backends */
1128 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1129
1130 struct snd_soc_pcm_runtime *be = dpcm_params->be;
1131 struct snd_pcm_substream *be_substream =
1132 snd_soc_dpcm_get_substream(be, stream);
1133
1134 if (be->dpcm[stream].users == 0)
1135 dev_err(be->dev, "no users %s at close - state %d\n",
1136 stream ? "capture" : "playback", be->dpcm[stream].state);
1137
1138 if (--be->dpcm[stream].users != 0)
1139 continue;
1140
1141 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1142 continue;
1143
1144 soc_pcm_close(be_substream);
1145 be_substream->runtime = NULL;
1146
1147 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1148 }
1149}
1150
1151/* Startup all new BE */
1152static int soc_dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1153{
1154 struct snd_soc_dpcm_params *dpcm_params;
1155 int err, count = 0;
1156
1157 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1158 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1159
1160 struct snd_soc_pcm_runtime *be = dpcm_params->be;
1161 struct snd_pcm_substream *be_substream =
1162 snd_soc_dpcm_get_substream(be, stream);
1163
1164 /* is this op for this BE ? */
1165 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1166 continue;
1167
1168 /* first time the dpcm_params is open ? */
1169 if (be->dpcm[stream].users == MAX_BE_USERS)
1170 dev_err(be->dev, "too many users %s at open - state %d\n",
1171 stream ? "capture" : "playback", be->dpcm[stream].state);
1172
1173 if (be->dpcm[stream].users++ != 0)
1174 continue;
1175
1176 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1177 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1178 continue;
1179
1180 dev_dbg(be->dev, "dpcm: open BE %s\n", be->dai_link->name);
1181
1182 be_substream->runtime = be->dpcm[stream].runtime;
1183 err = soc_pcm_open(be_substream);
1184 if (err < 0) {
1185 dev_err(be->dev, "BE open failed %d\n", err);
1186 be->dpcm[stream].users--;
1187 if (be->dpcm[stream].users < 0)
1188 dev_err(be->dev, "no users %s at unwind - state %d\n",
1189 stream ? "capture" : "playback",
1190 be->dpcm[stream].state);
1191
1192 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1193 goto unwind;
1194 }
1195
1196 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1197 count++;
1198 }
1199
1200 return count;
1201
1202unwind:
1203 /* disable any enabled and non active backends */
1204 list_for_each_entry_continue_reverse(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1205 struct snd_soc_pcm_runtime *be = dpcm_params->be;
1206 struct snd_pcm_substream *be_substream =
1207 snd_soc_dpcm_get_substream(be, stream);
1208
1209 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1210 continue;
1211
1212 if (be->dpcm[stream].users == 0)
1213 dev_err(be->dev, "no users %s at close - state %d\n",
1214 stream ? "capture" : "playback", be->dpcm[stream].state);
1215
1216 if (--be->dpcm[stream].users != 0)
1217 continue;
1218
1219 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1220 continue;
1221
1222 soc_pcm_close(be_substream);
1223 be_substream->runtime = NULL;
1224
1225 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1226 }
1227
1228 return err;
1229}
1230
1231void soc_dpcm_set_dynamic_runtime(struct snd_pcm_substream *substream)
1232{
1233 struct snd_pcm_runtime *runtime = substream->runtime;
1234 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1235 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1236 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1237
1238 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1239 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1240 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1241 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1242 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1243 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1244 runtime->hw.rates = cpu_dai_drv->playback.rates;
1245 } else {
1246 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1247 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1248 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1249 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1250 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1251 runtime->hw.rates = cpu_dai_drv->capture.rates;
1252 }
1253}
1254
1255static int soc_dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1256{
1257 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1258 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1259 int stream = fe_substream->stream, ret = 0;
1260
1261 mutex_lock(&fe->card->dpcm_mutex);
1262 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1263
1264 ret = soc_dpcm_be_dai_startup(fe, fe_substream->stream);
1265 if (ret < 0) {
1266 dev_err(fe->dev,"dpcm: failed to start some BEs %d\n", ret);
1267 goto be_err;
1268 }
1269
1270 dev_dbg(fe->dev, "dpcm: open FE %s\n", fe->dai_link->name);
1271
1272 /* start the DAI frontend */
1273 ret = soc_pcm_open(fe_substream);
1274 if (ret < 0) {
1275 dev_err(fe->dev,"dpcm: failed to start FE %d\n", ret);
1276 goto unwind;
1277 }
1278
1279 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1280
1281 soc_dpcm_set_dynamic_runtime(fe_substream);
1282 snd_pcm_limit_hw_rates(runtime);
1283
1284 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1285 mutex_unlock(&fe->card->dpcm_mutex);
1286 return 0;
1287
1288unwind:
1289 soc_dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1290be_err:
1291 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1292 mutex_unlock(&fe->card->dpcm_mutex);
1293 return ret;
1294}
1295
1296/* BE shutdown - called on DAPM sync updates (i.e. FE is already running)*/
1297static int soc_dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1298{
1299 struct snd_soc_dpcm_params *dpcm_params;
1300
1301 /* only shutdown backends that are either sinks or sources to this frontend DAI */
1302 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1303
1304 struct snd_soc_pcm_runtime *be = dpcm_params->be;
1305 struct snd_pcm_substream *be_substream =
1306 snd_soc_dpcm_get_substream(be, stream);
1307
1308 /* is this op for this BE ? */
1309 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1310 continue;
1311
1312 if (be->dpcm[stream].users == 0)
1313 dev_err(be->dev, "no users %s at close - state %d\n",
1314 stream ? "capture" : "playback", be->dpcm[stream].state);
1315
1316 if (--be->dpcm[stream].users != 0)
1317 continue;
1318
1319 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1320 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1321 continue;
1322
1323 dev_dbg(be->dev, "dpcm: close BE %s\n",
1324 dpcm_params->fe->dai_link->name);
1325
1326 soc_pcm_close(be_substream);
1327 be_substream->runtime = NULL;
1328
1329 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1330 }
1331 return 0;
1332}
1333
1334/* FE +BE shutdown - called on FE PCM ops */
1335static int soc_dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1336{
1337 struct snd_soc_pcm_runtime *fe = substream->private_data;
1338 int stream = substream->stream;
1339
1340 mutex_lock(&fe->card->dpcm_mutex);
1341 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1342
1343 dev_dbg(fe->dev, "dpcm: close FE %s\n", fe->dai_link->name);
1344
1345 /* now shutdown the frontend */
1346 soc_pcm_close(substream);
1347
1348 /* shutdown the BEs */
1349 soc_dpcm_be_dai_shutdown(fe, substream->stream);
1350 /* run the stream event for each BE */
1351 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1352 soc_dpcm_dapm_stream_event(fe, stream,
1353 fe->cpu_dai->driver->playback.stream_name,
1354 SND_SOC_DAPM_STREAM_STOP);
1355 else
1356 soc_dpcm_dapm_stream_event(fe, stream,
1357 fe->cpu_dai->driver->capture.stream_name,
1358 SND_SOC_DAPM_STREAM_STOP);
1359
1360 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1361 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1362
1363 mutex_unlock(&fe->card->dpcm_mutex);
1364 return 0;
1365}
1366
1367static int soc_dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1368{
1369 struct snd_soc_dpcm_params *dpcm_params;
1370 int ret;
1371
1372 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1373
1374 struct snd_soc_pcm_runtime *be = dpcm_params->be;
1375 struct snd_pcm_substream *be_substream =
1376 snd_soc_dpcm_get_substream(be, stream);
1377
1378 /* is this op for this BE ? */
1379 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1380 continue;
1381
1382 /* only allow hw_params() if no connected FEs are running */
1383 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1384 continue;
1385
1386 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1387 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1388 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1389 continue;
1390
1391 dev_dbg(be->dev, "dpcm: hw_params BE %s\n",
1392 dpcm_params->fe->dai_link->name);
1393
1394 /* copy params for each dpcm_params */
1395 memcpy(&dpcm_params->hw_params, &fe->dpcm[stream].hw_params,
1396 sizeof(struct snd_pcm_hw_params));
1397
1398 /* perform any hw_params fixups */
1399 if (be->dai_link->be_hw_params_fixup) {
1400 ret = be->dai_link->be_hw_params_fixup(be,
1401 &dpcm_params->hw_params);
1402 if (ret < 0) {
1403 dev_err(be->dev,
1404 "dpcm: hw_params BE fixup failed %d\n",
1405 ret);
1406 goto unwind;
1407 }
1408 }
1409
1410 ret = soc_pcm_hw_params(be_substream, &dpcm_params->hw_params);
1411 if (ret < 0) {
1412 dev_err(dpcm_params->be->dev, "dpcm: hw_params BE failed %d\n", ret);
1413 goto unwind;
1414 }
1415
1416 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1417 }
1418 return 0;
1419
1420unwind:
1421 /* disable any enabled and non active backends */
1422 list_for_each_entry_continue_reverse(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1423 struct snd_soc_pcm_runtime *be = dpcm_params->be;
1424 struct snd_pcm_substream *be_substream =
1425 snd_soc_dpcm_get_substream(be, stream);
1426
1427 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1428 continue;
1429
1430 /* only allow hw_free() if no connected FEs are running */
1431 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1432 continue;
1433
1434 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1435 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1436 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1437 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1438 continue;
1439
1440 soc_pcm_hw_free(be_substream);
1441 }
1442
1443 return ret;
1444}
1445
1446int soc_dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1447 struct snd_pcm_hw_params *params)
1448{
1449 struct snd_soc_pcm_runtime *fe = substream->private_data;
1450 int ret, stream = substream->stream;
1451
1452 mutex_lock(&fe->card->dpcm_mutex);
1453 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1454
1455 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1456 sizeof(struct snd_pcm_hw_params));
1457 ret = soc_dpcm_be_dai_hw_params(fe, substream->stream);
1458 if (ret < 0) {
1459 dev_err(fe->dev,"dpcm: hw_params failed for some BEs %d\n", ret);
1460 goto out;
1461 }
1462
1463 dev_dbg(fe->dev, "dpcm: hw_params FE %s rate %d chan %x fmt %d\n",
1464 fe->dai_link->name, params_rate(params), params_channels(params),
1465 params_format(params));
1466
1467 /* call hw_params on the frontend */
1468 ret = soc_pcm_hw_params(substream, params);
1469 if (ret < 0) {
1470 dev_err(fe->dev,"dpcm: hw_params FE failed %d\n", ret);
1471 soc_dpcm_be_dai_hw_free(fe, stream);
1472 } else
1473 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1474
1475out:
1476 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1477 mutex_unlock(&fe->card->dpcm_mutex);
1478 return ret;
1479}
1480
1481static int dpcm_do_trigger(struct snd_soc_dpcm_params *dpcm_params,
1482 struct snd_pcm_substream *substream, int cmd)
1483{
1484 int ret;
1485
1486 dev_dbg(dpcm_params->be->dev, "dpcm: trigger BE %s cmd %d\n",
1487 dpcm_params->fe->dai_link->name, cmd);
1488
1489 ret = soc_pcm_trigger(substream, cmd);
1490 if (ret < 0)
1491 dev_err(dpcm_params->be->dev,"dpcm: trigger BE failed %d\n", ret);
1492
1493 return ret;
1494}
1495
1496int soc_dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, int cmd)
1497{
1498 struct snd_soc_dpcm_params *dpcm_params;
1499 int ret = 0;
1500
1501 if ((cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE) ||
1502 (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH))
1503 return ret;
1504
1505 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1506
1507 struct snd_soc_pcm_runtime *be = dpcm_params->be;
1508 struct snd_pcm_substream *be_substream =
1509 snd_soc_dpcm_get_substream(be, stream);
1510
1511 /* is this op for this BE ? */
1512 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1513 continue;
1514
1515 switch (cmd) {
1516 case SNDRV_PCM_TRIGGER_START:
1517 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1518 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1519 continue;
1520
1521 ret = dpcm_do_trigger(dpcm_params, be_substream, cmd);
1522 if (ret)
1523 return ret;
1524
1525 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1526 break;
1527 case SNDRV_PCM_TRIGGER_RESUME:
1528 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1529 continue;
1530
1531 ret = dpcm_do_trigger(dpcm_params, be_substream, cmd);
1532 if (ret)
1533 return ret;
1534
1535 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1536 break;
1537 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1538 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1539 continue;
1540
1541 ret = dpcm_do_trigger(dpcm_params, be_substream, cmd);
1542 if (ret)
1543 return ret;
1544
1545 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1546 break;
1547 case SNDRV_PCM_TRIGGER_STOP:
1548 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1549 continue;
1550
1551 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1552 continue;
1553
1554 ret = dpcm_do_trigger(dpcm_params, be_substream, cmd);
1555 if (ret)
1556 return ret;
1557
1558 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1559 break;
1560 case SNDRV_PCM_TRIGGER_SUSPEND:
1561 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1562 continue;
1563
1564 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1565 continue;
1566
1567 ret = dpcm_do_trigger(dpcm_params, be_substream, cmd);
1568 if (ret)
1569 return ret;
1570
1571 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1572 break;
1573 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1574 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1575 continue;
1576
1577 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1578 continue;
1579
1580 ret = dpcm_do_trigger(dpcm_params, be_substream, cmd);
1581 if (ret)
1582 return ret;
1583
1584 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1585 break;
1586 }
1587 }
1588
1589 return ret;
1590}
1591EXPORT_SYMBOL_GPL(soc_dpcm_be_dai_trigger);
1592
1593int soc_dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
1594{
1595 struct snd_soc_pcm_runtime *fe = substream->private_data;
1596 int stream = substream->stream, ret;
1597 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1598
1599 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1600
1601 switch (trigger) {
1602 case SND_SOC_DPCM_TRIGGER_PRE:
1603 /* call trigger on the frontend before the backend. */
1604
1605 dev_dbg(fe->dev, "dpcm: pre trigger FE %s cmd %d\n",
1606 fe->dai_link->name, cmd);
1607
1608 ret = soc_pcm_trigger(substream, cmd);
1609 if (ret < 0) {
1610 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
1611 goto out;
1612 }
1613
1614 ret = soc_dpcm_be_dai_trigger(fe, substream->stream, cmd);
1615 break;
1616 case SND_SOC_DPCM_TRIGGER_POST:
1617 /* call trigger on the frontend after the backend. */
1618
1619 ret = soc_dpcm_be_dai_trigger(fe, substream->stream, cmd);
1620 if (ret < 0) {
1621 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
1622 goto out;
1623 }
1624
1625 dev_dbg(fe->dev, "dpcm: post trigger FE %s cmd %d\n",
1626 fe->dai_link->name, cmd);
1627
1628 ret = soc_pcm_trigger(substream, cmd);
1629 break;
1630 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1631 /* bespoke trigger() - handles both FE and BEs */
1632
1633 dev_dbg(fe->dev, "dpcm: bespoke trigger FE %s cmd %d\n",
1634 fe->dai_link->name, cmd);
1635
1636 ret = soc_pcm_bespoke_trigger(substream, cmd);
1637 if (ret < 0) {
1638 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
1639 goto out;
1640 }
1641 break;
1642 default:
1643 dev_err(fe->dev, "dpcm: invalid trigger cmd %d for %s\n", cmd,
1644 fe->dai_link->name);
1645 ret = -EINVAL;
1646 goto out;
1647 }
1648
1649 switch (cmd) {
1650 case SNDRV_PCM_TRIGGER_START:
1651 case SNDRV_PCM_TRIGGER_RESUME:
1652 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1653 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1654 break;
1655 case SNDRV_PCM_TRIGGER_STOP:
1656 case SNDRV_PCM_TRIGGER_SUSPEND:
1657 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1658 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1659 break;
1660 }
1661
1662out:
1663 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1664 return ret;
1665}
1666
1667static int soc_dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1668{
1669 struct snd_soc_dpcm_params *dpcm_params;
1670 int ret = 0;
1671
1672 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1673
1674 struct snd_soc_pcm_runtime *be = dpcm_params->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 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1683 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1684 continue;
1685
1686 dev_dbg(be->dev, "dpcm: prepare BE %s\n",
1687 dpcm_params->fe->dai_link->name);
1688
1689 ret = soc_pcm_prepare(be_substream);
1690 if (ret < 0) {
1691 dev_err(be->dev, "dpcm: backend prepare failed %d\n",
1692 ret);
1693 break;
1694 }
1695
1696 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1697 }
1698 return ret;
1699}
1700
1701int soc_dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
1702{
1703 struct snd_soc_pcm_runtime *fe = substream->private_data;
1704 int stream = substream->stream, ret = 0;
1705
1706 mutex_lock(&fe->card->dpcm_mutex);
1707
1708 dev_dbg(fe->dev, "dpcm: prepare FE %s\n", fe->dai_link->name);
1709
1710 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1711
1712 /* there is no point preparing this FE if there are no BEs */
1713 if (list_empty(&fe->dpcm[stream].be_clients)) {
1714 dev_err(fe->dev, "dpcm: no backend DAIs enabled for %s\n",
1715 fe->dai_link->name);
1716 ret = -EINVAL;
1717 goto out;
1718 }
1719
1720 ret = soc_dpcm_be_dai_prepare(fe, substream->stream);
1721 if (ret < 0)
1722 goto out;
1723
1724 /* call prepare on the frontend */
1725 ret = soc_pcm_prepare(substream);
1726 if (ret < 0) {
1727 dev_err(fe->dev,"dpcm: prepare FE %s failed\n", fe->dai_link->name);
1728 goto out;
1729 }
1730
1731 /* run the stream event for each BE */
1732 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1733 soc_dpcm_dapm_stream_event(fe, stream,
1734 fe->cpu_dai->driver->playback.stream_name,
1735 SND_SOC_DAPM_STREAM_START);
1736 else
1737 soc_dpcm_dapm_stream_event(fe, stream,
1738 fe->cpu_dai->driver->capture.stream_name,
1739 SND_SOC_DAPM_STREAM_START);
1740
1741 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1742
1743out:
1744 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1745 mutex_unlock(&fe->card->dpcm_mutex);
1746 return ret;
1747}
1748
1749static int soc_dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1750{
1751 struct snd_soc_dpcm_params *dpcm_params;
1752
1753 /* only hw_params backends that are either sinks or sources
1754 * to this frontend DAI */
1755 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1756
1757 struct snd_soc_pcm_runtime *be = dpcm_params->be;
1758 struct snd_pcm_substream *be_substream =
1759 snd_soc_dpcm_get_substream(be, stream);
1760
1761 /* is this op for this BE ? */
1762 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1763 continue;
1764
1765 /* only free hw when no longer used - check all FEs */
1766 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1767 continue;
1768
1769 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1770 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1771 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1772 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1773 continue;
1774
1775 dev_dbg(be->dev, "dpcm: hw_free BE %s\n",
1776 dpcm_params->fe->dai_link->name);
1777
1778 soc_pcm_hw_free(be_substream);
1779
1780 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1781 }
1782
1783 return 0;
1784}
1785
1786int soc_dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1787{
1788 struct snd_soc_pcm_runtime *fe = substream->private_data;
1789 int err, stream = substream->stream;
1790
1791 mutex_lock(&fe->card->dpcm_mutex);
1792 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1793
1794 dev_dbg(fe->dev, "dpcm: hw_free FE %s\n", fe->dai_link->name);
1795
1796 /* call hw_free on the frontend */
1797 err = soc_pcm_hw_free(substream);
1798 if (err < 0)
1799 dev_err(fe->dev,"dpcm: hw_free FE %s failed\n", fe->dai_link->name);
1800
1801 /* only hw_params backends that are either sinks or sources
1802 * to this frontend DAI */
1803 err = soc_dpcm_be_dai_hw_free(fe, stream);
1804
1805 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1806 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1807
1808 mutex_unlock(&fe->card->dpcm_mutex);
1809 return 0;
1810}
1811
1812static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1813 unsigned int cmd, void *arg)
1814{
1815 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1816 struct snd_soc_platform *platform = rtd->platform;
1817
1818 if (platform->driver->ops->ioctl)
1819 return platform->driver->ops->ioctl(substream, cmd, arg);
1820 return snd_pcm_lib_ioctl(substream, cmd, arg);
1821}
1822
1823static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1824{
1825 struct snd_pcm_substream *substream = snd_soc_dpcm_get_substream(fe, stream);
1826 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1827 int err;
1828
1829 dev_dbg(fe->dev, "runtime %s close on FE %s\n",
1830 stream ? "capture" : "playback", fe->dai_link->name);
1831
1832 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1833 /* call bespoke trigger - FE takes care of all BE triggers */
1834 dev_dbg(fe->dev, "dpcm: bespoke trigger FE %s cmd stop\n",
1835 fe->dai_link->name);
1836
1837 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1838 if (err < 0)
1839 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", err);
1840 } else {
1841 dev_dbg(fe->dev, "dpcm: trigger FE %s cmd stop\n",
1842 fe->dai_link->name);
1843
1844 err = soc_dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1845 if (err < 0)
1846 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", err);
1847 }
1848
1849 err = soc_dpcm_be_dai_hw_free(fe, stream);
1850 if (err < 0)
1851 dev_err(fe->dev,"dpcm: hw_free FE failed %d\n", err);
1852
1853 err = soc_dpcm_be_dai_shutdown(fe, stream);
1854 if (err < 0)
1855 dev_err(fe->dev,"dpcm: shutdown FE failed %d\n", err);
1856
1857 /* run the stream event for each BE */
1858 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1859 soc_dpcm_dapm_stream_event(fe, stream,
1860 fe->cpu_dai->driver->playback.stream_name,
1861 SND_SOC_DAPM_STREAM_NOP);
1862 else
1863 soc_dpcm_dapm_stream_event(fe, stream,
1864 fe->cpu_dai->driver->capture.stream_name,
1865 SND_SOC_DAPM_STREAM_NOP);
1866
1867 return 0;
1868}
1869
1870static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1871{
1872 struct snd_pcm_substream *substream = snd_soc_dpcm_get_substream(fe, stream);
1873 struct snd_soc_dpcm_params *dpcm_params;
1874 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1875 int ret;
1876
1877 dev_dbg(fe->dev, "runtime %s open on FE %s\n",
1878 stream ? "capture" : "playback", fe->dai_link->name);
1879
1880 /* Only start the BE if the FE is ready */
1881 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1882 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1883 return -EINVAL;
1884
1885 /* startup must always be called for new BEs */
1886 ret = soc_dpcm_be_dai_startup(fe, stream);
1887 if (ret < 0) {
1888 goto disconnect;
1889 return ret;
1890 }
1891
1892 /* keep going if FE state is > open */
1893 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1894 return 0;
1895
1896 ret = soc_dpcm_be_dai_hw_params(fe, stream);
1897 if (ret < 0) {
1898 goto close;
1899 return ret;
1900 }
1901
1902 /* keep going if FE state is > hw_params */
1903 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1904 return 0;
1905
1906
1907 ret = soc_dpcm_be_dai_prepare(fe, stream);
1908 if (ret < 0) {
1909 goto hw_free;
1910 return ret;
1911 }
1912
1913 /* run the stream event for each BE */
1914 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1915 soc_dpcm_dapm_stream_event(fe, stream,
1916 fe->cpu_dai->driver->playback.stream_name,
1917 SND_SOC_DAPM_STREAM_NOP);
1918 else
1919 soc_dpcm_dapm_stream_event(fe, stream,
1920 fe->cpu_dai->driver->capture.stream_name,
1921 SND_SOC_DAPM_STREAM_NOP);
1922
1923 /* keep going if FE state is > prepare */
1924 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1925 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1926 return 0;
1927
1928 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1929 /* call trigger on the frontend - FE takes care of all BE triggers */
1930 dev_dbg(fe->dev, "dpcm: bespoke trigger FE %s cmd start\n",
1931 fe->dai_link->name);
1932
1933 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1934 if (ret < 0) {
1935 dev_err(fe->dev,"dpcm: bespoke trigger FE failed %d\n", ret);
1936 goto hw_free;
1937 }
1938 } else {
1939 dev_dbg(fe->dev, "dpcm: trigger FE %s cmd start\n",
1940 fe->dai_link->name);
1941
1942 ret = soc_dpcm_be_dai_trigger(fe, stream,
1943 SNDRV_PCM_TRIGGER_START);
1944 if (ret < 0) {
1945 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
1946 goto hw_free;
1947 }
1948 }
1949
1950 return 0;
1951
1952hw_free:
1953 soc_dpcm_be_dai_hw_free(fe, stream);
1954close:
1955 soc_dpcm_be_dai_shutdown(fe, stream);
1956disconnect:
1957 /* disconnect any non started BEs */
1958 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1959 struct snd_soc_pcm_runtime *be = dpcm_params->be;
1960 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1961 dpcm_params->state = SND_SOC_DPCM_LINK_STATE_FREE;
1962 }
1963
1964 return ret;
1965}
1966
1967static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1968{
1969 int ret;
1970
1971 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1972 ret = dpcm_run_update_startup(fe, stream);
1973 if (ret < 0)
1974 dev_err(fe->dev, "failed to startup some BEs\n");
1975 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1976
1977 return ret;
1978}
1979
1980static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1981{
1982 int ret;
1983
1984 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1985 ret = dpcm_run_update_shutdown(fe, stream);
1986 if (ret < 0)
1987 dev_err(fe->dev, "failed to shutdown some BEs\n");
1988 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1989
1990 return ret;
1991}
1992
1993/* called when any mixer updates change FE -> BE the stream */
1994int soc_dpcm_runtime_update(struct snd_soc_dapm_widget *widget)
1995{
1996 struct snd_soc_card *card;
1997 int i, ret = 0, old, new, paths;
1998
1999 if (widget->codec)
2000 card = widget->codec->card;
2001 else if (widget->platform)
2002 card = widget->platform->card;
2003 else
2004 return -EINVAL;
2005
2006 mutex_lock(&card->dpcm_mutex);
2007
2008 for (i = 0; i < card->num_rtd; i++) {
2009 struct snd_soc_dapm_widget_list *list;
2010 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2011
2012 /* make sure link is FE */
2013 if (!fe->dai_link->dynamic)
2014 continue;
2015
2016 /* only check active links */
2017 if (!fe->cpu_dai->active)
2018 continue;
2019
2020 /* DAPM sync will call this to update DSP paths */
2021 dev_dbg(fe->dev, "DPCM runtime update for FE %s\n", fe->dai_link->name);
2022
2023 /* skip if FE doesn't have playback capability */
2024 if (!fe->cpu_dai->driver->playback.channels_min)
2025 goto capture;
2026
2027 paths = fe_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2028 if (paths < 0) {
SathishKumar Manief4feb32012-10-09 13:31:13 -07002029 pr_warn_ratelimited("%s no valid %s route from source to sink\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -07002030 fe->dai_link->name, "playback");
Iliyan Malchev734dc212012-10-02 09:22:36 -07002031 WARN_ON(1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002032 ret = paths;
2033 goto out;
2034 }
2035
2036 /* update any new playback paths */
2037 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2038 if (new) {
2039 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2040 fe_clear_pending(fe, SNDRV_PCM_STREAM_PLAYBACK);
2041 be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2042 }
2043
2044 /* update any old playback paths */
2045 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2046 if (old) {
2047 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2048 fe_clear_pending(fe, SNDRV_PCM_STREAM_PLAYBACK);
2049 be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2050 }
2051
2052capture:
2053 /* skip if FE doesn't have capture capability */
2054 if (!fe->cpu_dai->driver->capture.channels_min)
2055 continue;
2056
2057 paths = fe_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2058 if (paths < 0) {
SathishKumar Manief4feb32012-10-09 13:31:13 -07002059 pr_warn_ratelimited("%s no valid %s route from source to sink\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -07002060 fe->dai_link->name, "capture");
2061 ret = paths;
2062 goto out;
2063 }
2064
2065 /* update any new capture paths */
2066 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2067 if (new) {
2068 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2069 fe_clear_pending(fe, SNDRV_PCM_STREAM_CAPTURE);
2070 be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2071 }
2072
2073 /* update any old capture paths */
2074 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2075 if (old) {
2076 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2077 fe_clear_pending(fe, SNDRV_PCM_STREAM_CAPTURE);
2078 be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2079 }
2080
2081 fe_path_put(&list);
2082 }
2083
2084out:
2085 mutex_unlock(&card->dpcm_mutex);
2086 return ret;
2087}
2088
2089int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2090{
2091 struct snd_soc_dpcm_params *dpcm_params;
2092
2093 list_for_each_entry(dpcm_params,
2094 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients, list_be) {
2095
2096 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2097 struct snd_soc_dai *dai = be->codec_dai;
2098 struct snd_soc_dai_driver *drv = dai->driver;
2099
2100 if (be->dai_link->ignore_suspend)
2101 continue;
2102
2103 dev_dbg(be->dev, "BE digital mute %s\n", be->dai_link->name);
2104
2105 if (drv->ops->digital_mute && dai->playback_active)
2106 drv->ops->digital_mute(dai, mute);
2107 }
2108
2109 return 0;
2110}
2111
2112int soc_dpcm_be_cpu_dai_suspend(struct snd_soc_pcm_runtime *fe)
2113{
2114 struct snd_soc_dpcm_params *dpcm_params;
2115
2116 /* suspend for playback */
2117 list_for_each_entry(dpcm_params,
2118 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients, list_be) {
2119
2120 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2121 struct snd_soc_dai *dai = be->cpu_dai;
2122 struct snd_soc_dai_driver *drv = dai->driver;
2123
2124 if (be->dai_link->ignore_suspend)
2125 continue;
2126
2127 dev_dbg(be->dev, "pm: BE CPU DAI playback suspend %s\n",
2128 be->dai_link->name);
2129
2130 if (drv->suspend && !drv->ac97_control)
2131 drv->suspend(dai);
2132 }
2133
2134 /* suspend for capture */
2135 list_for_each_entry(dpcm_params,
2136 &fe->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients, list_be) {
2137
2138 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2139 struct snd_soc_dai *dai = be->cpu_dai;
2140 struct snd_soc_dai_driver *drv = dai->driver;
2141
2142 if (be->dai_link->ignore_suspend)
2143 continue;
2144
2145 dev_dbg(be->dev, "pm: BE CPU DAI capture suspend %s\n",
2146 be->dai_link->name);
2147
2148 if (drv->suspend && !drv->ac97_control)
2149 drv->suspend(dai);
2150 }
2151
2152 return 0;
2153}
2154
2155int soc_dpcm_be_ac97_cpu_dai_suspend(struct snd_soc_pcm_runtime *fe)
2156{
2157 struct snd_soc_dpcm_params *dpcm_params;
2158
2159 /* suspend for playback */
2160 list_for_each_entry(dpcm_params,
2161 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients, list_be) {
2162
2163 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2164 struct snd_soc_dai *dai = be->cpu_dai;
2165 struct snd_soc_dai_driver *drv = dai->driver;
2166
2167 if (be->dai_link->ignore_suspend)
2168 continue;
2169
2170 dev_dbg(be->dev, "pm: BE CPU DAI playback suspend %s\n",
2171 be->dai_link->name);
2172
2173 if (drv->suspend && drv->ac97_control)
2174 drv->suspend(dai);
2175 }
2176
2177 /* suspend for capture */
2178 list_for_each_entry(dpcm_params,
2179 &fe->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients, list_be) {
2180
2181 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2182 struct snd_soc_dai *dai = be->cpu_dai;
2183 struct snd_soc_dai_driver *drv = dai->driver;
2184
2185 if (be->dai_link->ignore_suspend)
2186 continue;
2187
2188 dev_dbg(be->dev, "pm: BE CPU DAI capture suspend %s\n",
2189 be->dai_link->name);
2190
2191 if (drv->suspend && drv->ac97_control)
2192 drv->suspend(dai);
2193 }
2194
2195 return 0;
2196}
2197
2198int soc_dpcm_be_platform_suspend(struct snd_soc_pcm_runtime *fe)
2199{
2200 struct snd_soc_dpcm_params *dpcm_params;
2201
2202 /* suspend for playback */
2203 list_for_each_entry(dpcm_params,
2204 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients, list_be) {
2205
2206 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2207 struct snd_soc_platform *platform = be->platform;
2208 struct snd_soc_platform_driver *drv = platform->driver;
2209 struct snd_soc_dai *dai = be->cpu_dai;
2210
2211 if (be->dai_link->ignore_suspend)
2212 continue;
2213
2214 dev_dbg(be->dev, "pm: BE platform playback suspend %s\n",
2215 be->dai_link->name);
2216
2217 if (drv->suspend && !platform->suspended) {
2218 drv->suspend(dai);
2219 platform->suspended = 1;
2220 }
2221 }
2222
2223 /* suspend for capture */
2224 list_for_each_entry(dpcm_params,
2225 &fe->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients, list_be) {
2226
2227 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2228 struct snd_soc_platform *platform = be->platform;
2229 struct snd_soc_platform_driver *drv = platform->driver;
2230 struct snd_soc_dai *dai = be->cpu_dai;
2231
2232 if (be->dai_link->ignore_suspend)
2233 continue;
2234
2235 dev_dbg(be->dev, "pm: BE platform capture suspend %s\n",
2236 be->dai_link->name);
2237
2238 if (drv->suspend && !platform->suspended) {
2239 drv->suspend(dai);
2240 platform->suspended = 1;
2241 }
2242 }
2243 return 0;
2244}
2245
2246int soc_dpcm_fe_suspend(struct snd_soc_pcm_runtime *fe)
2247{
2248 struct snd_soc_dai *dai = fe->cpu_dai;
2249 struct snd_soc_dai_driver *dai_drv = dai->driver;
2250 struct snd_soc_platform *platform = fe->platform;
2251 struct snd_soc_platform_driver *plat_drv = platform->driver;
2252
2253 if (dai_drv->suspend && !dai_drv->ac97_control)
2254 dai_drv->suspend(dai);
2255
2256 if (plat_drv->suspend && !platform->suspended) {
2257 plat_drv->suspend(dai);
2258 platform->suspended = 1;
2259 }
2260
2261 soc_dpcm_be_cpu_dai_suspend(fe);
2262 soc_dpcm_be_platform_suspend(fe);
2263
2264 return 0;
2265}
2266
2267int soc_dpcm_be_cpu_dai_resume(struct snd_soc_pcm_runtime *fe)
2268{
2269 struct snd_soc_dpcm_params *dpcm_params;
2270
2271 /* resume for playback */
2272 list_for_each_entry(dpcm_params,
2273 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients, list_be) {
2274
2275 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2276 struct snd_soc_dai *dai = be->cpu_dai;
2277 struct snd_soc_dai_driver *drv = dai->driver;
2278
2279 if (be->dai_link->ignore_suspend)
2280 continue;
2281
2282 dev_dbg(be->dev, "pm: BE CPU DAI playback resume %s\n",
2283 be->dai_link->name);
2284
2285 if (drv->resume && !drv->ac97_control)
2286 drv->resume(dai);
2287 }
2288
2289 /* suspend for capture */
2290 list_for_each_entry(dpcm_params,
2291 &fe->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients, list_be) {
2292
2293 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2294 struct snd_soc_dai *dai = be->cpu_dai;
2295 struct snd_soc_dai_driver *drv = dai->driver;
2296
2297 if (be->dai_link->ignore_suspend)
2298 continue;
2299
2300 dev_dbg(be->dev, "pm: BE CPU DAI capture resume %s\n",
2301 be->dai_link->name);
2302
2303 if (drv->resume && !drv->ac97_control)
2304 drv->resume(dai);
2305 }
2306
2307 return 0;
2308}
2309
2310int soc_dpcm_be_ac97_cpu_dai_resume(struct snd_soc_pcm_runtime *fe)
2311{
2312 struct snd_soc_dpcm_params *dpcm_params;
2313
2314 /* resume for playback */
2315 list_for_each_entry(dpcm_params,
2316 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients, list_be) {
2317
2318 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2319 struct snd_soc_dai *dai = be->cpu_dai;
2320 struct snd_soc_dai_driver *drv = dai->driver;
2321
2322 if (be->dai_link->ignore_suspend)
2323 continue;
2324
2325 dev_dbg(be->dev, "pm: BE CPU DAI playback resume %s\n",
2326 be->dai_link->name);
2327
2328 if (drv->resume && drv->ac97_control)
2329 drv->resume(dai);
2330 }
2331
2332 /* suspend for capture */
2333 list_for_each_entry(dpcm_params,
2334 &fe->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients, list_be) {
2335
2336 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2337 struct snd_soc_dai *dai = be->cpu_dai;
2338 struct snd_soc_dai_driver *drv = dai->driver;
2339
2340 if (be->dai_link->ignore_suspend)
2341 continue;
2342
2343 dev_dbg(be->dev, "pm: BE CPU DAI capture resume %s\n",
2344 be->dai_link->name);
2345
2346 if (drv->resume && drv->ac97_control)
2347 drv->resume(dai);
2348 }
2349
2350 return 0;
2351}
2352
2353int soc_dpcm_be_platform_resume(struct snd_soc_pcm_runtime *fe)
2354{
2355 struct snd_soc_dpcm_params *dpcm_params;
2356
2357 /* resume for playback */
2358 list_for_each_entry(dpcm_params,
2359 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients, list_be) {
2360
2361 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2362 struct snd_soc_platform *platform = be->platform;
2363 struct snd_soc_platform_driver *drv = platform->driver;
2364 struct snd_soc_dai *dai = be->cpu_dai;
2365
2366 if (be->dai_link->ignore_suspend)
2367 continue;
2368
2369 dev_dbg(be->dev, "pm: BE platform playback resume %s\n",
2370 be->dai_link->name);
2371
2372 if (drv->resume && platform->suspended) {
2373 drv->resume(dai);
2374 platform->suspended = 0;
2375 }
2376 }
2377
2378 /* resume for capture */
2379 list_for_each_entry(dpcm_params,
2380 &fe->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients, list_be) {
2381
2382 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2383 struct snd_soc_platform *platform = be->platform;
2384 struct snd_soc_platform_driver *drv = platform->driver;
2385 struct snd_soc_dai *dai = be->cpu_dai;
2386
2387 if (be->dai_link->ignore_suspend)
2388 continue;
2389
2390 dev_dbg(be->dev, "pm: BE platform capture resume %s\n",
2391 be->dai_link->name);
2392
2393 if (drv->resume && platform->suspended) {
2394 drv->resume(dai);
2395 platform->suspended = 0;
2396 }
2397 }
2398
2399 return 0;
2400}
2401
2402int soc_dpcm_fe_resume(struct snd_soc_pcm_runtime *fe)
2403{
2404 struct snd_soc_dai *dai = fe->cpu_dai;
2405 struct snd_soc_dai_driver *dai_drv = dai->driver;
2406 struct snd_soc_platform *platform = fe->platform;
2407 struct snd_soc_platform_driver *plat_drv = platform->driver;
2408
2409 soc_dpcm_be_cpu_dai_resume(fe);
2410 soc_dpcm_be_platform_resume(fe);
2411
2412 if (dai_drv->resume && !dai_drv->ac97_control)
2413 dai_drv->resume(dai);
2414
2415 if (plat_drv->resume && platform->suspended) {
2416 plat_drv->resume(dai);
2417 platform->suspended = 0;
2418 }
2419
2420 return 0;
2421}
2422
2423/* called when opening FE stream */
2424int soc_dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2425{
2426 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2427 struct snd_soc_dpcm_params *dpcm_params;
2428 struct snd_soc_dapm_widget_list *list;
2429 int ret;
2430 int stream = fe_substream->stream;
2431
2432 fe->dpcm[stream].runtime = fe_substream->runtime;
2433
2434 if (fe_path_get(fe, stream, &list) <= 0) {
SathishKumar Manief4feb32012-10-09 13:31:13 -07002435 pr_warn_ratelimited("asoc: %s no valid %s route from source to sink\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -07002436 fe->dai_link->name, stream ? "capture" : "playback");
2437 return -EINVAL;
2438 }
2439
2440 /* calculate valid and active FE <-> BE dpcm_paramss */
2441 dpcm_process_paths(fe, stream, &list, 1);
2442
2443 ret = soc_dpcm_fe_dai_startup(fe_substream);
2444 if (ret < 0) {
2445 /* clean up all links */
2446 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be)
2447 dpcm_params->state = SND_SOC_DPCM_LINK_STATE_FREE;
2448
2449 be_disconnect(fe, stream);
2450 fe->dpcm[stream].runtime = NULL;
2451 }
2452
2453 fe_clear_pending(fe, stream);
2454 fe_path_put(&list);
2455 return ret;
2456}
2457
2458/* called when closing FE stream */
2459int soc_dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2460{
2461 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2462 struct snd_soc_dpcm_params *dpcm_params;
2463 int stream = fe_substream->stream, ret;
2464
2465 ret = soc_dpcm_fe_dai_shutdown(fe_substream);
2466
2467 /* mark FE's links ready to prune */
2468 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be)
2469 dpcm_params->state = SND_SOC_DPCM_LINK_STATE_FREE;
2470
2471 be_disconnect(fe, stream);
2472
2473 fe->dpcm[stream].runtime = NULL;
2474
2475 return ret;
2476}
2477
Liam Girdwoodddee6272011-06-09 14:45:53 +01002478/* create a new pcm */
2479int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2480{
2481 struct snd_soc_codec *codec = rtd->codec;
2482 struct snd_soc_platform *platform = rtd->platform;
2483 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2484 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002485 struct snd_pcm_substream *substream[2];
Liam Girdwoodddee6272011-06-09 14:45:53 +01002486 struct snd_pcm *pcm;
2487 char new_name[64];
2488 int ret = 0, playback = 0, capture = 0;
2489
Steve Mucklef132c6c2012-06-06 18:30:57 -07002490 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2491 if (cpu_dai->driver->playback.channels_min)
2492 playback = 1;
2493 if (cpu_dai->driver->capture.channels_min)
2494 capture = 1;
2495 } else {
2496 if (codec_dai->driver->playback.channels_min)
2497 playback = 1;
2498 if (codec_dai->driver->capture.channels_min)
2499 capture = 1;
2500 }
Sangsu Parka5002312012-01-02 17:15:10 +09002501
Steve Mucklef132c6c2012-06-06 18:30:57 -07002502 /* create the PCM */
2503 if (rtd->dai_link->no_pcm) {
2504 snprintf(new_name, sizeof(new_name), "(%s)",
2505 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002506
Steve Mucklef132c6c2012-06-06 18:30:57 -07002507 ret = snd_pcm_new_soc_be(rtd->card->snd_card, new_name, num,
2508 playback, capture, &pcm);
2509 } else {
2510 if (rtd->dai_link->dynamic)
2511 snprintf(new_name, sizeof(new_name), "%s (*)",
2512 rtd->dai_link->stream_name);
2513 else
2514 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2515 rtd->dai_link->stream_name, codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002516
Steve Mucklef132c6c2012-06-06 18:30:57 -07002517 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2518 capture, &pcm);
2519 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002520 if (ret < 0) {
2521 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
2522 return ret;
2523 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002524 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002525
2526 rtd->pcm = pcm;
2527 pcm->private_data = rtd;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002528 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2529
2530 substream[SNDRV_PCM_STREAM_PLAYBACK] =
2531 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
2532 substream[SNDRV_PCM_STREAM_CAPTURE] =
2533 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
2534
2535 if (rtd->dai_link->no_pcm) {
2536 if (playback)
2537 substream[SNDRV_PCM_STREAM_PLAYBACK]->private_data = rtd;
2538 if (capture)
2539 substream[SNDRV_PCM_STREAM_CAPTURE]->private_data = rtd;
2540 goto out;
2541 }
2542
2543 /* setup any hostless PCMs - i.e. no host IO is performed */
2544 if (rtd->dai_link->no_host_mode) {
2545 if (substream[SNDRV_PCM_STREAM_PLAYBACK]) {
2546 substream[SNDRV_PCM_STREAM_PLAYBACK]->hw_no_buffer = 1;
2547 snd_soc_set_runtime_hwparams(
2548 substream[SNDRV_PCM_STREAM_PLAYBACK],
2549 &no_host_hardware);
2550 }
2551 if (substream[SNDRV_PCM_STREAM_CAPTURE]) {
2552 substream[SNDRV_PCM_STREAM_CAPTURE]->hw_no_buffer = 1;
2553 snd_soc_set_runtime_hwparams(
2554 substream[SNDRV_PCM_STREAM_CAPTURE],
2555 &no_host_hardware);
2556 }
2557 }
2558
2559 /* ASoC PCM operations */
2560 if (rtd->dai_link->dynamic) {
2561 rtd->ops.open = soc_dpcm_fe_dai_open;
2562 rtd->ops.hw_params = soc_dpcm_fe_dai_hw_params;
2563 rtd->ops.prepare = soc_dpcm_fe_dai_prepare;
2564 rtd->ops.trigger = soc_dpcm_fe_dai_trigger;
2565 rtd->ops.hw_free = soc_dpcm_fe_dai_hw_free;
2566 rtd->ops.close = soc_dpcm_fe_dai_close;
2567 rtd->ops.pointer = soc_pcm_pointer;
2568 rtd->ops.ioctl = soc_pcm_ioctl;
2569 } else {
2570 rtd->ops.open = soc_pcm_open;
2571 rtd->ops.hw_params = soc_pcm_hw_params;
2572 rtd->ops.prepare = soc_pcm_prepare;
2573 rtd->ops.trigger = soc_pcm_trigger;
2574 rtd->ops.hw_free = soc_pcm_hw_free;
2575 rtd->ops.close = soc_pcm_close;
2576 rtd->ops.pointer = soc_pcm_pointer;
2577 rtd->ops.ioctl = soc_pcm_ioctl;
2578 }
2579
Liam Girdwoodddee6272011-06-09 14:45:53 +01002580 if (platform->driver->ops) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002581 rtd->ops.ack = platform->driver->ops->ack;
2582 rtd->ops.copy = platform->driver->ops->copy;
2583 rtd->ops.silence = platform->driver->ops->silence;
2584 rtd->ops.page = platform->driver->ops->page;
2585 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002586 }
2587
2588 if (playback)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002589 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002590
2591 if (capture)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002592 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002593
2594 if (platform->driver->pcm_new) {
2595 ret = platform->driver->pcm_new(rtd);
2596 if (ret < 0) {
2597 pr_err("asoc: platform pcm constructor failed\n");
2598 return ret;
2599 }
2600 }
2601
2602 pcm->private_free = platform->driver->pcm_free;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002603out:
Liam Girdwoodddee6272011-06-09 14:45:53 +01002604 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
2605 cpu_dai->name);
2606 return ret;
2607}
Steve Mucklef132c6c2012-06-06 18:30:57 -07002608
2609#ifdef CONFIG_DEBUG_FS
2610static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2611{
2612 switch (state) {
2613 case SND_SOC_DPCM_STATE_NEW:
2614 return "new";
2615 case SND_SOC_DPCM_STATE_OPEN:
2616 return "open";
2617 case SND_SOC_DPCM_STATE_HW_PARAMS:
2618 return "hw_params";
2619 case SND_SOC_DPCM_STATE_PREPARE:
2620 return "prepare";
2621 case SND_SOC_DPCM_STATE_START:
2622 return "start";
2623 case SND_SOC_DPCM_STATE_STOP:
2624 return "stop";
2625 case SND_SOC_DPCM_STATE_SUSPEND:
2626 return "suspend";
2627 case SND_SOC_DPCM_STATE_PAUSED:
2628 return "paused";
2629 case SND_SOC_DPCM_STATE_HW_FREE:
2630 return "hw_free";
2631 case SND_SOC_DPCM_STATE_CLOSE:
2632 return "close";
2633 }
2634
2635 return "unknown";
2636}
2637
2638static int soc_dpcm_state_open_file(struct inode *inode, struct file *file)
2639{
2640 file->private_data = inode->i_private;
2641 return 0;
2642}
2643
2644static ssize_t soc_dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2645 int stream, char *buf, size_t size)
2646{
2647 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2648 struct snd_soc_dpcm_params *dpcm_params;
2649 ssize_t offset = 0;
2650
2651 /* FE state */
2652 offset += snprintf(buf + offset, size - offset,
2653 "[%s - %s]\n", fe->dai_link->name,
2654 stream ? "Capture" : "Playback");
2655
2656 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2657 dpcm_state_string(fe->dpcm[stream].state));
2658
2659 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2660 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2661 offset += snprintf(buf + offset, size - offset,
2662 "Hardware Params: "
2663 "Format = %s, Channels = %d, Rate = %d\n",
2664 snd_pcm_format_name(params_format(params)),
2665 params_channels(params),
2666 params_rate(params));
2667
2668 /* BEs state */
2669 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2670
2671 if (list_empty(&fe->dpcm[stream].be_clients)) {
2672 offset += snprintf(buf + offset, size - offset,
2673 " No active DSP links\n");
2674 goto out;
2675 }
2676
2677 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
2678 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2679
2680 offset += snprintf(buf + offset, size - offset,
2681 "- %s\n", be->dai_link->name);
2682
2683 offset += snprintf(buf + offset, size - offset,
2684 " State: %s\n",
2685 dpcm_state_string(fe->dpcm[stream].state));
2686
2687 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2688 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2689 offset += snprintf(buf + offset, size - offset,
2690 " Hardware Params: "
2691 "Format = %s, Channels = %d, Rate = %d\n",
2692 snd_pcm_format_name(params_format(params)),
2693 params_channels(params),
2694 params_rate(params));
2695 }
2696
2697out:
2698 return offset;
2699}
2700
2701static ssize_t soc_dpcm_state_read_file(struct file *file, char __user *user_buf,
2702 size_t count, loff_t *ppos)
2703{
2704 struct snd_soc_pcm_runtime *fe = file->private_data;
2705 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2706 char *buf;
2707
2708 buf = kmalloc(out_count, GFP_KERNEL);
2709 if (!buf)
2710 return -ENOMEM;
2711
2712 if (fe->cpu_dai->driver->playback.channels_min)
2713 offset += soc_dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2714 buf + offset, out_count - offset);
2715
2716 if (fe->cpu_dai->driver->capture.channels_min)
2717 offset += soc_dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2718 buf + offset, out_count - offset);
2719
2720 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2721
2722 kfree(buf);
2723
2724 return ret;
2725}
2726
2727static const struct file_operations soc_dpcm_state_fops = {
2728 .open = soc_dpcm_state_open_file,
2729 .read = soc_dpcm_state_read_file,
2730 .llseek = default_llseek,
2731};
2732
2733int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2734{
2735 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2736 rtd->card->debugfs_card_root);
2737 if (!rtd->debugfs_dpcm_root) {
2738 dev_dbg(rtd->dev,
2739 "ASoC: Failed to create dpcm debugfs directory %s\n",
2740 rtd->dai_link->name);
2741 return -EINVAL;
2742 }
2743
2744 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0644,
2745 rtd->debugfs_dpcm_root,
2746 rtd, &soc_dpcm_state_fops);
2747
2748 return 0;
2749}
2750#endif