blob: 317395824cd794d093e74b98013a8f2537b89056 [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>
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +020010 * Mark Brown <broonie@opensource.wolfsonmicro.com>
Liam Girdwoodddee6272011-06-09 14:45:53 +010011 *
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
Ricard Wanderlofcde79032015-08-24 14:16:51 +020037/*
38 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
39 *
40 * Returns true if the DAI supports the indicated stream type.
41 */
42static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
43{
44 struct snd_soc_pcm_stream *codec_stream;
45
46 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
47 codec_stream = &dai->driver->playback;
48 else
49 codec_stream = &dai->driver->capture;
50
51 /* If the codec specifies any rate at all, it supports the stream. */
52 return codec_stream->rates;
53}
54
Lars-Peter Clausen90996f42013-05-14 11:05:30 +020055/**
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010056 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
57 * @rtd: ASoC PCM runtime that is activated
58 * @stream: Direction of the PCM stream
59 *
60 * Increments the active count for all the DAIs and components attached to a PCM
61 * runtime. Should typically be called when a stream is opened.
62 *
63 * Must be called with the rtd->pcm_mutex being held
64 */
65void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
66{
67 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020068 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010069
70 lockdep_assert_held(&rtd->pcm_mutex);
71
72 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
73 cpu_dai->playback_active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020074 for (i = 0; i < rtd->num_codecs; i++)
75 rtd->codec_dais[i]->playback_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010076 } else {
77 cpu_dai->capture_active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020078 for (i = 0; i < rtd->num_codecs; i++)
79 rtd->codec_dais[i]->capture_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010080 }
81
82 cpu_dai->active++;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +010083 cpu_dai->component->active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020084 for (i = 0; i < rtd->num_codecs; i++) {
85 rtd->codec_dais[i]->active++;
86 rtd->codec_dais[i]->component->active++;
87 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010088}
89
90/**
91 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
92 * @rtd: ASoC PCM runtime that is deactivated
93 * @stream: Direction of the PCM stream
94 *
95 * Decrements the active count for all the DAIs and components attached to a PCM
96 * runtime. Should typically be called when a stream is closed.
97 *
98 * Must be called with the rtd->pcm_mutex being held
99 */
100void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
101{
102 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200103 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100104
105 lockdep_assert_held(&rtd->pcm_mutex);
106
107 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
108 cpu_dai->playback_active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200109 for (i = 0; i < rtd->num_codecs; i++)
110 rtd->codec_dais[i]->playback_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100111 } else {
112 cpu_dai->capture_active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200113 for (i = 0; i < rtd->num_codecs; i++)
114 rtd->codec_dais[i]->capture_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100115 }
116
117 cpu_dai->active--;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +0100118 cpu_dai->component->active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200119 for (i = 0; i < rtd->num_codecs; i++) {
120 rtd->codec_dais[i]->component->active--;
121 rtd->codec_dais[i]->active--;
122 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100123}
124
125/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100126 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
127 * @rtd: The ASoC PCM runtime that should be checked.
128 *
129 * This function checks whether the power down delay should be ignored for a
130 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
131 * been configured to ignore the delay, or if none of the components benefits
132 * from having the delay.
133 */
134bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
135{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200136 int i;
137 bool ignore = true;
138
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100139 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
140 return true;
141
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200142 for (i = 0; i < rtd->num_codecs; i++)
143 ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
144
145 return rtd->cpu_dai->component->ignore_pmdown_time && ignore;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100146}
147
148/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200149 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
150 * @substream: the pcm substream
151 * @hw: the hardware parameters
152 *
153 * Sets the substream runtime hardware parameters.
154 */
155int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
156 const struct snd_pcm_hardware *hw)
157{
158 struct snd_pcm_runtime *runtime = substream->runtime;
159 runtime->hw.info = hw->info;
160 runtime->hw.formats = hw->formats;
161 runtime->hw.period_bytes_min = hw->period_bytes_min;
162 runtime->hw.period_bytes_max = hw->period_bytes_max;
163 runtime->hw.periods_min = hw->periods_min;
164 runtime->hw.periods_max = hw->periods_max;
165 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
166 runtime->hw.fifo_size = hw->fifo_size;
167 return 0;
168}
169EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
170
Liam Girdwood01d75842012-04-25 12:12:49 +0100171/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000172int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100173 int event)
174{
175 struct snd_soc_dpcm *dpcm;
176
177 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
178
179 struct snd_soc_pcm_runtime *be = dpcm->be;
180
Liam Girdwood103d84a2012-11-19 14:39:15 +0000181 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100182 be->dai_link->name, event, dir);
183
184 snd_soc_dapm_stream_event(be, dir, event);
185 }
186
187 snd_soc_dapm_stream_event(fe, dir, event);
188
189 return 0;
190}
191
Dong Aisheng17841022011-08-29 17:15:14 +0800192static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
193 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100194{
195 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100196 int ret;
197
Nicolin Chen3635bf02013-11-13 18:56:24 +0800198 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
199 rtd->dai_link->symmetric_rates)) {
200 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
201 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100202
Nicolin Chen3635bf02013-11-13 18:56:24 +0800203 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
204 SNDRV_PCM_HW_PARAM_RATE,
205 soc_dai->rate, soc_dai->rate);
206 if (ret < 0) {
207 dev_err(soc_dai->dev,
208 "ASoC: Unable to apply rate constraint: %d\n",
209 ret);
210 return ret;
211 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100212 }
213
Nicolin Chen3635bf02013-11-13 18:56:24 +0800214 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
215 rtd->dai_link->symmetric_channels)) {
216 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
217 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100218
Nicolin Chen3635bf02013-11-13 18:56:24 +0800219 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
220 SNDRV_PCM_HW_PARAM_CHANNELS,
221 soc_dai->channels,
222 soc_dai->channels);
223 if (ret < 0) {
224 dev_err(soc_dai->dev,
225 "ASoC: Unable to apply channel symmetry constraint: %d\n",
226 ret);
227 return ret;
228 }
229 }
230
231 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
232 rtd->dai_link->symmetric_samplebits)) {
233 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
234 soc_dai->sample_bits);
235
236 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
237 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
238 soc_dai->sample_bits,
239 soc_dai->sample_bits);
240 if (ret < 0) {
241 dev_err(soc_dai->dev,
242 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
243 ret);
244 return ret;
245 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100246 }
247
248 return 0;
249}
250
Nicolin Chen3635bf02013-11-13 18:56:24 +0800251static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
252 struct snd_pcm_hw_params *params)
253{
254 struct snd_soc_pcm_runtime *rtd = substream->private_data;
255 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200256 unsigned int rate, channels, sample_bits, symmetry, i;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800257
258 rate = params_rate(params);
259 channels = params_channels(params);
260 sample_bits = snd_pcm_format_physical_width(params_format(params));
261
262 /* reject unmatched parameters when applying symmetry */
263 symmetry = cpu_dai->driver->symmetric_rates ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800264 rtd->dai_link->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200265
266 for (i = 0; i < rtd->num_codecs; i++)
267 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
268
Nicolin Chen3635bf02013-11-13 18:56:24 +0800269 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
270 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
271 cpu_dai->rate, rate);
272 return -EINVAL;
273 }
274
275 symmetry = cpu_dai->driver->symmetric_channels ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800276 rtd->dai_link->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200277
278 for (i = 0; i < rtd->num_codecs; i++)
279 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
280
Nicolin Chen3635bf02013-11-13 18:56:24 +0800281 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
282 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
283 cpu_dai->channels, channels);
284 return -EINVAL;
285 }
286
287 symmetry = cpu_dai->driver->symmetric_samplebits ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800288 rtd->dai_link->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200289
290 for (i = 0; i < rtd->num_codecs; i++)
291 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
292
Nicolin Chen3635bf02013-11-13 18:56:24 +0800293 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
294 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
295 cpu_dai->sample_bits, sample_bits);
296 return -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100297 }
298
299 return 0;
300}
301
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100302static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
303{
304 struct snd_soc_pcm_runtime *rtd = substream->private_data;
305 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100306 struct snd_soc_dai_link *link = rtd->dai_link;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200307 unsigned int symmetry, i;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100308
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200309 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
310 cpu_driver->symmetric_channels || link->symmetric_channels ||
311 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
312
313 for (i = 0; i < rtd->num_codecs; i++)
314 symmetry = symmetry ||
315 rtd->codec_dais[i]->driver->symmetric_rates ||
316 rtd->codec_dais[i]->driver->symmetric_channels ||
317 rtd->codec_dais[i]->driver->symmetric_samplebits;
318
319 return symmetry;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100320}
321
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200322static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000323{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200324 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Takashi Iwaic6068d32014-12-31 17:10:34 +0100325 int ret;
Mark Brown58ba9b22012-01-16 18:38:51 +0000326
327 if (!bits)
328 return;
329
Lars-Peter Clausen0e2a3752014-12-29 18:43:38 +0100330 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
331 if (ret != 0)
332 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
333 bits, ret);
Mark Brown58ba9b22012-01-16 18:38:51 +0000334}
335
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200336static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200337{
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200338 struct snd_soc_pcm_runtime *rtd = substream->private_data;
339 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200340 struct snd_soc_dai *codec_dai;
341 int i;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200342 unsigned int bits = 0, cpu_bits;
343
344 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200345 for (i = 0; i < rtd->num_codecs; i++) {
346 codec_dai = rtd->codec_dais[i];
347 if (codec_dai->driver->playback.sig_bits == 0) {
348 bits = 0;
349 break;
350 }
351 bits = max(codec_dai->driver->playback.sig_bits, bits);
352 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200353 cpu_bits = cpu_dai->driver->playback.sig_bits;
354 } else {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200355 for (i = 0; i < rtd->num_codecs; i++) {
356 codec_dai = rtd->codec_dais[i];
Daniel Mack5e63dfc2014-10-07 14:33:46 +0200357 if (codec_dai->driver->capture.sig_bits == 0) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200358 bits = 0;
359 break;
360 }
361 bits = max(codec_dai->driver->capture.sig_bits, bits);
362 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200363 cpu_bits = cpu_dai->driver->capture.sig_bits;
364 }
365
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200366 soc_pcm_set_msb(substream, bits);
367 soc_pcm_set_msb(substream, cpu_bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200368}
369
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200370static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200371{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200372 struct snd_pcm_runtime *runtime = substream->runtime;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100373 struct snd_pcm_hardware *hw = &runtime->hw;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200374 struct snd_soc_pcm_runtime *rtd = substream->private_data;
375 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
376 struct snd_soc_dai_driver *codec_dai_drv;
377 struct snd_soc_pcm_stream *codec_stream;
378 struct snd_soc_pcm_stream *cpu_stream;
379 unsigned int chan_min = 0, chan_max = UINT_MAX;
380 unsigned int rate_min = 0, rate_max = UINT_MAX;
381 unsigned int rates = UINT_MAX;
382 u64 formats = ULLONG_MAX;
383 int i;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100384
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200385 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
386 cpu_stream = &cpu_dai_drv->playback;
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100387 else
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200388 cpu_stream = &cpu_dai_drv->capture;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100389
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200390 /* first calculate min/max only for CODECs in the DAI link */
391 for (i = 0; i < rtd->num_codecs; i++) {
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200392
393 /*
394 * Skip CODECs which don't support the current stream type.
395 * Otherwise, since the rate, channel, and format values will
396 * zero in that case, we would have no usable settings left,
397 * causing the resulting setup to fail.
398 * At least one CODEC should match, otherwise we should have
399 * bailed out on a higher level, since there would be no
400 * CODEC to support the transfer direction in that case.
401 */
402 if (!snd_soc_dai_stream_valid(rtd->codec_dais[i],
403 substream->stream))
404 continue;
405
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200406 codec_dai_drv = rtd->codec_dais[i]->driver;
407 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
408 codec_stream = &codec_dai_drv->playback;
409 else
410 codec_stream = &codec_dai_drv->capture;
411 chan_min = max(chan_min, codec_stream->channels_min);
412 chan_max = min(chan_max, codec_stream->channels_max);
413 rate_min = max(rate_min, codec_stream->rate_min);
414 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
415 formats &= codec_stream->formats;
416 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
417 }
418
419 /*
420 * chan min/max cannot be enforced if there are multiple CODEC DAIs
421 * connected to a single CPU DAI, use CPU DAI's directly and let
422 * channel allocation be fixed up later
423 */
424 if (rtd->num_codecs > 1) {
425 chan_min = cpu_stream->channels_min;
426 chan_max = cpu_stream->channels_max;
427 }
428
429 hw->channels_min = max(chan_min, cpu_stream->channels_min);
430 hw->channels_max = min(chan_max, cpu_stream->channels_max);
431 if (hw->formats)
432 hw->formats &= formats & cpu_stream->formats;
433 else
434 hw->formats = formats & cpu_stream->formats;
435 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100436
437 snd_pcm_limit_hw_rates(runtime);
438
439 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200440 hw->rate_min = max(hw->rate_min, rate_min);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100441 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200442 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200443}
444
Mark Brown58ba9b22012-01-16 18:38:51 +0000445/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100446 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
447 * then initialized and any private data can be allocated. This also calls
448 * startup for the cpu DAI, platform, machine and codec DAI.
449 */
450static int soc_pcm_open(struct snd_pcm_substream *substream)
451{
452 struct snd_soc_pcm_runtime *rtd = substream->private_data;
453 struct snd_pcm_runtime *runtime = substream->runtime;
454 struct snd_soc_platform *platform = rtd->platform;
455 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200456 struct snd_soc_dai *codec_dai;
457 const char *codec_dai_name = "multicodec";
458 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100459
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800460 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200461 for (i = 0; i < rtd->num_codecs; i++)
462 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000463 pm_runtime_get_sync(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200464 for (i = 0; i < rtd->num_codecs; i++)
465 pm_runtime_get_sync(rtd->codec_dais[i]->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000466 pm_runtime_get_sync(platform->dev);
467
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100468 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100469
470 /* startup the audio subsystem */
Mark Brownc5914b02013-10-30 17:47:39 -0700471 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100472 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
473 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000474 dev_err(cpu_dai->dev, "ASoC: can't open interface"
475 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100476 goto out;
477 }
478 }
479
480 if (platform->driver->ops && platform->driver->ops->open) {
481 ret = platform->driver->ops->open(substream);
482 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000483 dev_err(platform->dev, "ASoC: can't open platform"
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200484 " %s: %d\n", platform->component.name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100485 goto platform_err;
486 }
487 }
488
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200489 for (i = 0; i < rtd->num_codecs; i++) {
490 codec_dai = rtd->codec_dais[i];
491 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
492 ret = codec_dai->driver->ops->startup(substream,
493 codec_dai);
494 if (ret < 0) {
495 dev_err(codec_dai->dev,
496 "ASoC: can't open codec %s: %d\n",
497 codec_dai->name, ret);
498 goto codec_dai_err;
499 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100500 }
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200501
502 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
503 codec_dai->tx_mask = 0;
504 else
505 codec_dai->rx_mask = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100506 }
507
508 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
509 ret = rtd->dai_link->ops->startup(substream);
510 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000511 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000512 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100513 goto machine_err;
514 }
515 }
516
Liam Girdwood01d75842012-04-25 12:12:49 +0100517 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
518 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
519 goto dynamic;
520
Liam Girdwoodddee6272011-06-09 14:45:53 +0100521 /* Check that the codec and cpu DAIs are compatible */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200522 soc_pcm_init_runtime_hw(substream);
523
524 if (rtd->num_codecs == 1)
525 codec_dai_name = rtd->codec_dai->name;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100526
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100527 if (soc_pcm_has_symmetry(substream))
528 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
529
Liam Girdwoodddee6272011-06-09 14:45:53 +0100530 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100531 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000532 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200533 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100534 goto config_err;
535 }
536 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000537 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200538 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100539 goto config_err;
540 }
541 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
542 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000543 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200544 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100545 goto config_err;
546 }
547
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200548 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000549
Liam Girdwoodddee6272011-06-09 14:45:53 +0100550 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800551 if (cpu_dai->active) {
552 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
553 if (ret != 0)
554 goto config_err;
555 }
556
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200557 for (i = 0; i < rtd->num_codecs; i++) {
558 if (rtd->codec_dais[i]->active) {
559 ret = soc_pcm_apply_symmetry(substream,
560 rtd->codec_dais[i]);
561 if (ret != 0)
562 goto config_err;
563 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100564 }
565
Liam Girdwood103d84a2012-11-19 14:39:15 +0000566 pr_debug("ASoC: %s <-> %s info:\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200567 codec_dai_name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000568 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
569 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100570 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000571 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100572 runtime->hw.rate_max);
573
Liam Girdwood01d75842012-04-25 12:12:49 +0100574dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100575
576 snd_soc_runtime_activate(rtd, substream->stream);
577
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100578 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100579 return 0;
580
581config_err:
582 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
583 rtd->dai_link->ops->shutdown(substream);
584
585machine_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200586 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100587
588codec_dai_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200589 while (--i >= 0) {
590 codec_dai = rtd->codec_dais[i];
591 if (codec_dai->driver->ops->shutdown)
592 codec_dai->driver->ops->shutdown(substream, codec_dai);
593 }
594
Liam Girdwoodddee6272011-06-09 14:45:53 +0100595 if (platform->driver->ops && platform->driver->ops->close)
596 platform->driver->ops->close(substream);
597
598platform_err:
599 if (cpu_dai->driver->ops->shutdown)
600 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
601out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100602 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000603
604 pm_runtime_put(platform->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200605 for (i = 0; i < rtd->num_codecs; i++)
606 pm_runtime_put(rtd->codec_dais[i]->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000607 pm_runtime_put(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200608 for (i = 0; i < rtd->num_codecs; i++) {
609 if (!rtd->codec_dais[i]->active)
610 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
611 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800612 if (!cpu_dai->active)
613 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000614
Liam Girdwoodddee6272011-06-09 14:45:53 +0100615 return ret;
616}
617
618/*
619 * Power down the audio subsystem pmdown_time msecs after close is called.
620 * This is to ensure there are no pops or clicks in between any music tracks
621 * due to DAPM power cycling.
622 */
623static void close_delayed_work(struct work_struct *work)
624{
625 struct snd_soc_pcm_runtime *rtd =
626 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200627 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
Liam Girdwoodddee6272011-06-09 14:45:53 +0100628
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100629 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100630
Liam Girdwood103d84a2012-11-19 14:39:15 +0000631 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100632 codec_dai->driver->playback.stream_name,
633 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600634 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100635
636 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600637 if (rtd->pop_wait == 1) {
638 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800639 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000640 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100641 }
642
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100643 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100644}
645
646/*
647 * Called by ALSA when a PCM substream is closed. Private data can be
648 * freed here. The cpu DAI, codec DAI, machine and platform are also
649 * shutdown.
650 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100651static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100652{
653 struct snd_soc_pcm_runtime *rtd = substream->private_data;
654 struct snd_soc_platform *platform = rtd->platform;
655 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200656 struct snd_soc_dai *codec_dai;
657 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100658
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100659 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100660
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100661 snd_soc_runtime_deactivate(rtd, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100662
Dong Aisheng17841022011-08-29 17:15:14 +0800663 /* clear the corresponding DAIs rate when inactive */
664 if (!cpu_dai->active)
665 cpu_dai->rate = 0;
666
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200667 for (i = 0; i < rtd->num_codecs; i++) {
668 codec_dai = rtd->codec_dais[i];
669 if (!codec_dai->active)
670 codec_dai->rate = 0;
671 }
Sascha Hauer25b76792011-08-17 09:20:01 +0200672
Ramesh Babuae116012014-10-15 12:34:59 +0530673 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
674
Liam Girdwoodddee6272011-06-09 14:45:53 +0100675 if (cpu_dai->driver->ops->shutdown)
676 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
677
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200678 for (i = 0; i < rtd->num_codecs; i++) {
679 codec_dai = rtd->codec_dais[i];
680 if (codec_dai->driver->ops->shutdown)
681 codec_dai->driver->ops->shutdown(substream, codec_dai);
682 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100683
684 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
685 rtd->dai_link->ops->shutdown(substream);
686
687 if (platform->driver->ops && platform->driver->ops->close)
688 platform->driver->ops->close(substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100689
690 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100691 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300692 /* powered down playback stream now */
693 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800694 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800695 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300696 } else {
697 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600698 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100699 queue_delayed_work(system_power_efficient_wq,
700 &rtd->delayed_work,
701 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300702 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100703 } else {
704 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800705 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000706 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100707 }
708
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100709 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000710
711 pm_runtime_put(platform->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200712 for (i = 0; i < rtd->num_codecs; i++)
713 pm_runtime_put(rtd->codec_dais[i]->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000714 pm_runtime_put(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200715 for (i = 0; i < rtd->num_codecs; i++) {
716 if (!rtd->codec_dais[i]->active)
717 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
718 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800719 if (!cpu_dai->active)
720 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000721
Liam Girdwoodddee6272011-06-09 14:45:53 +0100722 return 0;
723}
724
725/*
726 * Called by ALSA when the PCM substream is prepared, can set format, sample
727 * rate, etc. This function is non atomic and can be called multiple times,
728 * it can refer to the runtime info.
729 */
730static int soc_pcm_prepare(struct snd_pcm_substream *substream)
731{
732 struct snd_soc_pcm_runtime *rtd = substream->private_data;
733 struct snd_soc_platform *platform = rtd->platform;
734 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200735 struct snd_soc_dai *codec_dai;
736 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100737
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100738 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100739
740 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
741 ret = rtd->dai_link->ops->prepare(substream);
742 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000743 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
744 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100745 goto out;
746 }
747 }
748
749 if (platform->driver->ops && platform->driver->ops->prepare) {
750 ret = platform->driver->ops->prepare(substream);
751 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000752 dev_err(platform->dev, "ASoC: platform prepare error:"
753 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100754 goto out;
755 }
756 }
757
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200758 for (i = 0; i < rtd->num_codecs; i++) {
759 codec_dai = rtd->codec_dais[i];
760 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
761 ret = codec_dai->driver->ops->prepare(substream,
762 codec_dai);
763 if (ret < 0) {
764 dev_err(codec_dai->dev,
Jarkko Nikula90cc7f12014-12-23 11:04:41 +0200765 "ASoC: codec DAI prepare error: %d\n",
766 ret);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200767 goto out;
768 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100769 }
770 }
771
Mark Brownc5914b02013-10-30 17:47:39 -0700772 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100773 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
774 if (ret < 0) {
Jarkko Nikula90cc7f12014-12-23 11:04:41 +0200775 dev_err(cpu_dai->dev,
776 "ASoC: cpu DAI prepare error: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100777 goto out;
778 }
779 }
780
781 /* cancel any delayed stream shutdown that is pending */
782 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600783 rtd->pop_wait) {
784 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100785 cancel_delayed_work(&rtd->delayed_work);
786 }
787
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000788 snd_soc_dapm_stream_event(rtd, substream->stream,
789 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100790
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200791 for (i = 0; i < rtd->num_codecs; i++)
792 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
793 substream->stream);
Ramesh Babuae116012014-10-15 12:34:59 +0530794 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100795
796out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100797 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100798 return ret;
799}
800
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200801static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
802 unsigned int mask)
803{
804 struct snd_interval *interval;
805 int channels = hweight_long(mask);
806
807 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
808 interval->min = channels;
809 interval->max = channels;
810}
811
Benoit Cousson93e69582014-07-08 23:19:38 +0200812int soc_dai_hw_params(struct snd_pcm_substream *substream,
813 struct snd_pcm_hw_params *params,
814 struct snd_soc_dai *dai)
815{
816 int ret;
817
818 if (dai->driver->ops && dai->driver->ops->hw_params) {
819 ret = dai->driver->ops->hw_params(substream, params, dai);
820 if (ret < 0) {
821 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
822 dai->name, ret);
823 return ret;
824 }
825 }
826
827 return 0;
828}
829
Liam Girdwoodddee6272011-06-09 14:45:53 +0100830/*
831 * Called by ALSA when the hardware params are set by application. This
832 * function can also be called multiple times and can allocate buffers
833 * (using snd_pcm_lib_* ). It's non-atomic.
834 */
835static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
836 struct snd_pcm_hw_params *params)
837{
838 struct snd_soc_pcm_runtime *rtd = substream->private_data;
839 struct snd_soc_platform *platform = rtd->platform;
840 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200841 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100842
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100843 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100844
Nicolin Chen3635bf02013-11-13 18:56:24 +0800845 ret = soc_pcm_params_symmetry(substream, params);
846 if (ret)
847 goto out;
848
Liam Girdwoodddee6272011-06-09 14:45:53 +0100849 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
850 ret = rtd->dai_link->ops->hw_params(substream, params);
851 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000852 dev_err(rtd->card->dev, "ASoC: machine hw_params"
853 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100854 goto out;
855 }
856 }
857
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200858 for (i = 0; i < rtd->num_codecs; i++) {
859 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
860 struct snd_pcm_hw_params codec_params;
861
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200862 /*
863 * Skip CODECs which don't support the current stream type,
864 * the idea being that if a CODEC is not used for the currently
865 * set up transfer direction, it should not need to be
866 * configured, especially since the configuration used might
867 * not even be supported by that CODEC. There may be cases
868 * however where a CODEC needs to be set up although it is
869 * actually not being used for the transfer, e.g. if a
870 * capture-only CODEC is acting as an LRCLK and/or BCLK master
871 * for the DAI link including a playback-only CODEC.
872 * If this becomes necessary, we will have to augment the
873 * machine driver setup with information on how to act, so
874 * we can do the right thing here.
875 */
876 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
877 continue;
878
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200879 /* copy params for each codec */
880 codec_params = *params;
881
882 /* fixup params based on TDM slot masks */
883 if (codec_dai->tx_mask)
884 soc_pcm_codec_params_fixup(&codec_params,
885 codec_dai->tx_mask);
886 if (codec_dai->rx_mask)
887 soc_pcm_codec_params_fixup(&codec_params,
888 codec_dai->rx_mask);
889
Benoit Cousson93e69582014-07-08 23:19:38 +0200890 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
891 if(ret < 0)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100892 goto codec_err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200893
894 codec_dai->rate = params_rate(&codec_params);
895 codec_dai->channels = params_channels(&codec_params);
896 codec_dai->sample_bits = snd_pcm_format_physical_width(
897 params_format(&codec_params));
Liam Girdwoodddee6272011-06-09 14:45:53 +0100898 }
899
Benoit Cousson93e69582014-07-08 23:19:38 +0200900 ret = soc_dai_hw_params(substream, params, cpu_dai);
901 if (ret < 0)
902 goto interface_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100903
904 if (platform->driver->ops && platform->driver->ops->hw_params) {
905 ret = platform->driver->ops->hw_params(substream, params);
906 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000907 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200908 platform->component.name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100909 goto platform_err;
910 }
911 }
912
Nicolin Chen3635bf02013-11-13 18:56:24 +0800913 /* store the parameters for each DAIs */
Dong Aisheng17841022011-08-29 17:15:14 +0800914 cpu_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800915 cpu_dai->channels = params_channels(params);
916 cpu_dai->sample_bits =
917 snd_pcm_format_physical_width(params_format(params));
918
Liam Girdwoodddee6272011-06-09 14:45:53 +0100919out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100920 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100921 return ret;
922
923platform_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700924 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100925 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
926
927interface_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200928 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100929
930codec_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200931 while (--i >= 0) {
932 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
933 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
934 codec_dai->driver->ops->hw_free(substream, codec_dai);
935 codec_dai->rate = 0;
936 }
937
Liam Girdwoodddee6272011-06-09 14:45:53 +0100938 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
939 rtd->dai_link->ops->hw_free(substream);
940
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100941 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100942 return ret;
943}
944
945/*
946 * Frees resources allocated by hw_params, can be called multiple times
947 */
948static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
949{
950 struct snd_soc_pcm_runtime *rtd = substream->private_data;
951 struct snd_soc_platform *platform = rtd->platform;
952 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200953 struct snd_soc_dai *codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +0800954 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200955 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100956
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100957 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100958
Nicolin Chend3383422013-11-20 18:37:09 +0800959 /* clear the corresponding DAIs parameters when going to be inactive */
960 if (cpu_dai->active == 1) {
961 cpu_dai->rate = 0;
962 cpu_dai->channels = 0;
963 cpu_dai->sample_bits = 0;
964 }
965
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200966 for (i = 0; i < rtd->num_codecs; i++) {
967 codec_dai = rtd->codec_dais[i];
968 if (codec_dai->active == 1) {
969 codec_dai->rate = 0;
970 codec_dai->channels = 0;
971 codec_dai->sample_bits = 0;
972 }
Nicolin Chend3383422013-11-20 18:37:09 +0800973 }
974
Liam Girdwoodddee6272011-06-09 14:45:53 +0100975 /* apply codec digital mute */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200976 for (i = 0; i < rtd->num_codecs; i++) {
977 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
978 (!playback && rtd->codec_dais[i]->capture_active == 1))
979 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
980 substream->stream);
981 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100982
983 /* free any machine hw params */
984 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
985 rtd->dai_link->ops->hw_free(substream);
986
987 /* free any DMA resources */
988 if (platform->driver->ops && platform->driver->ops->hw_free)
989 platform->driver->ops->hw_free(substream);
990
991 /* now free hw params for the DAIs */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200992 for (i = 0; i < rtd->num_codecs; i++) {
993 codec_dai = rtd->codec_dais[i];
994 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
995 codec_dai->driver->ops->hw_free(substream, codec_dai);
996 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100997
Mark Brownc5914b02013-10-30 17:47:39 -0700998 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100999 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1000
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001001 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001002 return 0;
1003}
1004
1005static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1006{
1007 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1008 struct snd_soc_platform *platform = rtd->platform;
1009 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001010 struct snd_soc_dai *codec_dai;
1011 int i, ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001012
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001013 for (i = 0; i < rtd->num_codecs; i++) {
1014 codec_dai = rtd->codec_dais[i];
1015 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
1016 ret = codec_dai->driver->ops->trigger(substream,
1017 cmd, codec_dai);
1018 if (ret < 0)
1019 return ret;
1020 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001021 }
1022
1023 if (platform->driver->ops && platform->driver->ops->trigger) {
1024 ret = platform->driver->ops->trigger(substream, cmd);
1025 if (ret < 0)
1026 return ret;
1027 }
1028
Mark Brownc5914b02013-10-30 17:47:39 -07001029 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +01001030 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1031 if (ret < 0)
1032 return ret;
1033 }
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001034
1035 if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
1036 ret = rtd->dai_link->ops->trigger(substream, cmd);
1037 if (ret < 0)
1038 return ret;
1039 }
1040
Liam Girdwoodddee6272011-06-09 14:45:53 +01001041 return 0;
1042}
1043
Mark Brown45c0a182012-05-09 21:46:27 +01001044static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1045 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001046{
1047 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1048 struct snd_soc_platform *platform = rtd->platform;
1049 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001050 struct snd_soc_dai *codec_dai;
1051 int i, ret;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001052
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001053 for (i = 0; i < rtd->num_codecs; i++) {
1054 codec_dai = rtd->codec_dais[i];
1055 if (codec_dai->driver->ops &&
1056 codec_dai->driver->ops->bespoke_trigger) {
1057 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1058 cmd, codec_dai);
1059 if (ret < 0)
1060 return ret;
1061 }
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001062 }
1063
Jean-Francois Moinedcf0fa22014-01-03 09:19:18 +01001064 if (platform->driver->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001065 ret = platform->driver->bespoke_trigger(substream, cmd);
1066 if (ret < 0)
1067 return ret;
1068 }
1069
Mark Brownc5914b02013-10-30 17:47:39 -07001070 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001071 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1072 if (ret < 0)
1073 return ret;
1074 }
1075 return 0;
1076}
Liam Girdwoodddee6272011-06-09 14:45:53 +01001077/*
1078 * soc level wrapper for pointer callback
1079 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1080 * the runtime->delay will be updated accordingly.
1081 */
1082static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1083{
1084 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1085 struct snd_soc_platform *platform = rtd->platform;
1086 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001087 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001088 struct snd_pcm_runtime *runtime = substream->runtime;
1089 snd_pcm_uframes_t offset = 0;
1090 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001091 snd_pcm_sframes_t codec_delay = 0;
1092 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001093
1094 if (platform->driver->ops && platform->driver->ops->pointer)
1095 offset = platform->driver->ops->pointer(substream);
1096
Mark Brownc5914b02013-10-30 17:47:39 -07001097 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001098 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1099
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001100 for (i = 0; i < rtd->num_codecs; i++) {
1101 codec_dai = rtd->codec_dais[i];
1102 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
1103 codec_delay = max(codec_delay,
1104 codec_dai->driver->ops->delay(substream,
1105 codec_dai));
1106 }
1107 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001108
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001109 /*
1110 * None of the existing platform drivers implement delay(), so
1111 * for now the codec_dai of first multicodec entry is used
1112 */
Liam Girdwoodddee6272011-06-09 14:45:53 +01001113 if (platform->driver->delay)
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001114 delay += platform->driver->delay(substream, rtd->codec_dais[0]);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001115
1116 runtime->delay = delay;
1117
1118 return offset;
1119}
1120
Liam Girdwood01d75842012-04-25 12:12:49 +01001121/* connect a FE and BE */
1122static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1123 struct snd_soc_pcm_runtime *be, int stream)
1124{
1125 struct snd_soc_dpcm *dpcm;
1126
1127 /* only add new dpcms */
1128 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1129 if (dpcm->be == be && dpcm->fe == fe)
1130 return 0;
1131 }
1132
1133 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1134 if (!dpcm)
1135 return -ENOMEM;
1136
1137 dpcm->be = be;
1138 dpcm->fe = fe;
1139 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1140 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1141 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1142 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1143
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001144 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001145 stream ? "capture" : "playback", fe->dai_link->name,
1146 stream ? "<-" : "->", be->dai_link->name);
1147
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001148#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02001149 if (fe->debugfs_dpcm_root)
1150 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1151 fe->debugfs_dpcm_root, &dpcm->state);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001152#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001153 return 1;
1154}
1155
1156/* reparent a BE onto another FE */
1157static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1158 struct snd_soc_pcm_runtime *be, int stream)
1159{
1160 struct snd_soc_dpcm *dpcm;
1161 struct snd_pcm_substream *fe_substream, *be_substream;
1162
1163 /* reparent if BE is connected to other FEs */
1164 if (!be->dpcm[stream].users)
1165 return;
1166
1167 be_substream = snd_soc_dpcm_get_substream(be, stream);
1168
1169 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1170 if (dpcm->fe == fe)
1171 continue;
1172
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001173 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001174 stream ? "capture" : "playback",
1175 dpcm->fe->dai_link->name,
1176 stream ? "<-" : "->", dpcm->be->dai_link->name);
1177
1178 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1179 be_substream->runtime = fe_substream->runtime;
1180 break;
1181 }
1182}
1183
1184/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001185void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001186{
1187 struct snd_soc_dpcm *dpcm, *d;
1188
1189 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001190 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001191 stream ? "capture" : "playback",
1192 dpcm->be->dai_link->name);
1193
1194 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1195 continue;
1196
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001197 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001198 stream ? "capture" : "playback", fe->dai_link->name,
1199 stream ? "<-" : "->", dpcm->be->dai_link->name);
1200
1201 /* BEs still alive need new FE */
1202 dpcm_be_reparent(fe, dpcm->be, stream);
1203
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001204#ifdef CONFIG_DEBUG_FS
1205 debugfs_remove(dpcm->debugfs_state);
1206#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001207 list_del(&dpcm->list_be);
1208 list_del(&dpcm->list_fe);
1209 kfree(dpcm);
1210 }
1211}
1212
1213/* get BE for DAI widget and stream */
1214static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1215 struct snd_soc_dapm_widget *widget, int stream)
1216{
1217 struct snd_soc_pcm_runtime *be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001218 int i, j;
Liam Girdwood01d75842012-04-25 12:12:49 +01001219
1220 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1221 for (i = 0; i < card->num_links; i++) {
1222 be = &card->rtd[i];
1223
Liam Girdwood35ea0652012-06-05 19:26:59 +01001224 if (!be->dai_link->no_pcm)
1225 continue;
1226
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001227 if (be->cpu_dai->playback_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001228 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001229
1230 for (j = 0; j < be->num_codecs; j++) {
1231 struct snd_soc_dai *dai = be->codec_dais[j];
1232 if (dai->playback_widget == widget)
1233 return be;
1234 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001235 }
1236 } else {
1237
1238 for (i = 0; i < card->num_links; i++) {
1239 be = &card->rtd[i];
1240
Liam Girdwood35ea0652012-06-05 19:26:59 +01001241 if (!be->dai_link->no_pcm)
1242 continue;
1243
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001244 if (be->cpu_dai->capture_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001245 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001246
1247 for (j = 0; j < be->num_codecs; j++) {
1248 struct snd_soc_dai *dai = be->codec_dais[j];
1249 if (dai->capture_widget == widget)
1250 return be;
1251 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001252 }
1253 }
1254
Liam Girdwood103d84a2012-11-19 14:39:15 +00001255 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001256 stream ? "capture" : "playback", widget->name);
1257 return NULL;
1258}
1259
1260static inline struct snd_soc_dapm_widget *
Benoit Cousson37018612014-04-24 14:01:45 +02001261 dai_get_widget(struct snd_soc_dai *dai, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001262{
1263 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Benoit Cousson37018612014-04-24 14:01:45 +02001264 return dai->playback_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001265 else
Benoit Cousson37018612014-04-24 14:01:45 +02001266 return dai->capture_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001267}
1268
1269static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1270 struct snd_soc_dapm_widget *widget)
1271{
1272 int i;
1273
1274 for (i = 0; i < list->num_widgets; i++) {
1275 if (widget == list->widgets[i])
1276 return 1;
1277 }
1278
1279 return 0;
1280}
1281
Liam Girdwood23607022014-01-17 17:03:55 +00001282int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001283 int stream, struct snd_soc_dapm_widget_list **list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001284{
1285 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001286 int paths;
1287
Liam Girdwood01d75842012-04-25 12:12:49 +01001288 /* get number of valid DAI paths and their widgets */
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001289 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list);
Liam Girdwood01d75842012-04-25 12:12:49 +01001290
Liam Girdwood103d84a2012-11-19 14:39:15 +00001291 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001292 stream ? "capture" : "playback");
1293
Liam Girdwood01d75842012-04-25 12:12:49 +01001294 return paths;
1295}
1296
Liam Girdwood01d75842012-04-25 12:12:49 +01001297static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1298 struct snd_soc_dapm_widget_list **list_)
1299{
1300 struct snd_soc_dpcm *dpcm;
1301 struct snd_soc_dapm_widget_list *list = *list_;
1302 struct snd_soc_dapm_widget *widget;
1303 int prune = 0;
1304
1305 /* Destroy any old FE <--> BE connections */
1306 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001307 unsigned int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001308
1309 /* is there a valid CPU DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001310 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001311
1312 /* prune the BE if it's no longer in our active list */
1313 if (widget && widget_in_list(list, widget))
1314 continue;
1315
1316 /* is there a valid CODEC DAI widget for this BE */
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001317 for (i = 0; i < dpcm->be->num_codecs; i++) {
1318 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1319 widget = dai_get_widget(dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001320
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001321 /* prune the BE if it's no longer in our active list */
1322 if (widget && widget_in_list(list, widget))
1323 continue;
1324 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001325
Liam Girdwood103d84a2012-11-19 14:39:15 +00001326 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001327 stream ? "capture" : "playback",
1328 dpcm->be->dai_link->name, fe->dai_link->name);
1329 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1330 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1331 prune++;
1332 }
1333
Liam Girdwood103d84a2012-11-19 14:39:15 +00001334 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001335 return prune;
1336}
1337
1338static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1339 struct snd_soc_dapm_widget_list **list_)
1340{
1341 struct snd_soc_card *card = fe->card;
1342 struct snd_soc_dapm_widget_list *list = *list_;
1343 struct snd_soc_pcm_runtime *be;
1344 int i, new = 0, err;
1345
1346 /* Create any new FE <--> BE connections */
1347 for (i = 0; i < list->num_widgets; i++) {
1348
Mark Brown46162742013-06-05 19:36:11 +01001349 switch (list->widgets[i]->id) {
1350 case snd_soc_dapm_dai_in:
Koro Chenc5b85402015-07-06 10:02:10 +08001351 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1352 continue;
1353 break;
Mark Brown46162742013-06-05 19:36:11 +01001354 case snd_soc_dapm_dai_out:
Koro Chenc5b85402015-07-06 10:02:10 +08001355 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1356 continue;
Mark Brown46162742013-06-05 19:36:11 +01001357 break;
1358 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001359 continue;
Mark Brown46162742013-06-05 19:36:11 +01001360 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001361
1362 /* is there a valid BE rtd for this widget */
1363 be = dpcm_get_be(card, list->widgets[i], stream);
1364 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001365 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001366 list->widgets[i]->name);
1367 continue;
1368 }
1369
1370 /* make sure BE is a real BE */
1371 if (!be->dai_link->no_pcm)
1372 continue;
1373
1374 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001375 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001376 continue;
1377
1378 /* newly connected FE and BE */
1379 err = dpcm_be_connect(fe, be, stream);
1380 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001381 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001382 list->widgets[i]->name);
1383 break;
1384 } else if (err == 0) /* already connected */
1385 continue;
1386
1387 /* new */
1388 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1389 new++;
1390 }
1391
Liam Girdwood103d84a2012-11-19 14:39:15 +00001392 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001393 return new;
1394}
1395
1396/*
1397 * Find the corresponding BE DAIs that source or sink audio to this
1398 * FE substream.
1399 */
Liam Girdwood23607022014-01-17 17:03:55 +00001400int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001401 int stream, struct snd_soc_dapm_widget_list **list, int new)
1402{
1403 if (new)
1404 return dpcm_add_paths(fe, stream, list);
1405 else
1406 return dpcm_prune_paths(fe, stream, list);
1407}
1408
Liam Girdwood23607022014-01-17 17:03:55 +00001409void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001410{
1411 struct snd_soc_dpcm *dpcm;
1412
1413 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1414 dpcm->be->dpcm[stream].runtime_update =
1415 SND_SOC_DPCM_UPDATE_NO;
1416}
1417
1418static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1419 int stream)
1420{
1421 struct snd_soc_dpcm *dpcm;
1422
1423 /* disable any enabled and non active backends */
1424 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1425
1426 struct snd_soc_pcm_runtime *be = dpcm->be;
1427 struct snd_pcm_substream *be_substream =
1428 snd_soc_dpcm_get_substream(be, stream);
1429
1430 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001431 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001432 stream ? "capture" : "playback",
1433 be->dpcm[stream].state);
1434
1435 if (--be->dpcm[stream].users != 0)
1436 continue;
1437
1438 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1439 continue;
1440
1441 soc_pcm_close(be_substream);
1442 be_substream->runtime = NULL;
1443 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1444 }
1445}
1446
Liam Girdwood23607022014-01-17 17:03:55 +00001447int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001448{
1449 struct snd_soc_dpcm *dpcm;
1450 int err, count = 0;
1451
1452 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1453 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1454
1455 struct snd_soc_pcm_runtime *be = dpcm->be;
1456 struct snd_pcm_substream *be_substream =
1457 snd_soc_dpcm_get_substream(be, stream);
1458
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001459 if (!be_substream) {
1460 dev_err(be->dev, "ASoC: no backend %s stream\n",
1461 stream ? "capture" : "playback");
1462 continue;
1463 }
1464
Liam Girdwood01d75842012-04-25 12:12:49 +01001465 /* is this op for this BE ? */
1466 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1467 continue;
1468
1469 /* first time the dpcm is open ? */
1470 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001471 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001472 stream ? "capture" : "playback",
1473 be->dpcm[stream].state);
1474
1475 if (be->dpcm[stream].users++ != 0)
1476 continue;
1477
1478 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1479 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1480 continue;
1481
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001482 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1483 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001484
1485 be_substream->runtime = be->dpcm[stream].runtime;
1486 err = soc_pcm_open(be_substream);
1487 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001488 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001489 be->dpcm[stream].users--;
1490 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001491 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001492 stream ? "capture" : "playback",
1493 be->dpcm[stream].state);
1494
1495 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1496 goto unwind;
1497 }
1498
1499 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1500 count++;
1501 }
1502
1503 return count;
1504
1505unwind:
1506 /* disable any enabled and non active backends */
1507 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1508 struct snd_soc_pcm_runtime *be = dpcm->be;
1509 struct snd_pcm_substream *be_substream =
1510 snd_soc_dpcm_get_substream(be, stream);
1511
1512 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1513 continue;
1514
1515 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001516 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001517 stream ? "capture" : "playback",
1518 be->dpcm[stream].state);
1519
1520 if (--be->dpcm[stream].users != 0)
1521 continue;
1522
1523 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1524 continue;
1525
1526 soc_pcm_close(be_substream);
1527 be_substream->runtime = NULL;
1528 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1529 }
1530
1531 return err;
1532}
1533
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001534static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001535 struct snd_soc_pcm_stream *stream,
1536 u64 formats)
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001537{
1538 runtime->hw.rate_min = stream->rate_min;
1539 runtime->hw.rate_max = stream->rate_max;
1540 runtime->hw.channels_min = stream->channels_min;
1541 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001542 if (runtime->hw.formats)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001543 runtime->hw.formats &= formats & stream->formats;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001544 else
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001545 runtime->hw.formats = formats & stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001546 runtime->hw.rates = stream->rates;
1547}
1548
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001549static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1550{
1551 struct snd_soc_pcm_runtime *fe = substream->private_data;
1552 struct snd_soc_dpcm *dpcm;
1553 u64 formats = ULLONG_MAX;
1554 int stream = substream->stream;
1555
1556 if (!fe->dai_link->dpcm_merged_format)
1557 return formats;
1558
1559 /*
1560 * It returns merged BE codec format
1561 * if FE want to use it (= dpcm_merged_format)
1562 */
1563
1564 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1565 struct snd_soc_pcm_runtime *be = dpcm->be;
1566 struct snd_soc_dai_driver *codec_dai_drv;
1567 struct snd_soc_pcm_stream *codec_stream;
1568 int i;
1569
1570 for (i = 0; i < be->num_codecs; i++) {
1571 codec_dai_drv = be->codec_dais[i]->driver;
1572 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1573 codec_stream = &codec_dai_drv->playback;
1574 else
1575 codec_stream = &codec_dai_drv->capture;
1576
1577 formats &= codec_stream->formats;
1578 }
1579 }
1580
1581 return formats;
1582}
1583
Mark Brown45c0a182012-05-09 21:46:27 +01001584static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001585{
1586 struct snd_pcm_runtime *runtime = substream->runtime;
1587 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1588 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1589 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001590 u64 format = dpcm_runtime_base_format(substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001591
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001592 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001593 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001594 else
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001595 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
Liam Girdwood01d75842012-04-25 12:12:49 +01001596}
1597
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001598static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1599
1600/* Set FE's runtime_update state; the state is protected via PCM stream lock
1601 * for avoiding the race with trigger callback.
1602 * If the state is unset and a trigger is pending while the previous operation,
1603 * process the pending trigger action here.
1604 */
1605static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1606 int stream, enum snd_soc_dpcm_update state)
1607{
1608 struct snd_pcm_substream *substream =
1609 snd_soc_dpcm_get_substream(fe, stream);
1610
1611 snd_pcm_stream_lock_irq(substream);
1612 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1613 dpcm_fe_dai_do_trigger(substream,
1614 fe->dpcm[stream].trigger_pending - 1);
1615 fe->dpcm[stream].trigger_pending = 0;
1616 }
1617 fe->dpcm[stream].runtime_update = state;
1618 snd_pcm_stream_unlock_irq(substream);
1619}
1620
Liam Girdwood01d75842012-04-25 12:12:49 +01001621static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1622{
1623 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1624 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1625 int stream = fe_substream->stream, ret = 0;
1626
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001627 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001628
1629 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1630 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001631 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001632 goto be_err;
1633 }
1634
Liam Girdwood103d84a2012-11-19 14:39:15 +00001635 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001636
1637 /* start the DAI frontend */
1638 ret = soc_pcm_open(fe_substream);
1639 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001640 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001641 goto unwind;
1642 }
1643
1644 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1645
1646 dpcm_set_fe_runtime(fe_substream);
1647 snd_pcm_limit_hw_rates(runtime);
1648
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001649 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001650 return 0;
1651
1652unwind:
1653 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1654be_err:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001655 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001656 return ret;
1657}
1658
Liam Girdwood23607022014-01-17 17:03:55 +00001659int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001660{
1661 struct snd_soc_dpcm *dpcm;
1662
1663 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1664 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1665
1666 struct snd_soc_pcm_runtime *be = dpcm->be;
1667 struct snd_pcm_substream *be_substream =
1668 snd_soc_dpcm_get_substream(be, stream);
1669
1670 /* is this op for this BE ? */
1671 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1672 continue;
1673
1674 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001675 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001676 stream ? "capture" : "playback",
1677 be->dpcm[stream].state);
1678
1679 if (--be->dpcm[stream].users != 0)
1680 continue;
1681
1682 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1683 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1684 continue;
1685
Liam Girdwood103d84a2012-11-19 14:39:15 +00001686 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001687 dpcm->fe->dai_link->name);
1688
1689 soc_pcm_close(be_substream);
1690 be_substream->runtime = NULL;
1691
1692 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1693 }
1694 return 0;
1695}
1696
1697static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1698{
1699 struct snd_soc_pcm_runtime *fe = substream->private_data;
1700 int stream = substream->stream;
1701
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001702 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001703
1704 /* shutdown the BEs */
1705 dpcm_be_dai_shutdown(fe, substream->stream);
1706
Liam Girdwood103d84a2012-11-19 14:39:15 +00001707 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001708
1709 /* now shutdown the frontend */
1710 soc_pcm_close(substream);
1711
1712 /* run the stream event for each BE */
1713 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1714
1715 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001716 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001717 return 0;
1718}
1719
Liam Girdwood23607022014-01-17 17:03:55 +00001720int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001721{
1722 struct snd_soc_dpcm *dpcm;
1723
1724 /* only hw_params backends that are either sinks or sources
1725 * to this frontend DAI */
1726 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1727
1728 struct snd_soc_pcm_runtime *be = dpcm->be;
1729 struct snd_pcm_substream *be_substream =
1730 snd_soc_dpcm_get_substream(be, stream);
1731
1732 /* is this op for this BE ? */
1733 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1734 continue;
1735
1736 /* only free hw when no longer used - check all FEs */
1737 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1738 continue;
1739
Qiao Zhou36fba622014-12-03 10:13:43 +08001740 /* do not free hw if this BE is used by other FE */
1741 if (be->dpcm[stream].users > 1)
1742 continue;
1743
Liam Girdwood01d75842012-04-25 12:12:49 +01001744 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1745 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1746 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001747 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001748 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1749 continue;
1750
Liam Girdwood103d84a2012-11-19 14:39:15 +00001751 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001752 dpcm->fe->dai_link->name);
1753
1754 soc_pcm_hw_free(be_substream);
1755
1756 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1757 }
1758
1759 return 0;
1760}
1761
Mark Brown45c0a182012-05-09 21:46:27 +01001762static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001763{
1764 struct snd_soc_pcm_runtime *fe = substream->private_data;
1765 int err, stream = substream->stream;
1766
1767 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001768 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001769
Liam Girdwood103d84a2012-11-19 14:39:15 +00001770 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001771
1772 /* call hw_free on the frontend */
1773 err = soc_pcm_hw_free(substream);
1774 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001775 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001776 fe->dai_link->name);
1777
1778 /* only hw_params backends that are either sinks or sources
1779 * to this frontend DAI */
1780 err = dpcm_be_dai_hw_free(fe, stream);
1781
1782 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001783 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001784
1785 mutex_unlock(&fe->card->mutex);
1786 return 0;
1787}
1788
Liam Girdwood23607022014-01-17 17:03:55 +00001789int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001790{
1791 struct snd_soc_dpcm *dpcm;
1792 int ret;
1793
1794 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1795
1796 struct snd_soc_pcm_runtime *be = dpcm->be;
1797 struct snd_pcm_substream *be_substream =
1798 snd_soc_dpcm_get_substream(be, stream);
1799
1800 /* is this op for this BE ? */
1801 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1802 continue;
1803
1804 /* only allow hw_params() if no connected FEs are running */
1805 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1806 continue;
1807
1808 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1809 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1810 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1811 continue;
1812
Liam Girdwood103d84a2012-11-19 14:39:15 +00001813 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001814 dpcm->fe->dai_link->name);
1815
1816 /* copy params for each dpcm */
1817 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1818 sizeof(struct snd_pcm_hw_params));
1819
1820 /* perform any hw_params fixups */
1821 if (be->dai_link->be_hw_params_fixup) {
1822 ret = be->dai_link->be_hw_params_fixup(be,
1823 &dpcm->hw_params);
1824 if (ret < 0) {
1825 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001826 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001827 ret);
1828 goto unwind;
1829 }
1830 }
1831
1832 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1833 if (ret < 0) {
1834 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001835 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001836 goto unwind;
1837 }
1838
1839 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1840 }
1841 return 0;
1842
1843unwind:
1844 /* disable any enabled and non active backends */
1845 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1846 struct snd_soc_pcm_runtime *be = dpcm->be;
1847 struct snd_pcm_substream *be_substream =
1848 snd_soc_dpcm_get_substream(be, stream);
1849
1850 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1851 continue;
1852
1853 /* only allow hw_free() if no connected FEs are running */
1854 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1855 continue;
1856
1857 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1858 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1859 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1860 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1861 continue;
1862
1863 soc_pcm_hw_free(be_substream);
1864 }
1865
1866 return ret;
1867}
1868
Mark Brown45c0a182012-05-09 21:46:27 +01001869static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1870 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001871{
1872 struct snd_soc_pcm_runtime *fe = substream->private_data;
1873 int ret, stream = substream->stream;
1874
1875 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001876 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001877
1878 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1879 sizeof(struct snd_pcm_hw_params));
1880 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1881 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001882 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001883 goto out;
1884 }
1885
Liam Girdwood103d84a2012-11-19 14:39:15 +00001886 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001887 fe->dai_link->name, params_rate(params),
1888 params_channels(params), params_format(params));
1889
1890 /* call hw_params on the frontend */
1891 ret = soc_pcm_hw_params(substream, params);
1892 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001893 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001894 dpcm_be_dai_hw_free(fe, stream);
1895 } else
1896 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1897
1898out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001899 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001900 mutex_unlock(&fe->card->mutex);
1901 return ret;
1902}
1903
1904static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1905 struct snd_pcm_substream *substream, int cmd)
1906{
1907 int ret;
1908
Liam Girdwood103d84a2012-11-19 14:39:15 +00001909 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001910 dpcm->fe->dai_link->name, cmd);
1911
1912 ret = soc_pcm_trigger(substream, cmd);
1913 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001914 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001915
1916 return ret;
1917}
1918
Liam Girdwood23607022014-01-17 17:03:55 +00001919int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01001920 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001921{
1922 struct snd_soc_dpcm *dpcm;
1923 int ret = 0;
1924
1925 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1926
1927 struct snd_soc_pcm_runtime *be = dpcm->be;
1928 struct snd_pcm_substream *be_substream =
1929 snd_soc_dpcm_get_substream(be, stream);
1930
1931 /* is this op for this BE ? */
1932 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1933 continue;
1934
1935 switch (cmd) {
1936 case SNDRV_PCM_TRIGGER_START:
1937 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1938 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1939 continue;
1940
1941 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1942 if (ret)
1943 return ret;
1944
1945 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1946 break;
1947 case SNDRV_PCM_TRIGGER_RESUME:
1948 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1949 continue;
1950
1951 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1952 if (ret)
1953 return ret;
1954
1955 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1956 break;
1957 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1958 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1959 continue;
1960
1961 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1962 if (ret)
1963 return ret;
1964
1965 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1966 break;
1967 case SNDRV_PCM_TRIGGER_STOP:
1968 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1969 continue;
1970
1971 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1972 continue;
1973
1974 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1975 if (ret)
1976 return ret;
1977
1978 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1979 break;
1980 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08001981 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01001982 continue;
1983
1984 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1985 continue;
1986
1987 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1988 if (ret)
1989 return ret;
1990
1991 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1992 break;
1993 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1994 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1995 continue;
1996
1997 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1998 continue;
1999
2000 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2001 if (ret)
2002 return ret;
2003
2004 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2005 break;
2006 }
2007 }
2008
2009 return ret;
2010}
2011EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2012
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002013static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002014{
2015 struct snd_soc_pcm_runtime *fe = substream->private_data;
2016 int stream = substream->stream, ret;
2017 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2018
2019 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2020
2021 switch (trigger) {
2022 case SND_SOC_DPCM_TRIGGER_PRE:
2023 /* call trigger on the frontend before the backend. */
2024
Liam Girdwood103d84a2012-11-19 14:39:15 +00002025 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002026 fe->dai_link->name, cmd);
2027
2028 ret = soc_pcm_trigger(substream, cmd);
2029 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002030 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002031 goto out;
2032 }
2033
2034 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2035 break;
2036 case SND_SOC_DPCM_TRIGGER_POST:
2037 /* call trigger on the frontend after the backend. */
2038
2039 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2040 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002041 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002042 goto out;
2043 }
2044
Liam Girdwood103d84a2012-11-19 14:39:15 +00002045 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002046 fe->dai_link->name, cmd);
2047
2048 ret = soc_pcm_trigger(substream, cmd);
2049 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002050 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2051 /* bespoke trigger() - handles both FE and BEs */
2052
Liam Girdwood103d84a2012-11-19 14:39:15 +00002053 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002054 fe->dai_link->name, cmd);
2055
2056 ret = soc_pcm_bespoke_trigger(substream, cmd);
2057 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002058 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002059 goto out;
2060 }
2061 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002062 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00002063 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01002064 fe->dai_link->name);
2065 ret = -EINVAL;
2066 goto out;
2067 }
2068
2069 switch (cmd) {
2070 case SNDRV_PCM_TRIGGER_START:
2071 case SNDRV_PCM_TRIGGER_RESUME:
2072 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2073 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2074 break;
2075 case SNDRV_PCM_TRIGGER_STOP:
2076 case SNDRV_PCM_TRIGGER_SUSPEND:
2077 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2078 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2079 break;
2080 }
2081
2082out:
2083 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2084 return ret;
2085}
2086
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002087static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2088{
2089 struct snd_soc_pcm_runtime *fe = substream->private_data;
2090 int stream = substream->stream;
2091
2092 /* if FE's runtime_update is already set, we're in race;
2093 * process this trigger later at exit
2094 */
2095 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2096 fe->dpcm[stream].trigger_pending = cmd + 1;
2097 return 0; /* delayed, assuming it's successful */
2098 }
2099
2100 /* we're alone, let's trigger */
2101 return dpcm_fe_dai_do_trigger(substream, cmd);
2102}
2103
Liam Girdwood23607022014-01-17 17:03:55 +00002104int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002105{
2106 struct snd_soc_dpcm *dpcm;
2107 int ret = 0;
2108
2109 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2110
2111 struct snd_soc_pcm_runtime *be = dpcm->be;
2112 struct snd_pcm_substream *be_substream =
2113 snd_soc_dpcm_get_substream(be, stream);
2114
2115 /* is this op for this BE ? */
2116 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2117 continue;
2118
2119 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2120 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2121 continue;
2122
Liam Girdwood103d84a2012-11-19 14:39:15 +00002123 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002124 dpcm->fe->dai_link->name);
2125
2126 ret = soc_pcm_prepare(be_substream);
2127 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002128 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002129 ret);
2130 break;
2131 }
2132
2133 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2134 }
2135 return ret;
2136}
2137
Mark Brown45c0a182012-05-09 21:46:27 +01002138static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002139{
2140 struct snd_soc_pcm_runtime *fe = substream->private_data;
2141 int stream = substream->stream, ret = 0;
2142
2143 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2144
Liam Girdwood103d84a2012-11-19 14:39:15 +00002145 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002146
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002147 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002148
2149 /* there is no point preparing this FE if there are no BEs */
2150 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002151 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002152 fe->dai_link->name);
2153 ret = -EINVAL;
2154 goto out;
2155 }
2156
2157 ret = dpcm_be_dai_prepare(fe, substream->stream);
2158 if (ret < 0)
2159 goto out;
2160
2161 /* call prepare on the frontend */
2162 ret = soc_pcm_prepare(substream);
2163 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002164 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002165 fe->dai_link->name);
2166 goto out;
2167 }
2168
2169 /* run the stream event for each BE */
2170 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2171 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2172
2173out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002174 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002175 mutex_unlock(&fe->card->mutex);
2176
2177 return ret;
2178}
2179
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002180static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2181 unsigned int cmd, void *arg)
2182{
2183 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2184 struct snd_soc_platform *platform = rtd->platform;
2185
Mark Brownc5914b02013-10-30 17:47:39 -07002186 if (platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002187 return platform->driver->ops->ioctl(substream, cmd, arg);
2188 return snd_pcm_lib_ioctl(substream, cmd, arg);
2189}
2190
Liam Girdwood618dae12012-04-25 12:12:51 +01002191static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2192{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002193 struct snd_pcm_substream *substream =
2194 snd_soc_dpcm_get_substream(fe, stream);
2195 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002196 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01002197
Liam Girdwood103d84a2012-11-19 14:39:15 +00002198 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002199 stream ? "capture" : "playback", fe->dai_link->name);
2200
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002201 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2202 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002203 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002204 fe->dai_link->name);
2205
2206 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2207 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002208 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002209 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002210 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002211 fe->dai_link->name);
2212
2213 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2214 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002215 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002216 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002217
2218 err = dpcm_be_dai_hw_free(fe, stream);
2219 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002220 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002221
2222 err = dpcm_be_dai_shutdown(fe, stream);
2223 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002224 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002225
2226 /* run the stream event for each BE */
2227 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2228
2229 return 0;
2230}
2231
2232static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2233{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002234 struct snd_pcm_substream *substream =
2235 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002236 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002237 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002238 int ret;
2239
Liam Girdwood103d84a2012-11-19 14:39:15 +00002240 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002241 stream ? "capture" : "playback", fe->dai_link->name);
2242
2243 /* Only start the BE if the FE is ready */
2244 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2245 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2246 return -EINVAL;
2247
2248 /* startup must always be called for new BEs */
2249 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002250 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002251 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01002252
2253 /* keep going if FE state is > open */
2254 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2255 return 0;
2256
2257 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002258 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002259 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01002260
2261 /* keep going if FE state is > hw_params */
2262 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2263 return 0;
2264
2265
2266 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002267 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002268 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002269
2270 /* run the stream event for each BE */
2271 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2272
2273 /* keep going if FE state is > prepare */
2274 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2275 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2276 return 0;
2277
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002278 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2279 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002280 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002281 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01002282
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002283 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2284 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002285 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002286 goto hw_free;
2287 }
2288 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002289 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002290 fe->dai_link->name);
2291
2292 ret = dpcm_be_dai_trigger(fe, stream,
2293 SNDRV_PCM_TRIGGER_START);
2294 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002295 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002296 goto hw_free;
2297 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002298 }
2299
2300 return 0;
2301
2302hw_free:
2303 dpcm_be_dai_hw_free(fe, stream);
2304close:
2305 dpcm_be_dai_shutdown(fe, stream);
2306disconnect:
2307 /* disconnect any non started BEs */
2308 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2309 struct snd_soc_pcm_runtime *be = dpcm->be;
2310 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2311 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2312 }
2313
2314 return ret;
2315}
2316
2317static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2318{
2319 int ret;
2320
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002321 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002322 ret = dpcm_run_update_startup(fe, stream);
2323 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002324 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002325 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002326
2327 return ret;
2328}
2329
2330static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2331{
2332 int ret;
2333
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002334 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002335 ret = dpcm_run_update_shutdown(fe, stream);
2336 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002337 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002338 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002339
2340 return ret;
2341}
2342
2343/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2344 * any DAI links.
2345 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002346int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002347{
Liam Girdwood618dae12012-04-25 12:12:51 +01002348 int i, old, new, paths;
2349
Liam Girdwood618dae12012-04-25 12:12:51 +01002350 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2351 for (i = 0; i < card->num_rtd; i++) {
2352 struct snd_soc_dapm_widget_list *list;
2353 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2354
2355 /* make sure link is FE */
2356 if (!fe->dai_link->dynamic)
2357 continue;
2358
2359 /* only check active links */
2360 if (!fe->cpu_dai->active)
2361 continue;
2362
2363 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002364 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002365 fe->dai_link->name);
2366
2367 /* skip if FE doesn't have playback capability */
Qiao Zhou075207d2014-11-17 16:02:57 +08002368 if (!fe->cpu_dai->driver->playback.channels_min
2369 || !fe->codec_dai->driver->playback.channels_min)
2370 goto capture;
2371
2372 /* skip if FE isn't currently playing */
2373 if (!fe->cpu_dai->playback_active
2374 || !fe->codec_dai->playback_active)
Liam Girdwood618dae12012-04-25 12:12:51 +01002375 goto capture;
2376
2377 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2378 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002379 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002380 fe->dai_link->name, "playback");
2381 mutex_unlock(&card->mutex);
2382 return paths;
2383 }
2384
2385 /* update any new playback paths */
2386 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2387 if (new) {
2388 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2389 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2390 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2391 }
2392
2393 /* update any old playback paths */
2394 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2395 if (old) {
2396 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2397 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2398 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2399 }
2400
Qiao Zhou7ed9de72014-06-04 19:42:06 +08002401 dpcm_path_put(&list);
Liam Girdwood618dae12012-04-25 12:12:51 +01002402capture:
2403 /* skip if FE doesn't have capture capability */
Qiao Zhou075207d2014-11-17 16:02:57 +08002404 if (!fe->cpu_dai->driver->capture.channels_min
2405 || !fe->codec_dai->driver->capture.channels_min)
2406 continue;
2407
2408 /* skip if FE isn't currently capturing */
2409 if (!fe->cpu_dai->capture_active
2410 || !fe->codec_dai->capture_active)
Liam Girdwood618dae12012-04-25 12:12:51 +01002411 continue;
2412
2413 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2414 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002415 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002416 fe->dai_link->name, "capture");
2417 mutex_unlock(&card->mutex);
2418 return paths;
2419 }
2420
2421 /* update any new capture paths */
2422 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2423 if (new) {
2424 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2425 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2426 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2427 }
2428
2429 /* update any old capture paths */
2430 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2431 if (old) {
2432 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2433 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2434 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2435 }
2436
2437 dpcm_path_put(&list);
2438 }
2439
2440 mutex_unlock(&card->mutex);
2441 return 0;
2442}
Liam Girdwood01d75842012-04-25 12:12:49 +01002443int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2444{
2445 struct snd_soc_dpcm *dpcm;
2446 struct list_head *clients =
2447 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2448
2449 list_for_each_entry(dpcm, clients, list_be) {
2450
2451 struct snd_soc_pcm_runtime *be = dpcm->be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002452 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01002453
2454 if (be->dai_link->ignore_suspend)
2455 continue;
2456
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002457 for (i = 0; i < be->num_codecs; i++) {
2458 struct snd_soc_dai *dai = be->codec_dais[i];
2459 struct snd_soc_dai_driver *drv = dai->driver;
Liam Girdwood01d75842012-04-25 12:12:49 +01002460
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002461 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2462 be->dai_link->name);
2463
2464 if (drv->ops && drv->ops->digital_mute &&
2465 dai->playback_active)
2466 drv->ops->digital_mute(dai, mute);
2467 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002468 }
2469
2470 return 0;
2471}
2472
Mark Brown45c0a182012-05-09 21:46:27 +01002473static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002474{
2475 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2476 struct snd_soc_dpcm *dpcm;
2477 struct snd_soc_dapm_widget_list *list;
2478 int ret;
2479 int stream = fe_substream->stream;
2480
2481 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2482 fe->dpcm[stream].runtime = fe_substream->runtime;
2483
Qiao Zhou8f70e512014-09-10 17:54:07 +08002484 ret = dpcm_path_get(fe, stream, &list);
2485 if (ret < 0) {
2486 mutex_unlock(&fe->card->mutex);
2487 return ret;
2488 } else if (ret == 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002489 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002490 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002491 }
2492
2493 /* calculate valid and active FE <-> BE dpcms */
2494 dpcm_process_paths(fe, stream, &list, 1);
2495
2496 ret = dpcm_fe_dai_startup(fe_substream);
2497 if (ret < 0) {
2498 /* clean up all links */
2499 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2500 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2501
2502 dpcm_be_disconnect(fe, stream);
2503 fe->dpcm[stream].runtime = NULL;
2504 }
2505
2506 dpcm_clear_pending_state(fe, stream);
2507 dpcm_path_put(&list);
2508 mutex_unlock(&fe->card->mutex);
2509 return ret;
2510}
2511
Mark Brown45c0a182012-05-09 21:46:27 +01002512static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002513{
2514 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2515 struct snd_soc_dpcm *dpcm;
2516 int stream = fe_substream->stream, ret;
2517
2518 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2519 ret = dpcm_fe_dai_shutdown(fe_substream);
2520
2521 /* mark FE's links ready to prune */
2522 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2523 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2524
2525 dpcm_be_disconnect(fe, stream);
2526
2527 fe->dpcm[stream].runtime = NULL;
2528 mutex_unlock(&fe->card->mutex);
2529 return ret;
2530}
2531
Liam Girdwoodddee6272011-06-09 14:45:53 +01002532/* create a new pcm */
2533int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2534{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002535 struct snd_soc_platform *platform = rtd->platform;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002536 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002537 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2538 struct snd_pcm *pcm;
2539 char new_name[64];
2540 int ret = 0, playback = 0, capture = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002541 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002542
Liam Girdwood01d75842012-04-25 12:12:49 +01002543 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002544 playback = rtd->dai_link->dpcm_playback;
2545 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002546 } else {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002547 for (i = 0; i < rtd->num_codecs; i++) {
2548 codec_dai = rtd->codec_dais[i];
2549 if (codec_dai->driver->playback.channels_min)
2550 playback = 1;
2551 if (codec_dai->driver->capture.channels_min)
2552 capture = 1;
2553 }
2554
2555 capture = capture && cpu_dai->driver->capture.channels_min;
2556 playback = playback && cpu_dai->driver->playback.channels_min;
Liam Girdwood01d75842012-04-25 12:12:49 +01002557 }
Sangsu Parka5002312012-01-02 17:15:10 +09002558
Fabio Estevamd6bead02013-08-29 10:32:13 -03002559 if (rtd->dai_link->playback_only) {
2560 playback = 1;
2561 capture = 0;
2562 }
2563
2564 if (rtd->dai_link->capture_only) {
2565 playback = 0;
2566 capture = 1;
2567 }
2568
Liam Girdwood01d75842012-04-25 12:12:49 +01002569 /* create the PCM */
2570 if (rtd->dai_link->no_pcm) {
2571 snprintf(new_name, sizeof(new_name), "(%s)",
2572 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002573
Liam Girdwood01d75842012-04-25 12:12:49 +01002574 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2575 playback, capture, &pcm);
2576 } else {
2577 if (rtd->dai_link->dynamic)
2578 snprintf(new_name, sizeof(new_name), "%s (*)",
2579 rtd->dai_link->stream_name);
2580 else
2581 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002582 rtd->dai_link->stream_name,
2583 (rtd->num_codecs > 1) ?
2584 "multicodec" : rtd->codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002585
Liam Girdwood01d75842012-04-25 12:12:49 +01002586 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2587 capture, &pcm);
2588 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002589 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002590 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002591 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002592 return ret;
2593 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002594 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002595
2596 /* DAPM dai link stream work */
2597 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2598
Vinod Koul48c76992015-02-12 09:59:53 +05302599 pcm->nonatomic = rtd->dai_link->nonatomic;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002600 rtd->pcm = pcm;
2601 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002602
2603 if (rtd->dai_link->no_pcm) {
2604 if (playback)
2605 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2606 if (capture)
2607 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2608 goto out;
2609 }
2610
2611 /* ASoC PCM operations */
2612 if (rtd->dai_link->dynamic) {
2613 rtd->ops.open = dpcm_fe_dai_open;
2614 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2615 rtd->ops.prepare = dpcm_fe_dai_prepare;
2616 rtd->ops.trigger = dpcm_fe_dai_trigger;
2617 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2618 rtd->ops.close = dpcm_fe_dai_close;
2619 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002620 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002621 } else {
2622 rtd->ops.open = soc_pcm_open;
2623 rtd->ops.hw_params = soc_pcm_hw_params;
2624 rtd->ops.prepare = soc_pcm_prepare;
2625 rtd->ops.trigger = soc_pcm_trigger;
2626 rtd->ops.hw_free = soc_pcm_hw_free;
2627 rtd->ops.close = soc_pcm_close;
2628 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002629 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002630 }
2631
Liam Girdwoodddee6272011-06-09 14:45:53 +01002632 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002633 rtd->ops.ack = platform->driver->ops->ack;
2634 rtd->ops.copy = platform->driver->ops->copy;
2635 rtd->ops.silence = platform->driver->ops->silence;
2636 rtd->ops.page = platform->driver->ops->page;
2637 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002638 }
2639
2640 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002641 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002642
2643 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002644 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002645
2646 if (platform->driver->pcm_new) {
2647 ret = platform->driver->pcm_new(rtd);
2648 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002649 dev_err(platform->dev,
2650 "ASoC: pcm constructor failed: %d\n",
2651 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002652 return ret;
2653 }
2654 }
2655
2656 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002657out:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002658 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2659 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2660 cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002661 return ret;
2662}
Liam Girdwood01d75842012-04-25 12:12:49 +01002663
2664/* is the current PCM operation for this FE ? */
2665int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2666{
2667 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2668 return 1;
2669 return 0;
2670}
2671EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2672
2673/* is the current PCM operation for this BE ? */
2674int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2675 struct snd_soc_pcm_runtime *be, int stream)
2676{
2677 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2678 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2679 be->dpcm[stream].runtime_update))
2680 return 1;
2681 return 0;
2682}
2683EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2684
2685/* get the substream for this BE */
2686struct snd_pcm_substream *
2687 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2688{
2689 return be->pcm->streams[stream].substream;
2690}
2691EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2692
2693/* get the BE runtime state */
2694enum snd_soc_dpcm_state
2695 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2696{
2697 return be->dpcm[stream].state;
2698}
2699EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2700
2701/* set the BE runtime state */
2702void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2703 int stream, enum snd_soc_dpcm_state state)
2704{
2705 be->dpcm[stream].state = state;
2706}
2707EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2708
2709/*
2710 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2711 * are not running, paused or suspended for the specified stream direction.
2712 */
2713int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2714 struct snd_soc_pcm_runtime *be, int stream)
2715{
2716 struct snd_soc_dpcm *dpcm;
2717 int state;
2718
2719 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2720
2721 if (dpcm->fe == fe)
2722 continue;
2723
2724 state = dpcm->fe->dpcm[stream].state;
2725 if (state == SND_SOC_DPCM_STATE_START ||
2726 state == SND_SOC_DPCM_STATE_PAUSED ||
2727 state == SND_SOC_DPCM_STATE_SUSPEND)
2728 return 0;
2729 }
2730
2731 /* it's safe to free/stop this BE DAI */
2732 return 1;
2733}
2734EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2735
2736/*
2737 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2738 * running, paused or suspended for the specified stream direction.
2739 */
2740int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2741 struct snd_soc_pcm_runtime *be, int stream)
2742{
2743 struct snd_soc_dpcm *dpcm;
2744 int state;
2745
2746 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2747
2748 if (dpcm->fe == fe)
2749 continue;
2750
2751 state = dpcm->fe->dpcm[stream].state;
2752 if (state == SND_SOC_DPCM_STATE_START ||
2753 state == SND_SOC_DPCM_STATE_PAUSED ||
2754 state == SND_SOC_DPCM_STATE_SUSPEND ||
2755 state == SND_SOC_DPCM_STATE_PREPARE)
2756 return 0;
2757 }
2758
2759 /* it's safe to change hw_params */
2760 return 1;
2761}
2762EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002763
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002764int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2765 int cmd, struct snd_soc_platform *platform)
2766{
Mark Brownc5914b02013-10-30 17:47:39 -07002767 if (platform->driver->ops && platform->driver->ops->trigger)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002768 return platform->driver->ops->trigger(substream, cmd);
2769 return 0;
2770}
2771EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2772
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002773#ifdef CONFIG_DEBUG_FS
2774static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2775{
2776 switch (state) {
2777 case SND_SOC_DPCM_STATE_NEW:
2778 return "new";
2779 case SND_SOC_DPCM_STATE_OPEN:
2780 return "open";
2781 case SND_SOC_DPCM_STATE_HW_PARAMS:
2782 return "hw_params";
2783 case SND_SOC_DPCM_STATE_PREPARE:
2784 return "prepare";
2785 case SND_SOC_DPCM_STATE_START:
2786 return "start";
2787 case SND_SOC_DPCM_STATE_STOP:
2788 return "stop";
2789 case SND_SOC_DPCM_STATE_SUSPEND:
2790 return "suspend";
2791 case SND_SOC_DPCM_STATE_PAUSED:
2792 return "paused";
2793 case SND_SOC_DPCM_STATE_HW_FREE:
2794 return "hw_free";
2795 case SND_SOC_DPCM_STATE_CLOSE:
2796 return "close";
2797 }
2798
2799 return "unknown";
2800}
2801
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002802static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2803 int stream, char *buf, size_t size)
2804{
2805 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2806 struct snd_soc_dpcm *dpcm;
2807 ssize_t offset = 0;
2808
2809 /* FE state */
2810 offset += snprintf(buf + offset, size - offset,
2811 "[%s - %s]\n", fe->dai_link->name,
2812 stream ? "Capture" : "Playback");
2813
2814 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2815 dpcm_state_string(fe->dpcm[stream].state));
2816
2817 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2818 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2819 offset += snprintf(buf + offset, size - offset,
2820 "Hardware Params: "
2821 "Format = %s, Channels = %d, Rate = %d\n",
2822 snd_pcm_format_name(params_format(params)),
2823 params_channels(params),
2824 params_rate(params));
2825
2826 /* BEs state */
2827 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2828
2829 if (list_empty(&fe->dpcm[stream].be_clients)) {
2830 offset += snprintf(buf + offset, size - offset,
2831 " No active DSP links\n");
2832 goto out;
2833 }
2834
2835 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2836 struct snd_soc_pcm_runtime *be = dpcm->be;
2837 params = &dpcm->hw_params;
2838
2839 offset += snprintf(buf + offset, size - offset,
2840 "- %s\n", be->dai_link->name);
2841
2842 offset += snprintf(buf + offset, size - offset,
2843 " State: %s\n",
2844 dpcm_state_string(be->dpcm[stream].state));
2845
2846 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2847 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2848 offset += snprintf(buf + offset, size - offset,
2849 " Hardware Params: "
2850 "Format = %s, Channels = %d, Rate = %d\n",
2851 snd_pcm_format_name(params_format(params)),
2852 params_channels(params),
2853 params_rate(params));
2854 }
2855
2856out:
2857 return offset;
2858}
2859
2860static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2861 size_t count, loff_t *ppos)
2862{
2863 struct snd_soc_pcm_runtime *fe = file->private_data;
2864 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2865 char *buf;
2866
2867 buf = kmalloc(out_count, GFP_KERNEL);
2868 if (!buf)
2869 return -ENOMEM;
2870
2871 if (fe->cpu_dai->driver->playback.channels_min)
2872 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2873 buf + offset, out_count - offset);
2874
2875 if (fe->cpu_dai->driver->capture.channels_min)
2876 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2877 buf + offset, out_count - offset);
2878
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002879 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002880
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002881 kfree(buf);
2882 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002883}
2884
2885static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002886 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002887 .read = dpcm_state_read_file,
2888 .llseek = default_llseek,
2889};
2890
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02002891void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002892{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002893 if (!rtd->dai_link)
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02002894 return;
Mark Brownb3bba9a2012-05-08 10:33:47 +01002895
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02002896 if (!rtd->card->debugfs_card_root)
2897 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002898
2899 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2900 rtd->card->debugfs_card_root);
2901 if (!rtd->debugfs_dpcm_root) {
2902 dev_dbg(rtd->dev,
2903 "ASoC: Failed to create dpcm debugfs directory %s\n",
2904 rtd->dai_link->name);
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02002905 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002906 }
2907
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002908 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002909 rtd->debugfs_dpcm_root,
2910 rtd, &dpcm_state_fops);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002911}
2912#endif