blob: 2f6f545f4ee88eac4d203b78655fca04f9c1af57 [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>
Liam Girdwood01d75842012-04-25 12:12:49 +010025#include <linux/export.h>
Liam Girdwoodf86dcef2012-04-25 12:12:50 +010026#include <linux/debugfs.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010027#include <sound/core.h>
28#include <sound/pcm.h>
29#include <sound/pcm_params.h>
30#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010031#include <sound/soc-dpcm.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010032#include <sound/initval.h>
33
Liam Girdwood01d75842012-04-25 12:12:49 +010034#define DPCM_MAX_BE_USERS 8
35
Lars-Peter Clausen90996f42013-05-14 11:05:30 +020036/**
37 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
38 * @substream: the pcm substream
39 * @hw: the hardware parameters
40 *
41 * Sets the substream runtime hardware parameters.
42 */
43int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
44 const struct snd_pcm_hardware *hw)
45{
46 struct snd_pcm_runtime *runtime = substream->runtime;
47 runtime->hw.info = hw->info;
48 runtime->hw.formats = hw->formats;
49 runtime->hw.period_bytes_min = hw->period_bytes_min;
50 runtime->hw.period_bytes_max = hw->period_bytes_max;
51 runtime->hw.periods_min = hw->periods_min;
52 runtime->hw.periods_max = hw->periods_max;
53 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
54 runtime->hw.fifo_size = hw->fifo_size;
55 return 0;
56}
57EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
58
Liam Girdwood01d75842012-04-25 12:12:49 +010059/* DPCM stream event, send event to FE and all active BEs. */
60static int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
61 int event)
62{
63 struct snd_soc_dpcm *dpcm;
64
65 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
66
67 struct snd_soc_pcm_runtime *be = dpcm->be;
68
Liam Girdwood103d84a2012-11-19 14:39:15 +000069 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +010070 be->dai_link->name, event, dir);
71
72 snd_soc_dapm_stream_event(be, dir, event);
73 }
74
75 snd_soc_dapm_stream_event(fe, dir, event);
76
77 return 0;
78}
79
Dong Aisheng17841022011-08-29 17:15:14 +080080static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
81 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +010082{
83 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +010084 int ret;
85
Dong Aisheng17841022011-08-29 17:15:14 +080086 if (!soc_dai->driver->symmetric_rates &&
Liam Girdwoodddee6272011-06-09 14:45:53 +010087 !rtd->dai_link->symmetric_rates)
88 return 0;
89
90 /* This can happen if multiple streams are starting simultaneously -
91 * the second can need to get its constraints before the first has
92 * picked a rate. Complain and allow the application to carry on.
93 */
Dong Aisheng17841022011-08-29 17:15:14 +080094 if (!soc_dai->rate) {
95 dev_warn(soc_dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +000096 "ASoC: Not enforcing symmetric_rates due to race\n");
Liam Girdwoodddee6272011-06-09 14:45:53 +010097 return 0;
98 }
99
Liam Girdwood103d84a2012-11-19 14:39:15 +0000100 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n", soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100101
102 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
103 SNDRV_PCM_HW_PARAM_RATE,
Dong Aisheng17841022011-08-29 17:15:14 +0800104 soc_dai->rate, soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100105 if (ret < 0) {
Dong Aisheng17841022011-08-29 17:15:14 +0800106 dev_err(soc_dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +0000107 "ASoC: Unable to apply rate symmetry constraint: %d\n",
108 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100109 return ret;
110 }
111
112 return 0;
113}
114
115/*
Mark Brown58ba9b22012-01-16 18:38:51 +0000116 * List of sample sizes that might go over the bus for parameter
117 * application. There ought to be a wildcard sample size for things
118 * like the DAC/ADC resolution to use but there isn't right now.
119 */
120static int sample_sizes[] = {
Peter Ujfalusi88e33952012-01-25 10:09:41 +0200121 24, 32,
Mark Brown58ba9b22012-01-16 18:38:51 +0000122};
123
124static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
125 struct snd_soc_dai *dai)
126{
127 int ret, i, bits;
128
129 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
130 bits = dai->driver->playback.sig_bits;
131 else
132 bits = dai->driver->capture.sig_bits;
133
134 if (!bits)
135 return;
136
137 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
Mark Brown278047f2012-01-19 18:04:18 +0000138 if (bits >= sample_sizes[i])
139 continue;
140
141 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
142 sample_sizes[i], bits);
Mark Brown58ba9b22012-01-16 18:38:51 +0000143 if (ret != 0)
144 dev_warn(dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +0000145 "ASoC: Failed to set MSB %d/%d: %d\n",
Mark Brown58ba9b22012-01-16 18:38:51 +0000146 bits, sample_sizes[i], ret);
147 }
148}
149
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200150static void soc_pcm_init_runtime_hw(struct snd_pcm_hardware *hw,
151 struct snd_soc_pcm_stream *codec_stream,
152 struct snd_soc_pcm_stream *cpu_stream)
153{
154 hw->rate_min = max(codec_stream->rate_min, cpu_stream->rate_min);
155 hw->rate_max = max(codec_stream->rate_max, cpu_stream->rate_max);
156 hw->channels_min = max(codec_stream->channels_min,
157 cpu_stream->channels_min);
158 hw->channels_max = min(codec_stream->channels_max,
159 cpu_stream->channels_max);
160 hw->formats = codec_stream->formats & cpu_stream->formats;
161 hw->rates = codec_stream->rates & cpu_stream->rates;
162 if (codec_stream->rates
163 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
164 hw->rates |= cpu_stream->rates;
165 if (cpu_stream->rates
166 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
167 hw->rates |= codec_stream->rates;
168}
169
Mark Brown58ba9b22012-01-16 18:38:51 +0000170/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100171 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
172 * then initialized and any private data can be allocated. This also calls
173 * startup for the cpu DAI, platform, machine and codec DAI.
174 */
175static int soc_pcm_open(struct snd_pcm_substream *substream)
176{
177 struct snd_soc_pcm_runtime *rtd = substream->private_data;
178 struct snd_pcm_runtime *runtime = substream->runtime;
179 struct snd_soc_platform *platform = rtd->platform;
180 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
181 struct snd_soc_dai *codec_dai = rtd->codec_dai;
182 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
183 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
184 int ret = 0;
185
Mark Brownd6652ef2011-12-03 20:14:31 +0000186 pm_runtime_get_sync(cpu_dai->dev);
187 pm_runtime_get_sync(codec_dai->dev);
188 pm_runtime_get_sync(platform->dev);
189
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100190 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100191
192 /* startup the audio subsystem */
193 if (cpu_dai->driver->ops->startup) {
194 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
195 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000196 dev_err(cpu_dai->dev, "ASoC: can't open interface"
197 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100198 goto out;
199 }
200 }
201
202 if (platform->driver->ops && platform->driver->ops->open) {
203 ret = platform->driver->ops->open(substream);
204 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000205 dev_err(platform->dev, "ASoC: can't open platform"
206 " %s: %d\n", platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100207 goto platform_err;
208 }
209 }
210
211 if (codec_dai->driver->ops->startup) {
212 ret = codec_dai->driver->ops->startup(substream, codec_dai);
213 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000214 dev_err(codec_dai->dev, "ASoC: can't open codec"
215 " %s: %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100216 goto codec_dai_err;
217 }
218 }
219
220 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
221 ret = rtd->dai_link->ops->startup(substream);
222 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000223 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000224 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100225 goto machine_err;
226 }
227 }
228
Liam Girdwood01d75842012-04-25 12:12:49 +0100229 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
230 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
231 goto dynamic;
232
Liam Girdwoodddee6272011-06-09 14:45:53 +0100233 /* Check that the codec and cpu DAIs are compatible */
234 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200235 soc_pcm_init_runtime_hw(&runtime->hw, &codec_dai_drv->playback,
236 &cpu_dai_drv->playback);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100237 } else {
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200238 soc_pcm_init_runtime_hw(&runtime->hw, &codec_dai_drv->capture,
239 &cpu_dai_drv->capture);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100240 }
241
242 ret = -EINVAL;
243 snd_pcm_limit_hw_rates(runtime);
244 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000245 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100246 codec_dai->name, cpu_dai->name);
247 goto config_err;
248 }
249 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000250 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100251 codec_dai->name, cpu_dai->name);
252 goto config_err;
253 }
254 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
255 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000256 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100257 codec_dai->name, cpu_dai->name);
258 goto config_err;
259 }
260
Mark Brown58ba9b22012-01-16 18:38:51 +0000261 soc_pcm_apply_msb(substream, codec_dai);
262 soc_pcm_apply_msb(substream, cpu_dai);
263
Liam Girdwoodddee6272011-06-09 14:45:53 +0100264 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800265 if (cpu_dai->active) {
266 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
267 if (ret != 0)
268 goto config_err;
269 }
270
271 if (codec_dai->active) {
272 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100273 if (ret != 0)
274 goto config_err;
275 }
276
Liam Girdwood103d84a2012-11-19 14:39:15 +0000277 pr_debug("ASoC: %s <-> %s info:\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100278 codec_dai->name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000279 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
280 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100281 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000282 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100283 runtime->hw.rate_max);
284
Liam Girdwood01d75842012-04-25 12:12:49 +0100285dynamic:
Liam Girdwoodddee6272011-06-09 14:45:53 +0100286 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
287 cpu_dai->playback_active++;
288 codec_dai->playback_active++;
289 } else {
290 cpu_dai->capture_active++;
291 codec_dai->capture_active++;
292 }
293 cpu_dai->active++;
294 codec_dai->active++;
295 rtd->codec->active++;
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100296 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100297 return 0;
298
299config_err:
300 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
301 rtd->dai_link->ops->shutdown(substream);
302
303machine_err:
304 if (codec_dai->driver->ops->shutdown)
305 codec_dai->driver->ops->shutdown(substream, codec_dai);
306
307codec_dai_err:
308 if (platform->driver->ops && platform->driver->ops->close)
309 platform->driver->ops->close(substream);
310
311platform_err:
312 if (cpu_dai->driver->ops->shutdown)
313 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
314out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100315 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000316
317 pm_runtime_put(platform->dev);
318 pm_runtime_put(codec_dai->dev);
319 pm_runtime_put(cpu_dai->dev);
320
Liam Girdwoodddee6272011-06-09 14:45:53 +0100321 return ret;
322}
323
324/*
325 * Power down the audio subsystem pmdown_time msecs after close is called.
326 * This is to ensure there are no pops or clicks in between any music tracks
327 * due to DAPM power cycling.
328 */
329static void close_delayed_work(struct work_struct *work)
330{
331 struct snd_soc_pcm_runtime *rtd =
332 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
333 struct snd_soc_dai *codec_dai = rtd->codec_dai;
334
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100335 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100336
Liam Girdwood103d84a2012-11-19 14:39:15 +0000337 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100338 codec_dai->driver->playback.stream_name,
339 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600340 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100341
342 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600343 if (rtd->pop_wait == 1) {
344 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800345 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000346 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100347 }
348
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100349 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100350}
351
352/*
353 * Called by ALSA when a PCM substream is closed. Private data can be
354 * freed here. The cpu DAI, codec DAI, machine and platform are also
355 * shutdown.
356 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100357static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100358{
359 struct snd_soc_pcm_runtime *rtd = substream->private_data;
360 struct snd_soc_platform *platform = rtd->platform;
361 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
362 struct snd_soc_dai *codec_dai = rtd->codec_dai;
363 struct snd_soc_codec *codec = rtd->codec;
364
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100365 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100366
367 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
375 cpu_dai->active--;
376 codec_dai->active--;
377 codec->active--;
378
Dong Aisheng17841022011-08-29 17:15:14 +0800379 /* clear the corresponding DAIs rate when inactive */
380 if (!cpu_dai->active)
381 cpu_dai->rate = 0;
382
383 if (!codec_dai->active)
384 codec_dai->rate = 0;
Sascha Hauer25b76792011-08-17 09:20:01 +0200385
Liam Girdwoodddee6272011-06-09 14:45:53 +0100386 /* Muting the DAC suppresses artifacts caused during digital
387 * shutdown, for example from stopping clocks.
388 */
Mark Brownda183962013-02-06 15:44:07 +0000389 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100390
391 if (cpu_dai->driver->ops->shutdown)
392 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
393
394 if (codec_dai->driver->ops->shutdown)
395 codec_dai->driver->ops->shutdown(substream, codec_dai);
396
397 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
398 rtd->dai_link->ops->shutdown(substream);
399
400 if (platform->driver->ops && platform->driver->ops->close)
401 platform->driver->ops->close(substream);
402 cpu_dai->runtime = NULL;
403
404 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mark Brownb5d1d032012-02-08 20:10:56 +0000405 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
Mark Brown5b6247a2011-10-27 12:11:26 +0200406 rtd->dai_link->ignore_pmdown_time) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300407 /* powered down playback stream now */
408 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800409 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800410 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300411 } else {
412 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600413 rtd->pop_wait = 1;
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300414 schedule_delayed_work(&rtd->delayed_work,
415 msecs_to_jiffies(rtd->pmdown_time));
416 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100417 } else {
418 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800419 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000420 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100421 }
422
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100423 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000424
425 pm_runtime_put(platform->dev);
426 pm_runtime_put(codec_dai->dev);
427 pm_runtime_put(cpu_dai->dev);
428
Liam Girdwoodddee6272011-06-09 14:45:53 +0100429 return 0;
430}
431
432/*
433 * Called by ALSA when the PCM substream is prepared, can set format, sample
434 * rate, etc. This function is non atomic and can be called multiple times,
435 * it can refer to the runtime info.
436 */
437static int soc_pcm_prepare(struct snd_pcm_substream *substream)
438{
439 struct snd_soc_pcm_runtime *rtd = substream->private_data;
440 struct snd_soc_platform *platform = rtd->platform;
441 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
442 struct snd_soc_dai *codec_dai = rtd->codec_dai;
443 int ret = 0;
444
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100445 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100446
447 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
448 ret = rtd->dai_link->ops->prepare(substream);
449 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000450 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
451 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100452 goto out;
453 }
454 }
455
456 if (platform->driver->ops && platform->driver->ops->prepare) {
457 ret = platform->driver->ops->prepare(substream);
458 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000459 dev_err(platform->dev, "ASoC: platform prepare error:"
460 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100461 goto out;
462 }
463 }
464
465 if (codec_dai->driver->ops->prepare) {
466 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
467 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000468 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000469 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100470 goto out;
471 }
472 }
473
474 if (cpu_dai->driver->ops->prepare) {
475 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
476 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000477 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000478 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100479 goto out;
480 }
481 }
482
483 /* cancel any delayed stream shutdown that is pending */
484 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600485 rtd->pop_wait) {
486 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100487 cancel_delayed_work(&rtd->delayed_work);
488 }
489
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000490 snd_soc_dapm_stream_event(rtd, substream->stream,
491 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100492
Mark Brownda183962013-02-06 15:44:07 +0000493 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100494
495out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100496 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100497 return ret;
498}
499
500/*
501 * Called by ALSA when the hardware params are set by application. This
502 * function can also be called multiple times and can allocate buffers
503 * (using snd_pcm_lib_* ). It's non-atomic.
504 */
505static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
506 struct snd_pcm_hw_params *params)
507{
508 struct snd_soc_pcm_runtime *rtd = substream->private_data;
509 struct snd_soc_platform *platform = rtd->platform;
510 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
511 struct snd_soc_dai *codec_dai = rtd->codec_dai;
512 int ret = 0;
513
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100514 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100515
516 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
517 ret = rtd->dai_link->ops->hw_params(substream, params);
518 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000519 dev_err(rtd->card->dev, "ASoC: machine hw_params"
520 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100521 goto out;
522 }
523 }
524
525 if (codec_dai->driver->ops->hw_params) {
526 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
527 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000528 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
529 " %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100530 goto codec_err;
531 }
532 }
533
534 if (cpu_dai->driver->ops->hw_params) {
535 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
536 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000537 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000538 cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100539 goto interface_err;
540 }
541 }
542
543 if (platform->driver->ops && platform->driver->ops->hw_params) {
544 ret = platform->driver->ops->hw_params(substream, params);
545 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000546 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000547 platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100548 goto platform_err;
549 }
550 }
551
Dong Aisheng17841022011-08-29 17:15:14 +0800552 /* store the rate for each DAIs */
553 cpu_dai->rate = params_rate(params);
554 codec_dai->rate = params_rate(params);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100555
556out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100557 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100558 return ret;
559
560platform_err:
561 if (cpu_dai->driver->ops->hw_free)
562 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
563
564interface_err:
565 if (codec_dai->driver->ops->hw_free)
566 codec_dai->driver->ops->hw_free(substream, codec_dai);
567
568codec_err:
569 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
570 rtd->dai_link->ops->hw_free(substream);
571
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100572 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100573 return ret;
574}
575
576/*
577 * Frees resources allocated by hw_params, can be called multiple times
578 */
579static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
580{
581 struct snd_soc_pcm_runtime *rtd = substream->private_data;
582 struct snd_soc_platform *platform = rtd->platform;
583 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
584 struct snd_soc_dai *codec_dai = rtd->codec_dai;
585 struct snd_soc_codec *codec = rtd->codec;
586
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100587 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100588
589 /* apply codec digital mute */
590 if (!codec->active)
Mark Brownda183962013-02-06 15:44:07 +0000591 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100592
593 /* free any machine hw params */
594 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
595 rtd->dai_link->ops->hw_free(substream);
596
597 /* free any DMA resources */
598 if (platform->driver->ops && platform->driver->ops->hw_free)
599 platform->driver->ops->hw_free(substream);
600
601 /* now free hw params for the DAIs */
602 if (codec_dai->driver->ops->hw_free)
603 codec_dai->driver->ops->hw_free(substream, codec_dai);
604
605 if (cpu_dai->driver->ops->hw_free)
606 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
607
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100608 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100609 return 0;
610}
611
612static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
613{
614 struct snd_soc_pcm_runtime *rtd = substream->private_data;
615 struct snd_soc_platform *platform = rtd->platform;
616 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
617 struct snd_soc_dai *codec_dai = rtd->codec_dai;
618 int ret;
619
620 if (codec_dai->driver->ops->trigger) {
621 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
622 if (ret < 0)
623 return ret;
624 }
625
626 if (platform->driver->ops && platform->driver->ops->trigger) {
627 ret = platform->driver->ops->trigger(substream, cmd);
628 if (ret < 0)
629 return ret;
630 }
631
632 if (cpu_dai->driver->ops->trigger) {
633 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
634 if (ret < 0)
635 return ret;
636 }
637 return 0;
638}
639
Mark Brown45c0a182012-05-09 21:46:27 +0100640static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
641 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100642{
643 struct snd_soc_pcm_runtime *rtd = substream->private_data;
644 struct snd_soc_platform *platform = rtd->platform;
645 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
646 struct snd_soc_dai *codec_dai = rtd->codec_dai;
647 int ret;
648
649 if (codec_dai->driver->ops->bespoke_trigger) {
650 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
651 if (ret < 0)
652 return ret;
653 }
654
655 if (platform->driver->bespoke_trigger) {
656 ret = platform->driver->bespoke_trigger(substream, cmd);
657 if (ret < 0)
658 return ret;
659 }
660
661 if (cpu_dai->driver->ops->bespoke_trigger) {
662 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
663 if (ret < 0)
664 return ret;
665 }
666 return 0;
667}
Liam Girdwoodddee6272011-06-09 14:45:53 +0100668/*
669 * soc level wrapper for pointer callback
670 * If cpu_dai, codec_dai, platform driver has the delay callback, than
671 * the runtime->delay will be updated accordingly.
672 */
673static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
674{
675 struct snd_soc_pcm_runtime *rtd = substream->private_data;
676 struct snd_soc_platform *platform = rtd->platform;
677 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
678 struct snd_soc_dai *codec_dai = rtd->codec_dai;
679 struct snd_pcm_runtime *runtime = substream->runtime;
680 snd_pcm_uframes_t offset = 0;
681 snd_pcm_sframes_t delay = 0;
682
683 if (platform->driver->ops && platform->driver->ops->pointer)
684 offset = platform->driver->ops->pointer(substream);
685
686 if (cpu_dai->driver->ops->delay)
687 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
688
689 if (codec_dai->driver->ops->delay)
690 delay += codec_dai->driver->ops->delay(substream, codec_dai);
691
692 if (platform->driver->delay)
693 delay += platform->driver->delay(substream, codec_dai);
694
695 runtime->delay = delay;
696
697 return offset;
698}
699
Liam Girdwood01d75842012-04-25 12:12:49 +0100700/* connect a FE and BE */
701static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
702 struct snd_soc_pcm_runtime *be, int stream)
703{
704 struct snd_soc_dpcm *dpcm;
705
706 /* only add new dpcms */
707 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
708 if (dpcm->be == be && dpcm->fe == fe)
709 return 0;
710 }
711
712 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
713 if (!dpcm)
714 return -ENOMEM;
715
716 dpcm->be = be;
717 dpcm->fe = fe;
718 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
719 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
720 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
721 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
722
723 dev_dbg(fe->dev, " connected new DPCM %s path %s %s %s\n",
724 stream ? "capture" : "playback", fe->dai_link->name,
725 stream ? "<-" : "->", be->dai_link->name);
726
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100727#ifdef CONFIG_DEBUG_FS
728 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
729 fe->debugfs_dpcm_root, &dpcm->state);
730#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100731 return 1;
732}
733
734/* reparent a BE onto another FE */
735static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
736 struct snd_soc_pcm_runtime *be, int stream)
737{
738 struct snd_soc_dpcm *dpcm;
739 struct snd_pcm_substream *fe_substream, *be_substream;
740
741 /* reparent if BE is connected to other FEs */
742 if (!be->dpcm[stream].users)
743 return;
744
745 be_substream = snd_soc_dpcm_get_substream(be, stream);
746
747 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
748 if (dpcm->fe == fe)
749 continue;
750
751 dev_dbg(fe->dev, " reparent %s path %s %s %s\n",
752 stream ? "capture" : "playback",
753 dpcm->fe->dai_link->name,
754 stream ? "<-" : "->", dpcm->be->dai_link->name);
755
756 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
757 be_substream->runtime = fe_substream->runtime;
758 break;
759 }
760}
761
762/* disconnect a BE and FE */
763static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
764{
765 struct snd_soc_dpcm *dpcm, *d;
766
767 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000768 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100769 stream ? "capture" : "playback",
770 dpcm->be->dai_link->name);
771
772 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
773 continue;
774
775 dev_dbg(fe->dev, " freed DSP %s path %s %s %s\n",
776 stream ? "capture" : "playback", fe->dai_link->name,
777 stream ? "<-" : "->", dpcm->be->dai_link->name);
778
779 /* BEs still alive need new FE */
780 dpcm_be_reparent(fe, dpcm->be, stream);
781
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100782#ifdef CONFIG_DEBUG_FS
783 debugfs_remove(dpcm->debugfs_state);
784#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100785 list_del(&dpcm->list_be);
786 list_del(&dpcm->list_fe);
787 kfree(dpcm);
788 }
789}
790
791/* get BE for DAI widget and stream */
792static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
793 struct snd_soc_dapm_widget *widget, int stream)
794{
795 struct snd_soc_pcm_runtime *be;
796 int i;
797
798 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
799 for (i = 0; i < card->num_links; i++) {
800 be = &card->rtd[i];
801
Liam Girdwood35ea0652012-06-05 19:26:59 +0100802 if (!be->dai_link->no_pcm)
803 continue;
804
Liam Girdwood01d75842012-04-25 12:12:49 +0100805 if (be->cpu_dai->playback_widget == widget ||
806 be->codec_dai->playback_widget == widget)
807 return be;
808 }
809 } else {
810
811 for (i = 0; i < card->num_links; i++) {
812 be = &card->rtd[i];
813
Liam Girdwood35ea0652012-06-05 19:26:59 +0100814 if (!be->dai_link->no_pcm)
815 continue;
816
Liam Girdwood01d75842012-04-25 12:12:49 +0100817 if (be->cpu_dai->capture_widget == widget ||
818 be->codec_dai->capture_widget == widget)
819 return be;
820 }
821 }
822
Liam Girdwood103d84a2012-11-19 14:39:15 +0000823 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100824 stream ? "capture" : "playback", widget->name);
825 return NULL;
826}
827
828static inline struct snd_soc_dapm_widget *
829 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
830{
831 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
832 return rtd->cpu_dai->playback_widget;
833 else
834 return rtd->cpu_dai->capture_widget;
835}
836
837static inline struct snd_soc_dapm_widget *
838 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
839{
840 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
841 return rtd->codec_dai->playback_widget;
842 else
843 return rtd->codec_dai->capture_widget;
844}
845
846static int widget_in_list(struct snd_soc_dapm_widget_list *list,
847 struct snd_soc_dapm_widget *widget)
848{
849 int i;
850
851 for (i = 0; i < list->num_widgets; i++) {
852 if (widget == list->widgets[i])
853 return 1;
854 }
855
856 return 0;
857}
858
859static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
860 int stream, struct snd_soc_dapm_widget_list **list_)
861{
862 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
863 struct snd_soc_dapm_widget_list *list;
864 int paths;
865
866 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
867 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
868 if (list == NULL)
869 return -ENOMEM;
870
871 /* get number of valid DAI paths and their widgets */
872 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
873
Liam Girdwood103d84a2012-11-19 14:39:15 +0000874 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +0100875 stream ? "capture" : "playback");
876
877 *list_ = list;
878 return paths;
879}
880
881static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
882{
883 kfree(*list);
884}
885
886static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
887 struct snd_soc_dapm_widget_list **list_)
888{
889 struct snd_soc_dpcm *dpcm;
890 struct snd_soc_dapm_widget_list *list = *list_;
891 struct snd_soc_dapm_widget *widget;
892 int prune = 0;
893
894 /* Destroy any old FE <--> BE connections */
895 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
896
897 /* is there a valid CPU DAI widget for this BE */
898 widget = rtd_get_cpu_widget(dpcm->be, stream);
899
900 /* prune the BE if it's no longer in our active list */
901 if (widget && widget_in_list(list, widget))
902 continue;
903
904 /* is there a valid CODEC DAI widget for this BE */
905 widget = rtd_get_codec_widget(dpcm->be, stream);
906
907 /* prune the BE if it's no longer in our active list */
908 if (widget && widget_in_list(list, widget))
909 continue;
910
Liam Girdwood103d84a2012-11-19 14:39:15 +0000911 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100912 stream ? "capture" : "playback",
913 dpcm->be->dai_link->name, fe->dai_link->name);
914 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
915 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
916 prune++;
917 }
918
Liam Girdwood103d84a2012-11-19 14:39:15 +0000919 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +0100920 return prune;
921}
922
923static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
924 struct snd_soc_dapm_widget_list **list_)
925{
926 struct snd_soc_card *card = fe->card;
927 struct snd_soc_dapm_widget_list *list = *list_;
928 struct snd_soc_pcm_runtime *be;
929 int i, new = 0, err;
930
931 /* Create any new FE <--> BE connections */
932 for (i = 0; i < list->num_widgets; i++) {
933
934 if (list->widgets[i]->id != snd_soc_dapm_dai)
935 continue;
936
937 /* is there a valid BE rtd for this widget */
938 be = dpcm_get_be(card, list->widgets[i], stream);
939 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000940 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100941 list->widgets[i]->name);
942 continue;
943 }
944
945 /* make sure BE is a real BE */
946 if (!be->dai_link->no_pcm)
947 continue;
948
949 /* don't connect if FE is not running */
950 if (!fe->dpcm[stream].runtime)
951 continue;
952
953 /* newly connected FE and BE */
954 err = dpcm_be_connect(fe, be, stream);
955 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000956 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100957 list->widgets[i]->name);
958 break;
959 } else if (err == 0) /* already connected */
960 continue;
961
962 /* new */
963 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
964 new++;
965 }
966
Liam Girdwood103d84a2012-11-19 14:39:15 +0000967 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +0100968 return new;
969}
970
971/*
972 * Find the corresponding BE DAIs that source or sink audio to this
973 * FE substream.
974 */
975static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
976 int stream, struct snd_soc_dapm_widget_list **list, int new)
977{
978 if (new)
979 return dpcm_add_paths(fe, stream, list);
980 else
981 return dpcm_prune_paths(fe, stream, list);
982}
983
984static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
985{
986 struct snd_soc_dpcm *dpcm;
987
988 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
989 dpcm->be->dpcm[stream].runtime_update =
990 SND_SOC_DPCM_UPDATE_NO;
991}
992
993static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
994 int stream)
995{
996 struct snd_soc_dpcm *dpcm;
997
998 /* disable any enabled and non active backends */
999 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1000
1001 struct snd_soc_pcm_runtime *be = dpcm->be;
1002 struct snd_pcm_substream *be_substream =
1003 snd_soc_dpcm_get_substream(be, stream);
1004
1005 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001006 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001007 stream ? "capture" : "playback",
1008 be->dpcm[stream].state);
1009
1010 if (--be->dpcm[stream].users != 0)
1011 continue;
1012
1013 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1014 continue;
1015
1016 soc_pcm_close(be_substream);
1017 be_substream->runtime = NULL;
1018 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1019 }
1020}
1021
1022static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1023{
1024 struct snd_soc_dpcm *dpcm;
1025 int err, count = 0;
1026
1027 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1028 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1029
1030 struct snd_soc_pcm_runtime *be = dpcm->be;
1031 struct snd_pcm_substream *be_substream =
1032 snd_soc_dpcm_get_substream(be, stream);
1033
1034 /* is this op for this BE ? */
1035 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1036 continue;
1037
1038 /* first time the dpcm is open ? */
1039 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001040 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001041 stream ? "capture" : "playback",
1042 be->dpcm[stream].state);
1043
1044 if (be->dpcm[stream].users++ != 0)
1045 continue;
1046
1047 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1048 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1049 continue;
1050
Liam Girdwood103d84a2012-11-19 14:39:15 +00001051 dev_dbg(be->dev, "ASoC: open BE %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001052
1053 be_substream->runtime = be->dpcm[stream].runtime;
1054 err = soc_pcm_open(be_substream);
1055 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001056 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001057 be->dpcm[stream].users--;
1058 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001059 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001060 stream ? "capture" : "playback",
1061 be->dpcm[stream].state);
1062
1063 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1064 goto unwind;
1065 }
1066
1067 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1068 count++;
1069 }
1070
1071 return count;
1072
1073unwind:
1074 /* disable any enabled and non active backends */
1075 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1076 struct snd_soc_pcm_runtime *be = dpcm->be;
1077 struct snd_pcm_substream *be_substream =
1078 snd_soc_dpcm_get_substream(be, stream);
1079
1080 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1081 continue;
1082
1083 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001084 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001085 stream ? "capture" : "playback",
1086 be->dpcm[stream].state);
1087
1088 if (--be->dpcm[stream].users != 0)
1089 continue;
1090
1091 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1092 continue;
1093
1094 soc_pcm_close(be_substream);
1095 be_substream->runtime = NULL;
1096 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1097 }
1098
1099 return err;
1100}
1101
Mark Brown45c0a182012-05-09 21:46:27 +01001102static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001103{
1104 struct snd_pcm_runtime *runtime = substream->runtime;
1105 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1106 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1107 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1108
1109 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1110 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1111 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1112 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1113 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1114 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1115 runtime->hw.rates = cpu_dai_drv->playback.rates;
1116 } else {
1117 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1118 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1119 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1120 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1121 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1122 runtime->hw.rates = cpu_dai_drv->capture.rates;
1123 }
1124}
1125
1126static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1127{
1128 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1129 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1130 int stream = fe_substream->stream, ret = 0;
1131
1132 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1133
1134 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1135 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001136 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001137 goto be_err;
1138 }
1139
Liam Girdwood103d84a2012-11-19 14:39:15 +00001140 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001141
1142 /* start the DAI frontend */
1143 ret = soc_pcm_open(fe_substream);
1144 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001145 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001146 goto unwind;
1147 }
1148
1149 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1150
1151 dpcm_set_fe_runtime(fe_substream);
1152 snd_pcm_limit_hw_rates(runtime);
1153
1154 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1155 return 0;
1156
1157unwind:
1158 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1159be_err:
1160 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1161 return ret;
1162}
1163
1164static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1165{
1166 struct snd_soc_dpcm *dpcm;
1167
1168 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1169 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1170
1171 struct snd_soc_pcm_runtime *be = dpcm->be;
1172 struct snd_pcm_substream *be_substream =
1173 snd_soc_dpcm_get_substream(be, stream);
1174
1175 /* is this op for this BE ? */
1176 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1177 continue;
1178
1179 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001180 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001181 stream ? "capture" : "playback",
1182 be->dpcm[stream].state);
1183
1184 if (--be->dpcm[stream].users != 0)
1185 continue;
1186
1187 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1188 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1189 continue;
1190
Liam Girdwood103d84a2012-11-19 14:39:15 +00001191 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001192 dpcm->fe->dai_link->name);
1193
1194 soc_pcm_close(be_substream);
1195 be_substream->runtime = NULL;
1196
1197 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1198 }
1199 return 0;
1200}
1201
1202static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1203{
1204 struct snd_soc_pcm_runtime *fe = substream->private_data;
1205 int stream = substream->stream;
1206
1207 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1208
1209 /* shutdown the BEs */
1210 dpcm_be_dai_shutdown(fe, substream->stream);
1211
Liam Girdwood103d84a2012-11-19 14:39:15 +00001212 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001213
1214 /* now shutdown the frontend */
1215 soc_pcm_close(substream);
1216
1217 /* run the stream event for each BE */
1218 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1219
1220 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1221 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1222 return 0;
1223}
1224
1225static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1226{
1227 struct snd_soc_dpcm *dpcm;
1228
1229 /* only hw_params backends that are either sinks or sources
1230 * to this frontend DAI */
1231 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1232
1233 struct snd_soc_pcm_runtime *be = dpcm->be;
1234 struct snd_pcm_substream *be_substream =
1235 snd_soc_dpcm_get_substream(be, stream);
1236
1237 /* is this op for this BE ? */
1238 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1239 continue;
1240
1241 /* only free hw when no longer used - check all FEs */
1242 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1243 continue;
1244
1245 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1246 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1247 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001248 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001249 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1250 continue;
1251
Liam Girdwood103d84a2012-11-19 14:39:15 +00001252 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001253 dpcm->fe->dai_link->name);
1254
1255 soc_pcm_hw_free(be_substream);
1256
1257 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1258 }
1259
1260 return 0;
1261}
1262
Mark Brown45c0a182012-05-09 21:46:27 +01001263static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001264{
1265 struct snd_soc_pcm_runtime *fe = substream->private_data;
1266 int err, stream = substream->stream;
1267
1268 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1269 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1270
Liam Girdwood103d84a2012-11-19 14:39:15 +00001271 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001272
1273 /* call hw_free on the frontend */
1274 err = soc_pcm_hw_free(substream);
1275 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001276 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001277 fe->dai_link->name);
1278
1279 /* only hw_params backends that are either sinks or sources
1280 * to this frontend DAI */
1281 err = dpcm_be_dai_hw_free(fe, stream);
1282
1283 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1284 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1285
1286 mutex_unlock(&fe->card->mutex);
1287 return 0;
1288}
1289
1290static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1291{
1292 struct snd_soc_dpcm *dpcm;
1293 int ret;
1294
1295 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1296
1297 struct snd_soc_pcm_runtime *be = dpcm->be;
1298 struct snd_pcm_substream *be_substream =
1299 snd_soc_dpcm_get_substream(be, stream);
1300
1301 /* is this op for this BE ? */
1302 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1303 continue;
1304
1305 /* only allow hw_params() if no connected FEs are running */
1306 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1307 continue;
1308
1309 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1310 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1311 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1312 continue;
1313
Liam Girdwood103d84a2012-11-19 14:39:15 +00001314 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001315 dpcm->fe->dai_link->name);
1316
1317 /* copy params for each dpcm */
1318 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1319 sizeof(struct snd_pcm_hw_params));
1320
1321 /* perform any hw_params fixups */
1322 if (be->dai_link->be_hw_params_fixup) {
1323 ret = be->dai_link->be_hw_params_fixup(be,
1324 &dpcm->hw_params);
1325 if (ret < 0) {
1326 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001327 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001328 ret);
1329 goto unwind;
1330 }
1331 }
1332
1333 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1334 if (ret < 0) {
1335 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001336 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001337 goto unwind;
1338 }
1339
1340 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1341 }
1342 return 0;
1343
1344unwind:
1345 /* disable any enabled and non active backends */
1346 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1347 struct snd_soc_pcm_runtime *be = dpcm->be;
1348 struct snd_pcm_substream *be_substream =
1349 snd_soc_dpcm_get_substream(be, stream);
1350
1351 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1352 continue;
1353
1354 /* only allow hw_free() if no connected FEs are running */
1355 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1356 continue;
1357
1358 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1359 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1360 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1361 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1362 continue;
1363
1364 soc_pcm_hw_free(be_substream);
1365 }
1366
1367 return ret;
1368}
1369
Mark Brown45c0a182012-05-09 21:46:27 +01001370static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1371 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001372{
1373 struct snd_soc_pcm_runtime *fe = substream->private_data;
1374 int ret, stream = substream->stream;
1375
1376 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1377 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1378
1379 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1380 sizeof(struct snd_pcm_hw_params));
1381 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1382 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001383 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001384 goto out;
1385 }
1386
Liam Girdwood103d84a2012-11-19 14:39:15 +00001387 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001388 fe->dai_link->name, params_rate(params),
1389 params_channels(params), params_format(params));
1390
1391 /* call hw_params on the frontend */
1392 ret = soc_pcm_hw_params(substream, params);
1393 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001394 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001395 dpcm_be_dai_hw_free(fe, stream);
1396 } else
1397 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1398
1399out:
1400 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1401 mutex_unlock(&fe->card->mutex);
1402 return ret;
1403}
1404
1405static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1406 struct snd_pcm_substream *substream, int cmd)
1407{
1408 int ret;
1409
Liam Girdwood103d84a2012-11-19 14:39:15 +00001410 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001411 dpcm->fe->dai_link->name, cmd);
1412
1413 ret = soc_pcm_trigger(substream, cmd);
1414 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001415 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001416
1417 return ret;
1418}
1419
Mark Brown45c0a182012-05-09 21:46:27 +01001420static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1421 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001422{
1423 struct snd_soc_dpcm *dpcm;
1424 int ret = 0;
1425
1426 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1427
1428 struct snd_soc_pcm_runtime *be = dpcm->be;
1429 struct snd_pcm_substream *be_substream =
1430 snd_soc_dpcm_get_substream(be, stream);
1431
1432 /* is this op for this BE ? */
1433 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1434 continue;
1435
1436 switch (cmd) {
1437 case SNDRV_PCM_TRIGGER_START:
1438 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1439 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1440 continue;
1441
1442 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1443 if (ret)
1444 return ret;
1445
1446 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1447 break;
1448 case SNDRV_PCM_TRIGGER_RESUME:
1449 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1450 continue;
1451
1452 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1453 if (ret)
1454 return ret;
1455
1456 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1457 break;
1458 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1459 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1460 continue;
1461
1462 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1463 if (ret)
1464 return ret;
1465
1466 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1467 break;
1468 case SNDRV_PCM_TRIGGER_STOP:
1469 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1470 continue;
1471
1472 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1473 continue;
1474
1475 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1476 if (ret)
1477 return ret;
1478
1479 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1480 break;
1481 case SNDRV_PCM_TRIGGER_SUSPEND:
1482 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1483 continue;
1484
1485 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1486 continue;
1487
1488 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1489 if (ret)
1490 return ret;
1491
1492 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1493 break;
1494 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1495 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1496 continue;
1497
1498 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1499 continue;
1500
1501 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1502 if (ret)
1503 return ret;
1504
1505 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1506 break;
1507 }
1508 }
1509
1510 return ret;
1511}
1512EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1513
Mark Brown45c0a182012-05-09 21:46:27 +01001514static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001515{
1516 struct snd_soc_pcm_runtime *fe = substream->private_data;
1517 int stream = substream->stream, ret;
1518 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1519
1520 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1521
1522 switch (trigger) {
1523 case SND_SOC_DPCM_TRIGGER_PRE:
1524 /* call trigger on the frontend before the backend. */
1525
Liam Girdwood103d84a2012-11-19 14:39:15 +00001526 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001527 fe->dai_link->name, cmd);
1528
1529 ret = soc_pcm_trigger(substream, cmd);
1530 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001531 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001532 goto out;
1533 }
1534
1535 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1536 break;
1537 case SND_SOC_DPCM_TRIGGER_POST:
1538 /* call trigger on the frontend after the backend. */
1539
1540 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1541 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001542 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001543 goto out;
1544 }
1545
Liam Girdwood103d84a2012-11-19 14:39:15 +00001546 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001547 fe->dai_link->name, cmd);
1548
1549 ret = soc_pcm_trigger(substream, cmd);
1550 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001551 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1552 /* bespoke trigger() - handles both FE and BEs */
1553
Liam Girdwood103d84a2012-11-19 14:39:15 +00001554 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001555 fe->dai_link->name, cmd);
1556
1557 ret = soc_pcm_bespoke_trigger(substream, cmd);
1558 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001559 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001560 goto out;
1561 }
1562 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01001563 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00001564 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01001565 fe->dai_link->name);
1566 ret = -EINVAL;
1567 goto out;
1568 }
1569
1570 switch (cmd) {
1571 case SNDRV_PCM_TRIGGER_START:
1572 case SNDRV_PCM_TRIGGER_RESUME:
1573 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1574 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1575 break;
1576 case SNDRV_PCM_TRIGGER_STOP:
1577 case SNDRV_PCM_TRIGGER_SUSPEND:
1578 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1579 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1580 break;
1581 }
1582
1583out:
1584 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1585 return ret;
1586}
1587
1588static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1589{
1590 struct snd_soc_dpcm *dpcm;
1591 int ret = 0;
1592
1593 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1594
1595 struct snd_soc_pcm_runtime *be = dpcm->be;
1596 struct snd_pcm_substream *be_substream =
1597 snd_soc_dpcm_get_substream(be, stream);
1598
1599 /* is this op for this BE ? */
1600 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1601 continue;
1602
1603 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1604 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1605 continue;
1606
Liam Girdwood103d84a2012-11-19 14:39:15 +00001607 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001608 dpcm->fe->dai_link->name);
1609
1610 ret = soc_pcm_prepare(be_substream);
1611 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001612 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001613 ret);
1614 break;
1615 }
1616
1617 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1618 }
1619 return ret;
1620}
1621
Mark Brown45c0a182012-05-09 21:46:27 +01001622static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001623{
1624 struct snd_soc_pcm_runtime *fe = substream->private_data;
1625 int stream = substream->stream, ret = 0;
1626
1627 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1628
Liam Girdwood103d84a2012-11-19 14:39:15 +00001629 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001630
1631 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1632
1633 /* there is no point preparing this FE if there are no BEs */
1634 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001635 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001636 fe->dai_link->name);
1637 ret = -EINVAL;
1638 goto out;
1639 }
1640
1641 ret = dpcm_be_dai_prepare(fe, substream->stream);
1642 if (ret < 0)
1643 goto out;
1644
1645 /* call prepare on the frontend */
1646 ret = soc_pcm_prepare(substream);
1647 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001648 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001649 fe->dai_link->name);
1650 goto out;
1651 }
1652
1653 /* run the stream event for each BE */
1654 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1655 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1656
1657out:
1658 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1659 mutex_unlock(&fe->card->mutex);
1660
1661 return ret;
1662}
1663
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001664static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1665 unsigned int cmd, void *arg)
1666{
1667 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1668 struct snd_soc_platform *platform = rtd->platform;
1669
1670 if (platform->driver->ops->ioctl)
1671 return platform->driver->ops->ioctl(substream, cmd, arg);
1672 return snd_pcm_lib_ioctl(substream, cmd, arg);
1673}
1674
Liam Girdwood618dae12012-04-25 12:12:51 +01001675static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1676{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001677 struct snd_pcm_substream *substream =
1678 snd_soc_dpcm_get_substream(fe, stream);
1679 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001680 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01001681
Liam Girdwood103d84a2012-11-19 14:39:15 +00001682 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001683 stream ? "capture" : "playback", fe->dai_link->name);
1684
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001685 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1686 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001687 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001688 fe->dai_link->name);
1689
1690 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1691 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001692 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001693 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001694 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001695 fe->dai_link->name);
1696
1697 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1698 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001699 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001700 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001701
1702 err = dpcm_be_dai_hw_free(fe, stream);
1703 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001704 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001705
1706 err = dpcm_be_dai_shutdown(fe, stream);
1707 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001708 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001709
1710 /* run the stream event for each BE */
1711 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1712
1713 return 0;
1714}
1715
1716static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1717{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001718 struct snd_pcm_substream *substream =
1719 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01001720 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001721 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001722 int ret;
1723
Liam Girdwood103d84a2012-11-19 14:39:15 +00001724 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001725 stream ? "capture" : "playback", fe->dai_link->name);
1726
1727 /* Only start the BE if the FE is ready */
1728 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1729 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1730 return -EINVAL;
1731
1732 /* startup must always be called for new BEs */
1733 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001734 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001735 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01001736
1737 /* keep going if FE state is > open */
1738 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1739 return 0;
1740
1741 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001742 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001743 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01001744
1745 /* keep going if FE state is > hw_params */
1746 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1747 return 0;
1748
1749
1750 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001751 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001752 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01001753
1754 /* run the stream event for each BE */
1755 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1756
1757 /* keep going if FE state is > prepare */
1758 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1759 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1760 return 0;
1761
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001762 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1763 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001764 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001765 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01001766
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001767 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1768 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001769 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001770 goto hw_free;
1771 }
1772 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001773 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001774 fe->dai_link->name);
1775
1776 ret = dpcm_be_dai_trigger(fe, stream,
1777 SNDRV_PCM_TRIGGER_START);
1778 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001779 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001780 goto hw_free;
1781 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001782 }
1783
1784 return 0;
1785
1786hw_free:
1787 dpcm_be_dai_hw_free(fe, stream);
1788close:
1789 dpcm_be_dai_shutdown(fe, stream);
1790disconnect:
1791 /* disconnect any non started BEs */
1792 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1793 struct snd_soc_pcm_runtime *be = dpcm->be;
1794 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1795 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1796 }
1797
1798 return ret;
1799}
1800
1801static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1802{
1803 int ret;
1804
1805 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1806 ret = dpcm_run_update_startup(fe, stream);
1807 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001808 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001809 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1810
1811 return ret;
1812}
1813
1814static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1815{
1816 int ret;
1817
1818 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1819 ret = dpcm_run_update_shutdown(fe, stream);
1820 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001821 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001822 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1823
1824 return ret;
1825}
1826
1827/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1828 * any DAI links.
1829 */
1830int soc_dpcm_runtime_update(struct snd_soc_dapm_widget *widget)
1831{
1832 struct snd_soc_card *card;
1833 int i, old, new, paths;
1834
1835 if (widget->codec)
1836 card = widget->codec->card;
1837 else if (widget->platform)
1838 card = widget->platform->card;
1839 else
1840 return -EINVAL;
1841
1842 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1843 for (i = 0; i < card->num_rtd; i++) {
1844 struct snd_soc_dapm_widget_list *list;
1845 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1846
1847 /* make sure link is FE */
1848 if (!fe->dai_link->dynamic)
1849 continue;
1850
1851 /* only check active links */
1852 if (!fe->cpu_dai->active)
1853 continue;
1854
1855 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001856 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001857 fe->dai_link->name);
1858
1859 /* skip if FE doesn't have playback capability */
1860 if (!fe->cpu_dai->driver->playback.channels_min)
1861 goto capture;
1862
1863 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1864 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001865 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001866 fe->dai_link->name, "playback");
1867 mutex_unlock(&card->mutex);
1868 return paths;
1869 }
1870
1871 /* update any new playback paths */
1872 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1873 if (new) {
1874 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1875 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1876 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1877 }
1878
1879 /* update any old playback paths */
1880 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1881 if (old) {
1882 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1883 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1884 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1885 }
1886
1887capture:
1888 /* skip if FE doesn't have capture capability */
1889 if (!fe->cpu_dai->driver->capture.channels_min)
1890 continue;
1891
1892 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1893 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001894 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001895 fe->dai_link->name, "capture");
1896 mutex_unlock(&card->mutex);
1897 return paths;
1898 }
1899
1900 /* update any new capture paths */
1901 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1902 if (new) {
1903 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1904 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1905 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1906 }
1907
1908 /* update any old capture paths */
1909 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1910 if (old) {
1911 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1912 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1913 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1914 }
1915
1916 dpcm_path_put(&list);
1917 }
1918
1919 mutex_unlock(&card->mutex);
1920 return 0;
1921}
Liam Girdwood01d75842012-04-25 12:12:49 +01001922int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
1923{
1924 struct snd_soc_dpcm *dpcm;
1925 struct list_head *clients =
1926 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
1927
1928 list_for_each_entry(dpcm, clients, list_be) {
1929
1930 struct snd_soc_pcm_runtime *be = dpcm->be;
1931 struct snd_soc_dai *dai = be->codec_dai;
1932 struct snd_soc_dai_driver *drv = dai->driver;
1933
1934 if (be->dai_link->ignore_suspend)
1935 continue;
1936
Liam Girdwood103d84a2012-11-19 14:39:15 +00001937 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001938
1939 if (drv->ops->digital_mute && dai->playback_active)
1940 drv->ops->digital_mute(dai, mute);
1941 }
1942
1943 return 0;
1944}
1945
Mark Brown45c0a182012-05-09 21:46:27 +01001946static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001947{
1948 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1949 struct snd_soc_dpcm *dpcm;
1950 struct snd_soc_dapm_widget_list *list;
1951 int ret;
1952 int stream = fe_substream->stream;
1953
1954 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1955 fe->dpcm[stream].runtime = fe_substream->runtime;
1956
1957 if (dpcm_path_get(fe, stream, &list) <= 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001958 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001959 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01001960 }
1961
1962 /* calculate valid and active FE <-> BE dpcms */
1963 dpcm_process_paths(fe, stream, &list, 1);
1964
1965 ret = dpcm_fe_dai_startup(fe_substream);
1966 if (ret < 0) {
1967 /* clean up all links */
1968 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1969 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1970
1971 dpcm_be_disconnect(fe, stream);
1972 fe->dpcm[stream].runtime = NULL;
1973 }
1974
1975 dpcm_clear_pending_state(fe, stream);
1976 dpcm_path_put(&list);
1977 mutex_unlock(&fe->card->mutex);
1978 return ret;
1979}
1980
Mark Brown45c0a182012-05-09 21:46:27 +01001981static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001982{
1983 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1984 struct snd_soc_dpcm *dpcm;
1985 int stream = fe_substream->stream, ret;
1986
1987 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1988 ret = dpcm_fe_dai_shutdown(fe_substream);
1989
1990 /* mark FE's links ready to prune */
1991 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1992 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1993
1994 dpcm_be_disconnect(fe, stream);
1995
1996 fe->dpcm[stream].runtime = NULL;
1997 mutex_unlock(&fe->card->mutex);
1998 return ret;
1999}
2000
Liam Girdwoodddee6272011-06-09 14:45:53 +01002001/* create a new pcm */
2002int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2003{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002004 struct snd_soc_platform *platform = rtd->platform;
2005 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2006 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2007 struct snd_pcm *pcm;
2008 char new_name[64];
2009 int ret = 0, playback = 0, capture = 0;
2010
Liam Girdwood01d75842012-04-25 12:12:49 +01002011 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2012 if (cpu_dai->driver->playback.channels_min)
2013 playback = 1;
2014 if (cpu_dai->driver->capture.channels_min)
2015 capture = 1;
2016 } else {
2017 if (codec_dai->driver->playback.channels_min)
2018 playback = 1;
2019 if (codec_dai->driver->capture.channels_min)
2020 capture = 1;
2021 }
Sangsu Parka5002312012-01-02 17:15:10 +09002022
Liam Girdwood01d75842012-04-25 12:12:49 +01002023 /* create the PCM */
2024 if (rtd->dai_link->no_pcm) {
2025 snprintf(new_name, sizeof(new_name), "(%s)",
2026 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002027
Liam Girdwood01d75842012-04-25 12:12:49 +01002028 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2029 playback, capture, &pcm);
2030 } else {
2031 if (rtd->dai_link->dynamic)
2032 snprintf(new_name, sizeof(new_name), "%s (*)",
2033 rtd->dai_link->stream_name);
2034 else
2035 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2036 rtd->dai_link->stream_name, codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002037
Liam Girdwood01d75842012-04-25 12:12:49 +01002038 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2039 capture, &pcm);
2040 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002041 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002042 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002043 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002044 return ret;
2045 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002046 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002047
2048 /* DAPM dai link stream work */
2049 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2050
2051 rtd->pcm = pcm;
2052 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002053
2054 if (rtd->dai_link->no_pcm) {
2055 if (playback)
2056 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2057 if (capture)
2058 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2059 goto out;
2060 }
2061
2062 /* ASoC PCM operations */
2063 if (rtd->dai_link->dynamic) {
2064 rtd->ops.open = dpcm_fe_dai_open;
2065 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2066 rtd->ops.prepare = dpcm_fe_dai_prepare;
2067 rtd->ops.trigger = dpcm_fe_dai_trigger;
2068 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2069 rtd->ops.close = dpcm_fe_dai_close;
2070 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002071 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002072 } else {
2073 rtd->ops.open = soc_pcm_open;
2074 rtd->ops.hw_params = soc_pcm_hw_params;
2075 rtd->ops.prepare = soc_pcm_prepare;
2076 rtd->ops.trigger = soc_pcm_trigger;
2077 rtd->ops.hw_free = soc_pcm_hw_free;
2078 rtd->ops.close = soc_pcm_close;
2079 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002080 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002081 }
2082
Liam Girdwoodddee6272011-06-09 14:45:53 +01002083 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002084 rtd->ops.ack = platform->driver->ops->ack;
2085 rtd->ops.copy = platform->driver->ops->copy;
2086 rtd->ops.silence = platform->driver->ops->silence;
2087 rtd->ops.page = platform->driver->ops->page;
2088 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002089 }
2090
2091 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002092 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002093
2094 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002095 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002096
2097 if (platform->driver->pcm_new) {
2098 ret = platform->driver->pcm_new(rtd);
2099 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002100 dev_err(platform->dev,
2101 "ASoC: pcm constructor failed: %d\n",
2102 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002103 return ret;
2104 }
2105 }
2106
2107 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002108out:
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002109 dev_info(rtd->card->dev, " %s <-> %s mapping ok\n", codec_dai->name,
Liam Girdwoodddee6272011-06-09 14:45:53 +01002110 cpu_dai->name);
2111 return ret;
2112}
Liam Girdwood01d75842012-04-25 12:12:49 +01002113
2114/* is the current PCM operation for this FE ? */
2115int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2116{
2117 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2118 return 1;
2119 return 0;
2120}
2121EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2122
2123/* is the current PCM operation for this BE ? */
2124int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2125 struct snd_soc_pcm_runtime *be, int stream)
2126{
2127 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2128 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2129 be->dpcm[stream].runtime_update))
2130 return 1;
2131 return 0;
2132}
2133EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2134
2135/* get the substream for this BE */
2136struct snd_pcm_substream *
2137 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2138{
2139 return be->pcm->streams[stream].substream;
2140}
2141EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2142
2143/* get the BE runtime state */
2144enum snd_soc_dpcm_state
2145 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2146{
2147 return be->dpcm[stream].state;
2148}
2149EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2150
2151/* set the BE runtime state */
2152void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2153 int stream, enum snd_soc_dpcm_state state)
2154{
2155 be->dpcm[stream].state = state;
2156}
2157EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2158
2159/*
2160 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2161 * are not running, paused or suspended for the specified stream direction.
2162 */
2163int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2164 struct snd_soc_pcm_runtime *be, int stream)
2165{
2166 struct snd_soc_dpcm *dpcm;
2167 int state;
2168
2169 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2170
2171 if (dpcm->fe == fe)
2172 continue;
2173
2174 state = dpcm->fe->dpcm[stream].state;
2175 if (state == SND_SOC_DPCM_STATE_START ||
2176 state == SND_SOC_DPCM_STATE_PAUSED ||
2177 state == SND_SOC_DPCM_STATE_SUSPEND)
2178 return 0;
2179 }
2180
2181 /* it's safe to free/stop this BE DAI */
2182 return 1;
2183}
2184EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2185
2186/*
2187 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2188 * running, paused or suspended for the specified stream direction.
2189 */
2190int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2191 struct snd_soc_pcm_runtime *be, int stream)
2192{
2193 struct snd_soc_dpcm *dpcm;
2194 int state;
2195
2196 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2197
2198 if (dpcm->fe == fe)
2199 continue;
2200
2201 state = dpcm->fe->dpcm[stream].state;
2202 if (state == SND_SOC_DPCM_STATE_START ||
2203 state == SND_SOC_DPCM_STATE_PAUSED ||
2204 state == SND_SOC_DPCM_STATE_SUSPEND ||
2205 state == SND_SOC_DPCM_STATE_PREPARE)
2206 return 0;
2207 }
2208
2209 /* it's safe to change hw_params */
2210 return 1;
2211}
2212EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002213
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002214int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2215 int cmd, struct snd_soc_platform *platform)
2216{
2217 if (platform->driver->ops->trigger)
2218 return platform->driver->ops->trigger(substream, cmd);
2219 return 0;
2220}
2221EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2222
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002223#ifdef CONFIG_DEBUG_FS
2224static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2225{
2226 switch (state) {
2227 case SND_SOC_DPCM_STATE_NEW:
2228 return "new";
2229 case SND_SOC_DPCM_STATE_OPEN:
2230 return "open";
2231 case SND_SOC_DPCM_STATE_HW_PARAMS:
2232 return "hw_params";
2233 case SND_SOC_DPCM_STATE_PREPARE:
2234 return "prepare";
2235 case SND_SOC_DPCM_STATE_START:
2236 return "start";
2237 case SND_SOC_DPCM_STATE_STOP:
2238 return "stop";
2239 case SND_SOC_DPCM_STATE_SUSPEND:
2240 return "suspend";
2241 case SND_SOC_DPCM_STATE_PAUSED:
2242 return "paused";
2243 case SND_SOC_DPCM_STATE_HW_FREE:
2244 return "hw_free";
2245 case SND_SOC_DPCM_STATE_CLOSE:
2246 return "close";
2247 }
2248
2249 return "unknown";
2250}
2251
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002252static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2253 int stream, char *buf, size_t size)
2254{
2255 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2256 struct snd_soc_dpcm *dpcm;
2257 ssize_t offset = 0;
2258
2259 /* FE state */
2260 offset += snprintf(buf + offset, size - offset,
2261 "[%s - %s]\n", fe->dai_link->name,
2262 stream ? "Capture" : "Playback");
2263
2264 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2265 dpcm_state_string(fe->dpcm[stream].state));
2266
2267 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2268 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2269 offset += snprintf(buf + offset, size - offset,
2270 "Hardware Params: "
2271 "Format = %s, Channels = %d, Rate = %d\n",
2272 snd_pcm_format_name(params_format(params)),
2273 params_channels(params),
2274 params_rate(params));
2275
2276 /* BEs state */
2277 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2278
2279 if (list_empty(&fe->dpcm[stream].be_clients)) {
2280 offset += snprintf(buf + offset, size - offset,
2281 " No active DSP links\n");
2282 goto out;
2283 }
2284
2285 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2286 struct snd_soc_pcm_runtime *be = dpcm->be;
2287 params = &dpcm->hw_params;
2288
2289 offset += snprintf(buf + offset, size - offset,
2290 "- %s\n", be->dai_link->name);
2291
2292 offset += snprintf(buf + offset, size - offset,
2293 " State: %s\n",
2294 dpcm_state_string(be->dpcm[stream].state));
2295
2296 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2297 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2298 offset += snprintf(buf + offset, size - offset,
2299 " Hardware Params: "
2300 "Format = %s, Channels = %d, Rate = %d\n",
2301 snd_pcm_format_name(params_format(params)),
2302 params_channels(params),
2303 params_rate(params));
2304 }
2305
2306out:
2307 return offset;
2308}
2309
2310static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2311 size_t count, loff_t *ppos)
2312{
2313 struct snd_soc_pcm_runtime *fe = file->private_data;
2314 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2315 char *buf;
2316
2317 buf = kmalloc(out_count, GFP_KERNEL);
2318 if (!buf)
2319 return -ENOMEM;
2320
2321 if (fe->cpu_dai->driver->playback.channels_min)
2322 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2323 buf + offset, out_count - offset);
2324
2325 if (fe->cpu_dai->driver->capture.channels_min)
2326 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2327 buf + offset, out_count - offset);
2328
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002329 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002330
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002331 kfree(buf);
2332 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002333}
2334
2335static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002336 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002337 .read = dpcm_state_read_file,
2338 .llseek = default_llseek,
2339};
2340
2341int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2342{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002343 if (!rtd->dai_link)
2344 return 0;
2345
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002346 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2347 rtd->card->debugfs_card_root);
2348 if (!rtd->debugfs_dpcm_root) {
2349 dev_dbg(rtd->dev,
2350 "ASoC: Failed to create dpcm debugfs directory %s\n",
2351 rtd->dai_link->name);
2352 return -EINVAL;
2353 }
2354
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002355 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002356 rtd->debugfs_dpcm_root,
2357 rtd, &dpcm_state_fops);
2358
2359 return 0;
2360}
2361#endif