blob: 330c9a6b5cb5bf9810da6afd76a6cc8155b13845 [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;
Mark Brownd4e1a732013-07-18 11:52:17 +0100414 queue_delayed_work(system_power_efficient_wq,
415 &rtd->delayed_work,
416 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300417 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100418 } else {
419 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800420 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000421 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100422 }
423
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100424 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000425
426 pm_runtime_put(platform->dev);
427 pm_runtime_put(codec_dai->dev);
428 pm_runtime_put(cpu_dai->dev);
429
Liam Girdwoodddee6272011-06-09 14:45:53 +0100430 return 0;
431}
432
433/*
434 * Called by ALSA when the PCM substream is prepared, can set format, sample
435 * rate, etc. This function is non atomic and can be called multiple times,
436 * it can refer to the runtime info.
437 */
438static int soc_pcm_prepare(struct snd_pcm_substream *substream)
439{
440 struct snd_soc_pcm_runtime *rtd = substream->private_data;
441 struct snd_soc_platform *platform = rtd->platform;
442 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
443 struct snd_soc_dai *codec_dai = rtd->codec_dai;
444 int ret = 0;
445
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100446 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100447
448 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
449 ret = rtd->dai_link->ops->prepare(substream);
450 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000451 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
452 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100453 goto out;
454 }
455 }
456
457 if (platform->driver->ops && platform->driver->ops->prepare) {
458 ret = platform->driver->ops->prepare(substream);
459 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000460 dev_err(platform->dev, "ASoC: platform prepare error:"
461 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100462 goto out;
463 }
464 }
465
466 if (codec_dai->driver->ops->prepare) {
467 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
468 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000469 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000470 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100471 goto out;
472 }
473 }
474
475 if (cpu_dai->driver->ops->prepare) {
476 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
477 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000478 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000479 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100480 goto out;
481 }
482 }
483
484 /* cancel any delayed stream shutdown that is pending */
485 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600486 rtd->pop_wait) {
487 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100488 cancel_delayed_work(&rtd->delayed_work);
489 }
490
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000491 snd_soc_dapm_stream_event(rtd, substream->stream,
492 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100493
Mark Brownda183962013-02-06 15:44:07 +0000494 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100495
496out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100497 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100498 return ret;
499}
500
501/*
502 * Called by ALSA when the hardware params are set by application. This
503 * function can also be called multiple times and can allocate buffers
504 * (using snd_pcm_lib_* ). It's non-atomic.
505 */
506static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
507 struct snd_pcm_hw_params *params)
508{
509 struct snd_soc_pcm_runtime *rtd = substream->private_data;
510 struct snd_soc_platform *platform = rtd->platform;
511 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
512 struct snd_soc_dai *codec_dai = rtd->codec_dai;
513 int ret = 0;
514
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100515 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100516
517 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
518 ret = rtd->dai_link->ops->hw_params(substream, params);
519 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000520 dev_err(rtd->card->dev, "ASoC: machine hw_params"
521 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100522 goto out;
523 }
524 }
525
526 if (codec_dai->driver->ops->hw_params) {
527 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
528 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000529 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
530 " %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100531 goto codec_err;
532 }
533 }
534
535 if (cpu_dai->driver->ops->hw_params) {
536 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
537 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000538 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000539 cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100540 goto interface_err;
541 }
542 }
543
544 if (platform->driver->ops && platform->driver->ops->hw_params) {
545 ret = platform->driver->ops->hw_params(substream, params);
546 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000547 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000548 platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100549 goto platform_err;
550 }
551 }
552
Dong Aisheng17841022011-08-29 17:15:14 +0800553 /* store the rate for each DAIs */
554 cpu_dai->rate = params_rate(params);
555 codec_dai->rate = params_rate(params);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100556
557out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100558 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100559 return ret;
560
561platform_err:
562 if (cpu_dai->driver->ops->hw_free)
563 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
564
565interface_err:
566 if (codec_dai->driver->ops->hw_free)
567 codec_dai->driver->ops->hw_free(substream, codec_dai);
568
569codec_err:
570 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
571 rtd->dai_link->ops->hw_free(substream);
572
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100573 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100574 return ret;
575}
576
577/*
578 * Frees resources allocated by hw_params, can be called multiple times
579 */
580static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
581{
582 struct snd_soc_pcm_runtime *rtd = substream->private_data;
583 struct snd_soc_platform *platform = rtd->platform;
584 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
585 struct snd_soc_dai *codec_dai = rtd->codec_dai;
586 struct snd_soc_codec *codec = rtd->codec;
587
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100588 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100589
590 /* apply codec digital mute */
591 if (!codec->active)
Mark Brownda183962013-02-06 15:44:07 +0000592 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100593
594 /* free any machine hw params */
595 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
596 rtd->dai_link->ops->hw_free(substream);
597
598 /* free any DMA resources */
599 if (platform->driver->ops && platform->driver->ops->hw_free)
600 platform->driver->ops->hw_free(substream);
601
602 /* now free hw params for the DAIs */
603 if (codec_dai->driver->ops->hw_free)
604 codec_dai->driver->ops->hw_free(substream, codec_dai);
605
606 if (cpu_dai->driver->ops->hw_free)
607 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
608
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100609 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100610 return 0;
611}
612
613static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
614{
615 struct snd_soc_pcm_runtime *rtd = substream->private_data;
616 struct snd_soc_platform *platform = rtd->platform;
617 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
618 struct snd_soc_dai *codec_dai = rtd->codec_dai;
619 int ret;
620
621 if (codec_dai->driver->ops->trigger) {
622 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
623 if (ret < 0)
624 return ret;
625 }
626
627 if (platform->driver->ops && platform->driver->ops->trigger) {
628 ret = platform->driver->ops->trigger(substream, cmd);
629 if (ret < 0)
630 return ret;
631 }
632
633 if (cpu_dai->driver->ops->trigger) {
634 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
635 if (ret < 0)
636 return ret;
637 }
638 return 0;
639}
640
Mark Brown45c0a182012-05-09 21:46:27 +0100641static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
642 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100643{
644 struct snd_soc_pcm_runtime *rtd = substream->private_data;
645 struct snd_soc_platform *platform = rtd->platform;
646 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
647 struct snd_soc_dai *codec_dai = rtd->codec_dai;
648 int ret;
649
650 if (codec_dai->driver->ops->bespoke_trigger) {
651 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
652 if (ret < 0)
653 return ret;
654 }
655
656 if (platform->driver->bespoke_trigger) {
657 ret = platform->driver->bespoke_trigger(substream, cmd);
658 if (ret < 0)
659 return ret;
660 }
661
662 if (cpu_dai->driver->ops->bespoke_trigger) {
663 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
664 if (ret < 0)
665 return ret;
666 }
667 return 0;
668}
Liam Girdwoodddee6272011-06-09 14:45:53 +0100669/*
670 * soc level wrapper for pointer callback
671 * If cpu_dai, codec_dai, platform driver has the delay callback, than
672 * the runtime->delay will be updated accordingly.
673 */
674static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
675{
676 struct snd_soc_pcm_runtime *rtd = substream->private_data;
677 struct snd_soc_platform *platform = rtd->platform;
678 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
679 struct snd_soc_dai *codec_dai = rtd->codec_dai;
680 struct snd_pcm_runtime *runtime = substream->runtime;
681 snd_pcm_uframes_t offset = 0;
682 snd_pcm_sframes_t delay = 0;
683
684 if (platform->driver->ops && platform->driver->ops->pointer)
685 offset = platform->driver->ops->pointer(substream);
686
687 if (cpu_dai->driver->ops->delay)
688 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
689
690 if (codec_dai->driver->ops->delay)
691 delay += codec_dai->driver->ops->delay(substream, codec_dai);
692
693 if (platform->driver->delay)
694 delay += platform->driver->delay(substream, codec_dai);
695
696 runtime->delay = delay;
697
698 return offset;
699}
700
Liam Girdwood01d75842012-04-25 12:12:49 +0100701/* connect a FE and BE */
702static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
703 struct snd_soc_pcm_runtime *be, int stream)
704{
705 struct snd_soc_dpcm *dpcm;
706
707 /* only add new dpcms */
708 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
709 if (dpcm->be == be && dpcm->fe == fe)
710 return 0;
711 }
712
713 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
714 if (!dpcm)
715 return -ENOMEM;
716
717 dpcm->be = be;
718 dpcm->fe = fe;
719 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
720 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
721 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
722 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
723
724 dev_dbg(fe->dev, " connected new DPCM %s path %s %s %s\n",
725 stream ? "capture" : "playback", fe->dai_link->name,
726 stream ? "<-" : "->", be->dai_link->name);
727
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100728#ifdef CONFIG_DEBUG_FS
729 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
730 fe->debugfs_dpcm_root, &dpcm->state);
731#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100732 return 1;
733}
734
735/* reparent a BE onto another FE */
736static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
737 struct snd_soc_pcm_runtime *be, int stream)
738{
739 struct snd_soc_dpcm *dpcm;
740 struct snd_pcm_substream *fe_substream, *be_substream;
741
742 /* reparent if BE is connected to other FEs */
743 if (!be->dpcm[stream].users)
744 return;
745
746 be_substream = snd_soc_dpcm_get_substream(be, stream);
747
748 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
749 if (dpcm->fe == fe)
750 continue;
751
752 dev_dbg(fe->dev, " reparent %s path %s %s %s\n",
753 stream ? "capture" : "playback",
754 dpcm->fe->dai_link->name,
755 stream ? "<-" : "->", dpcm->be->dai_link->name);
756
757 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
758 be_substream->runtime = fe_substream->runtime;
759 break;
760 }
761}
762
763/* disconnect a BE and FE */
764static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
765{
766 struct snd_soc_dpcm *dpcm, *d;
767
768 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000769 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100770 stream ? "capture" : "playback",
771 dpcm->be->dai_link->name);
772
773 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
774 continue;
775
776 dev_dbg(fe->dev, " freed DSP %s path %s %s %s\n",
777 stream ? "capture" : "playback", fe->dai_link->name,
778 stream ? "<-" : "->", dpcm->be->dai_link->name);
779
780 /* BEs still alive need new FE */
781 dpcm_be_reparent(fe, dpcm->be, stream);
782
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100783#ifdef CONFIG_DEBUG_FS
784 debugfs_remove(dpcm->debugfs_state);
785#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100786 list_del(&dpcm->list_be);
787 list_del(&dpcm->list_fe);
788 kfree(dpcm);
789 }
790}
791
792/* get BE for DAI widget and stream */
793static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
794 struct snd_soc_dapm_widget *widget, int stream)
795{
796 struct snd_soc_pcm_runtime *be;
797 int i;
798
799 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
800 for (i = 0; i < card->num_links; i++) {
801 be = &card->rtd[i];
802
Liam Girdwood35ea0652012-06-05 19:26:59 +0100803 if (!be->dai_link->no_pcm)
804 continue;
805
Liam Girdwood01d75842012-04-25 12:12:49 +0100806 if (be->cpu_dai->playback_widget == widget ||
807 be->codec_dai->playback_widget == widget)
808 return be;
809 }
810 } else {
811
812 for (i = 0; i < card->num_links; i++) {
813 be = &card->rtd[i];
814
Liam Girdwood35ea0652012-06-05 19:26:59 +0100815 if (!be->dai_link->no_pcm)
816 continue;
817
Liam Girdwood01d75842012-04-25 12:12:49 +0100818 if (be->cpu_dai->capture_widget == widget ||
819 be->codec_dai->capture_widget == widget)
820 return be;
821 }
822 }
823
Liam Girdwood103d84a2012-11-19 14:39:15 +0000824 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100825 stream ? "capture" : "playback", widget->name);
826 return NULL;
827}
828
829static inline struct snd_soc_dapm_widget *
830 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
831{
832 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
833 return rtd->cpu_dai->playback_widget;
834 else
835 return rtd->cpu_dai->capture_widget;
836}
837
838static inline struct snd_soc_dapm_widget *
839 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
840{
841 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
842 return rtd->codec_dai->playback_widget;
843 else
844 return rtd->codec_dai->capture_widget;
845}
846
847static int widget_in_list(struct snd_soc_dapm_widget_list *list,
848 struct snd_soc_dapm_widget *widget)
849{
850 int i;
851
852 for (i = 0; i < list->num_widgets; i++) {
853 if (widget == list->widgets[i])
854 return 1;
855 }
856
857 return 0;
858}
859
860static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
861 int stream, struct snd_soc_dapm_widget_list **list_)
862{
863 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
864 struct snd_soc_dapm_widget_list *list;
865 int paths;
866
867 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
868 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
869 if (list == NULL)
870 return -ENOMEM;
871
872 /* get number of valid DAI paths and their widgets */
873 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
874
Liam Girdwood103d84a2012-11-19 14:39:15 +0000875 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +0100876 stream ? "capture" : "playback");
877
878 *list_ = list;
879 return paths;
880}
881
882static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
883{
884 kfree(*list);
885}
886
887static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
888 struct snd_soc_dapm_widget_list **list_)
889{
890 struct snd_soc_dpcm *dpcm;
891 struct snd_soc_dapm_widget_list *list = *list_;
892 struct snd_soc_dapm_widget *widget;
893 int prune = 0;
894
895 /* Destroy any old FE <--> BE connections */
896 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
897
898 /* is there a valid CPU DAI widget for this BE */
899 widget = rtd_get_cpu_widget(dpcm->be, stream);
900
901 /* prune the BE if it's no longer in our active list */
902 if (widget && widget_in_list(list, widget))
903 continue;
904
905 /* is there a valid CODEC DAI widget for this BE */
906 widget = rtd_get_codec_widget(dpcm->be, stream);
907
908 /* prune the BE if it's no longer in our active list */
909 if (widget && widget_in_list(list, widget))
910 continue;
911
Liam Girdwood103d84a2012-11-19 14:39:15 +0000912 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100913 stream ? "capture" : "playback",
914 dpcm->be->dai_link->name, fe->dai_link->name);
915 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
916 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
917 prune++;
918 }
919
Liam Girdwood103d84a2012-11-19 14:39:15 +0000920 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +0100921 return prune;
922}
923
924static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
925 struct snd_soc_dapm_widget_list **list_)
926{
927 struct snd_soc_card *card = fe->card;
928 struct snd_soc_dapm_widget_list *list = *list_;
929 struct snd_soc_pcm_runtime *be;
930 int i, new = 0, err;
931
932 /* Create any new FE <--> BE connections */
933 for (i = 0; i < list->num_widgets; i++) {
934
Mark Brown46162742013-06-05 19:36:11 +0100935 switch (list->widgets[i]->id) {
936 case snd_soc_dapm_dai_in:
937 case snd_soc_dapm_dai_out:
938 break;
939 default:
Liam Girdwood01d75842012-04-25 12:12:49 +0100940 continue;
Mark Brown46162742013-06-05 19:36:11 +0100941 }
Liam Girdwood01d75842012-04-25 12:12:49 +0100942
943 /* is there a valid BE rtd for this widget */
944 be = dpcm_get_be(card, list->widgets[i], stream);
945 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000946 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100947 list->widgets[i]->name);
948 continue;
949 }
950
951 /* make sure BE is a real BE */
952 if (!be->dai_link->no_pcm)
953 continue;
954
955 /* don't connect if FE is not running */
956 if (!fe->dpcm[stream].runtime)
957 continue;
958
959 /* newly connected FE and BE */
960 err = dpcm_be_connect(fe, be, stream);
961 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000962 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100963 list->widgets[i]->name);
964 break;
965 } else if (err == 0) /* already connected */
966 continue;
967
968 /* new */
969 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
970 new++;
971 }
972
Liam Girdwood103d84a2012-11-19 14:39:15 +0000973 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +0100974 return new;
975}
976
977/*
978 * Find the corresponding BE DAIs that source or sink audio to this
979 * FE substream.
980 */
981static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
982 int stream, struct snd_soc_dapm_widget_list **list, int new)
983{
984 if (new)
985 return dpcm_add_paths(fe, stream, list);
986 else
987 return dpcm_prune_paths(fe, stream, list);
988}
989
990static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
991{
992 struct snd_soc_dpcm *dpcm;
993
994 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
995 dpcm->be->dpcm[stream].runtime_update =
996 SND_SOC_DPCM_UPDATE_NO;
997}
998
999static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1000 int stream)
1001{
1002 struct snd_soc_dpcm *dpcm;
1003
1004 /* disable any enabled and non active backends */
1005 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1006
1007 struct snd_soc_pcm_runtime *be = dpcm->be;
1008 struct snd_pcm_substream *be_substream =
1009 snd_soc_dpcm_get_substream(be, stream);
1010
1011 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001012 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001013 stream ? "capture" : "playback",
1014 be->dpcm[stream].state);
1015
1016 if (--be->dpcm[stream].users != 0)
1017 continue;
1018
1019 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1020 continue;
1021
1022 soc_pcm_close(be_substream);
1023 be_substream->runtime = NULL;
1024 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1025 }
1026}
1027
1028static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1029{
1030 struct snd_soc_dpcm *dpcm;
1031 int err, count = 0;
1032
1033 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1034 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1035
1036 struct snd_soc_pcm_runtime *be = dpcm->be;
1037 struct snd_pcm_substream *be_substream =
1038 snd_soc_dpcm_get_substream(be, stream);
1039
1040 /* is this op for this BE ? */
1041 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1042 continue;
1043
1044 /* first time the dpcm is open ? */
1045 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001046 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001047 stream ? "capture" : "playback",
1048 be->dpcm[stream].state);
1049
1050 if (be->dpcm[stream].users++ != 0)
1051 continue;
1052
1053 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1054 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1055 continue;
1056
Liam Girdwood103d84a2012-11-19 14:39:15 +00001057 dev_dbg(be->dev, "ASoC: open BE %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001058
1059 be_substream->runtime = be->dpcm[stream].runtime;
1060 err = soc_pcm_open(be_substream);
1061 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001062 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001063 be->dpcm[stream].users--;
1064 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001065 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001066 stream ? "capture" : "playback",
1067 be->dpcm[stream].state);
1068
1069 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1070 goto unwind;
1071 }
1072
1073 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1074 count++;
1075 }
1076
1077 return count;
1078
1079unwind:
1080 /* disable any enabled and non active backends */
1081 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1082 struct snd_soc_pcm_runtime *be = dpcm->be;
1083 struct snd_pcm_substream *be_substream =
1084 snd_soc_dpcm_get_substream(be, stream);
1085
1086 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1087 continue;
1088
1089 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001090 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001091 stream ? "capture" : "playback",
1092 be->dpcm[stream].state);
1093
1094 if (--be->dpcm[stream].users != 0)
1095 continue;
1096
1097 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1098 continue;
1099
1100 soc_pcm_close(be_substream);
1101 be_substream->runtime = NULL;
1102 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1103 }
1104
1105 return err;
1106}
1107
Mark Brown45c0a182012-05-09 21:46:27 +01001108static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001109{
1110 struct snd_pcm_runtime *runtime = substream->runtime;
1111 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1112 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1113 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1114
1115 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1116 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1117 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1118 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1119 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1120 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1121 runtime->hw.rates = cpu_dai_drv->playback.rates;
1122 } else {
1123 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1124 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1125 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1126 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1127 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1128 runtime->hw.rates = cpu_dai_drv->capture.rates;
1129 }
1130}
1131
1132static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1133{
1134 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1135 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1136 int stream = fe_substream->stream, ret = 0;
1137
1138 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1139
1140 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1141 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001142 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001143 goto be_err;
1144 }
1145
Liam Girdwood103d84a2012-11-19 14:39:15 +00001146 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001147
1148 /* start the DAI frontend */
1149 ret = soc_pcm_open(fe_substream);
1150 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001151 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001152 goto unwind;
1153 }
1154
1155 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1156
1157 dpcm_set_fe_runtime(fe_substream);
1158 snd_pcm_limit_hw_rates(runtime);
1159
1160 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1161 return 0;
1162
1163unwind:
1164 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1165be_err:
1166 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1167 return ret;
1168}
1169
1170static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1171{
1172 struct snd_soc_dpcm *dpcm;
1173
1174 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1175 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1176
1177 struct snd_soc_pcm_runtime *be = dpcm->be;
1178 struct snd_pcm_substream *be_substream =
1179 snd_soc_dpcm_get_substream(be, stream);
1180
1181 /* is this op for this BE ? */
1182 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1183 continue;
1184
1185 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001186 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001187 stream ? "capture" : "playback",
1188 be->dpcm[stream].state);
1189
1190 if (--be->dpcm[stream].users != 0)
1191 continue;
1192
1193 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1194 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1195 continue;
1196
Liam Girdwood103d84a2012-11-19 14:39:15 +00001197 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001198 dpcm->fe->dai_link->name);
1199
1200 soc_pcm_close(be_substream);
1201 be_substream->runtime = NULL;
1202
1203 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1204 }
1205 return 0;
1206}
1207
1208static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1209{
1210 struct snd_soc_pcm_runtime *fe = substream->private_data;
1211 int stream = substream->stream;
1212
1213 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1214
1215 /* shutdown the BEs */
1216 dpcm_be_dai_shutdown(fe, substream->stream);
1217
Liam Girdwood103d84a2012-11-19 14:39:15 +00001218 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001219
1220 /* now shutdown the frontend */
1221 soc_pcm_close(substream);
1222
1223 /* run the stream event for each BE */
1224 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1225
1226 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1227 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1228 return 0;
1229}
1230
1231static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1232{
1233 struct snd_soc_dpcm *dpcm;
1234
1235 /* only hw_params backends that are either sinks or sources
1236 * to this frontend DAI */
1237 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1238
1239 struct snd_soc_pcm_runtime *be = dpcm->be;
1240 struct snd_pcm_substream *be_substream =
1241 snd_soc_dpcm_get_substream(be, stream);
1242
1243 /* is this op for this BE ? */
1244 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1245 continue;
1246
1247 /* only free hw when no longer used - check all FEs */
1248 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1249 continue;
1250
1251 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1252 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1253 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001254 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001255 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1256 continue;
1257
Liam Girdwood103d84a2012-11-19 14:39:15 +00001258 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001259 dpcm->fe->dai_link->name);
1260
1261 soc_pcm_hw_free(be_substream);
1262
1263 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1264 }
1265
1266 return 0;
1267}
1268
Mark Brown45c0a182012-05-09 21:46:27 +01001269static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001270{
1271 struct snd_soc_pcm_runtime *fe = substream->private_data;
1272 int err, stream = substream->stream;
1273
1274 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1275 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1276
Liam Girdwood103d84a2012-11-19 14:39:15 +00001277 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001278
1279 /* call hw_free on the frontend */
1280 err = soc_pcm_hw_free(substream);
1281 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001282 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001283 fe->dai_link->name);
1284
1285 /* only hw_params backends that are either sinks or sources
1286 * to this frontend DAI */
1287 err = dpcm_be_dai_hw_free(fe, stream);
1288
1289 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1290 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1291
1292 mutex_unlock(&fe->card->mutex);
1293 return 0;
1294}
1295
1296static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1297{
1298 struct snd_soc_dpcm *dpcm;
1299 int ret;
1300
1301 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1302
1303 struct snd_soc_pcm_runtime *be = dpcm->be;
1304 struct snd_pcm_substream *be_substream =
1305 snd_soc_dpcm_get_substream(be, stream);
1306
1307 /* is this op for this BE ? */
1308 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1309 continue;
1310
1311 /* only allow hw_params() if no connected FEs are running */
1312 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1313 continue;
1314
1315 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1316 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1317 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1318 continue;
1319
Liam Girdwood103d84a2012-11-19 14:39:15 +00001320 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001321 dpcm->fe->dai_link->name);
1322
1323 /* copy params for each dpcm */
1324 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1325 sizeof(struct snd_pcm_hw_params));
1326
1327 /* perform any hw_params fixups */
1328 if (be->dai_link->be_hw_params_fixup) {
1329 ret = be->dai_link->be_hw_params_fixup(be,
1330 &dpcm->hw_params);
1331 if (ret < 0) {
1332 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001333 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001334 ret);
1335 goto unwind;
1336 }
1337 }
1338
1339 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1340 if (ret < 0) {
1341 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001342 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001343 goto unwind;
1344 }
1345
1346 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1347 }
1348 return 0;
1349
1350unwind:
1351 /* disable any enabled and non active backends */
1352 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1353 struct snd_soc_pcm_runtime *be = dpcm->be;
1354 struct snd_pcm_substream *be_substream =
1355 snd_soc_dpcm_get_substream(be, stream);
1356
1357 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1358 continue;
1359
1360 /* only allow hw_free() if no connected FEs are running */
1361 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1362 continue;
1363
1364 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1365 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1366 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1367 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1368 continue;
1369
1370 soc_pcm_hw_free(be_substream);
1371 }
1372
1373 return ret;
1374}
1375
Mark Brown45c0a182012-05-09 21:46:27 +01001376static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1377 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001378{
1379 struct snd_soc_pcm_runtime *fe = substream->private_data;
1380 int ret, stream = substream->stream;
1381
1382 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1383 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1384
1385 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1386 sizeof(struct snd_pcm_hw_params));
1387 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1388 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001389 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001390 goto out;
1391 }
1392
Liam Girdwood103d84a2012-11-19 14:39:15 +00001393 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001394 fe->dai_link->name, params_rate(params),
1395 params_channels(params), params_format(params));
1396
1397 /* call hw_params on the frontend */
1398 ret = soc_pcm_hw_params(substream, params);
1399 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001400 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001401 dpcm_be_dai_hw_free(fe, stream);
1402 } else
1403 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1404
1405out:
1406 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1407 mutex_unlock(&fe->card->mutex);
1408 return ret;
1409}
1410
1411static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1412 struct snd_pcm_substream *substream, int cmd)
1413{
1414 int ret;
1415
Liam Girdwood103d84a2012-11-19 14:39:15 +00001416 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001417 dpcm->fe->dai_link->name, cmd);
1418
1419 ret = soc_pcm_trigger(substream, cmd);
1420 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001421 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001422
1423 return ret;
1424}
1425
Mark Brown45c0a182012-05-09 21:46:27 +01001426static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1427 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001428{
1429 struct snd_soc_dpcm *dpcm;
1430 int ret = 0;
1431
1432 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1433
1434 struct snd_soc_pcm_runtime *be = dpcm->be;
1435 struct snd_pcm_substream *be_substream =
1436 snd_soc_dpcm_get_substream(be, stream);
1437
1438 /* is this op for this BE ? */
1439 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1440 continue;
1441
1442 switch (cmd) {
1443 case SNDRV_PCM_TRIGGER_START:
1444 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1445 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1446 continue;
1447
1448 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1449 if (ret)
1450 return ret;
1451
1452 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1453 break;
1454 case SNDRV_PCM_TRIGGER_RESUME:
1455 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1456 continue;
1457
1458 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1459 if (ret)
1460 return ret;
1461
1462 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1463 break;
1464 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1465 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1466 continue;
1467
1468 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1469 if (ret)
1470 return ret;
1471
1472 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1473 break;
1474 case SNDRV_PCM_TRIGGER_STOP:
1475 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1476 continue;
1477
1478 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1479 continue;
1480
1481 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1482 if (ret)
1483 return ret;
1484
1485 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1486 break;
1487 case SNDRV_PCM_TRIGGER_SUSPEND:
1488 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1489 continue;
1490
1491 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1492 continue;
1493
1494 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1495 if (ret)
1496 return ret;
1497
1498 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1499 break;
1500 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1501 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1502 continue;
1503
1504 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1505 continue;
1506
1507 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1508 if (ret)
1509 return ret;
1510
1511 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1512 break;
1513 }
1514 }
1515
1516 return ret;
1517}
1518EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1519
Mark Brown45c0a182012-05-09 21:46:27 +01001520static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001521{
1522 struct snd_soc_pcm_runtime *fe = substream->private_data;
1523 int stream = substream->stream, ret;
1524 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1525
1526 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1527
1528 switch (trigger) {
1529 case SND_SOC_DPCM_TRIGGER_PRE:
1530 /* call trigger on the frontend before the backend. */
1531
Liam Girdwood103d84a2012-11-19 14:39:15 +00001532 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001533 fe->dai_link->name, cmd);
1534
1535 ret = soc_pcm_trigger(substream, cmd);
1536 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001537 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001538 goto out;
1539 }
1540
1541 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1542 break;
1543 case SND_SOC_DPCM_TRIGGER_POST:
1544 /* call trigger on the frontend after the backend. */
1545
1546 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1547 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001548 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001549 goto out;
1550 }
1551
Liam Girdwood103d84a2012-11-19 14:39:15 +00001552 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001553 fe->dai_link->name, cmd);
1554
1555 ret = soc_pcm_trigger(substream, cmd);
1556 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001557 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1558 /* bespoke trigger() - handles both FE and BEs */
1559
Liam Girdwood103d84a2012-11-19 14:39:15 +00001560 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001561 fe->dai_link->name, cmd);
1562
1563 ret = soc_pcm_bespoke_trigger(substream, cmd);
1564 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001565 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001566 goto out;
1567 }
1568 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01001569 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00001570 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01001571 fe->dai_link->name);
1572 ret = -EINVAL;
1573 goto out;
1574 }
1575
1576 switch (cmd) {
1577 case SNDRV_PCM_TRIGGER_START:
1578 case SNDRV_PCM_TRIGGER_RESUME:
1579 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1580 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1581 break;
1582 case SNDRV_PCM_TRIGGER_STOP:
1583 case SNDRV_PCM_TRIGGER_SUSPEND:
1584 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1585 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1586 break;
1587 }
1588
1589out:
1590 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1591 return ret;
1592}
1593
1594static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1595{
1596 struct snd_soc_dpcm *dpcm;
1597 int ret = 0;
1598
1599 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1600
1601 struct snd_soc_pcm_runtime *be = dpcm->be;
1602 struct snd_pcm_substream *be_substream =
1603 snd_soc_dpcm_get_substream(be, stream);
1604
1605 /* is this op for this BE ? */
1606 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1607 continue;
1608
1609 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1610 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1611 continue;
1612
Liam Girdwood103d84a2012-11-19 14:39:15 +00001613 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001614 dpcm->fe->dai_link->name);
1615
1616 ret = soc_pcm_prepare(be_substream);
1617 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001618 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001619 ret);
1620 break;
1621 }
1622
1623 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1624 }
1625 return ret;
1626}
1627
Mark Brown45c0a182012-05-09 21:46:27 +01001628static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001629{
1630 struct snd_soc_pcm_runtime *fe = substream->private_data;
1631 int stream = substream->stream, ret = 0;
1632
1633 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1634
Liam Girdwood103d84a2012-11-19 14:39:15 +00001635 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001636
1637 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1638
1639 /* there is no point preparing this FE if there are no BEs */
1640 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001641 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001642 fe->dai_link->name);
1643 ret = -EINVAL;
1644 goto out;
1645 }
1646
1647 ret = dpcm_be_dai_prepare(fe, substream->stream);
1648 if (ret < 0)
1649 goto out;
1650
1651 /* call prepare on the frontend */
1652 ret = soc_pcm_prepare(substream);
1653 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001654 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001655 fe->dai_link->name);
1656 goto out;
1657 }
1658
1659 /* run the stream event for each BE */
1660 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1661 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1662
1663out:
1664 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1665 mutex_unlock(&fe->card->mutex);
1666
1667 return ret;
1668}
1669
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001670static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1671 unsigned int cmd, void *arg)
1672{
1673 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1674 struct snd_soc_platform *platform = rtd->platform;
1675
1676 if (platform->driver->ops->ioctl)
1677 return platform->driver->ops->ioctl(substream, cmd, arg);
1678 return snd_pcm_lib_ioctl(substream, cmd, arg);
1679}
1680
Liam Girdwood618dae12012-04-25 12:12:51 +01001681static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1682{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001683 struct snd_pcm_substream *substream =
1684 snd_soc_dpcm_get_substream(fe, stream);
1685 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001686 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01001687
Liam Girdwood103d84a2012-11-19 14:39:15 +00001688 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001689 stream ? "capture" : "playback", fe->dai_link->name);
1690
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001691 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1692 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001693 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001694 fe->dai_link->name);
1695
1696 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1697 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001698 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001699 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001700 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001701 fe->dai_link->name);
1702
1703 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1704 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001705 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001706 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001707
1708 err = dpcm_be_dai_hw_free(fe, stream);
1709 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001710 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001711
1712 err = dpcm_be_dai_shutdown(fe, stream);
1713 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001714 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001715
1716 /* run the stream event for each BE */
1717 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1718
1719 return 0;
1720}
1721
1722static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1723{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001724 struct snd_pcm_substream *substream =
1725 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01001726 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001727 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001728 int ret;
1729
Liam Girdwood103d84a2012-11-19 14:39:15 +00001730 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001731 stream ? "capture" : "playback", fe->dai_link->name);
1732
1733 /* Only start the BE if the FE is ready */
1734 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1735 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1736 return -EINVAL;
1737
1738 /* startup must always be called for new BEs */
1739 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001740 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001741 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01001742
1743 /* keep going if FE state is > open */
1744 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1745 return 0;
1746
1747 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001748 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001749 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01001750
1751 /* keep going if FE state is > hw_params */
1752 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1753 return 0;
1754
1755
1756 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001757 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001758 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01001759
1760 /* run the stream event for each BE */
1761 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1762
1763 /* keep going if FE state is > prepare */
1764 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1765 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1766 return 0;
1767
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001768 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1769 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001770 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001771 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01001772
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001773 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1774 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001775 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001776 goto hw_free;
1777 }
1778 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001779 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001780 fe->dai_link->name);
1781
1782 ret = dpcm_be_dai_trigger(fe, stream,
1783 SNDRV_PCM_TRIGGER_START);
1784 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001785 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001786 goto hw_free;
1787 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001788 }
1789
1790 return 0;
1791
1792hw_free:
1793 dpcm_be_dai_hw_free(fe, stream);
1794close:
1795 dpcm_be_dai_shutdown(fe, stream);
1796disconnect:
1797 /* disconnect any non started BEs */
1798 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1799 struct snd_soc_pcm_runtime *be = dpcm->be;
1800 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1801 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1802 }
1803
1804 return ret;
1805}
1806
1807static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1808{
1809 int ret;
1810
1811 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1812 ret = dpcm_run_update_startup(fe, stream);
1813 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001814 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001815 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1816
1817 return ret;
1818}
1819
1820static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1821{
1822 int ret;
1823
1824 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1825 ret = dpcm_run_update_shutdown(fe, stream);
1826 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001827 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001828 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1829
1830 return ret;
1831}
1832
1833/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1834 * any DAI links.
1835 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02001836int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01001837{
Liam Girdwood618dae12012-04-25 12:12:51 +01001838 int i, old, new, paths;
1839
Liam Girdwood618dae12012-04-25 12:12:51 +01001840 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1841 for (i = 0; i < card->num_rtd; i++) {
1842 struct snd_soc_dapm_widget_list *list;
1843 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1844
1845 /* make sure link is FE */
1846 if (!fe->dai_link->dynamic)
1847 continue;
1848
1849 /* only check active links */
1850 if (!fe->cpu_dai->active)
1851 continue;
1852
1853 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001854 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001855 fe->dai_link->name);
1856
1857 /* skip if FE doesn't have playback capability */
1858 if (!fe->cpu_dai->driver->playback.channels_min)
1859 goto capture;
1860
1861 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1862 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001863 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001864 fe->dai_link->name, "playback");
1865 mutex_unlock(&card->mutex);
1866 return paths;
1867 }
1868
1869 /* update any new playback paths */
1870 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1871 if (new) {
1872 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1873 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1874 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1875 }
1876
1877 /* update any old playback paths */
1878 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1879 if (old) {
1880 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1881 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1882 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1883 }
1884
1885capture:
1886 /* skip if FE doesn't have capture capability */
1887 if (!fe->cpu_dai->driver->capture.channels_min)
1888 continue;
1889
1890 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1891 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001892 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001893 fe->dai_link->name, "capture");
1894 mutex_unlock(&card->mutex);
1895 return paths;
1896 }
1897
1898 /* update any new capture paths */
1899 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1900 if (new) {
1901 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1902 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1903 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1904 }
1905
1906 /* update any old capture paths */
1907 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1908 if (old) {
1909 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1910 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1911 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1912 }
1913
1914 dpcm_path_put(&list);
1915 }
1916
1917 mutex_unlock(&card->mutex);
1918 return 0;
1919}
Liam Girdwood01d75842012-04-25 12:12:49 +01001920int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
1921{
1922 struct snd_soc_dpcm *dpcm;
1923 struct list_head *clients =
1924 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
1925
1926 list_for_each_entry(dpcm, clients, list_be) {
1927
1928 struct snd_soc_pcm_runtime *be = dpcm->be;
1929 struct snd_soc_dai *dai = be->codec_dai;
1930 struct snd_soc_dai_driver *drv = dai->driver;
1931
1932 if (be->dai_link->ignore_suspend)
1933 continue;
1934
Liam Girdwood103d84a2012-11-19 14:39:15 +00001935 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001936
1937 if (drv->ops->digital_mute && dai->playback_active)
1938 drv->ops->digital_mute(dai, mute);
1939 }
1940
1941 return 0;
1942}
1943
Mark Brown45c0a182012-05-09 21:46:27 +01001944static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001945{
1946 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1947 struct snd_soc_dpcm *dpcm;
1948 struct snd_soc_dapm_widget_list *list;
1949 int ret;
1950 int stream = fe_substream->stream;
1951
1952 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1953 fe->dpcm[stream].runtime = fe_substream->runtime;
1954
1955 if (dpcm_path_get(fe, stream, &list) <= 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001956 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001957 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01001958 }
1959
1960 /* calculate valid and active FE <-> BE dpcms */
1961 dpcm_process_paths(fe, stream, &list, 1);
1962
1963 ret = dpcm_fe_dai_startup(fe_substream);
1964 if (ret < 0) {
1965 /* clean up all links */
1966 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1967 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1968
1969 dpcm_be_disconnect(fe, stream);
1970 fe->dpcm[stream].runtime = NULL;
1971 }
1972
1973 dpcm_clear_pending_state(fe, stream);
1974 dpcm_path_put(&list);
1975 mutex_unlock(&fe->card->mutex);
1976 return ret;
1977}
1978
Mark Brown45c0a182012-05-09 21:46:27 +01001979static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001980{
1981 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1982 struct snd_soc_dpcm *dpcm;
1983 int stream = fe_substream->stream, ret;
1984
1985 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1986 ret = dpcm_fe_dai_shutdown(fe_substream);
1987
1988 /* mark FE's links ready to prune */
1989 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1990 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1991
1992 dpcm_be_disconnect(fe, stream);
1993
1994 fe->dpcm[stream].runtime = NULL;
1995 mutex_unlock(&fe->card->mutex);
1996 return ret;
1997}
1998
Liam Girdwoodddee6272011-06-09 14:45:53 +01001999/* create a new pcm */
2000int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2001{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002002 struct snd_soc_platform *platform = rtd->platform;
2003 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2004 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2005 struct snd_pcm *pcm;
2006 char new_name[64];
2007 int ret = 0, playback = 0, capture = 0;
2008
Liam Girdwood01d75842012-04-25 12:12:49 +01002009 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2010 if (cpu_dai->driver->playback.channels_min)
2011 playback = 1;
2012 if (cpu_dai->driver->capture.channels_min)
2013 capture = 1;
2014 } else {
Mark Brown05679092013-06-01 23:13:53 +01002015 if (codec_dai->driver->playback.channels_min &&
2016 cpu_dai->driver->playback.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002017 playback = 1;
Mark Brown05679092013-06-01 23:13:53 +01002018 if (codec_dai->driver->capture.channels_min &&
2019 cpu_dai->driver->capture.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002020 capture = 1;
2021 }
Sangsu Parka5002312012-01-02 17:15:10 +09002022
Fabio Estevamd6bead02013-08-29 10:32:13 -03002023 if (rtd->dai_link->playback_only) {
2024 playback = 1;
2025 capture = 0;
2026 }
2027
2028 if (rtd->dai_link->capture_only) {
2029 playback = 0;
2030 capture = 1;
2031 }
2032
Liam Girdwood01d75842012-04-25 12:12:49 +01002033 /* create the PCM */
2034 if (rtd->dai_link->no_pcm) {
2035 snprintf(new_name, sizeof(new_name), "(%s)",
2036 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002037
Liam Girdwood01d75842012-04-25 12:12:49 +01002038 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2039 playback, capture, &pcm);
2040 } else {
2041 if (rtd->dai_link->dynamic)
2042 snprintf(new_name, sizeof(new_name), "%s (*)",
2043 rtd->dai_link->stream_name);
2044 else
2045 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2046 rtd->dai_link->stream_name, codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002047
Liam Girdwood01d75842012-04-25 12:12:49 +01002048 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2049 capture, &pcm);
2050 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002051 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002052 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002053 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002054 return ret;
2055 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002056 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002057
2058 /* DAPM dai link stream work */
2059 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2060
2061 rtd->pcm = pcm;
2062 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002063
2064 if (rtd->dai_link->no_pcm) {
2065 if (playback)
2066 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2067 if (capture)
2068 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2069 goto out;
2070 }
2071
2072 /* ASoC PCM operations */
2073 if (rtd->dai_link->dynamic) {
2074 rtd->ops.open = dpcm_fe_dai_open;
2075 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2076 rtd->ops.prepare = dpcm_fe_dai_prepare;
2077 rtd->ops.trigger = dpcm_fe_dai_trigger;
2078 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2079 rtd->ops.close = dpcm_fe_dai_close;
2080 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002081 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002082 } else {
2083 rtd->ops.open = soc_pcm_open;
2084 rtd->ops.hw_params = soc_pcm_hw_params;
2085 rtd->ops.prepare = soc_pcm_prepare;
2086 rtd->ops.trigger = soc_pcm_trigger;
2087 rtd->ops.hw_free = soc_pcm_hw_free;
2088 rtd->ops.close = soc_pcm_close;
2089 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002090 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002091 }
2092
Liam Girdwoodddee6272011-06-09 14:45:53 +01002093 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002094 rtd->ops.ack = platform->driver->ops->ack;
2095 rtd->ops.copy = platform->driver->ops->copy;
2096 rtd->ops.silence = platform->driver->ops->silence;
2097 rtd->ops.page = platform->driver->ops->page;
2098 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002099 }
2100
2101 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002102 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002103
2104 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002105 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002106
2107 if (platform->driver->pcm_new) {
2108 ret = platform->driver->pcm_new(rtd);
2109 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002110 dev_err(platform->dev,
2111 "ASoC: pcm constructor failed: %d\n",
2112 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002113 return ret;
2114 }
2115 }
2116
2117 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002118out:
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002119 dev_info(rtd->card->dev, " %s <-> %s mapping ok\n", codec_dai->name,
Liam Girdwoodddee6272011-06-09 14:45:53 +01002120 cpu_dai->name);
2121 return ret;
2122}
Liam Girdwood01d75842012-04-25 12:12:49 +01002123
2124/* is the current PCM operation for this FE ? */
2125int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2126{
2127 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2128 return 1;
2129 return 0;
2130}
2131EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2132
2133/* is the current PCM operation for this BE ? */
2134int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2135 struct snd_soc_pcm_runtime *be, int stream)
2136{
2137 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2138 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2139 be->dpcm[stream].runtime_update))
2140 return 1;
2141 return 0;
2142}
2143EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2144
2145/* get the substream for this BE */
2146struct snd_pcm_substream *
2147 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2148{
2149 return be->pcm->streams[stream].substream;
2150}
2151EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2152
2153/* get the BE runtime state */
2154enum snd_soc_dpcm_state
2155 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2156{
2157 return be->dpcm[stream].state;
2158}
2159EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2160
2161/* set the BE runtime state */
2162void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2163 int stream, enum snd_soc_dpcm_state state)
2164{
2165 be->dpcm[stream].state = state;
2166}
2167EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2168
2169/*
2170 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2171 * are not running, paused or suspended for the specified stream direction.
2172 */
2173int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2174 struct snd_soc_pcm_runtime *be, int stream)
2175{
2176 struct snd_soc_dpcm *dpcm;
2177 int state;
2178
2179 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2180
2181 if (dpcm->fe == fe)
2182 continue;
2183
2184 state = dpcm->fe->dpcm[stream].state;
2185 if (state == SND_SOC_DPCM_STATE_START ||
2186 state == SND_SOC_DPCM_STATE_PAUSED ||
2187 state == SND_SOC_DPCM_STATE_SUSPEND)
2188 return 0;
2189 }
2190
2191 /* it's safe to free/stop this BE DAI */
2192 return 1;
2193}
2194EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2195
2196/*
2197 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2198 * running, paused or suspended for the specified stream direction.
2199 */
2200int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2201 struct snd_soc_pcm_runtime *be, int stream)
2202{
2203 struct snd_soc_dpcm *dpcm;
2204 int state;
2205
2206 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2207
2208 if (dpcm->fe == fe)
2209 continue;
2210
2211 state = dpcm->fe->dpcm[stream].state;
2212 if (state == SND_SOC_DPCM_STATE_START ||
2213 state == SND_SOC_DPCM_STATE_PAUSED ||
2214 state == SND_SOC_DPCM_STATE_SUSPEND ||
2215 state == SND_SOC_DPCM_STATE_PREPARE)
2216 return 0;
2217 }
2218
2219 /* it's safe to change hw_params */
2220 return 1;
2221}
2222EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002223
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002224int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2225 int cmd, struct snd_soc_platform *platform)
2226{
2227 if (platform->driver->ops->trigger)
2228 return platform->driver->ops->trigger(substream, cmd);
2229 return 0;
2230}
2231EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2232
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002233#ifdef CONFIG_DEBUG_FS
2234static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2235{
2236 switch (state) {
2237 case SND_SOC_DPCM_STATE_NEW:
2238 return "new";
2239 case SND_SOC_DPCM_STATE_OPEN:
2240 return "open";
2241 case SND_SOC_DPCM_STATE_HW_PARAMS:
2242 return "hw_params";
2243 case SND_SOC_DPCM_STATE_PREPARE:
2244 return "prepare";
2245 case SND_SOC_DPCM_STATE_START:
2246 return "start";
2247 case SND_SOC_DPCM_STATE_STOP:
2248 return "stop";
2249 case SND_SOC_DPCM_STATE_SUSPEND:
2250 return "suspend";
2251 case SND_SOC_DPCM_STATE_PAUSED:
2252 return "paused";
2253 case SND_SOC_DPCM_STATE_HW_FREE:
2254 return "hw_free";
2255 case SND_SOC_DPCM_STATE_CLOSE:
2256 return "close";
2257 }
2258
2259 return "unknown";
2260}
2261
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002262static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2263 int stream, char *buf, size_t size)
2264{
2265 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2266 struct snd_soc_dpcm *dpcm;
2267 ssize_t offset = 0;
2268
2269 /* FE state */
2270 offset += snprintf(buf + offset, size - offset,
2271 "[%s - %s]\n", fe->dai_link->name,
2272 stream ? "Capture" : "Playback");
2273
2274 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2275 dpcm_state_string(fe->dpcm[stream].state));
2276
2277 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2278 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2279 offset += snprintf(buf + offset, size - offset,
2280 "Hardware Params: "
2281 "Format = %s, Channels = %d, Rate = %d\n",
2282 snd_pcm_format_name(params_format(params)),
2283 params_channels(params),
2284 params_rate(params));
2285
2286 /* BEs state */
2287 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2288
2289 if (list_empty(&fe->dpcm[stream].be_clients)) {
2290 offset += snprintf(buf + offset, size - offset,
2291 " No active DSP links\n");
2292 goto out;
2293 }
2294
2295 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2296 struct snd_soc_pcm_runtime *be = dpcm->be;
2297 params = &dpcm->hw_params;
2298
2299 offset += snprintf(buf + offset, size - offset,
2300 "- %s\n", be->dai_link->name);
2301
2302 offset += snprintf(buf + offset, size - offset,
2303 " State: %s\n",
2304 dpcm_state_string(be->dpcm[stream].state));
2305
2306 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2307 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2308 offset += snprintf(buf + offset, size - offset,
2309 " Hardware Params: "
2310 "Format = %s, Channels = %d, Rate = %d\n",
2311 snd_pcm_format_name(params_format(params)),
2312 params_channels(params),
2313 params_rate(params));
2314 }
2315
2316out:
2317 return offset;
2318}
2319
2320static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2321 size_t count, loff_t *ppos)
2322{
2323 struct snd_soc_pcm_runtime *fe = file->private_data;
2324 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2325 char *buf;
2326
2327 buf = kmalloc(out_count, GFP_KERNEL);
2328 if (!buf)
2329 return -ENOMEM;
2330
2331 if (fe->cpu_dai->driver->playback.channels_min)
2332 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2333 buf + offset, out_count - offset);
2334
2335 if (fe->cpu_dai->driver->capture.channels_min)
2336 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2337 buf + offset, out_count - offset);
2338
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002339 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002340
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002341 kfree(buf);
2342 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002343}
2344
2345static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002346 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002347 .read = dpcm_state_read_file,
2348 .llseek = default_llseek,
2349};
2350
2351int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2352{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002353 if (!rtd->dai_link)
2354 return 0;
2355
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002356 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2357 rtd->card->debugfs_card_root);
2358 if (!rtd->debugfs_dpcm_root) {
2359 dev_dbg(rtd->dev,
2360 "ASoC: Failed to create dpcm debugfs directory %s\n",
2361 rtd->dai_link->name);
2362 return -EINVAL;
2363 }
2364
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002365 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002366 rtd->debugfs_dpcm_root,
2367 rtd, &dpcm_state_fops);
2368
2369 return 0;
2370}
2371#endif