blob: 1e5beef6dbccdb059e303363c75716809d607c37 [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
Lars-Peter Clausen90996f42013-05-14 11:05:30 +020037/**
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010038 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
39 * @rtd: ASoC PCM runtime that is activated
40 * @stream: Direction of the PCM stream
41 *
42 * Increments the active count for all the DAIs and components attached to a PCM
43 * runtime. Should typically be called when a stream is opened.
44 *
45 * Must be called with the rtd->pcm_mutex being held
46 */
47void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
48{
49 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020050 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010051
52 lockdep_assert_held(&rtd->pcm_mutex);
53
54 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
55 cpu_dai->playback_active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020056 for (i = 0; i < rtd->num_codecs; i++)
57 rtd->codec_dais[i]->playback_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010058 } else {
59 cpu_dai->capture_active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020060 for (i = 0; i < rtd->num_codecs; i++)
61 rtd->codec_dais[i]->capture_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010062 }
63
64 cpu_dai->active++;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +010065 cpu_dai->component->active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020066 for (i = 0; i < rtd->num_codecs; i++) {
67 rtd->codec_dais[i]->active++;
68 rtd->codec_dais[i]->component->active++;
69 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010070}
71
72/**
73 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
74 * @rtd: ASoC PCM runtime that is deactivated
75 * @stream: Direction of the PCM stream
76 *
77 * Decrements the active count for all the DAIs and components attached to a PCM
78 * runtime. Should typically be called when a stream is closed.
79 *
80 * Must be called with the rtd->pcm_mutex being held
81 */
82void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
83{
84 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020085 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010086
87 lockdep_assert_held(&rtd->pcm_mutex);
88
89 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
90 cpu_dai->playback_active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020091 for (i = 0; i < rtd->num_codecs; i++)
92 rtd->codec_dais[i]->playback_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010093 } else {
94 cpu_dai->capture_active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020095 for (i = 0; i < rtd->num_codecs; i++)
96 rtd->codec_dais[i]->capture_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010097 }
98
99 cpu_dai->active--;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +0100100 cpu_dai->component->active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200101 for (i = 0; i < rtd->num_codecs; i++) {
102 rtd->codec_dais[i]->component->active--;
103 rtd->codec_dais[i]->active--;
104 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100105}
106
107/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100108 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
109 * @rtd: The ASoC PCM runtime that should be checked.
110 *
111 * This function checks whether the power down delay should be ignored for a
112 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
113 * been configured to ignore the delay, or if none of the components benefits
114 * from having the delay.
115 */
116bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
117{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200118 int i;
119 bool ignore = true;
120
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100121 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
122 return true;
123
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200124 for (i = 0; i < rtd->num_codecs; i++)
125 ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
126
127 return rtd->cpu_dai->component->ignore_pmdown_time && ignore;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100128}
129
130/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200131 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
132 * @substream: the pcm substream
133 * @hw: the hardware parameters
134 *
135 * Sets the substream runtime hardware parameters.
136 */
137int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
138 const struct snd_pcm_hardware *hw)
139{
140 struct snd_pcm_runtime *runtime = substream->runtime;
141 runtime->hw.info = hw->info;
142 runtime->hw.formats = hw->formats;
143 runtime->hw.period_bytes_min = hw->period_bytes_min;
144 runtime->hw.period_bytes_max = hw->period_bytes_max;
145 runtime->hw.periods_min = hw->periods_min;
146 runtime->hw.periods_max = hw->periods_max;
147 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
148 runtime->hw.fifo_size = hw->fifo_size;
149 return 0;
150}
151EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
152
Liam Girdwood01d75842012-04-25 12:12:49 +0100153/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000154int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100155 int event)
156{
157 struct snd_soc_dpcm *dpcm;
158
159 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
160
161 struct snd_soc_pcm_runtime *be = dpcm->be;
162
Liam Girdwood103d84a2012-11-19 14:39:15 +0000163 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100164 be->dai_link->name, event, dir);
165
166 snd_soc_dapm_stream_event(be, dir, event);
167 }
168
169 snd_soc_dapm_stream_event(fe, dir, event);
170
171 return 0;
172}
173
Dong Aisheng17841022011-08-29 17:15:14 +0800174static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
175 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100176{
177 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100178 int ret;
179
Nicolin Chen3635bf02013-11-13 18:56:24 +0800180 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
181 rtd->dai_link->symmetric_rates)) {
182 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
183 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100184
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200185 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800186 SNDRV_PCM_HW_PARAM_RATE,
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200187 soc_dai->rate);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800188 if (ret < 0) {
189 dev_err(soc_dai->dev,
190 "ASoC: Unable to apply rate constraint: %d\n",
191 ret);
192 return ret;
193 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100194 }
195
Nicolin Chen3635bf02013-11-13 18:56:24 +0800196 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
197 rtd->dai_link->symmetric_channels)) {
198 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
199 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100200
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200201 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800202 SNDRV_PCM_HW_PARAM_CHANNELS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800203 soc_dai->channels);
204 if (ret < 0) {
205 dev_err(soc_dai->dev,
206 "ASoC: Unable to apply channel symmetry constraint: %d\n",
207 ret);
208 return ret;
209 }
210 }
211
212 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
213 rtd->dai_link->symmetric_samplebits)) {
214 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
215 soc_dai->sample_bits);
216
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200217 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800218 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800219 soc_dai->sample_bits);
220 if (ret < 0) {
221 dev_err(soc_dai->dev,
222 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
223 ret);
224 return ret;
225 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100226 }
227
228 return 0;
229}
230
Nicolin Chen3635bf02013-11-13 18:56:24 +0800231static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
232 struct snd_pcm_hw_params *params)
233{
234 struct snd_soc_pcm_runtime *rtd = substream->private_data;
235 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200236 unsigned int rate, channels, sample_bits, symmetry, i;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800237
238 rate = params_rate(params);
239 channels = params_channels(params);
240 sample_bits = snd_pcm_format_physical_width(params_format(params));
241
242 /* reject unmatched parameters when applying symmetry */
243 symmetry = cpu_dai->driver->symmetric_rates ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800244 rtd->dai_link->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200245
246 for (i = 0; i < rtd->num_codecs; i++)
247 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
248
Nicolin Chen3635bf02013-11-13 18:56:24 +0800249 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
250 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
251 cpu_dai->rate, rate);
252 return -EINVAL;
253 }
254
255 symmetry = cpu_dai->driver->symmetric_channels ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800256 rtd->dai_link->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200257
258 for (i = 0; i < rtd->num_codecs; i++)
259 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
260
Nicolin Chen3635bf02013-11-13 18:56:24 +0800261 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
262 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
263 cpu_dai->channels, channels);
264 return -EINVAL;
265 }
266
267 symmetry = cpu_dai->driver->symmetric_samplebits ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800268 rtd->dai_link->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200269
270 for (i = 0; i < rtd->num_codecs; i++)
271 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
272
Nicolin Chen3635bf02013-11-13 18:56:24 +0800273 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
274 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
275 cpu_dai->sample_bits, sample_bits);
276 return -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100277 }
278
279 return 0;
280}
281
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100282static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
283{
284 struct snd_soc_pcm_runtime *rtd = substream->private_data;
285 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100286 struct snd_soc_dai_link *link = rtd->dai_link;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200287 unsigned int symmetry, i;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100288
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200289 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
290 cpu_driver->symmetric_channels || link->symmetric_channels ||
291 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
292
293 for (i = 0; i < rtd->num_codecs; i++)
294 symmetry = symmetry ||
295 rtd->codec_dais[i]->driver->symmetric_rates ||
296 rtd->codec_dais[i]->driver->symmetric_channels ||
297 rtd->codec_dais[i]->driver->symmetric_samplebits;
298
299 return symmetry;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100300}
301
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200302static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000303{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200304 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Takashi Iwaic6068d32014-12-31 17:10:34 +0100305 int ret;
Mark Brown58ba9b22012-01-16 18:38:51 +0000306
307 if (!bits)
308 return;
309
Lars-Peter Clausen0e2a3752014-12-29 18:43:38 +0100310 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
311 if (ret != 0)
312 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
313 bits, ret);
Mark Brown58ba9b22012-01-16 18:38:51 +0000314}
315
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200316static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200317{
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200318 struct snd_soc_pcm_runtime *rtd = substream->private_data;
319 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200320 struct snd_soc_dai *codec_dai;
321 int i;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200322 unsigned int bits = 0, cpu_bits;
323
324 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200325 for (i = 0; i < rtd->num_codecs; i++) {
326 codec_dai = rtd->codec_dais[i];
327 if (codec_dai->driver->playback.sig_bits == 0) {
328 bits = 0;
329 break;
330 }
331 bits = max(codec_dai->driver->playback.sig_bits, bits);
332 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200333 cpu_bits = cpu_dai->driver->playback.sig_bits;
334 } else {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200335 for (i = 0; i < rtd->num_codecs; i++) {
336 codec_dai = rtd->codec_dais[i];
Daniel Mack5e63dfc2014-10-07 14:33:46 +0200337 if (codec_dai->driver->capture.sig_bits == 0) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200338 bits = 0;
339 break;
340 }
341 bits = max(codec_dai->driver->capture.sig_bits, bits);
342 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200343 cpu_bits = cpu_dai->driver->capture.sig_bits;
344 }
345
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200346 soc_pcm_set_msb(substream, bits);
347 soc_pcm_set_msb(substream, cpu_bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200348}
349
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200350static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200351{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200352 struct snd_pcm_runtime *runtime = substream->runtime;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100353 struct snd_pcm_hardware *hw = &runtime->hw;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200354 struct snd_soc_pcm_runtime *rtd = substream->private_data;
355 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
356 struct snd_soc_dai_driver *codec_dai_drv;
357 struct snd_soc_pcm_stream *codec_stream;
358 struct snd_soc_pcm_stream *cpu_stream;
359 unsigned int chan_min = 0, chan_max = UINT_MAX;
360 unsigned int rate_min = 0, rate_max = UINT_MAX;
361 unsigned int rates = UINT_MAX;
362 u64 formats = ULLONG_MAX;
363 int i;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100364
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200365 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
366 cpu_stream = &cpu_dai_drv->playback;
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100367 else
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200368 cpu_stream = &cpu_dai_drv->capture;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100369
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200370 /* first calculate min/max only for CODECs in the DAI link */
371 for (i = 0; i < rtd->num_codecs; i++) {
372 codec_dai_drv = rtd->codec_dais[i]->driver;
373 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
374 codec_stream = &codec_dai_drv->playback;
375 else
376 codec_stream = &codec_dai_drv->capture;
377 chan_min = max(chan_min, codec_stream->channels_min);
378 chan_max = min(chan_max, codec_stream->channels_max);
379 rate_min = max(rate_min, codec_stream->rate_min);
380 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
381 formats &= codec_stream->formats;
382 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
383 }
384
385 /*
386 * chan min/max cannot be enforced if there are multiple CODEC DAIs
387 * connected to a single CPU DAI, use CPU DAI's directly and let
388 * channel allocation be fixed up later
389 */
390 if (rtd->num_codecs > 1) {
391 chan_min = cpu_stream->channels_min;
392 chan_max = cpu_stream->channels_max;
393 }
394
395 hw->channels_min = max(chan_min, cpu_stream->channels_min);
396 hw->channels_max = min(chan_max, cpu_stream->channels_max);
397 if (hw->formats)
398 hw->formats &= formats & cpu_stream->formats;
399 else
400 hw->formats = formats & cpu_stream->formats;
401 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100402
403 snd_pcm_limit_hw_rates(runtime);
404
405 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200406 hw->rate_min = max(hw->rate_min, rate_min);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100407 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200408 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200409}
410
Mark Brown58ba9b22012-01-16 18:38:51 +0000411/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100412 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
413 * then initialized and any private data can be allocated. This also calls
414 * startup for the cpu DAI, platform, machine and codec DAI.
415 */
416static int soc_pcm_open(struct snd_pcm_substream *substream)
417{
418 struct snd_soc_pcm_runtime *rtd = substream->private_data;
419 struct snd_pcm_runtime *runtime = substream->runtime;
420 struct snd_soc_platform *platform = rtd->platform;
421 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200422 struct snd_soc_dai *codec_dai;
423 const char *codec_dai_name = "multicodec";
424 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100425
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800426 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200427 for (i = 0; i < rtd->num_codecs; i++)
428 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000429 pm_runtime_get_sync(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200430 for (i = 0; i < rtd->num_codecs; i++)
431 pm_runtime_get_sync(rtd->codec_dais[i]->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000432 pm_runtime_get_sync(platform->dev);
433
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100434 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100435
436 /* startup the audio subsystem */
Mark Brownc5914b02013-10-30 17:47:39 -0700437 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100438 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
439 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000440 dev_err(cpu_dai->dev, "ASoC: can't open interface"
441 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100442 goto out;
443 }
444 }
445
446 if (platform->driver->ops && platform->driver->ops->open) {
447 ret = platform->driver->ops->open(substream);
448 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000449 dev_err(platform->dev, "ASoC: can't open platform"
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200450 " %s: %d\n", platform->component.name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100451 goto platform_err;
452 }
453 }
454
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200455 for (i = 0; i < rtd->num_codecs; i++) {
456 codec_dai = rtd->codec_dais[i];
457 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
458 ret = codec_dai->driver->ops->startup(substream,
459 codec_dai);
460 if (ret < 0) {
461 dev_err(codec_dai->dev,
462 "ASoC: can't open codec %s: %d\n",
463 codec_dai->name, ret);
464 goto codec_dai_err;
465 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100466 }
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200467
468 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
469 codec_dai->tx_mask = 0;
470 else
471 codec_dai->rx_mask = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100472 }
473
474 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
475 ret = rtd->dai_link->ops->startup(substream);
476 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000477 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000478 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100479 goto machine_err;
480 }
481 }
482
Liam Girdwood01d75842012-04-25 12:12:49 +0100483 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
484 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
485 goto dynamic;
486
Liam Girdwoodddee6272011-06-09 14:45:53 +0100487 /* Check that the codec and cpu DAIs are compatible */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200488 soc_pcm_init_runtime_hw(substream);
489
490 if (rtd->num_codecs == 1)
491 codec_dai_name = rtd->codec_dai->name;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100492
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100493 if (soc_pcm_has_symmetry(substream))
494 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
495
Liam Girdwoodddee6272011-06-09 14:45:53 +0100496 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100497 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000498 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200499 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100500 goto config_err;
501 }
502 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000503 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200504 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100505 goto config_err;
506 }
507 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
508 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000509 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200510 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100511 goto config_err;
512 }
513
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200514 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000515
Liam Girdwoodddee6272011-06-09 14:45:53 +0100516 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800517 if (cpu_dai->active) {
518 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
519 if (ret != 0)
520 goto config_err;
521 }
522
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200523 for (i = 0; i < rtd->num_codecs; i++) {
524 if (rtd->codec_dais[i]->active) {
525 ret = soc_pcm_apply_symmetry(substream,
526 rtd->codec_dais[i]);
527 if (ret != 0)
528 goto config_err;
529 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100530 }
531
Liam Girdwood103d84a2012-11-19 14:39:15 +0000532 pr_debug("ASoC: %s <-> %s info:\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200533 codec_dai_name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000534 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
535 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100536 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000537 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100538 runtime->hw.rate_max);
539
Liam Girdwood01d75842012-04-25 12:12:49 +0100540dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100541
542 snd_soc_runtime_activate(rtd, substream->stream);
543
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100544 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100545 return 0;
546
547config_err:
548 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
549 rtd->dai_link->ops->shutdown(substream);
550
551machine_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200552 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100553
554codec_dai_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200555 while (--i >= 0) {
556 codec_dai = rtd->codec_dais[i];
557 if (codec_dai->driver->ops->shutdown)
558 codec_dai->driver->ops->shutdown(substream, codec_dai);
559 }
560
Liam Girdwoodddee6272011-06-09 14:45:53 +0100561 if (platform->driver->ops && platform->driver->ops->close)
562 platform->driver->ops->close(substream);
563
564platform_err:
565 if (cpu_dai->driver->ops->shutdown)
566 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
567out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100568 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000569
570 pm_runtime_put(platform->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200571 for (i = 0; i < rtd->num_codecs; i++)
572 pm_runtime_put(rtd->codec_dais[i]->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000573 pm_runtime_put(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200574 for (i = 0; i < rtd->num_codecs; i++) {
575 if (!rtd->codec_dais[i]->active)
576 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
577 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800578 if (!cpu_dai->active)
579 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000580
Liam Girdwoodddee6272011-06-09 14:45:53 +0100581 return ret;
582}
583
584/*
585 * Power down the audio subsystem pmdown_time msecs after close is called.
586 * This is to ensure there are no pops or clicks in between any music tracks
587 * due to DAPM power cycling.
588 */
589static void close_delayed_work(struct work_struct *work)
590{
591 struct snd_soc_pcm_runtime *rtd =
592 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200593 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
Liam Girdwoodddee6272011-06-09 14:45:53 +0100594
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100595 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100596
Liam Girdwood103d84a2012-11-19 14:39:15 +0000597 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100598 codec_dai->driver->playback.stream_name,
599 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600600 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100601
602 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600603 if (rtd->pop_wait == 1) {
604 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800605 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000606 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100607 }
608
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100609 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100610}
611
612/*
613 * Called by ALSA when a PCM substream is closed. Private data can be
614 * freed here. The cpu DAI, codec DAI, machine and platform are also
615 * shutdown.
616 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100617static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100618{
619 struct snd_soc_pcm_runtime *rtd = substream->private_data;
620 struct snd_soc_platform *platform = rtd->platform;
621 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200622 struct snd_soc_dai *codec_dai;
623 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100624
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100625 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100626
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100627 snd_soc_runtime_deactivate(rtd, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100628
Dong Aisheng17841022011-08-29 17:15:14 +0800629 /* clear the corresponding DAIs rate when inactive */
630 if (!cpu_dai->active)
631 cpu_dai->rate = 0;
632
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200633 for (i = 0; i < rtd->num_codecs; i++) {
634 codec_dai = rtd->codec_dais[i];
635 if (!codec_dai->active)
636 codec_dai->rate = 0;
637 }
Sascha Hauer25b76792011-08-17 09:20:01 +0200638
Ramesh Babuae116012014-10-15 12:34:59 +0530639 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
640
Liam Girdwoodddee6272011-06-09 14:45:53 +0100641 if (cpu_dai->driver->ops->shutdown)
642 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
643
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200644 for (i = 0; i < rtd->num_codecs; i++) {
645 codec_dai = rtd->codec_dais[i];
646 if (codec_dai->driver->ops->shutdown)
647 codec_dai->driver->ops->shutdown(substream, codec_dai);
648 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100649
650 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
651 rtd->dai_link->ops->shutdown(substream);
652
653 if (platform->driver->ops && platform->driver->ops->close)
654 platform->driver->ops->close(substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100655
656 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100657 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300658 /* powered down playback stream now */
659 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800660 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800661 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300662 } else {
663 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600664 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100665 queue_delayed_work(system_power_efficient_wq,
666 &rtd->delayed_work,
667 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300668 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100669 } else {
670 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800671 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000672 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100673 }
674
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100675 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000676
677 pm_runtime_put(platform->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200678 for (i = 0; i < rtd->num_codecs; i++)
679 pm_runtime_put(rtd->codec_dais[i]->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000680 pm_runtime_put(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200681 for (i = 0; i < rtd->num_codecs; i++) {
682 if (!rtd->codec_dais[i]->active)
683 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
684 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800685 if (!cpu_dai->active)
686 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000687
Liam Girdwoodddee6272011-06-09 14:45:53 +0100688 return 0;
689}
690
691/*
692 * Called by ALSA when the PCM substream is prepared, can set format, sample
693 * rate, etc. This function is non atomic and can be called multiple times,
694 * it can refer to the runtime info.
695 */
696static int soc_pcm_prepare(struct snd_pcm_substream *substream)
697{
698 struct snd_soc_pcm_runtime *rtd = substream->private_data;
699 struct snd_soc_platform *platform = rtd->platform;
700 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200701 struct snd_soc_dai *codec_dai;
702 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100703
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100704 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100705
706 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
707 ret = rtd->dai_link->ops->prepare(substream);
708 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000709 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
710 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100711 goto out;
712 }
713 }
714
715 if (platform->driver->ops && platform->driver->ops->prepare) {
716 ret = platform->driver->ops->prepare(substream);
717 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000718 dev_err(platform->dev, "ASoC: platform prepare error:"
719 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100720 goto out;
721 }
722 }
723
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200724 for (i = 0; i < rtd->num_codecs; i++) {
725 codec_dai = rtd->codec_dais[i];
726 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
727 ret = codec_dai->driver->ops->prepare(substream,
728 codec_dai);
729 if (ret < 0) {
730 dev_err(codec_dai->dev,
Jarkko Nikula90cc7f12014-12-23 11:04:41 +0200731 "ASoC: codec DAI prepare error: %d\n",
732 ret);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200733 goto out;
734 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100735 }
736 }
737
Mark Brownc5914b02013-10-30 17:47:39 -0700738 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100739 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
740 if (ret < 0) {
Jarkko Nikula90cc7f12014-12-23 11:04:41 +0200741 dev_err(cpu_dai->dev,
742 "ASoC: cpu DAI prepare error: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100743 goto out;
744 }
745 }
746
747 /* cancel any delayed stream shutdown that is pending */
748 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600749 rtd->pop_wait) {
750 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100751 cancel_delayed_work(&rtd->delayed_work);
752 }
753
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000754 snd_soc_dapm_stream_event(rtd, substream->stream,
755 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100756
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200757 for (i = 0; i < rtd->num_codecs; i++)
758 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
759 substream->stream);
Ramesh Babuae116012014-10-15 12:34:59 +0530760 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100761
762out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100763 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100764 return ret;
765}
766
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200767static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
768 unsigned int mask)
769{
770 struct snd_interval *interval;
771 int channels = hweight_long(mask);
772
773 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
774 interval->min = channels;
775 interval->max = channels;
776}
777
Benoit Cousson93e69582014-07-08 23:19:38 +0200778int soc_dai_hw_params(struct snd_pcm_substream *substream,
779 struct snd_pcm_hw_params *params,
780 struct snd_soc_dai *dai)
781{
782 int ret;
783
784 if (dai->driver->ops && dai->driver->ops->hw_params) {
785 ret = dai->driver->ops->hw_params(substream, params, dai);
786 if (ret < 0) {
787 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
788 dai->name, ret);
789 return ret;
790 }
791 }
792
793 return 0;
794}
795
Liam Girdwoodddee6272011-06-09 14:45:53 +0100796/*
797 * Called by ALSA when the hardware params are set by application. This
798 * function can also be called multiple times and can allocate buffers
799 * (using snd_pcm_lib_* ). It's non-atomic.
800 */
801static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
802 struct snd_pcm_hw_params *params)
803{
804 struct snd_soc_pcm_runtime *rtd = substream->private_data;
805 struct snd_soc_platform *platform = rtd->platform;
806 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200807 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100808
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100809 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100810
Nicolin Chen3635bf02013-11-13 18:56:24 +0800811 ret = soc_pcm_params_symmetry(substream, params);
812 if (ret)
813 goto out;
814
Liam Girdwoodddee6272011-06-09 14:45:53 +0100815 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
816 ret = rtd->dai_link->ops->hw_params(substream, params);
817 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000818 dev_err(rtd->card->dev, "ASoC: machine hw_params"
819 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100820 goto out;
821 }
822 }
823
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200824 for (i = 0; i < rtd->num_codecs; i++) {
825 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
826 struct snd_pcm_hw_params codec_params;
827
828 /* copy params for each codec */
829 codec_params = *params;
830
831 /* fixup params based on TDM slot masks */
832 if (codec_dai->tx_mask)
833 soc_pcm_codec_params_fixup(&codec_params,
834 codec_dai->tx_mask);
835 if (codec_dai->rx_mask)
836 soc_pcm_codec_params_fixup(&codec_params,
837 codec_dai->rx_mask);
838
Benoit Cousson93e69582014-07-08 23:19:38 +0200839 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
840 if(ret < 0)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100841 goto codec_err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200842
843 codec_dai->rate = params_rate(&codec_params);
844 codec_dai->channels = params_channels(&codec_params);
845 codec_dai->sample_bits = snd_pcm_format_physical_width(
846 params_format(&codec_params));
Liam Girdwoodddee6272011-06-09 14:45:53 +0100847 }
848
Benoit Cousson93e69582014-07-08 23:19:38 +0200849 ret = soc_dai_hw_params(substream, params, cpu_dai);
850 if (ret < 0)
851 goto interface_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100852
853 if (platform->driver->ops && platform->driver->ops->hw_params) {
854 ret = platform->driver->ops->hw_params(substream, params);
855 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000856 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200857 platform->component.name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100858 goto platform_err;
859 }
860 }
861
Nicolin Chen3635bf02013-11-13 18:56:24 +0800862 /* store the parameters for each DAIs */
Dong Aisheng17841022011-08-29 17:15:14 +0800863 cpu_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800864 cpu_dai->channels = params_channels(params);
865 cpu_dai->sample_bits =
866 snd_pcm_format_physical_width(params_format(params));
867
Liam Girdwoodddee6272011-06-09 14:45:53 +0100868out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100869 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100870 return ret;
871
872platform_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700873 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100874 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
875
876interface_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200877 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100878
879codec_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200880 while (--i >= 0) {
881 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
882 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
883 codec_dai->driver->ops->hw_free(substream, codec_dai);
884 codec_dai->rate = 0;
885 }
886
Liam Girdwoodddee6272011-06-09 14:45:53 +0100887 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
888 rtd->dai_link->ops->hw_free(substream);
889
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100890 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100891 return ret;
892}
893
894/*
895 * Frees resources allocated by hw_params, can be called multiple times
896 */
897static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
898{
899 struct snd_soc_pcm_runtime *rtd = substream->private_data;
900 struct snd_soc_platform *platform = rtd->platform;
901 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200902 struct snd_soc_dai *codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +0800903 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200904 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100905
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100906 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100907
Nicolin Chend3383422013-11-20 18:37:09 +0800908 /* clear the corresponding DAIs parameters when going to be inactive */
909 if (cpu_dai->active == 1) {
910 cpu_dai->rate = 0;
911 cpu_dai->channels = 0;
912 cpu_dai->sample_bits = 0;
913 }
914
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200915 for (i = 0; i < rtd->num_codecs; i++) {
916 codec_dai = rtd->codec_dais[i];
917 if (codec_dai->active == 1) {
918 codec_dai->rate = 0;
919 codec_dai->channels = 0;
920 codec_dai->sample_bits = 0;
921 }
Nicolin Chend3383422013-11-20 18:37:09 +0800922 }
923
Liam Girdwoodddee6272011-06-09 14:45:53 +0100924 /* apply codec digital mute */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200925 for (i = 0; i < rtd->num_codecs; i++) {
926 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
927 (!playback && rtd->codec_dais[i]->capture_active == 1))
928 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
929 substream->stream);
930 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100931
932 /* free any machine hw params */
933 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
934 rtd->dai_link->ops->hw_free(substream);
935
936 /* free any DMA resources */
937 if (platform->driver->ops && platform->driver->ops->hw_free)
938 platform->driver->ops->hw_free(substream);
939
940 /* now free hw params for the DAIs */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200941 for (i = 0; i < rtd->num_codecs; i++) {
942 codec_dai = rtd->codec_dais[i];
943 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
944 codec_dai->driver->ops->hw_free(substream, codec_dai);
945 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100946
Mark Brownc5914b02013-10-30 17:47:39 -0700947 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100948 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
949
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100950 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100951 return 0;
952}
953
954static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
955{
956 struct snd_soc_pcm_runtime *rtd = substream->private_data;
957 struct snd_soc_platform *platform = rtd->platform;
958 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200959 struct snd_soc_dai *codec_dai;
960 int i, ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100961
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200962 for (i = 0; i < rtd->num_codecs; i++) {
963 codec_dai = rtd->codec_dais[i];
964 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
965 ret = codec_dai->driver->ops->trigger(substream,
966 cmd, codec_dai);
967 if (ret < 0)
968 return ret;
969 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100970 }
971
972 if (platform->driver->ops && platform->driver->ops->trigger) {
973 ret = platform->driver->ops->trigger(substream, cmd);
974 if (ret < 0)
975 return ret;
976 }
977
Mark Brownc5914b02013-10-30 17:47:39 -0700978 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100979 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
980 if (ret < 0)
981 return ret;
982 }
Jarkko Nikula4792b0d2014-04-28 14:17:52 +0200983
984 if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
985 ret = rtd->dai_link->ops->trigger(substream, cmd);
986 if (ret < 0)
987 return ret;
988 }
989
Liam Girdwoodddee6272011-06-09 14:45:53 +0100990 return 0;
991}
992
Mark Brown45c0a182012-05-09 21:46:27 +0100993static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
994 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100995{
996 struct snd_soc_pcm_runtime *rtd = substream->private_data;
997 struct snd_soc_platform *platform = rtd->platform;
998 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200999 struct snd_soc_dai *codec_dai;
1000 int i, ret;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001001
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001002 for (i = 0; i < rtd->num_codecs; i++) {
1003 codec_dai = rtd->codec_dais[i];
1004 if (codec_dai->driver->ops &&
1005 codec_dai->driver->ops->bespoke_trigger) {
1006 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1007 cmd, codec_dai);
1008 if (ret < 0)
1009 return ret;
1010 }
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001011 }
1012
Jean-Francois Moinedcf0fa22014-01-03 09:19:18 +01001013 if (platform->driver->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001014 ret = platform->driver->bespoke_trigger(substream, cmd);
1015 if (ret < 0)
1016 return ret;
1017 }
1018
Mark Brownc5914b02013-10-30 17:47:39 -07001019 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001020 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1021 if (ret < 0)
1022 return ret;
1023 }
1024 return 0;
1025}
Liam Girdwoodddee6272011-06-09 14:45:53 +01001026/*
1027 * soc level wrapper for pointer callback
1028 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1029 * the runtime->delay will be updated accordingly.
1030 */
1031static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1032{
1033 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1034 struct snd_soc_platform *platform = rtd->platform;
1035 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001036 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001037 struct snd_pcm_runtime *runtime = substream->runtime;
1038 snd_pcm_uframes_t offset = 0;
1039 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001040 snd_pcm_sframes_t codec_delay = 0;
1041 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001042
1043 if (platform->driver->ops && platform->driver->ops->pointer)
1044 offset = platform->driver->ops->pointer(substream);
1045
Mark Brownc5914b02013-10-30 17:47:39 -07001046 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001047 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1048
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001049 for (i = 0; i < rtd->num_codecs; i++) {
1050 codec_dai = rtd->codec_dais[i];
1051 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
1052 codec_delay = max(codec_delay,
1053 codec_dai->driver->ops->delay(substream,
1054 codec_dai));
1055 }
1056 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001057
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001058 /*
1059 * None of the existing platform drivers implement delay(), so
1060 * for now the codec_dai of first multicodec entry is used
1061 */
Liam Girdwoodddee6272011-06-09 14:45:53 +01001062 if (platform->driver->delay)
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001063 delay += platform->driver->delay(substream, rtd->codec_dais[0]);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001064
1065 runtime->delay = delay;
1066
1067 return offset;
1068}
1069
Liam Girdwood01d75842012-04-25 12:12:49 +01001070/* connect a FE and BE */
1071static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1072 struct snd_soc_pcm_runtime *be, int stream)
1073{
1074 struct snd_soc_dpcm *dpcm;
1075
1076 /* only add new dpcms */
1077 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1078 if (dpcm->be == be && dpcm->fe == fe)
1079 return 0;
1080 }
1081
1082 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1083 if (!dpcm)
1084 return -ENOMEM;
1085
1086 dpcm->be = be;
1087 dpcm->fe = fe;
1088 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1089 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1090 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1091 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1092
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001093 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001094 stream ? "capture" : "playback", fe->dai_link->name,
1095 stream ? "<-" : "->", be->dai_link->name);
1096
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001097#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02001098 if (fe->debugfs_dpcm_root)
1099 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1100 fe->debugfs_dpcm_root, &dpcm->state);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001101#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001102 return 1;
1103}
1104
1105/* reparent a BE onto another FE */
1106static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1107 struct snd_soc_pcm_runtime *be, int stream)
1108{
1109 struct snd_soc_dpcm *dpcm;
1110 struct snd_pcm_substream *fe_substream, *be_substream;
1111
1112 /* reparent if BE is connected to other FEs */
1113 if (!be->dpcm[stream].users)
1114 return;
1115
1116 be_substream = snd_soc_dpcm_get_substream(be, stream);
1117
1118 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1119 if (dpcm->fe == fe)
1120 continue;
1121
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001122 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001123 stream ? "capture" : "playback",
1124 dpcm->fe->dai_link->name,
1125 stream ? "<-" : "->", dpcm->be->dai_link->name);
1126
1127 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1128 be_substream->runtime = fe_substream->runtime;
1129 break;
1130 }
1131}
1132
1133/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001134void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001135{
1136 struct snd_soc_dpcm *dpcm, *d;
1137
1138 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001139 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001140 stream ? "capture" : "playback",
1141 dpcm->be->dai_link->name);
1142
1143 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1144 continue;
1145
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001146 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001147 stream ? "capture" : "playback", fe->dai_link->name,
1148 stream ? "<-" : "->", dpcm->be->dai_link->name);
1149
1150 /* BEs still alive need new FE */
1151 dpcm_be_reparent(fe, dpcm->be, stream);
1152
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001153#ifdef CONFIG_DEBUG_FS
1154 debugfs_remove(dpcm->debugfs_state);
1155#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001156 list_del(&dpcm->list_be);
1157 list_del(&dpcm->list_fe);
1158 kfree(dpcm);
1159 }
1160}
1161
1162/* get BE for DAI widget and stream */
1163static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1164 struct snd_soc_dapm_widget *widget, int stream)
1165{
1166 struct snd_soc_pcm_runtime *be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001167 int i, j;
Liam Girdwood01d75842012-04-25 12:12:49 +01001168
1169 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1170 for (i = 0; i < card->num_links; i++) {
1171 be = &card->rtd[i];
1172
Liam Girdwood35ea0652012-06-05 19:26:59 +01001173 if (!be->dai_link->no_pcm)
1174 continue;
1175
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001176 if (be->cpu_dai->playback_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001177 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001178
1179 for (j = 0; j < be->num_codecs; j++) {
1180 struct snd_soc_dai *dai = be->codec_dais[j];
1181 if (dai->playback_widget == widget)
1182 return be;
1183 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001184 }
1185 } else {
1186
1187 for (i = 0; i < card->num_links; i++) {
1188 be = &card->rtd[i];
1189
Liam Girdwood35ea0652012-06-05 19:26:59 +01001190 if (!be->dai_link->no_pcm)
1191 continue;
1192
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001193 if (be->cpu_dai->capture_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001194 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001195
1196 for (j = 0; j < be->num_codecs; j++) {
1197 struct snd_soc_dai *dai = be->codec_dais[j];
1198 if (dai->capture_widget == widget)
1199 return be;
1200 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001201 }
1202 }
1203
Liam Girdwood103d84a2012-11-19 14:39:15 +00001204 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001205 stream ? "capture" : "playback", widget->name);
1206 return NULL;
1207}
1208
1209static inline struct snd_soc_dapm_widget *
Benoit Cousson37018612014-04-24 14:01:45 +02001210 dai_get_widget(struct snd_soc_dai *dai, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001211{
1212 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Benoit Cousson37018612014-04-24 14:01:45 +02001213 return dai->playback_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001214 else
Benoit Cousson37018612014-04-24 14:01:45 +02001215 return dai->capture_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001216}
1217
1218static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1219 struct snd_soc_dapm_widget *widget)
1220{
1221 int i;
1222
1223 for (i = 0; i < list->num_widgets; i++) {
1224 if (widget == list->widgets[i])
1225 return 1;
1226 }
1227
1228 return 0;
1229}
1230
Liam Girdwood23607022014-01-17 17:03:55 +00001231int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001232 int stream, struct snd_soc_dapm_widget_list **list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001233{
1234 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001235 int paths;
1236
Liam Girdwood01d75842012-04-25 12:12:49 +01001237 /* get number of valid DAI paths and their widgets */
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001238 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list);
Liam Girdwood01d75842012-04-25 12:12:49 +01001239
Liam Girdwood103d84a2012-11-19 14:39:15 +00001240 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001241 stream ? "capture" : "playback");
1242
Liam Girdwood01d75842012-04-25 12:12:49 +01001243 return paths;
1244}
1245
Liam Girdwood01d75842012-04-25 12:12:49 +01001246static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1247 struct snd_soc_dapm_widget_list **list_)
1248{
1249 struct snd_soc_dpcm *dpcm;
1250 struct snd_soc_dapm_widget_list *list = *list_;
1251 struct snd_soc_dapm_widget *widget;
1252 int prune = 0;
1253
1254 /* Destroy any old FE <--> BE connections */
1255 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001256 unsigned int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001257
1258 /* is there a valid CPU DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001259 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001260
1261 /* prune the BE if it's no longer in our active list */
1262 if (widget && widget_in_list(list, widget))
1263 continue;
1264
1265 /* is there a valid CODEC DAI widget for this BE */
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001266 for (i = 0; i < dpcm->be->num_codecs; i++) {
1267 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1268 widget = dai_get_widget(dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001269
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001270 /* prune the BE if it's no longer in our active list */
1271 if (widget && widget_in_list(list, widget))
1272 continue;
1273 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001274
Liam Girdwood103d84a2012-11-19 14:39:15 +00001275 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001276 stream ? "capture" : "playback",
1277 dpcm->be->dai_link->name, fe->dai_link->name);
1278 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1279 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1280 prune++;
1281 }
1282
Liam Girdwood103d84a2012-11-19 14:39:15 +00001283 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001284 return prune;
1285}
1286
1287static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1288 struct snd_soc_dapm_widget_list **list_)
1289{
1290 struct snd_soc_card *card = fe->card;
1291 struct snd_soc_dapm_widget_list *list = *list_;
1292 struct snd_soc_pcm_runtime *be;
1293 int i, new = 0, err;
1294
1295 /* Create any new FE <--> BE connections */
1296 for (i = 0; i < list->num_widgets; i++) {
1297
Mark Brown46162742013-06-05 19:36:11 +01001298 switch (list->widgets[i]->id) {
1299 case snd_soc_dapm_dai_in:
Koro Chenc5b85402015-07-06 10:02:10 +08001300 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1301 continue;
1302 break;
Mark Brown46162742013-06-05 19:36:11 +01001303 case snd_soc_dapm_dai_out:
Koro Chenc5b85402015-07-06 10:02:10 +08001304 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1305 continue;
Mark Brown46162742013-06-05 19:36:11 +01001306 break;
1307 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001308 continue;
Mark Brown46162742013-06-05 19:36:11 +01001309 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001310
1311 /* is there a valid BE rtd for this widget */
1312 be = dpcm_get_be(card, list->widgets[i], stream);
1313 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001314 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001315 list->widgets[i]->name);
1316 continue;
1317 }
1318
1319 /* make sure BE is a real BE */
1320 if (!be->dai_link->no_pcm)
1321 continue;
1322
1323 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001324 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001325 continue;
1326
1327 /* newly connected FE and BE */
1328 err = dpcm_be_connect(fe, be, stream);
1329 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001330 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001331 list->widgets[i]->name);
1332 break;
1333 } else if (err == 0) /* already connected */
1334 continue;
1335
1336 /* new */
1337 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1338 new++;
1339 }
1340
Liam Girdwood103d84a2012-11-19 14:39:15 +00001341 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001342 return new;
1343}
1344
1345/*
1346 * Find the corresponding BE DAIs that source or sink audio to this
1347 * FE substream.
1348 */
Liam Girdwood23607022014-01-17 17:03:55 +00001349int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001350 int stream, struct snd_soc_dapm_widget_list **list, int new)
1351{
1352 if (new)
1353 return dpcm_add_paths(fe, stream, list);
1354 else
1355 return dpcm_prune_paths(fe, stream, list);
1356}
1357
Liam Girdwood23607022014-01-17 17:03:55 +00001358void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001359{
1360 struct snd_soc_dpcm *dpcm;
1361
1362 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1363 dpcm->be->dpcm[stream].runtime_update =
1364 SND_SOC_DPCM_UPDATE_NO;
1365}
1366
1367static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1368 int stream)
1369{
1370 struct snd_soc_dpcm *dpcm;
1371
1372 /* disable any enabled and non active backends */
1373 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1374
1375 struct snd_soc_pcm_runtime *be = dpcm->be;
1376 struct snd_pcm_substream *be_substream =
1377 snd_soc_dpcm_get_substream(be, stream);
1378
1379 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001380 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001381 stream ? "capture" : "playback",
1382 be->dpcm[stream].state);
1383
1384 if (--be->dpcm[stream].users != 0)
1385 continue;
1386
1387 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1388 continue;
1389
1390 soc_pcm_close(be_substream);
1391 be_substream->runtime = NULL;
1392 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1393 }
1394}
1395
Liam Girdwood23607022014-01-17 17:03:55 +00001396int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001397{
1398 struct snd_soc_dpcm *dpcm;
1399 int err, count = 0;
1400
1401 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1402 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1403
1404 struct snd_soc_pcm_runtime *be = dpcm->be;
1405 struct snd_pcm_substream *be_substream =
1406 snd_soc_dpcm_get_substream(be, stream);
1407
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001408 if (!be_substream) {
1409 dev_err(be->dev, "ASoC: no backend %s stream\n",
1410 stream ? "capture" : "playback");
1411 continue;
1412 }
1413
Liam Girdwood01d75842012-04-25 12:12:49 +01001414 /* is this op for this BE ? */
1415 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1416 continue;
1417
1418 /* first time the dpcm is open ? */
1419 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001420 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001421 stream ? "capture" : "playback",
1422 be->dpcm[stream].state);
1423
1424 if (be->dpcm[stream].users++ != 0)
1425 continue;
1426
1427 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1428 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1429 continue;
1430
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001431 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1432 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001433
1434 be_substream->runtime = be->dpcm[stream].runtime;
1435 err = soc_pcm_open(be_substream);
1436 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001437 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001438 be->dpcm[stream].users--;
1439 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001440 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001441 stream ? "capture" : "playback",
1442 be->dpcm[stream].state);
1443
1444 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1445 goto unwind;
1446 }
1447
1448 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1449 count++;
1450 }
1451
1452 return count;
1453
1454unwind:
1455 /* disable any enabled and non active backends */
1456 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1457 struct snd_soc_pcm_runtime *be = dpcm->be;
1458 struct snd_pcm_substream *be_substream =
1459 snd_soc_dpcm_get_substream(be, stream);
1460
1461 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1462 continue;
1463
1464 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001465 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001466 stream ? "capture" : "playback",
1467 be->dpcm[stream].state);
1468
1469 if (--be->dpcm[stream].users != 0)
1470 continue;
1471
1472 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1473 continue;
1474
1475 soc_pcm_close(be_substream);
1476 be_substream->runtime = NULL;
1477 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1478 }
1479
1480 return err;
1481}
1482
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001483static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001484 struct snd_soc_pcm_stream *stream,
1485 u64 formats)
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001486{
1487 runtime->hw.rate_min = stream->rate_min;
1488 runtime->hw.rate_max = stream->rate_max;
1489 runtime->hw.channels_min = stream->channels_min;
1490 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001491 if (runtime->hw.formats)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001492 runtime->hw.formats &= formats & stream->formats;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001493 else
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001494 runtime->hw.formats = formats & stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001495 runtime->hw.rates = stream->rates;
1496}
1497
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001498static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1499{
1500 struct snd_soc_pcm_runtime *fe = substream->private_data;
1501 struct snd_soc_dpcm *dpcm;
1502 u64 formats = ULLONG_MAX;
1503 int stream = substream->stream;
1504
1505 if (!fe->dai_link->dpcm_merged_format)
1506 return formats;
1507
1508 /*
1509 * It returns merged BE codec format
1510 * if FE want to use it (= dpcm_merged_format)
1511 */
1512
1513 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1514 struct snd_soc_pcm_runtime *be = dpcm->be;
1515 struct snd_soc_dai_driver *codec_dai_drv;
1516 struct snd_soc_pcm_stream *codec_stream;
1517 int i;
1518
1519 for (i = 0; i < be->num_codecs; i++) {
1520 codec_dai_drv = be->codec_dais[i]->driver;
1521 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1522 codec_stream = &codec_dai_drv->playback;
1523 else
1524 codec_stream = &codec_dai_drv->capture;
1525
1526 formats &= codec_stream->formats;
1527 }
1528 }
1529
1530 return formats;
1531}
1532
Mark Brown45c0a182012-05-09 21:46:27 +01001533static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001534{
1535 struct snd_pcm_runtime *runtime = substream->runtime;
1536 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1537 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1538 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001539 u64 format = dpcm_runtime_base_format(substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001540
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001541 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001542 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001543 else
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001544 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
Liam Girdwood01d75842012-04-25 12:12:49 +01001545}
1546
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001547static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1548
1549/* Set FE's runtime_update state; the state is protected via PCM stream lock
1550 * for avoiding the race with trigger callback.
1551 * If the state is unset and a trigger is pending while the previous operation,
1552 * process the pending trigger action here.
1553 */
1554static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1555 int stream, enum snd_soc_dpcm_update state)
1556{
1557 struct snd_pcm_substream *substream =
1558 snd_soc_dpcm_get_substream(fe, stream);
1559
1560 snd_pcm_stream_lock_irq(substream);
1561 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1562 dpcm_fe_dai_do_trigger(substream,
1563 fe->dpcm[stream].trigger_pending - 1);
1564 fe->dpcm[stream].trigger_pending = 0;
1565 }
1566 fe->dpcm[stream].runtime_update = state;
1567 snd_pcm_stream_unlock_irq(substream);
1568}
1569
Liam Girdwood01d75842012-04-25 12:12:49 +01001570static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1571{
1572 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1573 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1574 int stream = fe_substream->stream, ret = 0;
1575
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001576 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001577
1578 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1579 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001580 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001581 goto be_err;
1582 }
1583
Liam Girdwood103d84a2012-11-19 14:39:15 +00001584 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001585
1586 /* start the DAI frontend */
1587 ret = soc_pcm_open(fe_substream);
1588 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001589 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001590 goto unwind;
1591 }
1592
1593 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1594
1595 dpcm_set_fe_runtime(fe_substream);
1596 snd_pcm_limit_hw_rates(runtime);
1597
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001598 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001599 return 0;
1600
1601unwind:
1602 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1603be_err:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001604 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001605 return ret;
1606}
1607
Liam Girdwood23607022014-01-17 17:03:55 +00001608int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001609{
1610 struct snd_soc_dpcm *dpcm;
1611
1612 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1613 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1614
1615 struct snd_soc_pcm_runtime *be = dpcm->be;
1616 struct snd_pcm_substream *be_substream =
1617 snd_soc_dpcm_get_substream(be, stream);
1618
1619 /* is this op for this BE ? */
1620 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1621 continue;
1622
1623 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001624 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001625 stream ? "capture" : "playback",
1626 be->dpcm[stream].state);
1627
1628 if (--be->dpcm[stream].users != 0)
1629 continue;
1630
1631 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1632 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1633 continue;
1634
Liam Girdwood103d84a2012-11-19 14:39:15 +00001635 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001636 dpcm->fe->dai_link->name);
1637
1638 soc_pcm_close(be_substream);
1639 be_substream->runtime = NULL;
1640
1641 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1642 }
1643 return 0;
1644}
1645
1646static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1647{
1648 struct snd_soc_pcm_runtime *fe = substream->private_data;
1649 int stream = substream->stream;
1650
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001651 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001652
1653 /* shutdown the BEs */
1654 dpcm_be_dai_shutdown(fe, substream->stream);
1655
Liam Girdwood103d84a2012-11-19 14:39:15 +00001656 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001657
1658 /* now shutdown the frontend */
1659 soc_pcm_close(substream);
1660
1661 /* run the stream event for each BE */
1662 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1663
1664 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001665 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001666 return 0;
1667}
1668
Liam Girdwood23607022014-01-17 17:03:55 +00001669int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001670{
1671 struct snd_soc_dpcm *dpcm;
1672
1673 /* only hw_params backends that are either sinks or sources
1674 * to this frontend DAI */
1675 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1676
1677 struct snd_soc_pcm_runtime *be = dpcm->be;
1678 struct snd_pcm_substream *be_substream =
1679 snd_soc_dpcm_get_substream(be, stream);
1680
1681 /* is this op for this BE ? */
1682 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1683 continue;
1684
1685 /* only free hw when no longer used - check all FEs */
1686 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1687 continue;
1688
Qiao Zhou36fba622014-12-03 10:13:43 +08001689 /* do not free hw if this BE is used by other FE */
1690 if (be->dpcm[stream].users > 1)
1691 continue;
1692
Liam Girdwood01d75842012-04-25 12:12:49 +01001693 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1694 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1695 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001696 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001697 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1698 continue;
1699
Liam Girdwood103d84a2012-11-19 14:39:15 +00001700 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001701 dpcm->fe->dai_link->name);
1702
1703 soc_pcm_hw_free(be_substream);
1704
1705 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1706 }
1707
1708 return 0;
1709}
1710
Mark Brown45c0a182012-05-09 21:46:27 +01001711static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001712{
1713 struct snd_soc_pcm_runtime *fe = substream->private_data;
1714 int err, stream = substream->stream;
1715
1716 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001717 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001718
Liam Girdwood103d84a2012-11-19 14:39:15 +00001719 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001720
1721 /* call hw_free on the frontend */
1722 err = soc_pcm_hw_free(substream);
1723 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001724 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001725 fe->dai_link->name);
1726
1727 /* only hw_params backends that are either sinks or sources
1728 * to this frontend DAI */
1729 err = dpcm_be_dai_hw_free(fe, stream);
1730
1731 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001732 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001733
1734 mutex_unlock(&fe->card->mutex);
1735 return 0;
1736}
1737
Liam Girdwood23607022014-01-17 17:03:55 +00001738int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001739{
1740 struct snd_soc_dpcm *dpcm;
1741 int ret;
1742
1743 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1744
1745 struct snd_soc_pcm_runtime *be = dpcm->be;
1746 struct snd_pcm_substream *be_substream =
1747 snd_soc_dpcm_get_substream(be, stream);
1748
1749 /* is this op for this BE ? */
1750 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1751 continue;
1752
1753 /* only allow hw_params() if no connected FEs are running */
1754 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1755 continue;
1756
1757 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1758 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1759 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1760 continue;
1761
Liam Girdwood103d84a2012-11-19 14:39:15 +00001762 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001763 dpcm->fe->dai_link->name);
1764
1765 /* copy params for each dpcm */
1766 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1767 sizeof(struct snd_pcm_hw_params));
1768
1769 /* perform any hw_params fixups */
1770 if (be->dai_link->be_hw_params_fixup) {
1771 ret = be->dai_link->be_hw_params_fixup(be,
1772 &dpcm->hw_params);
1773 if (ret < 0) {
1774 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001775 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001776 ret);
1777 goto unwind;
1778 }
1779 }
1780
1781 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1782 if (ret < 0) {
1783 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001784 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001785 goto unwind;
1786 }
1787
1788 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1789 }
1790 return 0;
1791
1792unwind:
1793 /* disable any enabled and non active backends */
1794 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1795 struct snd_soc_pcm_runtime *be = dpcm->be;
1796 struct snd_pcm_substream *be_substream =
1797 snd_soc_dpcm_get_substream(be, stream);
1798
1799 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1800 continue;
1801
1802 /* only allow hw_free() if no connected FEs are running */
1803 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1804 continue;
1805
1806 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1807 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1808 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1809 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1810 continue;
1811
1812 soc_pcm_hw_free(be_substream);
1813 }
1814
1815 return ret;
1816}
1817
Mark Brown45c0a182012-05-09 21:46:27 +01001818static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1819 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001820{
1821 struct snd_soc_pcm_runtime *fe = substream->private_data;
1822 int ret, stream = substream->stream;
1823
1824 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001825 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001826
1827 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1828 sizeof(struct snd_pcm_hw_params));
1829 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1830 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001831 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001832 goto out;
1833 }
1834
Liam Girdwood103d84a2012-11-19 14:39:15 +00001835 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001836 fe->dai_link->name, params_rate(params),
1837 params_channels(params), params_format(params));
1838
1839 /* call hw_params on the frontend */
1840 ret = soc_pcm_hw_params(substream, params);
1841 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001842 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001843 dpcm_be_dai_hw_free(fe, stream);
1844 } else
1845 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1846
1847out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001848 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001849 mutex_unlock(&fe->card->mutex);
1850 return ret;
1851}
1852
1853static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1854 struct snd_pcm_substream *substream, int cmd)
1855{
1856 int ret;
1857
Liam Girdwood103d84a2012-11-19 14:39:15 +00001858 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001859 dpcm->fe->dai_link->name, cmd);
1860
1861 ret = soc_pcm_trigger(substream, cmd);
1862 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001863 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001864
1865 return ret;
1866}
1867
Liam Girdwood23607022014-01-17 17:03:55 +00001868int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01001869 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001870{
1871 struct snd_soc_dpcm *dpcm;
1872 int ret = 0;
1873
1874 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1875
1876 struct snd_soc_pcm_runtime *be = dpcm->be;
1877 struct snd_pcm_substream *be_substream =
1878 snd_soc_dpcm_get_substream(be, stream);
1879
1880 /* is this op for this BE ? */
1881 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1882 continue;
1883
1884 switch (cmd) {
1885 case SNDRV_PCM_TRIGGER_START:
1886 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1887 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1888 continue;
1889
1890 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1891 if (ret)
1892 return ret;
1893
1894 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1895 break;
1896 case SNDRV_PCM_TRIGGER_RESUME:
1897 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1898 continue;
1899
1900 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1901 if (ret)
1902 return ret;
1903
1904 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1905 break;
1906 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1907 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1908 continue;
1909
1910 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1911 if (ret)
1912 return ret;
1913
1914 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1915 break;
1916 case SNDRV_PCM_TRIGGER_STOP:
1917 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1918 continue;
1919
1920 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1921 continue;
1922
1923 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1924 if (ret)
1925 return ret;
1926
1927 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1928 break;
1929 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08001930 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01001931 continue;
1932
1933 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1934 continue;
1935
1936 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1937 if (ret)
1938 return ret;
1939
1940 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1941 break;
1942 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1943 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1944 continue;
1945
1946 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1947 continue;
1948
1949 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1950 if (ret)
1951 return ret;
1952
1953 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1954 break;
1955 }
1956 }
1957
1958 return ret;
1959}
1960EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1961
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001962static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001963{
1964 struct snd_soc_pcm_runtime *fe = substream->private_data;
1965 int stream = substream->stream, ret;
1966 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1967
1968 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1969
1970 switch (trigger) {
1971 case SND_SOC_DPCM_TRIGGER_PRE:
1972 /* call trigger on the frontend before the backend. */
1973
Liam Girdwood103d84a2012-11-19 14:39:15 +00001974 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001975 fe->dai_link->name, cmd);
1976
1977 ret = soc_pcm_trigger(substream, cmd);
1978 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001979 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001980 goto out;
1981 }
1982
1983 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1984 break;
1985 case SND_SOC_DPCM_TRIGGER_POST:
1986 /* call trigger on the frontend after the backend. */
1987
1988 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1989 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001990 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001991 goto out;
1992 }
1993
Liam Girdwood103d84a2012-11-19 14:39:15 +00001994 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001995 fe->dai_link->name, cmd);
1996
1997 ret = soc_pcm_trigger(substream, cmd);
1998 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001999 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2000 /* bespoke trigger() - handles both FE and BEs */
2001
Liam Girdwood103d84a2012-11-19 14:39:15 +00002002 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002003 fe->dai_link->name, cmd);
2004
2005 ret = soc_pcm_bespoke_trigger(substream, cmd);
2006 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002007 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002008 goto out;
2009 }
2010 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002011 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00002012 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01002013 fe->dai_link->name);
2014 ret = -EINVAL;
2015 goto out;
2016 }
2017
2018 switch (cmd) {
2019 case SNDRV_PCM_TRIGGER_START:
2020 case SNDRV_PCM_TRIGGER_RESUME:
2021 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2022 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2023 break;
2024 case SNDRV_PCM_TRIGGER_STOP:
2025 case SNDRV_PCM_TRIGGER_SUSPEND:
2026 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2027 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2028 break;
2029 }
2030
2031out:
2032 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2033 return ret;
2034}
2035
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002036static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2037{
2038 struct snd_soc_pcm_runtime *fe = substream->private_data;
2039 int stream = substream->stream;
2040
2041 /* if FE's runtime_update is already set, we're in race;
2042 * process this trigger later at exit
2043 */
2044 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2045 fe->dpcm[stream].trigger_pending = cmd + 1;
2046 return 0; /* delayed, assuming it's successful */
2047 }
2048
2049 /* we're alone, let's trigger */
2050 return dpcm_fe_dai_do_trigger(substream, cmd);
2051}
2052
Liam Girdwood23607022014-01-17 17:03:55 +00002053int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002054{
2055 struct snd_soc_dpcm *dpcm;
2056 int ret = 0;
2057
2058 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2059
2060 struct snd_soc_pcm_runtime *be = dpcm->be;
2061 struct snd_pcm_substream *be_substream =
2062 snd_soc_dpcm_get_substream(be, stream);
2063
2064 /* is this op for this BE ? */
2065 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2066 continue;
2067
2068 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2069 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2070 continue;
2071
Liam Girdwood103d84a2012-11-19 14:39:15 +00002072 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002073 dpcm->fe->dai_link->name);
2074
2075 ret = soc_pcm_prepare(be_substream);
2076 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002077 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002078 ret);
2079 break;
2080 }
2081
2082 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2083 }
2084 return ret;
2085}
2086
Mark Brown45c0a182012-05-09 21:46:27 +01002087static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002088{
2089 struct snd_soc_pcm_runtime *fe = substream->private_data;
2090 int stream = substream->stream, ret = 0;
2091
2092 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2093
Liam Girdwood103d84a2012-11-19 14:39:15 +00002094 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002095
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002096 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002097
2098 /* there is no point preparing this FE if there are no BEs */
2099 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002100 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002101 fe->dai_link->name);
2102 ret = -EINVAL;
2103 goto out;
2104 }
2105
2106 ret = dpcm_be_dai_prepare(fe, substream->stream);
2107 if (ret < 0)
2108 goto out;
2109
2110 /* call prepare on the frontend */
2111 ret = soc_pcm_prepare(substream);
2112 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002113 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002114 fe->dai_link->name);
2115 goto out;
2116 }
2117
2118 /* run the stream event for each BE */
2119 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2120 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2121
2122out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002123 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002124 mutex_unlock(&fe->card->mutex);
2125
2126 return ret;
2127}
2128
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002129static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2130 unsigned int cmd, void *arg)
2131{
2132 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2133 struct snd_soc_platform *platform = rtd->platform;
2134
Mark Brownc5914b02013-10-30 17:47:39 -07002135 if (platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002136 return platform->driver->ops->ioctl(substream, cmd, arg);
2137 return snd_pcm_lib_ioctl(substream, cmd, arg);
2138}
2139
Liam Girdwood618dae12012-04-25 12:12:51 +01002140static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2141{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002142 struct snd_pcm_substream *substream =
2143 snd_soc_dpcm_get_substream(fe, stream);
2144 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002145 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01002146
Liam Girdwood103d84a2012-11-19 14:39:15 +00002147 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002148 stream ? "capture" : "playback", fe->dai_link->name);
2149
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002150 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2151 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002152 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002153 fe->dai_link->name);
2154
2155 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2156 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002157 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002158 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002159 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002160 fe->dai_link->name);
2161
2162 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2163 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002164 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002165 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002166
2167 err = dpcm_be_dai_hw_free(fe, stream);
2168 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002169 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002170
2171 err = dpcm_be_dai_shutdown(fe, stream);
2172 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002173 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002174
2175 /* run the stream event for each BE */
2176 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2177
2178 return 0;
2179}
2180
2181static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2182{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002183 struct snd_pcm_substream *substream =
2184 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002185 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002186 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002187 int ret;
2188
Liam Girdwood103d84a2012-11-19 14:39:15 +00002189 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002190 stream ? "capture" : "playback", fe->dai_link->name);
2191
2192 /* Only start the BE if the FE is ready */
2193 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2194 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2195 return -EINVAL;
2196
2197 /* startup must always be called for new BEs */
2198 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002199 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002200 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01002201
2202 /* keep going if FE state is > open */
2203 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2204 return 0;
2205
2206 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002207 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002208 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01002209
2210 /* keep going if FE state is > hw_params */
2211 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2212 return 0;
2213
2214
2215 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002216 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002217 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002218
2219 /* run the stream event for each BE */
2220 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2221
2222 /* keep going if FE state is > prepare */
2223 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2224 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2225 return 0;
2226
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002227 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2228 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002229 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002230 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01002231
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002232 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2233 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002234 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002235 goto hw_free;
2236 }
2237 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002238 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002239 fe->dai_link->name);
2240
2241 ret = dpcm_be_dai_trigger(fe, stream,
2242 SNDRV_PCM_TRIGGER_START);
2243 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002244 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002245 goto hw_free;
2246 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002247 }
2248
2249 return 0;
2250
2251hw_free:
2252 dpcm_be_dai_hw_free(fe, stream);
2253close:
2254 dpcm_be_dai_shutdown(fe, stream);
2255disconnect:
2256 /* disconnect any non started BEs */
2257 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2258 struct snd_soc_pcm_runtime *be = dpcm->be;
2259 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2260 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2261 }
2262
2263 return ret;
2264}
2265
2266static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2267{
2268 int ret;
2269
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002270 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002271 ret = dpcm_run_update_startup(fe, stream);
2272 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002273 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002274 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002275
2276 return ret;
2277}
2278
2279static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2280{
2281 int ret;
2282
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002283 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002284 ret = dpcm_run_update_shutdown(fe, stream);
2285 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002286 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002287 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002288
2289 return ret;
2290}
2291
2292/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2293 * any DAI links.
2294 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002295int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002296{
Liam Girdwood618dae12012-04-25 12:12:51 +01002297 int i, old, new, paths;
2298
Liam Girdwood618dae12012-04-25 12:12:51 +01002299 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2300 for (i = 0; i < card->num_rtd; i++) {
2301 struct snd_soc_dapm_widget_list *list;
2302 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2303
2304 /* make sure link is FE */
2305 if (!fe->dai_link->dynamic)
2306 continue;
2307
2308 /* only check active links */
2309 if (!fe->cpu_dai->active)
2310 continue;
2311
2312 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002313 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002314 fe->dai_link->name);
2315
2316 /* skip if FE doesn't have playback capability */
Qiao Zhou075207d2014-11-17 16:02:57 +08002317 if (!fe->cpu_dai->driver->playback.channels_min
2318 || !fe->codec_dai->driver->playback.channels_min)
2319 goto capture;
2320
2321 /* skip if FE isn't currently playing */
2322 if (!fe->cpu_dai->playback_active
2323 || !fe->codec_dai->playback_active)
Liam Girdwood618dae12012-04-25 12:12:51 +01002324 goto capture;
2325
2326 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2327 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002328 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002329 fe->dai_link->name, "playback");
2330 mutex_unlock(&card->mutex);
2331 return paths;
2332 }
2333
2334 /* update any new playback paths */
2335 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2336 if (new) {
2337 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2338 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2339 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2340 }
2341
2342 /* update any old playback paths */
2343 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2344 if (old) {
2345 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2346 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2347 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2348 }
2349
Qiao Zhou7ed9de72014-06-04 19:42:06 +08002350 dpcm_path_put(&list);
Liam Girdwood618dae12012-04-25 12:12:51 +01002351capture:
2352 /* skip if FE doesn't have capture capability */
Qiao Zhou075207d2014-11-17 16:02:57 +08002353 if (!fe->cpu_dai->driver->capture.channels_min
2354 || !fe->codec_dai->driver->capture.channels_min)
2355 continue;
2356
2357 /* skip if FE isn't currently capturing */
2358 if (!fe->cpu_dai->capture_active
2359 || !fe->codec_dai->capture_active)
Liam Girdwood618dae12012-04-25 12:12:51 +01002360 continue;
2361
2362 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2363 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002364 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002365 fe->dai_link->name, "capture");
2366 mutex_unlock(&card->mutex);
2367 return paths;
2368 }
2369
2370 /* update any new capture paths */
2371 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2372 if (new) {
2373 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2374 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2375 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2376 }
2377
2378 /* update any old capture paths */
2379 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2380 if (old) {
2381 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2382 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2383 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2384 }
2385
2386 dpcm_path_put(&list);
2387 }
2388
2389 mutex_unlock(&card->mutex);
2390 return 0;
2391}
Liam Girdwood01d75842012-04-25 12:12:49 +01002392int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2393{
2394 struct snd_soc_dpcm *dpcm;
2395 struct list_head *clients =
2396 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2397
2398 list_for_each_entry(dpcm, clients, list_be) {
2399
2400 struct snd_soc_pcm_runtime *be = dpcm->be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002401 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01002402
2403 if (be->dai_link->ignore_suspend)
2404 continue;
2405
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002406 for (i = 0; i < be->num_codecs; i++) {
2407 struct snd_soc_dai *dai = be->codec_dais[i];
2408 struct snd_soc_dai_driver *drv = dai->driver;
Liam Girdwood01d75842012-04-25 12:12:49 +01002409
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002410 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2411 be->dai_link->name);
2412
2413 if (drv->ops && drv->ops->digital_mute &&
2414 dai->playback_active)
2415 drv->ops->digital_mute(dai, mute);
2416 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002417 }
2418
2419 return 0;
2420}
2421
Mark Brown45c0a182012-05-09 21:46:27 +01002422static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002423{
2424 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2425 struct snd_soc_dpcm *dpcm;
2426 struct snd_soc_dapm_widget_list *list;
2427 int ret;
2428 int stream = fe_substream->stream;
2429
2430 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2431 fe->dpcm[stream].runtime = fe_substream->runtime;
2432
Qiao Zhou8f70e512014-09-10 17:54:07 +08002433 ret = dpcm_path_get(fe, stream, &list);
2434 if (ret < 0) {
2435 mutex_unlock(&fe->card->mutex);
2436 return ret;
2437 } else if (ret == 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002438 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002439 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002440 }
2441
2442 /* calculate valid and active FE <-> BE dpcms */
2443 dpcm_process_paths(fe, stream, &list, 1);
2444
2445 ret = dpcm_fe_dai_startup(fe_substream);
2446 if (ret < 0) {
2447 /* clean up all links */
2448 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2449 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2450
2451 dpcm_be_disconnect(fe, stream);
2452 fe->dpcm[stream].runtime = NULL;
2453 }
2454
2455 dpcm_clear_pending_state(fe, stream);
2456 dpcm_path_put(&list);
2457 mutex_unlock(&fe->card->mutex);
2458 return ret;
2459}
2460
Mark Brown45c0a182012-05-09 21:46:27 +01002461static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002462{
2463 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2464 struct snd_soc_dpcm *dpcm;
2465 int stream = fe_substream->stream, ret;
2466
2467 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2468 ret = dpcm_fe_dai_shutdown(fe_substream);
2469
2470 /* mark FE's links ready to prune */
2471 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2472 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2473
2474 dpcm_be_disconnect(fe, stream);
2475
2476 fe->dpcm[stream].runtime = NULL;
2477 mutex_unlock(&fe->card->mutex);
2478 return ret;
2479}
2480
Liam Girdwoodddee6272011-06-09 14:45:53 +01002481/* create a new pcm */
2482int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2483{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002484 struct snd_soc_platform *platform = rtd->platform;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002485 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002486 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2487 struct snd_pcm *pcm;
2488 char new_name[64];
2489 int ret = 0, playback = 0, capture = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002490 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002491
Liam Girdwood01d75842012-04-25 12:12:49 +01002492 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002493 playback = rtd->dai_link->dpcm_playback;
2494 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002495 } else {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002496 for (i = 0; i < rtd->num_codecs; i++) {
2497 codec_dai = rtd->codec_dais[i];
2498 if (codec_dai->driver->playback.channels_min)
2499 playback = 1;
2500 if (codec_dai->driver->capture.channels_min)
2501 capture = 1;
2502 }
2503
2504 capture = capture && cpu_dai->driver->capture.channels_min;
2505 playback = playback && cpu_dai->driver->playback.channels_min;
Liam Girdwood01d75842012-04-25 12:12:49 +01002506 }
Sangsu Parka5002312012-01-02 17:15:10 +09002507
Fabio Estevamd6bead02013-08-29 10:32:13 -03002508 if (rtd->dai_link->playback_only) {
2509 playback = 1;
2510 capture = 0;
2511 }
2512
2513 if (rtd->dai_link->capture_only) {
2514 playback = 0;
2515 capture = 1;
2516 }
2517
Liam Girdwood01d75842012-04-25 12:12:49 +01002518 /* create the PCM */
2519 if (rtd->dai_link->no_pcm) {
2520 snprintf(new_name, sizeof(new_name), "(%s)",
2521 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002522
Liam Girdwood01d75842012-04-25 12:12:49 +01002523 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2524 playback, capture, &pcm);
2525 } else {
2526 if (rtd->dai_link->dynamic)
2527 snprintf(new_name, sizeof(new_name), "%s (*)",
2528 rtd->dai_link->stream_name);
2529 else
2530 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002531 rtd->dai_link->stream_name,
2532 (rtd->num_codecs > 1) ?
2533 "multicodec" : rtd->codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002534
Liam Girdwood01d75842012-04-25 12:12:49 +01002535 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2536 capture, &pcm);
2537 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002538 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002539 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002540 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002541 return ret;
2542 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002543 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002544
2545 /* DAPM dai link stream work */
2546 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2547
Vinod Koul48c76992015-02-12 09:59:53 +05302548 pcm->nonatomic = rtd->dai_link->nonatomic;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002549 rtd->pcm = pcm;
2550 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002551
2552 if (rtd->dai_link->no_pcm) {
2553 if (playback)
2554 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2555 if (capture)
2556 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2557 goto out;
2558 }
2559
2560 /* ASoC PCM operations */
2561 if (rtd->dai_link->dynamic) {
2562 rtd->ops.open = dpcm_fe_dai_open;
2563 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2564 rtd->ops.prepare = dpcm_fe_dai_prepare;
2565 rtd->ops.trigger = dpcm_fe_dai_trigger;
2566 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2567 rtd->ops.close = dpcm_fe_dai_close;
2568 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002569 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002570 } else {
2571 rtd->ops.open = soc_pcm_open;
2572 rtd->ops.hw_params = soc_pcm_hw_params;
2573 rtd->ops.prepare = soc_pcm_prepare;
2574 rtd->ops.trigger = soc_pcm_trigger;
2575 rtd->ops.hw_free = soc_pcm_hw_free;
2576 rtd->ops.close = soc_pcm_close;
2577 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002578 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002579 }
2580
Liam Girdwoodddee6272011-06-09 14:45:53 +01002581 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002582 rtd->ops.ack = platform->driver->ops->ack;
2583 rtd->ops.copy = platform->driver->ops->copy;
2584 rtd->ops.silence = platform->driver->ops->silence;
2585 rtd->ops.page = platform->driver->ops->page;
2586 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002587 }
2588
2589 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002590 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002591
2592 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002593 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002594
2595 if (platform->driver->pcm_new) {
2596 ret = platform->driver->pcm_new(rtd);
2597 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002598 dev_err(platform->dev,
2599 "ASoC: pcm constructor failed: %d\n",
2600 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002601 return ret;
2602 }
2603 }
2604
2605 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002606out:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002607 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2608 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2609 cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002610 return ret;
2611}
Liam Girdwood01d75842012-04-25 12:12:49 +01002612
2613/* is the current PCM operation for this FE ? */
2614int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2615{
2616 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2617 return 1;
2618 return 0;
2619}
2620EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2621
2622/* is the current PCM operation for this BE ? */
2623int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2624 struct snd_soc_pcm_runtime *be, int stream)
2625{
2626 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2627 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2628 be->dpcm[stream].runtime_update))
2629 return 1;
2630 return 0;
2631}
2632EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2633
2634/* get the substream for this BE */
2635struct snd_pcm_substream *
2636 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2637{
2638 return be->pcm->streams[stream].substream;
2639}
2640EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2641
2642/* get the BE runtime state */
2643enum snd_soc_dpcm_state
2644 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2645{
2646 return be->dpcm[stream].state;
2647}
2648EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2649
2650/* set the BE runtime state */
2651void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2652 int stream, enum snd_soc_dpcm_state state)
2653{
2654 be->dpcm[stream].state = state;
2655}
2656EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2657
2658/*
2659 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2660 * are not running, paused or suspended for the specified stream direction.
2661 */
2662int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2663 struct snd_soc_pcm_runtime *be, int stream)
2664{
2665 struct snd_soc_dpcm *dpcm;
2666 int state;
2667
2668 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2669
2670 if (dpcm->fe == fe)
2671 continue;
2672
2673 state = dpcm->fe->dpcm[stream].state;
2674 if (state == SND_SOC_DPCM_STATE_START ||
2675 state == SND_SOC_DPCM_STATE_PAUSED ||
2676 state == SND_SOC_DPCM_STATE_SUSPEND)
2677 return 0;
2678 }
2679
2680 /* it's safe to free/stop this BE DAI */
2681 return 1;
2682}
2683EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2684
2685/*
2686 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2687 * running, paused or suspended for the specified stream direction.
2688 */
2689int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2690 struct snd_soc_pcm_runtime *be, int stream)
2691{
2692 struct snd_soc_dpcm *dpcm;
2693 int state;
2694
2695 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2696
2697 if (dpcm->fe == fe)
2698 continue;
2699
2700 state = dpcm->fe->dpcm[stream].state;
2701 if (state == SND_SOC_DPCM_STATE_START ||
2702 state == SND_SOC_DPCM_STATE_PAUSED ||
2703 state == SND_SOC_DPCM_STATE_SUSPEND ||
2704 state == SND_SOC_DPCM_STATE_PREPARE)
2705 return 0;
2706 }
2707
2708 /* it's safe to change hw_params */
2709 return 1;
2710}
2711EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002712
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002713int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2714 int cmd, struct snd_soc_platform *platform)
2715{
Mark Brownc5914b02013-10-30 17:47:39 -07002716 if (platform->driver->ops && platform->driver->ops->trigger)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002717 return platform->driver->ops->trigger(substream, cmd);
2718 return 0;
2719}
2720EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2721
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002722#ifdef CONFIG_DEBUG_FS
2723static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2724{
2725 switch (state) {
2726 case SND_SOC_DPCM_STATE_NEW:
2727 return "new";
2728 case SND_SOC_DPCM_STATE_OPEN:
2729 return "open";
2730 case SND_SOC_DPCM_STATE_HW_PARAMS:
2731 return "hw_params";
2732 case SND_SOC_DPCM_STATE_PREPARE:
2733 return "prepare";
2734 case SND_SOC_DPCM_STATE_START:
2735 return "start";
2736 case SND_SOC_DPCM_STATE_STOP:
2737 return "stop";
2738 case SND_SOC_DPCM_STATE_SUSPEND:
2739 return "suspend";
2740 case SND_SOC_DPCM_STATE_PAUSED:
2741 return "paused";
2742 case SND_SOC_DPCM_STATE_HW_FREE:
2743 return "hw_free";
2744 case SND_SOC_DPCM_STATE_CLOSE:
2745 return "close";
2746 }
2747
2748 return "unknown";
2749}
2750
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002751static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2752 int stream, char *buf, size_t size)
2753{
2754 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2755 struct snd_soc_dpcm *dpcm;
2756 ssize_t offset = 0;
2757
2758 /* FE state */
2759 offset += snprintf(buf + offset, size - offset,
2760 "[%s - %s]\n", fe->dai_link->name,
2761 stream ? "Capture" : "Playback");
2762
2763 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2764 dpcm_state_string(fe->dpcm[stream].state));
2765
2766 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2767 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2768 offset += snprintf(buf + offset, size - offset,
2769 "Hardware Params: "
2770 "Format = %s, Channels = %d, Rate = %d\n",
2771 snd_pcm_format_name(params_format(params)),
2772 params_channels(params),
2773 params_rate(params));
2774
2775 /* BEs state */
2776 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2777
2778 if (list_empty(&fe->dpcm[stream].be_clients)) {
2779 offset += snprintf(buf + offset, size - offset,
2780 " No active DSP links\n");
2781 goto out;
2782 }
2783
2784 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2785 struct snd_soc_pcm_runtime *be = dpcm->be;
2786 params = &dpcm->hw_params;
2787
2788 offset += snprintf(buf + offset, size - offset,
2789 "- %s\n", be->dai_link->name);
2790
2791 offset += snprintf(buf + offset, size - offset,
2792 " State: %s\n",
2793 dpcm_state_string(be->dpcm[stream].state));
2794
2795 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2796 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2797 offset += snprintf(buf + offset, size - offset,
2798 " Hardware Params: "
2799 "Format = %s, Channels = %d, Rate = %d\n",
2800 snd_pcm_format_name(params_format(params)),
2801 params_channels(params),
2802 params_rate(params));
2803 }
2804
2805out:
2806 return offset;
2807}
2808
2809static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2810 size_t count, loff_t *ppos)
2811{
2812 struct snd_soc_pcm_runtime *fe = file->private_data;
2813 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2814 char *buf;
2815
2816 buf = kmalloc(out_count, GFP_KERNEL);
2817 if (!buf)
2818 return -ENOMEM;
2819
2820 if (fe->cpu_dai->driver->playback.channels_min)
2821 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2822 buf + offset, out_count - offset);
2823
2824 if (fe->cpu_dai->driver->capture.channels_min)
2825 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2826 buf + offset, out_count - offset);
2827
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002828 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002829
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002830 kfree(buf);
2831 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002832}
2833
2834static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002835 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002836 .read = dpcm_state_read_file,
2837 .llseek = default_llseek,
2838};
2839
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02002840void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002841{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002842 if (!rtd->dai_link)
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02002843 return;
Mark Brownb3bba9a2012-05-08 10:33:47 +01002844
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02002845 if (!rtd->card->debugfs_card_root)
2846 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002847
2848 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2849 rtd->card->debugfs_card_root);
2850 if (!rtd->debugfs_dpcm_root) {
2851 dev_dbg(rtd->dev,
2852 "ASoC: Failed to create dpcm debugfs directory %s\n",
2853 rtd->dai_link->name);
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02002854 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002855 }
2856
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002857 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002858 rtd->debugfs_dpcm_root,
2859 rtd, &dpcm_state_fops);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002860}
2861#endif