blob: cf191e6aebbe1d1bfeb9dd511d0fe9b5e5bd3bbd [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
36/* DPCM stream event, send event to FE and all active BEs. */
37static int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
38 int event)
39{
40 struct snd_soc_dpcm *dpcm;
41
42 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
43
44 struct snd_soc_pcm_runtime *be = dpcm->be;
45
Liam Girdwood103d84a2012-11-19 14:39:15 +000046 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +010047 be->dai_link->name, event, dir);
48
49 snd_soc_dapm_stream_event(be, dir, event);
50 }
51
52 snd_soc_dapm_stream_event(fe, dir, event);
53
54 return 0;
55}
56
Dong Aisheng17841022011-08-29 17:15:14 +080057static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
58 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +010059{
60 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +010061 int ret;
62
Dong Aisheng17841022011-08-29 17:15:14 +080063 if (!soc_dai->driver->symmetric_rates &&
Liam Girdwoodddee6272011-06-09 14:45:53 +010064 !rtd->dai_link->symmetric_rates)
65 return 0;
66
67 /* This can happen if multiple streams are starting simultaneously -
68 * the second can need to get its constraints before the first has
69 * picked a rate. Complain and allow the application to carry on.
70 */
Dong Aisheng17841022011-08-29 17:15:14 +080071 if (!soc_dai->rate) {
72 dev_warn(soc_dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +000073 "ASoC: Not enforcing symmetric_rates due to race\n");
Liam Girdwoodddee6272011-06-09 14:45:53 +010074 return 0;
75 }
76
Liam Girdwood103d84a2012-11-19 14:39:15 +000077 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n", soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +010078
79 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
80 SNDRV_PCM_HW_PARAM_RATE,
Dong Aisheng17841022011-08-29 17:15:14 +080081 soc_dai->rate, soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +010082 if (ret < 0) {
Dong Aisheng17841022011-08-29 17:15:14 +080083 dev_err(soc_dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +000084 "ASoC: Unable to apply rate symmetry constraint: %d\n",
85 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +010086 return ret;
87 }
88
89 return 0;
90}
91
92/*
Mark Brown58ba9b22012-01-16 18:38:51 +000093 * List of sample sizes that might go over the bus for parameter
94 * application. There ought to be a wildcard sample size for things
95 * like the DAC/ADC resolution to use but there isn't right now.
96 */
97static int sample_sizes[] = {
Peter Ujfalusi88e33952012-01-25 10:09:41 +020098 24, 32,
Mark Brown58ba9b22012-01-16 18:38:51 +000099};
100
101static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
102 struct snd_soc_dai *dai)
103{
104 int ret, i, bits;
105
106 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
107 bits = dai->driver->playback.sig_bits;
108 else
109 bits = dai->driver->capture.sig_bits;
110
111 if (!bits)
112 return;
113
114 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
Mark Brown278047f2012-01-19 18:04:18 +0000115 if (bits >= sample_sizes[i])
116 continue;
117
118 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
119 sample_sizes[i], bits);
Mark Brown58ba9b22012-01-16 18:38:51 +0000120 if (ret != 0)
121 dev_warn(dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +0000122 "ASoC: Failed to set MSB %d/%d: %d\n",
Mark Brown58ba9b22012-01-16 18:38:51 +0000123 bits, sample_sizes[i], ret);
124 }
125}
126
127/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100128 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
129 * then initialized and any private data can be allocated. This also calls
130 * startup for the cpu DAI, platform, machine and codec DAI.
131 */
132static int soc_pcm_open(struct snd_pcm_substream *substream)
133{
134 struct snd_soc_pcm_runtime *rtd = substream->private_data;
135 struct snd_pcm_runtime *runtime = substream->runtime;
136 struct snd_soc_platform *platform = rtd->platform;
137 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
138 struct snd_soc_dai *codec_dai = rtd->codec_dai;
139 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
140 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
141 int ret = 0;
142
Mark Brownd6652ef2011-12-03 20:14:31 +0000143 pm_runtime_get_sync(cpu_dai->dev);
144 pm_runtime_get_sync(codec_dai->dev);
145 pm_runtime_get_sync(platform->dev);
146
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100147 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100148
149 /* startup the audio subsystem */
150 if (cpu_dai->driver->ops->startup) {
151 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
152 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000153 dev_err(cpu_dai->dev, "ASoC: can't open interface"
154 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100155 goto out;
156 }
157 }
158
159 if (platform->driver->ops && platform->driver->ops->open) {
160 ret = platform->driver->ops->open(substream);
161 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000162 dev_err(platform->dev, "ASoC: can't open platform"
163 " %s: %d\n", platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100164 goto platform_err;
165 }
166 }
167
168 if (codec_dai->driver->ops->startup) {
169 ret = codec_dai->driver->ops->startup(substream, codec_dai);
170 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000171 dev_err(codec_dai->dev, "ASoC: can't open codec"
172 " %s: %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100173 goto codec_dai_err;
174 }
175 }
176
177 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
178 ret = rtd->dai_link->ops->startup(substream);
179 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000180 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000181 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100182 goto machine_err;
183 }
184 }
185
Liam Girdwood01d75842012-04-25 12:12:49 +0100186 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
187 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
188 goto dynamic;
189
Liam Girdwoodddee6272011-06-09 14:45:53 +0100190 /* Check that the codec and cpu DAIs are compatible */
191 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
192 runtime->hw.rate_min =
193 max(codec_dai_drv->playback.rate_min,
194 cpu_dai_drv->playback.rate_min);
195 runtime->hw.rate_max =
196 min(codec_dai_drv->playback.rate_max,
197 cpu_dai_drv->playback.rate_max);
198 runtime->hw.channels_min =
199 max(codec_dai_drv->playback.channels_min,
200 cpu_dai_drv->playback.channels_min);
201 runtime->hw.channels_max =
202 min(codec_dai_drv->playback.channels_max,
203 cpu_dai_drv->playback.channels_max);
204 runtime->hw.formats =
205 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
206 runtime->hw.rates =
207 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
208 if (codec_dai_drv->playback.rates
209 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
210 runtime->hw.rates |= cpu_dai_drv->playback.rates;
211 if (cpu_dai_drv->playback.rates
212 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
213 runtime->hw.rates |= codec_dai_drv->playback.rates;
214 } else {
215 runtime->hw.rate_min =
216 max(codec_dai_drv->capture.rate_min,
217 cpu_dai_drv->capture.rate_min);
218 runtime->hw.rate_max =
219 min(codec_dai_drv->capture.rate_max,
220 cpu_dai_drv->capture.rate_max);
221 runtime->hw.channels_min =
222 max(codec_dai_drv->capture.channels_min,
223 cpu_dai_drv->capture.channels_min);
224 runtime->hw.channels_max =
225 min(codec_dai_drv->capture.channels_max,
226 cpu_dai_drv->capture.channels_max);
227 runtime->hw.formats =
228 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
229 runtime->hw.rates =
230 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
231 if (codec_dai_drv->capture.rates
232 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
233 runtime->hw.rates |= cpu_dai_drv->capture.rates;
234 if (cpu_dai_drv->capture.rates
235 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
236 runtime->hw.rates |= codec_dai_drv->capture.rates;
237 }
238
239 ret = -EINVAL;
240 snd_pcm_limit_hw_rates(runtime);
241 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000242 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100243 codec_dai->name, cpu_dai->name);
244 goto config_err;
245 }
246 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000247 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100248 codec_dai->name, cpu_dai->name);
249 goto config_err;
250 }
251 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
252 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000253 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100254 codec_dai->name, cpu_dai->name);
255 goto config_err;
256 }
257
Mark Brown58ba9b22012-01-16 18:38:51 +0000258 soc_pcm_apply_msb(substream, codec_dai);
259 soc_pcm_apply_msb(substream, cpu_dai);
260
Liam Girdwoodddee6272011-06-09 14:45:53 +0100261 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800262 if (cpu_dai->active) {
263 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
264 if (ret != 0)
265 goto config_err;
266 }
267
268 if (codec_dai->active) {
269 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100270 if (ret != 0)
271 goto config_err;
272 }
273
Liam Girdwood103d84a2012-11-19 14:39:15 +0000274 pr_debug("ASoC: %s <-> %s info:\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100275 codec_dai->name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000276 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
277 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100278 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000279 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100280 runtime->hw.rate_max);
281
Liam Girdwood01d75842012-04-25 12:12:49 +0100282dynamic:
Liam Girdwoodddee6272011-06-09 14:45:53 +0100283 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
284 cpu_dai->playback_active++;
285 codec_dai->playback_active++;
286 } else {
287 cpu_dai->capture_active++;
288 codec_dai->capture_active++;
289 }
290 cpu_dai->active++;
291 codec_dai->active++;
292 rtd->codec->active++;
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100293 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100294 return 0;
295
296config_err:
297 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
298 rtd->dai_link->ops->shutdown(substream);
299
300machine_err:
301 if (codec_dai->driver->ops->shutdown)
302 codec_dai->driver->ops->shutdown(substream, codec_dai);
303
304codec_dai_err:
305 if (platform->driver->ops && platform->driver->ops->close)
306 platform->driver->ops->close(substream);
307
308platform_err:
309 if (cpu_dai->driver->ops->shutdown)
310 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
311out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100312 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000313
314 pm_runtime_put(platform->dev);
315 pm_runtime_put(codec_dai->dev);
316 pm_runtime_put(cpu_dai->dev);
317
Liam Girdwoodddee6272011-06-09 14:45:53 +0100318 return ret;
319}
320
321/*
322 * Power down the audio subsystem pmdown_time msecs after close is called.
323 * This is to ensure there are no pops or clicks in between any music tracks
324 * due to DAPM power cycling.
325 */
326static void close_delayed_work(struct work_struct *work)
327{
328 struct snd_soc_pcm_runtime *rtd =
329 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
330 struct snd_soc_dai *codec_dai = rtd->codec_dai;
331
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100332 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100333
Liam Girdwood103d84a2012-11-19 14:39:15 +0000334 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100335 codec_dai->driver->playback.stream_name,
336 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600337 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100338
339 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600340 if (rtd->pop_wait == 1) {
341 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800342 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000343 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100344 }
345
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100346 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100347}
348
349/*
350 * Called by ALSA when a PCM substream is closed. Private data can be
351 * freed here. The cpu DAI, codec DAI, machine and platform are also
352 * shutdown.
353 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100354static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100355{
356 struct snd_soc_pcm_runtime *rtd = substream->private_data;
357 struct snd_soc_platform *platform = rtd->platform;
358 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
359 struct snd_soc_dai *codec_dai = rtd->codec_dai;
360 struct snd_soc_codec *codec = rtd->codec;
361
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100362 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100363
364 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
365 cpu_dai->playback_active--;
366 codec_dai->playback_active--;
367 } else {
368 cpu_dai->capture_active--;
369 codec_dai->capture_active--;
370 }
371
372 cpu_dai->active--;
373 codec_dai->active--;
374 codec->active--;
375
Dong Aisheng17841022011-08-29 17:15:14 +0800376 /* clear the corresponding DAIs rate when inactive */
377 if (!cpu_dai->active)
378 cpu_dai->rate = 0;
379
380 if (!codec_dai->active)
381 codec_dai->rate = 0;
Sascha Hauer25b76792011-08-17 09:20:01 +0200382
Liam Girdwoodddee6272011-06-09 14:45:53 +0100383 /* Muting the DAC suppresses artifacts caused during digital
384 * shutdown, for example from stopping clocks.
385 */
386 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
387 snd_soc_dai_digital_mute(codec_dai, 1);
388
389 if (cpu_dai->driver->ops->shutdown)
390 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
391
392 if (codec_dai->driver->ops->shutdown)
393 codec_dai->driver->ops->shutdown(substream, codec_dai);
394
395 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
396 rtd->dai_link->ops->shutdown(substream);
397
398 if (platform->driver->ops && platform->driver->ops->close)
399 platform->driver->ops->close(substream);
400 cpu_dai->runtime = NULL;
401
402 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mark Brownb5d1d032012-02-08 20:10:56 +0000403 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
Mark Brown5b6247a2011-10-27 12:11:26 +0200404 rtd->dai_link->ignore_pmdown_time) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300405 /* powered down playback stream now */
406 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800407 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800408 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300409 } else {
410 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600411 rtd->pop_wait = 1;
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300412 schedule_delayed_work(&rtd->delayed_work,
413 msecs_to_jiffies(rtd->pmdown_time));
414 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100415 } else {
416 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800417 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000418 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100419 }
420
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100421 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000422
423 pm_runtime_put(platform->dev);
424 pm_runtime_put(codec_dai->dev);
425 pm_runtime_put(cpu_dai->dev);
426
Liam Girdwoodddee6272011-06-09 14:45:53 +0100427 return 0;
428}
429
430/*
431 * Called by ALSA when the PCM substream is prepared, can set format, sample
432 * rate, etc. This function is non atomic and can be called multiple times,
433 * it can refer to the runtime info.
434 */
435static int soc_pcm_prepare(struct snd_pcm_substream *substream)
436{
437 struct snd_soc_pcm_runtime *rtd = substream->private_data;
438 struct snd_soc_platform *platform = rtd->platform;
439 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
440 struct snd_soc_dai *codec_dai = rtd->codec_dai;
441 int ret = 0;
442
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100443 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100444
445 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
446 ret = rtd->dai_link->ops->prepare(substream);
447 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000448 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
449 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100450 goto out;
451 }
452 }
453
454 if (platform->driver->ops && platform->driver->ops->prepare) {
455 ret = platform->driver->ops->prepare(substream);
456 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000457 dev_err(platform->dev, "ASoC: platform prepare error:"
458 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100459 goto out;
460 }
461 }
462
463 if (codec_dai->driver->ops->prepare) {
464 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
465 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000466 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000467 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100468 goto out;
469 }
470 }
471
472 if (cpu_dai->driver->ops->prepare) {
473 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
474 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000475 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000476 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100477 goto out;
478 }
479 }
480
481 /* cancel any delayed stream shutdown that is pending */
482 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600483 rtd->pop_wait) {
484 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100485 cancel_delayed_work(&rtd->delayed_work);
486 }
487
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000488 snd_soc_dapm_stream_event(rtd, substream->stream,
489 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100490
491 snd_soc_dai_digital_mute(codec_dai, 0);
492
493out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100494 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100495 return ret;
496}
497
498/*
499 * Called by ALSA when the hardware params are set by application. This
500 * function can also be called multiple times and can allocate buffers
501 * (using snd_pcm_lib_* ). It's non-atomic.
502 */
503static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
504 struct snd_pcm_hw_params *params)
505{
506 struct snd_soc_pcm_runtime *rtd = substream->private_data;
507 struct snd_soc_platform *platform = rtd->platform;
508 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
509 struct snd_soc_dai *codec_dai = rtd->codec_dai;
510 int ret = 0;
511
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100512 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100513
514 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
515 ret = rtd->dai_link->ops->hw_params(substream, params);
516 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000517 dev_err(rtd->card->dev, "ASoC: machine hw_params"
518 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100519 goto out;
520 }
521 }
522
523 if (codec_dai->driver->ops->hw_params) {
524 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
525 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000526 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
527 " %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100528 goto codec_err;
529 }
530 }
531
532 if (cpu_dai->driver->ops->hw_params) {
533 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
534 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000535 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000536 cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100537 goto interface_err;
538 }
539 }
540
541 if (platform->driver->ops && platform->driver->ops->hw_params) {
542 ret = platform->driver->ops->hw_params(substream, params);
543 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000544 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000545 platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100546 goto platform_err;
547 }
548 }
549
Dong Aisheng17841022011-08-29 17:15:14 +0800550 /* store the rate for each DAIs */
551 cpu_dai->rate = params_rate(params);
552 codec_dai->rate = params_rate(params);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100553
554out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100555 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100556 return ret;
557
558platform_err:
559 if (cpu_dai->driver->ops->hw_free)
560 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
561
562interface_err:
563 if (codec_dai->driver->ops->hw_free)
564 codec_dai->driver->ops->hw_free(substream, codec_dai);
565
566codec_err:
567 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
568 rtd->dai_link->ops->hw_free(substream);
569
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100570 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100571 return ret;
572}
573
574/*
575 * Frees resources allocated by hw_params, can be called multiple times
576 */
577static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
578{
579 struct snd_soc_pcm_runtime *rtd = substream->private_data;
580 struct snd_soc_platform *platform = rtd->platform;
581 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
582 struct snd_soc_dai *codec_dai = rtd->codec_dai;
583 struct snd_soc_codec *codec = rtd->codec;
584
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100585 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100586
587 /* apply codec digital mute */
588 if (!codec->active)
589 snd_soc_dai_digital_mute(codec_dai, 1);
590
591 /* free any machine hw params */
592 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
593 rtd->dai_link->ops->hw_free(substream);
594
595 /* free any DMA resources */
596 if (platform->driver->ops && platform->driver->ops->hw_free)
597 platform->driver->ops->hw_free(substream);
598
599 /* now free hw params for the DAIs */
600 if (codec_dai->driver->ops->hw_free)
601 codec_dai->driver->ops->hw_free(substream, codec_dai);
602
603 if (cpu_dai->driver->ops->hw_free)
604 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
605
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100606 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100607 return 0;
608}
609
610static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
611{
612 struct snd_soc_pcm_runtime *rtd = substream->private_data;
613 struct snd_soc_platform *platform = rtd->platform;
614 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
615 struct snd_soc_dai *codec_dai = rtd->codec_dai;
616 int ret;
617
618 if (codec_dai->driver->ops->trigger) {
619 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
620 if (ret < 0)
621 return ret;
622 }
623
624 if (platform->driver->ops && platform->driver->ops->trigger) {
625 ret = platform->driver->ops->trigger(substream, cmd);
626 if (ret < 0)
627 return ret;
628 }
629
630 if (cpu_dai->driver->ops->trigger) {
631 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
632 if (ret < 0)
633 return ret;
634 }
635 return 0;
636}
637
Mark Brown45c0a182012-05-09 21:46:27 +0100638static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
639 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100640{
641 struct snd_soc_pcm_runtime *rtd = substream->private_data;
642 struct snd_soc_platform *platform = rtd->platform;
643 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
644 struct snd_soc_dai *codec_dai = rtd->codec_dai;
645 int ret;
646
647 if (codec_dai->driver->ops->bespoke_trigger) {
648 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
649 if (ret < 0)
650 return ret;
651 }
652
653 if (platform->driver->bespoke_trigger) {
654 ret = platform->driver->bespoke_trigger(substream, cmd);
655 if (ret < 0)
656 return ret;
657 }
658
659 if (cpu_dai->driver->ops->bespoke_trigger) {
660 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
661 if (ret < 0)
662 return ret;
663 }
664 return 0;
665}
Liam Girdwoodddee6272011-06-09 14:45:53 +0100666/*
667 * soc level wrapper for pointer callback
668 * If cpu_dai, codec_dai, platform driver has the delay callback, than
669 * the runtime->delay will be updated accordingly.
670 */
671static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
672{
673 struct snd_soc_pcm_runtime *rtd = substream->private_data;
674 struct snd_soc_platform *platform = rtd->platform;
675 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
676 struct snd_soc_dai *codec_dai = rtd->codec_dai;
677 struct snd_pcm_runtime *runtime = substream->runtime;
678 snd_pcm_uframes_t offset = 0;
679 snd_pcm_sframes_t delay = 0;
680
681 if (platform->driver->ops && platform->driver->ops->pointer)
682 offset = platform->driver->ops->pointer(substream);
683
684 if (cpu_dai->driver->ops->delay)
685 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
686
687 if (codec_dai->driver->ops->delay)
688 delay += codec_dai->driver->ops->delay(substream, codec_dai);
689
690 if (platform->driver->delay)
691 delay += platform->driver->delay(substream, codec_dai);
692
693 runtime->delay = delay;
694
695 return offset;
696}
697
Liam Girdwood01d75842012-04-25 12:12:49 +0100698/* connect a FE and BE */
699static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
700 struct snd_soc_pcm_runtime *be, int stream)
701{
702 struct snd_soc_dpcm *dpcm;
703
704 /* only add new dpcms */
705 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
706 if (dpcm->be == be && dpcm->fe == fe)
707 return 0;
708 }
709
710 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
711 if (!dpcm)
712 return -ENOMEM;
713
714 dpcm->be = be;
715 dpcm->fe = fe;
716 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
717 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
718 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
719 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
720
721 dev_dbg(fe->dev, " connected new DPCM %s path %s %s %s\n",
722 stream ? "capture" : "playback", fe->dai_link->name,
723 stream ? "<-" : "->", be->dai_link->name);
724
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100725#ifdef CONFIG_DEBUG_FS
726 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
727 fe->debugfs_dpcm_root, &dpcm->state);
728#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100729 return 1;
730}
731
732/* reparent a BE onto another FE */
733static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
734 struct snd_soc_pcm_runtime *be, int stream)
735{
736 struct snd_soc_dpcm *dpcm;
737 struct snd_pcm_substream *fe_substream, *be_substream;
738
739 /* reparent if BE is connected to other FEs */
740 if (!be->dpcm[stream].users)
741 return;
742
743 be_substream = snd_soc_dpcm_get_substream(be, stream);
744
745 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
746 if (dpcm->fe == fe)
747 continue;
748
749 dev_dbg(fe->dev, " reparent %s path %s %s %s\n",
750 stream ? "capture" : "playback",
751 dpcm->fe->dai_link->name,
752 stream ? "<-" : "->", dpcm->be->dai_link->name);
753
754 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
755 be_substream->runtime = fe_substream->runtime;
756 break;
757 }
758}
759
760/* disconnect a BE and FE */
761static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
762{
763 struct snd_soc_dpcm *dpcm, *d;
764
765 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000766 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100767 stream ? "capture" : "playback",
768 dpcm->be->dai_link->name);
769
770 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
771 continue;
772
773 dev_dbg(fe->dev, " freed DSP %s path %s %s %s\n",
774 stream ? "capture" : "playback", fe->dai_link->name,
775 stream ? "<-" : "->", dpcm->be->dai_link->name);
776
777 /* BEs still alive need new FE */
778 dpcm_be_reparent(fe, dpcm->be, stream);
779
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100780#ifdef CONFIG_DEBUG_FS
781 debugfs_remove(dpcm->debugfs_state);
782#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100783 list_del(&dpcm->list_be);
784 list_del(&dpcm->list_fe);
785 kfree(dpcm);
786 }
787}
788
789/* get BE for DAI widget and stream */
790static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
791 struct snd_soc_dapm_widget *widget, int stream)
792{
793 struct snd_soc_pcm_runtime *be;
794 int i;
795
796 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
797 for (i = 0; i < card->num_links; i++) {
798 be = &card->rtd[i];
799
Liam Girdwood35ea0652012-06-05 19:26:59 +0100800 if (!be->dai_link->no_pcm)
801 continue;
802
Liam Girdwood01d75842012-04-25 12:12:49 +0100803 if (be->cpu_dai->playback_widget == widget ||
804 be->codec_dai->playback_widget == widget)
805 return be;
806 }
807 } else {
808
809 for (i = 0; i < card->num_links; i++) {
810 be = &card->rtd[i];
811
Liam Girdwood35ea0652012-06-05 19:26:59 +0100812 if (!be->dai_link->no_pcm)
813 continue;
814
Liam Girdwood01d75842012-04-25 12:12:49 +0100815 if (be->cpu_dai->capture_widget == widget ||
816 be->codec_dai->capture_widget == widget)
817 return be;
818 }
819 }
820
Liam Girdwood103d84a2012-11-19 14:39:15 +0000821 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100822 stream ? "capture" : "playback", widget->name);
823 return NULL;
824}
825
826static inline struct snd_soc_dapm_widget *
827 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
828{
829 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
830 return rtd->cpu_dai->playback_widget;
831 else
832 return rtd->cpu_dai->capture_widget;
833}
834
835static inline struct snd_soc_dapm_widget *
836 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
837{
838 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
839 return rtd->codec_dai->playback_widget;
840 else
841 return rtd->codec_dai->capture_widget;
842}
843
844static int widget_in_list(struct snd_soc_dapm_widget_list *list,
845 struct snd_soc_dapm_widget *widget)
846{
847 int i;
848
849 for (i = 0; i < list->num_widgets; i++) {
850 if (widget == list->widgets[i])
851 return 1;
852 }
853
854 return 0;
855}
856
857static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
858 int stream, struct snd_soc_dapm_widget_list **list_)
859{
860 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
861 struct snd_soc_dapm_widget_list *list;
862 int paths;
863
864 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
865 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
866 if (list == NULL)
867 return -ENOMEM;
868
869 /* get number of valid DAI paths and their widgets */
870 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
871
Liam Girdwood103d84a2012-11-19 14:39:15 +0000872 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +0100873 stream ? "capture" : "playback");
874
875 *list_ = list;
876 return paths;
877}
878
879static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
880{
881 kfree(*list);
882}
883
884static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
885 struct snd_soc_dapm_widget_list **list_)
886{
887 struct snd_soc_dpcm *dpcm;
888 struct snd_soc_dapm_widget_list *list = *list_;
889 struct snd_soc_dapm_widget *widget;
890 int prune = 0;
891
892 /* Destroy any old FE <--> BE connections */
893 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
894
895 /* is there a valid CPU DAI widget for this BE */
896 widget = rtd_get_cpu_widget(dpcm->be, stream);
897
898 /* prune the BE if it's no longer in our active list */
899 if (widget && widget_in_list(list, widget))
900 continue;
901
902 /* is there a valid CODEC DAI widget for this BE */
903 widget = rtd_get_codec_widget(dpcm->be, stream);
904
905 /* prune the BE if it's no longer in our active list */
906 if (widget && widget_in_list(list, widget))
907 continue;
908
Liam Girdwood103d84a2012-11-19 14:39:15 +0000909 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100910 stream ? "capture" : "playback",
911 dpcm->be->dai_link->name, fe->dai_link->name);
912 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
913 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
914 prune++;
915 }
916
Liam Girdwood103d84a2012-11-19 14:39:15 +0000917 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +0100918 return prune;
919}
920
921static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
922 struct snd_soc_dapm_widget_list **list_)
923{
924 struct snd_soc_card *card = fe->card;
925 struct snd_soc_dapm_widget_list *list = *list_;
926 struct snd_soc_pcm_runtime *be;
927 int i, new = 0, err;
928
929 /* Create any new FE <--> BE connections */
930 for (i = 0; i < list->num_widgets; i++) {
931
932 if (list->widgets[i]->id != snd_soc_dapm_dai)
933 continue;
934
935 /* is there a valid BE rtd for this widget */
936 be = dpcm_get_be(card, list->widgets[i], stream);
937 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000938 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100939 list->widgets[i]->name);
940 continue;
941 }
942
943 /* make sure BE is a real BE */
944 if (!be->dai_link->no_pcm)
945 continue;
946
947 /* don't connect if FE is not running */
948 if (!fe->dpcm[stream].runtime)
949 continue;
950
951 /* newly connected FE and BE */
952 err = dpcm_be_connect(fe, be, stream);
953 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000954 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100955 list->widgets[i]->name);
956 break;
957 } else if (err == 0) /* already connected */
958 continue;
959
960 /* new */
961 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
962 new++;
963 }
964
Liam Girdwood103d84a2012-11-19 14:39:15 +0000965 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +0100966 return new;
967}
968
969/*
970 * Find the corresponding BE DAIs that source or sink audio to this
971 * FE substream.
972 */
973static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
974 int stream, struct snd_soc_dapm_widget_list **list, int new)
975{
976 if (new)
977 return dpcm_add_paths(fe, stream, list);
978 else
979 return dpcm_prune_paths(fe, stream, list);
980}
981
982static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
983{
984 struct snd_soc_dpcm *dpcm;
985
986 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
987 dpcm->be->dpcm[stream].runtime_update =
988 SND_SOC_DPCM_UPDATE_NO;
989}
990
991static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
992 int stream)
993{
994 struct snd_soc_dpcm *dpcm;
995
996 /* disable any enabled and non active backends */
997 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
998
999 struct snd_soc_pcm_runtime *be = dpcm->be;
1000 struct snd_pcm_substream *be_substream =
1001 snd_soc_dpcm_get_substream(be, stream);
1002
1003 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001004 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001005 stream ? "capture" : "playback",
1006 be->dpcm[stream].state);
1007
1008 if (--be->dpcm[stream].users != 0)
1009 continue;
1010
1011 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1012 continue;
1013
1014 soc_pcm_close(be_substream);
1015 be_substream->runtime = NULL;
1016 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1017 }
1018}
1019
1020static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1021{
1022 struct snd_soc_dpcm *dpcm;
1023 int err, count = 0;
1024
1025 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1026 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1027
1028 struct snd_soc_pcm_runtime *be = dpcm->be;
1029 struct snd_pcm_substream *be_substream =
1030 snd_soc_dpcm_get_substream(be, stream);
1031
1032 /* is this op for this BE ? */
1033 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1034 continue;
1035
1036 /* first time the dpcm is open ? */
1037 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001038 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001039 stream ? "capture" : "playback",
1040 be->dpcm[stream].state);
1041
1042 if (be->dpcm[stream].users++ != 0)
1043 continue;
1044
1045 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1046 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1047 continue;
1048
Liam Girdwood103d84a2012-11-19 14:39:15 +00001049 dev_dbg(be->dev, "ASoC: open BE %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001050
1051 be_substream->runtime = be->dpcm[stream].runtime;
1052 err = soc_pcm_open(be_substream);
1053 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001054 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001055 be->dpcm[stream].users--;
1056 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001057 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001058 stream ? "capture" : "playback",
1059 be->dpcm[stream].state);
1060
1061 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1062 goto unwind;
1063 }
1064
1065 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1066 count++;
1067 }
1068
1069 return count;
1070
1071unwind:
1072 /* disable any enabled and non active backends */
1073 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1074 struct snd_soc_pcm_runtime *be = dpcm->be;
1075 struct snd_pcm_substream *be_substream =
1076 snd_soc_dpcm_get_substream(be, stream);
1077
1078 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1079 continue;
1080
1081 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001082 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001083 stream ? "capture" : "playback",
1084 be->dpcm[stream].state);
1085
1086 if (--be->dpcm[stream].users != 0)
1087 continue;
1088
1089 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1090 continue;
1091
1092 soc_pcm_close(be_substream);
1093 be_substream->runtime = NULL;
1094 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1095 }
1096
1097 return err;
1098}
1099
Mark Brown45c0a182012-05-09 21:46:27 +01001100static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001101{
1102 struct snd_pcm_runtime *runtime = substream->runtime;
1103 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1104 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1105 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1106
1107 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1108 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1109 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1110 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1111 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1112 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1113 runtime->hw.rates = cpu_dai_drv->playback.rates;
1114 } else {
1115 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1116 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1117 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1118 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1119 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1120 runtime->hw.rates = cpu_dai_drv->capture.rates;
1121 }
1122}
1123
1124static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1125{
1126 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1127 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1128 int stream = fe_substream->stream, ret = 0;
1129
1130 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1131
1132 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1133 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001134 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001135 goto be_err;
1136 }
1137
Liam Girdwood103d84a2012-11-19 14:39:15 +00001138 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001139
1140 /* start the DAI frontend */
1141 ret = soc_pcm_open(fe_substream);
1142 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001143 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001144 goto unwind;
1145 }
1146
1147 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1148
1149 dpcm_set_fe_runtime(fe_substream);
1150 snd_pcm_limit_hw_rates(runtime);
1151
1152 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1153 return 0;
1154
1155unwind:
1156 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1157be_err:
1158 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1159 return ret;
1160}
1161
1162static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1163{
1164 struct snd_soc_dpcm *dpcm;
1165
1166 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1167 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1168
1169 struct snd_soc_pcm_runtime *be = dpcm->be;
1170 struct snd_pcm_substream *be_substream =
1171 snd_soc_dpcm_get_substream(be, stream);
1172
1173 /* is this op for this BE ? */
1174 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1175 continue;
1176
1177 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001178 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001179 stream ? "capture" : "playback",
1180 be->dpcm[stream].state);
1181
1182 if (--be->dpcm[stream].users != 0)
1183 continue;
1184
1185 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1186 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1187 continue;
1188
Liam Girdwood103d84a2012-11-19 14:39:15 +00001189 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001190 dpcm->fe->dai_link->name);
1191
1192 soc_pcm_close(be_substream);
1193 be_substream->runtime = NULL;
1194
1195 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1196 }
1197 return 0;
1198}
1199
1200static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1201{
1202 struct snd_soc_pcm_runtime *fe = substream->private_data;
1203 int stream = substream->stream;
1204
1205 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1206
1207 /* shutdown the BEs */
1208 dpcm_be_dai_shutdown(fe, substream->stream);
1209
Liam Girdwood103d84a2012-11-19 14:39:15 +00001210 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001211
1212 /* now shutdown the frontend */
1213 soc_pcm_close(substream);
1214
1215 /* run the stream event for each BE */
1216 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1217
1218 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1219 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1220 return 0;
1221}
1222
1223static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1224{
1225 struct snd_soc_dpcm *dpcm;
1226
1227 /* only hw_params backends that are either sinks or sources
1228 * to this frontend DAI */
1229 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1230
1231 struct snd_soc_pcm_runtime *be = dpcm->be;
1232 struct snd_pcm_substream *be_substream =
1233 snd_soc_dpcm_get_substream(be, stream);
1234
1235 /* is this op for this BE ? */
1236 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1237 continue;
1238
1239 /* only free hw when no longer used - check all FEs */
1240 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1241 continue;
1242
1243 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1244 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1245 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001246 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001247 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1248 continue;
1249
Liam Girdwood103d84a2012-11-19 14:39:15 +00001250 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001251 dpcm->fe->dai_link->name);
1252
1253 soc_pcm_hw_free(be_substream);
1254
1255 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1256 }
1257
1258 return 0;
1259}
1260
Mark Brown45c0a182012-05-09 21:46:27 +01001261static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001262{
1263 struct snd_soc_pcm_runtime *fe = substream->private_data;
1264 int err, stream = substream->stream;
1265
1266 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1267 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1268
Liam Girdwood103d84a2012-11-19 14:39:15 +00001269 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001270
1271 /* call hw_free on the frontend */
1272 err = soc_pcm_hw_free(substream);
1273 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001274 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001275 fe->dai_link->name);
1276
1277 /* only hw_params backends that are either sinks or sources
1278 * to this frontend DAI */
1279 err = dpcm_be_dai_hw_free(fe, stream);
1280
1281 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1282 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1283
1284 mutex_unlock(&fe->card->mutex);
1285 return 0;
1286}
1287
1288static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1289{
1290 struct snd_soc_dpcm *dpcm;
1291 int ret;
1292
1293 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1294
1295 struct snd_soc_pcm_runtime *be = dpcm->be;
1296 struct snd_pcm_substream *be_substream =
1297 snd_soc_dpcm_get_substream(be, stream);
1298
1299 /* is this op for this BE ? */
1300 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1301 continue;
1302
1303 /* only allow hw_params() if no connected FEs are running */
1304 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1305 continue;
1306
1307 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1308 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1309 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1310 continue;
1311
Liam Girdwood103d84a2012-11-19 14:39:15 +00001312 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001313 dpcm->fe->dai_link->name);
1314
1315 /* copy params for each dpcm */
1316 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1317 sizeof(struct snd_pcm_hw_params));
1318
1319 /* perform any hw_params fixups */
1320 if (be->dai_link->be_hw_params_fixup) {
1321 ret = be->dai_link->be_hw_params_fixup(be,
1322 &dpcm->hw_params);
1323 if (ret < 0) {
1324 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001325 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001326 ret);
1327 goto unwind;
1328 }
1329 }
1330
1331 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1332 if (ret < 0) {
1333 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001334 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001335 goto unwind;
1336 }
1337
1338 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1339 }
1340 return 0;
1341
1342unwind:
1343 /* disable any enabled and non active backends */
1344 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1345 struct snd_soc_pcm_runtime *be = dpcm->be;
1346 struct snd_pcm_substream *be_substream =
1347 snd_soc_dpcm_get_substream(be, stream);
1348
1349 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1350 continue;
1351
1352 /* only allow hw_free() if no connected FEs are running */
1353 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1354 continue;
1355
1356 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1357 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1358 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1359 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1360 continue;
1361
1362 soc_pcm_hw_free(be_substream);
1363 }
1364
1365 return ret;
1366}
1367
Mark Brown45c0a182012-05-09 21:46:27 +01001368static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1369 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001370{
1371 struct snd_soc_pcm_runtime *fe = substream->private_data;
1372 int ret, stream = substream->stream;
1373
1374 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1375 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1376
1377 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1378 sizeof(struct snd_pcm_hw_params));
1379 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1380 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001381 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001382 goto out;
1383 }
1384
Liam Girdwood103d84a2012-11-19 14:39:15 +00001385 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001386 fe->dai_link->name, params_rate(params),
1387 params_channels(params), params_format(params));
1388
1389 /* call hw_params on the frontend */
1390 ret = soc_pcm_hw_params(substream, params);
1391 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001392 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001393 dpcm_be_dai_hw_free(fe, stream);
1394 } else
1395 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1396
1397out:
1398 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1399 mutex_unlock(&fe->card->mutex);
1400 return ret;
1401}
1402
1403static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1404 struct snd_pcm_substream *substream, int cmd)
1405{
1406 int ret;
1407
Liam Girdwood103d84a2012-11-19 14:39:15 +00001408 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001409 dpcm->fe->dai_link->name, cmd);
1410
1411 ret = soc_pcm_trigger(substream, cmd);
1412 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001413 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001414
1415 return ret;
1416}
1417
Mark Brown45c0a182012-05-09 21:46:27 +01001418static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1419 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001420{
1421 struct snd_soc_dpcm *dpcm;
1422 int ret = 0;
1423
1424 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1425
1426 struct snd_soc_pcm_runtime *be = dpcm->be;
1427 struct snd_pcm_substream *be_substream =
1428 snd_soc_dpcm_get_substream(be, stream);
1429
1430 /* is this op for this BE ? */
1431 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1432 continue;
1433
1434 switch (cmd) {
1435 case SNDRV_PCM_TRIGGER_START:
1436 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1437 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1438 continue;
1439
1440 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1441 if (ret)
1442 return ret;
1443
1444 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1445 break;
1446 case SNDRV_PCM_TRIGGER_RESUME:
1447 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1448 continue;
1449
1450 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1451 if (ret)
1452 return ret;
1453
1454 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1455 break;
1456 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1457 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1458 continue;
1459
1460 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1461 if (ret)
1462 return ret;
1463
1464 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1465 break;
1466 case SNDRV_PCM_TRIGGER_STOP:
1467 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1468 continue;
1469
1470 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1471 continue;
1472
1473 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1474 if (ret)
1475 return ret;
1476
1477 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1478 break;
1479 case SNDRV_PCM_TRIGGER_SUSPEND:
1480 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1481 continue;
1482
1483 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1484 continue;
1485
1486 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1487 if (ret)
1488 return ret;
1489
1490 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1491 break;
1492 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1493 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1494 continue;
1495
1496 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1497 continue;
1498
1499 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1500 if (ret)
1501 return ret;
1502
1503 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1504 break;
1505 }
1506 }
1507
1508 return ret;
1509}
1510EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1511
Mark Brown45c0a182012-05-09 21:46:27 +01001512static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001513{
1514 struct snd_soc_pcm_runtime *fe = substream->private_data;
1515 int stream = substream->stream, ret;
1516 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1517
1518 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1519
1520 switch (trigger) {
1521 case SND_SOC_DPCM_TRIGGER_PRE:
1522 /* call trigger on the frontend before the backend. */
1523
Liam Girdwood103d84a2012-11-19 14:39:15 +00001524 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001525 fe->dai_link->name, cmd);
1526
1527 ret = soc_pcm_trigger(substream, cmd);
1528 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001529 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001530 goto out;
1531 }
1532
1533 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1534 break;
1535 case SND_SOC_DPCM_TRIGGER_POST:
1536 /* call trigger on the frontend after the backend. */
1537
1538 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1539 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001540 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001541 goto out;
1542 }
1543
Liam Girdwood103d84a2012-11-19 14:39:15 +00001544 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001545 fe->dai_link->name, cmd);
1546
1547 ret = soc_pcm_trigger(substream, cmd);
1548 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001549 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1550 /* bespoke trigger() - handles both FE and BEs */
1551
Liam Girdwood103d84a2012-11-19 14:39:15 +00001552 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001553 fe->dai_link->name, cmd);
1554
1555 ret = soc_pcm_bespoke_trigger(substream, cmd);
1556 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001557 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001558 goto out;
1559 }
1560 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01001561 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00001562 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01001563 fe->dai_link->name);
1564 ret = -EINVAL;
1565 goto out;
1566 }
1567
1568 switch (cmd) {
1569 case SNDRV_PCM_TRIGGER_START:
1570 case SNDRV_PCM_TRIGGER_RESUME:
1571 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1572 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1573 break;
1574 case SNDRV_PCM_TRIGGER_STOP:
1575 case SNDRV_PCM_TRIGGER_SUSPEND:
1576 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1577 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1578 break;
1579 }
1580
1581out:
1582 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1583 return ret;
1584}
1585
1586static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1587{
1588 struct snd_soc_dpcm *dpcm;
1589 int ret = 0;
1590
1591 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1592
1593 struct snd_soc_pcm_runtime *be = dpcm->be;
1594 struct snd_pcm_substream *be_substream =
1595 snd_soc_dpcm_get_substream(be, stream);
1596
1597 /* is this op for this BE ? */
1598 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1599 continue;
1600
1601 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1602 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1603 continue;
1604
Liam Girdwood103d84a2012-11-19 14:39:15 +00001605 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001606 dpcm->fe->dai_link->name);
1607
1608 ret = soc_pcm_prepare(be_substream);
1609 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001610 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001611 ret);
1612 break;
1613 }
1614
1615 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1616 }
1617 return ret;
1618}
1619
Mark Brown45c0a182012-05-09 21:46:27 +01001620static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001621{
1622 struct snd_soc_pcm_runtime *fe = substream->private_data;
1623 int stream = substream->stream, ret = 0;
1624
1625 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1626
Liam Girdwood103d84a2012-11-19 14:39:15 +00001627 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001628
1629 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1630
1631 /* there is no point preparing this FE if there are no BEs */
1632 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001633 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001634 fe->dai_link->name);
1635 ret = -EINVAL;
1636 goto out;
1637 }
1638
1639 ret = dpcm_be_dai_prepare(fe, substream->stream);
1640 if (ret < 0)
1641 goto out;
1642
1643 /* call prepare on the frontend */
1644 ret = soc_pcm_prepare(substream);
1645 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001646 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001647 fe->dai_link->name);
1648 goto out;
1649 }
1650
1651 /* run the stream event for each BE */
1652 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1653 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1654
1655out:
1656 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1657 mutex_unlock(&fe->card->mutex);
1658
1659 return ret;
1660}
1661
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001662static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1663 unsigned int cmd, void *arg)
1664{
1665 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1666 struct snd_soc_platform *platform = rtd->platform;
1667
1668 if (platform->driver->ops->ioctl)
1669 return platform->driver->ops->ioctl(substream, cmd, arg);
1670 return snd_pcm_lib_ioctl(substream, cmd, arg);
1671}
1672
Liam Girdwood618dae12012-04-25 12:12:51 +01001673static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1674{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001675 struct snd_pcm_substream *substream =
1676 snd_soc_dpcm_get_substream(fe, stream);
1677 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001678 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01001679
Liam Girdwood103d84a2012-11-19 14:39:15 +00001680 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001681 stream ? "capture" : "playback", fe->dai_link->name);
1682
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001683 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1684 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001685 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001686 fe->dai_link->name);
1687
1688 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1689 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001690 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001691 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001692 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001693 fe->dai_link->name);
1694
1695 err = dpcm_be_dai_trigger(fe, stream, 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 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001699
1700 err = dpcm_be_dai_hw_free(fe, stream);
1701 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001702 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001703
1704 err = dpcm_be_dai_shutdown(fe, stream);
1705 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001706 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001707
1708 /* run the stream event for each BE */
1709 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1710
1711 return 0;
1712}
1713
1714static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1715{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001716 struct snd_pcm_substream *substream =
1717 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01001718 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001719 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001720 int ret;
1721
Liam Girdwood103d84a2012-11-19 14:39:15 +00001722 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001723 stream ? "capture" : "playback", fe->dai_link->name);
1724
1725 /* Only start the BE if the FE is ready */
1726 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1727 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1728 return -EINVAL;
1729
1730 /* startup must always be called for new BEs */
1731 ret = dpcm_be_dai_startup(fe, stream);
1732 if (ret < 0) {
1733 goto disconnect;
1734 return ret;
1735 }
1736
1737 /* keep going if FE state is > open */
1738 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1739 return 0;
1740
1741 ret = dpcm_be_dai_hw_params(fe, stream);
1742 if (ret < 0) {
1743 goto close;
1744 return ret;
1745 }
1746
1747 /* keep going if FE state is > hw_params */
1748 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1749 return 0;
1750
1751
1752 ret = dpcm_be_dai_prepare(fe, stream);
1753 if (ret < 0) {
1754 goto hw_free;
1755 return ret;
1756 }
1757
1758 /* run the stream event for each BE */
1759 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1760
1761 /* keep going if FE state is > prepare */
1762 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1763 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1764 return 0;
1765
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001766 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1767 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001768 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001769 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01001770
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001771 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1772 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001773 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001774 goto hw_free;
1775 }
1776 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001777 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001778 fe->dai_link->name);
1779
1780 ret = dpcm_be_dai_trigger(fe, stream,
1781 SNDRV_PCM_TRIGGER_START);
1782 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001783 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001784 goto hw_free;
1785 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001786 }
1787
1788 return 0;
1789
1790hw_free:
1791 dpcm_be_dai_hw_free(fe, stream);
1792close:
1793 dpcm_be_dai_shutdown(fe, stream);
1794disconnect:
1795 /* disconnect any non started BEs */
1796 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1797 struct snd_soc_pcm_runtime *be = dpcm->be;
1798 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1799 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1800 }
1801
1802 return ret;
1803}
1804
1805static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1806{
1807 int ret;
1808
1809 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1810 ret = dpcm_run_update_startup(fe, stream);
1811 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001812 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001813 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1814
1815 return ret;
1816}
1817
1818static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1819{
1820 int ret;
1821
1822 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1823 ret = dpcm_run_update_shutdown(fe, stream);
1824 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001825 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001826 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1827
1828 return ret;
1829}
1830
1831/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1832 * any DAI links.
1833 */
1834int soc_dpcm_runtime_update(struct snd_soc_dapm_widget *widget)
1835{
1836 struct snd_soc_card *card;
1837 int i, old, new, paths;
1838
1839 if (widget->codec)
1840 card = widget->codec->card;
1841 else if (widget->platform)
1842 card = widget->platform->card;
1843 else
1844 return -EINVAL;
1845
1846 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1847 for (i = 0; i < card->num_rtd; i++) {
1848 struct snd_soc_dapm_widget_list *list;
1849 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1850
1851 /* make sure link is FE */
1852 if (!fe->dai_link->dynamic)
1853 continue;
1854
1855 /* only check active links */
1856 if (!fe->cpu_dai->active)
1857 continue;
1858
1859 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001860 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001861 fe->dai_link->name);
1862
1863 /* skip if FE doesn't have playback capability */
1864 if (!fe->cpu_dai->driver->playback.channels_min)
1865 goto capture;
1866
1867 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1868 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001869 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001870 fe->dai_link->name, "playback");
1871 mutex_unlock(&card->mutex);
1872 return paths;
1873 }
1874
1875 /* update any new playback paths */
1876 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1877 if (new) {
1878 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1879 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1880 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1881 }
1882
1883 /* update any old playback paths */
1884 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1885 if (old) {
1886 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1887 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1888 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1889 }
1890
1891capture:
1892 /* skip if FE doesn't have capture capability */
1893 if (!fe->cpu_dai->driver->capture.channels_min)
1894 continue;
1895
1896 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1897 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001898 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001899 fe->dai_link->name, "capture");
1900 mutex_unlock(&card->mutex);
1901 return paths;
1902 }
1903
1904 /* update any new capture paths */
1905 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1906 if (new) {
1907 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1908 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1909 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1910 }
1911
1912 /* update any old capture paths */
1913 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1914 if (old) {
1915 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1916 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1917 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1918 }
1919
1920 dpcm_path_put(&list);
1921 }
1922
1923 mutex_unlock(&card->mutex);
1924 return 0;
1925}
Liam Girdwood01d75842012-04-25 12:12:49 +01001926int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
1927{
1928 struct snd_soc_dpcm *dpcm;
1929 struct list_head *clients =
1930 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
1931
1932 list_for_each_entry(dpcm, clients, list_be) {
1933
1934 struct snd_soc_pcm_runtime *be = dpcm->be;
1935 struct snd_soc_dai *dai = be->codec_dai;
1936 struct snd_soc_dai_driver *drv = dai->driver;
1937
1938 if (be->dai_link->ignore_suspend)
1939 continue;
1940
Liam Girdwood103d84a2012-11-19 14:39:15 +00001941 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001942
1943 if (drv->ops->digital_mute && dai->playback_active)
1944 drv->ops->digital_mute(dai, mute);
1945 }
1946
1947 return 0;
1948}
1949
Mark Brown45c0a182012-05-09 21:46:27 +01001950static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001951{
1952 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1953 struct snd_soc_dpcm *dpcm;
1954 struct snd_soc_dapm_widget_list *list;
1955 int ret;
1956 int stream = fe_substream->stream;
1957
1958 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1959 fe->dpcm[stream].runtime = fe_substream->runtime;
1960
1961 if (dpcm_path_get(fe, stream, &list) <= 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001962 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001963 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01001964 }
1965
1966 /* calculate valid and active FE <-> BE dpcms */
1967 dpcm_process_paths(fe, stream, &list, 1);
1968
1969 ret = dpcm_fe_dai_startup(fe_substream);
1970 if (ret < 0) {
1971 /* clean up all links */
1972 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1973 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1974
1975 dpcm_be_disconnect(fe, stream);
1976 fe->dpcm[stream].runtime = NULL;
1977 }
1978
1979 dpcm_clear_pending_state(fe, stream);
1980 dpcm_path_put(&list);
1981 mutex_unlock(&fe->card->mutex);
1982 return ret;
1983}
1984
Mark Brown45c0a182012-05-09 21:46:27 +01001985static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001986{
1987 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1988 struct snd_soc_dpcm *dpcm;
1989 int stream = fe_substream->stream, ret;
1990
1991 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1992 ret = dpcm_fe_dai_shutdown(fe_substream);
1993
1994 /* mark FE's links ready to prune */
1995 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1996 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1997
1998 dpcm_be_disconnect(fe, stream);
1999
2000 fe->dpcm[stream].runtime = NULL;
2001 mutex_unlock(&fe->card->mutex);
2002 return ret;
2003}
2004
Liam Girdwoodddee6272011-06-09 14:45:53 +01002005/* create a new pcm */
2006int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2007{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002008 struct snd_soc_platform *platform = rtd->platform;
2009 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2010 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2011 struct snd_pcm *pcm;
2012 char new_name[64];
2013 int ret = 0, playback = 0, capture = 0;
2014
Liam Girdwood01d75842012-04-25 12:12:49 +01002015 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2016 if (cpu_dai->driver->playback.channels_min)
2017 playback = 1;
2018 if (cpu_dai->driver->capture.channels_min)
2019 capture = 1;
2020 } else {
2021 if (codec_dai->driver->playback.channels_min)
2022 playback = 1;
2023 if (codec_dai->driver->capture.channels_min)
2024 capture = 1;
2025 }
Sangsu Parka5002312012-01-02 17:15:10 +09002026
Liam Girdwood01d75842012-04-25 12:12:49 +01002027 /* create the PCM */
2028 if (rtd->dai_link->no_pcm) {
2029 snprintf(new_name, sizeof(new_name), "(%s)",
2030 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002031
Liam Girdwood01d75842012-04-25 12:12:49 +01002032 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2033 playback, capture, &pcm);
2034 } else {
2035 if (rtd->dai_link->dynamic)
2036 snprintf(new_name, sizeof(new_name), "%s (*)",
2037 rtd->dai_link->stream_name);
2038 else
2039 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2040 rtd->dai_link->stream_name, codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002041
Liam Girdwood01d75842012-04-25 12:12:49 +01002042 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2043 capture, &pcm);
2044 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002045 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002046 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002047 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002048 return ret;
2049 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002050 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002051
2052 /* DAPM dai link stream work */
2053 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2054
2055 rtd->pcm = pcm;
2056 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002057
2058 if (rtd->dai_link->no_pcm) {
2059 if (playback)
2060 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2061 if (capture)
2062 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2063 goto out;
2064 }
2065
2066 /* ASoC PCM operations */
2067 if (rtd->dai_link->dynamic) {
2068 rtd->ops.open = dpcm_fe_dai_open;
2069 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2070 rtd->ops.prepare = dpcm_fe_dai_prepare;
2071 rtd->ops.trigger = dpcm_fe_dai_trigger;
2072 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2073 rtd->ops.close = dpcm_fe_dai_close;
2074 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002075 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002076 } else {
2077 rtd->ops.open = soc_pcm_open;
2078 rtd->ops.hw_params = soc_pcm_hw_params;
2079 rtd->ops.prepare = soc_pcm_prepare;
2080 rtd->ops.trigger = soc_pcm_trigger;
2081 rtd->ops.hw_free = soc_pcm_hw_free;
2082 rtd->ops.close = soc_pcm_close;
2083 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002084 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002085 }
2086
Liam Girdwoodddee6272011-06-09 14:45:53 +01002087 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002088 rtd->ops.ack = platform->driver->ops->ack;
2089 rtd->ops.copy = platform->driver->ops->copy;
2090 rtd->ops.silence = platform->driver->ops->silence;
2091 rtd->ops.page = platform->driver->ops->page;
2092 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002093 }
2094
2095 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002096 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002097
2098 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002099 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002100
2101 if (platform->driver->pcm_new) {
2102 ret = platform->driver->pcm_new(rtd);
2103 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002104 dev_err(platform->dev,
2105 "ASoC: pcm constructor failed: %d\n",
2106 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002107 return ret;
2108 }
2109 }
2110
2111 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002112out:
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002113 dev_info(rtd->card->dev, " %s <-> %s mapping ok\n", codec_dai->name,
Liam Girdwoodddee6272011-06-09 14:45:53 +01002114 cpu_dai->name);
2115 return ret;
2116}
Liam Girdwood01d75842012-04-25 12:12:49 +01002117
2118/* is the current PCM operation for this FE ? */
2119int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2120{
2121 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2122 return 1;
2123 return 0;
2124}
2125EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2126
2127/* is the current PCM operation for this BE ? */
2128int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2129 struct snd_soc_pcm_runtime *be, int stream)
2130{
2131 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2132 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2133 be->dpcm[stream].runtime_update))
2134 return 1;
2135 return 0;
2136}
2137EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2138
2139/* get the substream for this BE */
2140struct snd_pcm_substream *
2141 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2142{
2143 return be->pcm->streams[stream].substream;
2144}
2145EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2146
2147/* get the BE runtime state */
2148enum snd_soc_dpcm_state
2149 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2150{
2151 return be->dpcm[stream].state;
2152}
2153EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2154
2155/* set the BE runtime state */
2156void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2157 int stream, enum snd_soc_dpcm_state state)
2158{
2159 be->dpcm[stream].state = state;
2160}
2161EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2162
2163/*
2164 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2165 * are not running, paused or suspended for the specified stream direction.
2166 */
2167int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2168 struct snd_soc_pcm_runtime *be, int stream)
2169{
2170 struct snd_soc_dpcm *dpcm;
2171 int state;
2172
2173 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2174
2175 if (dpcm->fe == fe)
2176 continue;
2177
2178 state = dpcm->fe->dpcm[stream].state;
2179 if (state == SND_SOC_DPCM_STATE_START ||
2180 state == SND_SOC_DPCM_STATE_PAUSED ||
2181 state == SND_SOC_DPCM_STATE_SUSPEND)
2182 return 0;
2183 }
2184
2185 /* it's safe to free/stop this BE DAI */
2186 return 1;
2187}
2188EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2189
2190/*
2191 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2192 * running, paused or suspended for the specified stream direction.
2193 */
2194int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2195 struct snd_soc_pcm_runtime *be, int stream)
2196{
2197 struct snd_soc_dpcm *dpcm;
2198 int state;
2199
2200 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2201
2202 if (dpcm->fe == fe)
2203 continue;
2204
2205 state = dpcm->fe->dpcm[stream].state;
2206 if (state == SND_SOC_DPCM_STATE_START ||
2207 state == SND_SOC_DPCM_STATE_PAUSED ||
2208 state == SND_SOC_DPCM_STATE_SUSPEND ||
2209 state == SND_SOC_DPCM_STATE_PREPARE)
2210 return 0;
2211 }
2212
2213 /* it's safe to change hw_params */
2214 return 1;
2215}
2216EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002217
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002218int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2219 int cmd, struct snd_soc_platform *platform)
2220{
2221 if (platform->driver->ops->trigger)
2222 return platform->driver->ops->trigger(substream, cmd);
2223 return 0;
2224}
2225EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2226
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002227#ifdef CONFIG_DEBUG_FS
2228static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2229{
2230 switch (state) {
2231 case SND_SOC_DPCM_STATE_NEW:
2232 return "new";
2233 case SND_SOC_DPCM_STATE_OPEN:
2234 return "open";
2235 case SND_SOC_DPCM_STATE_HW_PARAMS:
2236 return "hw_params";
2237 case SND_SOC_DPCM_STATE_PREPARE:
2238 return "prepare";
2239 case SND_SOC_DPCM_STATE_START:
2240 return "start";
2241 case SND_SOC_DPCM_STATE_STOP:
2242 return "stop";
2243 case SND_SOC_DPCM_STATE_SUSPEND:
2244 return "suspend";
2245 case SND_SOC_DPCM_STATE_PAUSED:
2246 return "paused";
2247 case SND_SOC_DPCM_STATE_HW_FREE:
2248 return "hw_free";
2249 case SND_SOC_DPCM_STATE_CLOSE:
2250 return "close";
2251 }
2252
2253 return "unknown";
2254}
2255
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002256static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2257 int stream, char *buf, size_t size)
2258{
2259 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2260 struct snd_soc_dpcm *dpcm;
2261 ssize_t offset = 0;
2262
2263 /* FE state */
2264 offset += snprintf(buf + offset, size - offset,
2265 "[%s - %s]\n", fe->dai_link->name,
2266 stream ? "Capture" : "Playback");
2267
2268 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2269 dpcm_state_string(fe->dpcm[stream].state));
2270
2271 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2272 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2273 offset += snprintf(buf + offset, size - offset,
2274 "Hardware Params: "
2275 "Format = %s, Channels = %d, Rate = %d\n",
2276 snd_pcm_format_name(params_format(params)),
2277 params_channels(params),
2278 params_rate(params));
2279
2280 /* BEs state */
2281 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2282
2283 if (list_empty(&fe->dpcm[stream].be_clients)) {
2284 offset += snprintf(buf + offset, size - offset,
2285 " No active DSP links\n");
2286 goto out;
2287 }
2288
2289 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2290 struct snd_soc_pcm_runtime *be = dpcm->be;
2291 params = &dpcm->hw_params;
2292
2293 offset += snprintf(buf + offset, size - offset,
2294 "- %s\n", be->dai_link->name);
2295
2296 offset += snprintf(buf + offset, size - offset,
2297 " State: %s\n",
2298 dpcm_state_string(be->dpcm[stream].state));
2299
2300 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2301 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2302 offset += snprintf(buf + offset, size - offset,
2303 " Hardware Params: "
2304 "Format = %s, Channels = %d, Rate = %d\n",
2305 snd_pcm_format_name(params_format(params)),
2306 params_channels(params),
2307 params_rate(params));
2308 }
2309
2310out:
2311 return offset;
2312}
2313
2314static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2315 size_t count, loff_t *ppos)
2316{
2317 struct snd_soc_pcm_runtime *fe = file->private_data;
2318 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2319 char *buf;
2320
2321 buf = kmalloc(out_count, GFP_KERNEL);
2322 if (!buf)
2323 return -ENOMEM;
2324
2325 if (fe->cpu_dai->driver->playback.channels_min)
2326 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2327 buf + offset, out_count - offset);
2328
2329 if (fe->cpu_dai->driver->capture.channels_min)
2330 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2331 buf + offset, out_count - offset);
2332
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002333 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002334
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002335 kfree(buf);
2336 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002337}
2338
2339static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002340 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002341 .read = dpcm_state_read_file,
2342 .llseek = default_llseek,
2343};
2344
2345int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2346{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002347 if (!rtd->dai_link)
2348 return 0;
2349
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002350 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2351 rtd->card->debugfs_card_root);
2352 if (!rtd->debugfs_dpcm_root) {
2353 dev_dbg(rtd->dev,
2354 "ASoC: Failed to create dpcm debugfs directory %s\n",
2355 rtd->dai_link->name);
2356 return -EINVAL;
2357 }
2358
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002359 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002360 rtd->debugfs_dpcm_root,
2361 rtd, &dpcm_state_fops);
2362
2363 return 0;
2364}
2365#endif