blob: a3a70214edee2ae2fffb576f2a245a0afc8763e0 [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/**
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010038 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
39 * @rtd: ASoC PCM runtime that is activated
40 * @stream: Direction of the PCM stream
41 *
42 * Increments the active count for all the DAIs and components attached to a PCM
43 * runtime. Should typically be called when a stream is opened.
44 *
45 * Must be called with the rtd->pcm_mutex being held
46 */
47void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
48{
49 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
50 struct snd_soc_dai *codec_dai = rtd->codec_dai;
51
52 lockdep_assert_held(&rtd->pcm_mutex);
53
54 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
55 cpu_dai->playback_active++;
56 codec_dai->playback_active++;
57 } else {
58 cpu_dai->capture_active++;
59 codec_dai->capture_active++;
60 }
61
62 cpu_dai->active++;
63 codec_dai->active++;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +010064 cpu_dai->component->active++;
65 codec_dai->component->active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010066}
67
68/**
69 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
70 * @rtd: ASoC PCM runtime that is deactivated
71 * @stream: Direction of the PCM stream
72 *
73 * Decrements the active count for all the DAIs and components attached to a PCM
74 * runtime. Should typically be called when a stream is closed.
75 *
76 * Must be called with the rtd->pcm_mutex being held
77 */
78void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
79{
80 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
81 struct snd_soc_dai *codec_dai = rtd->codec_dai;
82
83 lockdep_assert_held(&rtd->pcm_mutex);
84
85 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
86 cpu_dai->playback_active--;
87 codec_dai->playback_active--;
88 } else {
89 cpu_dai->capture_active--;
90 codec_dai->capture_active--;
91 }
92
93 cpu_dai->active--;
94 codec_dai->active--;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +010095 cpu_dai->component->active--;
96 codec_dai->component->active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010097}
98
99/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100100 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
101 * @rtd: The ASoC PCM runtime that should be checked.
102 *
103 * This function checks whether the power down delay should be ignored for a
104 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
105 * been configured to ignore the delay, or if none of the components benefits
106 * from having the delay.
107 */
108bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
109{
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100110 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
111 return true;
112
Lars-Peter Clausen3d594002014-03-05 13:17:48 +0100113 return rtd->cpu_dai->component->ignore_pmdown_time &&
114 rtd->codec_dai->component->ignore_pmdown_time;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100115}
116
117/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200118 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
119 * @substream: the pcm substream
120 * @hw: the hardware parameters
121 *
122 * Sets the substream runtime hardware parameters.
123 */
124int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
125 const struct snd_pcm_hardware *hw)
126{
127 struct snd_pcm_runtime *runtime = substream->runtime;
128 runtime->hw.info = hw->info;
129 runtime->hw.formats = hw->formats;
130 runtime->hw.period_bytes_min = hw->period_bytes_min;
131 runtime->hw.period_bytes_max = hw->period_bytes_max;
132 runtime->hw.periods_min = hw->periods_min;
133 runtime->hw.periods_max = hw->periods_max;
134 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
135 runtime->hw.fifo_size = hw->fifo_size;
136 return 0;
137}
138EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
139
Liam Girdwood01d75842012-04-25 12:12:49 +0100140/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000141int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100142 int event)
143{
144 struct snd_soc_dpcm *dpcm;
145
146 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
147
148 struct snd_soc_pcm_runtime *be = dpcm->be;
149
Liam Girdwood103d84a2012-11-19 14:39:15 +0000150 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100151 be->dai_link->name, event, dir);
152
153 snd_soc_dapm_stream_event(be, dir, event);
154 }
155
156 snd_soc_dapm_stream_event(fe, dir, event);
157
158 return 0;
159}
160
Dong Aisheng17841022011-08-29 17:15:14 +0800161static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
162 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100163{
164 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100165 int ret;
166
Nicolin Chen3635bf02013-11-13 18:56:24 +0800167 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
168 rtd->dai_link->symmetric_rates)) {
169 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
170 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100171
Nicolin Chen3635bf02013-11-13 18:56:24 +0800172 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
173 SNDRV_PCM_HW_PARAM_RATE,
174 soc_dai->rate, soc_dai->rate);
175 if (ret < 0) {
176 dev_err(soc_dai->dev,
177 "ASoC: Unable to apply rate constraint: %d\n",
178 ret);
179 return ret;
180 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100181 }
182
Nicolin Chen3635bf02013-11-13 18:56:24 +0800183 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
184 rtd->dai_link->symmetric_channels)) {
185 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
186 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100187
Nicolin Chen3635bf02013-11-13 18:56:24 +0800188 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
189 SNDRV_PCM_HW_PARAM_CHANNELS,
190 soc_dai->channels,
191 soc_dai->channels);
192 if (ret < 0) {
193 dev_err(soc_dai->dev,
194 "ASoC: Unable to apply channel symmetry constraint: %d\n",
195 ret);
196 return ret;
197 }
198 }
199
200 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
201 rtd->dai_link->symmetric_samplebits)) {
202 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
203 soc_dai->sample_bits);
204
205 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
206 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
207 soc_dai->sample_bits,
208 soc_dai->sample_bits);
209 if (ret < 0) {
210 dev_err(soc_dai->dev,
211 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
212 ret);
213 return ret;
214 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100215 }
216
217 return 0;
218}
219
Nicolin Chen3635bf02013-11-13 18:56:24 +0800220static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
221 struct snd_pcm_hw_params *params)
222{
223 struct snd_soc_pcm_runtime *rtd = substream->private_data;
224 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
225 struct snd_soc_dai *codec_dai = rtd->codec_dai;
226 unsigned int rate, channels, sample_bits, symmetry;
227
228 rate = params_rate(params);
229 channels = params_channels(params);
230 sample_bits = snd_pcm_format_physical_width(params_format(params));
231
232 /* reject unmatched parameters when applying symmetry */
233 symmetry = cpu_dai->driver->symmetric_rates ||
234 codec_dai->driver->symmetric_rates ||
235 rtd->dai_link->symmetric_rates;
236 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
237 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
238 cpu_dai->rate, rate);
239 return -EINVAL;
240 }
241
242 symmetry = cpu_dai->driver->symmetric_channels ||
243 codec_dai->driver->symmetric_channels ||
244 rtd->dai_link->symmetric_channels;
245 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
246 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
247 cpu_dai->channels, channels);
248 return -EINVAL;
249 }
250
251 symmetry = cpu_dai->driver->symmetric_samplebits ||
252 codec_dai->driver->symmetric_samplebits ||
253 rtd->dai_link->symmetric_samplebits;
254 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
255 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
256 cpu_dai->sample_bits, sample_bits);
257 return -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100258 }
259
260 return 0;
261}
262
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100263static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
264{
265 struct snd_soc_pcm_runtime *rtd = substream->private_data;
266 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
267 struct snd_soc_dai_driver *codec_driver = rtd->codec_dai->driver;
268 struct snd_soc_dai_link *link = rtd->dai_link;
269
270 return cpu_driver->symmetric_rates || codec_driver->symmetric_rates ||
271 link->symmetric_rates || cpu_driver->symmetric_channels ||
272 codec_driver->symmetric_channels || link->symmetric_channels ||
273 cpu_driver->symmetric_samplebits ||
274 codec_driver->symmetric_samplebits ||
275 link->symmetric_samplebits;
276}
277
Liam Girdwoodddee6272011-06-09 14:45:53 +0100278/*
Mark Brown58ba9b22012-01-16 18:38:51 +0000279 * List of sample sizes that might go over the bus for parameter
280 * application. There ought to be a wildcard sample size for things
281 * like the DAC/ADC resolution to use but there isn't right now.
282 */
283static int sample_sizes[] = {
Peter Ujfalusi88e33952012-01-25 10:09:41 +0200284 24, 32,
Mark Brown58ba9b22012-01-16 18:38:51 +0000285};
286
287static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
288 struct snd_soc_dai *dai)
289{
290 int ret, i, bits;
291
292 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
293 bits = dai->driver->playback.sig_bits;
294 else
295 bits = dai->driver->capture.sig_bits;
296
297 if (!bits)
298 return;
299
300 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
Mark Brown278047f2012-01-19 18:04:18 +0000301 if (bits >= sample_sizes[i])
302 continue;
303
304 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
305 sample_sizes[i], bits);
Mark Brown58ba9b22012-01-16 18:38:51 +0000306 if (ret != 0)
307 dev_warn(dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +0000308 "ASoC: Failed to set MSB %d/%d: %d\n",
Mark Brown58ba9b22012-01-16 18:38:51 +0000309 bits, sample_sizes[i], ret);
310 }
311}
312
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100313static void soc_pcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200314 struct snd_soc_pcm_stream *codec_stream,
315 struct snd_soc_pcm_stream *cpu_stream)
316{
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100317 struct snd_pcm_hardware *hw = &runtime->hw;
318
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200319 hw->channels_min = max(codec_stream->channels_min,
320 cpu_stream->channels_min);
321 hw->channels_max = min(codec_stream->channels_max,
322 cpu_stream->channels_max);
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100323 if (hw->formats)
324 hw->formats &= codec_stream->formats & cpu_stream->formats;
325 else
326 hw->formats = codec_stream->formats & cpu_stream->formats;
Lars-Peter Clausen55dcdb52014-01-11 10:24:44 +0100327 hw->rates = snd_pcm_rate_mask_intersect(codec_stream->rates,
328 cpu_stream->rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100329
Lars-Peter Clausen817873f2014-01-11 10:24:40 +0100330 hw->rate_min = 0;
331 hw->rate_max = UINT_MAX;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100332
333 snd_pcm_limit_hw_rates(runtime);
334
335 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
336 hw->rate_min = max(hw->rate_min, codec_stream->rate_min);
337 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
338 hw->rate_max = min_not_zero(hw->rate_max, codec_stream->rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200339}
340
Mark Brown58ba9b22012-01-16 18:38:51 +0000341/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100342 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
343 * then initialized and any private data can be allocated. This also calls
344 * startup for the cpu DAI, platform, machine and codec DAI.
345 */
346static int soc_pcm_open(struct snd_pcm_substream *substream)
347{
348 struct snd_soc_pcm_runtime *rtd = substream->private_data;
349 struct snd_pcm_runtime *runtime = substream->runtime;
350 struct snd_soc_platform *platform = rtd->platform;
351 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
352 struct snd_soc_dai *codec_dai = rtd->codec_dai;
353 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
354 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
355 int ret = 0;
356
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800357 pinctrl_pm_select_default_state(cpu_dai->dev);
358 pinctrl_pm_select_default_state(codec_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000359 pm_runtime_get_sync(cpu_dai->dev);
360 pm_runtime_get_sync(codec_dai->dev);
361 pm_runtime_get_sync(platform->dev);
362
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100363 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100364
365 /* startup the audio subsystem */
Mark Brownc5914b02013-10-30 17:47:39 -0700366 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100367 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
368 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000369 dev_err(cpu_dai->dev, "ASoC: can't open interface"
370 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100371 goto out;
372 }
373 }
374
375 if (platform->driver->ops && platform->driver->ops->open) {
376 ret = platform->driver->ops->open(substream);
377 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000378 dev_err(platform->dev, "ASoC: can't open platform"
379 " %s: %d\n", platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100380 goto platform_err;
381 }
382 }
383
Mark Brownc5914b02013-10-30 17:47:39 -0700384 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100385 ret = codec_dai->driver->ops->startup(substream, codec_dai);
386 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000387 dev_err(codec_dai->dev, "ASoC: can't open codec"
388 " %s: %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100389 goto codec_dai_err;
390 }
391 }
392
393 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
394 ret = rtd->dai_link->ops->startup(substream);
395 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000396 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000397 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100398 goto machine_err;
399 }
400 }
401
Liam Girdwood01d75842012-04-25 12:12:49 +0100402 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
403 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
404 goto dynamic;
405
Liam Girdwoodddee6272011-06-09 14:45:53 +0100406 /* Check that the codec and cpu DAIs are compatible */
407 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100408 soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->playback,
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200409 &cpu_dai_drv->playback);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100410 } else {
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100411 soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->capture,
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200412 &cpu_dai_drv->capture);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100413 }
414
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100415 if (soc_pcm_has_symmetry(substream))
416 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
417
Liam Girdwoodddee6272011-06-09 14:45:53 +0100418 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100419 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000420 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100421 codec_dai->name, cpu_dai->name);
422 goto config_err;
423 }
424 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000425 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100426 codec_dai->name, cpu_dai->name);
427 goto config_err;
428 }
429 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
430 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000431 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100432 codec_dai->name, cpu_dai->name);
433 goto config_err;
434 }
435
Mark Brown58ba9b22012-01-16 18:38:51 +0000436 soc_pcm_apply_msb(substream, codec_dai);
437 soc_pcm_apply_msb(substream, cpu_dai);
438
Liam Girdwoodddee6272011-06-09 14:45:53 +0100439 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800440 if (cpu_dai->active) {
441 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
442 if (ret != 0)
443 goto config_err;
444 }
445
446 if (codec_dai->active) {
447 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100448 if (ret != 0)
449 goto config_err;
450 }
451
Liam Girdwood103d84a2012-11-19 14:39:15 +0000452 pr_debug("ASoC: %s <-> %s info:\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100453 codec_dai->name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000454 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
455 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100456 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000457 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100458 runtime->hw.rate_max);
459
Liam Girdwood01d75842012-04-25 12:12:49 +0100460dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100461
462 snd_soc_runtime_activate(rtd, substream->stream);
463
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100464 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100465 return 0;
466
467config_err:
468 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
469 rtd->dai_link->ops->shutdown(substream);
470
471machine_err:
472 if (codec_dai->driver->ops->shutdown)
473 codec_dai->driver->ops->shutdown(substream, codec_dai);
474
475codec_dai_err:
476 if (platform->driver->ops && platform->driver->ops->close)
477 platform->driver->ops->close(substream);
478
479platform_err:
480 if (cpu_dai->driver->ops->shutdown)
481 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
482out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100483 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000484
485 pm_runtime_put(platform->dev);
486 pm_runtime_put(codec_dai->dev);
487 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800488 if (!codec_dai->active)
489 pinctrl_pm_select_sleep_state(codec_dai->dev);
490 if (!cpu_dai->active)
491 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000492
Liam Girdwoodddee6272011-06-09 14:45:53 +0100493 return ret;
494}
495
496/*
497 * Power down the audio subsystem pmdown_time msecs after close is called.
498 * This is to ensure there are no pops or clicks in between any music tracks
499 * due to DAPM power cycling.
500 */
501static void close_delayed_work(struct work_struct *work)
502{
503 struct snd_soc_pcm_runtime *rtd =
504 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
505 struct snd_soc_dai *codec_dai = rtd->codec_dai;
506
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100507 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100508
Liam Girdwood103d84a2012-11-19 14:39:15 +0000509 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100510 codec_dai->driver->playback.stream_name,
511 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600512 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100513
514 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600515 if (rtd->pop_wait == 1) {
516 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800517 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000518 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100519 }
520
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100521 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100522}
523
524/*
525 * Called by ALSA when a PCM substream is closed. Private data can be
526 * freed here. The cpu DAI, codec DAI, machine and platform are also
527 * shutdown.
528 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100529static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100530{
531 struct snd_soc_pcm_runtime *rtd = substream->private_data;
532 struct snd_soc_platform *platform = rtd->platform;
533 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
534 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100535
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100536 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100537
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100538 snd_soc_runtime_deactivate(rtd, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100539
Dong Aisheng17841022011-08-29 17:15:14 +0800540 /* clear the corresponding DAIs rate when inactive */
541 if (!cpu_dai->active)
542 cpu_dai->rate = 0;
543
544 if (!codec_dai->active)
545 codec_dai->rate = 0;
Sascha Hauer25b76792011-08-17 09:20:01 +0200546
Liam Girdwoodddee6272011-06-09 14:45:53 +0100547 if (cpu_dai->driver->ops->shutdown)
548 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
549
550 if (codec_dai->driver->ops->shutdown)
551 codec_dai->driver->ops->shutdown(substream, codec_dai);
552
553 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
554 rtd->dai_link->ops->shutdown(substream);
555
556 if (platform->driver->ops && platform->driver->ops->close)
557 platform->driver->ops->close(substream);
558 cpu_dai->runtime = NULL;
559
560 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100561 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300562 /* powered down playback stream now */
563 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800564 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800565 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300566 } else {
567 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600568 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100569 queue_delayed_work(system_power_efficient_wq,
570 &rtd->delayed_work,
571 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300572 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100573 } else {
574 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800575 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000576 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100577 }
578
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100579 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000580
581 pm_runtime_put(platform->dev);
582 pm_runtime_put(codec_dai->dev);
583 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800584 if (!codec_dai->active)
585 pinctrl_pm_select_sleep_state(codec_dai->dev);
586 if (!cpu_dai->active)
587 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000588
Liam Girdwoodddee6272011-06-09 14:45:53 +0100589 return 0;
590}
591
592/*
593 * Called by ALSA when the PCM substream is prepared, can set format, sample
594 * rate, etc. This function is non atomic and can be called multiple times,
595 * it can refer to the runtime info.
596 */
597static int soc_pcm_prepare(struct snd_pcm_substream *substream)
598{
599 struct snd_soc_pcm_runtime *rtd = substream->private_data;
600 struct snd_soc_platform *platform = rtd->platform;
601 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
602 struct snd_soc_dai *codec_dai = rtd->codec_dai;
603 int ret = 0;
604
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100605 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100606
607 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
608 ret = rtd->dai_link->ops->prepare(substream);
609 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000610 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
611 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100612 goto out;
613 }
614 }
615
616 if (platform->driver->ops && platform->driver->ops->prepare) {
617 ret = platform->driver->ops->prepare(substream);
618 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000619 dev_err(platform->dev, "ASoC: platform prepare error:"
620 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100621 goto out;
622 }
623 }
624
Mark Brownc5914b02013-10-30 17:47:39 -0700625 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100626 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
627 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000628 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000629 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100630 goto out;
631 }
632 }
633
Mark Brownc5914b02013-10-30 17:47:39 -0700634 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100635 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
636 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000637 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000638 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100639 goto out;
640 }
641 }
642
643 /* cancel any delayed stream shutdown that is pending */
644 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600645 rtd->pop_wait) {
646 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100647 cancel_delayed_work(&rtd->delayed_work);
648 }
649
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000650 snd_soc_dapm_stream_event(rtd, substream->stream,
651 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100652
Mark Brownda183962013-02-06 15:44:07 +0000653 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100654
655out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100656 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100657 return ret;
658}
659
660/*
661 * Called by ALSA when the hardware params are set by application. This
662 * function can also be called multiple times and can allocate buffers
663 * (using snd_pcm_lib_* ). It's non-atomic.
664 */
665static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
666 struct snd_pcm_hw_params *params)
667{
668 struct snd_soc_pcm_runtime *rtd = substream->private_data;
669 struct snd_soc_platform *platform = rtd->platform;
670 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
671 struct snd_soc_dai *codec_dai = rtd->codec_dai;
672 int ret = 0;
673
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100674 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100675
Nicolin Chen3635bf02013-11-13 18:56:24 +0800676 ret = soc_pcm_params_symmetry(substream, params);
677 if (ret)
678 goto out;
679
Liam Girdwoodddee6272011-06-09 14:45:53 +0100680 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
681 ret = rtd->dai_link->ops->hw_params(substream, params);
682 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000683 dev_err(rtd->card->dev, "ASoC: machine hw_params"
684 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100685 goto out;
686 }
687 }
688
Mark Brownc5914b02013-10-30 17:47:39 -0700689 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100690 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
691 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000692 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
693 " %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100694 goto codec_err;
695 }
696 }
697
Mark Brownc5914b02013-10-30 17:47:39 -0700698 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100699 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
700 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000701 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000702 cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100703 goto interface_err;
704 }
705 }
706
707 if (platform->driver->ops && platform->driver->ops->hw_params) {
708 ret = platform->driver->ops->hw_params(substream, params);
709 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000710 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000711 platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100712 goto platform_err;
713 }
714 }
715
Nicolin Chen3635bf02013-11-13 18:56:24 +0800716 /* store the parameters for each DAIs */
Dong Aisheng17841022011-08-29 17:15:14 +0800717 cpu_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800718 cpu_dai->channels = params_channels(params);
719 cpu_dai->sample_bits =
720 snd_pcm_format_physical_width(params_format(params));
721
Dong Aisheng17841022011-08-29 17:15:14 +0800722 codec_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800723 codec_dai->channels = params_channels(params);
724 codec_dai->sample_bits =
725 snd_pcm_format_physical_width(params_format(params));
Liam Girdwoodddee6272011-06-09 14:45:53 +0100726
727out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100728 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100729 return ret;
730
731platform_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700732 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100733 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
734
735interface_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700736 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100737 codec_dai->driver->ops->hw_free(substream, codec_dai);
738
739codec_err:
740 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
741 rtd->dai_link->ops->hw_free(substream);
742
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100743 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100744 return ret;
745}
746
747/*
748 * Frees resources allocated by hw_params, can be called multiple times
749 */
750static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
751{
752 struct snd_soc_pcm_runtime *rtd = substream->private_data;
753 struct snd_soc_platform *platform = rtd->platform;
754 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
755 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +0800756 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100757
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100758 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100759
Nicolin Chend3383422013-11-20 18:37:09 +0800760 /* clear the corresponding DAIs parameters when going to be inactive */
761 if (cpu_dai->active == 1) {
762 cpu_dai->rate = 0;
763 cpu_dai->channels = 0;
764 cpu_dai->sample_bits = 0;
765 }
766
767 if (codec_dai->active == 1) {
768 codec_dai->rate = 0;
769 codec_dai->channels = 0;
770 codec_dai->sample_bits = 0;
771 }
772
Liam Girdwoodddee6272011-06-09 14:45:53 +0100773 /* apply codec digital mute */
Nicolin Chen7f62b6e2013-12-04 11:18:36 +0800774 if ((playback && codec_dai->playback_active == 1) ||
775 (!playback && codec_dai->capture_active == 1))
Mark Brownda183962013-02-06 15:44:07 +0000776 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100777
778 /* free any machine hw params */
779 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
780 rtd->dai_link->ops->hw_free(substream);
781
782 /* free any DMA resources */
783 if (platform->driver->ops && platform->driver->ops->hw_free)
784 platform->driver->ops->hw_free(substream);
785
786 /* now free hw params for the DAIs */
Mark Brownc5914b02013-10-30 17:47:39 -0700787 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100788 codec_dai->driver->ops->hw_free(substream, codec_dai);
789
Mark Brownc5914b02013-10-30 17:47:39 -0700790 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100791 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
792
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100793 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100794 return 0;
795}
796
797static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
798{
799 struct snd_soc_pcm_runtime *rtd = substream->private_data;
800 struct snd_soc_platform *platform = rtd->platform;
801 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
802 struct snd_soc_dai *codec_dai = rtd->codec_dai;
803 int ret;
804
Mark Brownc5914b02013-10-30 17:47:39 -0700805 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100806 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
807 if (ret < 0)
808 return ret;
809 }
810
811 if (platform->driver->ops && platform->driver->ops->trigger) {
812 ret = platform->driver->ops->trigger(substream, cmd);
813 if (ret < 0)
814 return ret;
815 }
816
Mark Brownc5914b02013-10-30 17:47:39 -0700817 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100818 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
819 if (ret < 0)
820 return ret;
821 }
Jarkko Nikula4792b0d2014-04-28 14:17:52 +0200822
823 if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
824 ret = rtd->dai_link->ops->trigger(substream, cmd);
825 if (ret < 0)
826 return ret;
827 }
828
Liam Girdwoodddee6272011-06-09 14:45:53 +0100829 return 0;
830}
831
Mark Brown45c0a182012-05-09 21:46:27 +0100832static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
833 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100834{
835 struct snd_soc_pcm_runtime *rtd = substream->private_data;
836 struct snd_soc_platform *platform = rtd->platform;
837 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
838 struct snd_soc_dai *codec_dai = rtd->codec_dai;
839 int ret;
840
Mark Brownc5914b02013-10-30 17:47:39 -0700841 if (codec_dai->driver->ops &&
842 codec_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100843 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
844 if (ret < 0)
845 return ret;
846 }
847
Jean-Francois Moinedcf0fa22014-01-03 09:19:18 +0100848 if (platform->driver->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100849 ret = platform->driver->bespoke_trigger(substream, cmd);
850 if (ret < 0)
851 return ret;
852 }
853
Mark Brownc5914b02013-10-30 17:47:39 -0700854 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100855 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
856 if (ret < 0)
857 return ret;
858 }
859 return 0;
860}
Liam Girdwoodddee6272011-06-09 14:45:53 +0100861/*
862 * soc level wrapper for pointer callback
863 * If cpu_dai, codec_dai, platform driver has the delay callback, than
864 * the runtime->delay will be updated accordingly.
865 */
866static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
867{
868 struct snd_soc_pcm_runtime *rtd = substream->private_data;
869 struct snd_soc_platform *platform = rtd->platform;
870 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
871 struct snd_soc_dai *codec_dai = rtd->codec_dai;
872 struct snd_pcm_runtime *runtime = substream->runtime;
873 snd_pcm_uframes_t offset = 0;
874 snd_pcm_sframes_t delay = 0;
875
876 if (platform->driver->ops && platform->driver->ops->pointer)
877 offset = platform->driver->ops->pointer(substream);
878
Mark Brownc5914b02013-10-30 17:47:39 -0700879 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100880 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
881
Mark Brownc5914b02013-10-30 17:47:39 -0700882 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100883 delay += codec_dai->driver->ops->delay(substream, codec_dai);
884
885 if (platform->driver->delay)
886 delay += platform->driver->delay(substream, codec_dai);
887
888 runtime->delay = delay;
889
890 return offset;
891}
892
Liam Girdwood01d75842012-04-25 12:12:49 +0100893/* connect a FE and BE */
894static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
895 struct snd_soc_pcm_runtime *be, int stream)
896{
897 struct snd_soc_dpcm *dpcm;
898
899 /* only add new dpcms */
900 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
901 if (dpcm->be == be && dpcm->fe == fe)
902 return 0;
903 }
904
905 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
906 if (!dpcm)
907 return -ENOMEM;
908
909 dpcm->be = be;
910 dpcm->fe = fe;
911 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
912 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
913 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
914 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
915
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300916 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100917 stream ? "capture" : "playback", fe->dai_link->name,
918 stream ? "<-" : "->", be->dai_link->name);
919
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100920#ifdef CONFIG_DEBUG_FS
921 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
922 fe->debugfs_dpcm_root, &dpcm->state);
923#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100924 return 1;
925}
926
927/* reparent a BE onto another FE */
928static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
929 struct snd_soc_pcm_runtime *be, int stream)
930{
931 struct snd_soc_dpcm *dpcm;
932 struct snd_pcm_substream *fe_substream, *be_substream;
933
934 /* reparent if BE is connected to other FEs */
935 if (!be->dpcm[stream].users)
936 return;
937
938 be_substream = snd_soc_dpcm_get_substream(be, stream);
939
940 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
941 if (dpcm->fe == fe)
942 continue;
943
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300944 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100945 stream ? "capture" : "playback",
946 dpcm->fe->dai_link->name,
947 stream ? "<-" : "->", dpcm->be->dai_link->name);
948
949 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
950 be_substream->runtime = fe_substream->runtime;
951 break;
952 }
953}
954
955/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +0000956void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +0100957{
958 struct snd_soc_dpcm *dpcm, *d;
959
960 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000961 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100962 stream ? "capture" : "playback",
963 dpcm->be->dai_link->name);
964
965 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
966 continue;
967
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300968 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100969 stream ? "capture" : "playback", fe->dai_link->name,
970 stream ? "<-" : "->", dpcm->be->dai_link->name);
971
972 /* BEs still alive need new FE */
973 dpcm_be_reparent(fe, dpcm->be, stream);
974
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100975#ifdef CONFIG_DEBUG_FS
976 debugfs_remove(dpcm->debugfs_state);
977#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100978 list_del(&dpcm->list_be);
979 list_del(&dpcm->list_fe);
980 kfree(dpcm);
981 }
982}
983
984/* get BE for DAI widget and stream */
985static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
986 struct snd_soc_dapm_widget *widget, int stream)
987{
988 struct snd_soc_pcm_runtime *be;
989 int i;
990
991 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
992 for (i = 0; i < card->num_links; i++) {
993 be = &card->rtd[i];
994
Liam Girdwood35ea0652012-06-05 19:26:59 +0100995 if (!be->dai_link->no_pcm)
996 continue;
997
Liam Girdwood01d75842012-04-25 12:12:49 +0100998 if (be->cpu_dai->playback_widget == widget ||
999 be->codec_dai->playback_widget == widget)
1000 return be;
1001 }
1002 } else {
1003
1004 for (i = 0; i < card->num_links; i++) {
1005 be = &card->rtd[i];
1006
Liam Girdwood35ea0652012-06-05 19:26:59 +01001007 if (!be->dai_link->no_pcm)
1008 continue;
1009
Liam Girdwood01d75842012-04-25 12:12:49 +01001010 if (be->cpu_dai->capture_widget == widget ||
1011 be->codec_dai->capture_widget == widget)
1012 return be;
1013 }
1014 }
1015
Liam Girdwood103d84a2012-11-19 14:39:15 +00001016 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001017 stream ? "capture" : "playback", widget->name);
1018 return NULL;
1019}
1020
1021static inline struct snd_soc_dapm_widget *
1022 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
1023{
1024 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1025 return rtd->cpu_dai->playback_widget;
1026 else
1027 return rtd->cpu_dai->capture_widget;
1028}
1029
1030static inline struct snd_soc_dapm_widget *
1031 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
1032{
1033 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1034 return rtd->codec_dai->playback_widget;
1035 else
1036 return rtd->codec_dai->capture_widget;
1037}
1038
1039static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1040 struct snd_soc_dapm_widget *widget)
1041{
1042 int i;
1043
1044 for (i = 0; i < list->num_widgets; i++) {
1045 if (widget == list->widgets[i])
1046 return 1;
1047 }
1048
1049 return 0;
1050}
1051
Liam Girdwood23607022014-01-17 17:03:55 +00001052int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001053 int stream, struct snd_soc_dapm_widget_list **list_)
1054{
1055 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1056 struct snd_soc_dapm_widget_list *list;
1057 int paths;
1058
1059 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
1060 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
1061 if (list == NULL)
1062 return -ENOMEM;
1063
1064 /* get number of valid DAI paths and their widgets */
1065 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
1066
Liam Girdwood103d84a2012-11-19 14:39:15 +00001067 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001068 stream ? "capture" : "playback");
1069
1070 *list_ = list;
1071 return paths;
1072}
1073
Liam Girdwood01d75842012-04-25 12:12:49 +01001074static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1075 struct snd_soc_dapm_widget_list **list_)
1076{
1077 struct snd_soc_dpcm *dpcm;
1078 struct snd_soc_dapm_widget_list *list = *list_;
1079 struct snd_soc_dapm_widget *widget;
1080 int prune = 0;
1081
1082 /* Destroy any old FE <--> BE connections */
1083 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1084
1085 /* is there a valid CPU DAI widget for this BE */
1086 widget = rtd_get_cpu_widget(dpcm->be, stream);
1087
1088 /* prune the BE if it's no longer in our active list */
1089 if (widget && widget_in_list(list, widget))
1090 continue;
1091
1092 /* is there a valid CODEC DAI widget for this BE */
1093 widget = rtd_get_codec_widget(dpcm->be, stream);
1094
1095 /* prune the BE if it's no longer in our active list */
1096 if (widget && widget_in_list(list, widget))
1097 continue;
1098
Liam Girdwood103d84a2012-11-19 14:39:15 +00001099 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001100 stream ? "capture" : "playback",
1101 dpcm->be->dai_link->name, fe->dai_link->name);
1102 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1103 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1104 prune++;
1105 }
1106
Liam Girdwood103d84a2012-11-19 14:39:15 +00001107 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001108 return prune;
1109}
1110
1111static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1112 struct snd_soc_dapm_widget_list **list_)
1113{
1114 struct snd_soc_card *card = fe->card;
1115 struct snd_soc_dapm_widget_list *list = *list_;
1116 struct snd_soc_pcm_runtime *be;
1117 int i, new = 0, err;
1118
1119 /* Create any new FE <--> BE connections */
1120 for (i = 0; i < list->num_widgets; i++) {
1121
Mark Brown46162742013-06-05 19:36:11 +01001122 switch (list->widgets[i]->id) {
1123 case snd_soc_dapm_dai_in:
1124 case snd_soc_dapm_dai_out:
1125 break;
1126 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001127 continue;
Mark Brown46162742013-06-05 19:36:11 +01001128 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001129
1130 /* is there a valid BE rtd for this widget */
1131 be = dpcm_get_be(card, list->widgets[i], stream);
1132 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001133 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001134 list->widgets[i]->name);
1135 continue;
1136 }
1137
1138 /* make sure BE is a real BE */
1139 if (!be->dai_link->no_pcm)
1140 continue;
1141
1142 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001143 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001144 continue;
1145
1146 /* newly connected FE and BE */
1147 err = dpcm_be_connect(fe, be, stream);
1148 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001149 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001150 list->widgets[i]->name);
1151 break;
1152 } else if (err == 0) /* already connected */
1153 continue;
1154
1155 /* new */
1156 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1157 new++;
1158 }
1159
Liam Girdwood103d84a2012-11-19 14:39:15 +00001160 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001161 return new;
1162}
1163
1164/*
1165 * Find the corresponding BE DAIs that source or sink audio to this
1166 * FE substream.
1167 */
Liam Girdwood23607022014-01-17 17:03:55 +00001168int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001169 int stream, struct snd_soc_dapm_widget_list **list, int new)
1170{
1171 if (new)
1172 return dpcm_add_paths(fe, stream, list);
1173 else
1174 return dpcm_prune_paths(fe, stream, list);
1175}
1176
Liam Girdwood23607022014-01-17 17:03:55 +00001177void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001178{
1179 struct snd_soc_dpcm *dpcm;
1180
1181 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1182 dpcm->be->dpcm[stream].runtime_update =
1183 SND_SOC_DPCM_UPDATE_NO;
1184}
1185
1186static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1187 int stream)
1188{
1189 struct snd_soc_dpcm *dpcm;
1190
1191 /* disable any enabled and non active backends */
1192 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1193
1194 struct snd_soc_pcm_runtime *be = dpcm->be;
1195 struct snd_pcm_substream *be_substream =
1196 snd_soc_dpcm_get_substream(be, stream);
1197
1198 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001199 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001200 stream ? "capture" : "playback",
1201 be->dpcm[stream].state);
1202
1203 if (--be->dpcm[stream].users != 0)
1204 continue;
1205
1206 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1207 continue;
1208
1209 soc_pcm_close(be_substream);
1210 be_substream->runtime = NULL;
1211 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1212 }
1213}
1214
Liam Girdwood23607022014-01-17 17:03:55 +00001215int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001216{
1217 struct snd_soc_dpcm *dpcm;
1218 int err, count = 0;
1219
1220 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1221 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1222
1223 struct snd_soc_pcm_runtime *be = dpcm->be;
1224 struct snd_pcm_substream *be_substream =
1225 snd_soc_dpcm_get_substream(be, stream);
1226
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001227 if (!be_substream) {
1228 dev_err(be->dev, "ASoC: no backend %s stream\n",
1229 stream ? "capture" : "playback");
1230 continue;
1231 }
1232
Liam Girdwood01d75842012-04-25 12:12:49 +01001233 /* is this op for this BE ? */
1234 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1235 continue;
1236
1237 /* first time the dpcm is open ? */
1238 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001239 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001240 stream ? "capture" : "playback",
1241 be->dpcm[stream].state);
1242
1243 if (be->dpcm[stream].users++ != 0)
1244 continue;
1245
1246 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1247 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1248 continue;
1249
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001250 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1251 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001252
1253 be_substream->runtime = be->dpcm[stream].runtime;
1254 err = soc_pcm_open(be_substream);
1255 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001256 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001257 be->dpcm[stream].users--;
1258 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001259 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001260 stream ? "capture" : "playback",
1261 be->dpcm[stream].state);
1262
1263 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1264 goto unwind;
1265 }
1266
1267 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1268 count++;
1269 }
1270
1271 return count;
1272
1273unwind:
1274 /* disable any enabled and non active backends */
1275 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1276 struct snd_soc_pcm_runtime *be = dpcm->be;
1277 struct snd_pcm_substream *be_substream =
1278 snd_soc_dpcm_get_substream(be, stream);
1279
1280 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1281 continue;
1282
1283 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001284 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001285 stream ? "capture" : "playback",
1286 be->dpcm[stream].state);
1287
1288 if (--be->dpcm[stream].users != 0)
1289 continue;
1290
1291 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1292 continue;
1293
1294 soc_pcm_close(be_substream);
1295 be_substream->runtime = NULL;
1296 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1297 }
1298
1299 return err;
1300}
1301
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001302static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1303 struct snd_soc_pcm_stream *stream)
1304{
1305 runtime->hw.rate_min = stream->rate_min;
1306 runtime->hw.rate_max = stream->rate_max;
1307 runtime->hw.channels_min = stream->channels_min;
1308 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001309 if (runtime->hw.formats)
1310 runtime->hw.formats &= stream->formats;
1311 else
1312 runtime->hw.formats = stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001313 runtime->hw.rates = stream->rates;
1314}
1315
Mark Brown45c0a182012-05-09 21:46:27 +01001316static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001317{
1318 struct snd_pcm_runtime *runtime = substream->runtime;
1319 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1320 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1321 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1322
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001323 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1324 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1325 else
1326 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
Liam Girdwood01d75842012-04-25 12:12:49 +01001327}
1328
1329static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1330{
1331 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1332 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1333 int stream = fe_substream->stream, ret = 0;
1334
1335 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1336
1337 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1338 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001339 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001340 goto be_err;
1341 }
1342
Liam Girdwood103d84a2012-11-19 14:39:15 +00001343 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001344
1345 /* start the DAI frontend */
1346 ret = soc_pcm_open(fe_substream);
1347 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001348 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001349 goto unwind;
1350 }
1351
1352 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1353
1354 dpcm_set_fe_runtime(fe_substream);
1355 snd_pcm_limit_hw_rates(runtime);
1356
1357 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1358 return 0;
1359
1360unwind:
1361 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1362be_err:
1363 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1364 return ret;
1365}
1366
Liam Girdwood23607022014-01-17 17:03:55 +00001367int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001368{
1369 struct snd_soc_dpcm *dpcm;
1370
1371 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1372 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1373
1374 struct snd_soc_pcm_runtime *be = dpcm->be;
1375 struct snd_pcm_substream *be_substream =
1376 snd_soc_dpcm_get_substream(be, stream);
1377
1378 /* is this op for this BE ? */
1379 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1380 continue;
1381
1382 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001383 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001384 stream ? "capture" : "playback",
1385 be->dpcm[stream].state);
1386
1387 if (--be->dpcm[stream].users != 0)
1388 continue;
1389
1390 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1391 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1392 continue;
1393
Liam Girdwood103d84a2012-11-19 14:39:15 +00001394 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001395 dpcm->fe->dai_link->name);
1396
1397 soc_pcm_close(be_substream);
1398 be_substream->runtime = NULL;
1399
1400 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1401 }
1402 return 0;
1403}
1404
1405static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1406{
1407 struct snd_soc_pcm_runtime *fe = substream->private_data;
1408 int stream = substream->stream;
1409
1410 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1411
1412 /* shutdown the BEs */
1413 dpcm_be_dai_shutdown(fe, substream->stream);
1414
Liam Girdwood103d84a2012-11-19 14:39:15 +00001415 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001416
1417 /* now shutdown the frontend */
1418 soc_pcm_close(substream);
1419
1420 /* run the stream event for each BE */
1421 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1422
1423 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1424 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1425 return 0;
1426}
1427
Liam Girdwood23607022014-01-17 17:03:55 +00001428int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001429{
1430 struct snd_soc_dpcm *dpcm;
1431
1432 /* only hw_params backends that are either sinks or sources
1433 * to this frontend DAI */
1434 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1435
1436 struct snd_soc_pcm_runtime *be = dpcm->be;
1437 struct snd_pcm_substream *be_substream =
1438 snd_soc_dpcm_get_substream(be, stream);
1439
1440 /* is this op for this BE ? */
1441 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1442 continue;
1443
1444 /* only free hw when no longer used - check all FEs */
1445 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1446 continue;
1447
1448 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1449 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1450 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001451 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001452 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1453 continue;
1454
Liam Girdwood103d84a2012-11-19 14:39:15 +00001455 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001456 dpcm->fe->dai_link->name);
1457
1458 soc_pcm_hw_free(be_substream);
1459
1460 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1461 }
1462
1463 return 0;
1464}
1465
Mark Brown45c0a182012-05-09 21:46:27 +01001466static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001467{
1468 struct snd_soc_pcm_runtime *fe = substream->private_data;
1469 int err, stream = substream->stream;
1470
1471 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1472 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1473
Liam Girdwood103d84a2012-11-19 14:39:15 +00001474 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001475
1476 /* call hw_free on the frontend */
1477 err = soc_pcm_hw_free(substream);
1478 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001479 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001480 fe->dai_link->name);
1481
1482 /* only hw_params backends that are either sinks or sources
1483 * to this frontend DAI */
1484 err = dpcm_be_dai_hw_free(fe, stream);
1485
1486 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1487 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1488
1489 mutex_unlock(&fe->card->mutex);
1490 return 0;
1491}
1492
Liam Girdwood23607022014-01-17 17:03:55 +00001493int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001494{
1495 struct snd_soc_dpcm *dpcm;
1496 int ret;
1497
1498 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1499
1500 struct snd_soc_pcm_runtime *be = dpcm->be;
1501 struct snd_pcm_substream *be_substream =
1502 snd_soc_dpcm_get_substream(be, stream);
1503
1504 /* is this op for this BE ? */
1505 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1506 continue;
1507
1508 /* only allow hw_params() if no connected FEs are running */
1509 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1510 continue;
1511
1512 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1513 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1514 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1515 continue;
1516
Liam Girdwood103d84a2012-11-19 14:39:15 +00001517 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001518 dpcm->fe->dai_link->name);
1519
1520 /* copy params for each dpcm */
1521 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1522 sizeof(struct snd_pcm_hw_params));
1523
1524 /* perform any hw_params fixups */
1525 if (be->dai_link->be_hw_params_fixup) {
1526 ret = be->dai_link->be_hw_params_fixup(be,
1527 &dpcm->hw_params);
1528 if (ret < 0) {
1529 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001530 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001531 ret);
1532 goto unwind;
1533 }
1534 }
1535
1536 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1537 if (ret < 0) {
1538 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001539 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001540 goto unwind;
1541 }
1542
1543 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1544 }
1545 return 0;
1546
1547unwind:
1548 /* disable any enabled and non active backends */
1549 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1550 struct snd_soc_pcm_runtime *be = dpcm->be;
1551 struct snd_pcm_substream *be_substream =
1552 snd_soc_dpcm_get_substream(be, stream);
1553
1554 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1555 continue;
1556
1557 /* only allow hw_free() if no connected FEs are running */
1558 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1559 continue;
1560
1561 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1562 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1563 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1564 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1565 continue;
1566
1567 soc_pcm_hw_free(be_substream);
1568 }
1569
1570 return ret;
1571}
1572
Mark Brown45c0a182012-05-09 21:46:27 +01001573static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1574 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001575{
1576 struct snd_soc_pcm_runtime *fe = substream->private_data;
1577 int ret, stream = substream->stream;
1578
1579 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1580 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1581
1582 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1583 sizeof(struct snd_pcm_hw_params));
1584 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1585 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001586 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001587 goto out;
1588 }
1589
Liam Girdwood103d84a2012-11-19 14:39:15 +00001590 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001591 fe->dai_link->name, params_rate(params),
1592 params_channels(params), params_format(params));
1593
1594 /* call hw_params on the frontend */
1595 ret = soc_pcm_hw_params(substream, params);
1596 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001597 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001598 dpcm_be_dai_hw_free(fe, stream);
1599 } else
1600 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1601
1602out:
1603 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1604 mutex_unlock(&fe->card->mutex);
1605 return ret;
1606}
1607
1608static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1609 struct snd_pcm_substream *substream, int cmd)
1610{
1611 int ret;
1612
Liam Girdwood103d84a2012-11-19 14:39:15 +00001613 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001614 dpcm->fe->dai_link->name, cmd);
1615
1616 ret = soc_pcm_trigger(substream, cmd);
1617 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001618 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001619
1620 return ret;
1621}
1622
Liam Girdwood23607022014-01-17 17:03:55 +00001623int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01001624 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001625{
1626 struct snd_soc_dpcm *dpcm;
1627 int ret = 0;
1628
1629 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1630
1631 struct snd_soc_pcm_runtime *be = dpcm->be;
1632 struct snd_pcm_substream *be_substream =
1633 snd_soc_dpcm_get_substream(be, stream);
1634
1635 /* is this op for this BE ? */
1636 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1637 continue;
1638
1639 switch (cmd) {
1640 case SNDRV_PCM_TRIGGER_START:
1641 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1642 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1643 continue;
1644
1645 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1646 if (ret)
1647 return ret;
1648
1649 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1650 break;
1651 case SNDRV_PCM_TRIGGER_RESUME:
1652 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1653 continue;
1654
1655 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1656 if (ret)
1657 return ret;
1658
1659 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1660 break;
1661 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1662 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1663 continue;
1664
1665 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1666 if (ret)
1667 return ret;
1668
1669 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1670 break;
1671 case SNDRV_PCM_TRIGGER_STOP:
1672 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1673 continue;
1674
1675 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1676 continue;
1677
1678 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1679 if (ret)
1680 return ret;
1681
1682 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1683 break;
1684 case SNDRV_PCM_TRIGGER_SUSPEND:
1685 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1686 continue;
1687
1688 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1689 continue;
1690
1691 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1692 if (ret)
1693 return ret;
1694
1695 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1696 break;
1697 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1698 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1699 continue;
1700
1701 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1702 continue;
1703
1704 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1705 if (ret)
1706 return ret;
1707
1708 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1709 break;
1710 }
1711 }
1712
1713 return ret;
1714}
1715EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1716
Mark Brown45c0a182012-05-09 21:46:27 +01001717static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001718{
1719 struct snd_soc_pcm_runtime *fe = substream->private_data;
1720 int stream = substream->stream, ret;
1721 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1722
1723 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1724
1725 switch (trigger) {
1726 case SND_SOC_DPCM_TRIGGER_PRE:
1727 /* call trigger on the frontend before the backend. */
1728
Liam Girdwood103d84a2012-11-19 14:39:15 +00001729 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001730 fe->dai_link->name, cmd);
1731
1732 ret = soc_pcm_trigger(substream, cmd);
1733 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001734 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001735 goto out;
1736 }
1737
1738 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1739 break;
1740 case SND_SOC_DPCM_TRIGGER_POST:
1741 /* call trigger on the frontend after the backend. */
1742
1743 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1744 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001745 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001746 goto out;
1747 }
1748
Liam Girdwood103d84a2012-11-19 14:39:15 +00001749 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001750 fe->dai_link->name, cmd);
1751
1752 ret = soc_pcm_trigger(substream, cmd);
1753 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001754 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1755 /* bespoke trigger() - handles both FE and BEs */
1756
Liam Girdwood103d84a2012-11-19 14:39:15 +00001757 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001758 fe->dai_link->name, cmd);
1759
1760 ret = soc_pcm_bespoke_trigger(substream, cmd);
1761 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001762 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001763 goto out;
1764 }
1765 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01001766 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00001767 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01001768 fe->dai_link->name);
1769 ret = -EINVAL;
1770 goto out;
1771 }
1772
1773 switch (cmd) {
1774 case SNDRV_PCM_TRIGGER_START:
1775 case SNDRV_PCM_TRIGGER_RESUME:
1776 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1777 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1778 break;
1779 case SNDRV_PCM_TRIGGER_STOP:
1780 case SNDRV_PCM_TRIGGER_SUSPEND:
1781 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1782 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1783 break;
1784 }
1785
1786out:
1787 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1788 return ret;
1789}
1790
Liam Girdwood23607022014-01-17 17:03:55 +00001791int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001792{
1793 struct snd_soc_dpcm *dpcm;
1794 int ret = 0;
1795
1796 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1797
1798 struct snd_soc_pcm_runtime *be = dpcm->be;
1799 struct snd_pcm_substream *be_substream =
1800 snd_soc_dpcm_get_substream(be, stream);
1801
1802 /* is this op for this BE ? */
1803 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1804 continue;
1805
1806 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1807 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1808 continue;
1809
Liam Girdwood103d84a2012-11-19 14:39:15 +00001810 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001811 dpcm->fe->dai_link->name);
1812
1813 ret = soc_pcm_prepare(be_substream);
1814 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001815 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001816 ret);
1817 break;
1818 }
1819
1820 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1821 }
1822 return ret;
1823}
1824
Mark Brown45c0a182012-05-09 21:46:27 +01001825static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001826{
1827 struct snd_soc_pcm_runtime *fe = substream->private_data;
1828 int stream = substream->stream, ret = 0;
1829
1830 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1831
Liam Girdwood103d84a2012-11-19 14:39:15 +00001832 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001833
1834 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1835
1836 /* there is no point preparing this FE if there are no BEs */
1837 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001838 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001839 fe->dai_link->name);
1840 ret = -EINVAL;
1841 goto out;
1842 }
1843
1844 ret = dpcm_be_dai_prepare(fe, substream->stream);
1845 if (ret < 0)
1846 goto out;
1847
1848 /* call prepare on the frontend */
1849 ret = soc_pcm_prepare(substream);
1850 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001851 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001852 fe->dai_link->name);
1853 goto out;
1854 }
1855
1856 /* run the stream event for each BE */
1857 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1858 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1859
1860out:
1861 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1862 mutex_unlock(&fe->card->mutex);
1863
1864 return ret;
1865}
1866
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001867static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1868 unsigned int cmd, void *arg)
1869{
1870 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1871 struct snd_soc_platform *platform = rtd->platform;
1872
Mark Brownc5914b02013-10-30 17:47:39 -07001873 if (platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001874 return platform->driver->ops->ioctl(substream, cmd, arg);
1875 return snd_pcm_lib_ioctl(substream, cmd, arg);
1876}
1877
Liam Girdwood618dae12012-04-25 12:12:51 +01001878static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1879{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001880 struct snd_pcm_substream *substream =
1881 snd_soc_dpcm_get_substream(fe, stream);
1882 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001883 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01001884
Liam Girdwood103d84a2012-11-19 14:39:15 +00001885 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001886 stream ? "capture" : "playback", fe->dai_link->name);
1887
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001888 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1889 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001890 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001891 fe->dai_link->name);
1892
1893 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1894 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001895 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001896 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001897 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001898 fe->dai_link->name);
1899
1900 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1901 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001902 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001903 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001904
1905 err = dpcm_be_dai_hw_free(fe, stream);
1906 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001907 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001908
1909 err = dpcm_be_dai_shutdown(fe, stream);
1910 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001911 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001912
1913 /* run the stream event for each BE */
1914 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1915
1916 return 0;
1917}
1918
1919static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1920{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001921 struct snd_pcm_substream *substream =
1922 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01001923 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001924 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001925 int ret;
1926
Liam Girdwood103d84a2012-11-19 14:39:15 +00001927 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001928 stream ? "capture" : "playback", fe->dai_link->name);
1929
1930 /* Only start the BE if the FE is ready */
1931 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1932 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1933 return -EINVAL;
1934
1935 /* startup must always be called for new BEs */
1936 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001937 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001938 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01001939
1940 /* keep going if FE state is > open */
1941 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1942 return 0;
1943
1944 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001945 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001946 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01001947
1948 /* keep going if FE state is > hw_params */
1949 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1950 return 0;
1951
1952
1953 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001954 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001955 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01001956
1957 /* run the stream event for each BE */
1958 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1959
1960 /* keep going if FE state is > prepare */
1961 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1962 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1963 return 0;
1964
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001965 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1966 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001967 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001968 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01001969
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001970 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1971 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001972 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001973 goto hw_free;
1974 }
1975 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001976 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001977 fe->dai_link->name);
1978
1979 ret = dpcm_be_dai_trigger(fe, stream,
1980 SNDRV_PCM_TRIGGER_START);
1981 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001982 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001983 goto hw_free;
1984 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001985 }
1986
1987 return 0;
1988
1989hw_free:
1990 dpcm_be_dai_hw_free(fe, stream);
1991close:
1992 dpcm_be_dai_shutdown(fe, stream);
1993disconnect:
1994 /* disconnect any non started BEs */
1995 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1996 struct snd_soc_pcm_runtime *be = dpcm->be;
1997 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1998 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1999 }
2000
2001 return ret;
2002}
2003
2004static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2005{
2006 int ret;
2007
2008 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
2009 ret = dpcm_run_update_startup(fe, stream);
2010 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002011 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01002012 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2013
2014 return ret;
2015}
2016
2017static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2018{
2019 int ret;
2020
2021 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
2022 ret = dpcm_run_update_shutdown(fe, stream);
2023 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002024 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01002025 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2026
2027 return ret;
2028}
2029
2030/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2031 * any DAI links.
2032 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002033int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002034{
Liam Girdwood618dae12012-04-25 12:12:51 +01002035 int i, old, new, paths;
2036
Liam Girdwood618dae12012-04-25 12:12:51 +01002037 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2038 for (i = 0; i < card->num_rtd; i++) {
2039 struct snd_soc_dapm_widget_list *list;
2040 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2041
2042 /* make sure link is FE */
2043 if (!fe->dai_link->dynamic)
2044 continue;
2045
2046 /* only check active links */
2047 if (!fe->cpu_dai->active)
2048 continue;
2049
2050 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002051 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002052 fe->dai_link->name);
2053
2054 /* skip if FE doesn't have playback capability */
2055 if (!fe->cpu_dai->driver->playback.channels_min)
2056 goto capture;
2057
2058 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2059 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002060 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002061 fe->dai_link->name, "playback");
2062 mutex_unlock(&card->mutex);
2063 return paths;
2064 }
2065
2066 /* update any new playback paths */
2067 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2068 if (new) {
2069 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2070 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2071 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2072 }
2073
2074 /* update any old playback paths */
2075 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2076 if (old) {
2077 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2078 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2079 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2080 }
2081
2082capture:
2083 /* skip if FE doesn't have capture capability */
2084 if (!fe->cpu_dai->driver->capture.channels_min)
2085 continue;
2086
2087 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2088 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002089 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002090 fe->dai_link->name, "capture");
2091 mutex_unlock(&card->mutex);
2092 return paths;
2093 }
2094
2095 /* update any new capture paths */
2096 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2097 if (new) {
2098 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2099 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2100 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2101 }
2102
2103 /* update any old capture paths */
2104 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2105 if (old) {
2106 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2107 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2108 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2109 }
2110
2111 dpcm_path_put(&list);
2112 }
2113
2114 mutex_unlock(&card->mutex);
2115 return 0;
2116}
Liam Girdwood01d75842012-04-25 12:12:49 +01002117int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2118{
2119 struct snd_soc_dpcm *dpcm;
2120 struct list_head *clients =
2121 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2122
2123 list_for_each_entry(dpcm, clients, list_be) {
2124
2125 struct snd_soc_pcm_runtime *be = dpcm->be;
2126 struct snd_soc_dai *dai = be->codec_dai;
2127 struct snd_soc_dai_driver *drv = dai->driver;
2128
2129 if (be->dai_link->ignore_suspend)
2130 continue;
2131
Liam Girdwood103d84a2012-11-19 14:39:15 +00002132 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002133
Mark Brownc5914b02013-10-30 17:47:39 -07002134 if (drv->ops && drv->ops->digital_mute && dai->playback_active)
2135 drv->ops->digital_mute(dai, mute);
Liam Girdwood01d75842012-04-25 12:12:49 +01002136 }
2137
2138 return 0;
2139}
2140
Mark Brown45c0a182012-05-09 21:46:27 +01002141static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002142{
2143 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2144 struct snd_soc_dpcm *dpcm;
2145 struct snd_soc_dapm_widget_list *list;
2146 int ret;
2147 int stream = fe_substream->stream;
2148
2149 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2150 fe->dpcm[stream].runtime = fe_substream->runtime;
2151
2152 if (dpcm_path_get(fe, stream, &list) <= 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002153 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002154 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002155 }
2156
2157 /* calculate valid and active FE <-> BE dpcms */
2158 dpcm_process_paths(fe, stream, &list, 1);
2159
2160 ret = dpcm_fe_dai_startup(fe_substream);
2161 if (ret < 0) {
2162 /* clean up all links */
2163 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2164 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2165
2166 dpcm_be_disconnect(fe, stream);
2167 fe->dpcm[stream].runtime = NULL;
2168 }
2169
2170 dpcm_clear_pending_state(fe, stream);
2171 dpcm_path_put(&list);
2172 mutex_unlock(&fe->card->mutex);
2173 return ret;
2174}
2175
Mark Brown45c0a182012-05-09 21:46:27 +01002176static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002177{
2178 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2179 struct snd_soc_dpcm *dpcm;
2180 int stream = fe_substream->stream, ret;
2181
2182 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2183 ret = dpcm_fe_dai_shutdown(fe_substream);
2184
2185 /* mark FE's links ready to prune */
2186 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2187 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2188
2189 dpcm_be_disconnect(fe, stream);
2190
2191 fe->dpcm[stream].runtime = NULL;
2192 mutex_unlock(&fe->card->mutex);
2193 return ret;
2194}
2195
Liam Girdwoodddee6272011-06-09 14:45:53 +01002196/* create a new pcm */
2197int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2198{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002199 struct snd_soc_platform *platform = rtd->platform;
2200 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2201 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2202 struct snd_pcm *pcm;
2203 char new_name[64];
2204 int ret = 0, playback = 0, capture = 0;
2205
Liam Girdwood01d75842012-04-25 12:12:49 +01002206 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002207 playback = rtd->dai_link->dpcm_playback;
2208 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002209 } else {
Mark Brown05679092013-06-01 23:13:53 +01002210 if (codec_dai->driver->playback.channels_min &&
2211 cpu_dai->driver->playback.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002212 playback = 1;
Mark Brown05679092013-06-01 23:13:53 +01002213 if (codec_dai->driver->capture.channels_min &&
2214 cpu_dai->driver->capture.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002215 capture = 1;
2216 }
Sangsu Parka5002312012-01-02 17:15:10 +09002217
Fabio Estevamd6bead02013-08-29 10:32:13 -03002218 if (rtd->dai_link->playback_only) {
2219 playback = 1;
2220 capture = 0;
2221 }
2222
2223 if (rtd->dai_link->capture_only) {
2224 playback = 0;
2225 capture = 1;
2226 }
2227
Liam Girdwood01d75842012-04-25 12:12:49 +01002228 /* create the PCM */
2229 if (rtd->dai_link->no_pcm) {
2230 snprintf(new_name, sizeof(new_name), "(%s)",
2231 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002232
Liam Girdwood01d75842012-04-25 12:12:49 +01002233 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2234 playback, capture, &pcm);
2235 } else {
2236 if (rtd->dai_link->dynamic)
2237 snprintf(new_name, sizeof(new_name), "%s (*)",
2238 rtd->dai_link->stream_name);
2239 else
2240 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2241 rtd->dai_link->stream_name, codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002242
Liam Girdwood01d75842012-04-25 12:12:49 +01002243 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2244 capture, &pcm);
2245 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002246 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002247 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002248 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002249 return ret;
2250 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002251 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002252
2253 /* DAPM dai link stream work */
2254 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2255
2256 rtd->pcm = pcm;
2257 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002258
2259 if (rtd->dai_link->no_pcm) {
2260 if (playback)
2261 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2262 if (capture)
2263 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2264 goto out;
2265 }
2266
2267 /* ASoC PCM operations */
2268 if (rtd->dai_link->dynamic) {
2269 rtd->ops.open = dpcm_fe_dai_open;
2270 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2271 rtd->ops.prepare = dpcm_fe_dai_prepare;
2272 rtd->ops.trigger = dpcm_fe_dai_trigger;
2273 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2274 rtd->ops.close = dpcm_fe_dai_close;
2275 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002276 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002277 } else {
2278 rtd->ops.open = soc_pcm_open;
2279 rtd->ops.hw_params = soc_pcm_hw_params;
2280 rtd->ops.prepare = soc_pcm_prepare;
2281 rtd->ops.trigger = soc_pcm_trigger;
2282 rtd->ops.hw_free = soc_pcm_hw_free;
2283 rtd->ops.close = soc_pcm_close;
2284 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002285 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002286 }
2287
Liam Girdwoodddee6272011-06-09 14:45:53 +01002288 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002289 rtd->ops.ack = platform->driver->ops->ack;
2290 rtd->ops.copy = platform->driver->ops->copy;
2291 rtd->ops.silence = platform->driver->ops->silence;
2292 rtd->ops.page = platform->driver->ops->page;
2293 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002294 }
2295
2296 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002297 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002298
2299 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002300 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002301
2302 if (platform->driver->pcm_new) {
2303 ret = platform->driver->pcm_new(rtd);
2304 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002305 dev_err(platform->dev,
2306 "ASoC: pcm constructor failed: %d\n",
2307 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002308 return ret;
2309 }
2310 }
2311
2312 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002313out:
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03002314 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name,
Liam Girdwoodddee6272011-06-09 14:45:53 +01002315 cpu_dai->name);
2316 return ret;
2317}
Liam Girdwood01d75842012-04-25 12:12:49 +01002318
2319/* is the current PCM operation for this FE ? */
2320int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2321{
2322 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2323 return 1;
2324 return 0;
2325}
2326EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2327
2328/* is the current PCM operation for this BE ? */
2329int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2330 struct snd_soc_pcm_runtime *be, int stream)
2331{
2332 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2333 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2334 be->dpcm[stream].runtime_update))
2335 return 1;
2336 return 0;
2337}
2338EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2339
2340/* get the substream for this BE */
2341struct snd_pcm_substream *
2342 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2343{
2344 return be->pcm->streams[stream].substream;
2345}
2346EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2347
2348/* get the BE runtime state */
2349enum snd_soc_dpcm_state
2350 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2351{
2352 return be->dpcm[stream].state;
2353}
2354EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2355
2356/* set the BE runtime state */
2357void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2358 int stream, enum snd_soc_dpcm_state state)
2359{
2360 be->dpcm[stream].state = state;
2361}
2362EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2363
2364/*
2365 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2366 * are not running, paused or suspended for the specified stream direction.
2367 */
2368int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2369 struct snd_soc_pcm_runtime *be, int stream)
2370{
2371 struct snd_soc_dpcm *dpcm;
2372 int state;
2373
2374 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2375
2376 if (dpcm->fe == fe)
2377 continue;
2378
2379 state = dpcm->fe->dpcm[stream].state;
2380 if (state == SND_SOC_DPCM_STATE_START ||
2381 state == SND_SOC_DPCM_STATE_PAUSED ||
2382 state == SND_SOC_DPCM_STATE_SUSPEND)
2383 return 0;
2384 }
2385
2386 /* it's safe to free/stop this BE DAI */
2387 return 1;
2388}
2389EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2390
2391/*
2392 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2393 * running, paused or suspended for the specified stream direction.
2394 */
2395int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2396 struct snd_soc_pcm_runtime *be, int stream)
2397{
2398 struct snd_soc_dpcm *dpcm;
2399 int state;
2400
2401 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2402
2403 if (dpcm->fe == fe)
2404 continue;
2405
2406 state = dpcm->fe->dpcm[stream].state;
2407 if (state == SND_SOC_DPCM_STATE_START ||
2408 state == SND_SOC_DPCM_STATE_PAUSED ||
2409 state == SND_SOC_DPCM_STATE_SUSPEND ||
2410 state == SND_SOC_DPCM_STATE_PREPARE)
2411 return 0;
2412 }
2413
2414 /* it's safe to change hw_params */
2415 return 1;
2416}
2417EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002418
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002419int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2420 int cmd, struct snd_soc_platform *platform)
2421{
Mark Brownc5914b02013-10-30 17:47:39 -07002422 if (platform->driver->ops && platform->driver->ops->trigger)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002423 return platform->driver->ops->trigger(substream, cmd);
2424 return 0;
2425}
2426EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2427
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002428#ifdef CONFIG_DEBUG_FS
2429static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2430{
2431 switch (state) {
2432 case SND_SOC_DPCM_STATE_NEW:
2433 return "new";
2434 case SND_SOC_DPCM_STATE_OPEN:
2435 return "open";
2436 case SND_SOC_DPCM_STATE_HW_PARAMS:
2437 return "hw_params";
2438 case SND_SOC_DPCM_STATE_PREPARE:
2439 return "prepare";
2440 case SND_SOC_DPCM_STATE_START:
2441 return "start";
2442 case SND_SOC_DPCM_STATE_STOP:
2443 return "stop";
2444 case SND_SOC_DPCM_STATE_SUSPEND:
2445 return "suspend";
2446 case SND_SOC_DPCM_STATE_PAUSED:
2447 return "paused";
2448 case SND_SOC_DPCM_STATE_HW_FREE:
2449 return "hw_free";
2450 case SND_SOC_DPCM_STATE_CLOSE:
2451 return "close";
2452 }
2453
2454 return "unknown";
2455}
2456
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002457static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2458 int stream, char *buf, size_t size)
2459{
2460 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2461 struct snd_soc_dpcm *dpcm;
2462 ssize_t offset = 0;
2463
2464 /* FE state */
2465 offset += snprintf(buf + offset, size - offset,
2466 "[%s - %s]\n", fe->dai_link->name,
2467 stream ? "Capture" : "Playback");
2468
2469 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2470 dpcm_state_string(fe->dpcm[stream].state));
2471
2472 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2473 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2474 offset += snprintf(buf + offset, size - offset,
2475 "Hardware Params: "
2476 "Format = %s, Channels = %d, Rate = %d\n",
2477 snd_pcm_format_name(params_format(params)),
2478 params_channels(params),
2479 params_rate(params));
2480
2481 /* BEs state */
2482 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2483
2484 if (list_empty(&fe->dpcm[stream].be_clients)) {
2485 offset += snprintf(buf + offset, size - offset,
2486 " No active DSP links\n");
2487 goto out;
2488 }
2489
2490 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2491 struct snd_soc_pcm_runtime *be = dpcm->be;
2492 params = &dpcm->hw_params;
2493
2494 offset += snprintf(buf + offset, size - offset,
2495 "- %s\n", be->dai_link->name);
2496
2497 offset += snprintf(buf + offset, size - offset,
2498 " State: %s\n",
2499 dpcm_state_string(be->dpcm[stream].state));
2500
2501 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2502 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2503 offset += snprintf(buf + offset, size - offset,
2504 " Hardware Params: "
2505 "Format = %s, Channels = %d, Rate = %d\n",
2506 snd_pcm_format_name(params_format(params)),
2507 params_channels(params),
2508 params_rate(params));
2509 }
2510
2511out:
2512 return offset;
2513}
2514
2515static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2516 size_t count, loff_t *ppos)
2517{
2518 struct snd_soc_pcm_runtime *fe = file->private_data;
2519 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2520 char *buf;
2521
2522 buf = kmalloc(out_count, GFP_KERNEL);
2523 if (!buf)
2524 return -ENOMEM;
2525
2526 if (fe->cpu_dai->driver->playback.channels_min)
2527 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2528 buf + offset, out_count - offset);
2529
2530 if (fe->cpu_dai->driver->capture.channels_min)
2531 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2532 buf + offset, out_count - offset);
2533
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002534 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002535
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002536 kfree(buf);
2537 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002538}
2539
2540static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002541 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002542 .read = dpcm_state_read_file,
2543 .llseek = default_llseek,
2544};
2545
2546int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2547{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002548 if (!rtd->dai_link)
2549 return 0;
2550
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002551 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2552 rtd->card->debugfs_card_root);
2553 if (!rtd->debugfs_dpcm_root) {
2554 dev_dbg(rtd->dev,
2555 "ASoC: Failed to create dpcm debugfs directory %s\n",
2556 rtd->dai_link->name);
2557 return -EINVAL;
2558 }
2559
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002560 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002561 rtd->debugfs_dpcm_root,
2562 rtd, &dpcm_state_fops);
2563
2564 return 0;
2565}
2566#endif