blob: 09fed7014ed84e8955256aa4a0241ce5d2919ef6 [file] [log] [blame]
Liam Girdwoodddee6272011-06-09 14:45:53 +01001/*
2 * soc-pcm.c -- ALSA SoC PCM
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
8 *
9 * Authors: Liam Girdwood <lrg@ti.com>
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +020010 * Mark Brown <broonie@opensource.wolfsonmicro.com>
Liam Girdwoodddee6272011-06-09 14:45:53 +010011 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 */
18
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/delay.h>
Nicolin Chen988e8cc2013-11-04 14:57:31 +080022#include <linux/pinctrl/consumer.h>
Mark Brownd6652ef2011-12-03 20:14:31 +000023#include <linux/pm_runtime.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010024#include <linux/slab.h>
25#include <linux/workqueue.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010026#include <linux/export.h>
Liam Girdwoodf86dcef2012-04-25 12:12:50 +010027#include <linux/debugfs.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010028#include <sound/core.h>
29#include <sound/pcm.h>
30#include <sound/pcm_params.h>
31#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010032#include <sound/soc-dpcm.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010033#include <sound/initval.h>
34
Liam Girdwood01d75842012-04-25 12:12:49 +010035#define DPCM_MAX_BE_USERS 8
36
Ricard Wanderlofcde79032015-08-24 14:16:51 +020037/*
38 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
39 *
40 * Returns true if the DAI supports the indicated stream type.
41 */
42static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
43{
44 struct snd_soc_pcm_stream *codec_stream;
45
46 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
47 codec_stream = &dai->driver->playback;
48 else
49 codec_stream = &dai->driver->capture;
50
51 /* If the codec specifies any rate at all, it supports the stream. */
52 return codec_stream->rates;
53}
54
Lars-Peter Clausen90996f42013-05-14 11:05:30 +020055/**
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010056 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
57 * @rtd: ASoC PCM runtime that is activated
58 * @stream: Direction of the PCM stream
59 *
60 * Increments the active count for all the DAIs and components attached to a PCM
61 * runtime. Should typically be called when a stream is opened.
62 *
63 * Must be called with the rtd->pcm_mutex being held
64 */
65void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
66{
67 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020068 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010069
70 lockdep_assert_held(&rtd->pcm_mutex);
71
72 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
73 cpu_dai->playback_active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020074 for (i = 0; i < rtd->num_codecs; i++)
75 rtd->codec_dais[i]->playback_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010076 } else {
77 cpu_dai->capture_active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020078 for (i = 0; i < rtd->num_codecs; i++)
79 rtd->codec_dais[i]->capture_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010080 }
81
82 cpu_dai->active++;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +010083 cpu_dai->component->active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020084 for (i = 0; i < rtd->num_codecs; i++) {
85 rtd->codec_dais[i]->active++;
86 rtd->codec_dais[i]->component->active++;
87 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010088}
89
90/**
91 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
92 * @rtd: ASoC PCM runtime that is deactivated
93 * @stream: Direction of the PCM stream
94 *
95 * Decrements the active count for all the DAIs and components attached to a PCM
96 * runtime. Should typically be called when a stream is closed.
97 *
98 * Must be called with the rtd->pcm_mutex being held
99 */
100void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
101{
102 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200103 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100104
105 lockdep_assert_held(&rtd->pcm_mutex);
106
107 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
108 cpu_dai->playback_active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200109 for (i = 0; i < rtd->num_codecs; i++)
110 rtd->codec_dais[i]->playback_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100111 } else {
112 cpu_dai->capture_active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200113 for (i = 0; i < rtd->num_codecs; i++)
114 rtd->codec_dais[i]->capture_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100115 }
116
117 cpu_dai->active--;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +0100118 cpu_dai->component->active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200119 for (i = 0; i < rtd->num_codecs; i++) {
120 rtd->codec_dais[i]->component->active--;
121 rtd->codec_dais[i]->active--;
122 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100123}
124
125/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100126 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
127 * @rtd: The ASoC PCM runtime that should be checked.
128 *
129 * This function checks whether the power down delay should be ignored for a
130 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
131 * been configured to ignore the delay, or if none of the components benefits
132 * from having the delay.
133 */
134bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
135{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200136 int i;
137 bool ignore = true;
138
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100139 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
140 return true;
141
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200142 for (i = 0; i < rtd->num_codecs; i++)
143 ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
144
145 return rtd->cpu_dai->component->ignore_pmdown_time && ignore;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100146}
147
148/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200149 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
150 * @substream: the pcm substream
151 * @hw: the hardware parameters
152 *
153 * Sets the substream runtime hardware parameters.
154 */
155int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
156 const struct snd_pcm_hardware *hw)
157{
158 struct snd_pcm_runtime *runtime = substream->runtime;
159 runtime->hw.info = hw->info;
160 runtime->hw.formats = hw->formats;
161 runtime->hw.period_bytes_min = hw->period_bytes_min;
162 runtime->hw.period_bytes_max = hw->period_bytes_max;
163 runtime->hw.periods_min = hw->periods_min;
164 runtime->hw.periods_max = hw->periods_max;
165 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
166 runtime->hw.fifo_size = hw->fifo_size;
167 return 0;
168}
169EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
170
Liam Girdwood01d75842012-04-25 12:12:49 +0100171/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000172int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100173 int event)
174{
175 struct snd_soc_dpcm *dpcm;
176
177 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
178
179 struct snd_soc_pcm_runtime *be = dpcm->be;
180
Liam Girdwood103d84a2012-11-19 14:39:15 +0000181 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100182 be->dai_link->name, event, dir);
183
Banajit Goswamib1cd2e32017-07-14 23:15:05 -0700184 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
185 (be->dpcm[dir].users >= 1))
186 continue;
187
Liam Girdwood01d75842012-04-25 12:12:49 +0100188 snd_soc_dapm_stream_event(be, dir, event);
189 }
190
191 snd_soc_dapm_stream_event(fe, dir, event);
192
193 return 0;
194}
195
Dong Aisheng17841022011-08-29 17:15:14 +0800196static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
197 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100198{
199 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100200 int ret;
201
Nicolin Chen3635bf02013-11-13 18:56:24 +0800202 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
203 rtd->dai_link->symmetric_rates)) {
204 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
205 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100206
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200207 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800208 SNDRV_PCM_HW_PARAM_RATE,
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200209 soc_dai->rate);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800210 if (ret < 0) {
211 dev_err(soc_dai->dev,
212 "ASoC: Unable to apply rate constraint: %d\n",
213 ret);
214 return ret;
215 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100216 }
217
Nicolin Chen3635bf02013-11-13 18:56:24 +0800218 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
219 rtd->dai_link->symmetric_channels)) {
220 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
221 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100222
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200223 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800224 SNDRV_PCM_HW_PARAM_CHANNELS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800225 soc_dai->channels);
226 if (ret < 0) {
227 dev_err(soc_dai->dev,
228 "ASoC: Unable to apply channel symmetry constraint: %d\n",
229 ret);
230 return ret;
231 }
232 }
233
234 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
235 rtd->dai_link->symmetric_samplebits)) {
236 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
237 soc_dai->sample_bits);
238
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200239 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800240 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800241 soc_dai->sample_bits);
242 if (ret < 0) {
243 dev_err(soc_dai->dev,
244 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
245 ret);
246 return ret;
247 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100248 }
249
250 return 0;
251}
252
Nicolin Chen3635bf02013-11-13 18:56:24 +0800253static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
254 struct snd_pcm_hw_params *params)
255{
256 struct snd_soc_pcm_runtime *rtd = substream->private_data;
257 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200258 unsigned int rate, channels, sample_bits, symmetry, i;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800259
260 rate = params_rate(params);
261 channels = params_channels(params);
262 sample_bits = snd_pcm_format_physical_width(params_format(params));
263
264 /* reject unmatched parameters when applying symmetry */
265 symmetry = cpu_dai->driver->symmetric_rates ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800266 rtd->dai_link->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200267
268 for (i = 0; i < rtd->num_codecs; i++)
269 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
270
Nicolin Chen3635bf02013-11-13 18:56:24 +0800271 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
272 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
273 cpu_dai->rate, rate);
274 return -EINVAL;
275 }
276
277 symmetry = cpu_dai->driver->symmetric_channels ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800278 rtd->dai_link->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200279
280 for (i = 0; i < rtd->num_codecs; i++)
281 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
282
Nicolin Chen3635bf02013-11-13 18:56:24 +0800283 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
284 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
285 cpu_dai->channels, channels);
286 return -EINVAL;
287 }
288
289 symmetry = cpu_dai->driver->symmetric_samplebits ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800290 rtd->dai_link->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200291
292 for (i = 0; i < rtd->num_codecs; i++)
293 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
294
Nicolin Chen3635bf02013-11-13 18:56:24 +0800295 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
296 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
297 cpu_dai->sample_bits, sample_bits);
298 return -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100299 }
300
301 return 0;
302}
303
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100304static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
305{
306 struct snd_soc_pcm_runtime *rtd = substream->private_data;
307 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100308 struct snd_soc_dai_link *link = rtd->dai_link;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200309 unsigned int symmetry, i;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100310
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200311 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
312 cpu_driver->symmetric_channels || link->symmetric_channels ||
313 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
314
315 for (i = 0; i < rtd->num_codecs; i++)
316 symmetry = symmetry ||
317 rtd->codec_dais[i]->driver->symmetric_rates ||
318 rtd->codec_dais[i]->driver->symmetric_channels ||
319 rtd->codec_dais[i]->driver->symmetric_samplebits;
320
321 return symmetry;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100322}
323
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200324static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000325{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200326 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Takashi Iwaic6068d32014-12-31 17:10:34 +0100327 int ret;
Mark Brown58ba9b22012-01-16 18:38:51 +0000328
329 if (!bits)
330 return;
331
Lars-Peter Clausen0e2a3752014-12-29 18:43:38 +0100332 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
333 if (ret != 0)
334 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
335 bits, ret);
Mark Brown58ba9b22012-01-16 18:38:51 +0000336}
337
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200338static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200339{
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200340 struct snd_soc_pcm_runtime *rtd = substream->private_data;
341 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200342 struct snd_soc_dai *codec_dai;
343 int i;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200344 unsigned int bits = 0, cpu_bits;
345
346 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200347 for (i = 0; i < rtd->num_codecs; i++) {
348 codec_dai = rtd->codec_dais[i];
349 if (codec_dai->driver->playback.sig_bits == 0) {
350 bits = 0;
351 break;
352 }
353 bits = max(codec_dai->driver->playback.sig_bits, bits);
354 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200355 cpu_bits = cpu_dai->driver->playback.sig_bits;
356 } else {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200357 for (i = 0; i < rtd->num_codecs; i++) {
358 codec_dai = rtd->codec_dais[i];
Daniel Mack5e63dfc2014-10-07 14:33:46 +0200359 if (codec_dai->driver->capture.sig_bits == 0) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200360 bits = 0;
361 break;
362 }
363 bits = max(codec_dai->driver->capture.sig_bits, bits);
364 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200365 cpu_bits = cpu_dai->driver->capture.sig_bits;
366 }
367
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200368 soc_pcm_set_msb(substream, bits);
369 soc_pcm_set_msb(substream, cpu_bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200370}
371
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200372static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200373{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200374 struct snd_pcm_runtime *runtime = substream->runtime;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100375 struct snd_pcm_hardware *hw = &runtime->hw;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200376 struct snd_soc_pcm_runtime *rtd = substream->private_data;
377 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
378 struct snd_soc_dai_driver *codec_dai_drv;
379 struct snd_soc_pcm_stream *codec_stream;
380 struct snd_soc_pcm_stream *cpu_stream;
381 unsigned int chan_min = 0, chan_max = UINT_MAX;
382 unsigned int rate_min = 0, rate_max = UINT_MAX;
383 unsigned int rates = UINT_MAX;
384 u64 formats = ULLONG_MAX;
385 int i;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100386
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200387 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
388 cpu_stream = &cpu_dai_drv->playback;
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100389 else
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200390 cpu_stream = &cpu_dai_drv->capture;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100391
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200392 /* first calculate min/max only for CODECs in the DAI link */
393 for (i = 0; i < rtd->num_codecs; i++) {
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200394
395 /*
396 * Skip CODECs which don't support the current stream type.
397 * Otherwise, since the rate, channel, and format values will
398 * zero in that case, we would have no usable settings left,
399 * causing the resulting setup to fail.
400 * At least one CODEC should match, otherwise we should have
401 * bailed out on a higher level, since there would be no
402 * CODEC to support the transfer direction in that case.
403 */
404 if (!snd_soc_dai_stream_valid(rtd->codec_dais[i],
405 substream->stream))
406 continue;
407
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200408 codec_dai_drv = rtd->codec_dais[i]->driver;
409 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
410 codec_stream = &codec_dai_drv->playback;
411 else
412 codec_stream = &codec_dai_drv->capture;
413 chan_min = max(chan_min, codec_stream->channels_min);
414 chan_max = min(chan_max, codec_stream->channels_max);
415 rate_min = max(rate_min, codec_stream->rate_min);
416 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
417 formats &= codec_stream->formats;
418 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
419 }
420
421 /*
422 * chan min/max cannot be enforced if there are multiple CODEC DAIs
423 * connected to a single CPU DAI, use CPU DAI's directly and let
424 * channel allocation be fixed up later
425 */
426 if (rtd->num_codecs > 1) {
427 chan_min = cpu_stream->channels_min;
428 chan_max = cpu_stream->channels_max;
429 }
430
431 hw->channels_min = max(chan_min, cpu_stream->channels_min);
432 hw->channels_max = min(chan_max, cpu_stream->channels_max);
433 if (hw->formats)
434 hw->formats &= formats & cpu_stream->formats;
435 else
436 hw->formats = formats & cpu_stream->formats;
437 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100438
439 snd_pcm_limit_hw_rates(runtime);
440
441 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200442 hw->rate_min = max(hw->rate_min, rate_min);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100443 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200444 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200445}
446
Mark Brown58ba9b22012-01-16 18:38:51 +0000447/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100448 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
449 * then initialized and any private data can be allocated. This also calls
450 * startup for the cpu DAI, platform, machine and codec DAI.
451 */
452static int soc_pcm_open(struct snd_pcm_substream *substream)
453{
454 struct snd_soc_pcm_runtime *rtd = substream->private_data;
455 struct snd_pcm_runtime *runtime = substream->runtime;
456 struct snd_soc_platform *platform = rtd->platform;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000457 struct snd_soc_component *component;
458 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100459 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200460 struct snd_soc_dai *codec_dai;
461 const char *codec_dai_name = "multicodec";
462 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100463
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800464 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200465 for (i = 0; i < rtd->num_codecs; i++)
466 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000467
468 for_each_rtdcom(rtd, rtdcom) {
469 component = rtdcom->component;
470
471 pm_runtime_get_sync(component->dev);
472 }
Mark Brownd6652ef2011-12-03 20:14:31 +0000473
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100474 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100475
476 /* startup the audio subsystem */
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000477 if (cpu_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100478 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
479 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000480 dev_err(cpu_dai->dev, "ASoC: can't open interface"
481 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100482 goto out;
483 }
484 }
485
486 if (platform->driver->ops && platform->driver->ops->open) {
487 ret = platform->driver->ops->open(substream);
488 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000489 dev_err(platform->dev, "ASoC: can't open platform"
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200490 " %s: %d\n", platform->component.name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100491 goto platform_err;
492 }
493 }
494
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200495 for (i = 0; i < rtd->num_codecs; i++) {
496 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000497 if (codec_dai->driver->ops->startup) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200498 ret = codec_dai->driver->ops->startup(substream,
499 codec_dai);
500 if (ret < 0) {
501 dev_err(codec_dai->dev,
502 "ASoC: can't open codec %s: %d\n",
503 codec_dai->name, ret);
504 goto codec_dai_err;
505 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100506 }
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200507
508 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
509 codec_dai->tx_mask = 0;
510 else
511 codec_dai->rx_mask = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100512 }
513
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000514 if (rtd->dai_link->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100515 ret = rtd->dai_link->ops->startup(substream);
516 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000517 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000518 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100519 goto machine_err;
520 }
521 }
522
Liam Girdwood01d75842012-04-25 12:12:49 +0100523 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
524 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
525 goto dynamic;
526
Liam Girdwoodddee6272011-06-09 14:45:53 +0100527 /* Check that the codec and cpu DAIs are compatible */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200528 soc_pcm_init_runtime_hw(substream);
529
530 if (rtd->num_codecs == 1)
531 codec_dai_name = rtd->codec_dai->name;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100532
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100533 if (soc_pcm_has_symmetry(substream))
534 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
535
Liam Girdwoodddee6272011-06-09 14:45:53 +0100536 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100537 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000538 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200539 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100540 goto config_err;
541 }
542 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000543 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200544 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100545 goto config_err;
546 }
547 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
548 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000549 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200550 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100551 goto config_err;
552 }
553
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200554 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000555
Liam Girdwoodddee6272011-06-09 14:45:53 +0100556 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800557 if (cpu_dai->active) {
558 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
559 if (ret != 0)
560 goto config_err;
561 }
562
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200563 for (i = 0; i < rtd->num_codecs; i++) {
564 if (rtd->codec_dais[i]->active) {
565 ret = soc_pcm_apply_symmetry(substream,
566 rtd->codec_dais[i]);
567 if (ret != 0)
568 goto config_err;
569 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100570 }
571
Liam Girdwood103d84a2012-11-19 14:39:15 +0000572 pr_debug("ASoC: %s <-> %s info:\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200573 codec_dai_name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000574 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
575 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100576 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000577 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100578 runtime->hw.rate_max);
579
Liam Girdwood01d75842012-04-25 12:12:49 +0100580dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100581
582 snd_soc_runtime_activate(rtd, substream->stream);
583
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100584 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100585 return 0;
586
587config_err:
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000588 if (rtd->dai_link->ops->shutdown)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100589 rtd->dai_link->ops->shutdown(substream);
590
591machine_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200592 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100593
594codec_dai_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200595 while (--i >= 0) {
596 codec_dai = rtd->codec_dais[i];
597 if (codec_dai->driver->ops->shutdown)
598 codec_dai->driver->ops->shutdown(substream, codec_dai);
599 }
600
Liam Girdwoodddee6272011-06-09 14:45:53 +0100601 if (platform->driver->ops && platform->driver->ops->close)
602 platform->driver->ops->close(substream);
603
604platform_err:
605 if (cpu_dai->driver->ops->shutdown)
606 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
607out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100608 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000609
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000610 for_each_rtdcom(rtd, rtdcom) {
611 component = rtdcom->component;
612
613 pm_runtime_mark_last_busy(component->dev);
614 pm_runtime_put_autosuspend(component->dev);
Sanyog Kale3f809782016-01-05 17:14:49 +0530615 }
616
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200617 for (i = 0; i < rtd->num_codecs; i++) {
618 if (!rtd->codec_dais[i]->active)
619 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
620 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800621 if (!cpu_dai->active)
622 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000623
Liam Girdwoodddee6272011-06-09 14:45:53 +0100624 return ret;
625}
626
627/*
628 * Power down the audio subsystem pmdown_time msecs after close is called.
629 * This is to ensure there are no pops or clicks in between any music tracks
630 * due to DAPM power cycling.
631 */
632static void close_delayed_work(struct work_struct *work)
633{
634 struct snd_soc_pcm_runtime *rtd =
635 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200636 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
Liam Girdwoodddee6272011-06-09 14:45:53 +0100637
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100638 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100639
Liam Girdwood103d84a2012-11-19 14:39:15 +0000640 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100641 codec_dai->driver->playback.stream_name,
642 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600643 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100644
645 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600646 if (rtd->pop_wait == 1) {
647 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800648 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000649 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100650 }
651
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100652 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100653}
654
655/*
656 * Called by ALSA when a PCM substream is closed. Private data can be
657 * freed here. The cpu DAI, codec DAI, machine and platform are also
658 * shutdown.
659 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100660static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100661{
662 struct snd_soc_pcm_runtime *rtd = substream->private_data;
663 struct snd_soc_platform *platform = rtd->platform;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000664 struct snd_soc_component *component;
665 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100666 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200667 struct snd_soc_dai *codec_dai;
668 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100669
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100670 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100671
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100672 snd_soc_runtime_deactivate(rtd, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100673
Dong Aisheng17841022011-08-29 17:15:14 +0800674 /* clear the corresponding DAIs rate when inactive */
675 if (!cpu_dai->active)
676 cpu_dai->rate = 0;
677
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200678 for (i = 0; i < rtd->num_codecs; i++) {
679 codec_dai = rtd->codec_dais[i];
680 if (!codec_dai->active)
681 codec_dai->rate = 0;
682 }
Sascha Hauer25b76792011-08-17 09:20:01 +0200683
Ramesh Babuae116012014-10-15 12:34:59 +0530684 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
685
Liam Girdwoodddee6272011-06-09 14:45:53 +0100686 if (cpu_dai->driver->ops->shutdown)
687 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
688
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200689 for (i = 0; i < rtd->num_codecs; i++) {
690 codec_dai = rtd->codec_dais[i];
691 if (codec_dai->driver->ops->shutdown)
692 codec_dai->driver->ops->shutdown(substream, codec_dai);
693 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100694
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000695 if (rtd->dai_link->ops->shutdown)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100696 rtd->dai_link->ops->shutdown(substream);
697
698 if (platform->driver->ops && platform->driver->ops->close)
699 platform->driver->ops->close(substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100700
701 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100702 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300703 /* powered down playback stream now */
704 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800705 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800706 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300707 } else {
708 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600709 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100710 queue_delayed_work(system_power_efficient_wq,
711 &rtd->delayed_work,
712 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300713 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100714 } else {
715 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800716 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000717 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100718 }
719
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100720 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000721
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000722 for_each_rtdcom(rtd, rtdcom) {
723 component = rtdcom->component;
Sanyog Kale3f809782016-01-05 17:14:49 +0530724
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000725 pm_runtime_mark_last_busy(component->dev);
726 pm_runtime_put_autosuspend(component->dev);
Sanyog Kale3f809782016-01-05 17:14:49 +0530727 }
728
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200729 for (i = 0; i < rtd->num_codecs; i++) {
730 if (!rtd->codec_dais[i]->active)
731 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
732 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800733 if (!cpu_dai->active)
734 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000735
Liam Girdwoodddee6272011-06-09 14:45:53 +0100736 return 0;
737}
738
739/*
740 * Called by ALSA when the PCM substream is prepared, can set format, sample
741 * rate, etc. This function is non atomic and can be called multiple times,
742 * it can refer to the runtime info.
743 */
744static int soc_pcm_prepare(struct snd_pcm_substream *substream)
745{
746 struct snd_soc_pcm_runtime *rtd = substream->private_data;
747 struct snd_soc_platform *platform = rtd->platform;
748 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200749 struct snd_soc_dai *codec_dai;
750 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100751
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100752 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100753
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000754 if (rtd->dai_link->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100755 ret = rtd->dai_link->ops->prepare(substream);
756 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000757 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
758 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100759 goto out;
760 }
761 }
762
763 if (platform->driver->ops && platform->driver->ops->prepare) {
764 ret = platform->driver->ops->prepare(substream);
765 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000766 dev_err(platform->dev, "ASoC: platform prepare error:"
767 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100768 goto out;
769 }
770 }
771
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200772 for (i = 0; i < rtd->num_codecs; i++) {
773 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000774 if (codec_dai->driver->ops->prepare) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200775 ret = codec_dai->driver->ops->prepare(substream,
776 codec_dai);
777 if (ret < 0) {
778 dev_err(codec_dai->dev,
Jarkko Nikula90cc7f12014-12-23 11:04:41 +0200779 "ASoC: codec DAI prepare error: %d\n",
780 ret);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200781 goto out;
782 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100783 }
784 }
785
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000786 if (cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100787 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
788 if (ret < 0) {
Jarkko Nikula90cc7f12014-12-23 11:04:41 +0200789 dev_err(cpu_dai->dev,
790 "ASoC: cpu DAI prepare error: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100791 goto out;
792 }
793 }
794
795 /* cancel any delayed stream shutdown that is pending */
796 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600797 rtd->pop_wait) {
798 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100799 cancel_delayed_work(&rtd->delayed_work);
800 }
801
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000802 snd_soc_dapm_stream_event(rtd, substream->stream,
803 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100804
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200805 for (i = 0; i < rtd->num_codecs; i++)
806 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
807 substream->stream);
Ramesh Babuae116012014-10-15 12:34:59 +0530808 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100809
810out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100811 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100812 return ret;
813}
814
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200815static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
816 unsigned int mask)
817{
818 struct snd_interval *interval;
819 int channels = hweight_long(mask);
820
821 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
822 interval->min = channels;
823 interval->max = channels;
824}
825
Benoit Cousson93e69582014-07-08 23:19:38 +0200826int soc_dai_hw_params(struct snd_pcm_substream *substream,
827 struct snd_pcm_hw_params *params,
828 struct snd_soc_dai *dai)
829{
830 int ret;
831
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000832 if (dai->driver->ops->hw_params) {
Benoit Cousson93e69582014-07-08 23:19:38 +0200833 ret = dai->driver->ops->hw_params(substream, params, dai);
834 if (ret < 0) {
835 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
836 dai->name, ret);
837 return ret;
838 }
839 }
840
841 return 0;
842}
843
Liam Girdwoodddee6272011-06-09 14:45:53 +0100844/*
845 * Called by ALSA when the hardware params are set by application. This
846 * function can also be called multiple times and can allocate buffers
847 * (using snd_pcm_lib_* ). It's non-atomic.
848 */
849static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
850 struct snd_pcm_hw_params *params)
851{
852 struct snd_soc_pcm_runtime *rtd = substream->private_data;
853 struct snd_soc_platform *platform = rtd->platform;
854 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200855 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100856
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100857 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000858 if (rtd->dai_link->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100859 ret = rtd->dai_link->ops->hw_params(substream, params);
860 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000861 dev_err(rtd->card->dev, "ASoC: machine hw_params"
862 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100863 goto out;
864 }
865 }
866
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200867 for (i = 0; i < rtd->num_codecs; i++) {
868 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
869 struct snd_pcm_hw_params codec_params;
870
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200871 /*
872 * Skip CODECs which don't support the current stream type,
873 * the idea being that if a CODEC is not used for the currently
874 * set up transfer direction, it should not need to be
875 * configured, especially since the configuration used might
876 * not even be supported by that CODEC. There may be cases
877 * however where a CODEC needs to be set up although it is
878 * actually not being used for the transfer, e.g. if a
879 * capture-only CODEC is acting as an LRCLK and/or BCLK master
880 * for the DAI link including a playback-only CODEC.
881 * If this becomes necessary, we will have to augment the
882 * machine driver setup with information on how to act, so
883 * we can do the right thing here.
884 */
885 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
886 continue;
887
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200888 /* copy params for each codec */
889 codec_params = *params;
890
891 /* fixup params based on TDM slot masks */
892 if (codec_dai->tx_mask)
893 soc_pcm_codec_params_fixup(&codec_params,
894 codec_dai->tx_mask);
895 if (codec_dai->rx_mask)
896 soc_pcm_codec_params_fixup(&codec_params,
897 codec_dai->rx_mask);
898
Benoit Cousson93e69582014-07-08 23:19:38 +0200899 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
900 if(ret < 0)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100901 goto codec_err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200902
903 codec_dai->rate = params_rate(&codec_params);
904 codec_dai->channels = params_channels(&codec_params);
905 codec_dai->sample_bits = snd_pcm_format_physical_width(
906 params_format(&codec_params));
Liam Girdwoodddee6272011-06-09 14:45:53 +0100907 }
908
Benoit Cousson93e69582014-07-08 23:19:38 +0200909 ret = soc_dai_hw_params(substream, params, cpu_dai);
910 if (ret < 0)
911 goto interface_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100912
913 if (platform->driver->ops && platform->driver->ops->hw_params) {
914 ret = platform->driver->ops->hw_params(substream, params);
915 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000916 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200917 platform->component.name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100918 goto platform_err;
919 }
920 }
921
Nicolin Chen3635bf02013-11-13 18:56:24 +0800922 /* store the parameters for each DAIs */
Dong Aisheng17841022011-08-29 17:15:14 +0800923 cpu_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800924 cpu_dai->channels = params_channels(params);
925 cpu_dai->sample_bits =
926 snd_pcm_format_physical_width(params_format(params));
927
jiada wang957ce0c2017-09-20 15:25:30 +0900928
929 ret = soc_pcm_params_symmetry(substream, params);
930 if (ret)
931 goto platform_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100932out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100933 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100934 return ret;
935
936platform_err:
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000937 if (cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100938 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
939
940interface_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200941 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100942
943codec_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200944 while (--i >= 0) {
945 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000946 if (codec_dai->driver->ops->hw_free)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200947 codec_dai->driver->ops->hw_free(substream, codec_dai);
948 codec_dai->rate = 0;
949 }
950
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000951 if (rtd->dai_link->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100952 rtd->dai_link->ops->hw_free(substream);
953
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100954 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100955 return ret;
956}
957
958/*
959 * Frees resources allocated by hw_params, can be called multiple times
960 */
961static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
962{
963 struct snd_soc_pcm_runtime *rtd = substream->private_data;
964 struct snd_soc_platform *platform = rtd->platform;
965 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200966 struct snd_soc_dai *codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +0800967 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200968 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100969
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100970 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100971
Nicolin Chend3383422013-11-20 18:37:09 +0800972 /* clear the corresponding DAIs parameters when going to be inactive */
973 if (cpu_dai->active == 1) {
974 cpu_dai->rate = 0;
975 cpu_dai->channels = 0;
976 cpu_dai->sample_bits = 0;
977 }
978
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200979 for (i = 0; i < rtd->num_codecs; i++) {
980 codec_dai = rtd->codec_dais[i];
981 if (codec_dai->active == 1) {
982 codec_dai->rate = 0;
983 codec_dai->channels = 0;
984 codec_dai->sample_bits = 0;
985 }
Nicolin Chend3383422013-11-20 18:37:09 +0800986 }
987
Liam Girdwoodddee6272011-06-09 14:45:53 +0100988 /* apply codec digital mute */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200989 for (i = 0; i < rtd->num_codecs; i++) {
990 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
991 (!playback && rtd->codec_dais[i]->capture_active == 1))
992 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
993 substream->stream);
994 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100995
996 /* free any machine hw params */
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000997 if (rtd->dai_link->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100998 rtd->dai_link->ops->hw_free(substream);
999
1000 /* free any DMA resources */
1001 if (platform->driver->ops && platform->driver->ops->hw_free)
1002 platform->driver->ops->hw_free(substream);
1003
1004 /* now free hw params for the DAIs */
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001005 for (i = 0; i < rtd->num_codecs; i++) {
1006 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001007 if (codec_dai->driver->ops->hw_free)
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001008 codec_dai->driver->ops->hw_free(substream, codec_dai);
1009 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001010
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001011 if (cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001012 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1013
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001014 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001015 return 0;
1016}
1017
1018static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1019{
1020 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1021 struct snd_soc_platform *platform = rtd->platform;
1022 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001023 struct snd_soc_dai *codec_dai;
1024 int i, ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001025
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001026 for (i = 0; i < rtd->num_codecs; i++) {
1027 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001028 if (codec_dai->driver->ops->trigger) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001029 ret = codec_dai->driver->ops->trigger(substream,
1030 cmd, codec_dai);
1031 if (ret < 0)
1032 return ret;
1033 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001034 }
1035
1036 if (platform->driver->ops && platform->driver->ops->trigger) {
1037 ret = platform->driver->ops->trigger(substream, cmd);
1038 if (ret < 0)
1039 return ret;
1040 }
1041
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001042 if (cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +01001043 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1044 if (ret < 0)
1045 return ret;
1046 }
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001047
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +00001048 if (rtd->dai_link->ops->trigger) {
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001049 ret = rtd->dai_link->ops->trigger(substream, cmd);
1050 if (ret < 0)
1051 return ret;
1052 }
1053
Liam Girdwoodddee6272011-06-09 14:45:53 +01001054 return 0;
1055}
1056
Mark Brown45c0a182012-05-09 21:46:27 +01001057static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1058 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001059{
1060 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001061 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001062 struct snd_soc_dai *codec_dai;
1063 int i, ret;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001064
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001065 for (i = 0; i < rtd->num_codecs; i++) {
1066 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001067 if (codec_dai->driver->ops->bespoke_trigger) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001068 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1069 cmd, codec_dai);
1070 if (ret < 0)
1071 return ret;
1072 }
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001073 }
1074
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001075 if (cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001076 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1077 if (ret < 0)
1078 return ret;
1079 }
1080 return 0;
1081}
Liam Girdwoodddee6272011-06-09 14:45:53 +01001082/*
1083 * soc level wrapper for pointer callback
1084 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1085 * the runtime->delay will be updated accordingly.
1086 */
1087static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1088{
1089 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1090 struct snd_soc_platform *platform = rtd->platform;
1091 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001092 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001093 struct snd_pcm_runtime *runtime = substream->runtime;
1094 snd_pcm_uframes_t offset = 0;
1095 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001096 snd_pcm_sframes_t codec_delay = 0;
1097 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001098
1099 if (platform->driver->ops && platform->driver->ops->pointer)
1100 offset = platform->driver->ops->pointer(substream);
1101
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001102 if (cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001103 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1104
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001105 for (i = 0; i < rtd->num_codecs; i++) {
1106 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001107 if (codec_dai->driver->ops->delay)
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001108 codec_delay = max(codec_delay,
1109 codec_dai->driver->ops->delay(substream,
1110 codec_dai));
1111 }
1112 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001113
Liam Girdwoodddee6272011-06-09 14:45:53 +01001114 runtime->delay = delay;
1115
1116 return offset;
1117}
1118
Liam Girdwood01d75842012-04-25 12:12:49 +01001119/* connect a FE and BE */
1120static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1121 struct snd_soc_pcm_runtime *be, int stream)
1122{
1123 struct snd_soc_dpcm *dpcm;
1124
1125 /* only add new dpcms */
1126 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1127 if (dpcm->be == be && dpcm->fe == fe)
1128 return 0;
1129 }
1130
1131 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1132 if (!dpcm)
1133 return -ENOMEM;
1134
1135 dpcm->be = be;
1136 dpcm->fe = fe;
1137 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1138 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1139 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1140 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1141
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001142 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001143 stream ? "capture" : "playback", fe->dai_link->name,
1144 stream ? "<-" : "->", be->dai_link->name);
1145
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001146#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02001147 if (fe->debugfs_dpcm_root)
1148 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1149 fe->debugfs_dpcm_root, &dpcm->state);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001150#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001151 return 1;
1152}
1153
1154/* reparent a BE onto another FE */
1155static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1156 struct snd_soc_pcm_runtime *be, int stream)
1157{
1158 struct snd_soc_dpcm *dpcm;
1159 struct snd_pcm_substream *fe_substream, *be_substream;
1160
1161 /* reparent if BE is connected to other FEs */
1162 if (!be->dpcm[stream].users)
1163 return;
1164
1165 be_substream = snd_soc_dpcm_get_substream(be, stream);
1166
1167 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1168 if (dpcm->fe == fe)
1169 continue;
1170
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001171 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001172 stream ? "capture" : "playback",
1173 dpcm->fe->dai_link->name,
1174 stream ? "<-" : "->", dpcm->be->dai_link->name);
1175
1176 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1177 be_substream->runtime = fe_substream->runtime;
1178 break;
1179 }
1180}
1181
1182/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001183void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001184{
1185 struct snd_soc_dpcm *dpcm, *d;
1186
1187 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001188 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001189 stream ? "capture" : "playback",
1190 dpcm->be->dai_link->name);
1191
1192 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1193 continue;
1194
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001195 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001196 stream ? "capture" : "playback", fe->dai_link->name,
1197 stream ? "<-" : "->", dpcm->be->dai_link->name);
1198
1199 /* BEs still alive need new FE */
1200 dpcm_be_reparent(fe, dpcm->be, stream);
1201
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001202#ifdef CONFIG_DEBUG_FS
1203 debugfs_remove(dpcm->debugfs_state);
1204#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001205 list_del(&dpcm->list_be);
1206 list_del(&dpcm->list_fe);
1207 kfree(dpcm);
1208 }
1209}
1210
1211/* get BE for DAI widget and stream */
1212static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1213 struct snd_soc_dapm_widget *widget, int stream)
1214{
1215 struct snd_soc_pcm_runtime *be;
Mengdong Lin1a497982015-11-18 02:34:11 -05001216 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001217
1218 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001219 list_for_each_entry(be, &card->rtd_list, list) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001220
Liam Girdwood35ea0652012-06-05 19:26:59 +01001221 if (!be->dai_link->no_pcm)
1222 continue;
1223
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001224 if (be->cpu_dai->playback_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001225 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001226
Mengdong Lin1a497982015-11-18 02:34:11 -05001227 for (i = 0; i < be->num_codecs; i++) {
1228 struct snd_soc_dai *dai = be->codec_dais[i];
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001229 if (dai->playback_widget == widget)
1230 return be;
1231 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001232 }
1233 } else {
1234
Mengdong Lin1a497982015-11-18 02:34:11 -05001235 list_for_each_entry(be, &card->rtd_list, list) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001236
Liam Girdwood35ea0652012-06-05 19:26:59 +01001237 if (!be->dai_link->no_pcm)
1238 continue;
1239
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001240 if (be->cpu_dai->capture_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001241 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001242
Mengdong Lin1a497982015-11-18 02:34:11 -05001243 for (i = 0; i < be->num_codecs; i++) {
1244 struct snd_soc_dai *dai = be->codec_dais[i];
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001245 if (dai->capture_widget == widget)
1246 return be;
1247 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001248 }
1249 }
1250
Liam Girdwood103d84a2012-11-19 14:39:15 +00001251 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001252 stream ? "capture" : "playback", widget->name);
1253 return NULL;
1254}
1255
1256static inline struct snd_soc_dapm_widget *
Benoit Cousson37018612014-04-24 14:01:45 +02001257 dai_get_widget(struct snd_soc_dai *dai, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001258{
1259 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Benoit Cousson37018612014-04-24 14:01:45 +02001260 return dai->playback_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001261 else
Benoit Cousson37018612014-04-24 14:01:45 +02001262 return dai->capture_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001263}
1264
1265static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1266 struct snd_soc_dapm_widget *widget)
1267{
1268 int i;
1269
1270 for (i = 0; i < list->num_widgets; i++) {
1271 if (widget == list->widgets[i])
1272 return 1;
1273 }
1274
1275 return 0;
1276}
1277
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001278static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1279 enum snd_soc_dapm_direction dir)
1280{
1281 struct snd_soc_card *card = widget->dapm->card;
1282 struct snd_soc_pcm_runtime *rtd;
1283 int i;
1284
1285 if (dir == SND_SOC_DAPM_DIR_OUT) {
1286 list_for_each_entry(rtd, &card->rtd_list, list) {
1287 if (!rtd->dai_link->no_pcm)
1288 continue;
1289
1290 if (rtd->cpu_dai->playback_widget == widget)
1291 return true;
1292
1293 for (i = 0; i < rtd->num_codecs; ++i) {
1294 struct snd_soc_dai *dai = rtd->codec_dais[i];
1295 if (dai->playback_widget == widget)
1296 return true;
1297 }
1298 }
1299 } else { /* SND_SOC_DAPM_DIR_IN */
1300 list_for_each_entry(rtd, &card->rtd_list, list) {
1301 if (!rtd->dai_link->no_pcm)
1302 continue;
1303
1304 if (rtd->cpu_dai->capture_widget == widget)
1305 return true;
1306
1307 for (i = 0; i < rtd->num_codecs; ++i) {
1308 struct snd_soc_dai *dai = rtd->codec_dais[i];
1309 if (dai->capture_widget == widget)
1310 return true;
1311 }
1312 }
1313 }
1314
1315 return false;
1316}
1317
Liam Girdwood23607022014-01-17 17:03:55 +00001318int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001319 int stream, struct snd_soc_dapm_widget_list **list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001320{
1321 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001322 int paths;
1323
Liam Girdwood01d75842012-04-25 12:12:49 +01001324 /* get number of valid DAI paths and their widgets */
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001325 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001326 dpcm_end_walk_at_be);
Liam Girdwood01d75842012-04-25 12:12:49 +01001327
Liam Girdwood103d84a2012-11-19 14:39:15 +00001328 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001329 stream ? "capture" : "playback");
1330
Liam Girdwood01d75842012-04-25 12:12:49 +01001331 return paths;
1332}
1333
Liam Girdwood01d75842012-04-25 12:12:49 +01001334static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1335 struct snd_soc_dapm_widget_list **list_)
1336{
1337 struct snd_soc_dpcm *dpcm;
1338 struct snd_soc_dapm_widget_list *list = *list_;
1339 struct snd_soc_dapm_widget *widget;
1340 int prune = 0;
1341
1342 /* Destroy any old FE <--> BE connections */
1343 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001344 unsigned int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001345
1346 /* is there a valid CPU DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001347 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001348
1349 /* prune the BE if it's no longer in our active list */
1350 if (widget && widget_in_list(list, widget))
1351 continue;
1352
1353 /* is there a valid CODEC DAI widget for this BE */
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001354 for (i = 0; i < dpcm->be->num_codecs; i++) {
1355 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1356 widget = dai_get_widget(dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001357
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001358 /* prune the BE if it's no longer in our active list */
1359 if (widget && widget_in_list(list, widget))
1360 continue;
1361 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001362
Liam Girdwood103d84a2012-11-19 14:39:15 +00001363 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001364 stream ? "capture" : "playback",
1365 dpcm->be->dai_link->name, fe->dai_link->name);
1366 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1367 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1368 prune++;
1369 }
1370
Liam Girdwood103d84a2012-11-19 14:39:15 +00001371 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001372 return prune;
1373}
1374
1375static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1376 struct snd_soc_dapm_widget_list **list_)
1377{
1378 struct snd_soc_card *card = fe->card;
1379 struct snd_soc_dapm_widget_list *list = *list_;
1380 struct snd_soc_pcm_runtime *be;
1381 int i, new = 0, err;
1382
1383 /* Create any new FE <--> BE connections */
1384 for (i = 0; i < list->num_widgets; i++) {
1385
Mark Brown46162742013-06-05 19:36:11 +01001386 switch (list->widgets[i]->id) {
1387 case snd_soc_dapm_dai_in:
Koro Chenc5b85402015-07-06 10:02:10 +08001388 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1389 continue;
1390 break;
Mark Brown46162742013-06-05 19:36:11 +01001391 case snd_soc_dapm_dai_out:
Koro Chenc5b85402015-07-06 10:02:10 +08001392 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1393 continue;
Mark Brown46162742013-06-05 19:36:11 +01001394 break;
1395 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001396 continue;
Mark Brown46162742013-06-05 19:36:11 +01001397 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001398
1399 /* is there a valid BE rtd for this widget */
1400 be = dpcm_get_be(card, list->widgets[i], stream);
1401 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001402 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001403 list->widgets[i]->name);
1404 continue;
1405 }
1406
1407 /* make sure BE is a real BE */
1408 if (!be->dai_link->no_pcm)
1409 continue;
1410
1411 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001412 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001413 continue;
1414
1415 /* newly connected FE and BE */
1416 err = dpcm_be_connect(fe, be, stream);
1417 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001418 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001419 list->widgets[i]->name);
1420 break;
1421 } else if (err == 0) /* already connected */
1422 continue;
1423
1424 /* new */
1425 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1426 new++;
1427 }
1428
Liam Girdwood103d84a2012-11-19 14:39:15 +00001429 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001430 return new;
1431}
1432
1433/*
1434 * Find the corresponding BE DAIs that source or sink audio to this
1435 * FE substream.
1436 */
Liam Girdwood23607022014-01-17 17:03:55 +00001437int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001438 int stream, struct snd_soc_dapm_widget_list **list, int new)
1439{
1440 if (new)
1441 return dpcm_add_paths(fe, stream, list);
1442 else
1443 return dpcm_prune_paths(fe, stream, list);
1444}
1445
Liam Girdwood23607022014-01-17 17:03:55 +00001446void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001447{
1448 struct snd_soc_dpcm *dpcm;
1449
1450 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1451 dpcm->be->dpcm[stream].runtime_update =
1452 SND_SOC_DPCM_UPDATE_NO;
1453}
1454
1455static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1456 int stream)
1457{
1458 struct snd_soc_dpcm *dpcm;
1459
1460 /* disable any enabled and non active backends */
1461 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1462
1463 struct snd_soc_pcm_runtime *be = dpcm->be;
1464 struct snd_pcm_substream *be_substream =
1465 snd_soc_dpcm_get_substream(be, stream);
1466
1467 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001468 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001469 stream ? "capture" : "playback",
1470 be->dpcm[stream].state);
1471
1472 if (--be->dpcm[stream].users != 0)
1473 continue;
1474
1475 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1476 continue;
1477
1478 soc_pcm_close(be_substream);
1479 be_substream->runtime = NULL;
1480 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1481 }
1482}
1483
Liam Girdwood23607022014-01-17 17:03:55 +00001484int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001485{
1486 struct snd_soc_dpcm *dpcm;
1487 int err, count = 0;
1488
1489 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1490 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1491
1492 struct snd_soc_pcm_runtime *be = dpcm->be;
1493 struct snd_pcm_substream *be_substream =
1494 snd_soc_dpcm_get_substream(be, stream);
1495
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001496 if (!be_substream) {
1497 dev_err(be->dev, "ASoC: no backend %s stream\n",
1498 stream ? "capture" : "playback");
1499 continue;
1500 }
1501
Liam Girdwood01d75842012-04-25 12:12:49 +01001502 /* is this op for this BE ? */
1503 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1504 continue;
1505
1506 /* first time the dpcm is open ? */
1507 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001508 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001509 stream ? "capture" : "playback",
1510 be->dpcm[stream].state);
1511
1512 if (be->dpcm[stream].users++ != 0)
1513 continue;
1514
1515 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1516 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1517 continue;
1518
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001519 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1520 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001521
1522 be_substream->runtime = be->dpcm[stream].runtime;
1523 err = soc_pcm_open(be_substream);
1524 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001525 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001526 be->dpcm[stream].users--;
1527 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001528 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001529 stream ? "capture" : "playback",
1530 be->dpcm[stream].state);
1531
1532 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1533 goto unwind;
1534 }
1535
1536 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1537 count++;
1538 }
1539
1540 return count;
1541
1542unwind:
1543 /* disable any enabled and non active backends */
1544 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1545 struct snd_soc_pcm_runtime *be = dpcm->be;
1546 struct snd_pcm_substream *be_substream =
1547 snd_soc_dpcm_get_substream(be, stream);
1548
1549 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1550 continue;
1551
1552 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001553 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001554 stream ? "capture" : "playback",
1555 be->dpcm[stream].state);
1556
1557 if (--be->dpcm[stream].users != 0)
1558 continue;
1559
1560 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1561 continue;
1562
1563 soc_pcm_close(be_substream);
1564 be_substream->runtime = NULL;
1565 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1566 }
1567
1568 return err;
1569}
1570
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001571static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001572 struct snd_soc_pcm_stream *stream,
1573 u64 formats)
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001574{
1575 runtime->hw.rate_min = stream->rate_min;
1576 runtime->hw.rate_max = stream->rate_max;
1577 runtime->hw.channels_min = stream->channels_min;
1578 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001579 if (runtime->hw.formats)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001580 runtime->hw.formats &= formats & stream->formats;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001581 else
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001582 runtime->hw.formats = formats & stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001583 runtime->hw.rates = stream->rates;
1584}
1585
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001586static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1587{
1588 struct snd_soc_pcm_runtime *fe = substream->private_data;
1589 struct snd_soc_dpcm *dpcm;
1590 u64 formats = ULLONG_MAX;
1591 int stream = substream->stream;
1592
1593 if (!fe->dai_link->dpcm_merged_format)
1594 return formats;
1595
1596 /*
1597 * It returns merged BE codec format
1598 * if FE want to use it (= dpcm_merged_format)
1599 */
1600
1601 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1602 struct snd_soc_pcm_runtime *be = dpcm->be;
1603 struct snd_soc_dai_driver *codec_dai_drv;
1604 struct snd_soc_pcm_stream *codec_stream;
1605 int i;
1606
1607 for (i = 0; i < be->num_codecs; i++) {
1608 codec_dai_drv = be->codec_dais[i]->driver;
1609 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1610 codec_stream = &codec_dai_drv->playback;
1611 else
1612 codec_stream = &codec_dai_drv->capture;
1613
1614 formats &= codec_stream->formats;
1615 }
1616 }
1617
1618 return formats;
1619}
1620
Mark Brown45c0a182012-05-09 21:46:27 +01001621static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001622{
1623 struct snd_pcm_runtime *runtime = substream->runtime;
1624 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1625 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1626 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001627 u64 format = dpcm_runtime_base_format(substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001628
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001629 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001630 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001631 else
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001632 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
Liam Girdwood01d75842012-04-25 12:12:49 +01001633}
1634
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001635static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1636
1637/* Set FE's runtime_update state; the state is protected via PCM stream lock
1638 * for avoiding the race with trigger callback.
1639 * If the state is unset and a trigger is pending while the previous operation,
1640 * process the pending trigger action here.
1641 */
1642static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1643 int stream, enum snd_soc_dpcm_update state)
1644{
1645 struct snd_pcm_substream *substream =
1646 snd_soc_dpcm_get_substream(fe, stream);
1647
1648 snd_pcm_stream_lock_irq(substream);
1649 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1650 dpcm_fe_dai_do_trigger(substream,
1651 fe->dpcm[stream].trigger_pending - 1);
1652 fe->dpcm[stream].trigger_pending = 0;
1653 }
1654 fe->dpcm[stream].runtime_update = state;
1655 snd_pcm_stream_unlock_irq(substream);
1656}
1657
PC Liao906c7d62015-12-11 11:33:51 +08001658static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1659 int stream)
1660{
1661 struct snd_soc_dpcm *dpcm;
1662 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1663 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1664 int err;
1665
1666 /* apply symmetry for FE */
1667 if (soc_pcm_has_symmetry(fe_substream))
1668 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1669
1670 /* Symmetry only applies if we've got an active stream. */
1671 if (fe_cpu_dai->active) {
1672 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1673 if (err < 0)
1674 return err;
1675 }
1676
1677 /* apply symmetry for BE */
1678 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1679 struct snd_soc_pcm_runtime *be = dpcm->be;
1680 struct snd_pcm_substream *be_substream =
1681 snd_soc_dpcm_get_substream(be, stream);
1682 struct snd_soc_pcm_runtime *rtd = be_substream->private_data;
1683 int i;
1684
Jeeja KPf1176612016-09-06 14:17:55 +05301685 if (rtd->dai_link->be_hw_params_fixup)
1686 continue;
1687
PC Liao906c7d62015-12-11 11:33:51 +08001688 if (soc_pcm_has_symmetry(be_substream))
1689 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1690
1691 /* Symmetry only applies if we've got an active stream. */
1692 if (rtd->cpu_dai->active) {
1693 err = soc_pcm_apply_symmetry(be_substream, rtd->cpu_dai);
1694 if (err < 0)
1695 return err;
1696 }
1697
1698 for (i = 0; i < rtd->num_codecs; i++) {
1699 if (rtd->codec_dais[i]->active) {
1700 err = soc_pcm_apply_symmetry(be_substream,
1701 rtd->codec_dais[i]);
1702 if (err < 0)
1703 return err;
1704 }
1705 }
1706 }
1707
1708 return 0;
1709}
1710
Liam Girdwood01d75842012-04-25 12:12:49 +01001711static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1712{
1713 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1714 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1715 int stream = fe_substream->stream, ret = 0;
1716
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
1719 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1720 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001721 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001722 goto be_err;
1723 }
1724
Liam Girdwood103d84a2012-11-19 14:39:15 +00001725 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001726
1727 /* start the DAI frontend */
1728 ret = soc_pcm_open(fe_substream);
1729 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001730 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001731 goto unwind;
1732 }
1733
1734 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1735
1736 dpcm_set_fe_runtime(fe_substream);
1737 snd_pcm_limit_hw_rates(runtime);
1738
PC Liao906c7d62015-12-11 11:33:51 +08001739 ret = dpcm_apply_symmetry(fe_substream, stream);
1740 if (ret < 0) {
1741 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1742 ret);
1743 goto unwind;
1744 }
1745
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001746 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001747 return 0;
1748
1749unwind:
1750 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1751be_err:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001752 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001753 return ret;
1754}
1755
Liam Girdwood23607022014-01-17 17:03:55 +00001756int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001757{
1758 struct snd_soc_dpcm *dpcm;
1759
1760 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1761 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1762
1763 struct snd_soc_pcm_runtime *be = dpcm->be;
1764 struct snd_pcm_substream *be_substream =
1765 snd_soc_dpcm_get_substream(be, stream);
1766
1767 /* is this op for this BE ? */
1768 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1769 continue;
1770
1771 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001772 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001773 stream ? "capture" : "playback",
1774 be->dpcm[stream].state);
1775
1776 if (--be->dpcm[stream].users != 0)
1777 continue;
1778
1779 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1780 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1781 continue;
1782
Liam Girdwood103d84a2012-11-19 14:39:15 +00001783 dev_dbg(be->dev, "ASoC: close BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001784 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001785
1786 soc_pcm_close(be_substream);
1787 be_substream->runtime = NULL;
1788
1789 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1790 }
1791 return 0;
1792}
1793
1794static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1795{
1796 struct snd_soc_pcm_runtime *fe = substream->private_data;
1797 int stream = substream->stream;
1798
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001799 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001800
1801 /* shutdown the BEs */
1802 dpcm_be_dai_shutdown(fe, substream->stream);
1803
Liam Girdwood103d84a2012-11-19 14:39:15 +00001804 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001805
1806 /* now shutdown the frontend */
1807 soc_pcm_close(substream);
1808
1809 /* run the stream event for each BE */
1810 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1811
1812 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001813 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001814 return 0;
1815}
1816
Liam Girdwood23607022014-01-17 17:03:55 +00001817int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001818{
1819 struct snd_soc_dpcm *dpcm;
1820
1821 /* only hw_params backends that are either sinks or sources
1822 * to this frontend DAI */
1823 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1824
1825 struct snd_soc_pcm_runtime *be = dpcm->be;
1826 struct snd_pcm_substream *be_substream =
1827 snd_soc_dpcm_get_substream(be, stream);
1828
1829 /* is this op for this BE ? */
1830 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1831 continue;
1832
1833 /* only free hw when no longer used - check all FEs */
1834 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1835 continue;
1836
Qiao Zhou36fba622014-12-03 10:13:43 +08001837 /* do not free hw if this BE is used by other FE */
1838 if (be->dpcm[stream].users > 1)
1839 continue;
1840
Liam Girdwood01d75842012-04-25 12:12:49 +01001841 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1842 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1843 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001844 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Vinod Koul5e82d2b2016-02-01 22:26:40 +05301845 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1846 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01001847 continue;
1848
Liam Girdwood103d84a2012-11-19 14:39:15 +00001849 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001850 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001851
1852 soc_pcm_hw_free(be_substream);
1853
1854 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1855 }
1856
1857 return 0;
1858}
1859
Mark Brown45c0a182012-05-09 21:46:27 +01001860static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001861{
1862 struct snd_soc_pcm_runtime *fe = substream->private_data;
1863 int err, stream = substream->stream;
1864
1865 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001866 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001867
Liam Girdwood103d84a2012-11-19 14:39:15 +00001868 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001869
1870 /* call hw_free on the frontend */
1871 err = soc_pcm_hw_free(substream);
1872 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001873 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001874 fe->dai_link->name);
1875
1876 /* only hw_params backends that are either sinks or sources
1877 * to this frontend DAI */
1878 err = dpcm_be_dai_hw_free(fe, stream);
1879
1880 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001881 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001882
1883 mutex_unlock(&fe->card->mutex);
1884 return 0;
1885}
1886
Liam Girdwood23607022014-01-17 17:03:55 +00001887int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001888{
1889 struct snd_soc_dpcm *dpcm;
1890 int ret;
1891
1892 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1893
1894 struct snd_soc_pcm_runtime *be = dpcm->be;
1895 struct snd_pcm_substream *be_substream =
1896 snd_soc_dpcm_get_substream(be, stream);
1897
1898 /* is this op for this BE ? */
1899 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1900 continue;
1901
Liam Girdwood01d75842012-04-25 12:12:49 +01001902 /* copy params for each dpcm */
1903 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1904 sizeof(struct snd_pcm_hw_params));
1905
1906 /* perform any hw_params fixups */
1907 if (be->dai_link->be_hw_params_fixup) {
1908 ret = be->dai_link->be_hw_params_fixup(be,
1909 &dpcm->hw_params);
1910 if (ret < 0) {
1911 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001912 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001913 ret);
1914 goto unwind;
1915 }
1916 }
1917
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00001918 /* only allow hw_params() if no connected FEs are running */
1919 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1920 continue;
1921
1922 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1923 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1924 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1925 continue;
1926
1927 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001928 be->dai_link->name);
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00001929
Liam Girdwood01d75842012-04-25 12:12:49 +01001930 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1931 if (ret < 0) {
1932 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001933 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001934 goto unwind;
1935 }
1936
1937 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1938 }
1939 return 0;
1940
1941unwind:
1942 /* disable any enabled and non active backends */
1943 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1944 struct snd_soc_pcm_runtime *be = dpcm->be;
1945 struct snd_pcm_substream *be_substream =
1946 snd_soc_dpcm_get_substream(be, stream);
1947
1948 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1949 continue;
1950
1951 /* only allow hw_free() if no connected FEs are running */
1952 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1953 continue;
1954
1955 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1956 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1957 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1958 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1959 continue;
1960
1961 soc_pcm_hw_free(be_substream);
1962 }
1963
1964 return ret;
1965}
1966
Mark Brown45c0a182012-05-09 21:46:27 +01001967static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1968 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001969{
1970 struct snd_soc_pcm_runtime *fe = substream->private_data;
1971 int ret, stream = substream->stream;
1972
1973 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001974 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001975
1976 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1977 sizeof(struct snd_pcm_hw_params));
1978 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1979 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001980 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001981 goto out;
1982 }
1983
Liam Girdwood103d84a2012-11-19 14:39:15 +00001984 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001985 fe->dai_link->name, params_rate(params),
1986 params_channels(params), params_format(params));
1987
1988 /* call hw_params on the frontend */
1989 ret = soc_pcm_hw_params(substream, params);
1990 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001991 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001992 dpcm_be_dai_hw_free(fe, stream);
1993 } else
1994 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1995
1996out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001997 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001998 mutex_unlock(&fe->card->mutex);
1999 return ret;
2000}
2001
2002static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2003 struct snd_pcm_substream *substream, int cmd)
2004{
2005 int ret;
2006
Liam Girdwood103d84a2012-11-19 14:39:15 +00002007 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
彭东林94d215c2016-09-26 08:29:31 +00002008 dpcm->be->dai_link->name, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002009
2010 ret = soc_pcm_trigger(substream, cmd);
2011 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002012 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002013
2014 return ret;
2015}
2016
Liam Girdwood23607022014-01-17 17:03:55 +00002017int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01002018 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002019{
2020 struct snd_soc_dpcm *dpcm;
2021 int ret = 0;
2022
2023 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2024
2025 struct snd_soc_pcm_runtime *be = dpcm->be;
2026 struct snd_pcm_substream *be_substream =
2027 snd_soc_dpcm_get_substream(be, stream);
2028
2029 /* is this op for this BE ? */
2030 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2031 continue;
2032
2033 switch (cmd) {
2034 case SNDRV_PCM_TRIGGER_START:
2035 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2036 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2037 continue;
2038
2039 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2040 if (ret)
2041 return ret;
2042
2043 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2044 break;
2045 case SNDRV_PCM_TRIGGER_RESUME:
2046 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2047 continue;
2048
2049 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2050 if (ret)
2051 return ret;
2052
2053 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2054 break;
2055 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2056 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2057 continue;
2058
2059 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2060 if (ret)
2061 return ret;
2062
2063 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2064 break;
2065 case SNDRV_PCM_TRIGGER_STOP:
2066 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2067 continue;
2068
2069 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2070 continue;
2071
2072 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2073 if (ret)
2074 return ret;
2075
2076 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2077 break;
2078 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08002079 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01002080 continue;
2081
2082 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2083 continue;
2084
2085 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2086 if (ret)
2087 return ret;
2088
2089 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2090 break;
2091 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2092 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2093 continue;
2094
2095 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2096 continue;
2097
2098 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2099 if (ret)
2100 return ret;
2101
2102 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2103 break;
2104 }
2105 }
2106
2107 return ret;
2108}
2109EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2110
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002111static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002112{
2113 struct snd_soc_pcm_runtime *fe = substream->private_data;
2114 int stream = substream->stream, ret;
2115 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2116
2117 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2118
2119 switch (trigger) {
2120 case SND_SOC_DPCM_TRIGGER_PRE:
2121 /* call trigger on the frontend before the backend. */
2122
Liam Girdwood103d84a2012-11-19 14:39:15 +00002123 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002124 fe->dai_link->name, cmd);
2125
2126 ret = soc_pcm_trigger(substream, cmd);
2127 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002128 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002129 goto out;
2130 }
2131
2132 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2133 break;
2134 case SND_SOC_DPCM_TRIGGER_POST:
2135 /* call trigger on the frontend after the backend. */
2136
2137 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2138 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002139 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002140 goto out;
2141 }
2142
Liam Girdwood103d84a2012-11-19 14:39:15 +00002143 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002144 fe->dai_link->name, cmd);
2145
2146 ret = soc_pcm_trigger(substream, cmd);
2147 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002148 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2149 /* bespoke trigger() - handles both FE and BEs */
2150
Liam Girdwood103d84a2012-11-19 14:39:15 +00002151 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002152 fe->dai_link->name, cmd);
2153
2154 ret = soc_pcm_bespoke_trigger(substream, cmd);
2155 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002156 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002157 goto out;
2158 }
2159 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002160 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00002161 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01002162 fe->dai_link->name);
2163 ret = -EINVAL;
2164 goto out;
2165 }
2166
2167 switch (cmd) {
2168 case SNDRV_PCM_TRIGGER_START:
2169 case SNDRV_PCM_TRIGGER_RESUME:
2170 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2171 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2172 break;
2173 case SNDRV_PCM_TRIGGER_STOP:
2174 case SNDRV_PCM_TRIGGER_SUSPEND:
Liam Girdwood01d75842012-04-25 12:12:49 +01002175 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2176 break;
Patrick Lai9f169b92016-12-31 22:44:39 -08002177 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2178 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2179 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002180 }
2181
2182out:
2183 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2184 return ret;
2185}
2186
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002187static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2188{
2189 struct snd_soc_pcm_runtime *fe = substream->private_data;
2190 int stream = substream->stream;
2191
2192 /* if FE's runtime_update is already set, we're in race;
2193 * process this trigger later at exit
2194 */
2195 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2196 fe->dpcm[stream].trigger_pending = cmd + 1;
2197 return 0; /* delayed, assuming it's successful */
2198 }
2199
2200 /* we're alone, let's trigger */
2201 return dpcm_fe_dai_do_trigger(substream, cmd);
2202}
2203
Liam Girdwood23607022014-01-17 17:03:55 +00002204int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002205{
2206 struct snd_soc_dpcm *dpcm;
2207 int ret = 0;
2208
2209 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2210
2211 struct snd_soc_pcm_runtime *be = dpcm->be;
2212 struct snd_pcm_substream *be_substream =
2213 snd_soc_dpcm_get_substream(be, stream);
2214
2215 /* is this op for this BE ? */
2216 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2217 continue;
2218
2219 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
Koro Chen95f444d2015-10-28 10:15:34 +08002220 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2221 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01002222 continue;
2223
Liam Girdwood103d84a2012-11-19 14:39:15 +00002224 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002225 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002226
2227 ret = soc_pcm_prepare(be_substream);
2228 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002229 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002230 ret);
2231 break;
2232 }
2233
2234 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2235 }
2236 return ret;
2237}
2238
Mark Brown45c0a182012-05-09 21:46:27 +01002239static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002240{
2241 struct snd_soc_pcm_runtime *fe = substream->private_data;
2242 int stream = substream->stream, ret = 0;
2243
2244 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2245
Liam Girdwood103d84a2012-11-19 14:39:15 +00002246 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002247
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002248 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002249
2250 /* there is no point preparing this FE if there are no BEs */
2251 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002252 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002253 fe->dai_link->name);
2254 ret = -EINVAL;
2255 goto out;
2256 }
2257
2258 ret = dpcm_be_dai_prepare(fe, substream->stream);
2259 if (ret < 0)
2260 goto out;
2261
2262 /* call prepare on the frontend */
2263 ret = soc_pcm_prepare(substream);
2264 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002265 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002266 fe->dai_link->name);
2267 goto out;
2268 }
2269
2270 /* run the stream event for each BE */
2271 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2272 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2273
2274out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002275 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002276 mutex_unlock(&fe->card->mutex);
2277
2278 return ret;
2279}
2280
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002281static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2282 unsigned int cmd, void *arg)
2283{
2284 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2285 struct snd_soc_platform *platform = rtd->platform;
2286
Mark Brownc5914b02013-10-30 17:47:39 -07002287 if (platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002288 return platform->driver->ops->ioctl(substream, cmd, arg);
2289 return snd_pcm_lib_ioctl(substream, cmd, arg);
2290}
2291
Liam Girdwood618dae12012-04-25 12:12:51 +01002292static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2293{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002294 struct snd_pcm_substream *substream =
2295 snd_soc_dpcm_get_substream(fe, stream);
2296 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002297 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01002298
Liam Girdwood103d84a2012-11-19 14:39:15 +00002299 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002300 stream ? "capture" : "playback", fe->dai_link->name);
2301
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002302 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2303 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002304 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002305 fe->dai_link->name);
2306
2307 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2308 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002309 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002310 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002311 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002312 fe->dai_link->name);
2313
2314 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2315 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002316 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002317 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002318
2319 err = dpcm_be_dai_hw_free(fe, stream);
2320 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002321 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002322
2323 err = dpcm_be_dai_shutdown(fe, stream);
2324 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002325 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002326
2327 /* run the stream event for each BE */
2328 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2329
2330 return 0;
2331}
2332
2333static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2334{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002335 struct snd_pcm_substream *substream =
2336 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002337 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002338 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002339 int ret;
2340
Liam Girdwood103d84a2012-11-19 14:39:15 +00002341 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002342 stream ? "capture" : "playback", fe->dai_link->name);
2343
2344 /* Only start the BE if the FE is ready */
2345 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2346 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2347 return -EINVAL;
2348
2349 /* startup must always be called for new BEs */
2350 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002351 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002352 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01002353
2354 /* keep going if FE state is > open */
2355 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2356 return 0;
2357
2358 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002359 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002360 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01002361
2362 /* keep going if FE state is > hw_params */
2363 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2364 return 0;
2365
2366
2367 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002368 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002369 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002370
2371 /* run the stream event for each BE */
2372 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2373
2374 /* keep going if FE state is > prepare */
2375 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2376 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2377 return 0;
2378
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002379 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2380 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002381 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002382 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01002383
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002384 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2385 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002386 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002387 goto hw_free;
2388 }
2389 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002390 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002391 fe->dai_link->name);
2392
2393 ret = dpcm_be_dai_trigger(fe, stream,
2394 SNDRV_PCM_TRIGGER_START);
2395 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002396 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002397 goto hw_free;
2398 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002399 }
2400
2401 return 0;
2402
2403hw_free:
2404 dpcm_be_dai_hw_free(fe, stream);
2405close:
2406 dpcm_be_dai_shutdown(fe, stream);
2407disconnect:
2408 /* disconnect any non started BEs */
2409 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2410 struct snd_soc_pcm_runtime *be = dpcm->be;
2411 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2412 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2413 }
2414
2415 return ret;
2416}
2417
2418static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2419{
2420 int ret;
2421
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002422 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002423 ret = dpcm_run_update_startup(fe, stream);
2424 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002425 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002426 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002427
2428 return ret;
2429}
2430
2431static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2432{
2433 int ret;
2434
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002435 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002436 ret = dpcm_run_update_shutdown(fe, stream);
2437 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002438 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002439 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002440
2441 return ret;
2442}
2443
2444/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2445 * any DAI links.
2446 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002447int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002448{
Mengdong Lin1a497982015-11-18 02:34:11 -05002449 struct snd_soc_pcm_runtime *fe;
2450 int old, new, paths;
Liam Girdwood618dae12012-04-25 12:12:51 +01002451
Liam Girdwood618dae12012-04-25 12:12:51 +01002452 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Mengdong Lin1a497982015-11-18 02:34:11 -05002453 list_for_each_entry(fe, &card->rtd_list, list) {
Liam Girdwood618dae12012-04-25 12:12:51 +01002454 struct snd_soc_dapm_widget_list *list;
Liam Girdwood618dae12012-04-25 12:12:51 +01002455
2456 /* make sure link is FE */
2457 if (!fe->dai_link->dynamic)
2458 continue;
2459
2460 /* only check active links */
2461 if (!fe->cpu_dai->active)
2462 continue;
2463
2464 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002465 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002466 fe->dai_link->name);
2467
2468 /* skip if FE doesn't have playback capability */
Qiao Zhou075207d2014-11-17 16:02:57 +08002469 if (!fe->cpu_dai->driver->playback.channels_min
2470 || !fe->codec_dai->driver->playback.channels_min)
2471 goto capture;
2472
2473 /* skip if FE isn't currently playing */
2474 if (!fe->cpu_dai->playback_active
2475 || !fe->codec_dai->playback_active)
Liam Girdwood618dae12012-04-25 12:12:51 +01002476 goto capture;
2477
2478 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2479 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002480 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002481 fe->dai_link->name, "playback");
2482 mutex_unlock(&card->mutex);
2483 return paths;
2484 }
2485
2486 /* update any new playback paths */
2487 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2488 if (new) {
2489 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2490 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2491 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2492 }
2493
2494 /* update any old playback paths */
2495 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2496 if (old) {
2497 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2498 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2499 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2500 }
2501
Qiao Zhou7ed9de72014-06-04 19:42:06 +08002502 dpcm_path_put(&list);
Liam Girdwood618dae12012-04-25 12:12:51 +01002503capture:
2504 /* skip if FE doesn't have capture capability */
Qiao Zhou075207d2014-11-17 16:02:57 +08002505 if (!fe->cpu_dai->driver->capture.channels_min
2506 || !fe->codec_dai->driver->capture.channels_min)
2507 continue;
2508
2509 /* skip if FE isn't currently capturing */
2510 if (!fe->cpu_dai->capture_active
2511 || !fe->codec_dai->capture_active)
Liam Girdwood618dae12012-04-25 12:12:51 +01002512 continue;
2513
2514 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2515 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002516 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002517 fe->dai_link->name, "capture");
2518 mutex_unlock(&card->mutex);
2519 return paths;
2520 }
2521
2522 /* update any new capture paths */
2523 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2524 if (new) {
2525 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2526 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2527 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2528 }
2529
2530 /* update any old capture paths */
2531 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2532 if (old) {
2533 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2534 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2535 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2536 }
2537
2538 dpcm_path_put(&list);
2539 }
2540
2541 mutex_unlock(&card->mutex);
2542 return 0;
2543}
Liam Girdwood01d75842012-04-25 12:12:49 +01002544int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2545{
2546 struct snd_soc_dpcm *dpcm;
2547 struct list_head *clients =
2548 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2549
2550 list_for_each_entry(dpcm, clients, list_be) {
2551
2552 struct snd_soc_pcm_runtime *be = dpcm->be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002553 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01002554
2555 if (be->dai_link->ignore_suspend)
2556 continue;
2557
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002558 for (i = 0; i < be->num_codecs; i++) {
2559 struct snd_soc_dai *dai = be->codec_dais[i];
2560 struct snd_soc_dai_driver *drv = dai->driver;
Liam Girdwood01d75842012-04-25 12:12:49 +01002561
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002562 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2563 be->dai_link->name);
2564
2565 if (drv->ops && drv->ops->digital_mute &&
2566 dai->playback_active)
2567 drv->ops->digital_mute(dai, mute);
2568 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002569 }
2570
2571 return 0;
2572}
2573
Mark Brown45c0a182012-05-09 21:46:27 +01002574static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002575{
2576 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2577 struct snd_soc_dpcm *dpcm;
2578 struct snd_soc_dapm_widget_list *list;
2579 int ret;
2580 int stream = fe_substream->stream;
2581
2582 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2583 fe->dpcm[stream].runtime = fe_substream->runtime;
2584
Qiao Zhou8f70e512014-09-10 17:54:07 +08002585 ret = dpcm_path_get(fe, stream, &list);
2586 if (ret < 0) {
2587 mutex_unlock(&fe->card->mutex);
2588 return ret;
2589 } else if (ret == 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002590 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002591 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002592 }
2593
2594 /* calculate valid and active FE <-> BE dpcms */
2595 dpcm_process_paths(fe, stream, &list, 1);
2596
2597 ret = dpcm_fe_dai_startup(fe_substream);
2598 if (ret < 0) {
2599 /* clean up all links */
2600 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2601 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2602
2603 dpcm_be_disconnect(fe, stream);
2604 fe->dpcm[stream].runtime = NULL;
2605 }
2606
2607 dpcm_clear_pending_state(fe, stream);
2608 dpcm_path_put(&list);
2609 mutex_unlock(&fe->card->mutex);
2610 return ret;
2611}
2612
Mark Brown45c0a182012-05-09 21:46:27 +01002613static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002614{
2615 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2616 struct snd_soc_dpcm *dpcm;
2617 int stream = fe_substream->stream, ret;
2618
2619 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2620 ret = dpcm_fe_dai_shutdown(fe_substream);
2621
2622 /* mark FE's links ready to prune */
2623 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2624 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2625
2626 dpcm_be_disconnect(fe, stream);
2627
2628 fe->dpcm[stream].runtime = NULL;
2629 mutex_unlock(&fe->card->mutex);
2630 return ret;
2631}
2632
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02002633static void soc_pcm_private_free(struct snd_pcm *pcm)
2634{
2635 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
2636 struct snd_soc_platform *platform = rtd->platform;
2637
2638 /* need to sync the delayed work before releasing resources */
2639 flush_delayed_work(&rtd->delayed_work);
2640 if (platform->driver->pcm_free)
2641 platform->driver->pcm_free(pcm);
2642}
2643
Liam Girdwoodddee6272011-06-09 14:45:53 +01002644/* create a new pcm */
2645int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2646{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002647 struct snd_soc_platform *platform = rtd->platform;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002648 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002649 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2650 struct snd_pcm *pcm;
2651 char new_name[64];
2652 int ret = 0, playback = 0, capture = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002653 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002654
Liam Girdwood01d75842012-04-25 12:12:49 +01002655 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002656 playback = rtd->dai_link->dpcm_playback;
2657 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002658 } else {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002659 for (i = 0; i < rtd->num_codecs; i++) {
2660 codec_dai = rtd->codec_dais[i];
2661 if (codec_dai->driver->playback.channels_min)
2662 playback = 1;
2663 if (codec_dai->driver->capture.channels_min)
2664 capture = 1;
2665 }
2666
2667 capture = capture && cpu_dai->driver->capture.channels_min;
2668 playback = playback && cpu_dai->driver->playback.channels_min;
Liam Girdwood01d75842012-04-25 12:12:49 +01002669 }
Sangsu Parka5002312012-01-02 17:15:10 +09002670
Fabio Estevamd6bead02013-08-29 10:32:13 -03002671 if (rtd->dai_link->playback_only) {
2672 playback = 1;
2673 capture = 0;
2674 }
2675
2676 if (rtd->dai_link->capture_only) {
2677 playback = 0;
2678 capture = 1;
2679 }
2680
Liam Girdwood01d75842012-04-25 12:12:49 +01002681 /* create the PCM */
2682 if (rtd->dai_link->no_pcm) {
2683 snprintf(new_name, sizeof(new_name), "(%s)",
2684 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002685
Liam Girdwood01d75842012-04-25 12:12:49 +01002686 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2687 playback, capture, &pcm);
2688 } else {
2689 if (rtd->dai_link->dynamic)
2690 snprintf(new_name, sizeof(new_name), "%s (*)",
2691 rtd->dai_link->stream_name);
2692 else
2693 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002694 rtd->dai_link->stream_name,
2695 (rtd->num_codecs > 1) ?
2696 "multicodec" : rtd->codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002697
Liam Girdwood01d75842012-04-25 12:12:49 +01002698 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2699 capture, &pcm);
2700 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002701 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002702 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002703 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002704 return ret;
2705 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002706 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002707
2708 /* DAPM dai link stream work */
2709 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2710
Vinod Koul48c76992015-02-12 09:59:53 +05302711 pcm->nonatomic = rtd->dai_link->nonatomic;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002712 rtd->pcm = pcm;
2713 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002714
2715 if (rtd->dai_link->no_pcm) {
2716 if (playback)
2717 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2718 if (capture)
2719 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2720 goto out;
2721 }
2722
2723 /* ASoC PCM operations */
2724 if (rtd->dai_link->dynamic) {
2725 rtd->ops.open = dpcm_fe_dai_open;
2726 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2727 rtd->ops.prepare = dpcm_fe_dai_prepare;
2728 rtd->ops.trigger = dpcm_fe_dai_trigger;
2729 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2730 rtd->ops.close = dpcm_fe_dai_close;
2731 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002732 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002733 } else {
2734 rtd->ops.open = soc_pcm_open;
2735 rtd->ops.hw_params = soc_pcm_hw_params;
2736 rtd->ops.prepare = soc_pcm_prepare;
2737 rtd->ops.trigger = soc_pcm_trigger;
2738 rtd->ops.hw_free = soc_pcm_hw_free;
2739 rtd->ops.close = soc_pcm_close;
2740 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002741 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002742 }
2743
Liam Girdwoodddee6272011-06-09 14:45:53 +01002744 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002745 rtd->ops.ack = platform->driver->ops->ack;
Takashi Iwai29d1a872017-05-10 20:02:35 +02002746 rtd->ops.copy_user = platform->driver->ops->copy_user;
2747 rtd->ops.copy_kernel = platform->driver->ops->copy_kernel;
2748 rtd->ops.fill_silence = platform->driver->ops->fill_silence;
Liam Girdwood01d75842012-04-25 12:12:49 +01002749 rtd->ops.page = platform->driver->ops->page;
2750 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002751 }
2752
2753 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002754 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002755
2756 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002757 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002758
Johan Hovoldc641e5b2017-07-12 17:55:29 +02002759 if (platform->driver->pcm_new) {
2760 ret = platform->driver->pcm_new(rtd);
2761 if (ret < 0) {
2762 dev_err(platform->dev,
2763 "ASoC: pcm constructor failed: %d\n",
2764 ret);
2765 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002766 }
2767 }
Johan Hovoldc641e5b2017-07-12 17:55:29 +02002768
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02002769 pcm->private_free = soc_pcm_private_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002770out:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002771 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2772 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2773 cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002774 return ret;
2775}
Liam Girdwood01d75842012-04-25 12:12:49 +01002776
2777/* is the current PCM operation for this FE ? */
2778int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2779{
2780 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2781 return 1;
2782 return 0;
2783}
2784EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2785
2786/* is the current PCM operation for this BE ? */
2787int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2788 struct snd_soc_pcm_runtime *be, int stream)
2789{
2790 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2791 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2792 be->dpcm[stream].runtime_update))
2793 return 1;
2794 return 0;
2795}
2796EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2797
2798/* get the substream for this BE */
2799struct snd_pcm_substream *
2800 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2801{
2802 return be->pcm->streams[stream].substream;
2803}
2804EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2805
2806/* get the BE runtime state */
2807enum snd_soc_dpcm_state
2808 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2809{
2810 return be->dpcm[stream].state;
2811}
2812EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2813
2814/* set the BE runtime state */
2815void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2816 int stream, enum snd_soc_dpcm_state state)
2817{
2818 be->dpcm[stream].state = state;
2819}
2820EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2821
2822/*
2823 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2824 * are not running, paused or suspended for the specified stream direction.
2825 */
2826int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2827 struct snd_soc_pcm_runtime *be, int stream)
2828{
2829 struct snd_soc_dpcm *dpcm;
2830 int state;
2831
2832 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2833
2834 if (dpcm->fe == fe)
2835 continue;
2836
2837 state = dpcm->fe->dpcm[stream].state;
2838 if (state == SND_SOC_DPCM_STATE_START ||
2839 state == SND_SOC_DPCM_STATE_PAUSED ||
2840 state == SND_SOC_DPCM_STATE_SUSPEND)
2841 return 0;
2842 }
2843
2844 /* it's safe to free/stop this BE DAI */
2845 return 1;
2846}
2847EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2848
2849/*
2850 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2851 * running, paused or suspended for the specified stream direction.
2852 */
2853int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2854 struct snd_soc_pcm_runtime *be, int stream)
2855{
2856 struct snd_soc_dpcm *dpcm;
2857 int state;
2858
2859 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2860
2861 if (dpcm->fe == fe)
2862 continue;
2863
2864 state = dpcm->fe->dpcm[stream].state;
2865 if (state == SND_SOC_DPCM_STATE_START ||
2866 state == SND_SOC_DPCM_STATE_PAUSED ||
2867 state == SND_SOC_DPCM_STATE_SUSPEND ||
2868 state == SND_SOC_DPCM_STATE_PREPARE)
2869 return 0;
2870 }
2871
2872 /* it's safe to change hw_params */
2873 return 1;
2874}
2875EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002876
2877#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen852801412016-11-22 11:29:14 +01002878static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002879{
2880 switch (state) {
2881 case SND_SOC_DPCM_STATE_NEW:
2882 return "new";
2883 case SND_SOC_DPCM_STATE_OPEN:
2884 return "open";
2885 case SND_SOC_DPCM_STATE_HW_PARAMS:
2886 return "hw_params";
2887 case SND_SOC_DPCM_STATE_PREPARE:
2888 return "prepare";
2889 case SND_SOC_DPCM_STATE_START:
2890 return "start";
2891 case SND_SOC_DPCM_STATE_STOP:
2892 return "stop";
2893 case SND_SOC_DPCM_STATE_SUSPEND:
2894 return "suspend";
2895 case SND_SOC_DPCM_STATE_PAUSED:
2896 return "paused";
2897 case SND_SOC_DPCM_STATE_HW_FREE:
2898 return "hw_free";
2899 case SND_SOC_DPCM_STATE_CLOSE:
2900 return "close";
2901 }
2902
2903 return "unknown";
2904}
2905
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002906static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2907 int stream, char *buf, size_t size)
2908{
2909 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2910 struct snd_soc_dpcm *dpcm;
2911 ssize_t offset = 0;
2912
2913 /* FE state */
2914 offset += snprintf(buf + offset, size - offset,
2915 "[%s - %s]\n", fe->dai_link->name,
2916 stream ? "Capture" : "Playback");
2917
2918 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2919 dpcm_state_string(fe->dpcm[stream].state));
2920
2921 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2922 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2923 offset += snprintf(buf + offset, size - offset,
2924 "Hardware Params: "
2925 "Format = %s, Channels = %d, Rate = %d\n",
2926 snd_pcm_format_name(params_format(params)),
2927 params_channels(params),
2928 params_rate(params));
2929
2930 /* BEs state */
2931 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2932
2933 if (list_empty(&fe->dpcm[stream].be_clients)) {
2934 offset += snprintf(buf + offset, size - offset,
2935 " No active DSP links\n");
2936 goto out;
2937 }
2938
2939 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2940 struct snd_soc_pcm_runtime *be = dpcm->be;
2941 params = &dpcm->hw_params;
2942
2943 offset += snprintf(buf + offset, size - offset,
2944 "- %s\n", be->dai_link->name);
2945
2946 offset += snprintf(buf + offset, size - offset,
2947 " State: %s\n",
2948 dpcm_state_string(be->dpcm[stream].state));
2949
2950 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2951 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2952 offset += snprintf(buf + offset, size - offset,
2953 " Hardware Params: "
2954 "Format = %s, Channels = %d, Rate = %d\n",
2955 snd_pcm_format_name(params_format(params)),
2956 params_channels(params),
2957 params_rate(params));
2958 }
2959
2960out:
2961 return offset;
2962}
2963
2964static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2965 size_t count, loff_t *ppos)
2966{
2967 struct snd_soc_pcm_runtime *fe = file->private_data;
2968 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2969 char *buf;
2970
2971 buf = kmalloc(out_count, GFP_KERNEL);
2972 if (!buf)
2973 return -ENOMEM;
2974
2975 if (fe->cpu_dai->driver->playback.channels_min)
2976 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2977 buf + offset, out_count - offset);
2978
2979 if (fe->cpu_dai->driver->capture.channels_min)
2980 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2981 buf + offset, out_count - offset);
2982
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002983 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002984
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002985 kfree(buf);
2986 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002987}
2988
2989static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002990 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002991 .read = dpcm_state_read_file,
2992 .llseek = default_llseek,
2993};
2994
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02002995void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002996{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002997 if (!rtd->dai_link)
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02002998 return;
Mark Brownb3bba9a2012-05-08 10:33:47 +01002999
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003000 if (!rtd->card->debugfs_card_root)
3001 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003002
3003 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3004 rtd->card->debugfs_card_root);
3005 if (!rtd->debugfs_dpcm_root) {
3006 dev_dbg(rtd->dev,
3007 "ASoC: Failed to create dpcm debugfs directory %s\n",
3008 rtd->dai_link->name);
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003009 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003010 }
3011
Fabio Estevamf1e3f402017-07-29 11:40:55 -03003012 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3013 rtd, &dpcm_state_fops);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003014}
3015#endif