blob: ed1e077114a2601d322fed9a4c8efe516e81bb9f [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>
Nicolin Chen988e8cc2013-11-04 14:57:31 +080022#include <linux/pinctrl/consumer.h>
Mark Brownd6652ef2011-12-03 20:14:31 +000023#include <linux/pm_runtime.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010024#include <linux/slab.h>
25#include <linux/workqueue.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010026#include <linux/export.h>
Liam Girdwoodf86dcef2012-04-25 12:12:50 +010027#include <linux/debugfs.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010028#include <sound/core.h>
29#include <sound/pcm.h>
30#include <sound/pcm_params.h>
31#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010032#include <sound/soc-dpcm.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010033#include <sound/initval.h>
34
Liam Girdwood01d75842012-04-25 12:12:49 +010035#define DPCM_MAX_BE_USERS 8
36
Lars-Peter Clausen90996f42013-05-14 11:05:30 +020037/**
38 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
39 * @substream: the pcm substream
40 * @hw: the hardware parameters
41 *
42 * Sets the substream runtime hardware parameters.
43 */
44int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
45 const struct snd_pcm_hardware *hw)
46{
47 struct snd_pcm_runtime *runtime = substream->runtime;
48 runtime->hw.info = hw->info;
49 runtime->hw.formats = hw->formats;
50 runtime->hw.period_bytes_min = hw->period_bytes_min;
51 runtime->hw.period_bytes_max = hw->period_bytes_max;
52 runtime->hw.periods_min = hw->periods_min;
53 runtime->hw.periods_max = hw->periods_max;
54 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
55 runtime->hw.fifo_size = hw->fifo_size;
56 return 0;
57}
58EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
59
Liam Girdwood01d75842012-04-25 12:12:49 +010060/* DPCM stream event, send event to FE and all active BEs. */
61static int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
62 int event)
63{
64 struct snd_soc_dpcm *dpcm;
65
66 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
67
68 struct snd_soc_pcm_runtime *be = dpcm->be;
69
Liam Girdwood103d84a2012-11-19 14:39:15 +000070 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +010071 be->dai_link->name, event, dir);
72
73 snd_soc_dapm_stream_event(be, dir, event);
74 }
75
76 snd_soc_dapm_stream_event(fe, dir, event);
77
78 return 0;
79}
80
Dong Aisheng17841022011-08-29 17:15:14 +080081static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
82 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +010083{
84 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +010085 int ret;
86
Nicolin Chen3635bf02013-11-13 18:56:24 +080087 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
88 rtd->dai_link->symmetric_rates)) {
89 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
90 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +010091
Nicolin Chen3635bf02013-11-13 18:56:24 +080092 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
93 SNDRV_PCM_HW_PARAM_RATE,
94 soc_dai->rate, soc_dai->rate);
95 if (ret < 0) {
96 dev_err(soc_dai->dev,
97 "ASoC: Unable to apply rate constraint: %d\n",
98 ret);
99 return ret;
100 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100101 }
102
Nicolin Chen3635bf02013-11-13 18:56:24 +0800103 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
104 rtd->dai_link->symmetric_channels)) {
105 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
106 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100107
Nicolin Chen3635bf02013-11-13 18:56:24 +0800108 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
109 SNDRV_PCM_HW_PARAM_CHANNELS,
110 soc_dai->channels,
111 soc_dai->channels);
112 if (ret < 0) {
113 dev_err(soc_dai->dev,
114 "ASoC: Unable to apply channel symmetry constraint: %d\n",
115 ret);
116 return ret;
117 }
118 }
119
120 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
121 rtd->dai_link->symmetric_samplebits)) {
122 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
123 soc_dai->sample_bits);
124
125 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
126 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
127 soc_dai->sample_bits,
128 soc_dai->sample_bits);
129 if (ret < 0) {
130 dev_err(soc_dai->dev,
131 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
132 ret);
133 return ret;
134 }
135 }
136
137 return 0;
138}
139
140static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
141 struct snd_pcm_hw_params *params)
142{
143 struct snd_soc_pcm_runtime *rtd = substream->private_data;
144 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
145 struct snd_soc_dai *codec_dai = rtd->codec_dai;
146 unsigned int rate, channels, sample_bits, symmetry;
147
148 rate = params_rate(params);
149 channels = params_channels(params);
150 sample_bits = snd_pcm_format_physical_width(params_format(params));
151
152 /* reject unmatched parameters when applying symmetry */
153 symmetry = cpu_dai->driver->symmetric_rates ||
154 codec_dai->driver->symmetric_rates ||
155 rtd->dai_link->symmetric_rates;
156 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
157 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
158 cpu_dai->rate, rate);
159 return -EINVAL;
160 }
161
162 symmetry = cpu_dai->driver->symmetric_channels ||
163 codec_dai->driver->symmetric_channels ||
164 rtd->dai_link->symmetric_channels;
165 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
166 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
167 cpu_dai->channels, channels);
168 return -EINVAL;
169 }
170
171 symmetry = cpu_dai->driver->symmetric_samplebits ||
172 codec_dai->driver->symmetric_samplebits ||
173 rtd->dai_link->symmetric_samplebits;
174 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
175 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
176 cpu_dai->sample_bits, sample_bits);
177 return -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100178 }
179
180 return 0;
181}
182
183/*
Mark Brown58ba9b22012-01-16 18:38:51 +0000184 * List of sample sizes that might go over the bus for parameter
185 * application. There ought to be a wildcard sample size for things
186 * like the DAC/ADC resolution to use but there isn't right now.
187 */
188static int sample_sizes[] = {
Peter Ujfalusi88e33952012-01-25 10:09:41 +0200189 24, 32,
Mark Brown58ba9b22012-01-16 18:38:51 +0000190};
191
192static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
193 struct snd_soc_dai *dai)
194{
195 int ret, i, bits;
196
197 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
198 bits = dai->driver->playback.sig_bits;
199 else
200 bits = dai->driver->capture.sig_bits;
201
202 if (!bits)
203 return;
204
205 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
Mark Brown278047f2012-01-19 18:04:18 +0000206 if (bits >= sample_sizes[i])
207 continue;
208
209 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
210 sample_sizes[i], bits);
Mark Brown58ba9b22012-01-16 18:38:51 +0000211 if (ret != 0)
212 dev_warn(dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +0000213 "ASoC: Failed to set MSB %d/%d: %d\n",
Mark Brown58ba9b22012-01-16 18:38:51 +0000214 bits, sample_sizes[i], ret);
215 }
216}
217
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200218static void soc_pcm_init_runtime_hw(struct snd_pcm_hardware *hw,
219 struct snd_soc_pcm_stream *codec_stream,
220 struct snd_soc_pcm_stream *cpu_stream)
221{
222 hw->rate_min = max(codec_stream->rate_min, cpu_stream->rate_min);
223 hw->rate_max = max(codec_stream->rate_max, cpu_stream->rate_max);
224 hw->channels_min = max(codec_stream->channels_min,
225 cpu_stream->channels_min);
226 hw->channels_max = min(codec_stream->channels_max,
227 cpu_stream->channels_max);
228 hw->formats = codec_stream->formats & cpu_stream->formats;
229 hw->rates = codec_stream->rates & cpu_stream->rates;
230 if (codec_stream->rates
231 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
232 hw->rates |= cpu_stream->rates;
233 if (cpu_stream->rates
234 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
235 hw->rates |= codec_stream->rates;
236}
237
Mark Brown58ba9b22012-01-16 18:38:51 +0000238/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100239 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
240 * then initialized and any private data can be allocated. This also calls
241 * startup for the cpu DAI, platform, machine and codec DAI.
242 */
243static int soc_pcm_open(struct snd_pcm_substream *substream)
244{
245 struct snd_soc_pcm_runtime *rtd = substream->private_data;
246 struct snd_pcm_runtime *runtime = substream->runtime;
247 struct snd_soc_platform *platform = rtd->platform;
248 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
249 struct snd_soc_dai *codec_dai = rtd->codec_dai;
250 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
251 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
252 int ret = 0;
253
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800254 pinctrl_pm_select_default_state(cpu_dai->dev);
255 pinctrl_pm_select_default_state(codec_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000256 pm_runtime_get_sync(cpu_dai->dev);
257 pm_runtime_get_sync(codec_dai->dev);
258 pm_runtime_get_sync(platform->dev);
259
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100260 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100261
262 /* startup the audio subsystem */
Mark Brownc5914b02013-10-30 17:47:39 -0700263 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100264 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
265 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000266 dev_err(cpu_dai->dev, "ASoC: can't open interface"
267 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100268 goto out;
269 }
270 }
271
272 if (platform->driver->ops && platform->driver->ops->open) {
273 ret = platform->driver->ops->open(substream);
274 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000275 dev_err(platform->dev, "ASoC: can't open platform"
276 " %s: %d\n", platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100277 goto platform_err;
278 }
279 }
280
Mark Brownc5914b02013-10-30 17:47:39 -0700281 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100282 ret = codec_dai->driver->ops->startup(substream, codec_dai);
283 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000284 dev_err(codec_dai->dev, "ASoC: can't open codec"
285 " %s: %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100286 goto codec_dai_err;
287 }
288 }
289
290 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
291 ret = rtd->dai_link->ops->startup(substream);
292 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000293 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000294 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100295 goto machine_err;
296 }
297 }
298
Liam Girdwood01d75842012-04-25 12:12:49 +0100299 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
300 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
301 goto dynamic;
302
Liam Girdwoodddee6272011-06-09 14:45:53 +0100303 /* Check that the codec and cpu DAIs are compatible */
304 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200305 soc_pcm_init_runtime_hw(&runtime->hw, &codec_dai_drv->playback,
306 &cpu_dai_drv->playback);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100307 } else {
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200308 soc_pcm_init_runtime_hw(&runtime->hw, &codec_dai_drv->capture,
309 &cpu_dai_drv->capture);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100310 }
311
312 ret = -EINVAL;
313 snd_pcm_limit_hw_rates(runtime);
314 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000315 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100316 codec_dai->name, cpu_dai->name);
317 goto config_err;
318 }
319 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000320 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100321 codec_dai->name, cpu_dai->name);
322 goto config_err;
323 }
324 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
325 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000326 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100327 codec_dai->name, cpu_dai->name);
328 goto config_err;
329 }
330
Mark Brown58ba9b22012-01-16 18:38:51 +0000331 soc_pcm_apply_msb(substream, codec_dai);
332 soc_pcm_apply_msb(substream, cpu_dai);
333
Liam Girdwoodddee6272011-06-09 14:45:53 +0100334 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800335 if (cpu_dai->active) {
336 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
337 if (ret != 0)
338 goto config_err;
339 }
340
341 if (codec_dai->active) {
342 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100343 if (ret != 0)
344 goto config_err;
345 }
346
Liam Girdwood103d84a2012-11-19 14:39:15 +0000347 pr_debug("ASoC: %s <-> %s info:\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100348 codec_dai->name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000349 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
350 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100351 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000352 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100353 runtime->hw.rate_max);
354
Liam Girdwood01d75842012-04-25 12:12:49 +0100355dynamic:
Liam Girdwoodddee6272011-06-09 14:45:53 +0100356 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
357 cpu_dai->playback_active++;
358 codec_dai->playback_active++;
359 } else {
360 cpu_dai->capture_active++;
361 codec_dai->capture_active++;
362 }
363 cpu_dai->active++;
364 codec_dai->active++;
365 rtd->codec->active++;
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100366 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100367 return 0;
368
369config_err:
370 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
371 rtd->dai_link->ops->shutdown(substream);
372
373machine_err:
374 if (codec_dai->driver->ops->shutdown)
375 codec_dai->driver->ops->shutdown(substream, codec_dai);
376
377codec_dai_err:
378 if (platform->driver->ops && platform->driver->ops->close)
379 platform->driver->ops->close(substream);
380
381platform_err:
382 if (cpu_dai->driver->ops->shutdown)
383 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
384out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100385 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000386
387 pm_runtime_put(platform->dev);
388 pm_runtime_put(codec_dai->dev);
389 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800390 if (!codec_dai->active)
391 pinctrl_pm_select_sleep_state(codec_dai->dev);
392 if (!cpu_dai->active)
393 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000394
Liam Girdwoodddee6272011-06-09 14:45:53 +0100395 return ret;
396}
397
398/*
399 * Power down the audio subsystem pmdown_time msecs after close is called.
400 * This is to ensure there are no pops or clicks in between any music tracks
401 * due to DAPM power cycling.
402 */
403static void close_delayed_work(struct work_struct *work)
404{
405 struct snd_soc_pcm_runtime *rtd =
406 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
407 struct snd_soc_dai *codec_dai = rtd->codec_dai;
408
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100409 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100410
Liam Girdwood103d84a2012-11-19 14:39:15 +0000411 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100412 codec_dai->driver->playback.stream_name,
413 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600414 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100415
416 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600417 if (rtd->pop_wait == 1) {
418 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800419 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
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);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100424}
425
426/*
427 * Called by ALSA when a PCM substream is closed. Private data can be
428 * freed here. The cpu DAI, codec DAI, machine and platform are also
429 * shutdown.
430 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100431static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100432{
433 struct snd_soc_pcm_runtime *rtd = substream->private_data;
434 struct snd_soc_platform *platform = rtd->platform;
435 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
436 struct snd_soc_dai *codec_dai = rtd->codec_dai;
437 struct snd_soc_codec *codec = rtd->codec;
438
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100439 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100440
441 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
442 cpu_dai->playback_active--;
443 codec_dai->playback_active--;
444 } else {
445 cpu_dai->capture_active--;
446 codec_dai->capture_active--;
447 }
448
449 cpu_dai->active--;
450 codec_dai->active--;
451 codec->active--;
452
Dong Aisheng17841022011-08-29 17:15:14 +0800453 /* clear the corresponding DAIs rate when inactive */
Nicolin Chen3635bf02013-11-13 18:56:24 +0800454 if (!cpu_dai->active) {
Dong Aisheng17841022011-08-29 17:15:14 +0800455 cpu_dai->rate = 0;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800456 cpu_dai->channels = 0;
457 cpu_dai->sample_bits = 0;
458 }
Dong Aisheng17841022011-08-29 17:15:14 +0800459
Nicolin Chen3635bf02013-11-13 18:56:24 +0800460 if (!codec_dai->active) {
Dong Aisheng17841022011-08-29 17:15:14 +0800461 codec_dai->rate = 0;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800462 codec_dai->channels = 0;
463 codec_dai->sample_bits = 0;
464 }
Sascha Hauer25b76792011-08-17 09:20:01 +0200465
Liam Girdwoodddee6272011-06-09 14:45:53 +0100466 /* Muting the DAC suppresses artifacts caused during digital
467 * shutdown, for example from stopping clocks.
468 */
Mark Brownda183962013-02-06 15:44:07 +0000469 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100470
471 if (cpu_dai->driver->ops->shutdown)
472 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
473
474 if (codec_dai->driver->ops->shutdown)
475 codec_dai->driver->ops->shutdown(substream, codec_dai);
476
477 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
478 rtd->dai_link->ops->shutdown(substream);
479
480 if (platform->driver->ops && platform->driver->ops->close)
481 platform->driver->ops->close(substream);
482 cpu_dai->runtime = NULL;
483
484 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mark Brownb5d1d032012-02-08 20:10:56 +0000485 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
Mark Brown5b6247a2011-10-27 12:11:26 +0200486 rtd->dai_link->ignore_pmdown_time) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300487 /* powered down playback stream now */
488 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800489 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800490 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300491 } else {
492 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600493 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100494 queue_delayed_work(system_power_efficient_wq,
495 &rtd->delayed_work,
496 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300497 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100498 } else {
499 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800500 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000501 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100502 }
503
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100504 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000505
506 pm_runtime_put(platform->dev);
507 pm_runtime_put(codec_dai->dev);
508 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800509 if (!codec_dai->active)
510 pinctrl_pm_select_sleep_state(codec_dai->dev);
511 if (!cpu_dai->active)
512 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000513
Liam Girdwoodddee6272011-06-09 14:45:53 +0100514 return 0;
515}
516
517/*
518 * Called by ALSA when the PCM substream is prepared, can set format, sample
519 * rate, etc. This function is non atomic and can be called multiple times,
520 * it can refer to the runtime info.
521 */
522static int soc_pcm_prepare(struct snd_pcm_substream *substream)
523{
524 struct snd_soc_pcm_runtime *rtd = substream->private_data;
525 struct snd_soc_platform *platform = rtd->platform;
526 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
527 struct snd_soc_dai *codec_dai = rtd->codec_dai;
528 int ret = 0;
529
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100530 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100531
532 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
533 ret = rtd->dai_link->ops->prepare(substream);
534 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000535 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
536 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100537 goto out;
538 }
539 }
540
541 if (platform->driver->ops && platform->driver->ops->prepare) {
542 ret = platform->driver->ops->prepare(substream);
543 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000544 dev_err(platform->dev, "ASoC: platform prepare error:"
545 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100546 goto out;
547 }
548 }
549
Mark Brownc5914b02013-10-30 17:47:39 -0700550 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100551 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
552 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000553 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000554 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100555 goto out;
556 }
557 }
558
Mark Brownc5914b02013-10-30 17:47:39 -0700559 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100560 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
561 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000562 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000563 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100564 goto out;
565 }
566 }
567
568 /* cancel any delayed stream shutdown that is pending */
569 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600570 rtd->pop_wait) {
571 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100572 cancel_delayed_work(&rtd->delayed_work);
573 }
574
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000575 snd_soc_dapm_stream_event(rtd, substream->stream,
576 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100577
Mark Brownda183962013-02-06 15:44:07 +0000578 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100579
580out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100581 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100582 return ret;
583}
584
585/*
586 * Called by ALSA when the hardware params are set by application. This
587 * function can also be called multiple times and can allocate buffers
588 * (using snd_pcm_lib_* ). It's non-atomic.
589 */
590static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
591 struct snd_pcm_hw_params *params)
592{
593 struct snd_soc_pcm_runtime *rtd = substream->private_data;
594 struct snd_soc_platform *platform = rtd->platform;
595 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
596 struct snd_soc_dai *codec_dai = rtd->codec_dai;
597 int ret = 0;
598
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100599 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100600
Nicolin Chen3635bf02013-11-13 18:56:24 +0800601 ret = soc_pcm_params_symmetry(substream, params);
602 if (ret)
603 goto out;
604
Liam Girdwoodddee6272011-06-09 14:45:53 +0100605 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
606 ret = rtd->dai_link->ops->hw_params(substream, params);
607 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000608 dev_err(rtd->card->dev, "ASoC: machine hw_params"
609 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100610 goto out;
611 }
612 }
613
Mark Brownc5914b02013-10-30 17:47:39 -0700614 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100615 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
616 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000617 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
618 " %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100619 goto codec_err;
620 }
621 }
622
Mark Brownc5914b02013-10-30 17:47:39 -0700623 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100624 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
625 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000626 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000627 cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100628 goto interface_err;
629 }
630 }
631
632 if (platform->driver->ops && platform->driver->ops->hw_params) {
633 ret = platform->driver->ops->hw_params(substream, params);
634 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000635 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000636 platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100637 goto platform_err;
638 }
639 }
640
Nicolin Chen3635bf02013-11-13 18:56:24 +0800641 /* store the parameters for each DAIs */
Dong Aisheng17841022011-08-29 17:15:14 +0800642 cpu_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800643 cpu_dai->channels = params_channels(params);
644 cpu_dai->sample_bits =
645 snd_pcm_format_physical_width(params_format(params));
646
Dong Aisheng17841022011-08-29 17:15:14 +0800647 codec_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800648 codec_dai->channels = params_channels(params);
649 codec_dai->sample_bits =
650 snd_pcm_format_physical_width(params_format(params));
Liam Girdwoodddee6272011-06-09 14:45:53 +0100651
652out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100653 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100654 return ret;
655
656platform_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700657 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100658 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
659
660interface_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700661 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100662 codec_dai->driver->ops->hw_free(substream, codec_dai);
663
664codec_err:
665 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
666 rtd->dai_link->ops->hw_free(substream);
667
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100668 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100669 return ret;
670}
671
672/*
673 * Frees resources allocated by hw_params, can be called multiple times
674 */
675static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
676{
677 struct snd_soc_pcm_runtime *rtd = substream->private_data;
678 struct snd_soc_platform *platform = rtd->platform;
679 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
680 struct snd_soc_dai *codec_dai = rtd->codec_dai;
681 struct snd_soc_codec *codec = rtd->codec;
682
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100683 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100684
685 /* apply codec digital mute */
686 if (!codec->active)
Mark Brownda183962013-02-06 15:44:07 +0000687 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100688
689 /* free any machine hw params */
690 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
691 rtd->dai_link->ops->hw_free(substream);
692
693 /* free any DMA resources */
694 if (platform->driver->ops && platform->driver->ops->hw_free)
695 platform->driver->ops->hw_free(substream);
696
697 /* now free hw params for the DAIs */
Mark Brownc5914b02013-10-30 17:47:39 -0700698 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100699 codec_dai->driver->ops->hw_free(substream, codec_dai);
700
Mark Brownc5914b02013-10-30 17:47:39 -0700701 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100702 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
703
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100704 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100705 return 0;
706}
707
708static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
709{
710 struct snd_soc_pcm_runtime *rtd = substream->private_data;
711 struct snd_soc_platform *platform = rtd->platform;
712 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
713 struct snd_soc_dai *codec_dai = rtd->codec_dai;
714 int ret;
715
Mark Brownc5914b02013-10-30 17:47:39 -0700716 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100717 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
718 if (ret < 0)
719 return ret;
720 }
721
722 if (platform->driver->ops && platform->driver->ops->trigger) {
723 ret = platform->driver->ops->trigger(substream, cmd);
724 if (ret < 0)
725 return ret;
726 }
727
Mark Brownc5914b02013-10-30 17:47:39 -0700728 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100729 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
730 if (ret < 0)
731 return ret;
732 }
733 return 0;
734}
735
Mark Brown45c0a182012-05-09 21:46:27 +0100736static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
737 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100738{
739 struct snd_soc_pcm_runtime *rtd = substream->private_data;
740 struct snd_soc_platform *platform = rtd->platform;
741 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
742 struct snd_soc_dai *codec_dai = rtd->codec_dai;
743 int ret;
744
Mark Brownc5914b02013-10-30 17:47:39 -0700745 if (codec_dai->driver->ops &&
746 codec_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100747 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
748 if (ret < 0)
749 return ret;
750 }
751
Mark Brownc5914b02013-10-30 17:47:39 -0700752 if (platform->driver->ops && platform->driver->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100753 ret = platform->driver->bespoke_trigger(substream, cmd);
754 if (ret < 0)
755 return ret;
756 }
757
Mark Brownc5914b02013-10-30 17:47:39 -0700758 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100759 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
760 if (ret < 0)
761 return ret;
762 }
763 return 0;
764}
Liam Girdwoodddee6272011-06-09 14:45:53 +0100765/*
766 * soc level wrapper for pointer callback
767 * If cpu_dai, codec_dai, platform driver has the delay callback, than
768 * the runtime->delay will be updated accordingly.
769 */
770static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
771{
772 struct snd_soc_pcm_runtime *rtd = substream->private_data;
773 struct snd_soc_platform *platform = rtd->platform;
774 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
775 struct snd_soc_dai *codec_dai = rtd->codec_dai;
776 struct snd_pcm_runtime *runtime = substream->runtime;
777 snd_pcm_uframes_t offset = 0;
778 snd_pcm_sframes_t delay = 0;
779
780 if (platform->driver->ops && platform->driver->ops->pointer)
781 offset = platform->driver->ops->pointer(substream);
782
Mark Brownc5914b02013-10-30 17:47:39 -0700783 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100784 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
785
Mark Brownc5914b02013-10-30 17:47:39 -0700786 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100787 delay += codec_dai->driver->ops->delay(substream, codec_dai);
788
789 if (platform->driver->delay)
790 delay += platform->driver->delay(substream, codec_dai);
791
792 runtime->delay = delay;
793
794 return offset;
795}
796
Liam Girdwood01d75842012-04-25 12:12:49 +0100797/* connect a FE and BE */
798static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
799 struct snd_soc_pcm_runtime *be, int stream)
800{
801 struct snd_soc_dpcm *dpcm;
802
803 /* only add new dpcms */
804 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
805 if (dpcm->be == be && dpcm->fe == fe)
806 return 0;
807 }
808
809 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
810 if (!dpcm)
811 return -ENOMEM;
812
813 dpcm->be = be;
814 dpcm->fe = fe;
815 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
816 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
817 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
818 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
819
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300820 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100821 stream ? "capture" : "playback", fe->dai_link->name,
822 stream ? "<-" : "->", be->dai_link->name);
823
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100824#ifdef CONFIG_DEBUG_FS
825 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
826 fe->debugfs_dpcm_root, &dpcm->state);
827#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100828 return 1;
829}
830
831/* reparent a BE onto another FE */
832static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
833 struct snd_soc_pcm_runtime *be, int stream)
834{
835 struct snd_soc_dpcm *dpcm;
836 struct snd_pcm_substream *fe_substream, *be_substream;
837
838 /* reparent if BE is connected to other FEs */
839 if (!be->dpcm[stream].users)
840 return;
841
842 be_substream = snd_soc_dpcm_get_substream(be, stream);
843
844 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
845 if (dpcm->fe == fe)
846 continue;
847
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300848 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100849 stream ? "capture" : "playback",
850 dpcm->fe->dai_link->name,
851 stream ? "<-" : "->", dpcm->be->dai_link->name);
852
853 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
854 be_substream->runtime = fe_substream->runtime;
855 break;
856 }
857}
858
859/* disconnect a BE and FE */
860static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
861{
862 struct snd_soc_dpcm *dpcm, *d;
863
864 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000865 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100866 stream ? "capture" : "playback",
867 dpcm->be->dai_link->name);
868
869 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
870 continue;
871
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300872 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100873 stream ? "capture" : "playback", fe->dai_link->name,
874 stream ? "<-" : "->", dpcm->be->dai_link->name);
875
876 /* BEs still alive need new FE */
877 dpcm_be_reparent(fe, dpcm->be, stream);
878
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100879#ifdef CONFIG_DEBUG_FS
880 debugfs_remove(dpcm->debugfs_state);
881#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100882 list_del(&dpcm->list_be);
883 list_del(&dpcm->list_fe);
884 kfree(dpcm);
885 }
886}
887
888/* get BE for DAI widget and stream */
889static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
890 struct snd_soc_dapm_widget *widget, int stream)
891{
892 struct snd_soc_pcm_runtime *be;
893 int i;
894
895 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
896 for (i = 0; i < card->num_links; i++) {
897 be = &card->rtd[i];
898
Liam Girdwood35ea0652012-06-05 19:26:59 +0100899 if (!be->dai_link->no_pcm)
900 continue;
901
Liam Girdwood01d75842012-04-25 12:12:49 +0100902 if (be->cpu_dai->playback_widget == widget ||
903 be->codec_dai->playback_widget == widget)
904 return be;
905 }
906 } else {
907
908 for (i = 0; i < card->num_links; i++) {
909 be = &card->rtd[i];
910
Liam Girdwood35ea0652012-06-05 19:26:59 +0100911 if (!be->dai_link->no_pcm)
912 continue;
913
Liam Girdwood01d75842012-04-25 12:12:49 +0100914 if (be->cpu_dai->capture_widget == widget ||
915 be->codec_dai->capture_widget == widget)
916 return be;
917 }
918 }
919
Liam Girdwood103d84a2012-11-19 14:39:15 +0000920 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100921 stream ? "capture" : "playback", widget->name);
922 return NULL;
923}
924
925static inline struct snd_soc_dapm_widget *
926 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
927{
928 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
929 return rtd->cpu_dai->playback_widget;
930 else
931 return rtd->cpu_dai->capture_widget;
932}
933
934static inline struct snd_soc_dapm_widget *
935 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
936{
937 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
938 return rtd->codec_dai->playback_widget;
939 else
940 return rtd->codec_dai->capture_widget;
941}
942
943static int widget_in_list(struct snd_soc_dapm_widget_list *list,
944 struct snd_soc_dapm_widget *widget)
945{
946 int i;
947
948 for (i = 0; i < list->num_widgets; i++) {
949 if (widget == list->widgets[i])
950 return 1;
951 }
952
953 return 0;
954}
955
956static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
957 int stream, struct snd_soc_dapm_widget_list **list_)
958{
959 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
960 struct snd_soc_dapm_widget_list *list;
961 int paths;
962
963 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
964 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
965 if (list == NULL)
966 return -ENOMEM;
967
968 /* get number of valid DAI paths and their widgets */
969 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
970
Liam Girdwood103d84a2012-11-19 14:39:15 +0000971 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +0100972 stream ? "capture" : "playback");
973
974 *list_ = list;
975 return paths;
976}
977
978static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
979{
980 kfree(*list);
981}
982
983static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
984 struct snd_soc_dapm_widget_list **list_)
985{
986 struct snd_soc_dpcm *dpcm;
987 struct snd_soc_dapm_widget_list *list = *list_;
988 struct snd_soc_dapm_widget *widget;
989 int prune = 0;
990
991 /* Destroy any old FE <--> BE connections */
992 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
993
994 /* is there a valid CPU DAI widget for this BE */
995 widget = rtd_get_cpu_widget(dpcm->be, stream);
996
997 /* prune the BE if it's no longer in our active list */
998 if (widget && widget_in_list(list, widget))
999 continue;
1000
1001 /* is there a valid CODEC DAI widget for this BE */
1002 widget = rtd_get_codec_widget(dpcm->be, stream);
1003
1004 /* prune the BE if it's no longer in our active list */
1005 if (widget && widget_in_list(list, widget))
1006 continue;
1007
Liam Girdwood103d84a2012-11-19 14:39:15 +00001008 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001009 stream ? "capture" : "playback",
1010 dpcm->be->dai_link->name, fe->dai_link->name);
1011 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1012 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1013 prune++;
1014 }
1015
Liam Girdwood103d84a2012-11-19 14:39:15 +00001016 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001017 return prune;
1018}
1019
1020static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1021 struct snd_soc_dapm_widget_list **list_)
1022{
1023 struct snd_soc_card *card = fe->card;
1024 struct snd_soc_dapm_widget_list *list = *list_;
1025 struct snd_soc_pcm_runtime *be;
1026 int i, new = 0, err;
1027
1028 /* Create any new FE <--> BE connections */
1029 for (i = 0; i < list->num_widgets; i++) {
1030
Mark Brown46162742013-06-05 19:36:11 +01001031 switch (list->widgets[i]->id) {
1032 case snd_soc_dapm_dai_in:
1033 case snd_soc_dapm_dai_out:
1034 break;
1035 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001036 continue;
Mark Brown46162742013-06-05 19:36:11 +01001037 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001038
1039 /* is there a valid BE rtd for this widget */
1040 be = dpcm_get_be(card, list->widgets[i], stream);
1041 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001042 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001043 list->widgets[i]->name);
1044 continue;
1045 }
1046
1047 /* make sure BE is a real BE */
1048 if (!be->dai_link->no_pcm)
1049 continue;
1050
1051 /* don't connect if FE is not running */
1052 if (!fe->dpcm[stream].runtime)
1053 continue;
1054
1055 /* newly connected FE and BE */
1056 err = dpcm_be_connect(fe, be, stream);
1057 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001058 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001059 list->widgets[i]->name);
1060 break;
1061 } else if (err == 0) /* already connected */
1062 continue;
1063
1064 /* new */
1065 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1066 new++;
1067 }
1068
Liam Girdwood103d84a2012-11-19 14:39:15 +00001069 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001070 return new;
1071}
1072
1073/*
1074 * Find the corresponding BE DAIs that source or sink audio to this
1075 * FE substream.
1076 */
1077static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1078 int stream, struct snd_soc_dapm_widget_list **list, int new)
1079{
1080 if (new)
1081 return dpcm_add_paths(fe, stream, list);
1082 else
1083 return dpcm_prune_paths(fe, stream, list);
1084}
1085
1086static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1087{
1088 struct snd_soc_dpcm *dpcm;
1089
1090 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1091 dpcm->be->dpcm[stream].runtime_update =
1092 SND_SOC_DPCM_UPDATE_NO;
1093}
1094
1095static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1096 int stream)
1097{
1098 struct snd_soc_dpcm *dpcm;
1099
1100 /* disable any enabled and non active backends */
1101 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1102
1103 struct snd_soc_pcm_runtime *be = dpcm->be;
1104 struct snd_pcm_substream *be_substream =
1105 snd_soc_dpcm_get_substream(be, stream);
1106
1107 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001108 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001109 stream ? "capture" : "playback",
1110 be->dpcm[stream].state);
1111
1112 if (--be->dpcm[stream].users != 0)
1113 continue;
1114
1115 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1116 continue;
1117
1118 soc_pcm_close(be_substream);
1119 be_substream->runtime = NULL;
1120 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1121 }
1122}
1123
1124static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1125{
1126 struct snd_soc_dpcm *dpcm;
1127 int err, count = 0;
1128
1129 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1130 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1131
1132 struct snd_soc_pcm_runtime *be = dpcm->be;
1133 struct snd_pcm_substream *be_substream =
1134 snd_soc_dpcm_get_substream(be, stream);
1135
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001136 if (!be_substream) {
1137 dev_err(be->dev, "ASoC: no backend %s stream\n",
1138 stream ? "capture" : "playback");
1139 continue;
1140 }
1141
Liam Girdwood01d75842012-04-25 12:12:49 +01001142 /* is this op for this BE ? */
1143 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1144 continue;
1145
1146 /* first time the dpcm is open ? */
1147 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001148 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001149 stream ? "capture" : "playback",
1150 be->dpcm[stream].state);
1151
1152 if (be->dpcm[stream].users++ != 0)
1153 continue;
1154
1155 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1156 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1157 continue;
1158
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001159 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1160 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001161
1162 be_substream->runtime = be->dpcm[stream].runtime;
1163 err = soc_pcm_open(be_substream);
1164 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001165 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001166 be->dpcm[stream].users--;
1167 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001168 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001169 stream ? "capture" : "playback",
1170 be->dpcm[stream].state);
1171
1172 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1173 goto unwind;
1174 }
1175
1176 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1177 count++;
1178 }
1179
1180 return count;
1181
1182unwind:
1183 /* disable any enabled and non active backends */
1184 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1185 struct snd_soc_pcm_runtime *be = dpcm->be;
1186 struct snd_pcm_substream *be_substream =
1187 snd_soc_dpcm_get_substream(be, stream);
1188
1189 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1190 continue;
1191
1192 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001193 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001194 stream ? "capture" : "playback",
1195 be->dpcm[stream].state);
1196
1197 if (--be->dpcm[stream].users != 0)
1198 continue;
1199
1200 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1201 continue;
1202
1203 soc_pcm_close(be_substream);
1204 be_substream->runtime = NULL;
1205 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1206 }
1207
1208 return err;
1209}
1210
Mark Brown45c0a182012-05-09 21:46:27 +01001211static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001212{
1213 struct snd_pcm_runtime *runtime = substream->runtime;
1214 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1215 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1216 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1217
1218 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1219 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1220 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1221 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1222 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1223 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1224 runtime->hw.rates = cpu_dai_drv->playback.rates;
1225 } else {
1226 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1227 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1228 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1229 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1230 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1231 runtime->hw.rates = cpu_dai_drv->capture.rates;
1232 }
1233}
1234
1235static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1236{
1237 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1238 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1239 int stream = fe_substream->stream, ret = 0;
1240
1241 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1242
1243 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1244 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001245 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001246 goto be_err;
1247 }
1248
Liam Girdwood103d84a2012-11-19 14:39:15 +00001249 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001250
1251 /* start the DAI frontend */
1252 ret = soc_pcm_open(fe_substream);
1253 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001254 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001255 goto unwind;
1256 }
1257
1258 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1259
1260 dpcm_set_fe_runtime(fe_substream);
1261 snd_pcm_limit_hw_rates(runtime);
1262
1263 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1264 return 0;
1265
1266unwind:
1267 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1268be_err:
1269 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1270 return ret;
1271}
1272
1273static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1274{
1275 struct snd_soc_dpcm *dpcm;
1276
1277 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1278 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1279
1280 struct snd_soc_pcm_runtime *be = dpcm->be;
1281 struct snd_pcm_substream *be_substream =
1282 snd_soc_dpcm_get_substream(be, stream);
1283
1284 /* is this op for this BE ? */
1285 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1286 continue;
1287
1288 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001289 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001290 stream ? "capture" : "playback",
1291 be->dpcm[stream].state);
1292
1293 if (--be->dpcm[stream].users != 0)
1294 continue;
1295
1296 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1297 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1298 continue;
1299
Liam Girdwood103d84a2012-11-19 14:39:15 +00001300 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001301 dpcm->fe->dai_link->name);
1302
1303 soc_pcm_close(be_substream);
1304 be_substream->runtime = NULL;
1305
1306 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1307 }
1308 return 0;
1309}
1310
1311static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1312{
1313 struct snd_soc_pcm_runtime *fe = substream->private_data;
1314 int stream = substream->stream;
1315
1316 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1317
1318 /* shutdown the BEs */
1319 dpcm_be_dai_shutdown(fe, substream->stream);
1320
Liam Girdwood103d84a2012-11-19 14:39:15 +00001321 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001322
1323 /* now shutdown the frontend */
1324 soc_pcm_close(substream);
1325
1326 /* run the stream event for each BE */
1327 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1328
1329 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1330 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1331 return 0;
1332}
1333
1334static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1335{
1336 struct snd_soc_dpcm *dpcm;
1337
1338 /* only hw_params backends that are either sinks or sources
1339 * to this frontend DAI */
1340 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1341
1342 struct snd_soc_pcm_runtime *be = dpcm->be;
1343 struct snd_pcm_substream *be_substream =
1344 snd_soc_dpcm_get_substream(be, stream);
1345
1346 /* is this op for this BE ? */
1347 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1348 continue;
1349
1350 /* only free hw when no longer used - check all FEs */
1351 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1352 continue;
1353
1354 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1355 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1356 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001357 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001358 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1359 continue;
1360
Liam Girdwood103d84a2012-11-19 14:39:15 +00001361 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001362 dpcm->fe->dai_link->name);
1363
1364 soc_pcm_hw_free(be_substream);
1365
1366 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1367 }
1368
1369 return 0;
1370}
1371
Mark Brown45c0a182012-05-09 21:46:27 +01001372static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001373{
1374 struct snd_soc_pcm_runtime *fe = substream->private_data;
1375 int err, stream = substream->stream;
1376
1377 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1378 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1379
Liam Girdwood103d84a2012-11-19 14:39:15 +00001380 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001381
1382 /* call hw_free on the frontend */
1383 err = soc_pcm_hw_free(substream);
1384 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001385 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001386 fe->dai_link->name);
1387
1388 /* only hw_params backends that are either sinks or sources
1389 * to this frontend DAI */
1390 err = dpcm_be_dai_hw_free(fe, stream);
1391
1392 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1393 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1394
1395 mutex_unlock(&fe->card->mutex);
1396 return 0;
1397}
1398
1399static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1400{
1401 struct snd_soc_dpcm *dpcm;
1402 int ret;
1403
1404 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1405
1406 struct snd_soc_pcm_runtime *be = dpcm->be;
1407 struct snd_pcm_substream *be_substream =
1408 snd_soc_dpcm_get_substream(be, stream);
1409
1410 /* is this op for this BE ? */
1411 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1412 continue;
1413
1414 /* only allow hw_params() if no connected FEs are running */
1415 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1416 continue;
1417
1418 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1419 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1420 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1421 continue;
1422
Liam Girdwood103d84a2012-11-19 14:39:15 +00001423 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001424 dpcm->fe->dai_link->name);
1425
1426 /* copy params for each dpcm */
1427 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1428 sizeof(struct snd_pcm_hw_params));
1429
1430 /* perform any hw_params fixups */
1431 if (be->dai_link->be_hw_params_fixup) {
1432 ret = be->dai_link->be_hw_params_fixup(be,
1433 &dpcm->hw_params);
1434 if (ret < 0) {
1435 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001436 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001437 ret);
1438 goto unwind;
1439 }
1440 }
1441
1442 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1443 if (ret < 0) {
1444 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001445 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001446 goto unwind;
1447 }
1448
1449 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1450 }
1451 return 0;
1452
1453unwind:
1454 /* disable any enabled and non active backends */
1455 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1456 struct snd_soc_pcm_runtime *be = dpcm->be;
1457 struct snd_pcm_substream *be_substream =
1458 snd_soc_dpcm_get_substream(be, stream);
1459
1460 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1461 continue;
1462
1463 /* only allow hw_free() if no connected FEs are running */
1464 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1465 continue;
1466
1467 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1468 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1469 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1470 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1471 continue;
1472
1473 soc_pcm_hw_free(be_substream);
1474 }
1475
1476 return ret;
1477}
1478
Mark Brown45c0a182012-05-09 21:46:27 +01001479static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1480 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001481{
1482 struct snd_soc_pcm_runtime *fe = substream->private_data;
1483 int ret, stream = substream->stream;
1484
1485 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1486 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1487
1488 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1489 sizeof(struct snd_pcm_hw_params));
1490 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1491 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001492 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001493 goto out;
1494 }
1495
Liam Girdwood103d84a2012-11-19 14:39:15 +00001496 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001497 fe->dai_link->name, params_rate(params),
1498 params_channels(params), params_format(params));
1499
1500 /* call hw_params on the frontend */
1501 ret = soc_pcm_hw_params(substream, params);
1502 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001503 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001504 dpcm_be_dai_hw_free(fe, stream);
1505 } else
1506 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1507
1508out:
1509 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1510 mutex_unlock(&fe->card->mutex);
1511 return ret;
1512}
1513
1514static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1515 struct snd_pcm_substream *substream, int cmd)
1516{
1517 int ret;
1518
Liam Girdwood103d84a2012-11-19 14:39:15 +00001519 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001520 dpcm->fe->dai_link->name, cmd);
1521
1522 ret = soc_pcm_trigger(substream, cmd);
1523 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001524 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001525
1526 return ret;
1527}
1528
Mark Brown45c0a182012-05-09 21:46:27 +01001529static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1530 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001531{
1532 struct snd_soc_dpcm *dpcm;
1533 int ret = 0;
1534
1535 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1536
1537 struct snd_soc_pcm_runtime *be = dpcm->be;
1538 struct snd_pcm_substream *be_substream =
1539 snd_soc_dpcm_get_substream(be, stream);
1540
1541 /* is this op for this BE ? */
1542 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1543 continue;
1544
1545 switch (cmd) {
1546 case SNDRV_PCM_TRIGGER_START:
1547 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1548 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1549 continue;
1550
1551 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1552 if (ret)
1553 return ret;
1554
1555 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1556 break;
1557 case SNDRV_PCM_TRIGGER_RESUME:
1558 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1559 continue;
1560
1561 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1562 if (ret)
1563 return ret;
1564
1565 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1566 break;
1567 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1568 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1569 continue;
1570
1571 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1572 if (ret)
1573 return ret;
1574
1575 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1576 break;
1577 case SNDRV_PCM_TRIGGER_STOP:
1578 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1579 continue;
1580
1581 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1582 continue;
1583
1584 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1585 if (ret)
1586 return ret;
1587
1588 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1589 break;
1590 case SNDRV_PCM_TRIGGER_SUSPEND:
1591 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1592 continue;
1593
1594 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1595 continue;
1596
1597 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1598 if (ret)
1599 return ret;
1600
1601 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1602 break;
1603 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1604 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1605 continue;
1606
1607 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1608 continue;
1609
1610 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1611 if (ret)
1612 return ret;
1613
1614 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1615 break;
1616 }
1617 }
1618
1619 return ret;
1620}
1621EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1622
Mark Brown45c0a182012-05-09 21:46:27 +01001623static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001624{
1625 struct snd_soc_pcm_runtime *fe = substream->private_data;
1626 int stream = substream->stream, ret;
1627 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1628
1629 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1630
1631 switch (trigger) {
1632 case SND_SOC_DPCM_TRIGGER_PRE:
1633 /* call trigger on the frontend before the backend. */
1634
Liam Girdwood103d84a2012-11-19 14:39:15 +00001635 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001636 fe->dai_link->name, cmd);
1637
1638 ret = soc_pcm_trigger(substream, cmd);
1639 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001640 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001641 goto out;
1642 }
1643
1644 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1645 break;
1646 case SND_SOC_DPCM_TRIGGER_POST:
1647 /* call trigger on the frontend after the backend. */
1648
1649 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1650 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001651 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001652 goto out;
1653 }
1654
Liam Girdwood103d84a2012-11-19 14:39:15 +00001655 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001656 fe->dai_link->name, cmd);
1657
1658 ret = soc_pcm_trigger(substream, cmd);
1659 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001660 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1661 /* bespoke trigger() - handles both FE and BEs */
1662
Liam Girdwood103d84a2012-11-19 14:39:15 +00001663 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001664 fe->dai_link->name, cmd);
1665
1666 ret = soc_pcm_bespoke_trigger(substream, cmd);
1667 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001668 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001669 goto out;
1670 }
1671 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01001672 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00001673 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01001674 fe->dai_link->name);
1675 ret = -EINVAL;
1676 goto out;
1677 }
1678
1679 switch (cmd) {
1680 case SNDRV_PCM_TRIGGER_START:
1681 case SNDRV_PCM_TRIGGER_RESUME:
1682 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1683 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1684 break;
1685 case SNDRV_PCM_TRIGGER_STOP:
1686 case SNDRV_PCM_TRIGGER_SUSPEND:
1687 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1688 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1689 break;
1690 }
1691
1692out:
1693 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1694 return ret;
1695}
1696
1697static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1698{
1699 struct snd_soc_dpcm *dpcm;
1700 int ret = 0;
1701
1702 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1703
1704 struct snd_soc_pcm_runtime *be = dpcm->be;
1705 struct snd_pcm_substream *be_substream =
1706 snd_soc_dpcm_get_substream(be, stream);
1707
1708 /* is this op for this BE ? */
1709 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1710 continue;
1711
1712 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1713 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1714 continue;
1715
Liam Girdwood103d84a2012-11-19 14:39:15 +00001716 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001717 dpcm->fe->dai_link->name);
1718
1719 ret = soc_pcm_prepare(be_substream);
1720 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001721 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001722 ret);
1723 break;
1724 }
1725
1726 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1727 }
1728 return ret;
1729}
1730
Mark Brown45c0a182012-05-09 21:46:27 +01001731static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001732{
1733 struct snd_soc_pcm_runtime *fe = substream->private_data;
1734 int stream = substream->stream, ret = 0;
1735
1736 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1737
Liam Girdwood103d84a2012-11-19 14:39:15 +00001738 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001739
1740 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1741
1742 /* there is no point preparing this FE if there are no BEs */
1743 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001744 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001745 fe->dai_link->name);
1746 ret = -EINVAL;
1747 goto out;
1748 }
1749
1750 ret = dpcm_be_dai_prepare(fe, substream->stream);
1751 if (ret < 0)
1752 goto out;
1753
1754 /* call prepare on the frontend */
1755 ret = soc_pcm_prepare(substream);
1756 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001757 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001758 fe->dai_link->name);
1759 goto out;
1760 }
1761
1762 /* run the stream event for each BE */
1763 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1764 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1765
1766out:
1767 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1768 mutex_unlock(&fe->card->mutex);
1769
1770 return ret;
1771}
1772
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001773static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1774 unsigned int cmd, void *arg)
1775{
1776 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1777 struct snd_soc_platform *platform = rtd->platform;
1778
Mark Brownc5914b02013-10-30 17:47:39 -07001779 if (platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001780 return platform->driver->ops->ioctl(substream, cmd, arg);
1781 return snd_pcm_lib_ioctl(substream, cmd, arg);
1782}
1783
Liam Girdwood618dae12012-04-25 12:12:51 +01001784static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1785{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001786 struct snd_pcm_substream *substream =
1787 snd_soc_dpcm_get_substream(fe, stream);
1788 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001789 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01001790
Liam Girdwood103d84a2012-11-19 14:39:15 +00001791 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001792 stream ? "capture" : "playback", fe->dai_link->name);
1793
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001794 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1795 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001796 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001797 fe->dai_link->name);
1798
1799 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1800 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001801 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001802 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001803 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001804 fe->dai_link->name);
1805
1806 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1807 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001808 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001809 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001810
1811 err = dpcm_be_dai_hw_free(fe, stream);
1812 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001813 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001814
1815 err = dpcm_be_dai_shutdown(fe, stream);
1816 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001817 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001818
1819 /* run the stream event for each BE */
1820 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1821
1822 return 0;
1823}
1824
1825static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1826{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001827 struct snd_pcm_substream *substream =
1828 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01001829 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001830 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001831 int ret;
1832
Liam Girdwood103d84a2012-11-19 14:39:15 +00001833 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001834 stream ? "capture" : "playback", fe->dai_link->name);
1835
1836 /* Only start the BE if the FE is ready */
1837 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1838 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1839 return -EINVAL;
1840
1841 /* startup must always be called for new BEs */
1842 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001843 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001844 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01001845
1846 /* keep going if FE state is > open */
1847 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1848 return 0;
1849
1850 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001851 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001852 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01001853
1854 /* keep going if FE state is > hw_params */
1855 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1856 return 0;
1857
1858
1859 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001860 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001861 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01001862
1863 /* run the stream event for each BE */
1864 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1865
1866 /* keep going if FE state is > prepare */
1867 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1868 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1869 return 0;
1870
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001871 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1872 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001873 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001874 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01001875
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001876 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1877 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001878 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001879 goto hw_free;
1880 }
1881 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001882 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001883 fe->dai_link->name);
1884
1885 ret = dpcm_be_dai_trigger(fe, stream,
1886 SNDRV_PCM_TRIGGER_START);
1887 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001888 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001889 goto hw_free;
1890 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001891 }
1892
1893 return 0;
1894
1895hw_free:
1896 dpcm_be_dai_hw_free(fe, stream);
1897close:
1898 dpcm_be_dai_shutdown(fe, stream);
1899disconnect:
1900 /* disconnect any non started BEs */
1901 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1902 struct snd_soc_pcm_runtime *be = dpcm->be;
1903 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1904 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1905 }
1906
1907 return ret;
1908}
1909
1910static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1911{
1912 int ret;
1913
1914 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1915 ret = dpcm_run_update_startup(fe, stream);
1916 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001917 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001918 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1919
1920 return ret;
1921}
1922
1923static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1924{
1925 int ret;
1926
1927 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1928 ret = dpcm_run_update_shutdown(fe, stream);
1929 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001930 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001931 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1932
1933 return ret;
1934}
1935
1936/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1937 * any DAI links.
1938 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02001939int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01001940{
Liam Girdwood618dae12012-04-25 12:12:51 +01001941 int i, old, new, paths;
1942
Liam Girdwood618dae12012-04-25 12:12:51 +01001943 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1944 for (i = 0; i < card->num_rtd; i++) {
1945 struct snd_soc_dapm_widget_list *list;
1946 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1947
1948 /* make sure link is FE */
1949 if (!fe->dai_link->dynamic)
1950 continue;
1951
1952 /* only check active links */
1953 if (!fe->cpu_dai->active)
1954 continue;
1955
1956 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001957 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001958 fe->dai_link->name);
1959
1960 /* skip if FE doesn't have playback capability */
1961 if (!fe->cpu_dai->driver->playback.channels_min)
1962 goto capture;
1963
1964 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1965 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001966 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001967 fe->dai_link->name, "playback");
1968 mutex_unlock(&card->mutex);
1969 return paths;
1970 }
1971
1972 /* update any new playback paths */
1973 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1974 if (new) {
1975 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1976 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1977 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1978 }
1979
1980 /* update any old playback paths */
1981 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1982 if (old) {
1983 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1984 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1985 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1986 }
1987
1988capture:
1989 /* skip if FE doesn't have capture capability */
1990 if (!fe->cpu_dai->driver->capture.channels_min)
1991 continue;
1992
1993 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1994 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001995 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001996 fe->dai_link->name, "capture");
1997 mutex_unlock(&card->mutex);
1998 return paths;
1999 }
2000
2001 /* update any new capture paths */
2002 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2003 if (new) {
2004 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2005 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2006 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2007 }
2008
2009 /* update any old capture paths */
2010 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2011 if (old) {
2012 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2013 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2014 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2015 }
2016
2017 dpcm_path_put(&list);
2018 }
2019
2020 mutex_unlock(&card->mutex);
2021 return 0;
2022}
Liam Girdwood01d75842012-04-25 12:12:49 +01002023int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2024{
2025 struct snd_soc_dpcm *dpcm;
2026 struct list_head *clients =
2027 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2028
2029 list_for_each_entry(dpcm, clients, list_be) {
2030
2031 struct snd_soc_pcm_runtime *be = dpcm->be;
2032 struct snd_soc_dai *dai = be->codec_dai;
2033 struct snd_soc_dai_driver *drv = dai->driver;
2034
2035 if (be->dai_link->ignore_suspend)
2036 continue;
2037
Liam Girdwood103d84a2012-11-19 14:39:15 +00002038 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002039
Mark Brownc5914b02013-10-30 17:47:39 -07002040 if (drv->ops && drv->ops->digital_mute && dai->playback_active)
2041 drv->ops->digital_mute(dai, mute);
Liam Girdwood01d75842012-04-25 12:12:49 +01002042 }
2043
2044 return 0;
2045}
2046
Mark Brown45c0a182012-05-09 21:46:27 +01002047static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002048{
2049 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2050 struct snd_soc_dpcm *dpcm;
2051 struct snd_soc_dapm_widget_list *list;
2052 int ret;
2053 int stream = fe_substream->stream;
2054
2055 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2056 fe->dpcm[stream].runtime = fe_substream->runtime;
2057
2058 if (dpcm_path_get(fe, stream, &list) <= 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002059 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002060 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002061 }
2062
2063 /* calculate valid and active FE <-> BE dpcms */
2064 dpcm_process_paths(fe, stream, &list, 1);
2065
2066 ret = dpcm_fe_dai_startup(fe_substream);
2067 if (ret < 0) {
2068 /* clean up all links */
2069 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2070 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2071
2072 dpcm_be_disconnect(fe, stream);
2073 fe->dpcm[stream].runtime = NULL;
2074 }
2075
2076 dpcm_clear_pending_state(fe, stream);
2077 dpcm_path_put(&list);
2078 mutex_unlock(&fe->card->mutex);
2079 return ret;
2080}
2081
Mark Brown45c0a182012-05-09 21:46:27 +01002082static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002083{
2084 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2085 struct snd_soc_dpcm *dpcm;
2086 int stream = fe_substream->stream, ret;
2087
2088 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2089 ret = dpcm_fe_dai_shutdown(fe_substream);
2090
2091 /* mark FE's links ready to prune */
2092 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2093 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2094
2095 dpcm_be_disconnect(fe, stream);
2096
2097 fe->dpcm[stream].runtime = NULL;
2098 mutex_unlock(&fe->card->mutex);
2099 return ret;
2100}
2101
Liam Girdwoodddee6272011-06-09 14:45:53 +01002102/* create a new pcm */
2103int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2104{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002105 struct snd_soc_platform *platform = rtd->platform;
2106 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2107 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2108 struct snd_pcm *pcm;
2109 char new_name[64];
2110 int ret = 0, playback = 0, capture = 0;
2111
Liam Girdwood01d75842012-04-25 12:12:49 +01002112 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2113 if (cpu_dai->driver->playback.channels_min)
2114 playback = 1;
2115 if (cpu_dai->driver->capture.channels_min)
2116 capture = 1;
2117 } else {
Mark Brown05679092013-06-01 23:13:53 +01002118 if (codec_dai->driver->playback.channels_min &&
2119 cpu_dai->driver->playback.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002120 playback = 1;
Mark Brown05679092013-06-01 23:13:53 +01002121 if (codec_dai->driver->capture.channels_min &&
2122 cpu_dai->driver->capture.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002123 capture = 1;
2124 }
Sangsu Parka5002312012-01-02 17:15:10 +09002125
Fabio Estevamd6bead02013-08-29 10:32:13 -03002126 if (rtd->dai_link->playback_only) {
2127 playback = 1;
2128 capture = 0;
2129 }
2130
2131 if (rtd->dai_link->capture_only) {
2132 playback = 0;
2133 capture = 1;
2134 }
2135
Liam Girdwood01d75842012-04-25 12:12:49 +01002136 /* create the PCM */
2137 if (rtd->dai_link->no_pcm) {
2138 snprintf(new_name, sizeof(new_name), "(%s)",
2139 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002140
Liam Girdwood01d75842012-04-25 12:12:49 +01002141 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2142 playback, capture, &pcm);
2143 } else {
2144 if (rtd->dai_link->dynamic)
2145 snprintf(new_name, sizeof(new_name), "%s (*)",
2146 rtd->dai_link->stream_name);
2147 else
2148 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2149 rtd->dai_link->stream_name, codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002150
Liam Girdwood01d75842012-04-25 12:12:49 +01002151 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2152 capture, &pcm);
2153 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002154 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002155 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002156 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002157 return ret;
2158 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002159 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002160
2161 /* DAPM dai link stream work */
2162 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2163
2164 rtd->pcm = pcm;
2165 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002166
2167 if (rtd->dai_link->no_pcm) {
2168 if (playback)
2169 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2170 if (capture)
2171 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2172 goto out;
2173 }
2174
2175 /* ASoC PCM operations */
2176 if (rtd->dai_link->dynamic) {
2177 rtd->ops.open = dpcm_fe_dai_open;
2178 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2179 rtd->ops.prepare = dpcm_fe_dai_prepare;
2180 rtd->ops.trigger = dpcm_fe_dai_trigger;
2181 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2182 rtd->ops.close = dpcm_fe_dai_close;
2183 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002184 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002185 } else {
2186 rtd->ops.open = soc_pcm_open;
2187 rtd->ops.hw_params = soc_pcm_hw_params;
2188 rtd->ops.prepare = soc_pcm_prepare;
2189 rtd->ops.trigger = soc_pcm_trigger;
2190 rtd->ops.hw_free = soc_pcm_hw_free;
2191 rtd->ops.close = soc_pcm_close;
2192 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002193 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002194 }
2195
Liam Girdwoodddee6272011-06-09 14:45:53 +01002196 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002197 rtd->ops.ack = platform->driver->ops->ack;
2198 rtd->ops.copy = platform->driver->ops->copy;
2199 rtd->ops.silence = platform->driver->ops->silence;
2200 rtd->ops.page = platform->driver->ops->page;
2201 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002202 }
2203
2204 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002205 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002206
2207 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002208 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002209
2210 if (platform->driver->pcm_new) {
2211 ret = platform->driver->pcm_new(rtd);
2212 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002213 dev_err(platform->dev,
2214 "ASoC: pcm constructor failed: %d\n",
2215 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002216 return ret;
2217 }
2218 }
2219
2220 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002221out:
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03002222 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name,
Liam Girdwoodddee6272011-06-09 14:45:53 +01002223 cpu_dai->name);
2224 return ret;
2225}
Liam Girdwood01d75842012-04-25 12:12:49 +01002226
2227/* is the current PCM operation for this FE ? */
2228int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2229{
2230 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2231 return 1;
2232 return 0;
2233}
2234EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2235
2236/* is the current PCM operation for this BE ? */
2237int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2238 struct snd_soc_pcm_runtime *be, int stream)
2239{
2240 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2241 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2242 be->dpcm[stream].runtime_update))
2243 return 1;
2244 return 0;
2245}
2246EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2247
2248/* get the substream for this BE */
2249struct snd_pcm_substream *
2250 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2251{
2252 return be->pcm->streams[stream].substream;
2253}
2254EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2255
2256/* get the BE runtime state */
2257enum snd_soc_dpcm_state
2258 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2259{
2260 return be->dpcm[stream].state;
2261}
2262EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2263
2264/* set the BE runtime state */
2265void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2266 int stream, enum snd_soc_dpcm_state state)
2267{
2268 be->dpcm[stream].state = state;
2269}
2270EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2271
2272/*
2273 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2274 * are not running, paused or suspended for the specified stream direction.
2275 */
2276int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2277 struct snd_soc_pcm_runtime *be, int stream)
2278{
2279 struct snd_soc_dpcm *dpcm;
2280 int state;
2281
2282 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2283
2284 if (dpcm->fe == fe)
2285 continue;
2286
2287 state = dpcm->fe->dpcm[stream].state;
2288 if (state == SND_SOC_DPCM_STATE_START ||
2289 state == SND_SOC_DPCM_STATE_PAUSED ||
2290 state == SND_SOC_DPCM_STATE_SUSPEND)
2291 return 0;
2292 }
2293
2294 /* it's safe to free/stop this BE DAI */
2295 return 1;
2296}
2297EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2298
2299/*
2300 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2301 * running, paused or suspended for the specified stream direction.
2302 */
2303int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2304 struct snd_soc_pcm_runtime *be, int stream)
2305{
2306 struct snd_soc_dpcm *dpcm;
2307 int state;
2308
2309 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2310
2311 if (dpcm->fe == fe)
2312 continue;
2313
2314 state = dpcm->fe->dpcm[stream].state;
2315 if (state == SND_SOC_DPCM_STATE_START ||
2316 state == SND_SOC_DPCM_STATE_PAUSED ||
2317 state == SND_SOC_DPCM_STATE_SUSPEND ||
2318 state == SND_SOC_DPCM_STATE_PREPARE)
2319 return 0;
2320 }
2321
2322 /* it's safe to change hw_params */
2323 return 1;
2324}
2325EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002326
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002327int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2328 int cmd, struct snd_soc_platform *platform)
2329{
Mark Brownc5914b02013-10-30 17:47:39 -07002330 if (platform->driver->ops && platform->driver->ops->trigger)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002331 return platform->driver->ops->trigger(substream, cmd);
2332 return 0;
2333}
2334EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2335
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002336#ifdef CONFIG_DEBUG_FS
2337static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2338{
2339 switch (state) {
2340 case SND_SOC_DPCM_STATE_NEW:
2341 return "new";
2342 case SND_SOC_DPCM_STATE_OPEN:
2343 return "open";
2344 case SND_SOC_DPCM_STATE_HW_PARAMS:
2345 return "hw_params";
2346 case SND_SOC_DPCM_STATE_PREPARE:
2347 return "prepare";
2348 case SND_SOC_DPCM_STATE_START:
2349 return "start";
2350 case SND_SOC_DPCM_STATE_STOP:
2351 return "stop";
2352 case SND_SOC_DPCM_STATE_SUSPEND:
2353 return "suspend";
2354 case SND_SOC_DPCM_STATE_PAUSED:
2355 return "paused";
2356 case SND_SOC_DPCM_STATE_HW_FREE:
2357 return "hw_free";
2358 case SND_SOC_DPCM_STATE_CLOSE:
2359 return "close";
2360 }
2361
2362 return "unknown";
2363}
2364
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002365static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2366 int stream, char *buf, size_t size)
2367{
2368 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2369 struct snd_soc_dpcm *dpcm;
2370 ssize_t offset = 0;
2371
2372 /* FE state */
2373 offset += snprintf(buf + offset, size - offset,
2374 "[%s - %s]\n", fe->dai_link->name,
2375 stream ? "Capture" : "Playback");
2376
2377 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2378 dpcm_state_string(fe->dpcm[stream].state));
2379
2380 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2381 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2382 offset += snprintf(buf + offset, size - offset,
2383 "Hardware Params: "
2384 "Format = %s, Channels = %d, Rate = %d\n",
2385 snd_pcm_format_name(params_format(params)),
2386 params_channels(params),
2387 params_rate(params));
2388
2389 /* BEs state */
2390 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2391
2392 if (list_empty(&fe->dpcm[stream].be_clients)) {
2393 offset += snprintf(buf + offset, size - offset,
2394 " No active DSP links\n");
2395 goto out;
2396 }
2397
2398 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2399 struct snd_soc_pcm_runtime *be = dpcm->be;
2400 params = &dpcm->hw_params;
2401
2402 offset += snprintf(buf + offset, size - offset,
2403 "- %s\n", be->dai_link->name);
2404
2405 offset += snprintf(buf + offset, size - offset,
2406 " State: %s\n",
2407 dpcm_state_string(be->dpcm[stream].state));
2408
2409 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2410 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2411 offset += snprintf(buf + offset, size - offset,
2412 " Hardware Params: "
2413 "Format = %s, Channels = %d, Rate = %d\n",
2414 snd_pcm_format_name(params_format(params)),
2415 params_channels(params),
2416 params_rate(params));
2417 }
2418
2419out:
2420 return offset;
2421}
2422
2423static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2424 size_t count, loff_t *ppos)
2425{
2426 struct snd_soc_pcm_runtime *fe = file->private_data;
2427 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2428 char *buf;
2429
2430 buf = kmalloc(out_count, GFP_KERNEL);
2431 if (!buf)
2432 return -ENOMEM;
2433
2434 if (fe->cpu_dai->driver->playback.channels_min)
2435 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2436 buf + offset, out_count - offset);
2437
2438 if (fe->cpu_dai->driver->capture.channels_min)
2439 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2440 buf + offset, out_count - offset);
2441
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002442 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002443
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002444 kfree(buf);
2445 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002446}
2447
2448static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002449 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002450 .read = dpcm_state_read_file,
2451 .llseek = default_llseek,
2452};
2453
2454int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2455{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002456 if (!rtd->dai_link)
2457 return 0;
2458
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002459 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2460 rtd->card->debugfs_card_root);
2461 if (!rtd->debugfs_dpcm_root) {
2462 dev_dbg(rtd->dev,
2463 "ASoC: Failed to create dpcm debugfs directory %s\n",
2464 rtd->dai_link->name);
2465 return -EINVAL;
2466 }
2467
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002468 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002469 rtd->debugfs_dpcm_root,
2470 rtd, &dpcm_state_fops);
2471
2472 return 0;
2473}
2474#endif