blob: 88230ea330d8a4117b2bc9afe275972363711eb6 [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 }
822 return 0;
823}
824
Mark Brown45c0a182012-05-09 21:46:27 +0100825static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
826 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100827{
828 struct snd_soc_pcm_runtime *rtd = substream->private_data;
829 struct snd_soc_platform *platform = rtd->platform;
830 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
831 struct snd_soc_dai *codec_dai = rtd->codec_dai;
832 int ret;
833
Mark Brownc5914b02013-10-30 17:47:39 -0700834 if (codec_dai->driver->ops &&
835 codec_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100836 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
837 if (ret < 0)
838 return ret;
839 }
840
Jean-Francois Moinedcf0fa22014-01-03 09:19:18 +0100841 if (platform->driver->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100842 ret = platform->driver->bespoke_trigger(substream, cmd);
843 if (ret < 0)
844 return ret;
845 }
846
Mark Brownc5914b02013-10-30 17:47:39 -0700847 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100848 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
849 if (ret < 0)
850 return ret;
851 }
852 return 0;
853}
Liam Girdwoodddee6272011-06-09 14:45:53 +0100854/*
855 * soc level wrapper for pointer callback
856 * If cpu_dai, codec_dai, platform driver has the delay callback, than
857 * the runtime->delay will be updated accordingly.
858 */
859static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
860{
861 struct snd_soc_pcm_runtime *rtd = substream->private_data;
862 struct snd_soc_platform *platform = rtd->platform;
863 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
864 struct snd_soc_dai *codec_dai = rtd->codec_dai;
865 struct snd_pcm_runtime *runtime = substream->runtime;
866 snd_pcm_uframes_t offset = 0;
867 snd_pcm_sframes_t delay = 0;
868
869 if (platform->driver->ops && platform->driver->ops->pointer)
870 offset = platform->driver->ops->pointer(substream);
871
Mark Brownc5914b02013-10-30 17:47:39 -0700872 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100873 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
874
Mark Brownc5914b02013-10-30 17:47:39 -0700875 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100876 delay += codec_dai->driver->ops->delay(substream, codec_dai);
877
878 if (platform->driver->delay)
879 delay += platform->driver->delay(substream, codec_dai);
880
881 runtime->delay = delay;
882
883 return offset;
884}
885
Liam Girdwood01d75842012-04-25 12:12:49 +0100886/* connect a FE and BE */
887static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
888 struct snd_soc_pcm_runtime *be, int stream)
889{
890 struct snd_soc_dpcm *dpcm;
891
892 /* only add new dpcms */
893 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
894 if (dpcm->be == be && dpcm->fe == fe)
895 return 0;
896 }
897
898 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
899 if (!dpcm)
900 return -ENOMEM;
901
902 dpcm->be = be;
903 dpcm->fe = fe;
904 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
905 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
906 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
907 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
908
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300909 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100910 stream ? "capture" : "playback", fe->dai_link->name,
911 stream ? "<-" : "->", be->dai_link->name);
912
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100913#ifdef CONFIG_DEBUG_FS
914 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
915 fe->debugfs_dpcm_root, &dpcm->state);
916#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100917 return 1;
918}
919
920/* reparent a BE onto another FE */
921static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
922 struct snd_soc_pcm_runtime *be, int stream)
923{
924 struct snd_soc_dpcm *dpcm;
925 struct snd_pcm_substream *fe_substream, *be_substream;
926
927 /* reparent if BE is connected to other FEs */
928 if (!be->dpcm[stream].users)
929 return;
930
931 be_substream = snd_soc_dpcm_get_substream(be, stream);
932
933 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
934 if (dpcm->fe == fe)
935 continue;
936
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300937 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100938 stream ? "capture" : "playback",
939 dpcm->fe->dai_link->name,
940 stream ? "<-" : "->", dpcm->be->dai_link->name);
941
942 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
943 be_substream->runtime = fe_substream->runtime;
944 break;
945 }
946}
947
948/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +0000949void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +0100950{
951 struct snd_soc_dpcm *dpcm, *d;
952
953 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000954 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100955 stream ? "capture" : "playback",
956 dpcm->be->dai_link->name);
957
958 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
959 continue;
960
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300961 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100962 stream ? "capture" : "playback", fe->dai_link->name,
963 stream ? "<-" : "->", dpcm->be->dai_link->name);
964
965 /* BEs still alive need new FE */
966 dpcm_be_reparent(fe, dpcm->be, stream);
967
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100968#ifdef CONFIG_DEBUG_FS
969 debugfs_remove(dpcm->debugfs_state);
970#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100971 list_del(&dpcm->list_be);
972 list_del(&dpcm->list_fe);
973 kfree(dpcm);
974 }
975}
976
977/* get BE for DAI widget and stream */
978static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
979 struct snd_soc_dapm_widget *widget, int stream)
980{
981 struct snd_soc_pcm_runtime *be;
982 int i;
983
984 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
985 for (i = 0; i < card->num_links; i++) {
986 be = &card->rtd[i];
987
Liam Girdwood35ea0652012-06-05 19:26:59 +0100988 if (!be->dai_link->no_pcm)
989 continue;
990
Liam Girdwood01d75842012-04-25 12:12:49 +0100991 if (be->cpu_dai->playback_widget == widget ||
992 be->codec_dai->playback_widget == widget)
993 return be;
994 }
995 } else {
996
997 for (i = 0; i < card->num_links; i++) {
998 be = &card->rtd[i];
999
Liam Girdwood35ea0652012-06-05 19:26:59 +01001000 if (!be->dai_link->no_pcm)
1001 continue;
1002
Liam Girdwood01d75842012-04-25 12:12:49 +01001003 if (be->cpu_dai->capture_widget == widget ||
1004 be->codec_dai->capture_widget == widget)
1005 return be;
1006 }
1007 }
1008
Liam Girdwood103d84a2012-11-19 14:39:15 +00001009 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001010 stream ? "capture" : "playback", widget->name);
1011 return NULL;
1012}
1013
1014static inline struct snd_soc_dapm_widget *
Benoit Cousson37018612014-04-24 14:01:45 +02001015 dai_get_widget(struct snd_soc_dai *dai, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001016{
1017 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Benoit Cousson37018612014-04-24 14:01:45 +02001018 return dai->playback_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001019 else
Benoit Cousson37018612014-04-24 14:01:45 +02001020 return dai->capture_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001021}
1022
1023static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1024 struct snd_soc_dapm_widget *widget)
1025{
1026 int i;
1027
1028 for (i = 0; i < list->num_widgets; i++) {
1029 if (widget == list->widgets[i])
1030 return 1;
1031 }
1032
1033 return 0;
1034}
1035
Liam Girdwood23607022014-01-17 17:03:55 +00001036int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001037 int stream, struct snd_soc_dapm_widget_list **list_)
1038{
1039 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1040 struct snd_soc_dapm_widget_list *list;
1041 int paths;
1042
1043 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
1044 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
1045 if (list == NULL)
1046 return -ENOMEM;
1047
1048 /* get number of valid DAI paths and their widgets */
1049 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
1050
Liam Girdwood103d84a2012-11-19 14:39:15 +00001051 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001052 stream ? "capture" : "playback");
1053
1054 *list_ = list;
1055 return paths;
1056}
1057
Liam Girdwood01d75842012-04-25 12:12:49 +01001058static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1059 struct snd_soc_dapm_widget_list **list_)
1060{
1061 struct snd_soc_dpcm *dpcm;
1062 struct snd_soc_dapm_widget_list *list = *list_;
1063 struct snd_soc_dapm_widget *widget;
1064 int prune = 0;
1065
1066 /* Destroy any old FE <--> BE connections */
1067 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1068
1069 /* is there a valid CPU DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001070 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001071
1072 /* prune the BE if it's no longer in our active list */
1073 if (widget && widget_in_list(list, widget))
1074 continue;
1075
1076 /* is there a valid CODEC DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001077 widget = dai_get_widget(dpcm->be->codec_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001078
1079 /* prune the BE if it's no longer in our active list */
1080 if (widget && widget_in_list(list, widget))
1081 continue;
1082
Liam Girdwood103d84a2012-11-19 14:39:15 +00001083 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001084 stream ? "capture" : "playback",
1085 dpcm->be->dai_link->name, fe->dai_link->name);
1086 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1087 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1088 prune++;
1089 }
1090
Liam Girdwood103d84a2012-11-19 14:39:15 +00001091 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001092 return prune;
1093}
1094
1095static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1096 struct snd_soc_dapm_widget_list **list_)
1097{
1098 struct snd_soc_card *card = fe->card;
1099 struct snd_soc_dapm_widget_list *list = *list_;
1100 struct snd_soc_pcm_runtime *be;
1101 int i, new = 0, err;
1102
1103 /* Create any new FE <--> BE connections */
1104 for (i = 0; i < list->num_widgets; i++) {
1105
Mark Brown46162742013-06-05 19:36:11 +01001106 switch (list->widgets[i]->id) {
1107 case snd_soc_dapm_dai_in:
1108 case snd_soc_dapm_dai_out:
1109 break;
1110 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001111 continue;
Mark Brown46162742013-06-05 19:36:11 +01001112 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001113
1114 /* is there a valid BE rtd for this widget */
1115 be = dpcm_get_be(card, list->widgets[i], stream);
1116 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001117 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001118 list->widgets[i]->name);
1119 continue;
1120 }
1121
1122 /* make sure BE is a real BE */
1123 if (!be->dai_link->no_pcm)
1124 continue;
1125
1126 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001127 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001128 continue;
1129
1130 /* newly connected FE and BE */
1131 err = dpcm_be_connect(fe, be, stream);
1132 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001133 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001134 list->widgets[i]->name);
1135 break;
1136 } else if (err == 0) /* already connected */
1137 continue;
1138
1139 /* new */
1140 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1141 new++;
1142 }
1143
Liam Girdwood103d84a2012-11-19 14:39:15 +00001144 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001145 return new;
1146}
1147
1148/*
1149 * Find the corresponding BE DAIs that source or sink audio to this
1150 * FE substream.
1151 */
Liam Girdwood23607022014-01-17 17:03:55 +00001152int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001153 int stream, struct snd_soc_dapm_widget_list **list, int new)
1154{
1155 if (new)
1156 return dpcm_add_paths(fe, stream, list);
1157 else
1158 return dpcm_prune_paths(fe, stream, list);
1159}
1160
Liam Girdwood23607022014-01-17 17:03:55 +00001161void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001162{
1163 struct snd_soc_dpcm *dpcm;
1164
1165 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1166 dpcm->be->dpcm[stream].runtime_update =
1167 SND_SOC_DPCM_UPDATE_NO;
1168}
1169
1170static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1171 int stream)
1172{
1173 struct snd_soc_dpcm *dpcm;
1174
1175 /* disable any enabled and non active backends */
1176 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1177
1178 struct snd_soc_pcm_runtime *be = dpcm->be;
1179 struct snd_pcm_substream *be_substream =
1180 snd_soc_dpcm_get_substream(be, stream);
1181
1182 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001183 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001184 stream ? "capture" : "playback",
1185 be->dpcm[stream].state);
1186
1187 if (--be->dpcm[stream].users != 0)
1188 continue;
1189
1190 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1191 continue;
1192
1193 soc_pcm_close(be_substream);
1194 be_substream->runtime = NULL;
1195 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1196 }
1197}
1198
Liam Girdwood23607022014-01-17 17:03:55 +00001199int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001200{
1201 struct snd_soc_dpcm *dpcm;
1202 int err, count = 0;
1203
1204 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1205 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1206
1207 struct snd_soc_pcm_runtime *be = dpcm->be;
1208 struct snd_pcm_substream *be_substream =
1209 snd_soc_dpcm_get_substream(be, stream);
1210
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001211 if (!be_substream) {
1212 dev_err(be->dev, "ASoC: no backend %s stream\n",
1213 stream ? "capture" : "playback");
1214 continue;
1215 }
1216
Liam Girdwood01d75842012-04-25 12:12:49 +01001217 /* is this op for this BE ? */
1218 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1219 continue;
1220
1221 /* first time the dpcm is open ? */
1222 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001223 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001224 stream ? "capture" : "playback",
1225 be->dpcm[stream].state);
1226
1227 if (be->dpcm[stream].users++ != 0)
1228 continue;
1229
1230 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1231 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1232 continue;
1233
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001234 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1235 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001236
1237 be_substream->runtime = be->dpcm[stream].runtime;
1238 err = soc_pcm_open(be_substream);
1239 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001240 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001241 be->dpcm[stream].users--;
1242 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001243 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001244 stream ? "capture" : "playback",
1245 be->dpcm[stream].state);
1246
1247 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1248 goto unwind;
1249 }
1250
1251 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1252 count++;
1253 }
1254
1255 return count;
1256
1257unwind:
1258 /* disable any enabled and non active backends */
1259 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1260 struct snd_soc_pcm_runtime *be = dpcm->be;
1261 struct snd_pcm_substream *be_substream =
1262 snd_soc_dpcm_get_substream(be, stream);
1263
1264 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1265 continue;
1266
1267 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001268 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001269 stream ? "capture" : "playback",
1270 be->dpcm[stream].state);
1271
1272 if (--be->dpcm[stream].users != 0)
1273 continue;
1274
1275 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1276 continue;
1277
1278 soc_pcm_close(be_substream);
1279 be_substream->runtime = NULL;
1280 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1281 }
1282
1283 return err;
1284}
1285
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001286static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1287 struct snd_soc_pcm_stream *stream)
1288{
1289 runtime->hw.rate_min = stream->rate_min;
1290 runtime->hw.rate_max = stream->rate_max;
1291 runtime->hw.channels_min = stream->channels_min;
1292 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001293 if (runtime->hw.formats)
1294 runtime->hw.formats &= stream->formats;
1295 else
1296 runtime->hw.formats = stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001297 runtime->hw.rates = stream->rates;
1298}
1299
Mark Brown45c0a182012-05-09 21:46:27 +01001300static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001301{
1302 struct snd_pcm_runtime *runtime = substream->runtime;
1303 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1304 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1305 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1306
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001307 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1308 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1309 else
1310 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
Liam Girdwood01d75842012-04-25 12:12:49 +01001311}
1312
1313static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1314{
1315 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1316 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1317 int stream = fe_substream->stream, ret = 0;
1318
1319 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1320
1321 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1322 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001323 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001324 goto be_err;
1325 }
1326
Liam Girdwood103d84a2012-11-19 14:39:15 +00001327 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001328
1329 /* start the DAI frontend */
1330 ret = soc_pcm_open(fe_substream);
1331 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001332 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001333 goto unwind;
1334 }
1335
1336 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1337
1338 dpcm_set_fe_runtime(fe_substream);
1339 snd_pcm_limit_hw_rates(runtime);
1340
1341 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1342 return 0;
1343
1344unwind:
1345 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1346be_err:
1347 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1348 return ret;
1349}
1350
Liam Girdwood23607022014-01-17 17:03:55 +00001351int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001352{
1353 struct snd_soc_dpcm *dpcm;
1354
1355 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1356 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1357
1358 struct snd_soc_pcm_runtime *be = dpcm->be;
1359 struct snd_pcm_substream *be_substream =
1360 snd_soc_dpcm_get_substream(be, stream);
1361
1362 /* is this op for this BE ? */
1363 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1364 continue;
1365
1366 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001367 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001368 stream ? "capture" : "playback",
1369 be->dpcm[stream].state);
1370
1371 if (--be->dpcm[stream].users != 0)
1372 continue;
1373
1374 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1375 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1376 continue;
1377
Liam Girdwood103d84a2012-11-19 14:39:15 +00001378 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001379 dpcm->fe->dai_link->name);
1380
1381 soc_pcm_close(be_substream);
1382 be_substream->runtime = NULL;
1383
1384 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1385 }
1386 return 0;
1387}
1388
1389static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1390{
1391 struct snd_soc_pcm_runtime *fe = substream->private_data;
1392 int stream = substream->stream;
1393
1394 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1395
1396 /* shutdown the BEs */
1397 dpcm_be_dai_shutdown(fe, substream->stream);
1398
Liam Girdwood103d84a2012-11-19 14:39:15 +00001399 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001400
1401 /* now shutdown the frontend */
1402 soc_pcm_close(substream);
1403
1404 /* run the stream event for each BE */
1405 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1406
1407 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1408 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1409 return 0;
1410}
1411
Liam Girdwood23607022014-01-17 17:03:55 +00001412int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001413{
1414 struct snd_soc_dpcm *dpcm;
1415
1416 /* only hw_params backends that are either sinks or sources
1417 * to this frontend DAI */
1418 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1419
1420 struct snd_soc_pcm_runtime *be = dpcm->be;
1421 struct snd_pcm_substream *be_substream =
1422 snd_soc_dpcm_get_substream(be, stream);
1423
1424 /* is this op for this BE ? */
1425 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1426 continue;
1427
1428 /* only free hw when no longer used - check all FEs */
1429 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1430 continue;
1431
1432 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1433 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1434 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001435 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001436 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1437 continue;
1438
Liam Girdwood103d84a2012-11-19 14:39:15 +00001439 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001440 dpcm->fe->dai_link->name);
1441
1442 soc_pcm_hw_free(be_substream);
1443
1444 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1445 }
1446
1447 return 0;
1448}
1449
Mark Brown45c0a182012-05-09 21:46:27 +01001450static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001451{
1452 struct snd_soc_pcm_runtime *fe = substream->private_data;
1453 int err, stream = substream->stream;
1454
1455 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1456 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1457
Liam Girdwood103d84a2012-11-19 14:39:15 +00001458 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001459
1460 /* call hw_free on the frontend */
1461 err = soc_pcm_hw_free(substream);
1462 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001463 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001464 fe->dai_link->name);
1465
1466 /* only hw_params backends that are either sinks or sources
1467 * to this frontend DAI */
1468 err = dpcm_be_dai_hw_free(fe, stream);
1469
1470 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1471 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1472
1473 mutex_unlock(&fe->card->mutex);
1474 return 0;
1475}
1476
Liam Girdwood23607022014-01-17 17:03:55 +00001477int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001478{
1479 struct snd_soc_dpcm *dpcm;
1480 int ret;
1481
1482 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1483
1484 struct snd_soc_pcm_runtime *be = dpcm->be;
1485 struct snd_pcm_substream *be_substream =
1486 snd_soc_dpcm_get_substream(be, stream);
1487
1488 /* is this op for this BE ? */
1489 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1490 continue;
1491
1492 /* only allow hw_params() if no connected FEs are running */
1493 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1494 continue;
1495
1496 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1497 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1498 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1499 continue;
1500
Liam Girdwood103d84a2012-11-19 14:39:15 +00001501 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001502 dpcm->fe->dai_link->name);
1503
1504 /* copy params for each dpcm */
1505 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1506 sizeof(struct snd_pcm_hw_params));
1507
1508 /* perform any hw_params fixups */
1509 if (be->dai_link->be_hw_params_fixup) {
1510 ret = be->dai_link->be_hw_params_fixup(be,
1511 &dpcm->hw_params);
1512 if (ret < 0) {
1513 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001514 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001515 ret);
1516 goto unwind;
1517 }
1518 }
1519
1520 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1521 if (ret < 0) {
1522 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001523 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001524 goto unwind;
1525 }
1526
1527 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1528 }
1529 return 0;
1530
1531unwind:
1532 /* disable any enabled and non active backends */
1533 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1534 struct snd_soc_pcm_runtime *be = dpcm->be;
1535 struct snd_pcm_substream *be_substream =
1536 snd_soc_dpcm_get_substream(be, stream);
1537
1538 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1539 continue;
1540
1541 /* only allow hw_free() if no connected FEs are running */
1542 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1543 continue;
1544
1545 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1546 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1547 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1548 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1549 continue;
1550
1551 soc_pcm_hw_free(be_substream);
1552 }
1553
1554 return ret;
1555}
1556
Mark Brown45c0a182012-05-09 21:46:27 +01001557static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1558 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001559{
1560 struct snd_soc_pcm_runtime *fe = substream->private_data;
1561 int ret, stream = substream->stream;
1562
1563 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1564 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1565
1566 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1567 sizeof(struct snd_pcm_hw_params));
1568 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1569 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001570 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001571 goto out;
1572 }
1573
Liam Girdwood103d84a2012-11-19 14:39:15 +00001574 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001575 fe->dai_link->name, params_rate(params),
1576 params_channels(params), params_format(params));
1577
1578 /* call hw_params on the frontend */
1579 ret = soc_pcm_hw_params(substream, params);
1580 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001581 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001582 dpcm_be_dai_hw_free(fe, stream);
1583 } else
1584 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1585
1586out:
1587 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1588 mutex_unlock(&fe->card->mutex);
1589 return ret;
1590}
1591
1592static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1593 struct snd_pcm_substream *substream, int cmd)
1594{
1595 int ret;
1596
Liam Girdwood103d84a2012-11-19 14:39:15 +00001597 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001598 dpcm->fe->dai_link->name, cmd);
1599
1600 ret = soc_pcm_trigger(substream, cmd);
1601 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001602 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001603
1604 return ret;
1605}
1606
Liam Girdwood23607022014-01-17 17:03:55 +00001607int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01001608 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001609{
1610 struct snd_soc_dpcm *dpcm;
1611 int ret = 0;
1612
1613 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1614
1615 struct snd_soc_pcm_runtime *be = dpcm->be;
1616 struct snd_pcm_substream *be_substream =
1617 snd_soc_dpcm_get_substream(be, stream);
1618
1619 /* is this op for this BE ? */
1620 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1621 continue;
1622
1623 switch (cmd) {
1624 case SNDRV_PCM_TRIGGER_START:
1625 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1626 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1627 continue;
1628
1629 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1630 if (ret)
1631 return ret;
1632
1633 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1634 break;
1635 case SNDRV_PCM_TRIGGER_RESUME:
1636 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1637 continue;
1638
1639 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1640 if (ret)
1641 return ret;
1642
1643 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1644 break;
1645 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1646 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1647 continue;
1648
1649 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1650 if (ret)
1651 return ret;
1652
1653 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1654 break;
1655 case SNDRV_PCM_TRIGGER_STOP:
1656 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1657 continue;
1658
1659 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1660 continue;
1661
1662 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1663 if (ret)
1664 return ret;
1665
1666 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1667 break;
1668 case SNDRV_PCM_TRIGGER_SUSPEND:
1669 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1670 continue;
1671
1672 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1673 continue;
1674
1675 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1676 if (ret)
1677 return ret;
1678
1679 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1680 break;
1681 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1682 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1683 continue;
1684
1685 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1686 continue;
1687
1688 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1689 if (ret)
1690 return ret;
1691
1692 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1693 break;
1694 }
1695 }
1696
1697 return ret;
1698}
1699EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1700
Mark Brown45c0a182012-05-09 21:46:27 +01001701static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001702{
1703 struct snd_soc_pcm_runtime *fe = substream->private_data;
1704 int stream = substream->stream, ret;
1705 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1706
1707 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1708
1709 switch (trigger) {
1710 case SND_SOC_DPCM_TRIGGER_PRE:
1711 /* call trigger on the frontend before the backend. */
1712
Liam Girdwood103d84a2012-11-19 14:39:15 +00001713 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001714 fe->dai_link->name, cmd);
1715
1716 ret = soc_pcm_trigger(substream, cmd);
1717 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001718 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001719 goto out;
1720 }
1721
1722 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1723 break;
1724 case SND_SOC_DPCM_TRIGGER_POST:
1725 /* call trigger on the frontend after the backend. */
1726
1727 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1728 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001729 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001730 goto out;
1731 }
1732
Liam Girdwood103d84a2012-11-19 14:39:15 +00001733 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001734 fe->dai_link->name, cmd);
1735
1736 ret = soc_pcm_trigger(substream, cmd);
1737 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001738 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1739 /* bespoke trigger() - handles both FE and BEs */
1740
Liam Girdwood103d84a2012-11-19 14:39:15 +00001741 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001742 fe->dai_link->name, cmd);
1743
1744 ret = soc_pcm_bespoke_trigger(substream, cmd);
1745 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001746 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001747 goto out;
1748 }
1749 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01001750 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00001751 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01001752 fe->dai_link->name);
1753 ret = -EINVAL;
1754 goto out;
1755 }
1756
1757 switch (cmd) {
1758 case SNDRV_PCM_TRIGGER_START:
1759 case SNDRV_PCM_TRIGGER_RESUME:
1760 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1761 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1762 break;
1763 case SNDRV_PCM_TRIGGER_STOP:
1764 case SNDRV_PCM_TRIGGER_SUSPEND:
1765 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1766 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1767 break;
1768 }
1769
1770out:
1771 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1772 return ret;
1773}
1774
Liam Girdwood23607022014-01-17 17:03:55 +00001775int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001776{
1777 struct snd_soc_dpcm *dpcm;
1778 int ret = 0;
1779
1780 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1781
1782 struct snd_soc_pcm_runtime *be = dpcm->be;
1783 struct snd_pcm_substream *be_substream =
1784 snd_soc_dpcm_get_substream(be, stream);
1785
1786 /* is this op for this BE ? */
1787 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1788 continue;
1789
1790 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1791 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1792 continue;
1793
Liam Girdwood103d84a2012-11-19 14:39:15 +00001794 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001795 dpcm->fe->dai_link->name);
1796
1797 ret = soc_pcm_prepare(be_substream);
1798 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001799 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001800 ret);
1801 break;
1802 }
1803
1804 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1805 }
1806 return ret;
1807}
1808
Mark Brown45c0a182012-05-09 21:46:27 +01001809static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001810{
1811 struct snd_soc_pcm_runtime *fe = substream->private_data;
1812 int stream = substream->stream, ret = 0;
1813
1814 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1815
Liam Girdwood103d84a2012-11-19 14:39:15 +00001816 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001817
1818 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1819
1820 /* there is no point preparing this FE if there are no BEs */
1821 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001822 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001823 fe->dai_link->name);
1824 ret = -EINVAL;
1825 goto out;
1826 }
1827
1828 ret = dpcm_be_dai_prepare(fe, substream->stream);
1829 if (ret < 0)
1830 goto out;
1831
1832 /* call prepare on the frontend */
1833 ret = soc_pcm_prepare(substream);
1834 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001835 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001836 fe->dai_link->name);
1837 goto out;
1838 }
1839
1840 /* run the stream event for each BE */
1841 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1842 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1843
1844out:
1845 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1846 mutex_unlock(&fe->card->mutex);
1847
1848 return ret;
1849}
1850
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001851static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1852 unsigned int cmd, void *arg)
1853{
1854 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1855 struct snd_soc_platform *platform = rtd->platform;
1856
Mark Brownc5914b02013-10-30 17:47:39 -07001857 if (platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001858 return platform->driver->ops->ioctl(substream, cmd, arg);
1859 return snd_pcm_lib_ioctl(substream, cmd, arg);
1860}
1861
Liam Girdwood618dae12012-04-25 12:12:51 +01001862static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1863{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001864 struct snd_pcm_substream *substream =
1865 snd_soc_dpcm_get_substream(fe, stream);
1866 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001867 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01001868
Liam Girdwood103d84a2012-11-19 14:39:15 +00001869 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001870 stream ? "capture" : "playback", fe->dai_link->name);
1871
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001872 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1873 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001874 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001875 fe->dai_link->name);
1876
1877 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1878 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001879 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001880 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001881 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001882 fe->dai_link->name);
1883
1884 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1885 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001886 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001887 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001888
1889 err = dpcm_be_dai_hw_free(fe, stream);
1890 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001891 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001892
1893 err = dpcm_be_dai_shutdown(fe, stream);
1894 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001895 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001896
1897 /* run the stream event for each BE */
1898 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1899
1900 return 0;
1901}
1902
1903static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1904{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001905 struct snd_pcm_substream *substream =
1906 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01001907 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001908 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001909 int ret;
1910
Liam Girdwood103d84a2012-11-19 14:39:15 +00001911 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001912 stream ? "capture" : "playback", fe->dai_link->name);
1913
1914 /* Only start the BE if the FE is ready */
1915 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1916 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1917 return -EINVAL;
1918
1919 /* startup must always be called for new BEs */
1920 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001921 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001922 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01001923
1924 /* keep going if FE state is > open */
1925 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1926 return 0;
1927
1928 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001929 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001930 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01001931
1932 /* keep going if FE state is > hw_params */
1933 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1934 return 0;
1935
1936
1937 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001938 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001939 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01001940
1941 /* run the stream event for each BE */
1942 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1943
1944 /* keep going if FE state is > prepare */
1945 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1946 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1947 return 0;
1948
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001949 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1950 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001951 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001952 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01001953
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001954 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1955 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001956 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001957 goto hw_free;
1958 }
1959 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001960 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001961 fe->dai_link->name);
1962
1963 ret = dpcm_be_dai_trigger(fe, stream,
1964 SNDRV_PCM_TRIGGER_START);
1965 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001966 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001967 goto hw_free;
1968 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001969 }
1970
1971 return 0;
1972
1973hw_free:
1974 dpcm_be_dai_hw_free(fe, stream);
1975close:
1976 dpcm_be_dai_shutdown(fe, stream);
1977disconnect:
1978 /* disconnect any non started BEs */
1979 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1980 struct snd_soc_pcm_runtime *be = dpcm->be;
1981 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1982 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1983 }
1984
1985 return ret;
1986}
1987
1988static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1989{
1990 int ret;
1991
1992 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1993 ret = dpcm_run_update_startup(fe, stream);
1994 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001995 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001996 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1997
1998 return ret;
1999}
2000
2001static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2002{
2003 int ret;
2004
2005 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
2006 ret = dpcm_run_update_shutdown(fe, stream);
2007 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002008 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01002009 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2010
2011 return ret;
2012}
2013
2014/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2015 * any DAI links.
2016 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002017int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002018{
Liam Girdwood618dae12012-04-25 12:12:51 +01002019 int i, old, new, paths;
2020
Liam Girdwood618dae12012-04-25 12:12:51 +01002021 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2022 for (i = 0; i < card->num_rtd; i++) {
2023 struct snd_soc_dapm_widget_list *list;
2024 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2025
2026 /* make sure link is FE */
2027 if (!fe->dai_link->dynamic)
2028 continue;
2029
2030 /* only check active links */
2031 if (!fe->cpu_dai->active)
2032 continue;
2033
2034 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002035 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002036 fe->dai_link->name);
2037
2038 /* skip if FE doesn't have playback capability */
2039 if (!fe->cpu_dai->driver->playback.channels_min)
2040 goto capture;
2041
2042 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2043 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002044 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002045 fe->dai_link->name, "playback");
2046 mutex_unlock(&card->mutex);
2047 return paths;
2048 }
2049
2050 /* update any new playback paths */
2051 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2052 if (new) {
2053 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2054 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2055 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2056 }
2057
2058 /* update any old playback paths */
2059 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2060 if (old) {
2061 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2062 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2063 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2064 }
2065
2066capture:
2067 /* skip if FE doesn't have capture capability */
2068 if (!fe->cpu_dai->driver->capture.channels_min)
2069 continue;
2070
2071 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2072 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002073 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002074 fe->dai_link->name, "capture");
2075 mutex_unlock(&card->mutex);
2076 return paths;
2077 }
2078
2079 /* update any new capture paths */
2080 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2081 if (new) {
2082 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2083 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2084 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2085 }
2086
2087 /* update any old capture paths */
2088 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2089 if (old) {
2090 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2091 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2092 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2093 }
2094
2095 dpcm_path_put(&list);
2096 }
2097
2098 mutex_unlock(&card->mutex);
2099 return 0;
2100}
Liam Girdwood01d75842012-04-25 12:12:49 +01002101int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2102{
2103 struct snd_soc_dpcm *dpcm;
2104 struct list_head *clients =
2105 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2106
2107 list_for_each_entry(dpcm, clients, list_be) {
2108
2109 struct snd_soc_pcm_runtime *be = dpcm->be;
2110 struct snd_soc_dai *dai = be->codec_dai;
2111 struct snd_soc_dai_driver *drv = dai->driver;
2112
2113 if (be->dai_link->ignore_suspend)
2114 continue;
2115
Liam Girdwood103d84a2012-11-19 14:39:15 +00002116 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002117
Mark Brownc5914b02013-10-30 17:47:39 -07002118 if (drv->ops && drv->ops->digital_mute && dai->playback_active)
2119 drv->ops->digital_mute(dai, mute);
Liam Girdwood01d75842012-04-25 12:12:49 +01002120 }
2121
2122 return 0;
2123}
2124
Mark Brown45c0a182012-05-09 21:46:27 +01002125static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002126{
2127 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2128 struct snd_soc_dpcm *dpcm;
2129 struct snd_soc_dapm_widget_list *list;
2130 int ret;
2131 int stream = fe_substream->stream;
2132
2133 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2134 fe->dpcm[stream].runtime = fe_substream->runtime;
2135
2136 if (dpcm_path_get(fe, stream, &list) <= 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002137 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002138 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002139 }
2140
2141 /* calculate valid and active FE <-> BE dpcms */
2142 dpcm_process_paths(fe, stream, &list, 1);
2143
2144 ret = dpcm_fe_dai_startup(fe_substream);
2145 if (ret < 0) {
2146 /* clean up all links */
2147 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2148 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2149
2150 dpcm_be_disconnect(fe, stream);
2151 fe->dpcm[stream].runtime = NULL;
2152 }
2153
2154 dpcm_clear_pending_state(fe, stream);
2155 dpcm_path_put(&list);
2156 mutex_unlock(&fe->card->mutex);
2157 return ret;
2158}
2159
Mark Brown45c0a182012-05-09 21:46:27 +01002160static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002161{
2162 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2163 struct snd_soc_dpcm *dpcm;
2164 int stream = fe_substream->stream, ret;
2165
2166 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2167 ret = dpcm_fe_dai_shutdown(fe_substream);
2168
2169 /* mark FE's links ready to prune */
2170 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2171 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2172
2173 dpcm_be_disconnect(fe, stream);
2174
2175 fe->dpcm[stream].runtime = NULL;
2176 mutex_unlock(&fe->card->mutex);
2177 return ret;
2178}
2179
Liam Girdwoodddee6272011-06-09 14:45:53 +01002180/* create a new pcm */
2181int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2182{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002183 struct snd_soc_platform *platform = rtd->platform;
2184 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2185 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2186 struct snd_pcm *pcm;
2187 char new_name[64];
2188 int ret = 0, playback = 0, capture = 0;
2189
Liam Girdwood01d75842012-04-25 12:12:49 +01002190 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002191 playback = rtd->dai_link->dpcm_playback;
2192 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002193 } else {
Mark Brown05679092013-06-01 23:13:53 +01002194 if (codec_dai->driver->playback.channels_min &&
2195 cpu_dai->driver->playback.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002196 playback = 1;
Mark Brown05679092013-06-01 23:13:53 +01002197 if (codec_dai->driver->capture.channels_min &&
2198 cpu_dai->driver->capture.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002199 capture = 1;
2200 }
Sangsu Parka5002312012-01-02 17:15:10 +09002201
Fabio Estevamd6bead02013-08-29 10:32:13 -03002202 if (rtd->dai_link->playback_only) {
2203 playback = 1;
2204 capture = 0;
2205 }
2206
2207 if (rtd->dai_link->capture_only) {
2208 playback = 0;
2209 capture = 1;
2210 }
2211
Liam Girdwood01d75842012-04-25 12:12:49 +01002212 /* create the PCM */
2213 if (rtd->dai_link->no_pcm) {
2214 snprintf(new_name, sizeof(new_name), "(%s)",
2215 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002216
Liam Girdwood01d75842012-04-25 12:12:49 +01002217 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2218 playback, capture, &pcm);
2219 } else {
2220 if (rtd->dai_link->dynamic)
2221 snprintf(new_name, sizeof(new_name), "%s (*)",
2222 rtd->dai_link->stream_name);
2223 else
2224 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2225 rtd->dai_link->stream_name, codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002226
Liam Girdwood01d75842012-04-25 12:12:49 +01002227 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2228 capture, &pcm);
2229 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002230 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002231 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002232 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002233 return ret;
2234 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002235 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002236
2237 /* DAPM dai link stream work */
2238 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2239
2240 rtd->pcm = pcm;
2241 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002242
2243 if (rtd->dai_link->no_pcm) {
2244 if (playback)
2245 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2246 if (capture)
2247 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2248 goto out;
2249 }
2250
2251 /* ASoC PCM operations */
2252 if (rtd->dai_link->dynamic) {
2253 rtd->ops.open = dpcm_fe_dai_open;
2254 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2255 rtd->ops.prepare = dpcm_fe_dai_prepare;
2256 rtd->ops.trigger = dpcm_fe_dai_trigger;
2257 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2258 rtd->ops.close = dpcm_fe_dai_close;
2259 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002260 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002261 } else {
2262 rtd->ops.open = soc_pcm_open;
2263 rtd->ops.hw_params = soc_pcm_hw_params;
2264 rtd->ops.prepare = soc_pcm_prepare;
2265 rtd->ops.trigger = soc_pcm_trigger;
2266 rtd->ops.hw_free = soc_pcm_hw_free;
2267 rtd->ops.close = soc_pcm_close;
2268 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002269 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002270 }
2271
Liam Girdwoodddee6272011-06-09 14:45:53 +01002272 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002273 rtd->ops.ack = platform->driver->ops->ack;
2274 rtd->ops.copy = platform->driver->ops->copy;
2275 rtd->ops.silence = platform->driver->ops->silence;
2276 rtd->ops.page = platform->driver->ops->page;
2277 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002278 }
2279
2280 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002281 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002282
2283 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002284 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002285
2286 if (platform->driver->pcm_new) {
2287 ret = platform->driver->pcm_new(rtd);
2288 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002289 dev_err(platform->dev,
2290 "ASoC: pcm constructor failed: %d\n",
2291 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002292 return ret;
2293 }
2294 }
2295
2296 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002297out:
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03002298 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name,
Liam Girdwoodddee6272011-06-09 14:45:53 +01002299 cpu_dai->name);
2300 return ret;
2301}
Liam Girdwood01d75842012-04-25 12:12:49 +01002302
2303/* is the current PCM operation for this FE ? */
2304int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2305{
2306 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2307 return 1;
2308 return 0;
2309}
2310EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2311
2312/* is the current PCM operation for this BE ? */
2313int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2314 struct snd_soc_pcm_runtime *be, int stream)
2315{
2316 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2317 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2318 be->dpcm[stream].runtime_update))
2319 return 1;
2320 return 0;
2321}
2322EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2323
2324/* get the substream for this BE */
2325struct snd_pcm_substream *
2326 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2327{
2328 return be->pcm->streams[stream].substream;
2329}
2330EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2331
2332/* get the BE runtime state */
2333enum snd_soc_dpcm_state
2334 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2335{
2336 return be->dpcm[stream].state;
2337}
2338EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2339
2340/* set the BE runtime state */
2341void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2342 int stream, enum snd_soc_dpcm_state state)
2343{
2344 be->dpcm[stream].state = state;
2345}
2346EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2347
2348/*
2349 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2350 * are not running, paused or suspended for the specified stream direction.
2351 */
2352int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2353 struct snd_soc_pcm_runtime *be, int stream)
2354{
2355 struct snd_soc_dpcm *dpcm;
2356 int state;
2357
2358 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2359
2360 if (dpcm->fe == fe)
2361 continue;
2362
2363 state = dpcm->fe->dpcm[stream].state;
2364 if (state == SND_SOC_DPCM_STATE_START ||
2365 state == SND_SOC_DPCM_STATE_PAUSED ||
2366 state == SND_SOC_DPCM_STATE_SUSPEND)
2367 return 0;
2368 }
2369
2370 /* it's safe to free/stop this BE DAI */
2371 return 1;
2372}
2373EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2374
2375/*
2376 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2377 * running, paused or suspended for the specified stream direction.
2378 */
2379int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2380 struct snd_soc_pcm_runtime *be, int stream)
2381{
2382 struct snd_soc_dpcm *dpcm;
2383 int state;
2384
2385 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2386
2387 if (dpcm->fe == fe)
2388 continue;
2389
2390 state = dpcm->fe->dpcm[stream].state;
2391 if (state == SND_SOC_DPCM_STATE_START ||
2392 state == SND_SOC_DPCM_STATE_PAUSED ||
2393 state == SND_SOC_DPCM_STATE_SUSPEND ||
2394 state == SND_SOC_DPCM_STATE_PREPARE)
2395 return 0;
2396 }
2397
2398 /* it's safe to change hw_params */
2399 return 1;
2400}
2401EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002402
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002403int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2404 int cmd, struct snd_soc_platform *platform)
2405{
Mark Brownc5914b02013-10-30 17:47:39 -07002406 if (platform->driver->ops && platform->driver->ops->trigger)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002407 return platform->driver->ops->trigger(substream, cmd);
2408 return 0;
2409}
2410EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2411
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002412#ifdef CONFIG_DEBUG_FS
2413static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2414{
2415 switch (state) {
2416 case SND_SOC_DPCM_STATE_NEW:
2417 return "new";
2418 case SND_SOC_DPCM_STATE_OPEN:
2419 return "open";
2420 case SND_SOC_DPCM_STATE_HW_PARAMS:
2421 return "hw_params";
2422 case SND_SOC_DPCM_STATE_PREPARE:
2423 return "prepare";
2424 case SND_SOC_DPCM_STATE_START:
2425 return "start";
2426 case SND_SOC_DPCM_STATE_STOP:
2427 return "stop";
2428 case SND_SOC_DPCM_STATE_SUSPEND:
2429 return "suspend";
2430 case SND_SOC_DPCM_STATE_PAUSED:
2431 return "paused";
2432 case SND_SOC_DPCM_STATE_HW_FREE:
2433 return "hw_free";
2434 case SND_SOC_DPCM_STATE_CLOSE:
2435 return "close";
2436 }
2437
2438 return "unknown";
2439}
2440
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002441static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2442 int stream, char *buf, size_t size)
2443{
2444 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2445 struct snd_soc_dpcm *dpcm;
2446 ssize_t offset = 0;
2447
2448 /* FE state */
2449 offset += snprintf(buf + offset, size - offset,
2450 "[%s - %s]\n", fe->dai_link->name,
2451 stream ? "Capture" : "Playback");
2452
2453 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2454 dpcm_state_string(fe->dpcm[stream].state));
2455
2456 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2457 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2458 offset += snprintf(buf + offset, size - offset,
2459 "Hardware Params: "
2460 "Format = %s, Channels = %d, Rate = %d\n",
2461 snd_pcm_format_name(params_format(params)),
2462 params_channels(params),
2463 params_rate(params));
2464
2465 /* BEs state */
2466 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2467
2468 if (list_empty(&fe->dpcm[stream].be_clients)) {
2469 offset += snprintf(buf + offset, size - offset,
2470 " No active DSP links\n");
2471 goto out;
2472 }
2473
2474 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2475 struct snd_soc_pcm_runtime *be = dpcm->be;
2476 params = &dpcm->hw_params;
2477
2478 offset += snprintf(buf + offset, size - offset,
2479 "- %s\n", be->dai_link->name);
2480
2481 offset += snprintf(buf + offset, size - offset,
2482 " State: %s\n",
2483 dpcm_state_string(be->dpcm[stream].state));
2484
2485 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2486 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2487 offset += snprintf(buf + offset, size - offset,
2488 " Hardware Params: "
2489 "Format = %s, Channels = %d, Rate = %d\n",
2490 snd_pcm_format_name(params_format(params)),
2491 params_channels(params),
2492 params_rate(params));
2493 }
2494
2495out:
2496 return offset;
2497}
2498
2499static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2500 size_t count, loff_t *ppos)
2501{
2502 struct snd_soc_pcm_runtime *fe = file->private_data;
2503 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2504 char *buf;
2505
2506 buf = kmalloc(out_count, GFP_KERNEL);
2507 if (!buf)
2508 return -ENOMEM;
2509
2510 if (fe->cpu_dai->driver->playback.channels_min)
2511 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2512 buf + offset, out_count - offset);
2513
2514 if (fe->cpu_dai->driver->capture.channels_min)
2515 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2516 buf + offset, out_count - offset);
2517
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002518 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002519
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002520 kfree(buf);
2521 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002522}
2523
2524static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002525 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002526 .read = dpcm_state_read_file,
2527 .llseek = default_llseek,
2528};
2529
2530int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2531{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002532 if (!rtd->dai_link)
2533 return 0;
2534
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002535 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2536 rtd->card->debugfs_card_root);
2537 if (!rtd->debugfs_dpcm_root) {
2538 dev_dbg(rtd->dev,
2539 "ASoC: Failed to create dpcm debugfs directory %s\n",
2540 rtd->dai_link->name);
2541 return -EINVAL;
2542 }
2543
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002544 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002545 rtd->debugfs_dpcm_root,
2546 rtd, &dpcm_state_fops);
2547
2548 return 0;
2549}
2550#endif