blob: 4019bc10897c80a9b2b918c3a7f6ba0c13a24c52 [file] [log] [blame]
Kuninori Morimotoed517582018-07-02 06:22:44 +00001// SPDX-License-Identifier: GPL-2.0+
2//
3// soc-pcm.c -- ALSA SoC PCM
4//
5// Copyright 2005 Wolfson Microelectronics PLC.
6// Copyright 2005 Openedhand Ltd.
7// Copyright (C) 2010 Slimlogic Ltd.
8// Copyright (C) 2010 Texas Instruments Inc.
9//
10// Authors: Liam Girdwood <lrg@ti.com>
11// Mark Brown <broonie@opensource.wolfsonmicro.com>
Liam Girdwoodddee6272011-06-09 14:45:53 +010012
13#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/delay.h>
Nicolin Chen988e8cc2013-11-04 14:57:31 +080016#include <linux/pinctrl/consumer.h>
Mark Brownd6652ef2011-12-03 20:14:31 +000017#include <linux/pm_runtime.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010018#include <linux/slab.h>
19#include <linux/workqueue.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010020#include <linux/export.h>
Liam Girdwoodf86dcef2012-04-25 12:12:50 +010021#include <linux/debugfs.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010022#include <sound/core.h>
23#include <sound/pcm.h>
24#include <sound/pcm_params.h>
25#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010026#include <sound/soc-dpcm.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010027#include <sound/initval.h>
28
Liam Girdwood01d75842012-04-25 12:12:49 +010029#define DPCM_MAX_BE_USERS 8
30
Ricard Wanderlofcde79032015-08-24 14:16:51 +020031/*
32 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
33 *
34 * Returns true if the DAI supports the indicated stream type.
35 */
36static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
37{
38 struct snd_soc_pcm_stream *codec_stream;
39
40 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
41 codec_stream = &dai->driver->playback;
42 else
43 codec_stream = &dai->driver->capture;
44
45 /* If the codec specifies any rate at all, it supports the stream. */
46 return codec_stream->rates;
47}
48
Lars-Peter Clausen90996f42013-05-14 11:05:30 +020049/**
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010050 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
51 * @rtd: ASoC PCM runtime that is activated
52 * @stream: Direction of the PCM stream
53 *
54 * Increments the active count for all the DAIs and components attached to a PCM
55 * runtime. Should typically be called when a stream is opened.
56 *
57 * Must be called with the rtd->pcm_mutex being held
58 */
59void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
60{
61 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020062 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010063
64 lockdep_assert_held(&rtd->pcm_mutex);
65
66 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
67 cpu_dai->playback_active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020068 for (i = 0; i < rtd->num_codecs; i++)
69 rtd->codec_dais[i]->playback_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010070 } else {
71 cpu_dai->capture_active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020072 for (i = 0; i < rtd->num_codecs; i++)
73 rtd->codec_dais[i]->capture_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010074 }
75
76 cpu_dai->active++;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +010077 cpu_dai->component->active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020078 for (i = 0; i < rtd->num_codecs; i++) {
79 rtd->codec_dais[i]->active++;
80 rtd->codec_dais[i]->component->active++;
81 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010082}
83
84/**
85 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
86 * @rtd: ASoC PCM runtime that is deactivated
87 * @stream: Direction of the PCM stream
88 *
89 * Decrements the active count for all the DAIs and components attached to a PCM
90 * runtime. Should typically be called when a stream is closed.
91 *
92 * Must be called with the rtd->pcm_mutex being held
93 */
94void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
95{
96 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020097 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010098
99 lockdep_assert_held(&rtd->pcm_mutex);
100
101 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
102 cpu_dai->playback_active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200103 for (i = 0; i < rtd->num_codecs; i++)
104 rtd->codec_dais[i]->playback_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100105 } else {
106 cpu_dai->capture_active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200107 for (i = 0; i < rtd->num_codecs; i++)
108 rtd->codec_dais[i]->capture_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100109 }
110
111 cpu_dai->active--;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +0100112 cpu_dai->component->active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200113 for (i = 0; i < rtd->num_codecs; i++) {
114 rtd->codec_dais[i]->component->active--;
115 rtd->codec_dais[i]->active--;
116 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100117}
118
119/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100120 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
121 * @rtd: The ASoC PCM runtime that should be checked.
122 *
123 * This function checks whether the power down delay should be ignored for a
124 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
125 * been configured to ignore the delay, or if none of the components benefits
126 * from having the delay.
127 */
128bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
129{
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000130 struct snd_soc_rtdcom_list *rtdcom;
131 struct snd_soc_component *component;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200132 bool ignore = true;
133
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100134 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
135 return true;
136
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000137 for_each_rtdcom(rtd, rtdcom) {
138 component = rtdcom->component;
139
Kuninori Morimoto72c38182018-01-19 05:21:19 +0000140 ignore &= !component->driver->use_pmdown_time;
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000141 }
142
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000143 return ignore;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100144}
145
146/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200147 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
148 * @substream: the pcm substream
149 * @hw: the hardware parameters
150 *
151 * Sets the substream runtime hardware parameters.
152 */
153int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
154 const struct snd_pcm_hardware *hw)
155{
156 struct snd_pcm_runtime *runtime = substream->runtime;
157 runtime->hw.info = hw->info;
158 runtime->hw.formats = hw->formats;
159 runtime->hw.period_bytes_min = hw->period_bytes_min;
160 runtime->hw.period_bytes_max = hw->period_bytes_max;
161 runtime->hw.periods_min = hw->periods_min;
162 runtime->hw.periods_max = hw->periods_max;
163 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
164 runtime->hw.fifo_size = hw->fifo_size;
165 return 0;
166}
167EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
168
Liam Girdwood01d75842012-04-25 12:12:49 +0100169/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000170int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100171 int event)
172{
173 struct snd_soc_dpcm *dpcm;
174
175 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
176
177 struct snd_soc_pcm_runtime *be = dpcm->be;
178
Liam Girdwood103d84a2012-11-19 14:39:15 +0000179 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100180 be->dai_link->name, event, dir);
181
Banajit Goswamib1cd2e32017-07-14 23:15:05 -0700182 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
183 (be->dpcm[dir].users >= 1))
184 continue;
185
Liam Girdwood01d75842012-04-25 12:12:49 +0100186 snd_soc_dapm_stream_event(be, dir, event);
187 }
188
189 snd_soc_dapm_stream_event(fe, dir, event);
190
191 return 0;
192}
193
Dong Aisheng17841022011-08-29 17:15:14 +0800194static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
195 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100196{
197 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100198 int ret;
199
Nicolin Chen3635bf02013-11-13 18:56:24 +0800200 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
201 rtd->dai_link->symmetric_rates)) {
202 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
203 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100204
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200205 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800206 SNDRV_PCM_HW_PARAM_RATE,
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200207 soc_dai->rate);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800208 if (ret < 0) {
209 dev_err(soc_dai->dev,
210 "ASoC: Unable to apply rate constraint: %d\n",
211 ret);
212 return ret;
213 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100214 }
215
Nicolin Chen3635bf02013-11-13 18:56:24 +0800216 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
217 rtd->dai_link->symmetric_channels)) {
218 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
219 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100220
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200221 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800222 SNDRV_PCM_HW_PARAM_CHANNELS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800223 soc_dai->channels);
224 if (ret < 0) {
225 dev_err(soc_dai->dev,
226 "ASoC: Unable to apply channel symmetry constraint: %d\n",
227 ret);
228 return ret;
229 }
230 }
231
232 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
233 rtd->dai_link->symmetric_samplebits)) {
234 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
235 soc_dai->sample_bits);
236
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200237 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800238 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800239 soc_dai->sample_bits);
240 if (ret < 0) {
241 dev_err(soc_dai->dev,
242 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
243 ret);
244 return ret;
245 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100246 }
247
248 return 0;
249}
250
Nicolin Chen3635bf02013-11-13 18:56:24 +0800251static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
252 struct snd_pcm_hw_params *params)
253{
254 struct snd_soc_pcm_runtime *rtd = substream->private_data;
255 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200256 unsigned int rate, channels, sample_bits, symmetry, i;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800257
258 rate = params_rate(params);
259 channels = params_channels(params);
260 sample_bits = snd_pcm_format_physical_width(params_format(params));
261
262 /* reject unmatched parameters when applying symmetry */
263 symmetry = cpu_dai->driver->symmetric_rates ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800264 rtd->dai_link->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200265
266 for (i = 0; i < rtd->num_codecs; i++)
267 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
268
Nicolin Chen3635bf02013-11-13 18:56:24 +0800269 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
270 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
271 cpu_dai->rate, rate);
272 return -EINVAL;
273 }
274
275 symmetry = cpu_dai->driver->symmetric_channels ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800276 rtd->dai_link->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200277
278 for (i = 0; i < rtd->num_codecs; i++)
279 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
280
Nicolin Chen3635bf02013-11-13 18:56:24 +0800281 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
282 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
283 cpu_dai->channels, channels);
284 return -EINVAL;
285 }
286
287 symmetry = cpu_dai->driver->symmetric_samplebits ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800288 rtd->dai_link->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200289
290 for (i = 0; i < rtd->num_codecs; i++)
291 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
292
Nicolin Chen3635bf02013-11-13 18:56:24 +0800293 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
294 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
295 cpu_dai->sample_bits, sample_bits);
296 return -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100297 }
298
299 return 0;
300}
301
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100302static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
303{
304 struct snd_soc_pcm_runtime *rtd = substream->private_data;
305 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100306 struct snd_soc_dai_link *link = rtd->dai_link;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200307 unsigned int symmetry, i;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100308
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200309 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
310 cpu_driver->symmetric_channels || link->symmetric_channels ||
311 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
312
313 for (i = 0; i < rtd->num_codecs; i++)
314 symmetry = symmetry ||
315 rtd->codec_dais[i]->driver->symmetric_rates ||
316 rtd->codec_dais[i]->driver->symmetric_channels ||
317 rtd->codec_dais[i]->driver->symmetric_samplebits;
318
319 return symmetry;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100320}
321
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200322static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000323{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200324 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Takashi Iwaic6068d32014-12-31 17:10:34 +0100325 int ret;
Mark Brown58ba9b22012-01-16 18:38:51 +0000326
327 if (!bits)
328 return;
329
Lars-Peter Clausen0e2a3752014-12-29 18:43:38 +0100330 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
331 if (ret != 0)
332 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
333 bits, ret);
Mark Brown58ba9b22012-01-16 18:38:51 +0000334}
335
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200336static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200337{
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200338 struct snd_soc_pcm_runtime *rtd = substream->private_data;
339 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200340 struct snd_soc_dai *codec_dai;
341 int i;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200342 unsigned int bits = 0, cpu_bits;
343
344 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200345 for (i = 0; i < rtd->num_codecs; i++) {
346 codec_dai = rtd->codec_dais[i];
347 if (codec_dai->driver->playback.sig_bits == 0) {
348 bits = 0;
349 break;
350 }
351 bits = max(codec_dai->driver->playback.sig_bits, bits);
352 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200353 cpu_bits = cpu_dai->driver->playback.sig_bits;
354 } else {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200355 for (i = 0; i < rtd->num_codecs; i++) {
356 codec_dai = rtd->codec_dais[i];
Daniel Mack5e63dfc2014-10-07 14:33:46 +0200357 if (codec_dai->driver->capture.sig_bits == 0) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200358 bits = 0;
359 break;
360 }
361 bits = max(codec_dai->driver->capture.sig_bits, bits);
362 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200363 cpu_bits = cpu_dai->driver->capture.sig_bits;
364 }
365
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200366 soc_pcm_set_msb(substream, bits);
367 soc_pcm_set_msb(substream, cpu_bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200368}
369
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200370static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200371{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200372 struct snd_pcm_runtime *runtime = substream->runtime;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100373 struct snd_pcm_hardware *hw = &runtime->hw;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200374 struct snd_soc_pcm_runtime *rtd = substream->private_data;
375 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
376 struct snd_soc_dai_driver *codec_dai_drv;
377 struct snd_soc_pcm_stream *codec_stream;
378 struct snd_soc_pcm_stream *cpu_stream;
379 unsigned int chan_min = 0, chan_max = UINT_MAX;
380 unsigned int rate_min = 0, rate_max = UINT_MAX;
381 unsigned int rates = UINT_MAX;
382 u64 formats = ULLONG_MAX;
383 int i;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100384
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200385 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
386 cpu_stream = &cpu_dai_drv->playback;
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100387 else
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200388 cpu_stream = &cpu_dai_drv->capture;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100389
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200390 /* first calculate min/max only for CODECs in the DAI link */
391 for (i = 0; i < rtd->num_codecs; i++) {
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200392
393 /*
394 * Skip CODECs which don't support the current stream type.
395 * Otherwise, since the rate, channel, and format values will
396 * zero in that case, we would have no usable settings left,
397 * causing the resulting setup to fail.
398 * At least one CODEC should match, otherwise we should have
399 * bailed out on a higher level, since there would be no
400 * CODEC to support the transfer direction in that case.
401 */
402 if (!snd_soc_dai_stream_valid(rtd->codec_dais[i],
403 substream->stream))
404 continue;
405
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200406 codec_dai_drv = rtd->codec_dais[i]->driver;
407 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
408 codec_stream = &codec_dai_drv->playback;
409 else
410 codec_stream = &codec_dai_drv->capture;
411 chan_min = max(chan_min, codec_stream->channels_min);
412 chan_max = min(chan_max, codec_stream->channels_max);
413 rate_min = max(rate_min, codec_stream->rate_min);
414 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
415 formats &= codec_stream->formats;
416 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
417 }
418
419 /*
420 * chan min/max cannot be enforced if there are multiple CODEC DAIs
421 * connected to a single CPU DAI, use CPU DAI's directly and let
422 * channel allocation be fixed up later
423 */
424 if (rtd->num_codecs > 1) {
425 chan_min = cpu_stream->channels_min;
426 chan_max = cpu_stream->channels_max;
427 }
428
429 hw->channels_min = max(chan_min, cpu_stream->channels_min);
430 hw->channels_max = min(chan_max, cpu_stream->channels_max);
431 if (hw->formats)
432 hw->formats &= formats & cpu_stream->formats;
433 else
434 hw->formats = formats & cpu_stream->formats;
435 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100436
437 snd_pcm_limit_hw_rates(runtime);
438
439 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200440 hw->rate_min = max(hw->rate_min, rate_min);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100441 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200442 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200443}
444
Charles Keepax244e2932018-06-19 16:22:09 +0100445static int soc_pcm_components_close(struct snd_pcm_substream *substream,
446 struct snd_soc_component *last)
447{
448 struct snd_soc_pcm_runtime *rtd = substream->private_data;
449 struct snd_soc_rtdcom_list *rtdcom;
450 struct snd_soc_component *component;
451
452 for_each_rtdcom(rtd, rtdcom) {
453 component = rtdcom->component;
454
455 if (component == last)
456 break;
457
458 if (!component->driver->ops ||
459 !component->driver->ops->close)
460 continue;
461
462 component->driver->ops->close(substream);
463 }
464
465 return 0;
466}
467
Mark Brown58ba9b22012-01-16 18:38:51 +0000468/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100469 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
470 * then initialized and any private data can be allocated. This also calls
Charles Keepaxef050be2018-04-24 16:39:02 +0100471 * startup for the cpu DAI, component, machine and codec DAI.
Liam Girdwoodddee6272011-06-09 14:45:53 +0100472 */
473static int soc_pcm_open(struct snd_pcm_substream *substream)
474{
475 struct snd_soc_pcm_runtime *rtd = substream->private_data;
476 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000477 struct snd_soc_component *component;
478 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100479 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200480 struct snd_soc_dai *codec_dai;
481 const char *codec_dai_name = "multicodec";
Charles Keepax244e2932018-06-19 16:22:09 +0100482 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100483
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800484 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200485 for (i = 0; i < rtd->num_codecs; i++)
486 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000487
488 for_each_rtdcom(rtd, rtdcom) {
489 component = rtdcom->component;
490
491 pm_runtime_get_sync(component->dev);
492 }
Mark Brownd6652ef2011-12-03 20:14:31 +0000493
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100494 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100495
496 /* startup the audio subsystem */
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000497 if (cpu_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100498 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
499 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000500 dev_err(cpu_dai->dev, "ASoC: can't open interface"
501 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100502 goto out;
503 }
504 }
505
Kuninori Morimotob8135862017-10-11 01:37:23 +0000506 for_each_rtdcom(rtd, rtdcom) {
507 component = rtdcom->component;
508
Kuninori Morimotob8135862017-10-11 01:37:23 +0000509 if (!component->driver->ops ||
510 !component->driver->ops->open)
511 continue;
512
Charles Keepax244e2932018-06-19 16:22:09 +0100513 ret = component->driver->ops->open(substream);
514 if (ret < 0) {
Kuninori Morimotob8135862017-10-11 01:37:23 +0000515 dev_err(component->dev,
516 "ASoC: can't open component %s: %d\n",
Charles Keepax244e2932018-06-19 16:22:09 +0100517 component->name, ret);
518 goto component_err;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000519 }
520 }
Charles Keepax244e2932018-06-19 16:22:09 +0100521 component = NULL;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000522
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200523 for (i = 0; i < rtd->num_codecs; i++) {
524 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000525 if (codec_dai->driver->ops->startup) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200526 ret = codec_dai->driver->ops->startup(substream,
527 codec_dai);
528 if (ret < 0) {
529 dev_err(codec_dai->dev,
530 "ASoC: can't open codec %s: %d\n",
531 codec_dai->name, ret);
532 goto codec_dai_err;
533 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100534 }
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200535
536 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
537 codec_dai->tx_mask = 0;
538 else
539 codec_dai->rx_mask = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100540 }
541
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000542 if (rtd->dai_link->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100543 ret = rtd->dai_link->ops->startup(substream);
544 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000545 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000546 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100547 goto machine_err;
548 }
549 }
550
Liam Girdwood01d75842012-04-25 12:12:49 +0100551 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
552 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
553 goto dynamic;
554
Liam Girdwoodddee6272011-06-09 14:45:53 +0100555 /* Check that the codec and cpu DAIs are compatible */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200556 soc_pcm_init_runtime_hw(substream);
557
558 if (rtd->num_codecs == 1)
559 codec_dai_name = rtd->codec_dai->name;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100560
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100561 if (soc_pcm_has_symmetry(substream))
562 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
563
Liam Girdwoodddee6272011-06-09 14:45:53 +0100564 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100565 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000566 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200567 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100568 goto config_err;
569 }
570 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000571 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200572 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100573 goto config_err;
574 }
575 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
576 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000577 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200578 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100579 goto config_err;
580 }
581
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200582 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000583
Liam Girdwoodddee6272011-06-09 14:45:53 +0100584 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800585 if (cpu_dai->active) {
586 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
587 if (ret != 0)
588 goto config_err;
589 }
590
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200591 for (i = 0; i < rtd->num_codecs; i++) {
592 if (rtd->codec_dais[i]->active) {
593 ret = soc_pcm_apply_symmetry(substream,
594 rtd->codec_dais[i]);
595 if (ret != 0)
596 goto config_err;
597 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100598 }
599
Liam Girdwood103d84a2012-11-19 14:39:15 +0000600 pr_debug("ASoC: %s <-> %s info:\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200601 codec_dai_name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000602 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
603 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100604 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000605 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100606 runtime->hw.rate_max);
607
Liam Girdwood01d75842012-04-25 12:12:49 +0100608dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100609
610 snd_soc_runtime_activate(rtd, substream->stream);
611
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100612 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100613 return 0;
614
615config_err:
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000616 if (rtd->dai_link->ops->shutdown)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100617 rtd->dai_link->ops->shutdown(substream);
618
619machine_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200620 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100621
622codec_dai_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200623 while (--i >= 0) {
624 codec_dai = rtd->codec_dais[i];
625 if (codec_dai->driver->ops->shutdown)
626 codec_dai->driver->ops->shutdown(substream, codec_dai);
627 }
628
Kuninori Morimotob8135862017-10-11 01:37:23 +0000629component_err:
Charles Keepax244e2932018-06-19 16:22:09 +0100630 soc_pcm_components_close(substream, component);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000631
Liam Girdwoodddee6272011-06-09 14:45:53 +0100632 if (cpu_dai->driver->ops->shutdown)
633 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
634out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100635 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000636
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000637 for_each_rtdcom(rtd, rtdcom) {
638 component = rtdcom->component;
639
640 pm_runtime_mark_last_busy(component->dev);
641 pm_runtime_put_autosuspend(component->dev);
Sanyog Kale3f809782016-01-05 17:14:49 +0530642 }
643
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200644 for (i = 0; i < rtd->num_codecs; i++) {
645 if (!rtd->codec_dais[i]->active)
646 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
647 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800648 if (!cpu_dai->active)
649 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000650
Liam Girdwoodddee6272011-06-09 14:45:53 +0100651 return ret;
652}
653
654/*
655 * Power down the audio subsystem pmdown_time msecs after close is called.
656 * This is to ensure there are no pops or clicks in between any music tracks
657 * due to DAPM power cycling.
658 */
659static void close_delayed_work(struct work_struct *work)
660{
661 struct snd_soc_pcm_runtime *rtd =
662 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200663 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
Liam Girdwoodddee6272011-06-09 14:45:53 +0100664
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100665 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100666
Liam Girdwood103d84a2012-11-19 14:39:15 +0000667 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100668 codec_dai->driver->playback.stream_name,
669 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600670 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100671
672 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600673 if (rtd->pop_wait == 1) {
674 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800675 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000676 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100677 }
678
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100679 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100680}
681
682/*
683 * Called by ALSA when a PCM substream is closed. Private data can be
Charles Keepaxef050be2018-04-24 16:39:02 +0100684 * freed here. The cpu DAI, codec DAI, machine and components are also
Liam Girdwoodddee6272011-06-09 14:45:53 +0100685 * shutdown.
686 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100687static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100688{
689 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000690 struct snd_soc_component *component;
691 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100692 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200693 struct snd_soc_dai *codec_dai;
694 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100695
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100696 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100697
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100698 snd_soc_runtime_deactivate(rtd, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100699
Dong Aisheng17841022011-08-29 17:15:14 +0800700 /* clear the corresponding DAIs rate when inactive */
701 if (!cpu_dai->active)
702 cpu_dai->rate = 0;
703
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200704 for (i = 0; i < rtd->num_codecs; i++) {
705 codec_dai = rtd->codec_dais[i];
706 if (!codec_dai->active)
707 codec_dai->rate = 0;
708 }
Sascha Hauer25b76792011-08-17 09:20:01 +0200709
Ramesh Babuae116012014-10-15 12:34:59 +0530710 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
711
Liam Girdwoodddee6272011-06-09 14:45:53 +0100712 if (cpu_dai->driver->ops->shutdown)
713 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
714
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200715 for (i = 0; i < rtd->num_codecs; i++) {
716 codec_dai = rtd->codec_dais[i];
717 if (codec_dai->driver->ops->shutdown)
718 codec_dai->driver->ops->shutdown(substream, codec_dai);
719 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100720
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000721 if (rtd->dai_link->ops->shutdown)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100722 rtd->dai_link->ops->shutdown(substream);
723
Charles Keepax244e2932018-06-19 16:22:09 +0100724 soc_pcm_components_close(substream, NULL);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000725
Liam Girdwoodddee6272011-06-09 14:45:53 +0100726 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100727 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300728 /* powered down playback stream now */
729 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800730 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800731 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300732 } else {
733 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600734 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100735 queue_delayed_work(system_power_efficient_wq,
736 &rtd->delayed_work,
737 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300738 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100739 } else {
740 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800741 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000742 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100743 }
744
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100745 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000746
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000747 for_each_rtdcom(rtd, rtdcom) {
748 component = rtdcom->component;
Sanyog Kale3f809782016-01-05 17:14:49 +0530749
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000750 pm_runtime_mark_last_busy(component->dev);
751 pm_runtime_put_autosuspend(component->dev);
Sanyog Kale3f809782016-01-05 17:14:49 +0530752 }
753
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200754 for (i = 0; i < rtd->num_codecs; i++) {
755 if (!rtd->codec_dais[i]->active)
756 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
757 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800758 if (!cpu_dai->active)
759 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000760
Liam Girdwoodddee6272011-06-09 14:45:53 +0100761 return 0;
762}
763
764/*
765 * Called by ALSA when the PCM substream is prepared, can set format, sample
766 * rate, etc. This function is non atomic and can be called multiple times,
767 * it can refer to the runtime info.
768 */
769static int soc_pcm_prepare(struct snd_pcm_substream *substream)
770{
771 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000772 struct snd_soc_component *component;
773 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100774 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200775 struct snd_soc_dai *codec_dai;
776 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100777
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100778 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100779
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000780 if (rtd->dai_link->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100781 ret = rtd->dai_link->ops->prepare(substream);
782 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000783 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
784 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100785 goto out;
786 }
787 }
788
Kuninori Morimotob8135862017-10-11 01:37:23 +0000789 for_each_rtdcom(rtd, rtdcom) {
790 component = rtdcom->component;
791
Kuninori Morimotob8135862017-10-11 01:37:23 +0000792 if (!component->driver->ops ||
793 !component->driver->ops->prepare)
794 continue;
795
796 ret = component->driver->ops->prepare(substream);
797 if (ret < 0) {
798 dev_err(component->dev,
799 "ASoC: platform prepare error: %d\n", ret);
800 goto out;
801 }
802 }
803
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200804 for (i = 0; i < rtd->num_codecs; i++) {
805 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000806 if (codec_dai->driver->ops->prepare) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200807 ret = codec_dai->driver->ops->prepare(substream,
808 codec_dai);
809 if (ret < 0) {
810 dev_err(codec_dai->dev,
Jarkko Nikula90cc7f12014-12-23 11:04:41 +0200811 "ASoC: codec DAI prepare error: %d\n",
812 ret);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200813 goto out;
814 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100815 }
816 }
817
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000818 if (cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100819 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
820 if (ret < 0) {
Jarkko Nikula90cc7f12014-12-23 11:04:41 +0200821 dev_err(cpu_dai->dev,
822 "ASoC: cpu DAI prepare error: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100823 goto out;
824 }
825 }
826
827 /* cancel any delayed stream shutdown that is pending */
828 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600829 rtd->pop_wait) {
830 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100831 cancel_delayed_work(&rtd->delayed_work);
832 }
833
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000834 snd_soc_dapm_stream_event(rtd, substream->stream,
835 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100836
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200837 for (i = 0; i < rtd->num_codecs; i++)
838 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
839 substream->stream);
Ramesh Babuae116012014-10-15 12:34:59 +0530840 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100841
842out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100843 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100844 return ret;
845}
846
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200847static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
848 unsigned int mask)
849{
850 struct snd_interval *interval;
851 int channels = hweight_long(mask);
852
853 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
854 interval->min = channels;
855 interval->max = channels;
856}
857
Benoit Cousson93e69582014-07-08 23:19:38 +0200858int soc_dai_hw_params(struct snd_pcm_substream *substream,
859 struct snd_pcm_hw_params *params,
860 struct snd_soc_dai *dai)
861{
Liam Girdwooda655de82018-07-02 16:59:54 +0100862 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Benoit Cousson93e69582014-07-08 23:19:38 +0200863 int ret;
864
Liam Girdwooda655de82018-07-02 16:59:54 +0100865 /* perform any topology hw_params fixups before DAI */
866 if (rtd->dai_link->be_hw_params_fixup) {
867 ret = rtd->dai_link->be_hw_params_fixup(rtd, params);
868 if (ret < 0) {
869 dev_err(rtd->dev,
870 "ASoC: hw_params topology fixup failed %d\n",
871 ret);
872 return ret;
873 }
874 }
875
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000876 if (dai->driver->ops->hw_params) {
Benoit Cousson93e69582014-07-08 23:19:38 +0200877 ret = dai->driver->ops->hw_params(substream, params, dai);
878 if (ret < 0) {
879 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
880 dai->name, ret);
881 return ret;
882 }
883 }
884
885 return 0;
886}
887
Charles Keepax244e2932018-06-19 16:22:09 +0100888static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
889 struct snd_soc_component *last)
890{
891 struct snd_soc_pcm_runtime *rtd = substream->private_data;
892 struct snd_soc_rtdcom_list *rtdcom;
893 struct snd_soc_component *component;
894
895 for_each_rtdcom(rtd, rtdcom) {
896 component = rtdcom->component;
897
898 if (component == last)
899 break;
900
901 if (!component->driver->ops ||
902 !component->driver->ops->hw_free)
903 continue;
904
905 component->driver->ops->hw_free(substream);
906 }
907
908 return 0;
909}
910
Liam Girdwoodddee6272011-06-09 14:45:53 +0100911/*
912 * Called by ALSA when the hardware params are set by application. This
913 * function can also be called multiple times and can allocate buffers
914 * (using snd_pcm_lib_* ). It's non-atomic.
915 */
916static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
917 struct snd_pcm_hw_params *params)
918{
919 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000920 struct snd_soc_component *component;
921 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100922 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Charles Keepax244e2932018-06-19 16:22:09 +0100923 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100924
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100925 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000926 if (rtd->dai_link->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100927 ret = rtd->dai_link->ops->hw_params(substream, params);
928 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000929 dev_err(rtd->card->dev, "ASoC: machine hw_params"
930 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100931 goto out;
932 }
933 }
934
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200935 for (i = 0; i < rtd->num_codecs; i++) {
936 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
937 struct snd_pcm_hw_params codec_params;
938
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200939 /*
940 * Skip CODECs which don't support the current stream type,
941 * the idea being that if a CODEC is not used for the currently
942 * set up transfer direction, it should not need to be
943 * configured, especially since the configuration used might
944 * not even be supported by that CODEC. There may be cases
945 * however where a CODEC needs to be set up although it is
946 * actually not being used for the transfer, e.g. if a
947 * capture-only CODEC is acting as an LRCLK and/or BCLK master
948 * for the DAI link including a playback-only CODEC.
949 * If this becomes necessary, we will have to augment the
950 * machine driver setup with information on how to act, so
951 * we can do the right thing here.
952 */
953 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
954 continue;
955
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200956 /* copy params for each codec */
957 codec_params = *params;
958
959 /* fixup params based on TDM slot masks */
960 if (codec_dai->tx_mask)
961 soc_pcm_codec_params_fixup(&codec_params,
962 codec_dai->tx_mask);
963 if (codec_dai->rx_mask)
964 soc_pcm_codec_params_fixup(&codec_params,
965 codec_dai->rx_mask);
966
Benoit Cousson93e69582014-07-08 23:19:38 +0200967 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
968 if(ret < 0)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100969 goto codec_err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200970
971 codec_dai->rate = params_rate(&codec_params);
972 codec_dai->channels = params_channels(&codec_params);
973 codec_dai->sample_bits = snd_pcm_format_physical_width(
974 params_format(&codec_params));
Liam Girdwoodddee6272011-06-09 14:45:53 +0100975 }
976
Benoit Cousson93e69582014-07-08 23:19:38 +0200977 ret = soc_dai_hw_params(substream, params, cpu_dai);
978 if (ret < 0)
979 goto interface_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100980
Kuninori Morimotob8135862017-10-11 01:37:23 +0000981 for_each_rtdcom(rtd, rtdcom) {
982 component = rtdcom->component;
983
Kuninori Morimotob8135862017-10-11 01:37:23 +0000984 if (!component->driver->ops ||
985 !component->driver->ops->hw_params)
986 continue;
987
Charles Keepax244e2932018-06-19 16:22:09 +0100988 ret = component->driver->ops->hw_params(substream, params);
989 if (ret < 0) {
Kuninori Morimotob8135862017-10-11 01:37:23 +0000990 dev_err(component->dev,
991 "ASoC: %s hw params failed: %d\n",
Charles Keepax244e2932018-06-19 16:22:09 +0100992 component->name, ret);
993 goto component_err;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000994 }
995 }
Charles Keepax244e2932018-06-19 16:22:09 +0100996 component = NULL;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000997
Nicolin Chen3635bf02013-11-13 18:56:24 +0800998 /* store the parameters for each DAIs */
Dong Aisheng17841022011-08-29 17:15:14 +0800999 cpu_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +08001000 cpu_dai->channels = params_channels(params);
1001 cpu_dai->sample_bits =
1002 snd_pcm_format_physical_width(params_format(params));
1003
jiada wang957ce0c2017-09-20 15:25:30 +09001004 ret = soc_pcm_params_symmetry(substream, params);
1005 if (ret)
Kuninori Morimotob8135862017-10-11 01:37:23 +00001006 goto component_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001007out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001008 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001009 return ret;
1010
Kuninori Morimotob8135862017-10-11 01:37:23 +00001011component_err:
Charles Keepax244e2932018-06-19 16:22:09 +01001012 soc_pcm_components_hw_free(substream, component);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001013
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001014 if (cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001015 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1016
1017interface_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001018 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001019
1020codec_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001021 while (--i >= 0) {
1022 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001023 if (codec_dai->driver->ops->hw_free)
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001024 codec_dai->driver->ops->hw_free(substream, codec_dai);
1025 codec_dai->rate = 0;
1026 }
1027
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +00001028 if (rtd->dai_link->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001029 rtd->dai_link->ops->hw_free(substream);
1030
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001031 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001032 return ret;
1033}
1034
1035/*
1036 * Frees resources allocated by hw_params, can be called multiple times
1037 */
1038static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1039{
1040 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001041 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001042 struct snd_soc_dai *codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +08001043 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001044 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001045
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001046 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001047
Nicolin Chend3383422013-11-20 18:37:09 +08001048 /* clear the corresponding DAIs parameters when going to be inactive */
1049 if (cpu_dai->active == 1) {
1050 cpu_dai->rate = 0;
1051 cpu_dai->channels = 0;
1052 cpu_dai->sample_bits = 0;
1053 }
1054
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001055 for (i = 0; i < rtd->num_codecs; i++) {
1056 codec_dai = rtd->codec_dais[i];
1057 if (codec_dai->active == 1) {
1058 codec_dai->rate = 0;
1059 codec_dai->channels = 0;
1060 codec_dai->sample_bits = 0;
1061 }
Nicolin Chend3383422013-11-20 18:37:09 +08001062 }
1063
Liam Girdwoodddee6272011-06-09 14:45:53 +01001064 /* apply codec digital mute */
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001065 for (i = 0; i < rtd->num_codecs; i++) {
1066 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
1067 (!playback && rtd->codec_dais[i]->capture_active == 1))
1068 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
1069 substream->stream);
1070 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001071
1072 /* free any machine hw params */
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +00001073 if (rtd->dai_link->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001074 rtd->dai_link->ops->hw_free(substream);
1075
Kuninori Morimotob8135862017-10-11 01:37:23 +00001076 /* free any component resources */
Charles Keepax244e2932018-06-19 16:22:09 +01001077 soc_pcm_components_hw_free(substream, NULL);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001078
Liam Girdwoodddee6272011-06-09 14:45:53 +01001079 /* now free hw params for the DAIs */
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001080 for (i = 0; i < rtd->num_codecs; i++) {
1081 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001082 if (codec_dai->driver->ops->hw_free)
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001083 codec_dai->driver->ops->hw_free(substream, codec_dai);
1084 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001085
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001086 if (cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001087 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1088
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001089 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001090 return 0;
1091}
1092
1093static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1094{
1095 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001096 struct snd_soc_component *component;
1097 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001098 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001099 struct snd_soc_dai *codec_dai;
1100 int i, ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001101
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001102 for (i = 0; i < rtd->num_codecs; i++) {
1103 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001104 if (codec_dai->driver->ops->trigger) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001105 ret = codec_dai->driver->ops->trigger(substream,
1106 cmd, codec_dai);
1107 if (ret < 0)
1108 return ret;
1109 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001110 }
1111
Kuninori Morimotob8135862017-10-11 01:37:23 +00001112 for_each_rtdcom(rtd, rtdcom) {
1113 component = rtdcom->component;
1114
Kuninori Morimotob8135862017-10-11 01:37:23 +00001115 if (!component->driver->ops ||
1116 !component->driver->ops->trigger)
1117 continue;
1118
1119 ret = component->driver->ops->trigger(substream, cmd);
1120 if (ret < 0)
1121 return ret;
1122 }
1123
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001124 if (cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +01001125 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1126 if (ret < 0)
1127 return ret;
1128 }
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001129
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +00001130 if (rtd->dai_link->ops->trigger) {
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001131 ret = rtd->dai_link->ops->trigger(substream, cmd);
1132 if (ret < 0)
1133 return ret;
1134 }
1135
Liam Girdwoodddee6272011-06-09 14:45:53 +01001136 return 0;
1137}
1138
Mark Brown45c0a182012-05-09 21:46:27 +01001139static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1140 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001141{
1142 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001143 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001144 struct snd_soc_dai *codec_dai;
1145 int i, ret;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001146
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001147 for (i = 0; i < rtd->num_codecs; i++) {
1148 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001149 if (codec_dai->driver->ops->bespoke_trigger) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001150 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1151 cmd, codec_dai);
1152 if (ret < 0)
1153 return ret;
1154 }
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001155 }
1156
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001157 if (cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001158 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1159 if (ret < 0)
1160 return ret;
1161 }
1162 return 0;
1163}
Liam Girdwoodddee6272011-06-09 14:45:53 +01001164/*
1165 * soc level wrapper for pointer callback
Charles Keepaxef050be2018-04-24 16:39:02 +01001166 * If cpu_dai, codec_dai, component driver has the delay callback, then
Liam Girdwoodddee6272011-06-09 14:45:53 +01001167 * the runtime->delay will be updated accordingly.
1168 */
1169static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1170{
1171 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001172 struct snd_soc_component *component;
1173 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001174 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001175 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001176 struct snd_pcm_runtime *runtime = substream->runtime;
1177 snd_pcm_uframes_t offset = 0;
1178 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001179 snd_pcm_sframes_t codec_delay = 0;
1180 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001181
Kuninori Morimotob8135862017-10-11 01:37:23 +00001182 for_each_rtdcom(rtd, rtdcom) {
1183 component = rtdcom->component;
1184
Kuninori Morimotob8135862017-10-11 01:37:23 +00001185 if (!component->driver->ops ||
1186 !component->driver->ops->pointer)
1187 continue;
1188
1189 /* FIXME: use 1st pointer */
1190 offset = component->driver->ops->pointer(substream);
1191 break;
1192 }
1193
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001194 if (cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001195 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1196
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001197 for (i = 0; i < rtd->num_codecs; i++) {
1198 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001199 if (codec_dai->driver->ops->delay)
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001200 codec_delay = max(codec_delay,
1201 codec_dai->driver->ops->delay(substream,
1202 codec_dai));
1203 }
1204 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001205
Liam Girdwoodddee6272011-06-09 14:45:53 +01001206 runtime->delay = delay;
1207
1208 return offset;
1209}
1210
Liam Girdwood01d75842012-04-25 12:12:49 +01001211/* connect a FE and BE */
1212static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1213 struct snd_soc_pcm_runtime *be, int stream)
1214{
1215 struct snd_soc_dpcm *dpcm;
1216
1217 /* only add new dpcms */
1218 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1219 if (dpcm->be == be && dpcm->fe == fe)
1220 return 0;
1221 }
1222
1223 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1224 if (!dpcm)
1225 return -ENOMEM;
1226
1227 dpcm->be = be;
1228 dpcm->fe = fe;
1229 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1230 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1231 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1232 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1233
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001234 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001235 stream ? "capture" : "playback", fe->dai_link->name,
1236 stream ? "<-" : "->", be->dai_link->name);
1237
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001238#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02001239 if (fe->debugfs_dpcm_root)
1240 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1241 fe->debugfs_dpcm_root, &dpcm->state);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001242#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001243 return 1;
1244}
1245
1246/* reparent a BE onto another FE */
1247static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1248 struct snd_soc_pcm_runtime *be, int stream)
1249{
1250 struct snd_soc_dpcm *dpcm;
1251 struct snd_pcm_substream *fe_substream, *be_substream;
1252
1253 /* reparent if BE is connected to other FEs */
1254 if (!be->dpcm[stream].users)
1255 return;
1256
1257 be_substream = snd_soc_dpcm_get_substream(be, stream);
1258
1259 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1260 if (dpcm->fe == fe)
1261 continue;
1262
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001263 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001264 stream ? "capture" : "playback",
1265 dpcm->fe->dai_link->name,
1266 stream ? "<-" : "->", dpcm->be->dai_link->name);
1267
1268 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1269 be_substream->runtime = fe_substream->runtime;
1270 break;
1271 }
1272}
1273
1274/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001275void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001276{
1277 struct snd_soc_dpcm *dpcm, *d;
1278
1279 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001280 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001281 stream ? "capture" : "playback",
1282 dpcm->be->dai_link->name);
1283
1284 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1285 continue;
1286
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001287 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001288 stream ? "capture" : "playback", fe->dai_link->name,
1289 stream ? "<-" : "->", dpcm->be->dai_link->name);
1290
1291 /* BEs still alive need new FE */
1292 dpcm_be_reparent(fe, dpcm->be, stream);
1293
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001294#ifdef CONFIG_DEBUG_FS
1295 debugfs_remove(dpcm->debugfs_state);
1296#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001297 list_del(&dpcm->list_be);
1298 list_del(&dpcm->list_fe);
1299 kfree(dpcm);
1300 }
1301}
1302
1303/* get BE for DAI widget and stream */
1304static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1305 struct snd_soc_dapm_widget *widget, int stream)
1306{
1307 struct snd_soc_pcm_runtime *be;
Mengdong Lin1a497982015-11-18 02:34:11 -05001308 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001309
Liam Girdwood3c146462018-03-14 20:43:51 +00001310 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1311
Liam Girdwood01d75842012-04-25 12:12:49 +01001312 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001313 list_for_each_entry(be, &card->rtd_list, list) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001314
Liam Girdwood35ea0652012-06-05 19:26:59 +01001315 if (!be->dai_link->no_pcm)
1316 continue;
1317
Liam Girdwood3c146462018-03-14 20:43:51 +00001318 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1319 be->cpu_dai->playback_widget ?
1320 be->cpu_dai->playback_widget->name : "(not set)");
1321
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001322 if (be->cpu_dai->playback_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001323 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001324
Mengdong Lin1a497982015-11-18 02:34:11 -05001325 for (i = 0; i < be->num_codecs; i++) {
1326 struct snd_soc_dai *dai = be->codec_dais[i];
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001327 if (dai->playback_widget == widget)
1328 return be;
1329 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001330 }
1331 } else {
1332
Mengdong Lin1a497982015-11-18 02:34:11 -05001333 list_for_each_entry(be, &card->rtd_list, list) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001334
Liam Girdwood35ea0652012-06-05 19:26:59 +01001335 if (!be->dai_link->no_pcm)
1336 continue;
1337
Liam Girdwood3c146462018-03-14 20:43:51 +00001338 dev_dbg(card->dev, "ASoC: try BE %s\n",
1339 be->cpu_dai->capture_widget ?
1340 be->cpu_dai->capture_widget->name : "(not set)");
1341
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001342 if (be->cpu_dai->capture_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001343 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001344
Mengdong Lin1a497982015-11-18 02:34:11 -05001345 for (i = 0; i < be->num_codecs; i++) {
1346 struct snd_soc_dai *dai = be->codec_dais[i];
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001347 if (dai->capture_widget == widget)
1348 return be;
1349 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001350 }
1351 }
1352
Liam Girdwood3c146462018-03-14 20:43:51 +00001353 /* dai link name and stream name set correctly ? */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001354 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001355 stream ? "capture" : "playback", widget->name);
1356 return NULL;
1357}
1358
1359static inline struct snd_soc_dapm_widget *
Benoit Cousson37018612014-04-24 14:01:45 +02001360 dai_get_widget(struct snd_soc_dai *dai, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001361{
1362 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Benoit Cousson37018612014-04-24 14:01:45 +02001363 return dai->playback_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001364 else
Benoit Cousson37018612014-04-24 14:01:45 +02001365 return dai->capture_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001366}
1367
1368static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1369 struct snd_soc_dapm_widget *widget)
1370{
1371 int i;
1372
1373 for (i = 0; i < list->num_widgets; i++) {
1374 if (widget == list->widgets[i])
1375 return 1;
1376 }
1377
1378 return 0;
1379}
1380
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001381static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1382 enum snd_soc_dapm_direction dir)
1383{
1384 struct snd_soc_card *card = widget->dapm->card;
1385 struct snd_soc_pcm_runtime *rtd;
1386 int i;
1387
1388 if (dir == SND_SOC_DAPM_DIR_OUT) {
1389 list_for_each_entry(rtd, &card->rtd_list, list) {
1390 if (!rtd->dai_link->no_pcm)
1391 continue;
1392
1393 if (rtd->cpu_dai->playback_widget == widget)
1394 return true;
1395
1396 for (i = 0; i < rtd->num_codecs; ++i) {
1397 struct snd_soc_dai *dai = rtd->codec_dais[i];
1398 if (dai->playback_widget == widget)
1399 return true;
1400 }
1401 }
1402 } else { /* SND_SOC_DAPM_DIR_IN */
1403 list_for_each_entry(rtd, &card->rtd_list, list) {
1404 if (!rtd->dai_link->no_pcm)
1405 continue;
1406
1407 if (rtd->cpu_dai->capture_widget == widget)
1408 return true;
1409
1410 for (i = 0; i < rtd->num_codecs; ++i) {
1411 struct snd_soc_dai *dai = rtd->codec_dais[i];
1412 if (dai->capture_widget == widget)
1413 return true;
1414 }
1415 }
1416 }
1417
1418 return false;
1419}
1420
Liam Girdwood23607022014-01-17 17:03:55 +00001421int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001422 int stream, struct snd_soc_dapm_widget_list **list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001423{
1424 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001425 int paths;
1426
Liam Girdwood01d75842012-04-25 12:12:49 +01001427 /* get number of valid DAI paths and their widgets */
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001428 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001429 dpcm_end_walk_at_be);
Liam Girdwood01d75842012-04-25 12:12:49 +01001430
Liam Girdwood103d84a2012-11-19 14:39:15 +00001431 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001432 stream ? "capture" : "playback");
1433
Liam Girdwood01d75842012-04-25 12:12:49 +01001434 return paths;
1435}
1436
Liam Girdwood01d75842012-04-25 12:12:49 +01001437static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1438 struct snd_soc_dapm_widget_list **list_)
1439{
1440 struct snd_soc_dpcm *dpcm;
1441 struct snd_soc_dapm_widget_list *list = *list_;
1442 struct snd_soc_dapm_widget *widget;
1443 int prune = 0;
1444
1445 /* Destroy any old FE <--> BE connections */
1446 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001447 unsigned int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001448
1449 /* is there a valid CPU DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001450 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001451
1452 /* prune the BE if it's no longer in our active list */
1453 if (widget && widget_in_list(list, widget))
1454 continue;
1455
1456 /* is there a valid CODEC DAI widget for this BE */
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001457 for (i = 0; i < dpcm->be->num_codecs; i++) {
1458 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1459 widget = dai_get_widget(dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001460
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001461 /* prune the BE if it's no longer in our active list */
1462 if (widget && widget_in_list(list, widget))
1463 continue;
1464 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001465
Liam Girdwood103d84a2012-11-19 14:39:15 +00001466 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001467 stream ? "capture" : "playback",
1468 dpcm->be->dai_link->name, fe->dai_link->name);
1469 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1470 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1471 prune++;
1472 }
1473
Liam Girdwood103d84a2012-11-19 14:39:15 +00001474 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001475 return prune;
1476}
1477
1478static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1479 struct snd_soc_dapm_widget_list **list_)
1480{
1481 struct snd_soc_card *card = fe->card;
1482 struct snd_soc_dapm_widget_list *list = *list_;
1483 struct snd_soc_pcm_runtime *be;
1484 int i, new = 0, err;
1485
1486 /* Create any new FE <--> BE connections */
1487 for (i = 0; i < list->num_widgets; i++) {
1488
Mark Brown46162742013-06-05 19:36:11 +01001489 switch (list->widgets[i]->id) {
1490 case snd_soc_dapm_dai_in:
Koro Chenc5b85402015-07-06 10:02:10 +08001491 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1492 continue;
1493 break;
Mark Brown46162742013-06-05 19:36:11 +01001494 case snd_soc_dapm_dai_out:
Koro Chenc5b85402015-07-06 10:02:10 +08001495 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1496 continue;
Mark Brown46162742013-06-05 19:36:11 +01001497 break;
1498 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001499 continue;
Mark Brown46162742013-06-05 19:36:11 +01001500 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001501
1502 /* is there a valid BE rtd for this widget */
1503 be = dpcm_get_be(card, list->widgets[i], stream);
1504 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001505 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001506 list->widgets[i]->name);
1507 continue;
1508 }
1509
1510 /* make sure BE is a real BE */
1511 if (!be->dai_link->no_pcm)
1512 continue;
1513
1514 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001515 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001516 continue;
1517
1518 /* newly connected FE and BE */
1519 err = dpcm_be_connect(fe, be, stream);
1520 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001521 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001522 list->widgets[i]->name);
1523 break;
1524 } else if (err == 0) /* already connected */
1525 continue;
1526
1527 /* new */
1528 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1529 new++;
1530 }
1531
Liam Girdwood103d84a2012-11-19 14:39:15 +00001532 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001533 return new;
1534}
1535
1536/*
1537 * Find the corresponding BE DAIs that source or sink audio to this
1538 * FE substream.
1539 */
Liam Girdwood23607022014-01-17 17:03:55 +00001540int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001541 int stream, struct snd_soc_dapm_widget_list **list, int new)
1542{
1543 if (new)
1544 return dpcm_add_paths(fe, stream, list);
1545 else
1546 return dpcm_prune_paths(fe, stream, list);
1547}
1548
Liam Girdwood23607022014-01-17 17:03:55 +00001549void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001550{
1551 struct snd_soc_dpcm *dpcm;
1552
1553 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1554 dpcm->be->dpcm[stream].runtime_update =
1555 SND_SOC_DPCM_UPDATE_NO;
1556}
1557
1558static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1559 int stream)
1560{
1561 struct snd_soc_dpcm *dpcm;
1562
1563 /* disable any enabled and non active backends */
1564 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1565
1566 struct snd_soc_pcm_runtime *be = dpcm->be;
1567 struct snd_pcm_substream *be_substream =
1568 snd_soc_dpcm_get_substream(be, stream);
1569
1570 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001571 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001572 stream ? "capture" : "playback",
1573 be->dpcm[stream].state);
1574
1575 if (--be->dpcm[stream].users != 0)
1576 continue;
1577
1578 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1579 continue;
1580
1581 soc_pcm_close(be_substream);
1582 be_substream->runtime = NULL;
1583 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1584 }
1585}
1586
Liam Girdwood23607022014-01-17 17:03:55 +00001587int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001588{
1589 struct snd_soc_dpcm *dpcm;
1590 int err, count = 0;
1591
1592 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1593 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1594
1595 struct snd_soc_pcm_runtime *be = dpcm->be;
1596 struct snd_pcm_substream *be_substream =
1597 snd_soc_dpcm_get_substream(be, stream);
1598
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001599 if (!be_substream) {
1600 dev_err(be->dev, "ASoC: no backend %s stream\n",
1601 stream ? "capture" : "playback");
1602 continue;
1603 }
1604
Liam Girdwood01d75842012-04-25 12:12:49 +01001605 /* is this op for this BE ? */
1606 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1607 continue;
1608
1609 /* first time the dpcm is open ? */
1610 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001611 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001612 stream ? "capture" : "playback",
1613 be->dpcm[stream].state);
1614
1615 if (be->dpcm[stream].users++ != 0)
1616 continue;
1617
1618 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1619 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1620 continue;
1621
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001622 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1623 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001624
1625 be_substream->runtime = be->dpcm[stream].runtime;
1626 err = soc_pcm_open(be_substream);
1627 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001628 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001629 be->dpcm[stream].users--;
1630 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001631 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001632 stream ? "capture" : "playback",
1633 be->dpcm[stream].state);
1634
1635 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1636 goto unwind;
1637 }
1638
1639 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1640 count++;
1641 }
1642
1643 return count;
1644
1645unwind:
1646 /* disable any enabled and non active backends */
1647 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1648 struct snd_soc_pcm_runtime *be = dpcm->be;
1649 struct snd_pcm_substream *be_substream =
1650 snd_soc_dpcm_get_substream(be, stream);
1651
1652 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1653 continue;
1654
1655 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001656 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001657 stream ? "capture" : "playback",
1658 be->dpcm[stream].state);
1659
1660 if (--be->dpcm[stream].users != 0)
1661 continue;
1662
1663 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1664 continue;
1665
1666 soc_pcm_close(be_substream);
1667 be_substream->runtime = NULL;
1668 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1669 }
1670
1671 return err;
1672}
1673
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001674static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Jerome Brunet435ffb72018-07-05 12:13:48 +02001675 struct snd_soc_pcm_stream *stream)
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001676{
1677 runtime->hw.rate_min = stream->rate_min;
1678 runtime->hw.rate_max = stream->rate_max;
1679 runtime->hw.channels_min = stream->channels_min;
1680 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001681 if (runtime->hw.formats)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001682 runtime->hw.formats &= stream->formats;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001683 else
Jerome Brunet435ffb72018-07-05 12:13:48 +02001684 runtime->hw.formats = stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001685 runtime->hw.rates = stream->rates;
1686}
1687
Jerome Brunet435ffb72018-07-05 12:13:48 +02001688static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1689 u64 *formats)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001690{
1691 struct snd_soc_pcm_runtime *fe = substream->private_data;
1692 struct snd_soc_dpcm *dpcm;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001693 int stream = substream->stream;
1694
1695 if (!fe->dai_link->dpcm_merged_format)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001696 return;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001697
1698 /*
1699 * It returns merged BE codec format
1700 * if FE want to use it (= dpcm_merged_format)
1701 */
1702
1703 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1704 struct snd_soc_pcm_runtime *be = dpcm->be;
1705 struct snd_soc_dai_driver *codec_dai_drv;
1706 struct snd_soc_pcm_stream *codec_stream;
1707 int i;
1708
1709 for (i = 0; i < be->num_codecs; i++) {
1710 codec_dai_drv = be->codec_dais[i]->driver;
1711 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1712 codec_stream = &codec_dai_drv->playback;
1713 else
1714 codec_stream = &codec_dai_drv->capture;
1715
Jerome Brunet435ffb72018-07-05 12:13:48 +02001716 *formats &= codec_stream->formats;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001717 }
1718 }
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001719}
1720
Jerome Brunet435ffb72018-07-05 12:13:48 +02001721static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1722 unsigned int *channels_min,
1723 unsigned int *channels_max)
Jiada Wangf4c277b2018-06-20 18:25:20 +09001724{
1725 struct snd_soc_pcm_runtime *fe = substream->private_data;
1726 struct snd_soc_dpcm *dpcm;
1727 int stream = substream->stream;
1728
1729 if (!fe->dai_link->dpcm_merged_chan)
1730 return;
1731
Jiada Wangf4c277b2018-06-20 18:25:20 +09001732 /*
1733 * It returns merged BE codec channel;
1734 * if FE want to use it (= dpcm_merged_chan)
1735 */
1736
1737 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1738 struct snd_soc_pcm_runtime *be = dpcm->be;
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001739 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001740 struct snd_soc_dai_driver *codec_dai_drv;
1741 struct snd_soc_pcm_stream *codec_stream;
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001742 struct snd_soc_pcm_stream *cpu_stream;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001743
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001744 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1745 cpu_stream = &cpu_dai_drv->playback;
1746 else
1747 cpu_stream = &cpu_dai_drv->capture;
1748
1749 *channels_min = max(*channels_min, cpu_stream->channels_min);
1750 *channels_max = min(*channels_max, cpu_stream->channels_max);
1751
1752 /*
1753 * chan min/max cannot be enforced if there are multiple CODEC
1754 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1755 */
1756 if (be->num_codecs == 1) {
1757 codec_dai_drv = be->codec_dais[0]->driver;
1758
Jiada Wangf4c277b2018-06-20 18:25:20 +09001759 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1760 codec_stream = &codec_dai_drv->playback;
1761 else
1762 codec_stream = &codec_dai_drv->capture;
1763
1764 *channels_min = max(*channels_min,
1765 codec_stream->channels_min);
1766 *channels_max = min(*channels_max,
1767 codec_stream->channels_max);
1768 }
1769 }
1770}
1771
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001772static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1773 unsigned int *rates,
1774 unsigned int *rate_min,
1775 unsigned int *rate_max)
1776{
1777 struct snd_soc_pcm_runtime *fe = substream->private_data;
1778 struct snd_soc_dpcm *dpcm;
1779 int stream = substream->stream;
1780
1781 if (!fe->dai_link->dpcm_merged_rate)
1782 return;
1783
1784 /*
1785 * It returns merged BE codec channel;
1786 * if FE want to use it (= dpcm_merged_chan)
1787 */
1788
1789 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1790 struct snd_soc_pcm_runtime *be = dpcm->be;
1791 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1792 struct snd_soc_dai_driver *codec_dai_drv;
1793 struct snd_soc_pcm_stream *codec_stream;
1794 struct snd_soc_pcm_stream *cpu_stream;
1795 int i;
1796
1797 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1798 cpu_stream = &cpu_dai_drv->playback;
1799 else
1800 cpu_stream = &cpu_dai_drv->capture;
1801
1802 *rate_min = max(*rate_min, cpu_stream->rate_min);
1803 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1804 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1805
1806 for (i = 0; i < be->num_codecs; i++) {
1807 /*
1808 * Skip CODECs which don't support the current stream
1809 * type. See soc_pcm_init_runtime_hw() for more details
1810 */
1811 if (!snd_soc_dai_stream_valid(be->codec_dais[i],
1812 stream))
1813 continue;
1814
1815 codec_dai_drv = be->codec_dais[i]->driver;
1816 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1817 codec_stream = &codec_dai_drv->playback;
1818 else
1819 codec_stream = &codec_dai_drv->capture;
1820
1821 *rate_min = max(*rate_min, codec_stream->rate_min);
1822 *rate_max = min_not_zero(*rate_max,
1823 codec_stream->rate_max);
1824 *rates = snd_pcm_rate_mask_intersect(*rates,
1825 codec_stream->rates);
1826 }
1827 }
1828}
1829
Mark Brown45c0a182012-05-09 21:46:27 +01001830static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001831{
1832 struct snd_pcm_runtime *runtime = substream->runtime;
1833 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1834 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1835 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1836
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001837 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001838 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001839 else
Jerome Brunet435ffb72018-07-05 12:13:48 +02001840 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
Jiada Wangf4c277b2018-06-20 18:25:20 +09001841
Jerome Brunet435ffb72018-07-05 12:13:48 +02001842 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1843 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1844 &runtime->hw.channels_max);
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001845 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1846 &runtime->hw.rate_min, &runtime->hw.rate_max);
Liam Girdwood01d75842012-04-25 12:12:49 +01001847}
1848
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001849static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1850
1851/* Set FE's runtime_update state; the state is protected via PCM stream lock
1852 * for avoiding the race with trigger callback.
1853 * If the state is unset and a trigger is pending while the previous operation,
1854 * process the pending trigger action here.
1855 */
1856static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1857 int stream, enum snd_soc_dpcm_update state)
1858{
1859 struct snd_pcm_substream *substream =
1860 snd_soc_dpcm_get_substream(fe, stream);
1861
1862 snd_pcm_stream_lock_irq(substream);
1863 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1864 dpcm_fe_dai_do_trigger(substream,
1865 fe->dpcm[stream].trigger_pending - 1);
1866 fe->dpcm[stream].trigger_pending = 0;
1867 }
1868 fe->dpcm[stream].runtime_update = state;
1869 snd_pcm_stream_unlock_irq(substream);
1870}
1871
PC Liao906c7d62015-12-11 11:33:51 +08001872static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1873 int stream)
1874{
1875 struct snd_soc_dpcm *dpcm;
1876 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1877 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1878 int err;
1879
1880 /* apply symmetry for FE */
1881 if (soc_pcm_has_symmetry(fe_substream))
1882 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1883
1884 /* Symmetry only applies if we've got an active stream. */
1885 if (fe_cpu_dai->active) {
1886 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1887 if (err < 0)
1888 return err;
1889 }
1890
1891 /* apply symmetry for BE */
1892 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1893 struct snd_soc_pcm_runtime *be = dpcm->be;
1894 struct snd_pcm_substream *be_substream =
1895 snd_soc_dpcm_get_substream(be, stream);
1896 struct snd_soc_pcm_runtime *rtd = be_substream->private_data;
1897 int i;
1898
Jeeja KPf1176612016-09-06 14:17:55 +05301899 if (rtd->dai_link->be_hw_params_fixup)
1900 continue;
1901
PC Liao906c7d62015-12-11 11:33:51 +08001902 if (soc_pcm_has_symmetry(be_substream))
1903 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1904
1905 /* Symmetry only applies if we've got an active stream. */
1906 if (rtd->cpu_dai->active) {
Kai Chieh Chuang99bcedb2018-05-28 10:18:19 +08001907 err = soc_pcm_apply_symmetry(fe_substream,
1908 rtd->cpu_dai);
PC Liao906c7d62015-12-11 11:33:51 +08001909 if (err < 0)
1910 return err;
1911 }
1912
1913 for (i = 0; i < rtd->num_codecs; i++) {
1914 if (rtd->codec_dais[i]->active) {
Kai Chieh Chuang99bcedb2018-05-28 10:18:19 +08001915 err = soc_pcm_apply_symmetry(fe_substream,
PC Liao906c7d62015-12-11 11:33:51 +08001916 rtd->codec_dais[i]);
1917 if (err < 0)
1918 return err;
1919 }
1920 }
1921 }
1922
1923 return 0;
1924}
1925
Liam Girdwood01d75842012-04-25 12:12:49 +01001926static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1927{
1928 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1929 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1930 int stream = fe_substream->stream, ret = 0;
1931
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001932 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001933
1934 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1935 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001936 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001937 goto be_err;
1938 }
1939
Liam Girdwood103d84a2012-11-19 14:39:15 +00001940 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001941
1942 /* start the DAI frontend */
1943 ret = soc_pcm_open(fe_substream);
1944 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001945 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001946 goto unwind;
1947 }
1948
1949 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1950
1951 dpcm_set_fe_runtime(fe_substream);
1952 snd_pcm_limit_hw_rates(runtime);
1953
PC Liao906c7d62015-12-11 11:33:51 +08001954 ret = dpcm_apply_symmetry(fe_substream, stream);
1955 if (ret < 0) {
1956 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1957 ret);
1958 goto unwind;
1959 }
1960
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001961 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001962 return 0;
1963
1964unwind:
1965 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1966be_err:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001967 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001968 return ret;
1969}
1970
Liam Girdwood23607022014-01-17 17:03:55 +00001971int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001972{
1973 struct snd_soc_dpcm *dpcm;
1974
1975 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1976 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1977
1978 struct snd_soc_pcm_runtime *be = dpcm->be;
1979 struct snd_pcm_substream *be_substream =
1980 snd_soc_dpcm_get_substream(be, stream);
1981
1982 /* is this op for this BE ? */
1983 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1984 continue;
1985
1986 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001987 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001988 stream ? "capture" : "playback",
1989 be->dpcm[stream].state);
1990
1991 if (--be->dpcm[stream].users != 0)
1992 continue;
1993
1994 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Kai Chieh Chuang9c0ac702018-05-28 10:18:18 +08001995 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1996 soc_pcm_hw_free(be_substream);
1997 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1998 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001999
Liam Girdwood103d84a2012-11-19 14:39:15 +00002000 dev_dbg(be->dev, "ASoC: close BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002001 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002002
2003 soc_pcm_close(be_substream);
2004 be_substream->runtime = NULL;
2005
2006 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2007 }
2008 return 0;
2009}
2010
2011static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
2012{
2013 struct snd_soc_pcm_runtime *fe = substream->private_data;
2014 int stream = substream->stream;
2015
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002016 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002017
2018 /* shutdown the BEs */
2019 dpcm_be_dai_shutdown(fe, substream->stream);
2020
Liam Girdwood103d84a2012-11-19 14:39:15 +00002021 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002022
2023 /* now shutdown the frontend */
2024 soc_pcm_close(substream);
2025
2026 /* run the stream event for each BE */
2027 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
2028
2029 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002030 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002031 return 0;
2032}
2033
Liam Girdwood23607022014-01-17 17:03:55 +00002034int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002035{
2036 struct snd_soc_dpcm *dpcm;
2037
2038 /* only hw_params backends that are either sinks or sources
2039 * to this frontend DAI */
2040 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2041
2042 struct snd_soc_pcm_runtime *be = dpcm->be;
2043 struct snd_pcm_substream *be_substream =
2044 snd_soc_dpcm_get_substream(be, stream);
2045
2046 /* is this op for this BE ? */
2047 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2048 continue;
2049
2050 /* only free hw when no longer used - check all FEs */
2051 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2052 continue;
2053
Qiao Zhou36fba622014-12-03 10:13:43 +08002054 /* do not free hw if this BE is used by other FE */
2055 if (be->dpcm[stream].users > 1)
2056 continue;
2057
Liam Girdwood01d75842012-04-25 12:12:49 +01002058 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2059 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2060 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08002061 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Vinod Koul5e82d2b2016-02-01 22:26:40 +05302062 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2063 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01002064 continue;
2065
Liam Girdwood103d84a2012-11-19 14:39:15 +00002066 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002067 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002068
2069 soc_pcm_hw_free(be_substream);
2070
2071 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2072 }
2073
2074 return 0;
2075}
2076
Mark Brown45c0a182012-05-09 21:46:27 +01002077static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002078{
2079 struct snd_soc_pcm_runtime *fe = substream->private_data;
2080 int err, stream = substream->stream;
2081
2082 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002083 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002084
Liam Girdwood103d84a2012-11-19 14:39:15 +00002085 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002086
2087 /* call hw_free on the frontend */
2088 err = soc_pcm_hw_free(substream);
2089 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002090 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002091 fe->dai_link->name);
2092
2093 /* only hw_params backends that are either sinks or sources
2094 * to this frontend DAI */
2095 err = dpcm_be_dai_hw_free(fe, stream);
2096
2097 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002098 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002099
2100 mutex_unlock(&fe->card->mutex);
2101 return 0;
2102}
2103
Liam Girdwood23607022014-01-17 17:03:55 +00002104int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002105{
2106 struct snd_soc_dpcm *dpcm;
2107 int ret;
2108
2109 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2110
2111 struct snd_soc_pcm_runtime *be = dpcm->be;
2112 struct snd_pcm_substream *be_substream =
2113 snd_soc_dpcm_get_substream(be, stream);
2114
2115 /* is this op for this BE ? */
2116 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2117 continue;
2118
Liam Girdwood01d75842012-04-25 12:12:49 +01002119 /* copy params for each dpcm */
2120 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2121 sizeof(struct snd_pcm_hw_params));
2122
2123 /* perform any hw_params fixups */
2124 if (be->dai_link->be_hw_params_fixup) {
2125 ret = be->dai_link->be_hw_params_fixup(be,
2126 &dpcm->hw_params);
2127 if (ret < 0) {
2128 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002129 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002130 ret);
2131 goto unwind;
2132 }
2133 }
2134
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002135 /* only allow hw_params() if no connected FEs are running */
2136 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2137 continue;
2138
2139 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2140 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2141 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2142 continue;
2143
2144 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002145 be->dai_link->name);
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002146
Liam Girdwood01d75842012-04-25 12:12:49 +01002147 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2148 if (ret < 0) {
2149 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002150 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002151 goto unwind;
2152 }
2153
2154 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2155 }
2156 return 0;
2157
2158unwind:
2159 /* disable any enabled and non active backends */
2160 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2161 struct snd_soc_pcm_runtime *be = dpcm->be;
2162 struct snd_pcm_substream *be_substream =
2163 snd_soc_dpcm_get_substream(be, stream);
2164
2165 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2166 continue;
2167
2168 /* only allow hw_free() if no connected FEs are running */
2169 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2170 continue;
2171
2172 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2173 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2174 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2175 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2176 continue;
2177
2178 soc_pcm_hw_free(be_substream);
2179 }
2180
2181 return ret;
2182}
2183
Mark Brown45c0a182012-05-09 21:46:27 +01002184static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2185 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01002186{
2187 struct snd_soc_pcm_runtime *fe = substream->private_data;
2188 int ret, stream = substream->stream;
2189
2190 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002191 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002192
2193 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2194 sizeof(struct snd_pcm_hw_params));
2195 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2196 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002197 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002198 goto out;
2199 }
2200
Liam Girdwood103d84a2012-11-19 14:39:15 +00002201 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002202 fe->dai_link->name, params_rate(params),
2203 params_channels(params), params_format(params));
2204
2205 /* call hw_params on the frontend */
2206 ret = soc_pcm_hw_params(substream, params);
2207 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002208 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002209 dpcm_be_dai_hw_free(fe, stream);
2210 } else
2211 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2212
2213out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002214 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002215 mutex_unlock(&fe->card->mutex);
2216 return ret;
2217}
2218
2219static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2220 struct snd_pcm_substream *substream, int cmd)
2221{
2222 int ret;
2223
Liam Girdwood103d84a2012-11-19 14:39:15 +00002224 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
彭东林94d215c2016-09-26 08:29:31 +00002225 dpcm->be->dai_link->name, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002226
2227 ret = soc_pcm_trigger(substream, cmd);
2228 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002229 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002230
2231 return ret;
2232}
2233
Liam Girdwood23607022014-01-17 17:03:55 +00002234int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01002235 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002236{
2237 struct snd_soc_dpcm *dpcm;
2238 int ret = 0;
2239
2240 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2241
2242 struct snd_soc_pcm_runtime *be = dpcm->be;
2243 struct snd_pcm_substream *be_substream =
2244 snd_soc_dpcm_get_substream(be, stream);
2245
2246 /* is this op for this BE ? */
2247 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2248 continue;
2249
2250 switch (cmd) {
2251 case SNDRV_PCM_TRIGGER_START:
2252 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2253 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2254 continue;
2255
2256 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2257 if (ret)
2258 return ret;
2259
2260 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2261 break;
2262 case SNDRV_PCM_TRIGGER_RESUME:
2263 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2264 continue;
2265
2266 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2267 if (ret)
2268 return ret;
2269
2270 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2271 break;
2272 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2273 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2274 continue;
2275
2276 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2277 if (ret)
2278 return ret;
2279
2280 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2281 break;
2282 case SNDRV_PCM_TRIGGER_STOP:
2283 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2284 continue;
2285
2286 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2287 continue;
2288
2289 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2290 if (ret)
2291 return ret;
2292
2293 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2294 break;
2295 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08002296 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01002297 continue;
2298
2299 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2300 continue;
2301
2302 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2303 if (ret)
2304 return ret;
2305
2306 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2307 break;
2308 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2309 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2310 continue;
2311
2312 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2313 continue;
2314
2315 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2316 if (ret)
2317 return ret;
2318
2319 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2320 break;
2321 }
2322 }
2323
2324 return ret;
2325}
2326EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2327
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002328static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002329{
2330 struct snd_soc_pcm_runtime *fe = substream->private_data;
2331 int stream = substream->stream, ret;
2332 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2333
2334 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2335
2336 switch (trigger) {
2337 case SND_SOC_DPCM_TRIGGER_PRE:
2338 /* call trigger on the frontend before the backend. */
2339
Liam Girdwood103d84a2012-11-19 14:39:15 +00002340 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002341 fe->dai_link->name, cmd);
2342
2343 ret = soc_pcm_trigger(substream, cmd);
2344 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002345 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002346 goto out;
2347 }
2348
2349 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2350 break;
2351 case SND_SOC_DPCM_TRIGGER_POST:
2352 /* call trigger on the frontend after the backend. */
2353
2354 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2355 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002356 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002357 goto out;
2358 }
2359
Liam Girdwood103d84a2012-11-19 14:39:15 +00002360 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002361 fe->dai_link->name, cmd);
2362
2363 ret = soc_pcm_trigger(substream, cmd);
2364 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002365 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2366 /* bespoke trigger() - handles both FE and BEs */
2367
Liam Girdwood103d84a2012-11-19 14:39:15 +00002368 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002369 fe->dai_link->name, cmd);
2370
2371 ret = soc_pcm_bespoke_trigger(substream, cmd);
2372 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002373 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002374 goto out;
2375 }
2376 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002377 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00002378 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01002379 fe->dai_link->name);
2380 ret = -EINVAL;
2381 goto out;
2382 }
2383
2384 switch (cmd) {
2385 case SNDRV_PCM_TRIGGER_START:
2386 case SNDRV_PCM_TRIGGER_RESUME:
2387 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2388 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2389 break;
2390 case SNDRV_PCM_TRIGGER_STOP:
2391 case SNDRV_PCM_TRIGGER_SUSPEND:
Liam Girdwood01d75842012-04-25 12:12:49 +01002392 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2393 break;
Patrick Lai9f169b92016-12-31 22:44:39 -08002394 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2395 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2396 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002397 }
2398
2399out:
2400 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2401 return ret;
2402}
2403
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002404static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2405{
2406 struct snd_soc_pcm_runtime *fe = substream->private_data;
2407 int stream = substream->stream;
2408
2409 /* if FE's runtime_update is already set, we're in race;
2410 * process this trigger later at exit
2411 */
2412 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2413 fe->dpcm[stream].trigger_pending = cmd + 1;
2414 return 0; /* delayed, assuming it's successful */
2415 }
2416
2417 /* we're alone, let's trigger */
2418 return dpcm_fe_dai_do_trigger(substream, cmd);
2419}
2420
Liam Girdwood23607022014-01-17 17:03:55 +00002421int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002422{
2423 struct snd_soc_dpcm *dpcm;
2424 int ret = 0;
2425
2426 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2427
2428 struct snd_soc_pcm_runtime *be = dpcm->be;
2429 struct snd_pcm_substream *be_substream =
2430 snd_soc_dpcm_get_substream(be, stream);
2431
2432 /* is this op for this BE ? */
2433 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2434 continue;
2435
2436 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
Koro Chen95f444d2015-10-28 10:15:34 +08002437 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2438 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01002439 continue;
2440
Liam Girdwood103d84a2012-11-19 14:39:15 +00002441 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002442 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002443
2444 ret = soc_pcm_prepare(be_substream);
2445 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002446 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002447 ret);
2448 break;
2449 }
2450
2451 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2452 }
2453 return ret;
2454}
2455
Mark Brown45c0a182012-05-09 21:46:27 +01002456static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002457{
2458 struct snd_soc_pcm_runtime *fe = substream->private_data;
2459 int stream = substream->stream, ret = 0;
2460
2461 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2462
Liam Girdwood103d84a2012-11-19 14:39:15 +00002463 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002464
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002465 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002466
2467 /* there is no point preparing this FE if there are no BEs */
2468 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002469 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002470 fe->dai_link->name);
2471 ret = -EINVAL;
2472 goto out;
2473 }
2474
2475 ret = dpcm_be_dai_prepare(fe, substream->stream);
2476 if (ret < 0)
2477 goto out;
2478
2479 /* call prepare on the frontend */
2480 ret = soc_pcm_prepare(substream);
2481 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002482 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002483 fe->dai_link->name);
2484 goto out;
2485 }
2486
2487 /* run the stream event for each BE */
2488 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2489 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2490
2491out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002492 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002493 mutex_unlock(&fe->card->mutex);
2494
2495 return ret;
2496}
2497
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002498static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2499 unsigned int cmd, void *arg)
2500{
2501 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +00002502 struct snd_soc_component *component;
2503 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002504
Kuninori Morimotob8135862017-10-11 01:37:23 +00002505 for_each_rtdcom(rtd, rtdcom) {
2506 component = rtdcom->component;
2507
Kuninori Morimotob8135862017-10-11 01:37:23 +00002508 if (!component->driver->ops ||
2509 !component->driver->ops->ioctl)
2510 continue;
2511
2512 /* FIXME: use 1st ioctl */
2513 return component->driver->ops->ioctl(substream, cmd, arg);
2514 }
2515
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002516 return snd_pcm_lib_ioctl(substream, cmd, arg);
2517}
2518
Liam Girdwood618dae12012-04-25 12:12:51 +01002519static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2520{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002521 struct snd_pcm_substream *substream =
2522 snd_soc_dpcm_get_substream(fe, stream);
2523 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002524 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01002525
Liam Girdwood103d84a2012-11-19 14:39:15 +00002526 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002527 stream ? "capture" : "playback", fe->dai_link->name);
2528
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002529 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2530 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002531 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002532 fe->dai_link->name);
2533
2534 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2535 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002536 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002537 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002538 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002539 fe->dai_link->name);
2540
2541 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2542 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002543 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002544 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002545
2546 err = dpcm_be_dai_hw_free(fe, stream);
2547 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002548 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002549
2550 err = dpcm_be_dai_shutdown(fe, stream);
2551 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002552 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002553
2554 /* run the stream event for each BE */
2555 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2556
2557 return 0;
2558}
2559
2560static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2561{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002562 struct snd_pcm_substream *substream =
2563 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002564 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002565 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002566 int ret;
2567
Liam Girdwood103d84a2012-11-19 14:39:15 +00002568 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002569 stream ? "capture" : "playback", fe->dai_link->name);
2570
2571 /* Only start the BE if the FE is ready */
2572 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2573 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2574 return -EINVAL;
2575
2576 /* startup must always be called for new BEs */
2577 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002578 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002579 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01002580
2581 /* keep going if FE state is > open */
2582 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2583 return 0;
2584
2585 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002586 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002587 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01002588
2589 /* keep going if FE state is > hw_params */
2590 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2591 return 0;
2592
2593
2594 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002595 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002596 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002597
2598 /* run the stream event for each BE */
2599 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2600
2601 /* keep going if FE state is > prepare */
2602 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2603 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2604 return 0;
2605
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002606 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2607 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002608 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002609 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01002610
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002611 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2612 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002613 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002614 goto hw_free;
2615 }
2616 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002617 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002618 fe->dai_link->name);
2619
2620 ret = dpcm_be_dai_trigger(fe, stream,
2621 SNDRV_PCM_TRIGGER_START);
2622 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002623 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002624 goto hw_free;
2625 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002626 }
2627
2628 return 0;
2629
2630hw_free:
2631 dpcm_be_dai_hw_free(fe, stream);
2632close:
2633 dpcm_be_dai_shutdown(fe, stream);
2634disconnect:
2635 /* disconnect any non started BEs */
2636 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2637 struct snd_soc_pcm_runtime *be = dpcm->be;
2638 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2639 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2640 }
2641
2642 return ret;
2643}
2644
2645static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2646{
2647 int ret;
2648
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002649 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002650 ret = dpcm_run_update_startup(fe, stream);
2651 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002652 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002653 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002654
2655 return ret;
2656}
2657
2658static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2659{
2660 int ret;
2661
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002662 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002663 ret = dpcm_run_update_shutdown(fe, stream);
2664 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002665 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002666 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002667
2668 return ret;
2669}
2670
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002671static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2672{
2673 struct snd_soc_dapm_widget_list *list;
2674 int count, paths;
2675
2676 if (!fe->dai_link->dynamic)
2677 return 0;
2678
2679 /* only check active links */
2680 if (!fe->cpu_dai->active)
2681 return 0;
2682
2683 /* DAPM sync will call this to update DSP paths */
2684 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2685 new ? "new" : "old", fe->dai_link->name);
2686
2687 /* skip if FE doesn't have playback capability */
2688 if (!fe->cpu_dai->driver->playback.channels_min ||
2689 !fe->codec_dai->driver->playback.channels_min)
2690 goto capture;
2691
2692 /* skip if FE isn't currently playing */
2693 if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
2694 goto capture;
2695
2696 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2697 if (paths < 0) {
2698 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2699 fe->dai_link->name, "playback");
2700 return paths;
2701 }
2702
2703 /* update any playback paths */
2704 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
2705 if (count) {
2706 if (new)
2707 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2708 else
2709 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2710
2711 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2712 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2713 }
2714
2715 dpcm_path_put(&list);
2716
2717capture:
2718 /* skip if FE doesn't have capture capability */
2719 if (!fe->cpu_dai->driver->capture.channels_min ||
2720 !fe->codec_dai->driver->capture.channels_min)
2721 return 0;
2722
2723 /* skip if FE isn't currently capturing */
2724 if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
2725 return 0;
2726
2727 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2728 if (paths < 0) {
2729 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2730 fe->dai_link->name, "capture");
2731 return paths;
2732 }
2733
2734 /* update any old capture paths */
2735 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
2736 if (count) {
2737 if (new)
2738 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2739 else
2740 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2741
2742 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2743 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2744 }
2745
2746 dpcm_path_put(&list);
2747
2748 return 0;
2749}
2750
Liam Girdwood618dae12012-04-25 12:12:51 +01002751/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2752 * any DAI links.
2753 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002754int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002755{
Mengdong Lin1a497982015-11-18 02:34:11 -05002756 struct snd_soc_pcm_runtime *fe;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002757 int ret = 0;
Liam Girdwood618dae12012-04-25 12:12:51 +01002758
Liam Girdwood618dae12012-04-25 12:12:51 +01002759 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002760 /* shutdown all old paths first */
Mengdong Lin1a497982015-11-18 02:34:11 -05002761 list_for_each_entry(fe, &card->rtd_list, list) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002762 ret = soc_dpcm_fe_runtime_update(fe, 0);
2763 if (ret)
2764 goto out;
Liam Girdwood618dae12012-04-25 12:12:51 +01002765 }
2766
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002767 /* bring new paths up */
2768 list_for_each_entry(fe, &card->rtd_list, list) {
2769 ret = soc_dpcm_fe_runtime_update(fe, 1);
2770 if (ret)
2771 goto out;
2772 }
2773
2774out:
Liam Girdwood618dae12012-04-25 12:12:51 +01002775 mutex_unlock(&card->mutex);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002776 return ret;
Liam Girdwood618dae12012-04-25 12:12:51 +01002777}
Liam Girdwood01d75842012-04-25 12:12:49 +01002778int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2779{
2780 struct snd_soc_dpcm *dpcm;
2781 struct list_head *clients =
2782 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2783
2784 list_for_each_entry(dpcm, clients, list_be) {
2785
2786 struct snd_soc_pcm_runtime *be = dpcm->be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002787 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01002788
2789 if (be->dai_link->ignore_suspend)
2790 continue;
2791
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002792 for (i = 0; i < be->num_codecs; i++) {
2793 struct snd_soc_dai *dai = be->codec_dais[i];
2794 struct snd_soc_dai_driver *drv = dai->driver;
Liam Girdwood01d75842012-04-25 12:12:49 +01002795
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002796 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2797 be->dai_link->name);
2798
2799 if (drv->ops && drv->ops->digital_mute &&
2800 dai->playback_active)
2801 drv->ops->digital_mute(dai, mute);
2802 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002803 }
2804
2805 return 0;
2806}
2807
Mark Brown45c0a182012-05-09 21:46:27 +01002808static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002809{
2810 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2811 struct snd_soc_dpcm *dpcm;
2812 struct snd_soc_dapm_widget_list *list;
2813 int ret;
2814 int stream = fe_substream->stream;
2815
2816 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2817 fe->dpcm[stream].runtime = fe_substream->runtime;
2818
Qiao Zhou8f70e512014-09-10 17:54:07 +08002819 ret = dpcm_path_get(fe, stream, &list);
2820 if (ret < 0) {
2821 mutex_unlock(&fe->card->mutex);
2822 return ret;
2823 } else if (ret == 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002824 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002825 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002826 }
2827
2828 /* calculate valid and active FE <-> BE dpcms */
2829 dpcm_process_paths(fe, stream, &list, 1);
2830
2831 ret = dpcm_fe_dai_startup(fe_substream);
2832 if (ret < 0) {
2833 /* clean up all links */
2834 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2835 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2836
2837 dpcm_be_disconnect(fe, stream);
2838 fe->dpcm[stream].runtime = NULL;
2839 }
2840
2841 dpcm_clear_pending_state(fe, stream);
2842 dpcm_path_put(&list);
2843 mutex_unlock(&fe->card->mutex);
2844 return ret;
2845}
2846
Mark Brown45c0a182012-05-09 21:46:27 +01002847static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002848{
2849 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2850 struct snd_soc_dpcm *dpcm;
2851 int stream = fe_substream->stream, ret;
2852
2853 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2854 ret = dpcm_fe_dai_shutdown(fe_substream);
2855
2856 /* mark FE's links ready to prune */
2857 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2858 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2859
2860 dpcm_be_disconnect(fe, stream);
2861
2862 fe->dpcm[stream].runtime = NULL;
2863 mutex_unlock(&fe->card->mutex);
2864 return ret;
2865}
2866
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02002867static void soc_pcm_private_free(struct snd_pcm *pcm)
2868{
2869 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002870 struct snd_soc_rtdcom_list *rtdcom;
2871 struct snd_soc_component *component;
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02002872
Kuninori Morimotof30a4c32018-01-24 05:18:46 +00002873 /* need to sync the delayed work before releasing resources */
2874 flush_delayed_work(&rtd->delayed_work);
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002875 for_each_rtdcom(rtd, rtdcom) {
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002876 component = rtdcom->component;
2877
Kuninori Morimoto11fb14f2018-05-08 03:19:49 +00002878 if (component->driver->pcm_free)
2879 component->driver->pcm_free(pcm);
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002880 }
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02002881}
2882
Kuninori Morimotob8135862017-10-11 01:37:23 +00002883static int soc_rtdcom_ack(struct snd_pcm_substream *substream)
2884{
2885 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2886 struct snd_soc_rtdcom_list *rtdcom;
2887 struct snd_soc_component *component;
2888
2889 for_each_rtdcom(rtd, rtdcom) {
2890 component = rtdcom->component;
2891
2892 if (!component->driver->ops ||
2893 !component->driver->ops->ack)
2894 continue;
2895
2896 /* FIXME. it returns 1st ask now */
2897 return component->driver->ops->ack(substream);
2898 }
2899
2900 return -EINVAL;
2901}
2902
2903static int soc_rtdcom_copy_user(struct snd_pcm_substream *substream, int channel,
2904 unsigned long pos, void __user *buf,
2905 unsigned long bytes)
2906{
2907 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2908 struct snd_soc_rtdcom_list *rtdcom;
2909 struct snd_soc_component *component;
2910
2911 for_each_rtdcom(rtd, rtdcom) {
2912 component = rtdcom->component;
2913
2914 if (!component->driver->ops ||
2915 !component->driver->ops->copy_user)
2916 continue;
2917
2918 /* FIXME. it returns 1st copy now */
2919 return component->driver->ops->copy_user(substream, channel,
2920 pos, buf, bytes);
2921 }
2922
2923 return -EINVAL;
2924}
2925
2926static int soc_rtdcom_copy_kernel(struct snd_pcm_substream *substream, int channel,
2927 unsigned long pos, void *buf, unsigned long bytes)
2928{
2929 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2930 struct snd_soc_rtdcom_list *rtdcom;
2931 struct snd_soc_component *component;
2932
2933 for_each_rtdcom(rtd, rtdcom) {
2934 component = rtdcom->component;
2935
2936 if (!component->driver->ops ||
2937 !component->driver->ops->copy_kernel)
2938 continue;
2939
2940 /* FIXME. it returns 1st copy now */
2941 return component->driver->ops->copy_kernel(substream, channel,
2942 pos, buf, bytes);
2943 }
2944
2945 return -EINVAL;
2946}
2947
2948static int soc_rtdcom_fill_silence(struct snd_pcm_substream *substream, int channel,
2949 unsigned long pos, unsigned long bytes)
2950{
2951 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2952 struct snd_soc_rtdcom_list *rtdcom;
2953 struct snd_soc_component *component;
2954
2955 for_each_rtdcom(rtd, rtdcom) {
2956 component = rtdcom->component;
2957
2958 if (!component->driver->ops ||
2959 !component->driver->ops->fill_silence)
2960 continue;
2961
2962 /* FIXME. it returns 1st silence now */
2963 return component->driver->ops->fill_silence(substream, channel,
2964 pos, bytes);
2965 }
2966
2967 return -EINVAL;
2968}
2969
2970static struct page *soc_rtdcom_page(struct snd_pcm_substream *substream,
2971 unsigned long offset)
2972{
2973 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2974 struct snd_soc_rtdcom_list *rtdcom;
2975 struct snd_soc_component *component;
2976 struct page *page;
2977
2978 for_each_rtdcom(rtd, rtdcom) {
2979 component = rtdcom->component;
2980
2981 if (!component->driver->ops ||
2982 !component->driver->ops->page)
2983 continue;
2984
2985 /* FIXME. it returns 1st page now */
2986 page = component->driver->ops->page(substream, offset);
2987 if (page)
2988 return page;
2989 }
2990
2991 return NULL;
2992}
2993
2994static int soc_rtdcom_mmap(struct snd_pcm_substream *substream,
2995 struct vm_area_struct *vma)
2996{
2997 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2998 struct snd_soc_rtdcom_list *rtdcom;
2999 struct snd_soc_component *component;
3000
3001 for_each_rtdcom(rtd, rtdcom) {
3002 component = rtdcom->component;
3003
3004 if (!component->driver->ops ||
3005 !component->driver->ops->mmap)
3006 continue;
3007
3008 /* FIXME. it returns 1st mmap now */
3009 return component->driver->ops->mmap(substream, vma);
3010 }
3011
3012 return -EINVAL;
3013}
3014
Liam Girdwoodddee6272011-06-09 14:45:53 +01003015/* create a new pcm */
3016int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
3017{
Benoit Cousson2e5894d2014-07-08 23:19:35 +02003018 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003019 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimotof523ace2017-09-26 01:00:53 +00003020 struct snd_soc_component *component;
3021 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003022 struct snd_pcm *pcm;
3023 char new_name[64];
3024 int ret = 0, playback = 0, capture = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02003025 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003026
Liam Girdwood01d75842012-04-25 12:12:49 +01003027 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00003028 playback = rtd->dai_link->dpcm_playback;
3029 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01003030 } else {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02003031 for (i = 0; i < rtd->num_codecs; i++) {
3032 codec_dai = rtd->codec_dais[i];
3033 if (codec_dai->driver->playback.channels_min)
3034 playback = 1;
3035 if (codec_dai->driver->capture.channels_min)
3036 capture = 1;
3037 }
3038
3039 capture = capture && cpu_dai->driver->capture.channels_min;
3040 playback = playback && cpu_dai->driver->playback.channels_min;
Liam Girdwood01d75842012-04-25 12:12:49 +01003041 }
Sangsu Parka5002312012-01-02 17:15:10 +09003042
Fabio Estevamd6bead02013-08-29 10:32:13 -03003043 if (rtd->dai_link->playback_only) {
3044 playback = 1;
3045 capture = 0;
3046 }
3047
3048 if (rtd->dai_link->capture_only) {
3049 playback = 0;
3050 capture = 1;
3051 }
3052
Liam Girdwood01d75842012-04-25 12:12:49 +01003053 /* create the PCM */
3054 if (rtd->dai_link->no_pcm) {
3055 snprintf(new_name, sizeof(new_name), "(%s)",
3056 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003057
Liam Girdwood01d75842012-04-25 12:12:49 +01003058 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
3059 playback, capture, &pcm);
3060 } else {
3061 if (rtd->dai_link->dynamic)
3062 snprintf(new_name, sizeof(new_name), "%s (*)",
3063 rtd->dai_link->stream_name);
3064 else
3065 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02003066 rtd->dai_link->stream_name,
3067 (rtd->num_codecs > 1) ?
3068 "multicodec" : rtd->codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003069
Liam Girdwood01d75842012-04-25 12:12:49 +01003070 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
3071 capture, &pcm);
3072 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01003073 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00003074 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01003075 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003076 return ret;
3077 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00003078 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003079
3080 /* DAPM dai link stream work */
3081 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
3082
Vinod Koul48c76992015-02-12 09:59:53 +05303083 pcm->nonatomic = rtd->dai_link->nonatomic;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003084 rtd->pcm = pcm;
3085 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01003086
3087 if (rtd->dai_link->no_pcm) {
3088 if (playback)
3089 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
3090 if (capture)
3091 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
3092 goto out;
3093 }
3094
3095 /* ASoC PCM operations */
3096 if (rtd->dai_link->dynamic) {
3097 rtd->ops.open = dpcm_fe_dai_open;
3098 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
3099 rtd->ops.prepare = dpcm_fe_dai_prepare;
3100 rtd->ops.trigger = dpcm_fe_dai_trigger;
3101 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
3102 rtd->ops.close = dpcm_fe_dai_close;
3103 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01003104 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01003105 } else {
3106 rtd->ops.open = soc_pcm_open;
3107 rtd->ops.hw_params = soc_pcm_hw_params;
3108 rtd->ops.prepare = soc_pcm_prepare;
3109 rtd->ops.trigger = soc_pcm_trigger;
3110 rtd->ops.hw_free = soc_pcm_hw_free;
3111 rtd->ops.close = soc_pcm_close;
3112 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01003113 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01003114 }
3115
Kuninori Morimotob8135862017-10-11 01:37:23 +00003116 for_each_rtdcom(rtd, rtdcom) {
3117 const struct snd_pcm_ops *ops = rtdcom->component->driver->ops;
3118
3119 if (!ops)
3120 continue;
3121
3122 if (ops->ack)
3123 rtd->ops.ack = soc_rtdcom_ack;
3124 if (ops->copy_user)
3125 rtd->ops.copy_user = soc_rtdcom_copy_user;
3126 if (ops->copy_kernel)
3127 rtd->ops.copy_kernel = soc_rtdcom_copy_kernel;
3128 if (ops->fill_silence)
3129 rtd->ops.fill_silence = soc_rtdcom_fill_silence;
3130 if (ops->page)
3131 rtd->ops.page = soc_rtdcom_page;
3132 if (ops->mmap)
3133 rtd->ops.mmap = soc_rtdcom_mmap;
3134 }
3135
Liam Girdwoodddee6272011-06-09 14:45:53 +01003136 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01003137 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003138
3139 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01003140 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003141
Kuninori Morimotof523ace2017-09-26 01:00:53 +00003142 for_each_rtdcom(rtd, rtdcom) {
3143 component = rtdcom->component;
3144
Kuninori Morimoto11fb14f2018-05-08 03:19:49 +00003145 if (!component->driver->pcm_new)
Kuninori Morimotof523ace2017-09-26 01:00:53 +00003146 continue;
3147
Kuninori Morimoto11fb14f2018-05-08 03:19:49 +00003148 ret = component->driver->pcm_new(rtd);
Johan Hovoldc641e5b2017-07-12 17:55:29 +02003149 if (ret < 0) {
Kuninori Morimotof523ace2017-09-26 01:00:53 +00003150 dev_err(component->dev,
Johan Hovoldc641e5b2017-07-12 17:55:29 +02003151 "ASoC: pcm constructor failed: %d\n",
3152 ret);
3153 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003154 }
3155 }
Johan Hovoldc641e5b2017-07-12 17:55:29 +02003156
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02003157 pcm->private_free = soc_pcm_private_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01003158out:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02003159 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3160 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3161 cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003162 return ret;
3163}
Liam Girdwood01d75842012-04-25 12:12:49 +01003164
3165/* is the current PCM operation for this FE ? */
3166int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3167{
3168 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3169 return 1;
3170 return 0;
3171}
3172EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3173
3174/* is the current PCM operation for this BE ? */
3175int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3176 struct snd_soc_pcm_runtime *be, int stream)
3177{
3178 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3179 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3180 be->dpcm[stream].runtime_update))
3181 return 1;
3182 return 0;
3183}
3184EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3185
3186/* get the substream for this BE */
3187struct snd_pcm_substream *
3188 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3189{
3190 return be->pcm->streams[stream].substream;
3191}
3192EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3193
3194/* get the BE runtime state */
3195enum snd_soc_dpcm_state
3196 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3197{
3198 return be->dpcm[stream].state;
3199}
3200EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3201
3202/* set the BE runtime state */
3203void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3204 int stream, enum snd_soc_dpcm_state state)
3205{
3206 be->dpcm[stream].state = state;
3207}
3208EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3209
3210/*
3211 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3212 * are not running, paused or suspended for the specified stream direction.
3213 */
3214int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3215 struct snd_soc_pcm_runtime *be, int stream)
3216{
3217 struct snd_soc_dpcm *dpcm;
3218 int state;
3219
3220 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3221
3222 if (dpcm->fe == fe)
3223 continue;
3224
3225 state = dpcm->fe->dpcm[stream].state;
3226 if (state == SND_SOC_DPCM_STATE_START ||
3227 state == SND_SOC_DPCM_STATE_PAUSED ||
3228 state == SND_SOC_DPCM_STATE_SUSPEND)
3229 return 0;
3230 }
3231
3232 /* it's safe to free/stop this BE DAI */
3233 return 1;
3234}
3235EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3236
3237/*
3238 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3239 * running, paused or suspended for the specified stream direction.
3240 */
3241int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3242 struct snd_soc_pcm_runtime *be, int stream)
3243{
3244 struct snd_soc_dpcm *dpcm;
3245 int state;
3246
3247 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3248
3249 if (dpcm->fe == fe)
3250 continue;
3251
3252 state = dpcm->fe->dpcm[stream].state;
3253 if (state == SND_SOC_DPCM_STATE_START ||
3254 state == SND_SOC_DPCM_STATE_PAUSED ||
3255 state == SND_SOC_DPCM_STATE_SUSPEND ||
3256 state == SND_SOC_DPCM_STATE_PREPARE)
3257 return 0;
3258 }
3259
3260 /* it's safe to change hw_params */
3261 return 1;
3262}
3263EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003264
3265#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen852801412016-11-22 11:29:14 +01003266static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003267{
3268 switch (state) {
3269 case SND_SOC_DPCM_STATE_NEW:
3270 return "new";
3271 case SND_SOC_DPCM_STATE_OPEN:
3272 return "open";
3273 case SND_SOC_DPCM_STATE_HW_PARAMS:
3274 return "hw_params";
3275 case SND_SOC_DPCM_STATE_PREPARE:
3276 return "prepare";
3277 case SND_SOC_DPCM_STATE_START:
3278 return "start";
3279 case SND_SOC_DPCM_STATE_STOP:
3280 return "stop";
3281 case SND_SOC_DPCM_STATE_SUSPEND:
3282 return "suspend";
3283 case SND_SOC_DPCM_STATE_PAUSED:
3284 return "paused";
3285 case SND_SOC_DPCM_STATE_HW_FREE:
3286 return "hw_free";
3287 case SND_SOC_DPCM_STATE_CLOSE:
3288 return "close";
3289 }
3290
3291 return "unknown";
3292}
3293
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003294static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3295 int stream, char *buf, size_t size)
3296{
3297 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3298 struct snd_soc_dpcm *dpcm;
3299 ssize_t offset = 0;
3300
3301 /* FE state */
3302 offset += snprintf(buf + offset, size - offset,
3303 "[%s - %s]\n", fe->dai_link->name,
3304 stream ? "Capture" : "Playback");
3305
3306 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3307 dpcm_state_string(fe->dpcm[stream].state));
3308
3309 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3310 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3311 offset += snprintf(buf + offset, size - offset,
3312 "Hardware Params: "
3313 "Format = %s, Channels = %d, Rate = %d\n",
3314 snd_pcm_format_name(params_format(params)),
3315 params_channels(params),
3316 params_rate(params));
3317
3318 /* BEs state */
3319 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3320
3321 if (list_empty(&fe->dpcm[stream].be_clients)) {
3322 offset += snprintf(buf + offset, size - offset,
3323 " No active DSP links\n");
3324 goto out;
3325 }
3326
3327 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
3328 struct snd_soc_pcm_runtime *be = dpcm->be;
3329 params = &dpcm->hw_params;
3330
3331 offset += snprintf(buf + offset, size - offset,
3332 "- %s\n", be->dai_link->name);
3333
3334 offset += snprintf(buf + offset, size - offset,
3335 " State: %s\n",
3336 dpcm_state_string(be->dpcm[stream].state));
3337
3338 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3339 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3340 offset += snprintf(buf + offset, size - offset,
3341 " Hardware Params: "
3342 "Format = %s, Channels = %d, Rate = %d\n",
3343 snd_pcm_format_name(params_format(params)),
3344 params_channels(params),
3345 params_rate(params));
3346 }
3347
3348out:
3349 return offset;
3350}
3351
3352static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3353 size_t count, loff_t *ppos)
3354{
3355 struct snd_soc_pcm_runtime *fe = file->private_data;
3356 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3357 char *buf;
3358
3359 buf = kmalloc(out_count, GFP_KERNEL);
3360 if (!buf)
3361 return -ENOMEM;
3362
3363 if (fe->cpu_dai->driver->playback.channels_min)
3364 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3365 buf + offset, out_count - offset);
3366
3367 if (fe->cpu_dai->driver->capture.channels_min)
3368 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3369 buf + offset, out_count - offset);
3370
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003371 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003372
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003373 kfree(buf);
3374 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003375}
3376
3377static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003378 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003379 .read = dpcm_state_read_file,
3380 .llseek = default_llseek,
3381};
3382
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003383void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003384{
Mark Brownb3bba9a2012-05-08 10:33:47 +01003385 if (!rtd->dai_link)
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003386 return;
Mark Brownb3bba9a2012-05-08 10:33:47 +01003387
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003388 if (!rtd->card->debugfs_card_root)
3389 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003390
3391 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3392 rtd->card->debugfs_card_root);
3393 if (!rtd->debugfs_dpcm_root) {
3394 dev_dbg(rtd->dev,
3395 "ASoC: Failed to create dpcm debugfs directory %s\n",
3396 rtd->dai_link->name);
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003397 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003398 }
3399
Fabio Estevamf1e3f402017-07-29 11:40:55 -03003400 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3401 rtd, &dpcm_state_fops);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003402}
3403#endif