blob: 8be1d22dc87a0f6e7543ec70e710643d0f9f62b4 [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
Lars-Peter Clausen90996f42013-05-14 11:05:30 +020031/**
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010032 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
33 * @rtd: ASoC PCM runtime that is activated
34 * @stream: Direction of the PCM stream
35 *
36 * Increments the active count for all the DAIs and components attached to a PCM
37 * runtime. Should typically be called when a stream is opened.
38 *
39 * Must be called with the rtd->pcm_mutex being held
40 */
41void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
42{
43 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +000044 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020045 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010046
47 lockdep_assert_held(&rtd->pcm_mutex);
48
49 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
50 cpu_dai->playback_active++;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +000051 for_each_rtd_codec_dai(rtd, i, codec_dai)
52 codec_dai->playback_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010053 } else {
54 cpu_dai->capture_active++;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +000055 for_each_rtd_codec_dai(rtd, i, codec_dai)
56 codec_dai->capture_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010057 }
58
59 cpu_dai->active++;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +010060 cpu_dai->component->active++;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +000061 for_each_rtd_codec_dai(rtd, i, codec_dai) {
62 codec_dai->active++;
63 codec_dai->component->active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020064 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010065}
66
67/**
68 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
69 * @rtd: ASoC PCM runtime that is deactivated
70 * @stream: Direction of the PCM stream
71 *
72 * Decrements the active count for all the DAIs and components attached to a PCM
73 * runtime. Should typically be called when a stream is closed.
74 *
75 * Must be called with the rtd->pcm_mutex being held
76 */
77void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
78{
79 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +000080 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020081 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010082
83 lockdep_assert_held(&rtd->pcm_mutex);
84
85 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
86 cpu_dai->playback_active--;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +000087 for_each_rtd_codec_dai(rtd, i, codec_dai)
88 codec_dai->playback_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010089 } else {
90 cpu_dai->capture_active--;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +000091 for_each_rtd_codec_dai(rtd, i, codec_dai)
92 codec_dai->capture_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010093 }
94
95 cpu_dai->active--;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +010096 cpu_dai->component->active--;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +000097 for_each_rtd_codec_dai(rtd, i, codec_dai) {
98 codec_dai->component->active--;
99 codec_dai->active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200100 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100101}
102
103/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100104 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
105 * @rtd: The ASoC PCM runtime that should be checked.
106 *
107 * This function checks whether the power down delay should be ignored for a
108 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
109 * been configured to ignore the delay, or if none of the components benefits
110 * from having the delay.
111 */
112bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
113{
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000114 struct snd_soc_rtdcom_list *rtdcom;
115 struct snd_soc_component *component;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200116 bool ignore = true;
117
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100118 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
119 return true;
120
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000121 for_each_rtdcom(rtd, rtdcom) {
122 component = rtdcom->component;
123
Kuninori Morimoto72c38182018-01-19 05:21:19 +0000124 ignore &= !component->driver->use_pmdown_time;
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000125 }
126
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000127 return ignore;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100128}
129
130/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200131 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
132 * @substream: the pcm substream
133 * @hw: the hardware parameters
134 *
135 * Sets the substream runtime hardware parameters.
136 */
137int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
138 const struct snd_pcm_hardware *hw)
139{
140 struct snd_pcm_runtime *runtime = substream->runtime;
141 runtime->hw.info = hw->info;
142 runtime->hw.formats = hw->formats;
143 runtime->hw.period_bytes_min = hw->period_bytes_min;
144 runtime->hw.period_bytes_max = hw->period_bytes_max;
145 runtime->hw.periods_min = hw->periods_min;
146 runtime->hw.periods_max = hw->periods_max;
147 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
148 runtime->hw.fifo_size = hw->fifo_size;
149 return 0;
150}
151EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
152
Liam Girdwood01d75842012-04-25 12:12:49 +0100153/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000154int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100155 int event)
156{
157 struct snd_soc_dpcm *dpcm;
158
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +0000159 for_each_dpcm_be(fe, dir, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +0100160
161 struct snd_soc_pcm_runtime *be = dpcm->be;
162
Liam Girdwood103d84a2012-11-19 14:39:15 +0000163 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100164 be->dai_link->name, event, dir);
165
Banajit Goswamib1cd2e32017-07-14 23:15:05 -0700166 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
167 (be->dpcm[dir].users >= 1))
168 continue;
169
Liam Girdwood01d75842012-04-25 12:12:49 +0100170 snd_soc_dapm_stream_event(be, dir, event);
171 }
172
173 snd_soc_dapm_stream_event(fe, dir, event);
174
175 return 0;
176}
177
Dong Aisheng17841022011-08-29 17:15:14 +0800178static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
179 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100180{
181 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100182 int ret;
183
Nicolin Chen3635bf02013-11-13 18:56:24 +0800184 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
185 rtd->dai_link->symmetric_rates)) {
186 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
187 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100188
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200189 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800190 SNDRV_PCM_HW_PARAM_RATE,
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200191 soc_dai->rate);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800192 if (ret < 0) {
193 dev_err(soc_dai->dev,
194 "ASoC: Unable to apply rate constraint: %d\n",
195 ret);
196 return ret;
197 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100198 }
199
Nicolin Chen3635bf02013-11-13 18:56:24 +0800200 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
201 rtd->dai_link->symmetric_channels)) {
202 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
203 soc_dai->channels);
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_CHANNELS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800207 soc_dai->channels);
208 if (ret < 0) {
209 dev_err(soc_dai->dev,
210 "ASoC: Unable to apply channel symmetry constraint: %d\n",
211 ret);
212 return ret;
213 }
214 }
215
216 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
217 rtd->dai_link->symmetric_samplebits)) {
218 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
219 soc_dai->sample_bits);
220
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_SAMPLE_BITS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800223 soc_dai->sample_bits);
224 if (ret < 0) {
225 dev_err(soc_dai->dev,
226 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
227 ret);
228 return ret;
229 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100230 }
231
232 return 0;
233}
234
Nicolin Chen3635bf02013-11-13 18:56:24 +0800235static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
236 struct snd_pcm_hw_params *params)
237{
238 struct snd_soc_pcm_runtime *rtd = substream->private_data;
239 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000240 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200241 unsigned int rate, channels, sample_bits, symmetry, i;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800242
243 rate = params_rate(params);
244 channels = params_channels(params);
245 sample_bits = snd_pcm_format_physical_width(params_format(params));
246
247 /* reject unmatched parameters when applying symmetry */
248 symmetry = cpu_dai->driver->symmetric_rates ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800249 rtd->dai_link->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200250
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000251 for_each_rtd_codec_dai(rtd, i, codec_dai)
252 symmetry |= codec_dai->driver->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200253
Nicolin Chen3635bf02013-11-13 18:56:24 +0800254 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
255 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
256 cpu_dai->rate, rate);
257 return -EINVAL;
258 }
259
260 symmetry = cpu_dai->driver->symmetric_channels ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800261 rtd->dai_link->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200262
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000263 for_each_rtd_codec_dai(rtd, i, codec_dai)
264 symmetry |= codec_dai->driver->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200265
Nicolin Chen3635bf02013-11-13 18:56:24 +0800266 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
267 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
268 cpu_dai->channels, channels);
269 return -EINVAL;
270 }
271
272 symmetry = cpu_dai->driver->symmetric_samplebits ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800273 rtd->dai_link->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200274
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000275 for_each_rtd_codec_dai(rtd, i, codec_dai)
276 symmetry |= codec_dai->driver->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200277
Nicolin Chen3635bf02013-11-13 18:56:24 +0800278 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
279 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
280 cpu_dai->sample_bits, sample_bits);
281 return -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100282 }
283
284 return 0;
285}
286
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100287static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
288{
289 struct snd_soc_pcm_runtime *rtd = substream->private_data;
290 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100291 struct snd_soc_dai_link *link = rtd->dai_link;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000292 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200293 unsigned int symmetry, i;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100294
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200295 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
296 cpu_driver->symmetric_channels || link->symmetric_channels ||
297 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
298
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000299 for_each_rtd_codec_dai(rtd, i, codec_dai)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200300 symmetry = symmetry ||
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000301 codec_dai->driver->symmetric_rates ||
302 codec_dai->driver->symmetric_channels ||
303 codec_dai->driver->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200304
305 return symmetry;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100306}
307
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200308static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000309{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200310 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Takashi Iwaic6068d32014-12-31 17:10:34 +0100311 int ret;
Mark Brown58ba9b22012-01-16 18:38:51 +0000312
313 if (!bits)
314 return;
315
Lars-Peter Clausen0e2a3752014-12-29 18:43:38 +0100316 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
317 if (ret != 0)
318 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
319 bits, ret);
Mark Brown58ba9b22012-01-16 18:38:51 +0000320}
321
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200322static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200323{
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200324 struct snd_soc_pcm_runtime *rtd = substream->private_data;
325 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200326 struct snd_soc_dai *codec_dai;
327 int i;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200328 unsigned int bits = 0, cpu_bits;
329
330 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000331 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200332 if (codec_dai->driver->playback.sig_bits == 0) {
333 bits = 0;
334 break;
335 }
336 bits = max(codec_dai->driver->playback.sig_bits, bits);
337 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200338 cpu_bits = cpu_dai->driver->playback.sig_bits;
339 } else {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000340 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Daniel Mack5e63dfc2014-10-07 14:33:46 +0200341 if (codec_dai->driver->capture.sig_bits == 0) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200342 bits = 0;
343 break;
344 }
345 bits = max(codec_dai->driver->capture.sig_bits, bits);
346 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200347 cpu_bits = cpu_dai->driver->capture.sig_bits;
348 }
349
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200350 soc_pcm_set_msb(substream, bits);
351 soc_pcm_set_msb(substream, cpu_bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200352}
353
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200354static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200355{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200356 struct snd_pcm_runtime *runtime = substream->runtime;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100357 struct snd_pcm_hardware *hw = &runtime->hw;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200358 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000359 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200360 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
361 struct snd_soc_dai_driver *codec_dai_drv;
362 struct snd_soc_pcm_stream *codec_stream;
363 struct snd_soc_pcm_stream *cpu_stream;
364 unsigned int chan_min = 0, chan_max = UINT_MAX;
365 unsigned int rate_min = 0, rate_max = UINT_MAX;
366 unsigned int rates = UINT_MAX;
367 u64 formats = ULLONG_MAX;
368 int i;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100369
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200370 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
371 cpu_stream = &cpu_dai_drv->playback;
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100372 else
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200373 cpu_stream = &cpu_dai_drv->capture;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100374
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200375 /* first calculate min/max only for CODECs in the DAI link */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000376 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200377
378 /*
379 * Skip CODECs which don't support the current stream type.
380 * Otherwise, since the rate, channel, and format values will
381 * zero in that case, we would have no usable settings left,
382 * causing the resulting setup to fail.
383 * At least one CODEC should match, otherwise we should have
384 * bailed out on a higher level, since there would be no
385 * CODEC to support the transfer direction in that case.
386 */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000387 if (!snd_soc_dai_stream_valid(codec_dai,
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200388 substream->stream))
389 continue;
390
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000391 codec_dai_drv = codec_dai->driver;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200392 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
393 codec_stream = &codec_dai_drv->playback;
394 else
395 codec_stream = &codec_dai_drv->capture;
396 chan_min = max(chan_min, codec_stream->channels_min);
397 chan_max = min(chan_max, codec_stream->channels_max);
398 rate_min = max(rate_min, codec_stream->rate_min);
399 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
400 formats &= codec_stream->formats;
401 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
402 }
403
404 /*
405 * chan min/max cannot be enforced if there are multiple CODEC DAIs
406 * connected to a single CPU DAI, use CPU DAI's directly and let
407 * channel allocation be fixed up later
408 */
409 if (rtd->num_codecs > 1) {
410 chan_min = cpu_stream->channels_min;
411 chan_max = cpu_stream->channels_max;
412 }
413
414 hw->channels_min = max(chan_min, cpu_stream->channels_min);
415 hw->channels_max = min(chan_max, cpu_stream->channels_max);
416 if (hw->formats)
417 hw->formats &= formats & cpu_stream->formats;
418 else
419 hw->formats = formats & cpu_stream->formats;
420 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100421
422 snd_pcm_limit_hw_rates(runtime);
423
424 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200425 hw->rate_min = max(hw->rate_min, rate_min);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100426 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200427 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200428}
429
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900430static int soc_pcm_components_open(struct snd_pcm_substream *substream,
431 struct snd_soc_component **last)
432{
433 struct snd_soc_pcm_runtime *rtd = substream->private_data;
434 struct snd_soc_rtdcom_list *rtdcom;
435 struct snd_soc_component *component;
436 int ret = 0;
437
438 for_each_rtdcom(rtd, rtdcom) {
439 component = rtdcom->component;
440 *last = component;
441
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900442 ret = snd_soc_component_module_get_when_open(component);
443 if (ret < 0) {
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900444 dev_err(component->dev,
445 "ASoC: can't get module %s\n",
446 component->name);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900447 return ret;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900448 }
449
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900450 ret = snd_soc_component_open(component, substream);
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900451 if (ret < 0) {
452 dev_err(component->dev,
453 "ASoC: can't open component %s: %d\n",
454 component->name, ret);
455 return ret;
456 }
457 }
458 *last = NULL;
459 return 0;
460}
461
Charles Keepax244e2932018-06-19 16:22:09 +0100462static int soc_pcm_components_close(struct snd_pcm_substream *substream,
463 struct snd_soc_component *last)
464{
465 struct snd_soc_pcm_runtime *rtd = substream->private_data;
466 struct snd_soc_rtdcom_list *rtdcom;
467 struct snd_soc_component *component;
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900468 int ret = 0;
Charles Keepax244e2932018-06-19 16:22:09 +0100469
470 for_each_rtdcom(rtd, rtdcom) {
471 component = rtdcom->component;
472
473 if (component == last)
474 break;
475
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900476 ret |= snd_soc_component_close(component, substream);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900477 snd_soc_component_module_put_when_close(component);
Charles Keepax244e2932018-06-19 16:22:09 +0100478 }
479
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900480 return ret;
Charles Keepax244e2932018-06-19 16:22:09 +0100481}
482
Mark Brown58ba9b22012-01-16 18:38:51 +0000483/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100484 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
485 * then initialized and any private data can be allocated. This also calls
Charles Keepaxef050be2018-04-24 16:39:02 +0100486 * startup for the cpu DAI, component, machine and codec DAI.
Liam Girdwoodddee6272011-06-09 14:45:53 +0100487 */
488static int soc_pcm_open(struct snd_pcm_substream *substream)
489{
490 struct snd_soc_pcm_runtime *rtd = substream->private_data;
491 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000492 struct snd_soc_component *component;
493 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100494 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200495 struct snd_soc_dai *codec_dai;
496 const char *codec_dai_name = "multicodec";
Charles Keepax244e2932018-06-19 16:22:09 +0100497 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100498
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800499 pinctrl_pm_select_default_state(cpu_dai->dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000500 for_each_rtd_codec_dai(rtd, i, codec_dai)
501 pinctrl_pm_select_default_state(codec_dai->dev);
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000502
503 for_each_rtdcom(rtd, rtdcom) {
504 component = rtdcom->component;
505
506 pm_runtime_get_sync(component->dev);
507 }
Mark Brownd6652ef2011-12-03 20:14:31 +0000508
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100509 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100510
511 /* startup the audio subsystem */
Kuninori Morimoto5a52a042019-07-22 10:33:32 +0900512 ret = snd_soc_dai_startup(cpu_dai, substream);
513 if (ret < 0) {
514 dev_err(cpu_dai->dev, "ASoC: can't open interface %s: %d\n",
515 cpu_dai->name, ret);
516 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100517 }
518
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900519 ret = soc_pcm_components_open(substream, &component);
520 if (ret < 0)
521 goto component_err;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000522
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000523 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto5a52a042019-07-22 10:33:32 +0900524 ret = snd_soc_dai_startup(codec_dai, substream);
525 if (ret < 0) {
526 dev_err(codec_dai->dev,
527 "ASoC: can't open codec %s: %d\n",
528 codec_dai->name, ret);
529 goto codec_dai_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100530 }
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200531
532 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
533 codec_dai->tx_mask = 0;
534 else
535 codec_dai->rx_mask = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100536 }
537
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000538 if (rtd->dai_link->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100539 ret = rtd->dai_link->ops->startup(substream);
540 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000541 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000542 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100543 goto machine_err;
544 }
545 }
546
Liam Girdwood01d75842012-04-25 12:12:49 +0100547 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
548 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
549 goto dynamic;
550
Liam Girdwoodddee6272011-06-09 14:45:53 +0100551 /* Check that the codec and cpu DAIs are compatible */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200552 soc_pcm_init_runtime_hw(substream);
553
554 if (rtd->num_codecs == 1)
555 codec_dai_name = rtd->codec_dai->name;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100556
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100557 if (soc_pcm_has_symmetry(substream))
558 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
559
Liam Girdwoodddee6272011-06-09 14:45:53 +0100560 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100561 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000562 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200563 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100564 goto config_err;
565 }
566 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000567 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200568 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100569 goto config_err;
570 }
571 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
572 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000573 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200574 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100575 goto config_err;
576 }
577
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200578 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000579
Liam Girdwoodddee6272011-06-09 14:45:53 +0100580 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800581 if (cpu_dai->active) {
582 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
583 if (ret != 0)
584 goto config_err;
585 }
586
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000587 for_each_rtd_codec_dai(rtd, i, codec_dai) {
588 if (codec_dai->active) {
589 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200590 if (ret != 0)
591 goto config_err;
592 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100593 }
594
Liam Girdwood103d84a2012-11-19 14:39:15 +0000595 pr_debug("ASoC: %s <-> %s info:\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200596 codec_dai_name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000597 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
598 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100599 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000600 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100601 runtime->hw.rate_max);
602
Liam Girdwood01d75842012-04-25 12:12:49 +0100603dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100604
605 snd_soc_runtime_activate(rtd, substream->stream);
606
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100607 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100608 return 0;
609
610config_err:
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000611 if (rtd->dai_link->ops->shutdown)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100612 rtd->dai_link->ops->shutdown(substream);
613
614machine_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200615 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100616
617codec_dai_err:
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900618 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai)
619 snd_soc_dai_shutdown(codec_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200620
Kuninori Morimotob8135862017-10-11 01:37:23 +0000621component_err:
Charles Keepax244e2932018-06-19 16:22:09 +0100622 soc_pcm_components_close(substream, component);
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900623
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900624 snd_soc_dai_shutdown(cpu_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100625out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100626 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000627
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000628 for_each_rtdcom(rtd, rtdcom) {
629 component = rtdcom->component;
630
631 pm_runtime_mark_last_busy(component->dev);
632 pm_runtime_put_autosuspend(component->dev);
Sanyog Kale3f809782016-01-05 17:14:49 +0530633 }
634
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000635 for_each_rtd_codec_dai(rtd, i, codec_dai) {
636 if (!codec_dai->active)
637 pinctrl_pm_select_sleep_state(codec_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200638 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800639 if (!cpu_dai->active)
640 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000641
Liam Girdwoodddee6272011-06-09 14:45:53 +0100642 return ret;
643}
644
645/*
646 * Power down the audio subsystem pmdown_time msecs after close is called.
647 * This is to ensure there are no pops or clicks in between any music tracks
648 * due to DAPM power cycling.
649 */
650static void close_delayed_work(struct work_struct *work)
651{
652 struct snd_soc_pcm_runtime *rtd =
653 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200654 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
Liam Girdwoodddee6272011-06-09 14:45:53 +0100655
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100656 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100657
Liam Girdwood103d84a2012-11-19 14:39:15 +0000658 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100659 codec_dai->driver->playback.stream_name,
660 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600661 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100662
663 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600664 if (rtd->pop_wait == 1) {
665 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800666 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000667 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100668 }
669
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100670 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100671}
672
Jerome Bruneta3420312019-07-25 18:59:47 +0200673static void codec2codec_close_delayed_work(struct work_struct *work)
674{
675 /*
676 * Currently nothing to do for c2c links
677 * Since c2c links are internal nodes in the DAPM graph and
678 * don't interface with the outside world or application layer
679 * we don't have to do any special handling on close.
680 */
681}
682
Liam Girdwoodddee6272011-06-09 14:45:53 +0100683/*
684 * Called by ALSA when a PCM substream is closed. Private data can be
Charles Keepaxef050be2018-04-24 16:39:02 +0100685 * freed here. The cpu DAI, codec DAI, machine and components are also
Liam Girdwoodddee6272011-06-09 14:45:53 +0100686 * shutdown.
687 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100688static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100689{
690 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000691 struct snd_soc_component *component;
692 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100693 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200694 struct snd_soc_dai *codec_dai;
695 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100696
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100697 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100698
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100699 snd_soc_runtime_deactivate(rtd, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100700
Dong Aisheng17841022011-08-29 17:15:14 +0800701 /* clear the corresponding DAIs rate when inactive */
702 if (!cpu_dai->active)
703 cpu_dai->rate = 0;
704
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000705 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200706 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
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900712 snd_soc_dai_shutdown(cpu_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100713
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900714 for_each_rtd_codec_dai(rtd, i, codec_dai)
715 snd_soc_dai_shutdown(codec_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100716
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000717 if (rtd->dai_link->ops->shutdown)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100718 rtd->dai_link->ops->shutdown(substream);
719
Charles Keepax244e2932018-06-19 16:22:09 +0100720 soc_pcm_components_close(substream, NULL);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000721
Liam Girdwoodddee6272011-06-09 14:45:53 +0100722 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100723 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300724 /* powered down playback stream now */
725 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800726 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800727 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300728 } else {
729 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600730 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100731 queue_delayed_work(system_power_efficient_wq,
732 &rtd->delayed_work,
733 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300734 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100735 } else {
736 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800737 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000738 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100739 }
740
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100741 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000742
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000743 for_each_rtdcom(rtd, rtdcom) {
744 component = rtdcom->component;
Sanyog Kale3f809782016-01-05 17:14:49 +0530745
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000746 pm_runtime_mark_last_busy(component->dev);
747 pm_runtime_put_autosuspend(component->dev);
Sanyog Kale3f809782016-01-05 17:14:49 +0530748 }
749
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000750 for_each_rtd_codec_dai(rtd, i, codec_dai) {
751 if (!codec_dai->active)
752 pinctrl_pm_select_sleep_state(codec_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200753 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800754 if (!cpu_dai->active)
755 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000756
Liam Girdwoodddee6272011-06-09 14:45:53 +0100757 return 0;
758}
759
760/*
761 * Called by ALSA when the PCM substream is prepared, can set format, sample
762 * rate, etc. This function is non atomic and can be called multiple times,
763 * it can refer to the runtime info.
764 */
765static int soc_pcm_prepare(struct snd_pcm_substream *substream)
766{
767 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000768 struct snd_soc_component *component;
769 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100770 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200771 struct snd_soc_dai *codec_dai;
772 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100773
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100774 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100775
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000776 if (rtd->dai_link->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100777 ret = rtd->dai_link->ops->prepare(substream);
778 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000779 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
780 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100781 goto out;
782 }
783 }
784
Kuninori Morimotob8135862017-10-11 01:37:23 +0000785 for_each_rtdcom(rtd, rtdcom) {
786 component = rtdcom->component;
787
Kuninori Morimoto6d537232019-07-26 13:50:13 +0900788 ret = snd_soc_component_prepare(component, substream);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000789 if (ret < 0) {
790 dev_err(component->dev,
791 "ASoC: platform prepare error: %d\n", ret);
792 goto out;
793 }
794 }
795
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000796 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto4beb8e12019-07-22 10:33:45 +0900797 ret = snd_soc_dai_prepare(codec_dai, substream);
798 if (ret < 0) {
799 dev_err(codec_dai->dev,
800 "ASoC: codec DAI prepare error: %d\n",
801 ret);
802 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100803 }
804 }
805
Kuninori Morimoto4beb8e12019-07-22 10:33:45 +0900806 ret = snd_soc_dai_prepare(cpu_dai, substream);
807 if (ret < 0) {
808 dev_err(cpu_dai->dev,
809 "ASoC: cpu DAI prepare error: %d\n", ret);
810 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100811 }
812
813 /* cancel any delayed stream shutdown that is pending */
814 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600815 rtd->pop_wait) {
816 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100817 cancel_delayed_work(&rtd->delayed_work);
818 }
819
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000820 snd_soc_dapm_stream_event(rtd, substream->stream,
821 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100822
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000823 for_each_rtd_codec_dai(rtd, i, codec_dai)
824 snd_soc_dai_digital_mute(codec_dai, 0,
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200825 substream->stream);
Ramesh Babuae116012014-10-15 12:34:59 +0530826 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100827
828out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100829 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100830 return ret;
831}
832
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200833static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
834 unsigned int mask)
835{
836 struct snd_interval *interval;
837 int channels = hweight_long(mask);
838
839 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
840 interval->min = channels;
841 interval->max = channels;
842}
843
Charles Keepax244e2932018-06-19 16:22:09 +0100844static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
845 struct snd_soc_component *last)
846{
847 struct snd_soc_pcm_runtime *rtd = substream->private_data;
848 struct snd_soc_rtdcom_list *rtdcom;
849 struct snd_soc_component *component;
850
851 for_each_rtdcom(rtd, rtdcom) {
852 component = rtdcom->component;
853
854 if (component == last)
855 break;
856
857 if (!component->driver->ops ||
858 !component->driver->ops->hw_free)
859 continue;
860
861 component->driver->ops->hw_free(substream);
862 }
863
864 return 0;
865}
866
Liam Girdwoodddee6272011-06-09 14:45:53 +0100867/*
868 * Called by ALSA when the hardware params are set by application. This
869 * function can also be called multiple times and can allocate buffers
870 * (using snd_pcm_lib_* ). It's non-atomic.
871 */
872static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
873 struct snd_pcm_hw_params *params)
874{
875 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000876 struct snd_soc_component *component;
877 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100878 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000879 struct snd_soc_dai *codec_dai;
Charles Keepax244e2932018-06-19 16:22:09 +0100880 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100881
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100882 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000883 if (rtd->dai_link->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100884 ret = rtd->dai_link->ops->hw_params(substream, params);
885 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000886 dev_err(rtd->card->dev, "ASoC: machine hw_params"
887 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100888 goto out;
889 }
890 }
891
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000892 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200893 struct snd_pcm_hw_params codec_params;
894
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200895 /*
896 * Skip CODECs which don't support the current stream type,
897 * the idea being that if a CODEC is not used for the currently
898 * set up transfer direction, it should not need to be
899 * configured, especially since the configuration used might
900 * not even be supported by that CODEC. There may be cases
901 * however where a CODEC needs to be set up although it is
902 * actually not being used for the transfer, e.g. if a
903 * capture-only CODEC is acting as an LRCLK and/or BCLK master
904 * for the DAI link including a playback-only CODEC.
905 * If this becomes necessary, we will have to augment the
906 * machine driver setup with information on how to act, so
907 * we can do the right thing here.
908 */
909 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
910 continue;
911
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200912 /* copy params for each codec */
913 codec_params = *params;
914
915 /* fixup params based on TDM slot masks */
Rander Wang570f18b2019-03-08 16:38:57 +0800916 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
917 codec_dai->tx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200918 soc_pcm_codec_params_fixup(&codec_params,
919 codec_dai->tx_mask);
Rander Wang570f18b2019-03-08 16:38:57 +0800920
921 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
922 codec_dai->rx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200923 soc_pcm_codec_params_fixup(&codec_params,
924 codec_dai->rx_mask);
925
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +0900926 ret = snd_soc_dai_hw_params(codec_dai, substream,
927 &codec_params);
Benoit Cousson93e69582014-07-08 23:19:38 +0200928 if(ret < 0)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100929 goto codec_err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200930
931 codec_dai->rate = params_rate(&codec_params);
932 codec_dai->channels = params_channels(&codec_params);
933 codec_dai->sample_bits = snd_pcm_format_physical_width(
934 params_format(&codec_params));
Charles Keepax078a85f2019-01-31 13:30:18 +0000935
936 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100937 }
938
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +0900939 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
Benoit Cousson93e69582014-07-08 23:19:38 +0200940 if (ret < 0)
941 goto interface_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100942
Kuninori Morimotoca58221d2019-05-13 16:07:43 +0900943 /* store the parameters for each DAIs */
944 cpu_dai->rate = params_rate(params);
945 cpu_dai->channels = params_channels(params);
946 cpu_dai->sample_bits =
947 snd_pcm_format_physical_width(params_format(params));
948
949 snd_soc_dapm_update_dai(substream, params, cpu_dai);
950
Kuninori Morimotob8135862017-10-11 01:37:23 +0000951 for_each_rtdcom(rtd, rtdcom) {
952 component = rtdcom->component;
953
Kuninori Morimoto245c5392019-07-26 13:50:19 +0900954 ret = snd_soc_component_hw_params(component, substream, params);
Charles Keepax244e2932018-06-19 16:22:09 +0100955 if (ret < 0) {
Kuninori Morimotob8135862017-10-11 01:37:23 +0000956 dev_err(component->dev,
957 "ASoC: %s hw params failed: %d\n",
Charles Keepax244e2932018-06-19 16:22:09 +0100958 component->name, ret);
959 goto component_err;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000960 }
961 }
Charles Keepax244e2932018-06-19 16:22:09 +0100962 component = NULL;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000963
jiada wang957ce0c2017-09-20 15:25:30 +0900964 ret = soc_pcm_params_symmetry(substream, params);
965 if (ret)
Kuninori Morimotob8135862017-10-11 01:37:23 +0000966 goto component_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100967out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100968 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100969 return ret;
970
Kuninori Morimotob8135862017-10-11 01:37:23 +0000971component_err:
Charles Keepax244e2932018-06-19 16:22:09 +0100972 soc_pcm_components_hw_free(substream, component);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000973
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900974 snd_soc_dai_hw_free(cpu_dai, substream);
Kuninori Morimoto2371abd2019-05-13 16:07:52 +0900975 cpu_dai->rate = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100976
977interface_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200978 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100979
980codec_err:
Kuninori Morimoto6d11b122018-09-18 01:28:30 +0000981 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
Jerome Brunetf47b9ad2019-04-29 11:47:50 +0200982 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
983 continue;
984
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900985 snd_soc_dai_hw_free(codec_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200986 codec_dai->rate = 0;
987 }
988
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000989 if (rtd->dai_link->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100990 rtd->dai_link->ops->hw_free(substream);
991
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100992 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100993 return ret;
994}
995
996/*
997 * Frees resources allocated by hw_params, can be called multiple times
998 */
999static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1000{
1001 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001002 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001003 struct snd_soc_dai *codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +08001004 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001005 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001006
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001007 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001008
Nicolin Chend3383422013-11-20 18:37:09 +08001009 /* clear the corresponding DAIs parameters when going to be inactive */
1010 if (cpu_dai->active == 1) {
1011 cpu_dai->rate = 0;
1012 cpu_dai->channels = 0;
1013 cpu_dai->sample_bits = 0;
1014 }
1015
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001016 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001017 if (codec_dai->active == 1) {
1018 codec_dai->rate = 0;
1019 codec_dai->channels = 0;
1020 codec_dai->sample_bits = 0;
1021 }
Nicolin Chend3383422013-11-20 18:37:09 +08001022 }
1023
Liam Girdwoodddee6272011-06-09 14:45:53 +01001024 /* apply codec digital mute */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001025 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1026 if ((playback && codec_dai->playback_active == 1) ||
1027 (!playback && codec_dai->capture_active == 1))
1028 snd_soc_dai_digital_mute(codec_dai, 1,
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001029 substream->stream);
1030 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001031
1032 /* free any machine hw params */
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +00001033 if (rtd->dai_link->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001034 rtd->dai_link->ops->hw_free(substream);
1035
Kuninori Morimotob8135862017-10-11 01:37:23 +00001036 /* free any component resources */
Charles Keepax244e2932018-06-19 16:22:09 +01001037 soc_pcm_components_hw_free(substream, NULL);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001038
Liam Girdwoodddee6272011-06-09 14:45:53 +01001039 /* now free hw params for the DAIs */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001040 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Jerome Brunetf47b9ad2019-04-29 11:47:50 +02001041 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1042 continue;
1043
Kuninori Morimoto846faae2019-07-22 10:33:19 +09001044 snd_soc_dai_hw_free(codec_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001045 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001046
Kuninori Morimoto846faae2019-07-22 10:33:19 +09001047 snd_soc_dai_hw_free(cpu_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001048
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001049 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001050 return 0;
1051}
1052
1053static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1054{
1055 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001056 struct snd_soc_component *component;
1057 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001058 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001059 struct snd_soc_dai *codec_dai;
1060 int i, ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001061
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001062 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto95aef352019-07-22 10:33:51 +09001063 ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
1064 if (ret < 0)
1065 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001066 }
1067
Kuninori Morimotob8135862017-10-11 01:37:23 +00001068 for_each_rtdcom(rtd, rtdcom) {
1069 component = rtdcom->component;
1070
Kuninori Morimotob8135862017-10-11 01:37:23 +00001071 if (!component->driver->ops ||
1072 !component->driver->ops->trigger)
1073 continue;
1074
1075 ret = component->driver->ops->trigger(substream, cmd);
1076 if (ret < 0)
1077 return ret;
1078 }
1079
Kuninori Morimoto95aef352019-07-22 10:33:51 +09001080 snd_soc_dai_trigger(cpu_dai, substream, cmd);
1081 if (ret < 0)
1082 return ret;
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001083
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +00001084 if (rtd->dai_link->ops->trigger) {
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001085 ret = rtd->dai_link->ops->trigger(substream, cmd);
1086 if (ret < 0)
1087 return ret;
1088 }
1089
Liam Girdwoodddee6272011-06-09 14:45:53 +01001090 return 0;
1091}
1092
Mark Brown45c0a182012-05-09 21:46:27 +01001093static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1094 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001095{
1096 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001097 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001098 struct snd_soc_dai *codec_dai;
1099 int i, ret;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001100
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001101 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto5c0769af2019-07-22 10:33:56 +09001102 ret = snd_soc_dai_bespoke_trigger(codec_dai, substream, cmd);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001103 if (ret < 0)
1104 return ret;
1105 }
Kuninori Morimoto5c0769af2019-07-22 10:33:56 +09001106
1107 snd_soc_dai_bespoke_trigger(cpu_dai, substream, cmd);
1108 if (ret < 0)
1109 return ret;
1110
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001111 return 0;
1112}
Liam Girdwoodddee6272011-06-09 14:45:53 +01001113/*
1114 * soc level wrapper for pointer callback
Charles Keepaxef050be2018-04-24 16:39:02 +01001115 * If cpu_dai, codec_dai, component driver has the delay callback, then
Liam Girdwoodddee6272011-06-09 14:45:53 +01001116 * the runtime->delay will be updated accordingly.
1117 */
1118static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1119{
1120 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001121 struct snd_soc_component *component;
1122 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001123 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001124 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001125 struct snd_pcm_runtime *runtime = substream->runtime;
1126 snd_pcm_uframes_t offset = 0;
1127 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001128 snd_pcm_sframes_t codec_delay = 0;
1129 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001130
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301131 /* clearing the previous total delay */
1132 runtime->delay = 0;
1133
Kuninori Morimotob8135862017-10-11 01:37:23 +00001134 for_each_rtdcom(rtd, rtdcom) {
1135 component = rtdcom->component;
1136
Kuninori Morimotob8135862017-10-11 01:37:23 +00001137 if (!component->driver->ops ||
1138 !component->driver->ops->pointer)
1139 continue;
1140
1141 /* FIXME: use 1st pointer */
1142 offset = component->driver->ops->pointer(substream);
1143 break;
1144 }
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301145 /* base delay if assigned in pointer callback */
1146 delay = runtime->delay;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001147
Kuninori Morimoto1dea80d2019-07-22 10:34:09 +09001148 delay += snd_soc_dai_delay(cpu_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001149
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001150 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto1dea80d2019-07-22 10:34:09 +09001151 codec_delay = max(codec_delay,
1152 snd_soc_dai_delay(codec_dai, substream));
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001153 }
1154 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001155
Liam Girdwoodddee6272011-06-09 14:45:53 +01001156 runtime->delay = delay;
1157
1158 return offset;
1159}
1160
Liam Girdwood01d75842012-04-25 12:12:49 +01001161/* connect a FE and BE */
1162static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1163 struct snd_soc_pcm_runtime *be, int stream)
1164{
1165 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001166 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001167
1168 /* only add new dpcms */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001169 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001170 if (dpcm->be == be && dpcm->fe == fe)
1171 return 0;
1172 }
1173
1174 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1175 if (!dpcm)
1176 return -ENOMEM;
1177
1178 dpcm->be = be;
1179 dpcm->fe = fe;
1180 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1181 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001182 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001183 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1184 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001185 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001186
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001187 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001188 stream ? "capture" : "playback", fe->dai_link->name,
1189 stream ? "<-" : "->", be->dai_link->name);
1190
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001191#ifdef CONFIG_DEBUG_FS
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +02001192 dpcm->debugfs_state = debugfs_create_dir(be->dai_link->name,
1193 fe->debugfs_dpcm_root);
1194 debugfs_create_u32("state", 0644, dpcm->debugfs_state, &dpcm->state);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001195#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001196 return 1;
1197}
1198
1199/* reparent a BE onto another FE */
1200static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1201 struct snd_soc_pcm_runtime *be, int stream)
1202{
1203 struct snd_soc_dpcm *dpcm;
1204 struct snd_pcm_substream *fe_substream, *be_substream;
1205
1206 /* reparent if BE is connected to other FEs */
1207 if (!be->dpcm[stream].users)
1208 return;
1209
1210 be_substream = snd_soc_dpcm_get_substream(be, stream);
1211
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00001212 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001213 if (dpcm->fe == fe)
1214 continue;
1215
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001216 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001217 stream ? "capture" : "playback",
1218 dpcm->fe->dai_link->name,
1219 stream ? "<-" : "->", dpcm->be->dai_link->name);
1220
1221 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1222 be_substream->runtime = fe_substream->runtime;
1223 break;
1224 }
1225}
1226
1227/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001228void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001229{
1230 struct snd_soc_dpcm *dpcm, *d;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001231 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001232
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001233 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001234 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001235 stream ? "capture" : "playback",
1236 dpcm->be->dai_link->name);
1237
1238 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1239 continue;
1240
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001241 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001242 stream ? "capture" : "playback", fe->dai_link->name,
1243 stream ? "<-" : "->", dpcm->be->dai_link->name);
1244
1245 /* BEs still alive need new FE */
1246 dpcm_be_reparent(fe, dpcm->be, stream);
1247
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001248#ifdef CONFIG_DEBUG_FS
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +02001249 debugfs_remove_recursive(dpcm->debugfs_state);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001250#endif
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001251 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001252 list_del(&dpcm->list_be);
1253 list_del(&dpcm->list_fe);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001254 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001255 kfree(dpcm);
1256 }
1257}
1258
1259/* get BE for DAI widget and stream */
1260static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1261 struct snd_soc_dapm_widget *widget, int stream)
1262{
1263 struct snd_soc_pcm_runtime *be;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001264 struct snd_soc_dai *dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05001265 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001266
Liam Girdwood3c146462018-03-14 20:43:51 +00001267 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1268
Liam Girdwood01d75842012-04-25 12:12:49 +01001269 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001270 for_each_card_rtds(card, be) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001271
Liam Girdwood35ea0652012-06-05 19:26:59 +01001272 if (!be->dai_link->no_pcm)
1273 continue;
1274
Liam Girdwood3c146462018-03-14 20:43:51 +00001275 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1276 be->cpu_dai->playback_widget ?
1277 be->cpu_dai->playback_widget->name : "(not set)");
1278
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001279 if (be->cpu_dai->playback_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001280 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001281
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001282 for_each_rtd_codec_dai(be, i, dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001283 if (dai->playback_widget == widget)
1284 return be;
1285 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001286 }
1287 } else {
1288
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001289 for_each_card_rtds(card, be) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001290
Liam Girdwood35ea0652012-06-05 19:26:59 +01001291 if (!be->dai_link->no_pcm)
1292 continue;
1293
Liam Girdwood3c146462018-03-14 20:43:51 +00001294 dev_dbg(card->dev, "ASoC: try BE %s\n",
1295 be->cpu_dai->capture_widget ?
1296 be->cpu_dai->capture_widget->name : "(not set)");
1297
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001298 if (be->cpu_dai->capture_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001299 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001300
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001301 for_each_rtd_codec_dai(be, i, dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001302 if (dai->capture_widget == widget)
1303 return be;
1304 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001305 }
1306 }
1307
Liam Girdwood3c146462018-03-14 20:43:51 +00001308 /* dai link name and stream name set correctly ? */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001309 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001310 stream ? "capture" : "playback", widget->name);
1311 return NULL;
1312}
1313
1314static inline struct snd_soc_dapm_widget *
Benoit Cousson37018612014-04-24 14:01:45 +02001315 dai_get_widget(struct snd_soc_dai *dai, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001316{
1317 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Benoit Cousson37018612014-04-24 14:01:45 +02001318 return dai->playback_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001319 else
Benoit Cousson37018612014-04-24 14:01:45 +02001320 return dai->capture_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001321}
1322
1323static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1324 struct snd_soc_dapm_widget *widget)
1325{
1326 int i;
1327
1328 for (i = 0; i < list->num_widgets; i++) {
1329 if (widget == list->widgets[i])
1330 return 1;
1331 }
1332
1333 return 0;
1334}
1335
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001336static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1337 enum snd_soc_dapm_direction dir)
1338{
1339 struct snd_soc_card *card = widget->dapm->card;
1340 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001341 struct snd_soc_dai *dai;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001342 int i;
1343
1344 if (dir == SND_SOC_DAPM_DIR_OUT) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001345 for_each_card_rtds(card, rtd) {
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001346 if (!rtd->dai_link->no_pcm)
1347 continue;
1348
1349 if (rtd->cpu_dai->playback_widget == widget)
1350 return true;
1351
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001352 for_each_rtd_codec_dai(rtd, i, dai) {
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001353 if (dai->playback_widget == widget)
1354 return true;
1355 }
1356 }
1357 } else { /* SND_SOC_DAPM_DIR_IN */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001358 for_each_card_rtds(card, rtd) {
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001359 if (!rtd->dai_link->no_pcm)
1360 continue;
1361
1362 if (rtd->cpu_dai->capture_widget == widget)
1363 return true;
1364
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001365 for_each_rtd_codec_dai(rtd, i, dai) {
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001366 if (dai->capture_widget == widget)
1367 return true;
1368 }
1369 }
1370 }
1371
1372 return false;
1373}
1374
Liam Girdwood23607022014-01-17 17:03:55 +00001375int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001376 int stream, struct snd_soc_dapm_widget_list **list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001377{
1378 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001379 int paths;
1380
Liam Girdwood01d75842012-04-25 12:12:49 +01001381 /* get number of valid DAI paths and their widgets */
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001382 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001383 dpcm_end_walk_at_be);
Liam Girdwood01d75842012-04-25 12:12:49 +01001384
Liam Girdwood103d84a2012-11-19 14:39:15 +00001385 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001386 stream ? "capture" : "playback");
1387
Liam Girdwood01d75842012-04-25 12:12:49 +01001388 return paths;
1389}
1390
Liam Girdwood01d75842012-04-25 12:12:49 +01001391static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1392 struct snd_soc_dapm_widget_list **list_)
1393{
1394 struct snd_soc_dpcm *dpcm;
1395 struct snd_soc_dapm_widget_list *list = *list_;
1396 struct snd_soc_dapm_widget *widget;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001397 struct snd_soc_dai *dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001398 int prune = 0;
1399
1400 /* Destroy any old FE <--> BE connections */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001401 for_each_dpcm_be(fe, stream, dpcm) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001402 unsigned int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001403
1404 /* is there a valid CPU DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001405 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001406
1407 /* prune the BE if it's no longer in our active list */
1408 if (widget && widget_in_list(list, widget))
1409 continue;
1410
1411 /* is there a valid CODEC DAI widget for this BE */
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001412 for_each_rtd_codec_dai(dpcm->be, i, dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001413 widget = dai_get_widget(dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001414
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001415 /* prune the BE if it's no longer in our active list */
1416 if (widget && widget_in_list(list, widget))
1417 continue;
1418 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001419
Liam Girdwood103d84a2012-11-19 14:39:15 +00001420 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001421 stream ? "capture" : "playback",
1422 dpcm->be->dai_link->name, fe->dai_link->name);
1423 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1424 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1425 prune++;
1426 }
1427
Liam Girdwood103d84a2012-11-19 14:39:15 +00001428 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001429 return prune;
1430}
1431
1432static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1433 struct snd_soc_dapm_widget_list **list_)
1434{
1435 struct snd_soc_card *card = fe->card;
1436 struct snd_soc_dapm_widget_list *list = *list_;
1437 struct snd_soc_pcm_runtime *be;
1438 int i, new = 0, err;
1439
1440 /* Create any new FE <--> BE connections */
1441 for (i = 0; i < list->num_widgets; i++) {
1442
Mark Brown46162742013-06-05 19:36:11 +01001443 switch (list->widgets[i]->id) {
1444 case snd_soc_dapm_dai_in:
Koro Chenc5b85402015-07-06 10:02:10 +08001445 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1446 continue;
1447 break;
Mark Brown46162742013-06-05 19:36:11 +01001448 case snd_soc_dapm_dai_out:
Koro Chenc5b85402015-07-06 10:02:10 +08001449 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1450 continue;
Mark Brown46162742013-06-05 19:36:11 +01001451 break;
1452 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001453 continue;
Mark Brown46162742013-06-05 19:36:11 +01001454 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001455
1456 /* is there a valid BE rtd for this widget */
1457 be = dpcm_get_be(card, list->widgets[i], stream);
1458 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001459 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001460 list->widgets[i]->name);
1461 continue;
1462 }
1463
1464 /* make sure BE is a real BE */
1465 if (!be->dai_link->no_pcm)
1466 continue;
1467
1468 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001469 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001470 continue;
1471
1472 /* newly connected FE and BE */
1473 err = dpcm_be_connect(fe, be, stream);
1474 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001475 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001476 list->widgets[i]->name);
1477 break;
1478 } else if (err == 0) /* already connected */
1479 continue;
1480
1481 /* new */
1482 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1483 new++;
1484 }
1485
Liam Girdwood103d84a2012-11-19 14:39:15 +00001486 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001487 return new;
1488}
1489
1490/*
1491 * Find the corresponding BE DAIs that source or sink audio to this
1492 * FE substream.
1493 */
Liam Girdwood23607022014-01-17 17:03:55 +00001494int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001495 int stream, struct snd_soc_dapm_widget_list **list, int new)
1496{
1497 if (new)
1498 return dpcm_add_paths(fe, stream, list);
1499 else
1500 return dpcm_prune_paths(fe, stream, list);
1501}
1502
Liam Girdwood23607022014-01-17 17:03:55 +00001503void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001504{
1505 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001506 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001507
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001508 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001509 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood01d75842012-04-25 12:12:49 +01001510 dpcm->be->dpcm[stream].runtime_update =
1511 SND_SOC_DPCM_UPDATE_NO;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001512 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001513}
1514
1515static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1516 int stream)
1517{
1518 struct snd_soc_dpcm *dpcm;
1519
1520 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001521 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001522
1523 struct snd_soc_pcm_runtime *be = dpcm->be;
1524 struct snd_pcm_substream *be_substream =
1525 snd_soc_dpcm_get_substream(be, stream);
1526
1527 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001528 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001529 stream ? "capture" : "playback",
1530 be->dpcm[stream].state);
1531
1532 if (--be->dpcm[stream].users != 0)
1533 continue;
1534
1535 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1536 continue;
1537
1538 soc_pcm_close(be_substream);
1539 be_substream->runtime = NULL;
1540 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1541 }
1542}
1543
Liam Girdwood23607022014-01-17 17:03:55 +00001544int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001545{
1546 struct snd_soc_dpcm *dpcm;
1547 int err, count = 0;
1548
1549 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001550 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001551
1552 struct snd_soc_pcm_runtime *be = dpcm->be;
1553 struct snd_pcm_substream *be_substream =
1554 snd_soc_dpcm_get_substream(be, stream);
1555
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001556 if (!be_substream) {
1557 dev_err(be->dev, "ASoC: no backend %s stream\n",
1558 stream ? "capture" : "playback");
1559 continue;
1560 }
1561
Liam Girdwood01d75842012-04-25 12:12:49 +01001562 /* is this op for this BE ? */
1563 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1564 continue;
1565
1566 /* first time the dpcm is open ? */
1567 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001568 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001569 stream ? "capture" : "playback",
1570 be->dpcm[stream].state);
1571
1572 if (be->dpcm[stream].users++ != 0)
1573 continue;
1574
1575 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1576 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1577 continue;
1578
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001579 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1580 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001581
1582 be_substream->runtime = be->dpcm[stream].runtime;
1583 err = soc_pcm_open(be_substream);
1584 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001585 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001586 be->dpcm[stream].users--;
1587 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001588 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001589 stream ? "capture" : "playback",
1590 be->dpcm[stream].state);
1591
1592 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1593 goto unwind;
1594 }
1595
1596 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1597 count++;
1598 }
1599
1600 return count;
1601
1602unwind:
1603 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001604 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001605 struct snd_soc_pcm_runtime *be = dpcm->be;
1606 struct snd_pcm_substream *be_substream =
1607 snd_soc_dpcm_get_substream(be, stream);
1608
1609 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1610 continue;
1611
1612 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001613 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001614 stream ? "capture" : "playback",
1615 be->dpcm[stream].state);
1616
1617 if (--be->dpcm[stream].users != 0)
1618 continue;
1619
1620 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1621 continue;
1622
1623 soc_pcm_close(be_substream);
1624 be_substream->runtime = NULL;
1625 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1626 }
1627
1628 return err;
1629}
1630
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001631static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Jerome Brunet435ffb72018-07-05 12:13:48 +02001632 struct snd_soc_pcm_stream *stream)
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001633{
1634 runtime->hw.rate_min = stream->rate_min;
Charles Keepaxe33ffbd9c2018-08-27 14:26:47 +01001635 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001636 runtime->hw.channels_min = stream->channels_min;
1637 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001638 if (runtime->hw.formats)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001639 runtime->hw.formats &= stream->formats;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001640 else
Jerome Brunet435ffb72018-07-05 12:13:48 +02001641 runtime->hw.formats = stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001642 runtime->hw.rates = stream->rates;
1643}
1644
Jerome Brunet435ffb72018-07-05 12:13:48 +02001645static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1646 u64 *formats)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001647{
1648 struct snd_soc_pcm_runtime *fe = substream->private_data;
1649 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001650 struct snd_soc_dai *dai;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001651 int stream = substream->stream;
1652
1653 if (!fe->dai_link->dpcm_merged_format)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001654 return;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001655
1656 /*
1657 * It returns merged BE codec format
1658 * if FE want to use it (= dpcm_merged_format)
1659 */
1660
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001661 for_each_dpcm_be(fe, stream, dpcm) {
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001662 struct snd_soc_pcm_runtime *be = dpcm->be;
1663 struct snd_soc_dai_driver *codec_dai_drv;
1664 struct snd_soc_pcm_stream *codec_stream;
1665 int i;
1666
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001667 for_each_rtd_codec_dai(be, i, dai) {
Jerome Brunet4febced2018-06-27 17:36:38 +02001668 /*
1669 * Skip CODECs which don't support the current stream
1670 * type. See soc_pcm_init_runtime_hw() for more details
1671 */
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001672 if (!snd_soc_dai_stream_valid(dai, stream))
Jerome Brunet4febced2018-06-27 17:36:38 +02001673 continue;
1674
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001675 codec_dai_drv = dai->driver;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001676 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1677 codec_stream = &codec_dai_drv->playback;
1678 else
1679 codec_stream = &codec_dai_drv->capture;
1680
Jerome Brunet435ffb72018-07-05 12:13:48 +02001681 *formats &= codec_stream->formats;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001682 }
1683 }
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001684}
1685
Jerome Brunet435ffb72018-07-05 12:13:48 +02001686static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1687 unsigned int *channels_min,
1688 unsigned int *channels_max)
Jiada Wangf4c277b2018-06-20 18:25:20 +09001689{
1690 struct snd_soc_pcm_runtime *fe = substream->private_data;
1691 struct snd_soc_dpcm *dpcm;
1692 int stream = substream->stream;
1693
1694 if (!fe->dai_link->dpcm_merged_chan)
1695 return;
1696
Jiada Wangf4c277b2018-06-20 18:25:20 +09001697 /*
1698 * It returns merged BE codec channel;
1699 * if FE want to use it (= dpcm_merged_chan)
1700 */
1701
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001702 for_each_dpcm_be(fe, stream, dpcm) {
Jiada Wangf4c277b2018-06-20 18:25:20 +09001703 struct snd_soc_pcm_runtime *be = dpcm->be;
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001704 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001705 struct snd_soc_dai_driver *codec_dai_drv;
1706 struct snd_soc_pcm_stream *codec_stream;
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001707 struct snd_soc_pcm_stream *cpu_stream;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001708
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001709 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1710 cpu_stream = &cpu_dai_drv->playback;
1711 else
1712 cpu_stream = &cpu_dai_drv->capture;
1713
1714 *channels_min = max(*channels_min, cpu_stream->channels_min);
1715 *channels_max = min(*channels_max, cpu_stream->channels_max);
1716
1717 /*
1718 * chan min/max cannot be enforced if there are multiple CODEC
1719 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1720 */
1721 if (be->num_codecs == 1) {
1722 codec_dai_drv = be->codec_dais[0]->driver;
1723
Jiada Wangf4c277b2018-06-20 18:25:20 +09001724 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1725 codec_stream = &codec_dai_drv->playback;
1726 else
1727 codec_stream = &codec_dai_drv->capture;
1728
1729 *channels_min = max(*channels_min,
1730 codec_stream->channels_min);
1731 *channels_max = min(*channels_max,
1732 codec_stream->channels_max);
1733 }
1734 }
1735}
1736
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001737static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1738 unsigned int *rates,
1739 unsigned int *rate_min,
1740 unsigned int *rate_max)
1741{
1742 struct snd_soc_pcm_runtime *fe = substream->private_data;
1743 struct snd_soc_dpcm *dpcm;
1744 int stream = substream->stream;
1745
1746 if (!fe->dai_link->dpcm_merged_rate)
1747 return;
1748
1749 /*
1750 * It returns merged BE codec channel;
1751 * if FE want to use it (= dpcm_merged_chan)
1752 */
1753
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001754 for_each_dpcm_be(fe, stream, dpcm) {
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001755 struct snd_soc_pcm_runtime *be = dpcm->be;
1756 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1757 struct snd_soc_dai_driver *codec_dai_drv;
1758 struct snd_soc_pcm_stream *codec_stream;
1759 struct snd_soc_pcm_stream *cpu_stream;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001760 struct snd_soc_dai *dai;
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001761 int i;
1762
1763 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1764 cpu_stream = &cpu_dai_drv->playback;
1765 else
1766 cpu_stream = &cpu_dai_drv->capture;
1767
1768 *rate_min = max(*rate_min, cpu_stream->rate_min);
1769 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1770 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1771
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001772 for_each_rtd_codec_dai(be, i, dai) {
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001773 /*
1774 * Skip CODECs which don't support the current stream
1775 * type. See soc_pcm_init_runtime_hw() for more details
1776 */
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001777 if (!snd_soc_dai_stream_valid(dai, stream))
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001778 continue;
1779
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001780 codec_dai_drv = dai->driver;
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001781 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1782 codec_stream = &codec_dai_drv->playback;
1783 else
1784 codec_stream = &codec_dai_drv->capture;
1785
1786 *rate_min = max(*rate_min, codec_stream->rate_min);
1787 *rate_max = min_not_zero(*rate_max,
1788 codec_stream->rate_max);
1789 *rates = snd_pcm_rate_mask_intersect(*rates,
1790 codec_stream->rates);
1791 }
1792 }
1793}
1794
Mark Brown45c0a182012-05-09 21:46:27 +01001795static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001796{
1797 struct snd_pcm_runtime *runtime = substream->runtime;
1798 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1799 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1800 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1801
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001802 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001803 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001804 else
Jerome Brunet435ffb72018-07-05 12:13:48 +02001805 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
Jiada Wangf4c277b2018-06-20 18:25:20 +09001806
Jerome Brunet435ffb72018-07-05 12:13:48 +02001807 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1808 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1809 &runtime->hw.channels_max);
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001810 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1811 &runtime->hw.rate_min, &runtime->hw.rate_max);
Liam Girdwood01d75842012-04-25 12:12:49 +01001812}
1813
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001814static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1815
1816/* Set FE's runtime_update state; the state is protected via PCM stream lock
1817 * for avoiding the race with trigger callback.
1818 * If the state is unset and a trigger is pending while the previous operation,
1819 * process the pending trigger action here.
1820 */
1821static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1822 int stream, enum snd_soc_dpcm_update state)
1823{
1824 struct snd_pcm_substream *substream =
1825 snd_soc_dpcm_get_substream(fe, stream);
1826
1827 snd_pcm_stream_lock_irq(substream);
1828 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1829 dpcm_fe_dai_do_trigger(substream,
1830 fe->dpcm[stream].trigger_pending - 1);
1831 fe->dpcm[stream].trigger_pending = 0;
1832 }
1833 fe->dpcm[stream].runtime_update = state;
1834 snd_pcm_stream_unlock_irq(substream);
1835}
1836
PC Liao906c7d62015-12-11 11:33:51 +08001837static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1838 int stream)
1839{
1840 struct snd_soc_dpcm *dpcm;
1841 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1842 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1843 int err;
1844
1845 /* apply symmetry for FE */
1846 if (soc_pcm_has_symmetry(fe_substream))
1847 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1848
1849 /* Symmetry only applies if we've got an active stream. */
1850 if (fe_cpu_dai->active) {
1851 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1852 if (err < 0)
1853 return err;
1854 }
1855
1856 /* apply symmetry for BE */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001857 for_each_dpcm_be(fe, stream, dpcm) {
PC Liao906c7d62015-12-11 11:33:51 +08001858 struct snd_soc_pcm_runtime *be = dpcm->be;
1859 struct snd_pcm_substream *be_substream =
1860 snd_soc_dpcm_get_substream(be, stream);
Jerome Brunet6246f282019-04-01 15:03:54 +02001861 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001862 struct snd_soc_dai *codec_dai;
PC Liao906c7d62015-12-11 11:33:51 +08001863 int i;
1864
Jerome Brunet6246f282019-04-01 15:03:54 +02001865 /* A backend may not have the requested substream */
1866 if (!be_substream)
1867 continue;
1868
1869 rtd = be_substream->private_data;
Jeeja KPf1176612016-09-06 14:17:55 +05301870 if (rtd->dai_link->be_hw_params_fixup)
1871 continue;
1872
PC Liao906c7d62015-12-11 11:33:51 +08001873 if (soc_pcm_has_symmetry(be_substream))
1874 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1875
1876 /* Symmetry only applies if we've got an active stream. */
1877 if (rtd->cpu_dai->active) {
Kai Chieh Chuang99bcedb2018-05-28 10:18:19 +08001878 err = soc_pcm_apply_symmetry(fe_substream,
1879 rtd->cpu_dai);
PC Liao906c7d62015-12-11 11:33:51 +08001880 if (err < 0)
1881 return err;
1882 }
1883
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001884 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1885 if (codec_dai->active) {
Kai Chieh Chuang99bcedb2018-05-28 10:18:19 +08001886 err = soc_pcm_apply_symmetry(fe_substream,
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001887 codec_dai);
PC Liao906c7d62015-12-11 11:33:51 +08001888 if (err < 0)
1889 return err;
1890 }
1891 }
1892 }
1893
1894 return 0;
1895}
1896
Liam Girdwood01d75842012-04-25 12:12:49 +01001897static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1898{
1899 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1900 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1901 int stream = fe_substream->stream, ret = 0;
1902
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001903 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001904
1905 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1906 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001907 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001908 goto be_err;
1909 }
1910
Liam Girdwood103d84a2012-11-19 14:39:15 +00001911 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001912
1913 /* start the DAI frontend */
1914 ret = soc_pcm_open(fe_substream);
1915 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001916 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001917 goto unwind;
1918 }
1919
1920 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1921
1922 dpcm_set_fe_runtime(fe_substream);
1923 snd_pcm_limit_hw_rates(runtime);
1924
PC Liao906c7d62015-12-11 11:33:51 +08001925 ret = dpcm_apply_symmetry(fe_substream, stream);
1926 if (ret < 0) {
1927 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1928 ret);
1929 goto unwind;
1930 }
1931
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001932 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001933 return 0;
1934
1935unwind:
1936 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1937be_err:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001938 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001939 return ret;
1940}
1941
Liam Girdwood23607022014-01-17 17:03:55 +00001942int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001943{
1944 struct snd_soc_dpcm *dpcm;
1945
1946 /* only shutdown BEs that are either sinks or sources to this FE DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001947 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001948
1949 struct snd_soc_pcm_runtime *be = dpcm->be;
1950 struct snd_pcm_substream *be_substream =
1951 snd_soc_dpcm_get_substream(be, stream);
1952
1953 /* is this op for this BE ? */
1954 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1955 continue;
1956
1957 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001958 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001959 stream ? "capture" : "playback",
1960 be->dpcm[stream].state);
1961
1962 if (--be->dpcm[stream].users != 0)
1963 continue;
1964
1965 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Kai Chieh Chuang9c0ac702018-05-28 10:18:18 +08001966 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1967 soc_pcm_hw_free(be_substream);
1968 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1969 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001970
Liam Girdwood103d84a2012-11-19 14:39:15 +00001971 dev_dbg(be->dev, "ASoC: close BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001972 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001973
1974 soc_pcm_close(be_substream);
1975 be_substream->runtime = NULL;
1976
1977 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1978 }
1979 return 0;
1980}
1981
1982static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1983{
1984 struct snd_soc_pcm_runtime *fe = substream->private_data;
1985 int stream = substream->stream;
1986
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001987 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001988
1989 /* shutdown the BEs */
1990 dpcm_be_dai_shutdown(fe, substream->stream);
1991
Liam Girdwood103d84a2012-11-19 14:39:15 +00001992 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001993
1994 /* now shutdown the frontend */
1995 soc_pcm_close(substream);
1996
1997 /* run the stream event for each BE */
1998 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1999
2000 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002001 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002002 return 0;
2003}
2004
Liam Girdwood23607022014-01-17 17:03:55 +00002005int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002006{
2007 struct snd_soc_dpcm *dpcm;
2008
2009 /* only hw_params backends that are either sinks or sources
2010 * to this frontend DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002011 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002012
2013 struct snd_soc_pcm_runtime *be = dpcm->be;
2014 struct snd_pcm_substream *be_substream =
2015 snd_soc_dpcm_get_substream(be, stream);
2016
2017 /* is this op for this BE ? */
2018 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2019 continue;
2020
2021 /* only free hw when no longer used - check all FEs */
2022 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2023 continue;
2024
Qiao Zhou36fba622014-12-03 10:13:43 +08002025 /* do not free hw if this BE is used by other FE */
2026 if (be->dpcm[stream].users > 1)
2027 continue;
2028
Liam Girdwood01d75842012-04-25 12:12:49 +01002029 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2030 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2031 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08002032 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Vinod Koul5e82d2b2016-02-01 22:26:40 +05302033 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2034 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01002035 continue;
2036
Liam Girdwood103d84a2012-11-19 14:39:15 +00002037 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002038 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002039
2040 soc_pcm_hw_free(be_substream);
2041
2042 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2043 }
2044
2045 return 0;
2046}
2047
Mark Brown45c0a182012-05-09 21:46:27 +01002048static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002049{
2050 struct snd_soc_pcm_runtime *fe = substream->private_data;
2051 int err, stream = substream->stream;
2052
2053 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002054 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002055
Liam Girdwood103d84a2012-11-19 14:39:15 +00002056 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002057
2058 /* call hw_free on the frontend */
2059 err = soc_pcm_hw_free(substream);
2060 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002061 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002062 fe->dai_link->name);
2063
2064 /* only hw_params backends that are either sinks or sources
2065 * to this frontend DAI */
2066 err = dpcm_be_dai_hw_free(fe, stream);
2067
2068 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002069 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002070
2071 mutex_unlock(&fe->card->mutex);
2072 return 0;
2073}
2074
Liam Girdwood23607022014-01-17 17:03:55 +00002075int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002076{
2077 struct snd_soc_dpcm *dpcm;
2078 int ret;
2079
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002080 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002081
2082 struct snd_soc_pcm_runtime *be = dpcm->be;
2083 struct snd_pcm_substream *be_substream =
2084 snd_soc_dpcm_get_substream(be, stream);
2085
2086 /* is this op for this BE ? */
2087 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2088 continue;
2089
Liam Girdwood01d75842012-04-25 12:12:49 +01002090 /* copy params for each dpcm */
2091 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2092 sizeof(struct snd_pcm_hw_params));
2093
2094 /* perform any hw_params fixups */
2095 if (be->dai_link->be_hw_params_fixup) {
2096 ret = be->dai_link->be_hw_params_fixup(be,
2097 &dpcm->hw_params);
2098 if (ret < 0) {
2099 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002100 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002101 ret);
2102 goto unwind;
2103 }
2104 }
2105
Libin Yangae061d22019-04-19 09:53:12 +08002106 /* copy the fixed-up hw params for BE dai */
2107 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2108 sizeof(struct snd_pcm_hw_params));
2109
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002110 /* only allow hw_params() if no connected FEs are running */
2111 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2112 continue;
2113
2114 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2115 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2116 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2117 continue;
2118
2119 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002120 be->dai_link->name);
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002121
Liam Girdwood01d75842012-04-25 12:12:49 +01002122 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2123 if (ret < 0) {
2124 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002125 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002126 goto unwind;
2127 }
2128
2129 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2130 }
2131 return 0;
2132
2133unwind:
2134 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002135 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002136 struct snd_soc_pcm_runtime *be = dpcm->be;
2137 struct snd_pcm_substream *be_substream =
2138 snd_soc_dpcm_get_substream(be, stream);
2139
2140 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2141 continue;
2142
2143 /* only allow hw_free() if no connected FEs are running */
2144 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2145 continue;
2146
2147 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2148 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2149 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2150 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2151 continue;
2152
2153 soc_pcm_hw_free(be_substream);
2154 }
2155
2156 return ret;
2157}
2158
Mark Brown45c0a182012-05-09 21:46:27 +01002159static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2160 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01002161{
2162 struct snd_soc_pcm_runtime *fe = substream->private_data;
2163 int ret, stream = substream->stream;
2164
2165 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002166 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002167
2168 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2169 sizeof(struct snd_pcm_hw_params));
2170 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2171 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002172 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002173 goto out;
2174 }
2175
Liam Girdwood103d84a2012-11-19 14:39:15 +00002176 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002177 fe->dai_link->name, params_rate(params),
2178 params_channels(params), params_format(params));
2179
2180 /* call hw_params on the frontend */
2181 ret = soc_pcm_hw_params(substream, params);
2182 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002183 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002184 dpcm_be_dai_hw_free(fe, stream);
2185 } else
2186 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2187
2188out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002189 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002190 mutex_unlock(&fe->card->mutex);
2191 return ret;
2192}
2193
2194static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2195 struct snd_pcm_substream *substream, int cmd)
2196{
2197 int ret;
2198
Liam Girdwood103d84a2012-11-19 14:39:15 +00002199 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
彭东林94d215c2016-09-26 08:29:31 +00002200 dpcm->be->dai_link->name, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002201
2202 ret = soc_pcm_trigger(substream, cmd);
2203 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002204 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002205
2206 return ret;
2207}
2208
Liam Girdwood23607022014-01-17 17:03:55 +00002209int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01002210 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002211{
2212 struct snd_soc_dpcm *dpcm;
2213 int ret = 0;
2214
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002215 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002216
2217 struct snd_soc_pcm_runtime *be = dpcm->be;
2218 struct snd_pcm_substream *be_substream =
2219 snd_soc_dpcm_get_substream(be, stream);
2220
2221 /* is this op for this BE ? */
2222 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2223 continue;
2224
2225 switch (cmd) {
2226 case SNDRV_PCM_TRIGGER_START:
2227 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2228 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2229 continue;
2230
2231 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2232 if (ret)
2233 return ret;
2234
2235 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2236 break;
2237 case SNDRV_PCM_TRIGGER_RESUME:
2238 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2239 continue;
2240
2241 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2242 if (ret)
2243 return ret;
2244
2245 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2246 break;
2247 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2248 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2249 continue;
2250
2251 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2252 if (ret)
2253 return ret;
2254
2255 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2256 break;
2257 case SNDRV_PCM_TRIGGER_STOP:
2258 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2259 continue;
2260
2261 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2262 continue;
2263
2264 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2265 if (ret)
2266 return ret;
2267
2268 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2269 break;
2270 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08002271 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01002272 continue;
2273
2274 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2275 continue;
2276
2277 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2278 if (ret)
2279 return ret;
2280
2281 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2282 break;
2283 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2284 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2285 continue;
2286
2287 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2288 continue;
2289
2290 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2291 if (ret)
2292 return ret;
2293
2294 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2295 break;
2296 }
2297 }
2298
2299 return ret;
2300}
2301EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2302
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002303static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002304{
2305 struct snd_soc_pcm_runtime *fe = substream->private_data;
2306 int stream = substream->stream, ret;
2307 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2308
2309 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2310
2311 switch (trigger) {
2312 case SND_SOC_DPCM_TRIGGER_PRE:
2313 /* call trigger on the frontend before the backend. */
2314
Liam Girdwood103d84a2012-11-19 14:39:15 +00002315 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002316 fe->dai_link->name, cmd);
2317
2318 ret = soc_pcm_trigger(substream, cmd);
2319 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002320 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002321 goto out;
2322 }
2323
2324 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2325 break;
2326 case SND_SOC_DPCM_TRIGGER_POST:
2327 /* call trigger on the frontend after the backend. */
2328
2329 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2330 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002331 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002332 goto out;
2333 }
2334
Liam Girdwood103d84a2012-11-19 14:39:15 +00002335 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002336 fe->dai_link->name, cmd);
2337
2338 ret = soc_pcm_trigger(substream, cmd);
2339 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002340 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2341 /* bespoke trigger() - handles both FE and BEs */
2342
Liam Girdwood103d84a2012-11-19 14:39:15 +00002343 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002344 fe->dai_link->name, cmd);
2345
2346 ret = soc_pcm_bespoke_trigger(substream, cmd);
2347 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002348 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002349 goto out;
2350 }
2351 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002352 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00002353 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01002354 fe->dai_link->name);
2355 ret = -EINVAL;
2356 goto out;
2357 }
2358
2359 switch (cmd) {
2360 case SNDRV_PCM_TRIGGER_START:
2361 case SNDRV_PCM_TRIGGER_RESUME:
2362 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2363 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2364 break;
2365 case SNDRV_PCM_TRIGGER_STOP:
2366 case SNDRV_PCM_TRIGGER_SUSPEND:
Liam Girdwood01d75842012-04-25 12:12:49 +01002367 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2368 break;
Patrick Lai9f169b92016-12-31 22:44:39 -08002369 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2370 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2371 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002372 }
2373
2374out:
2375 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2376 return ret;
2377}
2378
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002379static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2380{
2381 struct snd_soc_pcm_runtime *fe = substream->private_data;
2382 int stream = substream->stream;
2383
2384 /* if FE's runtime_update is already set, we're in race;
2385 * process this trigger later at exit
2386 */
2387 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2388 fe->dpcm[stream].trigger_pending = cmd + 1;
2389 return 0; /* delayed, assuming it's successful */
2390 }
2391
2392 /* we're alone, let's trigger */
2393 return dpcm_fe_dai_do_trigger(substream, cmd);
2394}
2395
Liam Girdwood23607022014-01-17 17:03:55 +00002396int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002397{
2398 struct snd_soc_dpcm *dpcm;
2399 int ret = 0;
2400
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002401 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002402
2403 struct snd_soc_pcm_runtime *be = dpcm->be;
2404 struct snd_pcm_substream *be_substream =
2405 snd_soc_dpcm_get_substream(be, stream);
2406
2407 /* is this op for this BE ? */
2408 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2409 continue;
2410
2411 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
Koro Chen95f444d2015-10-28 10:15:34 +08002412 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
Libin Yang5087a8f2019-05-08 10:32:41 +08002413 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2414 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Liam Girdwood01d75842012-04-25 12:12:49 +01002415 continue;
2416
Liam Girdwood103d84a2012-11-19 14:39:15 +00002417 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002418 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002419
2420 ret = soc_pcm_prepare(be_substream);
2421 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002422 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002423 ret);
2424 break;
2425 }
2426
2427 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2428 }
2429 return ret;
2430}
2431
Mark Brown45c0a182012-05-09 21:46:27 +01002432static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002433{
2434 struct snd_soc_pcm_runtime *fe = substream->private_data;
2435 int stream = substream->stream, ret = 0;
2436
2437 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2438
Liam Girdwood103d84a2012-11-19 14:39:15 +00002439 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002440
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002441 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002442
2443 /* there is no point preparing this FE if there are no BEs */
2444 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002445 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002446 fe->dai_link->name);
2447 ret = -EINVAL;
2448 goto out;
2449 }
2450
2451 ret = dpcm_be_dai_prepare(fe, substream->stream);
2452 if (ret < 0)
2453 goto out;
2454
2455 /* call prepare on the frontend */
2456 ret = soc_pcm_prepare(substream);
2457 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002458 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002459 fe->dai_link->name);
2460 goto out;
2461 }
2462
2463 /* run the stream event for each BE */
2464 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2465 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2466
2467out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002468 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002469 mutex_unlock(&fe->card->mutex);
2470
2471 return ret;
2472}
2473
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002474static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2475 unsigned int cmd, void *arg)
2476{
2477 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +00002478 struct snd_soc_component *component;
2479 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002480
Kuninori Morimotob8135862017-10-11 01:37:23 +00002481 for_each_rtdcom(rtd, rtdcom) {
2482 component = rtdcom->component;
2483
Kuninori Morimotob8135862017-10-11 01:37:23 +00002484 if (!component->driver->ops ||
2485 !component->driver->ops->ioctl)
2486 continue;
2487
2488 /* FIXME: use 1st ioctl */
2489 return component->driver->ops->ioctl(substream, cmd, arg);
2490 }
2491
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002492 return snd_pcm_lib_ioctl(substream, cmd, arg);
2493}
2494
Liam Girdwood618dae12012-04-25 12:12:51 +01002495static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2496{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002497 struct snd_pcm_substream *substream =
2498 snd_soc_dpcm_get_substream(fe, stream);
2499 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002500 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01002501
Liam Girdwood103d84a2012-11-19 14:39:15 +00002502 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002503 stream ? "capture" : "playback", fe->dai_link->name);
2504
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002505 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2506 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002507 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002508 fe->dai_link->name);
2509
2510 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2511 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002512 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002513 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002514 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002515 fe->dai_link->name);
2516
2517 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2518 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002519 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002520 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002521
2522 err = dpcm_be_dai_hw_free(fe, stream);
2523 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002524 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002525
2526 err = dpcm_be_dai_shutdown(fe, stream);
2527 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002528 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002529
2530 /* run the stream event for each BE */
2531 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2532
2533 return 0;
2534}
2535
2536static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2537{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002538 struct snd_pcm_substream *substream =
2539 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002540 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002541 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002542 int ret;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002543 unsigned long flags;
Liam Girdwood618dae12012-04-25 12:12:51 +01002544
Liam Girdwood103d84a2012-11-19 14:39:15 +00002545 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002546 stream ? "capture" : "playback", fe->dai_link->name);
2547
2548 /* Only start the BE if the FE is ready */
2549 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2550 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2551 return -EINVAL;
2552
2553 /* startup must always be called for new BEs */
2554 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002555 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002556 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01002557
2558 /* keep going if FE state is > open */
2559 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2560 return 0;
2561
2562 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002563 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002564 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01002565
2566 /* keep going if FE state is > hw_params */
2567 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2568 return 0;
2569
2570
2571 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002572 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002573 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002574
2575 /* run the stream event for each BE */
2576 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2577
2578 /* keep going if FE state is > prepare */
2579 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2580 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2581 return 0;
2582
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002583 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2584 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002585 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002586 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01002587
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002588 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2589 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002590 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002591 goto hw_free;
2592 }
2593 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002594 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002595 fe->dai_link->name);
2596
2597 ret = dpcm_be_dai_trigger(fe, stream,
2598 SNDRV_PCM_TRIGGER_START);
2599 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002600 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002601 goto hw_free;
2602 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002603 }
2604
2605 return 0;
2606
2607hw_free:
2608 dpcm_be_dai_hw_free(fe, stream);
2609close:
2610 dpcm_be_dai_shutdown(fe, stream);
2611disconnect:
2612 /* disconnect any non started BEs */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002613 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002614 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood618dae12012-04-25 12:12:51 +01002615 struct snd_soc_pcm_runtime *be = dpcm->be;
2616 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2617 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2618 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002619 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood618dae12012-04-25 12:12:51 +01002620
2621 return ret;
2622}
2623
2624static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2625{
2626 int ret;
2627
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002628 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002629 ret = dpcm_run_update_startup(fe, stream);
2630 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002631 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002632 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002633
2634 return ret;
2635}
2636
2637static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2638{
2639 int ret;
2640
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002641 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002642 ret = dpcm_run_update_shutdown(fe, stream);
2643 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002644 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002645 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002646
2647 return ret;
2648}
2649
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002650static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2651{
2652 struct snd_soc_dapm_widget_list *list;
2653 int count, paths;
2654
2655 if (!fe->dai_link->dynamic)
2656 return 0;
2657
2658 /* only check active links */
2659 if (!fe->cpu_dai->active)
2660 return 0;
2661
2662 /* DAPM sync will call this to update DSP paths */
2663 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2664 new ? "new" : "old", fe->dai_link->name);
2665
2666 /* skip if FE doesn't have playback capability */
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002667 if (!snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_PLAYBACK) ||
2668 !snd_soc_dai_stream_valid(fe->codec_dai, SNDRV_PCM_STREAM_PLAYBACK))
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002669 goto capture;
2670
2671 /* skip if FE isn't currently playing */
2672 if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
2673 goto capture;
2674
2675 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2676 if (paths < 0) {
2677 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2678 fe->dai_link->name, "playback");
2679 return paths;
2680 }
2681
2682 /* update any playback paths */
2683 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
2684 if (count) {
2685 if (new)
2686 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2687 else
2688 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2689
2690 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2691 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2692 }
2693
2694 dpcm_path_put(&list);
2695
2696capture:
2697 /* skip if FE doesn't have capture capability */
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002698 if (!snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_CAPTURE) ||
2699 !snd_soc_dai_stream_valid(fe->codec_dai, SNDRV_PCM_STREAM_CAPTURE))
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002700 return 0;
2701
2702 /* skip if FE isn't currently capturing */
2703 if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
2704 return 0;
2705
2706 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2707 if (paths < 0) {
2708 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2709 fe->dai_link->name, "capture");
2710 return paths;
2711 }
2712
2713 /* update any old capture paths */
2714 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
2715 if (count) {
2716 if (new)
2717 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2718 else
2719 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2720
2721 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2722 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2723 }
2724
2725 dpcm_path_put(&list);
2726
2727 return 0;
2728}
2729
Liam Girdwood618dae12012-04-25 12:12:51 +01002730/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2731 * any DAI links.
2732 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002733int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002734{
Mengdong Lin1a497982015-11-18 02:34:11 -05002735 struct snd_soc_pcm_runtime *fe;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002736 int ret = 0;
Liam Girdwood618dae12012-04-25 12:12:51 +01002737
Liam Girdwood618dae12012-04-25 12:12:51 +01002738 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002739 /* shutdown all old paths first */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002740 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002741 ret = soc_dpcm_fe_runtime_update(fe, 0);
2742 if (ret)
2743 goto out;
Liam Girdwood618dae12012-04-25 12:12:51 +01002744 }
2745
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002746 /* bring new paths up */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002747 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002748 ret = soc_dpcm_fe_runtime_update(fe, 1);
2749 if (ret)
2750 goto out;
2751 }
2752
2753out:
Liam Girdwood618dae12012-04-25 12:12:51 +01002754 mutex_unlock(&card->mutex);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002755 return ret;
Liam Girdwood618dae12012-04-25 12:12:51 +01002756}
Liam Girdwood01d75842012-04-25 12:12:49 +01002757int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2758{
2759 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00002760 struct snd_soc_dai *dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01002761
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002762 for_each_dpcm_be(fe, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002763
2764 struct snd_soc_pcm_runtime *be = dpcm->be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002765 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01002766
2767 if (be->dai_link->ignore_suspend)
2768 continue;
2769
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00002770 for_each_rtd_codec_dai(be, i, dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002771 struct snd_soc_dai_driver *drv = dai->driver;
Liam Girdwood01d75842012-04-25 12:12:49 +01002772
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002773 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2774 be->dai_link->name);
2775
2776 if (drv->ops && drv->ops->digital_mute &&
2777 dai->playback_active)
2778 drv->ops->digital_mute(dai, mute);
2779 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002780 }
2781
2782 return 0;
2783}
2784
Mark Brown45c0a182012-05-09 21:46:27 +01002785static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002786{
2787 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2788 struct snd_soc_dpcm *dpcm;
2789 struct snd_soc_dapm_widget_list *list;
2790 int ret;
2791 int stream = fe_substream->stream;
2792
2793 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2794 fe->dpcm[stream].runtime = fe_substream->runtime;
2795
Qiao Zhou8f70e512014-09-10 17:54:07 +08002796 ret = dpcm_path_get(fe, stream, &list);
2797 if (ret < 0) {
2798 mutex_unlock(&fe->card->mutex);
2799 return ret;
2800 } else if (ret == 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002801 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002802 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002803 }
2804
2805 /* calculate valid and active FE <-> BE dpcms */
2806 dpcm_process_paths(fe, stream, &list, 1);
2807
2808 ret = dpcm_fe_dai_startup(fe_substream);
2809 if (ret < 0) {
2810 /* clean up all links */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002811 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood01d75842012-04-25 12:12:49 +01002812 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2813
2814 dpcm_be_disconnect(fe, stream);
2815 fe->dpcm[stream].runtime = NULL;
2816 }
2817
2818 dpcm_clear_pending_state(fe, stream);
2819 dpcm_path_put(&list);
2820 mutex_unlock(&fe->card->mutex);
2821 return ret;
2822}
2823
Mark Brown45c0a182012-05-09 21:46:27 +01002824static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002825{
2826 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2827 struct snd_soc_dpcm *dpcm;
2828 int stream = fe_substream->stream, ret;
2829
2830 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2831 ret = dpcm_fe_dai_shutdown(fe_substream);
2832
2833 /* mark FE's links ready to prune */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002834 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood01d75842012-04-25 12:12:49 +01002835 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2836
2837 dpcm_be_disconnect(fe, stream);
2838
2839 fe->dpcm[stream].runtime = NULL;
2840 mutex_unlock(&fe->card->mutex);
2841 return ret;
2842}
2843
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02002844static void soc_pcm_private_free(struct snd_pcm *pcm)
2845{
2846 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002847 struct snd_soc_rtdcom_list *rtdcom;
2848 struct snd_soc_component *component;
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02002849
Kuninori Morimotof30a4c32018-01-24 05:18:46 +00002850 /* need to sync the delayed work before releasing resources */
2851 flush_delayed_work(&rtd->delayed_work);
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002852 for_each_rtdcom(rtd, rtdcom) {
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002853 component = rtdcom->component;
2854
Kuninori Morimoto11fb14f2018-05-08 03:19:49 +00002855 if (component->driver->pcm_free)
2856 component->driver->pcm_free(pcm);
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002857 }
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02002858}
2859
Kuninori Morimotob8135862017-10-11 01:37:23 +00002860static int soc_rtdcom_copy_user(struct snd_pcm_substream *substream, int channel,
2861 unsigned long pos, void __user *buf,
2862 unsigned long bytes)
2863{
2864 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2865 struct snd_soc_rtdcom_list *rtdcom;
2866 struct snd_soc_component *component;
2867
2868 for_each_rtdcom(rtd, rtdcom) {
2869 component = rtdcom->component;
2870
2871 if (!component->driver->ops ||
2872 !component->driver->ops->copy_user)
2873 continue;
2874
2875 /* FIXME. it returns 1st copy now */
2876 return component->driver->ops->copy_user(substream, channel,
2877 pos, buf, bytes);
2878 }
2879
2880 return -EINVAL;
2881}
2882
Kuninori Morimotob8135862017-10-11 01:37:23 +00002883static struct page *soc_rtdcom_page(struct snd_pcm_substream *substream,
2884 unsigned long offset)
2885{
2886 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2887 struct snd_soc_rtdcom_list *rtdcom;
2888 struct snd_soc_component *component;
2889 struct page *page;
2890
2891 for_each_rtdcom(rtd, rtdcom) {
2892 component = rtdcom->component;
2893
2894 if (!component->driver->ops ||
2895 !component->driver->ops->page)
2896 continue;
2897
2898 /* FIXME. it returns 1st page now */
2899 page = component->driver->ops->page(substream, offset);
2900 if (page)
2901 return page;
2902 }
2903
2904 return NULL;
2905}
2906
2907static int soc_rtdcom_mmap(struct snd_pcm_substream *substream,
2908 struct vm_area_struct *vma)
2909{
2910 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2911 struct snd_soc_rtdcom_list *rtdcom;
2912 struct snd_soc_component *component;
2913
2914 for_each_rtdcom(rtd, rtdcom) {
2915 component = rtdcom->component;
2916
2917 if (!component->driver->ops ||
2918 !component->driver->ops->mmap)
2919 continue;
2920
2921 /* FIXME. it returns 1st mmap now */
2922 return component->driver->ops->mmap(substream, vma);
2923 }
2924
2925 return -EINVAL;
2926}
2927
Liam Girdwoodddee6272011-06-09 14:45:53 +01002928/* create a new pcm */
2929int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2930{
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002931 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002932 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002933 struct snd_soc_component *component;
2934 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002935 struct snd_pcm *pcm;
2936 char new_name[64];
2937 int ret = 0, playback = 0, capture = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002938 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002939
Liam Girdwood01d75842012-04-25 12:12:49 +01002940 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002941 playback = rtd->dai_link->dpcm_playback;
2942 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002943 } else {
Jerome Bruneta3420312019-07-25 18:59:47 +02002944 /* Adapt stream for codec2codec links */
2945 struct snd_soc_pcm_stream *cpu_capture = rtd->dai_link->params ?
2946 &cpu_dai->driver->playback : &cpu_dai->driver->capture;
2947 struct snd_soc_pcm_stream *cpu_playback = rtd->dai_link->params ?
2948 &cpu_dai->driver->capture : &cpu_dai->driver->playback;
2949
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002950 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002951 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2952 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002953 playback = 1;
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002954 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2955 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002956 capture = 1;
2957 }
Jerome Bruneta3420312019-07-25 18:59:47 +02002958
2959 capture = capture && cpu_capture->channels_min;
2960 playback = playback && cpu_playback->channels_min;
Liam Girdwood01d75842012-04-25 12:12:49 +01002961 }
Sangsu Parka5002312012-01-02 17:15:10 +09002962
Fabio Estevamd6bead02013-08-29 10:32:13 -03002963 if (rtd->dai_link->playback_only) {
2964 playback = 1;
2965 capture = 0;
2966 }
2967
2968 if (rtd->dai_link->capture_only) {
2969 playback = 0;
2970 capture = 1;
2971 }
2972
Liam Girdwood01d75842012-04-25 12:12:49 +01002973 /* create the PCM */
Jerome Bruneta3420312019-07-25 18:59:47 +02002974 if (rtd->dai_link->params) {
2975 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2976 rtd->dai_link->stream_name);
2977
2978 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2979 playback, capture, &pcm);
2980 } else if (rtd->dai_link->no_pcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002981 snprintf(new_name, sizeof(new_name), "(%s)",
2982 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002983
Liam Girdwood01d75842012-04-25 12:12:49 +01002984 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2985 playback, capture, &pcm);
2986 } else {
2987 if (rtd->dai_link->dynamic)
2988 snprintf(new_name, sizeof(new_name), "%s (*)",
2989 rtd->dai_link->stream_name);
2990 else
2991 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002992 rtd->dai_link->stream_name,
2993 (rtd->num_codecs > 1) ?
2994 "multicodec" : rtd->codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002995
Liam Girdwood01d75842012-04-25 12:12:49 +01002996 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2997 capture, &pcm);
2998 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002999 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00003000 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01003001 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003002 return ret;
3003 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00003004 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003005
3006 /* DAPM dai link stream work */
Jerome Bruneta3420312019-07-25 18:59:47 +02003007 if (rtd->dai_link->params)
3008 INIT_DELAYED_WORK(&rtd->delayed_work,
3009 codec2codec_close_delayed_work);
3010 else
3011 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003012
Vinod Koul48c76992015-02-12 09:59:53 +05303013 pcm->nonatomic = rtd->dai_link->nonatomic;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003014 rtd->pcm = pcm;
3015 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01003016
Jerome Bruneta3420312019-07-25 18:59:47 +02003017 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
Liam Girdwood01d75842012-04-25 12:12:49 +01003018 if (playback)
3019 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
3020 if (capture)
3021 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
3022 goto out;
3023 }
3024
3025 /* ASoC PCM operations */
3026 if (rtd->dai_link->dynamic) {
3027 rtd->ops.open = dpcm_fe_dai_open;
3028 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
3029 rtd->ops.prepare = dpcm_fe_dai_prepare;
3030 rtd->ops.trigger = dpcm_fe_dai_trigger;
3031 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
3032 rtd->ops.close = dpcm_fe_dai_close;
3033 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01003034 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01003035 } else {
3036 rtd->ops.open = soc_pcm_open;
3037 rtd->ops.hw_params = soc_pcm_hw_params;
3038 rtd->ops.prepare = soc_pcm_prepare;
3039 rtd->ops.trigger = soc_pcm_trigger;
3040 rtd->ops.hw_free = soc_pcm_hw_free;
3041 rtd->ops.close = soc_pcm_close;
3042 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01003043 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01003044 }
3045
Kuninori Morimotob8135862017-10-11 01:37:23 +00003046 for_each_rtdcom(rtd, rtdcom) {
3047 const struct snd_pcm_ops *ops = rtdcom->component->driver->ops;
3048
3049 if (!ops)
3050 continue;
3051
Kuninori Morimotob8135862017-10-11 01:37:23 +00003052 if (ops->copy_user)
3053 rtd->ops.copy_user = soc_rtdcom_copy_user;
Kuninori Morimotob8135862017-10-11 01:37:23 +00003054 if (ops->page)
3055 rtd->ops.page = soc_rtdcom_page;
3056 if (ops->mmap)
3057 rtd->ops.mmap = soc_rtdcom_mmap;
3058 }
3059
Liam Girdwoodddee6272011-06-09 14:45:53 +01003060 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01003061 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003062
3063 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01003064 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003065
Kuninori Morimotof523ace2017-09-26 01:00:53 +00003066 for_each_rtdcom(rtd, rtdcom) {
3067 component = rtdcom->component;
3068
Kuninori Morimoto11fb14f2018-05-08 03:19:49 +00003069 if (!component->driver->pcm_new)
Kuninori Morimotof523ace2017-09-26 01:00:53 +00003070 continue;
3071
Kuninori Morimoto11fb14f2018-05-08 03:19:49 +00003072 ret = component->driver->pcm_new(rtd);
Johan Hovoldc641e5b2017-07-12 17:55:29 +02003073 if (ret < 0) {
Kuninori Morimotof523ace2017-09-26 01:00:53 +00003074 dev_err(component->dev,
Johan Hovoldc641e5b2017-07-12 17:55:29 +02003075 "ASoC: pcm constructor failed: %d\n",
3076 ret);
3077 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003078 }
3079 }
Johan Hovoldc641e5b2017-07-12 17:55:29 +02003080
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02003081 pcm->private_free = soc_pcm_private_free;
Takashi Iwai3d21ef02019-01-11 15:58:39 +01003082 pcm->no_device_suspend = true;
Liam Girdwood01d75842012-04-25 12:12:49 +01003083out:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02003084 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3085 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3086 cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003087 return ret;
3088}
Liam Girdwood01d75842012-04-25 12:12:49 +01003089
3090/* is the current PCM operation for this FE ? */
3091int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3092{
3093 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3094 return 1;
3095 return 0;
3096}
3097EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3098
3099/* is the current PCM operation for this BE ? */
3100int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3101 struct snd_soc_pcm_runtime *be, int stream)
3102{
3103 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3104 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3105 be->dpcm[stream].runtime_update))
3106 return 1;
3107 return 0;
3108}
3109EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3110
3111/* get the substream for this BE */
3112struct snd_pcm_substream *
3113 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3114{
3115 return be->pcm->streams[stream].substream;
3116}
3117EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3118
3119/* get the BE runtime state */
3120enum snd_soc_dpcm_state
3121 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3122{
3123 return be->dpcm[stream].state;
3124}
3125EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3126
3127/* set the BE runtime state */
3128void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3129 int stream, enum snd_soc_dpcm_state state)
3130{
3131 be->dpcm[stream].state = state;
3132}
3133EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3134
3135/*
3136 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3137 * are not running, paused or suspended for the specified stream direction.
3138 */
3139int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3140 struct snd_soc_pcm_runtime *be, int stream)
3141{
3142 struct snd_soc_dpcm *dpcm;
3143 int state;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003144 int ret = 1;
3145 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01003146
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003147 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00003148 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01003149
3150 if (dpcm->fe == fe)
3151 continue;
3152
3153 state = dpcm->fe->dpcm[stream].state;
3154 if (state == SND_SOC_DPCM_STATE_START ||
3155 state == SND_SOC_DPCM_STATE_PAUSED ||
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003156 state == SND_SOC_DPCM_STATE_SUSPEND) {
3157 ret = 0;
3158 break;
3159 }
Liam Girdwood01d75842012-04-25 12:12:49 +01003160 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003161 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01003162
3163 /* it's safe to free/stop this BE DAI */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003164 return ret;
Liam Girdwood01d75842012-04-25 12:12:49 +01003165}
3166EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3167
3168/*
3169 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3170 * running, paused or suspended for the specified stream direction.
3171 */
3172int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3173 struct snd_soc_pcm_runtime *be, int stream)
3174{
3175 struct snd_soc_dpcm *dpcm;
3176 int state;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003177 int ret = 1;
3178 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01003179
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003180 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00003181 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01003182
3183 if (dpcm->fe == fe)
3184 continue;
3185
3186 state = dpcm->fe->dpcm[stream].state;
3187 if (state == SND_SOC_DPCM_STATE_START ||
3188 state == SND_SOC_DPCM_STATE_PAUSED ||
3189 state == SND_SOC_DPCM_STATE_SUSPEND ||
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003190 state == SND_SOC_DPCM_STATE_PREPARE) {
3191 ret = 0;
3192 break;
3193 }
Liam Girdwood01d75842012-04-25 12:12:49 +01003194 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003195 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01003196
3197 /* it's safe to change hw_params */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003198 return ret;
Liam Girdwood01d75842012-04-25 12:12:49 +01003199}
3200EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003201
3202#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen852801412016-11-22 11:29:14 +01003203static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003204{
3205 switch (state) {
3206 case SND_SOC_DPCM_STATE_NEW:
3207 return "new";
3208 case SND_SOC_DPCM_STATE_OPEN:
3209 return "open";
3210 case SND_SOC_DPCM_STATE_HW_PARAMS:
3211 return "hw_params";
3212 case SND_SOC_DPCM_STATE_PREPARE:
3213 return "prepare";
3214 case SND_SOC_DPCM_STATE_START:
3215 return "start";
3216 case SND_SOC_DPCM_STATE_STOP:
3217 return "stop";
3218 case SND_SOC_DPCM_STATE_SUSPEND:
3219 return "suspend";
3220 case SND_SOC_DPCM_STATE_PAUSED:
3221 return "paused";
3222 case SND_SOC_DPCM_STATE_HW_FREE:
3223 return "hw_free";
3224 case SND_SOC_DPCM_STATE_CLOSE:
3225 return "close";
3226 }
3227
3228 return "unknown";
3229}
3230
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003231static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3232 int stream, char *buf, size_t size)
3233{
3234 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3235 struct snd_soc_dpcm *dpcm;
3236 ssize_t offset = 0;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003237 unsigned long flags;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003238
3239 /* FE state */
3240 offset += snprintf(buf + offset, size - offset,
3241 "[%s - %s]\n", fe->dai_link->name,
3242 stream ? "Capture" : "Playback");
3243
3244 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3245 dpcm_state_string(fe->dpcm[stream].state));
3246
3247 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3248 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3249 offset += snprintf(buf + offset, size - offset,
3250 "Hardware Params: "
3251 "Format = %s, Channels = %d, Rate = %d\n",
3252 snd_pcm_format_name(params_format(params)),
3253 params_channels(params),
3254 params_rate(params));
3255
3256 /* BEs state */
3257 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3258
3259 if (list_empty(&fe->dpcm[stream].be_clients)) {
3260 offset += snprintf(buf + offset, size - offset,
3261 " No active DSP links\n");
3262 goto out;
3263 }
3264
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003265 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00003266 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003267 struct snd_soc_pcm_runtime *be = dpcm->be;
3268 params = &dpcm->hw_params;
3269
3270 offset += snprintf(buf + offset, size - offset,
3271 "- %s\n", be->dai_link->name);
3272
3273 offset += snprintf(buf + offset, size - offset,
3274 " State: %s\n",
3275 dpcm_state_string(be->dpcm[stream].state));
3276
3277 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3278 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3279 offset += snprintf(buf + offset, size - offset,
3280 " Hardware Params: "
3281 "Format = %s, Channels = %d, Rate = %d\n",
3282 snd_pcm_format_name(params_format(params)),
3283 params_channels(params),
3284 params_rate(params));
3285 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003286 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003287out:
3288 return offset;
3289}
3290
3291static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3292 size_t count, loff_t *ppos)
3293{
3294 struct snd_soc_pcm_runtime *fe = file->private_data;
3295 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3296 char *buf;
3297
3298 buf = kmalloc(out_count, GFP_KERNEL);
3299 if (!buf)
3300 return -ENOMEM;
3301
Kuninori Morimoto467fece2019-07-22 10:36:16 +09003302 if (snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003303 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3304 buf + offset, out_count - offset);
3305
Kuninori Morimoto467fece2019-07-22 10:36:16 +09003306 if (snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003307 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3308 buf + offset, out_count - offset);
3309
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003310 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003311
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003312 kfree(buf);
3313 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003314}
3315
3316static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003317 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003318 .read = dpcm_state_read_file,
3319 .llseek = default_llseek,
3320};
3321
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003322void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003323{
Mark Brownb3bba9a2012-05-08 10:33:47 +01003324 if (!rtd->dai_link)
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003325 return;
Mark Brownb3bba9a2012-05-08 10:33:47 +01003326
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003327 if (!rtd->card->debugfs_card_root)
3328 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003329
3330 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3331 rtd->card->debugfs_card_root);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003332
Fabio Estevamf1e3f402017-07-29 11:40:55 -03003333 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3334 rtd, &dpcm_state_fops);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003335}
3336#endif