blob: 9abaa52efb2e682146eeaf4af481c25e5345f167 [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
Mark Brown46162742013-06-05 19:36:11 +0100934 switch (list->widgets[i]->id) {
935 case snd_soc_dapm_dai_in:
936 case snd_soc_dapm_dai_out:
937 break;
938 default:
Liam Girdwood01d75842012-04-25 12:12:49 +0100939 continue;
Mark Brown46162742013-06-05 19:36:11 +0100940 }
Liam Girdwood01d75842012-04-25 12:12:49 +0100941
942 /* is there a valid BE rtd for this widget */
943 be = dpcm_get_be(card, list->widgets[i], stream);
944 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000945 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100946 list->widgets[i]->name);
947 continue;
948 }
949
950 /* make sure BE is a real BE */
951 if (!be->dai_link->no_pcm)
952 continue;
953
954 /* don't connect if FE is not running */
955 if (!fe->dpcm[stream].runtime)
956 continue;
957
958 /* newly connected FE and BE */
959 err = dpcm_be_connect(fe, be, stream);
960 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000961 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100962 list->widgets[i]->name);
963 break;
964 } else if (err == 0) /* already connected */
965 continue;
966
967 /* new */
968 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
969 new++;
970 }
971
Liam Girdwood103d84a2012-11-19 14:39:15 +0000972 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +0100973 return new;
974}
975
976/*
977 * Find the corresponding BE DAIs that source or sink audio to this
978 * FE substream.
979 */
980static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
981 int stream, struct snd_soc_dapm_widget_list **list, int new)
982{
983 if (new)
984 return dpcm_add_paths(fe, stream, list);
985 else
986 return dpcm_prune_paths(fe, stream, list);
987}
988
989static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
990{
991 struct snd_soc_dpcm *dpcm;
992
993 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
994 dpcm->be->dpcm[stream].runtime_update =
995 SND_SOC_DPCM_UPDATE_NO;
996}
997
998static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
999 int stream)
1000{
1001 struct snd_soc_dpcm *dpcm;
1002
1003 /* disable any enabled and non active backends */
1004 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1005
1006 struct snd_soc_pcm_runtime *be = dpcm->be;
1007 struct snd_pcm_substream *be_substream =
1008 snd_soc_dpcm_get_substream(be, stream);
1009
1010 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001011 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001012 stream ? "capture" : "playback",
1013 be->dpcm[stream].state);
1014
1015 if (--be->dpcm[stream].users != 0)
1016 continue;
1017
1018 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1019 continue;
1020
1021 soc_pcm_close(be_substream);
1022 be_substream->runtime = NULL;
1023 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1024 }
1025}
1026
1027static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1028{
1029 struct snd_soc_dpcm *dpcm;
1030 int err, count = 0;
1031
1032 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1033 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1034
1035 struct snd_soc_pcm_runtime *be = dpcm->be;
1036 struct snd_pcm_substream *be_substream =
1037 snd_soc_dpcm_get_substream(be, stream);
1038
1039 /* is this op for this BE ? */
1040 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1041 continue;
1042
1043 /* first time the dpcm is open ? */
1044 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001045 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001046 stream ? "capture" : "playback",
1047 be->dpcm[stream].state);
1048
1049 if (be->dpcm[stream].users++ != 0)
1050 continue;
1051
1052 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1053 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1054 continue;
1055
Liam Girdwood103d84a2012-11-19 14:39:15 +00001056 dev_dbg(be->dev, "ASoC: open BE %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001057
1058 be_substream->runtime = be->dpcm[stream].runtime;
1059 err = soc_pcm_open(be_substream);
1060 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001061 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001062 be->dpcm[stream].users--;
1063 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001064 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001065 stream ? "capture" : "playback",
1066 be->dpcm[stream].state);
1067
1068 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1069 goto unwind;
1070 }
1071
1072 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1073 count++;
1074 }
1075
1076 return count;
1077
1078unwind:
1079 /* disable any enabled and non active backends */
1080 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1081 struct snd_soc_pcm_runtime *be = dpcm->be;
1082 struct snd_pcm_substream *be_substream =
1083 snd_soc_dpcm_get_substream(be, stream);
1084
1085 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1086 continue;
1087
1088 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001089 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001090 stream ? "capture" : "playback",
1091 be->dpcm[stream].state);
1092
1093 if (--be->dpcm[stream].users != 0)
1094 continue;
1095
1096 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1097 continue;
1098
1099 soc_pcm_close(be_substream);
1100 be_substream->runtime = NULL;
1101 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1102 }
1103
1104 return err;
1105}
1106
Mark Brown45c0a182012-05-09 21:46:27 +01001107static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001108{
1109 struct snd_pcm_runtime *runtime = substream->runtime;
1110 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1111 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1112 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1113
1114 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1115 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1116 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1117 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1118 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1119 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1120 runtime->hw.rates = cpu_dai_drv->playback.rates;
1121 } else {
1122 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1123 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1124 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1125 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1126 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1127 runtime->hw.rates = cpu_dai_drv->capture.rates;
1128 }
1129}
1130
1131static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1132{
1133 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1134 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1135 int stream = fe_substream->stream, ret = 0;
1136
1137 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1138
1139 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1140 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001141 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001142 goto be_err;
1143 }
1144
Liam Girdwood103d84a2012-11-19 14:39:15 +00001145 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001146
1147 /* start the DAI frontend */
1148 ret = soc_pcm_open(fe_substream);
1149 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001150 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001151 goto unwind;
1152 }
1153
1154 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1155
1156 dpcm_set_fe_runtime(fe_substream);
1157 snd_pcm_limit_hw_rates(runtime);
1158
1159 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1160 return 0;
1161
1162unwind:
1163 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1164be_err:
1165 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1166 return ret;
1167}
1168
1169static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1170{
1171 struct snd_soc_dpcm *dpcm;
1172
1173 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1174 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1175
1176 struct snd_soc_pcm_runtime *be = dpcm->be;
1177 struct snd_pcm_substream *be_substream =
1178 snd_soc_dpcm_get_substream(be, stream);
1179
1180 /* is this op for this BE ? */
1181 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1182 continue;
1183
1184 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001185 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001186 stream ? "capture" : "playback",
1187 be->dpcm[stream].state);
1188
1189 if (--be->dpcm[stream].users != 0)
1190 continue;
1191
1192 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1193 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1194 continue;
1195
Liam Girdwood103d84a2012-11-19 14:39:15 +00001196 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001197 dpcm->fe->dai_link->name);
1198
1199 soc_pcm_close(be_substream);
1200 be_substream->runtime = NULL;
1201
1202 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1203 }
1204 return 0;
1205}
1206
1207static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1208{
1209 struct snd_soc_pcm_runtime *fe = substream->private_data;
1210 int stream = substream->stream;
1211
1212 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1213
1214 /* shutdown the BEs */
1215 dpcm_be_dai_shutdown(fe, substream->stream);
1216
Liam Girdwood103d84a2012-11-19 14:39:15 +00001217 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001218
1219 /* now shutdown the frontend */
1220 soc_pcm_close(substream);
1221
1222 /* run the stream event for each BE */
1223 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1224
1225 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1226 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1227 return 0;
1228}
1229
1230static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1231{
1232 struct snd_soc_dpcm *dpcm;
1233
1234 /* only hw_params backends that are either sinks or sources
1235 * to this frontend DAI */
1236 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1237
1238 struct snd_soc_pcm_runtime *be = dpcm->be;
1239 struct snd_pcm_substream *be_substream =
1240 snd_soc_dpcm_get_substream(be, stream);
1241
1242 /* is this op for this BE ? */
1243 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1244 continue;
1245
1246 /* only free hw when no longer used - check all FEs */
1247 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1248 continue;
1249
1250 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1251 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1252 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001253 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001254 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1255 continue;
1256
Liam Girdwood103d84a2012-11-19 14:39:15 +00001257 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001258 dpcm->fe->dai_link->name);
1259
1260 soc_pcm_hw_free(be_substream);
1261
1262 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1263 }
1264
1265 return 0;
1266}
1267
Mark Brown45c0a182012-05-09 21:46:27 +01001268static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001269{
1270 struct snd_soc_pcm_runtime *fe = substream->private_data;
1271 int err, stream = substream->stream;
1272
1273 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1274 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1275
Liam Girdwood103d84a2012-11-19 14:39:15 +00001276 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001277
1278 /* call hw_free on the frontend */
1279 err = soc_pcm_hw_free(substream);
1280 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001281 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001282 fe->dai_link->name);
1283
1284 /* only hw_params backends that are either sinks or sources
1285 * to this frontend DAI */
1286 err = dpcm_be_dai_hw_free(fe, stream);
1287
1288 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1289 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1290
1291 mutex_unlock(&fe->card->mutex);
1292 return 0;
1293}
1294
1295static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1296{
1297 struct snd_soc_dpcm *dpcm;
1298 int ret;
1299
1300 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1301
1302 struct snd_soc_pcm_runtime *be = dpcm->be;
1303 struct snd_pcm_substream *be_substream =
1304 snd_soc_dpcm_get_substream(be, stream);
1305
1306 /* is this op for this BE ? */
1307 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1308 continue;
1309
1310 /* only allow hw_params() if no connected FEs are running */
1311 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1312 continue;
1313
1314 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1315 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1316 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1317 continue;
1318
Liam Girdwood103d84a2012-11-19 14:39:15 +00001319 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001320 dpcm->fe->dai_link->name);
1321
1322 /* copy params for each dpcm */
1323 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1324 sizeof(struct snd_pcm_hw_params));
1325
1326 /* perform any hw_params fixups */
1327 if (be->dai_link->be_hw_params_fixup) {
1328 ret = be->dai_link->be_hw_params_fixup(be,
1329 &dpcm->hw_params);
1330 if (ret < 0) {
1331 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001332 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001333 ret);
1334 goto unwind;
1335 }
1336 }
1337
1338 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1339 if (ret < 0) {
1340 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001341 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001342 goto unwind;
1343 }
1344
1345 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1346 }
1347 return 0;
1348
1349unwind:
1350 /* disable any enabled and non active backends */
1351 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1352 struct snd_soc_pcm_runtime *be = dpcm->be;
1353 struct snd_pcm_substream *be_substream =
1354 snd_soc_dpcm_get_substream(be, stream);
1355
1356 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1357 continue;
1358
1359 /* only allow hw_free() if no connected FEs are running */
1360 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1361 continue;
1362
1363 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1364 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1365 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1366 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1367 continue;
1368
1369 soc_pcm_hw_free(be_substream);
1370 }
1371
1372 return ret;
1373}
1374
Mark Brown45c0a182012-05-09 21:46:27 +01001375static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1376 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001377{
1378 struct snd_soc_pcm_runtime *fe = substream->private_data;
1379 int ret, stream = substream->stream;
1380
1381 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1382 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1383
1384 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1385 sizeof(struct snd_pcm_hw_params));
1386 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1387 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001388 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001389 goto out;
1390 }
1391
Liam Girdwood103d84a2012-11-19 14:39:15 +00001392 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001393 fe->dai_link->name, params_rate(params),
1394 params_channels(params), params_format(params));
1395
1396 /* call hw_params on the frontend */
1397 ret = soc_pcm_hw_params(substream, params);
1398 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001399 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001400 dpcm_be_dai_hw_free(fe, stream);
1401 } else
1402 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1403
1404out:
1405 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1406 mutex_unlock(&fe->card->mutex);
1407 return ret;
1408}
1409
1410static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1411 struct snd_pcm_substream *substream, int cmd)
1412{
1413 int ret;
1414
Liam Girdwood103d84a2012-11-19 14:39:15 +00001415 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001416 dpcm->fe->dai_link->name, cmd);
1417
1418 ret = soc_pcm_trigger(substream, cmd);
1419 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001420 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001421
1422 return ret;
1423}
1424
Mark Brown45c0a182012-05-09 21:46:27 +01001425static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1426 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001427{
1428 struct snd_soc_dpcm *dpcm;
1429 int ret = 0;
1430
1431 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1432
1433 struct snd_soc_pcm_runtime *be = dpcm->be;
1434 struct snd_pcm_substream *be_substream =
1435 snd_soc_dpcm_get_substream(be, stream);
1436
1437 /* is this op for this BE ? */
1438 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1439 continue;
1440
1441 switch (cmd) {
1442 case SNDRV_PCM_TRIGGER_START:
1443 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1444 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1445 continue;
1446
1447 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1448 if (ret)
1449 return ret;
1450
1451 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1452 break;
1453 case SNDRV_PCM_TRIGGER_RESUME:
1454 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1455 continue;
1456
1457 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1458 if (ret)
1459 return ret;
1460
1461 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1462 break;
1463 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1464 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1465 continue;
1466
1467 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1468 if (ret)
1469 return ret;
1470
1471 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1472 break;
1473 case SNDRV_PCM_TRIGGER_STOP:
1474 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1475 continue;
1476
1477 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1478 continue;
1479
1480 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1481 if (ret)
1482 return ret;
1483
1484 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1485 break;
1486 case SNDRV_PCM_TRIGGER_SUSPEND:
1487 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1488 continue;
1489
1490 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1491 continue;
1492
1493 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1494 if (ret)
1495 return ret;
1496
1497 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1498 break;
1499 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1500 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1501 continue;
1502
1503 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1504 continue;
1505
1506 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1507 if (ret)
1508 return ret;
1509
1510 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1511 break;
1512 }
1513 }
1514
1515 return ret;
1516}
1517EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1518
Mark Brown45c0a182012-05-09 21:46:27 +01001519static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001520{
1521 struct snd_soc_pcm_runtime *fe = substream->private_data;
1522 int stream = substream->stream, ret;
1523 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1524
1525 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1526
1527 switch (trigger) {
1528 case SND_SOC_DPCM_TRIGGER_PRE:
1529 /* call trigger on the frontend before the backend. */
1530
Liam Girdwood103d84a2012-11-19 14:39:15 +00001531 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001532 fe->dai_link->name, cmd);
1533
1534 ret = soc_pcm_trigger(substream, cmd);
1535 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001536 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001537 goto out;
1538 }
1539
1540 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1541 break;
1542 case SND_SOC_DPCM_TRIGGER_POST:
1543 /* call trigger on the frontend after the backend. */
1544
1545 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1546 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001547 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001548 goto out;
1549 }
1550
Liam Girdwood103d84a2012-11-19 14:39:15 +00001551 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001552 fe->dai_link->name, cmd);
1553
1554 ret = soc_pcm_trigger(substream, cmd);
1555 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001556 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1557 /* bespoke trigger() - handles both FE and BEs */
1558
Liam Girdwood103d84a2012-11-19 14:39:15 +00001559 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001560 fe->dai_link->name, cmd);
1561
1562 ret = soc_pcm_bespoke_trigger(substream, cmd);
1563 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001564 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001565 goto out;
1566 }
1567 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01001568 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00001569 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01001570 fe->dai_link->name);
1571 ret = -EINVAL;
1572 goto out;
1573 }
1574
1575 switch (cmd) {
1576 case SNDRV_PCM_TRIGGER_START:
1577 case SNDRV_PCM_TRIGGER_RESUME:
1578 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1579 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1580 break;
1581 case SNDRV_PCM_TRIGGER_STOP:
1582 case SNDRV_PCM_TRIGGER_SUSPEND:
1583 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1584 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1585 break;
1586 }
1587
1588out:
1589 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1590 return ret;
1591}
1592
1593static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1594{
1595 struct snd_soc_dpcm *dpcm;
1596 int ret = 0;
1597
1598 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1599
1600 struct snd_soc_pcm_runtime *be = dpcm->be;
1601 struct snd_pcm_substream *be_substream =
1602 snd_soc_dpcm_get_substream(be, stream);
1603
1604 /* is this op for this BE ? */
1605 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1606 continue;
1607
1608 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1609 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1610 continue;
1611
Liam Girdwood103d84a2012-11-19 14:39:15 +00001612 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001613 dpcm->fe->dai_link->name);
1614
1615 ret = soc_pcm_prepare(be_substream);
1616 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001617 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001618 ret);
1619 break;
1620 }
1621
1622 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1623 }
1624 return ret;
1625}
1626
Mark Brown45c0a182012-05-09 21:46:27 +01001627static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001628{
1629 struct snd_soc_pcm_runtime *fe = substream->private_data;
1630 int stream = substream->stream, ret = 0;
1631
1632 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1633
Liam Girdwood103d84a2012-11-19 14:39:15 +00001634 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001635
1636 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1637
1638 /* there is no point preparing this FE if there are no BEs */
1639 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001640 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001641 fe->dai_link->name);
1642 ret = -EINVAL;
1643 goto out;
1644 }
1645
1646 ret = dpcm_be_dai_prepare(fe, substream->stream);
1647 if (ret < 0)
1648 goto out;
1649
1650 /* call prepare on the frontend */
1651 ret = soc_pcm_prepare(substream);
1652 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001653 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001654 fe->dai_link->name);
1655 goto out;
1656 }
1657
1658 /* run the stream event for each BE */
1659 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1660 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1661
1662out:
1663 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1664 mutex_unlock(&fe->card->mutex);
1665
1666 return ret;
1667}
1668
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001669static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1670 unsigned int cmd, void *arg)
1671{
1672 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1673 struct snd_soc_platform *platform = rtd->platform;
1674
1675 if (platform->driver->ops->ioctl)
1676 return platform->driver->ops->ioctl(substream, cmd, arg);
1677 return snd_pcm_lib_ioctl(substream, cmd, arg);
1678}
1679
Liam Girdwood618dae12012-04-25 12:12:51 +01001680static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1681{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001682 struct snd_pcm_substream *substream =
1683 snd_soc_dpcm_get_substream(fe, stream);
1684 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001685 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01001686
Liam Girdwood103d84a2012-11-19 14:39:15 +00001687 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001688 stream ? "capture" : "playback", fe->dai_link->name);
1689
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001690 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1691 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001692 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001693 fe->dai_link->name);
1694
1695 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1696 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001697 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001698 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001699 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001700 fe->dai_link->name);
1701
1702 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1703 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001704 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001705 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001706
1707 err = dpcm_be_dai_hw_free(fe, stream);
1708 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001709 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001710
1711 err = dpcm_be_dai_shutdown(fe, stream);
1712 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001713 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001714
1715 /* run the stream event for each BE */
1716 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1717
1718 return 0;
1719}
1720
1721static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1722{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001723 struct snd_pcm_substream *substream =
1724 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01001725 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001726 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001727 int ret;
1728
Liam Girdwood103d84a2012-11-19 14:39:15 +00001729 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001730 stream ? "capture" : "playback", fe->dai_link->name);
1731
1732 /* Only start the BE if the FE is ready */
1733 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1734 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1735 return -EINVAL;
1736
1737 /* startup must always be called for new BEs */
1738 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001739 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001740 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01001741
1742 /* keep going if FE state is > open */
1743 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1744 return 0;
1745
1746 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001747 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001748 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01001749
1750 /* keep going if FE state is > hw_params */
1751 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1752 return 0;
1753
1754
1755 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001756 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001757 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01001758
1759 /* run the stream event for each BE */
1760 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1761
1762 /* keep going if FE state is > prepare */
1763 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1764 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1765 return 0;
1766
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001767 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1768 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001769 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001770 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01001771
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001772 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1773 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001774 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001775 goto hw_free;
1776 }
1777 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001778 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001779 fe->dai_link->name);
1780
1781 ret = dpcm_be_dai_trigger(fe, stream,
1782 SNDRV_PCM_TRIGGER_START);
1783 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001784 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001785 goto hw_free;
1786 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001787 }
1788
1789 return 0;
1790
1791hw_free:
1792 dpcm_be_dai_hw_free(fe, stream);
1793close:
1794 dpcm_be_dai_shutdown(fe, stream);
1795disconnect:
1796 /* disconnect any non started BEs */
1797 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1798 struct snd_soc_pcm_runtime *be = dpcm->be;
1799 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1800 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1801 }
1802
1803 return ret;
1804}
1805
1806static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1807{
1808 int ret;
1809
1810 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1811 ret = dpcm_run_update_startup(fe, stream);
1812 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001813 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001814 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1815
1816 return ret;
1817}
1818
1819static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1820{
1821 int ret;
1822
1823 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1824 ret = dpcm_run_update_shutdown(fe, stream);
1825 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001826 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001827 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1828
1829 return ret;
1830}
1831
1832/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1833 * any DAI links.
1834 */
1835int soc_dpcm_runtime_update(struct snd_soc_dapm_widget *widget)
1836{
1837 struct snd_soc_card *card;
1838 int i, old, new, paths;
1839
1840 if (widget->codec)
1841 card = widget->codec->card;
1842 else if (widget->platform)
1843 card = widget->platform->card;
1844 else
1845 return -EINVAL;
1846
1847 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1848 for (i = 0; i < card->num_rtd; i++) {
1849 struct snd_soc_dapm_widget_list *list;
1850 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1851
1852 /* make sure link is FE */
1853 if (!fe->dai_link->dynamic)
1854 continue;
1855
1856 /* only check active links */
1857 if (!fe->cpu_dai->active)
1858 continue;
1859
1860 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001861 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001862 fe->dai_link->name);
1863
1864 /* skip if FE doesn't have playback capability */
1865 if (!fe->cpu_dai->driver->playback.channels_min)
1866 goto capture;
1867
1868 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1869 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001870 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001871 fe->dai_link->name, "playback");
1872 mutex_unlock(&card->mutex);
1873 return paths;
1874 }
1875
1876 /* update any new playback paths */
1877 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1878 if (new) {
1879 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1880 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1881 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1882 }
1883
1884 /* update any old playback paths */
1885 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1886 if (old) {
1887 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1888 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1889 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1890 }
1891
1892capture:
1893 /* skip if FE doesn't have capture capability */
1894 if (!fe->cpu_dai->driver->capture.channels_min)
1895 continue;
1896
1897 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1898 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001899 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001900 fe->dai_link->name, "capture");
1901 mutex_unlock(&card->mutex);
1902 return paths;
1903 }
1904
1905 /* update any new capture paths */
1906 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1907 if (new) {
1908 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1909 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1910 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1911 }
1912
1913 /* update any old capture paths */
1914 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1915 if (old) {
1916 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1917 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1918 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1919 }
1920
1921 dpcm_path_put(&list);
1922 }
1923
1924 mutex_unlock(&card->mutex);
1925 return 0;
1926}
Liam Girdwood01d75842012-04-25 12:12:49 +01001927int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
1928{
1929 struct snd_soc_dpcm *dpcm;
1930 struct list_head *clients =
1931 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
1932
1933 list_for_each_entry(dpcm, clients, list_be) {
1934
1935 struct snd_soc_pcm_runtime *be = dpcm->be;
1936 struct snd_soc_dai *dai = be->codec_dai;
1937 struct snd_soc_dai_driver *drv = dai->driver;
1938
1939 if (be->dai_link->ignore_suspend)
1940 continue;
1941
Liam Girdwood103d84a2012-11-19 14:39:15 +00001942 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001943
1944 if (drv->ops->digital_mute && dai->playback_active)
1945 drv->ops->digital_mute(dai, mute);
1946 }
1947
1948 return 0;
1949}
1950
Mark Brown45c0a182012-05-09 21:46:27 +01001951static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001952{
1953 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1954 struct snd_soc_dpcm *dpcm;
1955 struct snd_soc_dapm_widget_list *list;
1956 int ret;
1957 int stream = fe_substream->stream;
1958
1959 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1960 fe->dpcm[stream].runtime = fe_substream->runtime;
1961
1962 if (dpcm_path_get(fe, stream, &list) <= 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001963 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001964 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01001965 }
1966
1967 /* calculate valid and active FE <-> BE dpcms */
1968 dpcm_process_paths(fe, stream, &list, 1);
1969
1970 ret = dpcm_fe_dai_startup(fe_substream);
1971 if (ret < 0) {
1972 /* clean up all links */
1973 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1974 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1975
1976 dpcm_be_disconnect(fe, stream);
1977 fe->dpcm[stream].runtime = NULL;
1978 }
1979
1980 dpcm_clear_pending_state(fe, stream);
1981 dpcm_path_put(&list);
1982 mutex_unlock(&fe->card->mutex);
1983 return ret;
1984}
1985
Mark Brown45c0a182012-05-09 21:46:27 +01001986static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001987{
1988 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1989 struct snd_soc_dpcm *dpcm;
1990 int stream = fe_substream->stream, ret;
1991
1992 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1993 ret = dpcm_fe_dai_shutdown(fe_substream);
1994
1995 /* mark FE's links ready to prune */
1996 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1997 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1998
1999 dpcm_be_disconnect(fe, stream);
2000
2001 fe->dpcm[stream].runtime = NULL;
2002 mutex_unlock(&fe->card->mutex);
2003 return ret;
2004}
2005
Liam Girdwoodddee6272011-06-09 14:45:53 +01002006/* create a new pcm */
2007int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2008{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002009 struct snd_soc_platform *platform = rtd->platform;
2010 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2011 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2012 struct snd_pcm *pcm;
2013 char new_name[64];
2014 int ret = 0, playback = 0, capture = 0;
2015
Liam Girdwood01d75842012-04-25 12:12:49 +01002016 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2017 if (cpu_dai->driver->playback.channels_min)
2018 playback = 1;
2019 if (cpu_dai->driver->capture.channels_min)
2020 capture = 1;
2021 } else {
Mark Brown05679092013-06-01 23:13:53 +01002022 if (codec_dai->driver->playback.channels_min &&
2023 cpu_dai->driver->playback.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002024 playback = 1;
Mark Brown05679092013-06-01 23:13:53 +01002025 if (codec_dai->driver->capture.channels_min &&
2026 cpu_dai->driver->capture.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002027 capture = 1;
2028 }
Sangsu Parka5002312012-01-02 17:15:10 +09002029
Fabio Estevamd6bead02013-08-29 10:32:13 -03002030 if (rtd->dai_link->playback_only) {
2031 playback = 1;
2032 capture = 0;
2033 }
2034
2035 if (rtd->dai_link->capture_only) {
2036 playback = 0;
2037 capture = 1;
2038 }
2039
Liam Girdwood01d75842012-04-25 12:12:49 +01002040 /* create the PCM */
2041 if (rtd->dai_link->no_pcm) {
2042 snprintf(new_name, sizeof(new_name), "(%s)",
2043 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002044
Liam Girdwood01d75842012-04-25 12:12:49 +01002045 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2046 playback, capture, &pcm);
2047 } else {
2048 if (rtd->dai_link->dynamic)
2049 snprintf(new_name, sizeof(new_name), "%s (*)",
2050 rtd->dai_link->stream_name);
2051 else
2052 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2053 rtd->dai_link->stream_name, codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002054
Liam Girdwood01d75842012-04-25 12:12:49 +01002055 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2056 capture, &pcm);
2057 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002058 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002059 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002060 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002061 return ret;
2062 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002063 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002064
2065 /* DAPM dai link stream work */
2066 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2067
2068 rtd->pcm = pcm;
2069 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002070
2071 if (rtd->dai_link->no_pcm) {
2072 if (playback)
2073 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2074 if (capture)
2075 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2076 goto out;
2077 }
2078
2079 /* ASoC PCM operations */
2080 if (rtd->dai_link->dynamic) {
2081 rtd->ops.open = dpcm_fe_dai_open;
2082 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2083 rtd->ops.prepare = dpcm_fe_dai_prepare;
2084 rtd->ops.trigger = dpcm_fe_dai_trigger;
2085 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2086 rtd->ops.close = dpcm_fe_dai_close;
2087 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002088 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002089 } else {
2090 rtd->ops.open = soc_pcm_open;
2091 rtd->ops.hw_params = soc_pcm_hw_params;
2092 rtd->ops.prepare = soc_pcm_prepare;
2093 rtd->ops.trigger = soc_pcm_trigger;
2094 rtd->ops.hw_free = soc_pcm_hw_free;
2095 rtd->ops.close = soc_pcm_close;
2096 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002097 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002098 }
2099
Liam Girdwoodddee6272011-06-09 14:45:53 +01002100 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002101 rtd->ops.ack = platform->driver->ops->ack;
2102 rtd->ops.copy = platform->driver->ops->copy;
2103 rtd->ops.silence = platform->driver->ops->silence;
2104 rtd->ops.page = platform->driver->ops->page;
2105 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002106 }
2107
2108 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002109 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002110
2111 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002112 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002113
2114 if (platform->driver->pcm_new) {
2115 ret = platform->driver->pcm_new(rtd);
2116 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002117 dev_err(platform->dev,
2118 "ASoC: pcm constructor failed: %d\n",
2119 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002120 return ret;
2121 }
2122 }
2123
2124 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002125out:
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002126 dev_info(rtd->card->dev, " %s <-> %s mapping ok\n", codec_dai->name,
Liam Girdwoodddee6272011-06-09 14:45:53 +01002127 cpu_dai->name);
2128 return ret;
2129}
Liam Girdwood01d75842012-04-25 12:12:49 +01002130
2131/* is the current PCM operation for this FE ? */
2132int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2133{
2134 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2135 return 1;
2136 return 0;
2137}
2138EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2139
2140/* is the current PCM operation for this BE ? */
2141int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2142 struct snd_soc_pcm_runtime *be, int stream)
2143{
2144 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2145 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2146 be->dpcm[stream].runtime_update))
2147 return 1;
2148 return 0;
2149}
2150EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2151
2152/* get the substream for this BE */
2153struct snd_pcm_substream *
2154 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2155{
2156 return be->pcm->streams[stream].substream;
2157}
2158EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2159
2160/* get the BE runtime state */
2161enum snd_soc_dpcm_state
2162 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2163{
2164 return be->dpcm[stream].state;
2165}
2166EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2167
2168/* set the BE runtime state */
2169void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2170 int stream, enum snd_soc_dpcm_state state)
2171{
2172 be->dpcm[stream].state = state;
2173}
2174EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2175
2176/*
2177 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2178 * are not running, paused or suspended for the specified stream direction.
2179 */
2180int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2181 struct snd_soc_pcm_runtime *be, int stream)
2182{
2183 struct snd_soc_dpcm *dpcm;
2184 int state;
2185
2186 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2187
2188 if (dpcm->fe == fe)
2189 continue;
2190
2191 state = dpcm->fe->dpcm[stream].state;
2192 if (state == SND_SOC_DPCM_STATE_START ||
2193 state == SND_SOC_DPCM_STATE_PAUSED ||
2194 state == SND_SOC_DPCM_STATE_SUSPEND)
2195 return 0;
2196 }
2197
2198 /* it's safe to free/stop this BE DAI */
2199 return 1;
2200}
2201EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2202
2203/*
2204 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2205 * running, paused or suspended for the specified stream direction.
2206 */
2207int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2208 struct snd_soc_pcm_runtime *be, int stream)
2209{
2210 struct snd_soc_dpcm *dpcm;
2211 int state;
2212
2213 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2214
2215 if (dpcm->fe == fe)
2216 continue;
2217
2218 state = dpcm->fe->dpcm[stream].state;
2219 if (state == SND_SOC_DPCM_STATE_START ||
2220 state == SND_SOC_DPCM_STATE_PAUSED ||
2221 state == SND_SOC_DPCM_STATE_SUSPEND ||
2222 state == SND_SOC_DPCM_STATE_PREPARE)
2223 return 0;
2224 }
2225
2226 /* it's safe to change hw_params */
2227 return 1;
2228}
2229EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002230
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002231int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2232 int cmd, struct snd_soc_platform *platform)
2233{
2234 if (platform->driver->ops->trigger)
2235 return platform->driver->ops->trigger(substream, cmd);
2236 return 0;
2237}
2238EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2239
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002240#ifdef CONFIG_DEBUG_FS
2241static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2242{
2243 switch (state) {
2244 case SND_SOC_DPCM_STATE_NEW:
2245 return "new";
2246 case SND_SOC_DPCM_STATE_OPEN:
2247 return "open";
2248 case SND_SOC_DPCM_STATE_HW_PARAMS:
2249 return "hw_params";
2250 case SND_SOC_DPCM_STATE_PREPARE:
2251 return "prepare";
2252 case SND_SOC_DPCM_STATE_START:
2253 return "start";
2254 case SND_SOC_DPCM_STATE_STOP:
2255 return "stop";
2256 case SND_SOC_DPCM_STATE_SUSPEND:
2257 return "suspend";
2258 case SND_SOC_DPCM_STATE_PAUSED:
2259 return "paused";
2260 case SND_SOC_DPCM_STATE_HW_FREE:
2261 return "hw_free";
2262 case SND_SOC_DPCM_STATE_CLOSE:
2263 return "close";
2264 }
2265
2266 return "unknown";
2267}
2268
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002269static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2270 int stream, char *buf, size_t size)
2271{
2272 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2273 struct snd_soc_dpcm *dpcm;
2274 ssize_t offset = 0;
2275
2276 /* FE state */
2277 offset += snprintf(buf + offset, size - offset,
2278 "[%s - %s]\n", fe->dai_link->name,
2279 stream ? "Capture" : "Playback");
2280
2281 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2282 dpcm_state_string(fe->dpcm[stream].state));
2283
2284 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2285 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2286 offset += snprintf(buf + offset, size - offset,
2287 "Hardware Params: "
2288 "Format = %s, Channels = %d, Rate = %d\n",
2289 snd_pcm_format_name(params_format(params)),
2290 params_channels(params),
2291 params_rate(params));
2292
2293 /* BEs state */
2294 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2295
2296 if (list_empty(&fe->dpcm[stream].be_clients)) {
2297 offset += snprintf(buf + offset, size - offset,
2298 " No active DSP links\n");
2299 goto out;
2300 }
2301
2302 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2303 struct snd_soc_pcm_runtime *be = dpcm->be;
2304 params = &dpcm->hw_params;
2305
2306 offset += snprintf(buf + offset, size - offset,
2307 "- %s\n", be->dai_link->name);
2308
2309 offset += snprintf(buf + offset, size - offset,
2310 " State: %s\n",
2311 dpcm_state_string(be->dpcm[stream].state));
2312
2313 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2314 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2315 offset += snprintf(buf + offset, size - offset,
2316 " Hardware Params: "
2317 "Format = %s, Channels = %d, Rate = %d\n",
2318 snd_pcm_format_name(params_format(params)),
2319 params_channels(params),
2320 params_rate(params));
2321 }
2322
2323out:
2324 return offset;
2325}
2326
2327static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2328 size_t count, loff_t *ppos)
2329{
2330 struct snd_soc_pcm_runtime *fe = file->private_data;
2331 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2332 char *buf;
2333
2334 buf = kmalloc(out_count, GFP_KERNEL);
2335 if (!buf)
2336 return -ENOMEM;
2337
2338 if (fe->cpu_dai->driver->playback.channels_min)
2339 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2340 buf + offset, out_count - offset);
2341
2342 if (fe->cpu_dai->driver->capture.channels_min)
2343 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2344 buf + offset, out_count - offset);
2345
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002346 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002347
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002348 kfree(buf);
2349 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002350}
2351
2352static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002353 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002354 .read = dpcm_state_read_file,
2355 .llseek = default_llseek,
2356};
2357
2358int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2359{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002360 if (!rtd->dai_link)
2361 return 0;
2362
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002363 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2364 rtd->card->debugfs_card_root);
2365 if (!rtd->debugfs_dpcm_root) {
2366 dev_dbg(rtd->dev,
2367 "ASoC: Failed to create dpcm debugfs directory %s\n",
2368 rtd->dai_link->name);
2369 return -EINVAL;
2370 }
2371
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002372 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002373 rtd->debugfs_dpcm_root,
2374 rtd, &dpcm_state_fops);
2375
2376 return 0;
2377}
2378#endif