blob: 2d846b3dd70ca1337ec8d62b2dc41a38858afcc2 [file] [log] [blame]
Liam Girdwoodddee6272011-06-09 14:45:53 +01001/*
2 * soc-pcm.c -- ALSA SoC PCM
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
8 *
9 * Authors: Liam Girdwood <lrg@ti.com>
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +020010 * Mark Brown <broonie@opensource.wolfsonmicro.com>
Liam Girdwoodddee6272011-06-09 14:45:53 +010011 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 */
18
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/delay.h>
Nicolin Chen988e8cc2013-11-04 14:57:31 +080022#include <linux/pinctrl/consumer.h>
Mark Brownd6652ef2011-12-03 20:14:31 +000023#include <linux/pm_runtime.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010024#include <linux/slab.h>
25#include <linux/workqueue.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010026#include <linux/export.h>
Liam Girdwoodf86dcef2012-04-25 12:12:50 +010027#include <linux/debugfs.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010028#include <sound/core.h>
29#include <sound/pcm.h>
30#include <sound/pcm_params.h>
31#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010032#include <sound/soc-dpcm.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010033#include <sound/initval.h>
34
Liam Girdwood01d75842012-04-25 12:12:49 +010035#define DPCM_MAX_BE_USERS 8
36
Ricard Wanderlofcde79032015-08-24 14:16:51 +020037/*
38 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
39 *
40 * Returns true if the DAI supports the indicated stream type.
41 */
42static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
43{
44 struct snd_soc_pcm_stream *codec_stream;
45
46 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
47 codec_stream = &dai->driver->playback;
48 else
49 codec_stream = &dai->driver->capture;
50
51 /* If the codec specifies any rate at all, it supports the stream. */
52 return codec_stream->rates;
53}
54
Lars-Peter Clausen90996f42013-05-14 11:05:30 +020055/**
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010056 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
57 * @rtd: ASoC PCM runtime that is activated
58 * @stream: Direction of the PCM stream
59 *
60 * Increments the active count for all the DAIs and components attached to a PCM
61 * runtime. Should typically be called when a stream is opened.
62 *
63 * Must be called with the rtd->pcm_mutex being held
64 */
65void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
66{
67 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020068 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010069
70 lockdep_assert_held(&rtd->pcm_mutex);
71
72 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
73 cpu_dai->playback_active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020074 for (i = 0; i < rtd->num_codecs; i++)
75 rtd->codec_dais[i]->playback_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010076 } else {
77 cpu_dai->capture_active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020078 for (i = 0; i < rtd->num_codecs; i++)
79 rtd->codec_dais[i]->capture_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010080 }
81
82 cpu_dai->active++;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +010083 cpu_dai->component->active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020084 for (i = 0; i < rtd->num_codecs; i++) {
85 rtd->codec_dais[i]->active++;
86 rtd->codec_dais[i]->component->active++;
87 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010088}
89
90/**
91 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
92 * @rtd: ASoC PCM runtime that is deactivated
93 * @stream: Direction of the PCM stream
94 *
95 * Decrements the active count for all the DAIs and components attached to a PCM
96 * runtime. Should typically be called when a stream is closed.
97 *
98 * Must be called with the rtd->pcm_mutex being held
99 */
100void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
101{
102 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200103 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100104
105 lockdep_assert_held(&rtd->pcm_mutex);
106
107 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
108 cpu_dai->playback_active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200109 for (i = 0; i < rtd->num_codecs; i++)
110 rtd->codec_dais[i]->playback_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100111 } else {
112 cpu_dai->capture_active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200113 for (i = 0; i < rtd->num_codecs; i++)
114 rtd->codec_dais[i]->capture_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100115 }
116
117 cpu_dai->active--;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +0100118 cpu_dai->component->active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200119 for (i = 0; i < rtd->num_codecs; i++) {
120 rtd->codec_dais[i]->component->active--;
121 rtd->codec_dais[i]->active--;
122 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100123}
124
125/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100126 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
127 * @rtd: The ASoC PCM runtime that should be checked.
128 *
129 * This function checks whether the power down delay should be ignored for a
130 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
131 * been configured to ignore the delay, or if none of the components benefits
132 * from having the delay.
133 */
134bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
135{
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000136 struct snd_soc_rtdcom_list *rtdcom;
137 struct snd_soc_component *component;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200138 bool ignore = true;
139
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100140 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
141 return true;
142
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000143 for_each_rtdcom(rtd, rtdcom) {
144 component = rtdcom->component;
145
Kuninori Morimoto72c38182018-01-19 05:21:19 +0000146 ignore &= !component->driver->use_pmdown_time;
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000147 }
148
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000149 return ignore;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100150}
151
152/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200153 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
154 * @substream: the pcm substream
155 * @hw: the hardware parameters
156 *
157 * Sets the substream runtime hardware parameters.
158 */
159int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
160 const struct snd_pcm_hardware *hw)
161{
162 struct snd_pcm_runtime *runtime = substream->runtime;
163 runtime->hw.info = hw->info;
164 runtime->hw.formats = hw->formats;
165 runtime->hw.period_bytes_min = hw->period_bytes_min;
166 runtime->hw.period_bytes_max = hw->period_bytes_max;
167 runtime->hw.periods_min = hw->periods_min;
168 runtime->hw.periods_max = hw->periods_max;
169 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
170 runtime->hw.fifo_size = hw->fifo_size;
171 return 0;
172}
173EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
174
Liam Girdwood01d75842012-04-25 12:12:49 +0100175/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000176int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100177 int event)
178{
179 struct snd_soc_dpcm *dpcm;
180
181 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
182
183 struct snd_soc_pcm_runtime *be = dpcm->be;
184
Liam Girdwood103d84a2012-11-19 14:39:15 +0000185 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100186 be->dai_link->name, event, dir);
187
Banajit Goswamib1cd2e32017-07-14 23:15:05 -0700188 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
189 (be->dpcm[dir].users >= 1))
190 continue;
191
Liam Girdwood01d75842012-04-25 12:12:49 +0100192 snd_soc_dapm_stream_event(be, dir, event);
193 }
194
195 snd_soc_dapm_stream_event(fe, dir, event);
196
197 return 0;
198}
199
Dong Aisheng17841022011-08-29 17:15:14 +0800200static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
201 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100202{
203 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100204 int ret;
205
Nicolin Chen3635bf02013-11-13 18:56:24 +0800206 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
207 rtd->dai_link->symmetric_rates)) {
208 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
209 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100210
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200211 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800212 SNDRV_PCM_HW_PARAM_RATE,
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200213 soc_dai->rate);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800214 if (ret < 0) {
215 dev_err(soc_dai->dev,
216 "ASoC: Unable to apply rate constraint: %d\n",
217 ret);
218 return ret;
219 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100220 }
221
Nicolin Chen3635bf02013-11-13 18:56:24 +0800222 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
223 rtd->dai_link->symmetric_channels)) {
224 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
225 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100226
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200227 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800228 SNDRV_PCM_HW_PARAM_CHANNELS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800229 soc_dai->channels);
230 if (ret < 0) {
231 dev_err(soc_dai->dev,
232 "ASoC: Unable to apply channel symmetry constraint: %d\n",
233 ret);
234 return ret;
235 }
236 }
237
238 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
239 rtd->dai_link->symmetric_samplebits)) {
240 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
241 soc_dai->sample_bits);
242
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200243 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800244 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800245 soc_dai->sample_bits);
246 if (ret < 0) {
247 dev_err(soc_dai->dev,
248 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
249 ret);
250 return ret;
251 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100252 }
253
254 return 0;
255}
256
Nicolin Chen3635bf02013-11-13 18:56:24 +0800257static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
258 struct snd_pcm_hw_params *params)
259{
260 struct snd_soc_pcm_runtime *rtd = substream->private_data;
261 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200262 unsigned int rate, channels, sample_bits, symmetry, i;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800263
264 rate = params_rate(params);
265 channels = params_channels(params);
266 sample_bits = snd_pcm_format_physical_width(params_format(params));
267
268 /* reject unmatched parameters when applying symmetry */
269 symmetry = cpu_dai->driver->symmetric_rates ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800270 rtd->dai_link->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200271
272 for (i = 0; i < rtd->num_codecs; i++)
273 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
274
Nicolin Chen3635bf02013-11-13 18:56:24 +0800275 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
276 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
277 cpu_dai->rate, rate);
278 return -EINVAL;
279 }
280
281 symmetry = cpu_dai->driver->symmetric_channels ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800282 rtd->dai_link->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200283
284 for (i = 0; i < rtd->num_codecs; i++)
285 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
286
Nicolin Chen3635bf02013-11-13 18:56:24 +0800287 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
288 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
289 cpu_dai->channels, channels);
290 return -EINVAL;
291 }
292
293 symmetry = cpu_dai->driver->symmetric_samplebits ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800294 rtd->dai_link->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200295
296 for (i = 0; i < rtd->num_codecs; i++)
297 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
298
Nicolin Chen3635bf02013-11-13 18:56:24 +0800299 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
300 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
301 cpu_dai->sample_bits, sample_bits);
302 return -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100303 }
304
305 return 0;
306}
307
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100308static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
309{
310 struct snd_soc_pcm_runtime *rtd = substream->private_data;
311 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100312 struct snd_soc_dai_link *link = rtd->dai_link;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200313 unsigned int symmetry, i;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100314
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200315 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
316 cpu_driver->symmetric_channels || link->symmetric_channels ||
317 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
318
319 for (i = 0; i < rtd->num_codecs; i++)
320 symmetry = symmetry ||
321 rtd->codec_dais[i]->driver->symmetric_rates ||
322 rtd->codec_dais[i]->driver->symmetric_channels ||
323 rtd->codec_dais[i]->driver->symmetric_samplebits;
324
325 return symmetry;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100326}
327
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200328static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000329{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200330 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Takashi Iwaic6068d32014-12-31 17:10:34 +0100331 int ret;
Mark Brown58ba9b22012-01-16 18:38:51 +0000332
333 if (!bits)
334 return;
335
Lars-Peter Clausen0e2a3752014-12-29 18:43:38 +0100336 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
337 if (ret != 0)
338 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
339 bits, ret);
Mark Brown58ba9b22012-01-16 18:38:51 +0000340}
341
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200342static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200343{
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200344 struct snd_soc_pcm_runtime *rtd = substream->private_data;
345 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200346 struct snd_soc_dai *codec_dai;
347 int i;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200348 unsigned int bits = 0, cpu_bits;
349
350 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200351 for (i = 0; i < rtd->num_codecs; i++) {
352 codec_dai = rtd->codec_dais[i];
353 if (codec_dai->driver->playback.sig_bits == 0) {
354 bits = 0;
355 break;
356 }
357 bits = max(codec_dai->driver->playback.sig_bits, bits);
358 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200359 cpu_bits = cpu_dai->driver->playback.sig_bits;
360 } else {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200361 for (i = 0; i < rtd->num_codecs; i++) {
362 codec_dai = rtd->codec_dais[i];
Daniel Mack5e63dfc2014-10-07 14:33:46 +0200363 if (codec_dai->driver->capture.sig_bits == 0) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200364 bits = 0;
365 break;
366 }
367 bits = max(codec_dai->driver->capture.sig_bits, bits);
368 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200369 cpu_bits = cpu_dai->driver->capture.sig_bits;
370 }
371
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200372 soc_pcm_set_msb(substream, bits);
373 soc_pcm_set_msb(substream, cpu_bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200374}
375
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200376static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200377{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200378 struct snd_pcm_runtime *runtime = substream->runtime;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100379 struct snd_pcm_hardware *hw = &runtime->hw;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200380 struct snd_soc_pcm_runtime *rtd = substream->private_data;
381 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
382 struct snd_soc_dai_driver *codec_dai_drv;
383 struct snd_soc_pcm_stream *codec_stream;
384 struct snd_soc_pcm_stream *cpu_stream;
385 unsigned int chan_min = 0, chan_max = UINT_MAX;
386 unsigned int rate_min = 0, rate_max = UINT_MAX;
387 unsigned int rates = UINT_MAX;
388 u64 formats = ULLONG_MAX;
389 int i;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100390
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200391 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
392 cpu_stream = &cpu_dai_drv->playback;
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100393 else
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200394 cpu_stream = &cpu_dai_drv->capture;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100395
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200396 /* first calculate min/max only for CODECs in the DAI link */
397 for (i = 0; i < rtd->num_codecs; i++) {
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200398
399 /*
400 * Skip CODECs which don't support the current stream type.
401 * Otherwise, since the rate, channel, and format values will
402 * zero in that case, we would have no usable settings left,
403 * causing the resulting setup to fail.
404 * At least one CODEC should match, otherwise we should have
405 * bailed out on a higher level, since there would be no
406 * CODEC to support the transfer direction in that case.
407 */
408 if (!snd_soc_dai_stream_valid(rtd->codec_dais[i],
409 substream->stream))
410 continue;
411
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200412 codec_dai_drv = rtd->codec_dais[i]->driver;
413 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
414 codec_stream = &codec_dai_drv->playback;
415 else
416 codec_stream = &codec_dai_drv->capture;
417 chan_min = max(chan_min, codec_stream->channels_min);
418 chan_max = min(chan_max, codec_stream->channels_max);
419 rate_min = max(rate_min, codec_stream->rate_min);
420 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
421 formats &= codec_stream->formats;
422 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
423 }
424
425 /*
426 * chan min/max cannot be enforced if there are multiple CODEC DAIs
427 * connected to a single CPU DAI, use CPU DAI's directly and let
428 * channel allocation be fixed up later
429 */
430 if (rtd->num_codecs > 1) {
431 chan_min = cpu_stream->channels_min;
432 chan_max = cpu_stream->channels_max;
433 }
434
435 hw->channels_min = max(chan_min, cpu_stream->channels_min);
436 hw->channels_max = min(chan_max, cpu_stream->channels_max);
437 if (hw->formats)
438 hw->formats &= formats & cpu_stream->formats;
439 else
440 hw->formats = formats & cpu_stream->formats;
441 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100442
443 snd_pcm_limit_hw_rates(runtime);
444
445 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200446 hw->rate_min = max(hw->rate_min, rate_min);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100447 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200448 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200449}
450
Mark Brown58ba9b22012-01-16 18:38:51 +0000451/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100452 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
453 * then initialized and any private data can be allocated. This also calls
Charles Keepaxef050be2018-04-24 16:39:02 +0100454 * startup for the cpu DAI, component, machine and codec DAI.
Liam Girdwoodddee6272011-06-09 14:45:53 +0100455 */
456static int soc_pcm_open(struct snd_pcm_substream *substream)
457{
458 struct snd_soc_pcm_runtime *rtd = substream->private_data;
459 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000460 struct snd_soc_component *component;
461 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100462 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200463 struct snd_soc_dai *codec_dai;
464 const char *codec_dai_name = "multicodec";
Kuninori Morimotob8135862017-10-11 01:37:23 +0000465 int i, ret = 0, __ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100466
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800467 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200468 for (i = 0; i < rtd->num_codecs; i++)
469 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000470
471 for_each_rtdcom(rtd, rtdcom) {
472 component = rtdcom->component;
473
474 pm_runtime_get_sync(component->dev);
475 }
Mark Brownd6652ef2011-12-03 20:14:31 +0000476
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100477 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100478
479 /* startup the audio subsystem */
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000480 if (cpu_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100481 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
482 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000483 dev_err(cpu_dai->dev, "ASoC: can't open interface"
484 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100485 goto out;
486 }
487 }
488
Kuninori Morimotob8135862017-10-11 01:37:23 +0000489 ret = 0;
490 for_each_rtdcom(rtd, rtdcom) {
491 component = rtdcom->component;
492
Kuninori Morimotob8135862017-10-11 01:37:23 +0000493 if (!component->driver->ops ||
494 !component->driver->ops->open)
495 continue;
496
497 __ret = component->driver->ops->open(substream);
498 if (__ret < 0) {
499 dev_err(component->dev,
500 "ASoC: can't open component %s: %d\n",
Daniel Mack637917b2018-05-19 08:01:19 +0200501 component->name, __ret);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000502 ret = __ret;
503 }
504 }
505 if (ret < 0)
506 goto component_err;
507
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200508 for (i = 0; i < rtd->num_codecs; i++) {
509 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000510 if (codec_dai->driver->ops->startup) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200511 ret = codec_dai->driver->ops->startup(substream,
512 codec_dai);
513 if (ret < 0) {
514 dev_err(codec_dai->dev,
515 "ASoC: can't open codec %s: %d\n",
516 codec_dai->name, ret);
517 goto codec_dai_err;
518 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100519 }
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200520
521 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
522 codec_dai->tx_mask = 0;
523 else
524 codec_dai->rx_mask = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100525 }
526
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000527 if (rtd->dai_link->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100528 ret = rtd->dai_link->ops->startup(substream);
529 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000530 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000531 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100532 goto machine_err;
533 }
534 }
535
Liam Girdwood01d75842012-04-25 12:12:49 +0100536 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
537 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
538 goto dynamic;
539
Liam Girdwoodddee6272011-06-09 14:45:53 +0100540 /* Check that the codec and cpu DAIs are compatible */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200541 soc_pcm_init_runtime_hw(substream);
542
543 if (rtd->num_codecs == 1)
544 codec_dai_name = rtd->codec_dai->name;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100545
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100546 if (soc_pcm_has_symmetry(substream))
547 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
548
Liam Girdwoodddee6272011-06-09 14:45:53 +0100549 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100550 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000551 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200552 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100553 goto config_err;
554 }
555 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000556 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200557 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100558 goto config_err;
559 }
560 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
561 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000562 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\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
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200567 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000568
Liam Girdwoodddee6272011-06-09 14:45:53 +0100569 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800570 if (cpu_dai->active) {
571 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
572 if (ret != 0)
573 goto config_err;
574 }
575
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200576 for (i = 0; i < rtd->num_codecs; i++) {
577 if (rtd->codec_dais[i]->active) {
578 ret = soc_pcm_apply_symmetry(substream,
579 rtd->codec_dais[i]);
580 if (ret != 0)
581 goto config_err;
582 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100583 }
584
Liam Girdwood103d84a2012-11-19 14:39:15 +0000585 pr_debug("ASoC: %s <-> %s info:\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200586 codec_dai_name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000587 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
588 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100589 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000590 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100591 runtime->hw.rate_max);
592
Liam Girdwood01d75842012-04-25 12:12:49 +0100593dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100594
595 snd_soc_runtime_activate(rtd, substream->stream);
596
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100597 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100598 return 0;
599
600config_err:
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000601 if (rtd->dai_link->ops->shutdown)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100602 rtd->dai_link->ops->shutdown(substream);
603
604machine_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200605 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100606
607codec_dai_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200608 while (--i >= 0) {
609 codec_dai = rtd->codec_dais[i];
610 if (codec_dai->driver->ops->shutdown)
611 codec_dai->driver->ops->shutdown(substream, codec_dai);
612 }
613
Kuninori Morimotob8135862017-10-11 01:37:23 +0000614component_err:
615 for_each_rtdcom(rtd, rtdcom) {
616 component = rtdcom->component;
617
Kuninori Morimotob8135862017-10-11 01:37:23 +0000618 if (!component->driver->ops ||
619 !component->driver->ops->close)
620 continue;
621
622 component->driver->ops->close(substream);
623 }
624
Liam Girdwoodddee6272011-06-09 14:45:53 +0100625 if (cpu_dai->driver->ops->shutdown)
626 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
627out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100628 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000629
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000630 for_each_rtdcom(rtd, rtdcom) {
631 component = rtdcom->component;
632
633 pm_runtime_mark_last_busy(component->dev);
634 pm_runtime_put_autosuspend(component->dev);
Sanyog Kale3f809782016-01-05 17:14:49 +0530635 }
636
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200637 for (i = 0; i < rtd->num_codecs; i++) {
638 if (!rtd->codec_dais[i]->active)
639 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
640 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800641 if (!cpu_dai->active)
642 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000643
Liam Girdwoodddee6272011-06-09 14:45:53 +0100644 return ret;
645}
646
647/*
648 * Power down the audio subsystem pmdown_time msecs after close is called.
649 * This is to ensure there are no pops or clicks in between any music tracks
650 * due to DAPM power cycling.
651 */
652static void close_delayed_work(struct work_struct *work)
653{
654 struct snd_soc_pcm_runtime *rtd =
655 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200656 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
Liam Girdwoodddee6272011-06-09 14:45:53 +0100657
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100658 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100659
Liam Girdwood103d84a2012-11-19 14:39:15 +0000660 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100661 codec_dai->driver->playback.stream_name,
662 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600663 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100664
665 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600666 if (rtd->pop_wait == 1) {
667 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800668 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000669 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100670 }
671
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100672 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100673}
674
675/*
676 * Called by ALSA when a PCM substream is closed. Private data can be
Charles Keepaxef050be2018-04-24 16:39:02 +0100677 * freed here. The cpu DAI, codec DAI, machine and components are also
Liam Girdwoodddee6272011-06-09 14:45:53 +0100678 * shutdown.
679 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100680static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100681{
682 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000683 struct snd_soc_component *component;
684 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100685 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200686 struct snd_soc_dai *codec_dai;
687 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100688
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100689 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100690
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100691 snd_soc_runtime_deactivate(rtd, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100692
Dong Aisheng17841022011-08-29 17:15:14 +0800693 /* clear the corresponding DAIs rate when inactive */
694 if (!cpu_dai->active)
695 cpu_dai->rate = 0;
696
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200697 for (i = 0; i < rtd->num_codecs; i++) {
698 codec_dai = rtd->codec_dais[i];
699 if (!codec_dai->active)
700 codec_dai->rate = 0;
701 }
Sascha Hauer25b76792011-08-17 09:20:01 +0200702
Ramesh Babuae116012014-10-15 12:34:59 +0530703 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
704
Liam Girdwoodddee6272011-06-09 14:45:53 +0100705 if (cpu_dai->driver->ops->shutdown)
706 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
707
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200708 for (i = 0; i < rtd->num_codecs; i++) {
709 codec_dai = rtd->codec_dais[i];
710 if (codec_dai->driver->ops->shutdown)
711 codec_dai->driver->ops->shutdown(substream, codec_dai);
712 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100713
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000714 if (rtd->dai_link->ops->shutdown)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100715 rtd->dai_link->ops->shutdown(substream);
716
Kuninori Morimotob8135862017-10-11 01:37:23 +0000717 for_each_rtdcom(rtd, rtdcom) {
718 component = rtdcom->component;
719
Kuninori Morimotob8135862017-10-11 01:37:23 +0000720 if (!component->driver->ops ||
721 !component->driver->ops->close)
722 continue;
723
724 component->driver->ops->close(substream);
725 }
726
Liam Girdwoodddee6272011-06-09 14:45:53 +0100727 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100728 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300729 /* powered down playback stream now */
730 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800731 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800732 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300733 } else {
734 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600735 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100736 queue_delayed_work(system_power_efficient_wq,
737 &rtd->delayed_work,
738 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300739 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100740 } else {
741 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800742 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000743 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100744 }
745
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100746 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000747
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000748 for_each_rtdcom(rtd, rtdcom) {
749 component = rtdcom->component;
Sanyog Kale3f809782016-01-05 17:14:49 +0530750
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000751 pm_runtime_mark_last_busy(component->dev);
752 pm_runtime_put_autosuspend(component->dev);
Sanyog Kale3f809782016-01-05 17:14:49 +0530753 }
754
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200755 for (i = 0; i < rtd->num_codecs; i++) {
756 if (!rtd->codec_dais[i]->active)
757 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
758 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800759 if (!cpu_dai->active)
760 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000761
Liam Girdwoodddee6272011-06-09 14:45:53 +0100762 return 0;
763}
764
765/*
766 * Called by ALSA when the PCM substream is prepared, can set format, sample
767 * rate, etc. This function is non atomic and can be called multiple times,
768 * it can refer to the runtime info.
769 */
770static int soc_pcm_prepare(struct snd_pcm_substream *substream)
771{
772 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000773 struct snd_soc_component *component;
774 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100775 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200776 struct snd_soc_dai *codec_dai;
777 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100778
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100779 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100780
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000781 if (rtd->dai_link->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100782 ret = rtd->dai_link->ops->prepare(substream);
783 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000784 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
785 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100786 goto out;
787 }
788 }
789
Kuninori Morimotob8135862017-10-11 01:37:23 +0000790 for_each_rtdcom(rtd, rtdcom) {
791 component = rtdcom->component;
792
Kuninori Morimotob8135862017-10-11 01:37:23 +0000793 if (!component->driver->ops ||
794 !component->driver->ops->prepare)
795 continue;
796
797 ret = component->driver->ops->prepare(substream);
798 if (ret < 0) {
799 dev_err(component->dev,
800 "ASoC: platform prepare error: %d\n", ret);
801 goto out;
802 }
803 }
804
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200805 for (i = 0; i < rtd->num_codecs; i++) {
806 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000807 if (codec_dai->driver->ops->prepare) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200808 ret = codec_dai->driver->ops->prepare(substream,
809 codec_dai);
810 if (ret < 0) {
811 dev_err(codec_dai->dev,
Jarkko Nikula90cc7f12014-12-23 11:04:41 +0200812 "ASoC: codec DAI prepare error: %d\n",
813 ret);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200814 goto out;
815 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100816 }
817 }
818
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000819 if (cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100820 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
821 if (ret < 0) {
Jarkko Nikula90cc7f12014-12-23 11:04:41 +0200822 dev_err(cpu_dai->dev,
823 "ASoC: cpu DAI prepare error: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100824 goto out;
825 }
826 }
827
828 /* cancel any delayed stream shutdown that is pending */
829 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600830 rtd->pop_wait) {
831 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100832 cancel_delayed_work(&rtd->delayed_work);
833 }
834
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000835 snd_soc_dapm_stream_event(rtd, substream->stream,
836 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100837
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200838 for (i = 0; i < rtd->num_codecs; i++)
839 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
840 substream->stream);
Ramesh Babuae116012014-10-15 12:34:59 +0530841 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100842
843out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100844 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100845 return ret;
846}
847
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200848static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
849 unsigned int mask)
850{
851 struct snd_interval *interval;
852 int channels = hweight_long(mask);
853
854 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
855 interval->min = channels;
856 interval->max = channels;
857}
858
Benoit Cousson93e69582014-07-08 23:19:38 +0200859int soc_dai_hw_params(struct snd_pcm_substream *substream,
860 struct snd_pcm_hw_params *params,
861 struct snd_soc_dai *dai)
862{
863 int ret;
864
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000865 if (dai->driver->ops->hw_params) {
Benoit Cousson93e69582014-07-08 23:19:38 +0200866 ret = dai->driver->ops->hw_params(substream, params, dai);
867 if (ret < 0) {
868 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
869 dai->name, ret);
870 return ret;
871 }
872 }
873
874 return 0;
875}
876
Liam Girdwoodddee6272011-06-09 14:45:53 +0100877/*
878 * Called by ALSA when the hardware params are set by application. This
879 * function can also be called multiple times and can allocate buffers
880 * (using snd_pcm_lib_* ). It's non-atomic.
881 */
882static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
883 struct snd_pcm_hw_params *params)
884{
885 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000886 struct snd_soc_component *component;
887 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100888 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000889 int i, ret = 0, __ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100890
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100891 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000892 if (rtd->dai_link->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100893 ret = rtd->dai_link->ops->hw_params(substream, params);
894 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000895 dev_err(rtd->card->dev, "ASoC: machine hw_params"
896 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100897 goto out;
898 }
899 }
900
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200901 for (i = 0; i < rtd->num_codecs; i++) {
902 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
903 struct snd_pcm_hw_params codec_params;
904
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200905 /*
906 * Skip CODECs which don't support the current stream type,
907 * the idea being that if a CODEC is not used for the currently
908 * set up transfer direction, it should not need to be
909 * configured, especially since the configuration used might
910 * not even be supported by that CODEC. There may be cases
911 * however where a CODEC needs to be set up although it is
912 * actually not being used for the transfer, e.g. if a
913 * capture-only CODEC is acting as an LRCLK and/or BCLK master
914 * for the DAI link including a playback-only CODEC.
915 * If this becomes necessary, we will have to augment the
916 * machine driver setup with information on how to act, so
917 * we can do the right thing here.
918 */
919 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
920 continue;
921
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200922 /* copy params for each codec */
923 codec_params = *params;
924
925 /* fixup params based on TDM slot masks */
926 if (codec_dai->tx_mask)
927 soc_pcm_codec_params_fixup(&codec_params,
928 codec_dai->tx_mask);
929 if (codec_dai->rx_mask)
930 soc_pcm_codec_params_fixup(&codec_params,
931 codec_dai->rx_mask);
932
Benoit Cousson93e69582014-07-08 23:19:38 +0200933 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
934 if(ret < 0)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100935 goto codec_err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200936
937 codec_dai->rate = params_rate(&codec_params);
938 codec_dai->channels = params_channels(&codec_params);
939 codec_dai->sample_bits = snd_pcm_format_physical_width(
940 params_format(&codec_params));
Liam Girdwoodddee6272011-06-09 14:45:53 +0100941 }
942
Benoit Cousson93e69582014-07-08 23:19:38 +0200943 ret = soc_dai_hw_params(substream, params, cpu_dai);
944 if (ret < 0)
945 goto interface_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100946
Kuninori Morimotob8135862017-10-11 01:37:23 +0000947 ret = 0;
948 for_each_rtdcom(rtd, rtdcom) {
949 component = rtdcom->component;
950
Kuninori Morimotob8135862017-10-11 01:37:23 +0000951 if (!component->driver->ops ||
952 !component->driver->ops->hw_params)
953 continue;
954
955 __ret = component->driver->ops->hw_params(substream, params);
956 if (__ret < 0) {
957 dev_err(component->dev,
958 "ASoC: %s hw params failed: %d\n",
959 component->name, ret);
960 ret = __ret;
961 }
962 }
963 if (ret < 0)
964 goto component_err;
965
Nicolin Chen3635bf02013-11-13 18:56:24 +0800966 /* store the parameters for each DAIs */
Dong Aisheng17841022011-08-29 17:15:14 +0800967 cpu_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800968 cpu_dai->channels = params_channels(params);
969 cpu_dai->sample_bits =
970 snd_pcm_format_physical_width(params_format(params));
971
jiada wang957ce0c2017-09-20 15:25:30 +0900972 ret = soc_pcm_params_symmetry(substream, params);
973 if (ret)
Kuninori Morimotob8135862017-10-11 01:37:23 +0000974 goto component_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100975out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100976 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100977 return ret;
978
Kuninori Morimotob8135862017-10-11 01:37:23 +0000979component_err:
980 for_each_rtdcom(rtd, rtdcom) {
981 component = rtdcom->component;
982
Kuninori Morimotob8135862017-10-11 01:37:23 +0000983 if (!component->driver->ops ||
984 !component->driver->ops->hw_free)
985 continue;
986
987 component->driver->ops->hw_free(substream);
988 }
989
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000990 if (cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100991 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
992
993interface_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200994 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100995
996codec_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200997 while (--i >= 0) {
998 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000999 if (codec_dai->driver->ops->hw_free)
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001000 codec_dai->driver->ops->hw_free(substream, codec_dai);
1001 codec_dai->rate = 0;
1002 }
1003
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +00001004 if (rtd->dai_link->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001005 rtd->dai_link->ops->hw_free(substream);
1006
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001007 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001008 return ret;
1009}
1010
1011/*
1012 * Frees resources allocated by hw_params, can be called multiple times
1013 */
1014static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1015{
1016 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001017 struct snd_soc_component *component;
1018 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001019 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001020 struct snd_soc_dai *codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +08001021 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001022 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001023
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001024 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001025
Nicolin Chend3383422013-11-20 18:37:09 +08001026 /* clear the corresponding DAIs parameters when going to be inactive */
1027 if (cpu_dai->active == 1) {
1028 cpu_dai->rate = 0;
1029 cpu_dai->channels = 0;
1030 cpu_dai->sample_bits = 0;
1031 }
1032
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001033 for (i = 0; i < rtd->num_codecs; i++) {
1034 codec_dai = rtd->codec_dais[i];
1035 if (codec_dai->active == 1) {
1036 codec_dai->rate = 0;
1037 codec_dai->channels = 0;
1038 codec_dai->sample_bits = 0;
1039 }
Nicolin Chend3383422013-11-20 18:37:09 +08001040 }
1041
Liam Girdwoodddee6272011-06-09 14:45:53 +01001042 /* apply codec digital mute */
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001043 for (i = 0; i < rtd->num_codecs; i++) {
1044 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
1045 (!playback && rtd->codec_dais[i]->capture_active == 1))
1046 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
1047 substream->stream);
1048 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001049
1050 /* free any machine hw params */
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +00001051 if (rtd->dai_link->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001052 rtd->dai_link->ops->hw_free(substream);
1053
Kuninori Morimotob8135862017-10-11 01:37:23 +00001054 /* free any component resources */
1055 for_each_rtdcom(rtd, rtdcom) {
1056 component = rtdcom->component;
1057
Kuninori Morimotob8135862017-10-11 01:37:23 +00001058 if (!component->driver->ops ||
1059 !component->driver->ops->hw_free)
1060 continue;
1061
1062 component->driver->ops->hw_free(substream);
1063 }
1064
Liam Girdwoodddee6272011-06-09 14:45:53 +01001065 /* now free hw params for the DAIs */
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001066 for (i = 0; i < rtd->num_codecs; i++) {
1067 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001068 if (codec_dai->driver->ops->hw_free)
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001069 codec_dai->driver->ops->hw_free(substream, codec_dai);
1070 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001071
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001072 if (cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001073 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1074
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001075 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001076 return 0;
1077}
1078
1079static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1080{
1081 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001082 struct snd_soc_component *component;
1083 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001084 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001085 struct snd_soc_dai *codec_dai;
1086 int i, ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001087
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001088 for (i = 0; i < rtd->num_codecs; i++) {
1089 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001090 if (codec_dai->driver->ops->trigger) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001091 ret = codec_dai->driver->ops->trigger(substream,
1092 cmd, codec_dai);
1093 if (ret < 0)
1094 return ret;
1095 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001096 }
1097
Kuninori Morimotob8135862017-10-11 01:37:23 +00001098 for_each_rtdcom(rtd, rtdcom) {
1099 component = rtdcom->component;
1100
Kuninori Morimotob8135862017-10-11 01:37:23 +00001101 if (!component->driver->ops ||
1102 !component->driver->ops->trigger)
1103 continue;
1104
1105 ret = component->driver->ops->trigger(substream, cmd);
1106 if (ret < 0)
1107 return ret;
1108 }
1109
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001110 if (cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +01001111 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1112 if (ret < 0)
1113 return ret;
1114 }
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001115
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +00001116 if (rtd->dai_link->ops->trigger) {
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001117 ret = rtd->dai_link->ops->trigger(substream, cmd);
1118 if (ret < 0)
1119 return ret;
1120 }
1121
Liam Girdwoodddee6272011-06-09 14:45:53 +01001122 return 0;
1123}
1124
Mark Brown45c0a182012-05-09 21:46:27 +01001125static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1126 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001127{
1128 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001129 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001130 struct snd_soc_dai *codec_dai;
1131 int i, ret;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001132
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001133 for (i = 0; i < rtd->num_codecs; i++) {
1134 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001135 if (codec_dai->driver->ops->bespoke_trigger) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001136 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1137 cmd, codec_dai);
1138 if (ret < 0)
1139 return ret;
1140 }
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001141 }
1142
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001143 if (cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001144 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1145 if (ret < 0)
1146 return ret;
1147 }
1148 return 0;
1149}
Liam Girdwoodddee6272011-06-09 14:45:53 +01001150/*
1151 * soc level wrapper for pointer callback
Charles Keepaxef050be2018-04-24 16:39:02 +01001152 * If cpu_dai, codec_dai, component driver has the delay callback, then
Liam Girdwoodddee6272011-06-09 14:45:53 +01001153 * the runtime->delay will be updated accordingly.
1154 */
1155static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1156{
1157 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001158 struct snd_soc_component *component;
1159 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001160 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001161 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001162 struct snd_pcm_runtime *runtime = substream->runtime;
1163 snd_pcm_uframes_t offset = 0;
1164 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001165 snd_pcm_sframes_t codec_delay = 0;
1166 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001167
Kuninori Morimotob8135862017-10-11 01:37:23 +00001168 for_each_rtdcom(rtd, rtdcom) {
1169 component = rtdcom->component;
1170
Kuninori Morimotob8135862017-10-11 01:37:23 +00001171 if (!component->driver->ops ||
1172 !component->driver->ops->pointer)
1173 continue;
1174
1175 /* FIXME: use 1st pointer */
1176 offset = component->driver->ops->pointer(substream);
1177 break;
1178 }
1179
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001180 if (cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001181 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1182
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001183 for (i = 0; i < rtd->num_codecs; i++) {
1184 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001185 if (codec_dai->driver->ops->delay)
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001186 codec_delay = max(codec_delay,
1187 codec_dai->driver->ops->delay(substream,
1188 codec_dai));
1189 }
1190 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001191
Liam Girdwoodddee6272011-06-09 14:45:53 +01001192 runtime->delay = delay;
1193
1194 return offset;
1195}
1196
Liam Girdwood01d75842012-04-25 12:12:49 +01001197/* connect a FE and BE */
1198static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1199 struct snd_soc_pcm_runtime *be, int stream)
1200{
1201 struct snd_soc_dpcm *dpcm;
1202
1203 /* only add new dpcms */
1204 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1205 if (dpcm->be == be && dpcm->fe == fe)
1206 return 0;
1207 }
1208
1209 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1210 if (!dpcm)
1211 return -ENOMEM;
1212
1213 dpcm->be = be;
1214 dpcm->fe = fe;
1215 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1216 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1217 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1218 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1219
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001220 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001221 stream ? "capture" : "playback", fe->dai_link->name,
1222 stream ? "<-" : "->", be->dai_link->name);
1223
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001224#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02001225 if (fe->debugfs_dpcm_root)
1226 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1227 fe->debugfs_dpcm_root, &dpcm->state);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001228#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001229 return 1;
1230}
1231
1232/* reparent a BE onto another FE */
1233static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1234 struct snd_soc_pcm_runtime *be, int stream)
1235{
1236 struct snd_soc_dpcm *dpcm;
1237 struct snd_pcm_substream *fe_substream, *be_substream;
1238
1239 /* reparent if BE is connected to other FEs */
1240 if (!be->dpcm[stream].users)
1241 return;
1242
1243 be_substream = snd_soc_dpcm_get_substream(be, stream);
1244
1245 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1246 if (dpcm->fe == fe)
1247 continue;
1248
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001249 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001250 stream ? "capture" : "playback",
1251 dpcm->fe->dai_link->name,
1252 stream ? "<-" : "->", dpcm->be->dai_link->name);
1253
1254 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1255 be_substream->runtime = fe_substream->runtime;
1256 break;
1257 }
1258}
1259
1260/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001261void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001262{
1263 struct snd_soc_dpcm *dpcm, *d;
1264
1265 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001266 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001267 stream ? "capture" : "playback",
1268 dpcm->be->dai_link->name);
1269
1270 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1271 continue;
1272
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001273 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001274 stream ? "capture" : "playback", fe->dai_link->name,
1275 stream ? "<-" : "->", dpcm->be->dai_link->name);
1276
1277 /* BEs still alive need new FE */
1278 dpcm_be_reparent(fe, dpcm->be, stream);
1279
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001280#ifdef CONFIG_DEBUG_FS
1281 debugfs_remove(dpcm->debugfs_state);
1282#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001283 list_del(&dpcm->list_be);
1284 list_del(&dpcm->list_fe);
1285 kfree(dpcm);
1286 }
1287}
1288
1289/* get BE for DAI widget and stream */
1290static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1291 struct snd_soc_dapm_widget *widget, int stream)
1292{
1293 struct snd_soc_pcm_runtime *be;
Mengdong Lin1a497982015-11-18 02:34:11 -05001294 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001295
Liam Girdwood3c146462018-03-14 20:43:51 +00001296 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1297
Liam Girdwood01d75842012-04-25 12:12:49 +01001298 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001299 list_for_each_entry(be, &card->rtd_list, list) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001300
Liam Girdwood35ea0652012-06-05 19:26:59 +01001301 if (!be->dai_link->no_pcm)
1302 continue;
1303
Liam Girdwood3c146462018-03-14 20:43:51 +00001304 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1305 be->cpu_dai->playback_widget ?
1306 be->cpu_dai->playback_widget->name : "(not set)");
1307
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001308 if (be->cpu_dai->playback_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001309 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001310
Mengdong Lin1a497982015-11-18 02:34:11 -05001311 for (i = 0; i < be->num_codecs; i++) {
1312 struct snd_soc_dai *dai = be->codec_dais[i];
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001313 if (dai->playback_widget == widget)
1314 return be;
1315 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001316 }
1317 } else {
1318
Mengdong Lin1a497982015-11-18 02:34:11 -05001319 list_for_each_entry(be, &card->rtd_list, list) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001320
Liam Girdwood35ea0652012-06-05 19:26:59 +01001321 if (!be->dai_link->no_pcm)
1322 continue;
1323
Liam Girdwood3c146462018-03-14 20:43:51 +00001324 dev_dbg(card->dev, "ASoC: try BE %s\n",
1325 be->cpu_dai->capture_widget ?
1326 be->cpu_dai->capture_widget->name : "(not set)");
1327
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001328 if (be->cpu_dai->capture_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001329 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001330
Mengdong Lin1a497982015-11-18 02:34:11 -05001331 for (i = 0; i < be->num_codecs; i++) {
1332 struct snd_soc_dai *dai = be->codec_dais[i];
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001333 if (dai->capture_widget == widget)
1334 return be;
1335 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001336 }
1337 }
1338
Liam Girdwood3c146462018-03-14 20:43:51 +00001339 /* dai link name and stream name set correctly ? */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001340 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001341 stream ? "capture" : "playback", widget->name);
1342 return NULL;
1343}
1344
1345static inline struct snd_soc_dapm_widget *
Benoit Cousson37018612014-04-24 14:01:45 +02001346 dai_get_widget(struct snd_soc_dai *dai, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001347{
1348 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Benoit Cousson37018612014-04-24 14:01:45 +02001349 return dai->playback_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001350 else
Benoit Cousson37018612014-04-24 14:01:45 +02001351 return dai->capture_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001352}
1353
1354static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1355 struct snd_soc_dapm_widget *widget)
1356{
1357 int i;
1358
1359 for (i = 0; i < list->num_widgets; i++) {
1360 if (widget == list->widgets[i])
1361 return 1;
1362 }
1363
1364 return 0;
1365}
1366
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001367static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1368 enum snd_soc_dapm_direction dir)
1369{
1370 struct snd_soc_card *card = widget->dapm->card;
1371 struct snd_soc_pcm_runtime *rtd;
1372 int i;
1373
1374 if (dir == SND_SOC_DAPM_DIR_OUT) {
1375 list_for_each_entry(rtd, &card->rtd_list, list) {
1376 if (!rtd->dai_link->no_pcm)
1377 continue;
1378
1379 if (rtd->cpu_dai->playback_widget == widget)
1380 return true;
1381
1382 for (i = 0; i < rtd->num_codecs; ++i) {
1383 struct snd_soc_dai *dai = rtd->codec_dais[i];
1384 if (dai->playback_widget == widget)
1385 return true;
1386 }
1387 }
1388 } else { /* SND_SOC_DAPM_DIR_IN */
1389 list_for_each_entry(rtd, &card->rtd_list, list) {
1390 if (!rtd->dai_link->no_pcm)
1391 continue;
1392
1393 if (rtd->cpu_dai->capture_widget == widget)
1394 return true;
1395
1396 for (i = 0; i < rtd->num_codecs; ++i) {
1397 struct snd_soc_dai *dai = rtd->codec_dais[i];
1398 if (dai->capture_widget == widget)
1399 return true;
1400 }
1401 }
1402 }
1403
1404 return false;
1405}
1406
Liam Girdwood23607022014-01-17 17:03:55 +00001407int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001408 int stream, struct snd_soc_dapm_widget_list **list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001409{
1410 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001411 int paths;
1412
Liam Girdwood01d75842012-04-25 12:12:49 +01001413 /* get number of valid DAI paths and their widgets */
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001414 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001415 dpcm_end_walk_at_be);
Liam Girdwood01d75842012-04-25 12:12:49 +01001416
Liam Girdwood103d84a2012-11-19 14:39:15 +00001417 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001418 stream ? "capture" : "playback");
1419
Liam Girdwood01d75842012-04-25 12:12:49 +01001420 return paths;
1421}
1422
Liam Girdwood01d75842012-04-25 12:12:49 +01001423static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1424 struct snd_soc_dapm_widget_list **list_)
1425{
1426 struct snd_soc_dpcm *dpcm;
1427 struct snd_soc_dapm_widget_list *list = *list_;
1428 struct snd_soc_dapm_widget *widget;
1429 int prune = 0;
1430
1431 /* Destroy any old FE <--> BE connections */
1432 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001433 unsigned int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001434
1435 /* is there a valid CPU DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001436 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001437
1438 /* prune the BE if it's no longer in our active list */
1439 if (widget && widget_in_list(list, widget))
1440 continue;
1441
1442 /* is there a valid CODEC DAI widget for this BE */
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001443 for (i = 0; i < dpcm->be->num_codecs; i++) {
1444 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1445 widget = dai_get_widget(dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001446
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001447 /* prune the BE if it's no longer in our active list */
1448 if (widget && widget_in_list(list, widget))
1449 continue;
1450 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001451
Liam Girdwood103d84a2012-11-19 14:39:15 +00001452 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001453 stream ? "capture" : "playback",
1454 dpcm->be->dai_link->name, fe->dai_link->name);
1455 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1456 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1457 prune++;
1458 }
1459
Liam Girdwood103d84a2012-11-19 14:39:15 +00001460 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001461 return prune;
1462}
1463
1464static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1465 struct snd_soc_dapm_widget_list **list_)
1466{
1467 struct snd_soc_card *card = fe->card;
1468 struct snd_soc_dapm_widget_list *list = *list_;
1469 struct snd_soc_pcm_runtime *be;
1470 int i, new = 0, err;
1471
1472 /* Create any new FE <--> BE connections */
1473 for (i = 0; i < list->num_widgets; i++) {
1474
Mark Brown46162742013-06-05 19:36:11 +01001475 switch (list->widgets[i]->id) {
1476 case snd_soc_dapm_dai_in:
Koro Chenc5b85402015-07-06 10:02:10 +08001477 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1478 continue;
1479 break;
Mark Brown46162742013-06-05 19:36:11 +01001480 case snd_soc_dapm_dai_out:
Koro Chenc5b85402015-07-06 10:02:10 +08001481 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1482 continue;
Mark Brown46162742013-06-05 19:36:11 +01001483 break;
1484 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001485 continue;
Mark Brown46162742013-06-05 19:36:11 +01001486 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001487
1488 /* is there a valid BE rtd for this widget */
1489 be = dpcm_get_be(card, list->widgets[i], stream);
1490 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001491 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001492 list->widgets[i]->name);
1493 continue;
1494 }
1495
1496 /* make sure BE is a real BE */
1497 if (!be->dai_link->no_pcm)
1498 continue;
1499
1500 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001501 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001502 continue;
1503
1504 /* newly connected FE and BE */
1505 err = dpcm_be_connect(fe, be, stream);
1506 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001507 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001508 list->widgets[i]->name);
1509 break;
1510 } else if (err == 0) /* already connected */
1511 continue;
1512
1513 /* new */
1514 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1515 new++;
1516 }
1517
Liam Girdwood103d84a2012-11-19 14:39:15 +00001518 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001519 return new;
1520}
1521
1522/*
1523 * Find the corresponding BE DAIs that source or sink audio to this
1524 * FE substream.
1525 */
Liam Girdwood23607022014-01-17 17:03:55 +00001526int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001527 int stream, struct snd_soc_dapm_widget_list **list, int new)
1528{
1529 if (new)
1530 return dpcm_add_paths(fe, stream, list);
1531 else
1532 return dpcm_prune_paths(fe, stream, list);
1533}
1534
Liam Girdwood23607022014-01-17 17:03:55 +00001535void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001536{
1537 struct snd_soc_dpcm *dpcm;
1538
1539 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1540 dpcm->be->dpcm[stream].runtime_update =
1541 SND_SOC_DPCM_UPDATE_NO;
1542}
1543
1544static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1545 int stream)
1546{
1547 struct snd_soc_dpcm *dpcm;
1548
1549 /* disable any enabled and non active backends */
1550 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1551
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
1556 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001557 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001558 stream ? "capture" : "playback",
1559 be->dpcm[stream].state);
1560
1561 if (--be->dpcm[stream].users != 0)
1562 continue;
1563
1564 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1565 continue;
1566
1567 soc_pcm_close(be_substream);
1568 be_substream->runtime = NULL;
1569 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1570 }
1571}
1572
Liam Girdwood23607022014-01-17 17:03:55 +00001573int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001574{
1575 struct snd_soc_dpcm *dpcm;
1576 int err, count = 0;
1577
1578 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1579 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1580
1581 struct snd_soc_pcm_runtime *be = dpcm->be;
1582 struct snd_pcm_substream *be_substream =
1583 snd_soc_dpcm_get_substream(be, stream);
1584
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001585 if (!be_substream) {
1586 dev_err(be->dev, "ASoC: no backend %s stream\n",
1587 stream ? "capture" : "playback");
1588 continue;
1589 }
1590
Liam Girdwood01d75842012-04-25 12:12:49 +01001591 /* is this op for this BE ? */
1592 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1593 continue;
1594
1595 /* first time the dpcm is open ? */
1596 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001597 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001598 stream ? "capture" : "playback",
1599 be->dpcm[stream].state);
1600
1601 if (be->dpcm[stream].users++ != 0)
1602 continue;
1603
1604 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1605 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1606 continue;
1607
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001608 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1609 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001610
1611 be_substream->runtime = be->dpcm[stream].runtime;
1612 err = soc_pcm_open(be_substream);
1613 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001614 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001615 be->dpcm[stream].users--;
1616 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001617 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001618 stream ? "capture" : "playback",
1619 be->dpcm[stream].state);
1620
1621 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1622 goto unwind;
1623 }
1624
1625 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1626 count++;
1627 }
1628
1629 return count;
1630
1631unwind:
1632 /* disable any enabled and non active backends */
1633 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1634 struct snd_soc_pcm_runtime *be = dpcm->be;
1635 struct snd_pcm_substream *be_substream =
1636 snd_soc_dpcm_get_substream(be, stream);
1637
1638 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1639 continue;
1640
1641 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001642 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001643 stream ? "capture" : "playback",
1644 be->dpcm[stream].state);
1645
1646 if (--be->dpcm[stream].users != 0)
1647 continue;
1648
1649 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1650 continue;
1651
1652 soc_pcm_close(be_substream);
1653 be_substream->runtime = NULL;
1654 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1655 }
1656
1657 return err;
1658}
1659
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001660static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001661 struct snd_soc_pcm_stream *stream,
1662 u64 formats)
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001663{
1664 runtime->hw.rate_min = stream->rate_min;
1665 runtime->hw.rate_max = stream->rate_max;
1666 runtime->hw.channels_min = stream->channels_min;
1667 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001668 if (runtime->hw.formats)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001669 runtime->hw.formats &= formats & stream->formats;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001670 else
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001671 runtime->hw.formats = formats & stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001672 runtime->hw.rates = stream->rates;
1673}
1674
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001675static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1676{
1677 struct snd_soc_pcm_runtime *fe = substream->private_data;
1678 struct snd_soc_dpcm *dpcm;
1679 u64 formats = ULLONG_MAX;
1680 int stream = substream->stream;
1681
1682 if (!fe->dai_link->dpcm_merged_format)
1683 return formats;
1684
1685 /*
1686 * It returns merged BE codec format
1687 * if FE want to use it (= dpcm_merged_format)
1688 */
1689
1690 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1691 struct snd_soc_pcm_runtime *be = dpcm->be;
1692 struct snd_soc_dai_driver *codec_dai_drv;
1693 struct snd_soc_pcm_stream *codec_stream;
1694 int i;
1695
1696 for (i = 0; i < be->num_codecs; i++) {
1697 codec_dai_drv = be->codec_dais[i]->driver;
1698 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1699 codec_stream = &codec_dai_drv->playback;
1700 else
1701 codec_stream = &codec_dai_drv->capture;
1702
1703 formats &= codec_stream->formats;
1704 }
1705 }
1706
1707 return formats;
1708}
1709
Mark Brown45c0a182012-05-09 21:46:27 +01001710static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001711{
1712 struct snd_pcm_runtime *runtime = substream->runtime;
1713 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1714 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1715 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001716 u64 format = dpcm_runtime_base_format(substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001717
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001718 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001719 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001720 else
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001721 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
Liam Girdwood01d75842012-04-25 12:12:49 +01001722}
1723
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001724static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1725
1726/* Set FE's runtime_update state; the state is protected via PCM stream lock
1727 * for avoiding the race with trigger callback.
1728 * If the state is unset and a trigger is pending while the previous operation,
1729 * process the pending trigger action here.
1730 */
1731static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1732 int stream, enum snd_soc_dpcm_update state)
1733{
1734 struct snd_pcm_substream *substream =
1735 snd_soc_dpcm_get_substream(fe, stream);
1736
1737 snd_pcm_stream_lock_irq(substream);
1738 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1739 dpcm_fe_dai_do_trigger(substream,
1740 fe->dpcm[stream].trigger_pending - 1);
1741 fe->dpcm[stream].trigger_pending = 0;
1742 }
1743 fe->dpcm[stream].runtime_update = state;
1744 snd_pcm_stream_unlock_irq(substream);
1745}
1746
PC Liao906c7d62015-12-11 11:33:51 +08001747static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1748 int stream)
1749{
1750 struct snd_soc_dpcm *dpcm;
1751 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1752 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1753 int err;
1754
1755 /* apply symmetry for FE */
1756 if (soc_pcm_has_symmetry(fe_substream))
1757 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1758
1759 /* Symmetry only applies if we've got an active stream. */
1760 if (fe_cpu_dai->active) {
1761 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1762 if (err < 0)
1763 return err;
1764 }
1765
1766 /* apply symmetry for BE */
1767 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1768 struct snd_soc_pcm_runtime *be = dpcm->be;
1769 struct snd_pcm_substream *be_substream =
1770 snd_soc_dpcm_get_substream(be, stream);
1771 struct snd_soc_pcm_runtime *rtd = be_substream->private_data;
1772 int i;
1773
Jeeja KPf1176612016-09-06 14:17:55 +05301774 if (rtd->dai_link->be_hw_params_fixup)
1775 continue;
1776
PC Liao906c7d62015-12-11 11:33:51 +08001777 if (soc_pcm_has_symmetry(be_substream))
1778 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1779
1780 /* Symmetry only applies if we've got an active stream. */
1781 if (rtd->cpu_dai->active) {
1782 err = soc_pcm_apply_symmetry(be_substream, rtd->cpu_dai);
1783 if (err < 0)
1784 return err;
1785 }
1786
1787 for (i = 0; i < rtd->num_codecs; i++) {
1788 if (rtd->codec_dais[i]->active) {
1789 err = soc_pcm_apply_symmetry(be_substream,
1790 rtd->codec_dais[i]);
1791 if (err < 0)
1792 return err;
1793 }
1794 }
1795 }
1796
1797 return 0;
1798}
1799
Liam Girdwood01d75842012-04-25 12:12:49 +01001800static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1801{
1802 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1803 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1804 int stream = fe_substream->stream, ret = 0;
1805
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001806 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001807
1808 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1809 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001810 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001811 goto be_err;
1812 }
1813
Liam Girdwood103d84a2012-11-19 14:39:15 +00001814 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001815
1816 /* start the DAI frontend */
1817 ret = soc_pcm_open(fe_substream);
1818 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001819 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001820 goto unwind;
1821 }
1822
1823 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1824
1825 dpcm_set_fe_runtime(fe_substream);
1826 snd_pcm_limit_hw_rates(runtime);
1827
PC Liao906c7d62015-12-11 11:33:51 +08001828 ret = dpcm_apply_symmetry(fe_substream, stream);
1829 if (ret < 0) {
1830 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1831 ret);
1832 goto unwind;
1833 }
1834
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001835 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001836 return 0;
1837
1838unwind:
1839 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1840be_err:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001841 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001842 return ret;
1843}
1844
Liam Girdwood23607022014-01-17 17:03:55 +00001845int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001846{
1847 struct snd_soc_dpcm *dpcm;
1848
1849 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1850 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1851
1852 struct snd_soc_pcm_runtime *be = dpcm->be;
1853 struct snd_pcm_substream *be_substream =
1854 snd_soc_dpcm_get_substream(be, stream);
1855
1856 /* is this op for this BE ? */
1857 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1858 continue;
1859
1860 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001861 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001862 stream ? "capture" : "playback",
1863 be->dpcm[stream].state);
1864
1865 if (--be->dpcm[stream].users != 0)
1866 continue;
1867
1868 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Kai Chieh Chuang9c0ac702018-05-28 10:18:18 +08001869 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1870 soc_pcm_hw_free(be_substream);
1871 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1872 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001873
Liam Girdwood103d84a2012-11-19 14:39:15 +00001874 dev_dbg(be->dev, "ASoC: close BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001875 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001876
1877 soc_pcm_close(be_substream);
1878 be_substream->runtime = NULL;
1879
1880 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1881 }
1882 return 0;
1883}
1884
1885static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1886{
1887 struct snd_soc_pcm_runtime *fe = substream->private_data;
1888 int stream = substream->stream;
1889
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001890 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001891
1892 /* shutdown the BEs */
1893 dpcm_be_dai_shutdown(fe, substream->stream);
1894
Liam Girdwood103d84a2012-11-19 14:39:15 +00001895 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001896
1897 /* now shutdown the frontend */
1898 soc_pcm_close(substream);
1899
1900 /* run the stream event for each BE */
1901 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1902
1903 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001904 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001905 return 0;
1906}
1907
Liam Girdwood23607022014-01-17 17:03:55 +00001908int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001909{
1910 struct snd_soc_dpcm *dpcm;
1911
1912 /* only hw_params backends that are either sinks or sources
1913 * to this frontend DAI */
1914 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1915
1916 struct snd_soc_pcm_runtime *be = dpcm->be;
1917 struct snd_pcm_substream *be_substream =
1918 snd_soc_dpcm_get_substream(be, stream);
1919
1920 /* is this op for this BE ? */
1921 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1922 continue;
1923
1924 /* only free hw when no longer used - check all FEs */
1925 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1926 continue;
1927
Qiao Zhou36fba622014-12-03 10:13:43 +08001928 /* do not free hw if this BE is used by other FE */
1929 if (be->dpcm[stream].users > 1)
1930 continue;
1931
Liam Girdwood01d75842012-04-25 12:12:49 +01001932 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1933 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1934 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001935 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Vinod Koul5e82d2b2016-02-01 22:26:40 +05301936 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1937 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01001938 continue;
1939
Liam Girdwood103d84a2012-11-19 14:39:15 +00001940 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001941 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001942
1943 soc_pcm_hw_free(be_substream);
1944
1945 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1946 }
1947
1948 return 0;
1949}
1950
Mark Brown45c0a182012-05-09 21:46:27 +01001951static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001952{
1953 struct snd_soc_pcm_runtime *fe = substream->private_data;
1954 int err, stream = substream->stream;
1955
1956 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001957 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001958
Liam Girdwood103d84a2012-11-19 14:39:15 +00001959 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001960
1961 /* call hw_free on the frontend */
1962 err = soc_pcm_hw_free(substream);
1963 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001964 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001965 fe->dai_link->name);
1966
1967 /* only hw_params backends that are either sinks or sources
1968 * to this frontend DAI */
1969 err = dpcm_be_dai_hw_free(fe, stream);
1970
1971 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001972 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001973
1974 mutex_unlock(&fe->card->mutex);
1975 return 0;
1976}
1977
Liam Girdwood23607022014-01-17 17:03:55 +00001978int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001979{
1980 struct snd_soc_dpcm *dpcm;
1981 int ret;
1982
1983 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1984
1985 struct snd_soc_pcm_runtime *be = dpcm->be;
1986 struct snd_pcm_substream *be_substream =
1987 snd_soc_dpcm_get_substream(be, stream);
1988
1989 /* is this op for this BE ? */
1990 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1991 continue;
1992
Liam Girdwood01d75842012-04-25 12:12:49 +01001993 /* copy params for each dpcm */
1994 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1995 sizeof(struct snd_pcm_hw_params));
1996
1997 /* perform any hw_params fixups */
1998 if (be->dai_link->be_hw_params_fixup) {
1999 ret = be->dai_link->be_hw_params_fixup(be,
2000 &dpcm->hw_params);
2001 if (ret < 0) {
2002 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002003 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002004 ret);
2005 goto unwind;
2006 }
2007 }
2008
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002009 /* only allow hw_params() if no connected FEs are running */
2010 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2011 continue;
2012
2013 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2014 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2015 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2016 continue;
2017
2018 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002019 be->dai_link->name);
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002020
Liam Girdwood01d75842012-04-25 12:12:49 +01002021 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2022 if (ret < 0) {
2023 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002024 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002025 goto unwind;
2026 }
2027
2028 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2029 }
2030 return 0;
2031
2032unwind:
2033 /* disable any enabled and non active backends */
2034 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2035 struct snd_soc_pcm_runtime *be = dpcm->be;
2036 struct snd_pcm_substream *be_substream =
2037 snd_soc_dpcm_get_substream(be, stream);
2038
2039 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2040 continue;
2041
2042 /* only allow hw_free() if no connected FEs are running */
2043 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2044 continue;
2045
2046 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2047 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2048 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2049 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2050 continue;
2051
2052 soc_pcm_hw_free(be_substream);
2053 }
2054
2055 return ret;
2056}
2057
Mark Brown45c0a182012-05-09 21:46:27 +01002058static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2059 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01002060{
2061 struct snd_soc_pcm_runtime *fe = substream->private_data;
2062 int ret, stream = substream->stream;
2063
2064 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002065 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002066
2067 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2068 sizeof(struct snd_pcm_hw_params));
2069 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2070 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002071 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002072 goto out;
2073 }
2074
Liam Girdwood103d84a2012-11-19 14:39:15 +00002075 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002076 fe->dai_link->name, params_rate(params),
2077 params_channels(params), params_format(params));
2078
2079 /* call hw_params on the frontend */
2080 ret = soc_pcm_hw_params(substream, params);
2081 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002082 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002083 dpcm_be_dai_hw_free(fe, stream);
2084 } else
2085 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2086
2087out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002088 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002089 mutex_unlock(&fe->card->mutex);
2090 return ret;
2091}
2092
2093static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2094 struct snd_pcm_substream *substream, int cmd)
2095{
2096 int ret;
2097
Liam Girdwood103d84a2012-11-19 14:39:15 +00002098 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
彭东林94d215c2016-09-26 08:29:31 +00002099 dpcm->be->dai_link->name, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002100
2101 ret = soc_pcm_trigger(substream, cmd);
2102 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002103 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002104
2105 return ret;
2106}
2107
Liam Girdwood23607022014-01-17 17:03:55 +00002108int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01002109 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002110{
2111 struct snd_soc_dpcm *dpcm;
2112 int ret = 0;
2113
2114 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2115
2116 struct snd_soc_pcm_runtime *be = dpcm->be;
2117 struct snd_pcm_substream *be_substream =
2118 snd_soc_dpcm_get_substream(be, stream);
2119
2120 /* is this op for this BE ? */
2121 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2122 continue;
2123
2124 switch (cmd) {
2125 case SNDRV_PCM_TRIGGER_START:
2126 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2127 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2128 continue;
2129
2130 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2131 if (ret)
2132 return ret;
2133
2134 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2135 break;
2136 case SNDRV_PCM_TRIGGER_RESUME:
2137 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2138 continue;
2139
2140 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2141 if (ret)
2142 return ret;
2143
2144 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2145 break;
2146 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2147 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2148 continue;
2149
2150 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2151 if (ret)
2152 return ret;
2153
2154 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2155 break;
2156 case SNDRV_PCM_TRIGGER_STOP:
2157 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2158 continue;
2159
2160 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2161 continue;
2162
2163 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2164 if (ret)
2165 return ret;
2166
2167 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2168 break;
2169 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08002170 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01002171 continue;
2172
2173 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2174 continue;
2175
2176 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2177 if (ret)
2178 return ret;
2179
2180 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2181 break;
2182 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2183 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2184 continue;
2185
2186 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2187 continue;
2188
2189 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2190 if (ret)
2191 return ret;
2192
2193 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2194 break;
2195 }
2196 }
2197
2198 return ret;
2199}
2200EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2201
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002202static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002203{
2204 struct snd_soc_pcm_runtime *fe = substream->private_data;
2205 int stream = substream->stream, ret;
2206 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2207
2208 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2209
2210 switch (trigger) {
2211 case SND_SOC_DPCM_TRIGGER_PRE:
2212 /* call trigger on the frontend before the backend. */
2213
Liam Girdwood103d84a2012-11-19 14:39:15 +00002214 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002215 fe->dai_link->name, cmd);
2216
2217 ret = soc_pcm_trigger(substream, cmd);
2218 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002219 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002220 goto out;
2221 }
2222
2223 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2224 break;
2225 case SND_SOC_DPCM_TRIGGER_POST:
2226 /* call trigger on the frontend after the backend. */
2227
2228 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2229 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002230 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002231 goto out;
2232 }
2233
Liam Girdwood103d84a2012-11-19 14:39:15 +00002234 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002235 fe->dai_link->name, cmd);
2236
2237 ret = soc_pcm_trigger(substream, cmd);
2238 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002239 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2240 /* bespoke trigger() - handles both FE and BEs */
2241
Liam Girdwood103d84a2012-11-19 14:39:15 +00002242 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002243 fe->dai_link->name, cmd);
2244
2245 ret = soc_pcm_bespoke_trigger(substream, cmd);
2246 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002247 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002248 goto out;
2249 }
2250 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002251 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00002252 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01002253 fe->dai_link->name);
2254 ret = -EINVAL;
2255 goto out;
2256 }
2257
2258 switch (cmd) {
2259 case SNDRV_PCM_TRIGGER_START:
2260 case SNDRV_PCM_TRIGGER_RESUME:
2261 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2262 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2263 break;
2264 case SNDRV_PCM_TRIGGER_STOP:
2265 case SNDRV_PCM_TRIGGER_SUSPEND:
Liam Girdwood01d75842012-04-25 12:12:49 +01002266 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2267 break;
Patrick Lai9f169b92016-12-31 22:44:39 -08002268 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2269 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2270 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002271 }
2272
2273out:
2274 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2275 return ret;
2276}
2277
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002278static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2279{
2280 struct snd_soc_pcm_runtime *fe = substream->private_data;
2281 int stream = substream->stream;
2282
2283 /* if FE's runtime_update is already set, we're in race;
2284 * process this trigger later at exit
2285 */
2286 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2287 fe->dpcm[stream].trigger_pending = cmd + 1;
2288 return 0; /* delayed, assuming it's successful */
2289 }
2290
2291 /* we're alone, let's trigger */
2292 return dpcm_fe_dai_do_trigger(substream, cmd);
2293}
2294
Liam Girdwood23607022014-01-17 17:03:55 +00002295int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002296{
2297 struct snd_soc_dpcm *dpcm;
2298 int ret = 0;
2299
2300 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2301
2302 struct snd_soc_pcm_runtime *be = dpcm->be;
2303 struct snd_pcm_substream *be_substream =
2304 snd_soc_dpcm_get_substream(be, stream);
2305
2306 /* is this op for this BE ? */
2307 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2308 continue;
2309
2310 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
Koro Chen95f444d2015-10-28 10:15:34 +08002311 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2312 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01002313 continue;
2314
Liam Girdwood103d84a2012-11-19 14:39:15 +00002315 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002316 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002317
2318 ret = soc_pcm_prepare(be_substream);
2319 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002320 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002321 ret);
2322 break;
2323 }
2324
2325 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2326 }
2327 return ret;
2328}
2329
Mark Brown45c0a182012-05-09 21:46:27 +01002330static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002331{
2332 struct snd_soc_pcm_runtime *fe = substream->private_data;
2333 int stream = substream->stream, ret = 0;
2334
2335 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2336
Liam Girdwood103d84a2012-11-19 14:39:15 +00002337 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002338
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002339 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002340
2341 /* there is no point preparing this FE if there are no BEs */
2342 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002343 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002344 fe->dai_link->name);
2345 ret = -EINVAL;
2346 goto out;
2347 }
2348
2349 ret = dpcm_be_dai_prepare(fe, substream->stream);
2350 if (ret < 0)
2351 goto out;
2352
2353 /* call prepare on the frontend */
2354 ret = soc_pcm_prepare(substream);
2355 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002356 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002357 fe->dai_link->name);
2358 goto out;
2359 }
2360
2361 /* run the stream event for each BE */
2362 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2363 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2364
2365out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002366 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002367 mutex_unlock(&fe->card->mutex);
2368
2369 return ret;
2370}
2371
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002372static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2373 unsigned int cmd, void *arg)
2374{
2375 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +00002376 struct snd_soc_component *component;
2377 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002378
Kuninori Morimotob8135862017-10-11 01:37:23 +00002379 for_each_rtdcom(rtd, rtdcom) {
2380 component = rtdcom->component;
2381
Kuninori Morimotob8135862017-10-11 01:37:23 +00002382 if (!component->driver->ops ||
2383 !component->driver->ops->ioctl)
2384 continue;
2385
2386 /* FIXME: use 1st ioctl */
2387 return component->driver->ops->ioctl(substream, cmd, arg);
2388 }
2389
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002390 return snd_pcm_lib_ioctl(substream, cmd, arg);
2391}
2392
Liam Girdwood618dae12012-04-25 12:12:51 +01002393static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2394{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002395 struct snd_pcm_substream *substream =
2396 snd_soc_dpcm_get_substream(fe, stream);
2397 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002398 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01002399
Liam Girdwood103d84a2012-11-19 14:39:15 +00002400 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002401 stream ? "capture" : "playback", fe->dai_link->name);
2402
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002403 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2404 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002405 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002406 fe->dai_link->name);
2407
2408 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2409 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002410 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002411 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002412 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002413 fe->dai_link->name);
2414
2415 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2416 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002417 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002418 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002419
2420 err = dpcm_be_dai_hw_free(fe, stream);
2421 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002422 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002423
2424 err = dpcm_be_dai_shutdown(fe, stream);
2425 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002426 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002427
2428 /* run the stream event for each BE */
2429 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2430
2431 return 0;
2432}
2433
2434static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2435{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002436 struct snd_pcm_substream *substream =
2437 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002438 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002439 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002440 int ret;
2441
Liam Girdwood103d84a2012-11-19 14:39:15 +00002442 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002443 stream ? "capture" : "playback", fe->dai_link->name);
2444
2445 /* Only start the BE if the FE is ready */
2446 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2447 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2448 return -EINVAL;
2449
2450 /* startup must always be called for new BEs */
2451 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002452 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002453 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01002454
2455 /* keep going if FE state is > open */
2456 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2457 return 0;
2458
2459 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002460 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002461 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01002462
2463 /* keep going if FE state is > hw_params */
2464 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2465 return 0;
2466
2467
2468 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002469 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002470 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002471
2472 /* run the stream event for each BE */
2473 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2474
2475 /* keep going if FE state is > prepare */
2476 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2477 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2478 return 0;
2479
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002480 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2481 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002482 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002483 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01002484
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002485 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2486 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002487 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002488 goto hw_free;
2489 }
2490 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002491 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002492 fe->dai_link->name);
2493
2494 ret = dpcm_be_dai_trigger(fe, stream,
2495 SNDRV_PCM_TRIGGER_START);
2496 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002497 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002498 goto hw_free;
2499 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002500 }
2501
2502 return 0;
2503
2504hw_free:
2505 dpcm_be_dai_hw_free(fe, stream);
2506close:
2507 dpcm_be_dai_shutdown(fe, stream);
2508disconnect:
2509 /* disconnect any non started BEs */
2510 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2511 struct snd_soc_pcm_runtime *be = dpcm->be;
2512 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2513 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2514 }
2515
2516 return ret;
2517}
2518
2519static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2520{
2521 int ret;
2522
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002523 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002524 ret = dpcm_run_update_startup(fe, stream);
2525 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002526 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002527 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002528
2529 return ret;
2530}
2531
2532static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2533{
2534 int ret;
2535
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002536 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002537 ret = dpcm_run_update_shutdown(fe, stream);
2538 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002539 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002540 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002541
2542 return ret;
2543}
2544
2545/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2546 * any DAI links.
2547 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002548int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002549{
Mengdong Lin1a497982015-11-18 02:34:11 -05002550 struct snd_soc_pcm_runtime *fe;
2551 int old, new, paths;
Liam Girdwood618dae12012-04-25 12:12:51 +01002552
Liam Girdwood618dae12012-04-25 12:12:51 +01002553 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Mengdong Lin1a497982015-11-18 02:34:11 -05002554 list_for_each_entry(fe, &card->rtd_list, list) {
Liam Girdwood618dae12012-04-25 12:12:51 +01002555 struct snd_soc_dapm_widget_list *list;
Liam Girdwood618dae12012-04-25 12:12:51 +01002556
2557 /* make sure link is FE */
2558 if (!fe->dai_link->dynamic)
2559 continue;
2560
2561 /* only check active links */
2562 if (!fe->cpu_dai->active)
2563 continue;
2564
2565 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002566 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002567 fe->dai_link->name);
2568
2569 /* skip if FE doesn't have playback capability */
Qiao Zhou075207d2014-11-17 16:02:57 +08002570 if (!fe->cpu_dai->driver->playback.channels_min
2571 || !fe->codec_dai->driver->playback.channels_min)
2572 goto capture;
2573
2574 /* skip if FE isn't currently playing */
2575 if (!fe->cpu_dai->playback_active
2576 || !fe->codec_dai->playback_active)
Liam Girdwood618dae12012-04-25 12:12:51 +01002577 goto capture;
2578
2579 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2580 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002581 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002582 fe->dai_link->name, "playback");
2583 mutex_unlock(&card->mutex);
2584 return paths;
2585 }
2586
2587 /* update any new playback paths */
2588 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2589 if (new) {
2590 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2591 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2592 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2593 }
2594
2595 /* update any old playback paths */
2596 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2597 if (old) {
2598 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2599 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2600 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2601 }
2602
Qiao Zhou7ed9de72014-06-04 19:42:06 +08002603 dpcm_path_put(&list);
Liam Girdwood618dae12012-04-25 12:12:51 +01002604capture:
2605 /* skip if FE doesn't have capture capability */
Qiao Zhou075207d2014-11-17 16:02:57 +08002606 if (!fe->cpu_dai->driver->capture.channels_min
2607 || !fe->codec_dai->driver->capture.channels_min)
2608 continue;
2609
2610 /* skip if FE isn't currently capturing */
2611 if (!fe->cpu_dai->capture_active
2612 || !fe->codec_dai->capture_active)
Liam Girdwood618dae12012-04-25 12:12:51 +01002613 continue;
2614
2615 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2616 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002617 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002618 fe->dai_link->name, "capture");
2619 mutex_unlock(&card->mutex);
2620 return paths;
2621 }
2622
2623 /* update any new capture paths */
2624 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2625 if (new) {
2626 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2627 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2628 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2629 }
2630
2631 /* update any old capture paths */
2632 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2633 if (old) {
2634 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2635 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2636 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2637 }
2638
2639 dpcm_path_put(&list);
2640 }
2641
2642 mutex_unlock(&card->mutex);
2643 return 0;
2644}
Liam Girdwood01d75842012-04-25 12:12:49 +01002645int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2646{
2647 struct snd_soc_dpcm *dpcm;
2648 struct list_head *clients =
2649 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2650
2651 list_for_each_entry(dpcm, clients, list_be) {
2652
2653 struct snd_soc_pcm_runtime *be = dpcm->be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002654 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01002655
2656 if (be->dai_link->ignore_suspend)
2657 continue;
2658
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002659 for (i = 0; i < be->num_codecs; i++) {
2660 struct snd_soc_dai *dai = be->codec_dais[i];
2661 struct snd_soc_dai_driver *drv = dai->driver;
Liam Girdwood01d75842012-04-25 12:12:49 +01002662
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002663 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2664 be->dai_link->name);
2665
2666 if (drv->ops && drv->ops->digital_mute &&
2667 dai->playback_active)
2668 drv->ops->digital_mute(dai, mute);
2669 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002670 }
2671
2672 return 0;
2673}
2674
Mark Brown45c0a182012-05-09 21:46:27 +01002675static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002676{
2677 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2678 struct snd_soc_dpcm *dpcm;
2679 struct snd_soc_dapm_widget_list *list;
2680 int ret;
2681 int stream = fe_substream->stream;
2682
2683 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2684 fe->dpcm[stream].runtime = fe_substream->runtime;
2685
Qiao Zhou8f70e512014-09-10 17:54:07 +08002686 ret = dpcm_path_get(fe, stream, &list);
2687 if (ret < 0) {
2688 mutex_unlock(&fe->card->mutex);
2689 return ret;
2690 } else if (ret == 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002691 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002692 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002693 }
2694
2695 /* calculate valid and active FE <-> BE dpcms */
2696 dpcm_process_paths(fe, stream, &list, 1);
2697
2698 ret = dpcm_fe_dai_startup(fe_substream);
2699 if (ret < 0) {
2700 /* clean up all links */
2701 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2702 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2703
2704 dpcm_be_disconnect(fe, stream);
2705 fe->dpcm[stream].runtime = NULL;
2706 }
2707
2708 dpcm_clear_pending_state(fe, stream);
2709 dpcm_path_put(&list);
2710 mutex_unlock(&fe->card->mutex);
2711 return ret;
2712}
2713
Mark Brown45c0a182012-05-09 21:46:27 +01002714static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002715{
2716 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2717 struct snd_soc_dpcm *dpcm;
2718 int stream = fe_substream->stream, ret;
2719
2720 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2721 ret = dpcm_fe_dai_shutdown(fe_substream);
2722
2723 /* mark FE's links ready to prune */
2724 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2725 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2726
2727 dpcm_be_disconnect(fe, stream);
2728
2729 fe->dpcm[stream].runtime = NULL;
2730 mutex_unlock(&fe->card->mutex);
2731 return ret;
2732}
2733
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02002734static void soc_pcm_private_free(struct snd_pcm *pcm)
2735{
2736 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002737 struct snd_soc_rtdcom_list *rtdcom;
2738 struct snd_soc_component *component;
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02002739
Kuninori Morimotof30a4c32018-01-24 05:18:46 +00002740 /* need to sync the delayed work before releasing resources */
2741 flush_delayed_work(&rtd->delayed_work);
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002742 for_each_rtdcom(rtd, rtdcom) {
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002743 component = rtdcom->component;
2744
Kuninori Morimoto11fb14f2018-05-08 03:19:49 +00002745 if (component->driver->pcm_free)
2746 component->driver->pcm_free(pcm);
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002747 }
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02002748}
2749
Kuninori Morimotob8135862017-10-11 01:37:23 +00002750static int soc_rtdcom_ack(struct snd_pcm_substream *substream)
2751{
2752 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2753 struct snd_soc_rtdcom_list *rtdcom;
2754 struct snd_soc_component *component;
2755
2756 for_each_rtdcom(rtd, rtdcom) {
2757 component = rtdcom->component;
2758
2759 if (!component->driver->ops ||
2760 !component->driver->ops->ack)
2761 continue;
2762
2763 /* FIXME. it returns 1st ask now */
2764 return component->driver->ops->ack(substream);
2765 }
2766
2767 return -EINVAL;
2768}
2769
2770static int soc_rtdcom_copy_user(struct snd_pcm_substream *substream, int channel,
2771 unsigned long pos, void __user *buf,
2772 unsigned long bytes)
2773{
2774 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2775 struct snd_soc_rtdcom_list *rtdcom;
2776 struct snd_soc_component *component;
2777
2778 for_each_rtdcom(rtd, rtdcom) {
2779 component = rtdcom->component;
2780
2781 if (!component->driver->ops ||
2782 !component->driver->ops->copy_user)
2783 continue;
2784
2785 /* FIXME. it returns 1st copy now */
2786 return component->driver->ops->copy_user(substream, channel,
2787 pos, buf, bytes);
2788 }
2789
2790 return -EINVAL;
2791}
2792
2793static int soc_rtdcom_copy_kernel(struct snd_pcm_substream *substream, int channel,
2794 unsigned long pos, void *buf, unsigned long bytes)
2795{
2796 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2797 struct snd_soc_rtdcom_list *rtdcom;
2798 struct snd_soc_component *component;
2799
2800 for_each_rtdcom(rtd, rtdcom) {
2801 component = rtdcom->component;
2802
2803 if (!component->driver->ops ||
2804 !component->driver->ops->copy_kernel)
2805 continue;
2806
2807 /* FIXME. it returns 1st copy now */
2808 return component->driver->ops->copy_kernel(substream, channel,
2809 pos, buf, bytes);
2810 }
2811
2812 return -EINVAL;
2813}
2814
2815static int soc_rtdcom_fill_silence(struct snd_pcm_substream *substream, int channel,
2816 unsigned long pos, unsigned long bytes)
2817{
2818 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2819 struct snd_soc_rtdcom_list *rtdcom;
2820 struct snd_soc_component *component;
2821
2822 for_each_rtdcom(rtd, rtdcom) {
2823 component = rtdcom->component;
2824
2825 if (!component->driver->ops ||
2826 !component->driver->ops->fill_silence)
2827 continue;
2828
2829 /* FIXME. it returns 1st silence now */
2830 return component->driver->ops->fill_silence(substream, channel,
2831 pos, bytes);
2832 }
2833
2834 return -EINVAL;
2835}
2836
2837static struct page *soc_rtdcom_page(struct snd_pcm_substream *substream,
2838 unsigned long offset)
2839{
2840 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2841 struct snd_soc_rtdcom_list *rtdcom;
2842 struct snd_soc_component *component;
2843 struct page *page;
2844
2845 for_each_rtdcom(rtd, rtdcom) {
2846 component = rtdcom->component;
2847
2848 if (!component->driver->ops ||
2849 !component->driver->ops->page)
2850 continue;
2851
2852 /* FIXME. it returns 1st page now */
2853 page = component->driver->ops->page(substream, offset);
2854 if (page)
2855 return page;
2856 }
2857
2858 return NULL;
2859}
2860
2861static int soc_rtdcom_mmap(struct snd_pcm_substream *substream,
2862 struct vm_area_struct *vma)
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->mmap)
2873 continue;
2874
2875 /* FIXME. it returns 1st mmap now */
2876 return component->driver->ops->mmap(substream, vma);
2877 }
2878
2879 return -EINVAL;
2880}
2881
Liam Girdwoodddee6272011-06-09 14:45:53 +01002882/* create a new pcm */
2883int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2884{
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002885 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002886 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002887 struct snd_soc_component *component;
2888 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002889 struct snd_pcm *pcm;
2890 char new_name[64];
2891 int ret = 0, playback = 0, capture = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002892 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002893
Liam Girdwood01d75842012-04-25 12:12:49 +01002894 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002895 playback = rtd->dai_link->dpcm_playback;
2896 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002897 } else {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002898 for (i = 0; i < rtd->num_codecs; i++) {
2899 codec_dai = rtd->codec_dais[i];
2900 if (codec_dai->driver->playback.channels_min)
2901 playback = 1;
2902 if (codec_dai->driver->capture.channels_min)
2903 capture = 1;
2904 }
2905
2906 capture = capture && cpu_dai->driver->capture.channels_min;
2907 playback = playback && cpu_dai->driver->playback.channels_min;
Liam Girdwood01d75842012-04-25 12:12:49 +01002908 }
Sangsu Parka5002312012-01-02 17:15:10 +09002909
Fabio Estevamd6bead02013-08-29 10:32:13 -03002910 if (rtd->dai_link->playback_only) {
2911 playback = 1;
2912 capture = 0;
2913 }
2914
2915 if (rtd->dai_link->capture_only) {
2916 playback = 0;
2917 capture = 1;
2918 }
2919
Liam Girdwood01d75842012-04-25 12:12:49 +01002920 /* create the PCM */
2921 if (rtd->dai_link->no_pcm) {
2922 snprintf(new_name, sizeof(new_name), "(%s)",
2923 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002924
Liam Girdwood01d75842012-04-25 12:12:49 +01002925 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2926 playback, capture, &pcm);
2927 } else {
2928 if (rtd->dai_link->dynamic)
2929 snprintf(new_name, sizeof(new_name), "%s (*)",
2930 rtd->dai_link->stream_name);
2931 else
2932 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002933 rtd->dai_link->stream_name,
2934 (rtd->num_codecs > 1) ?
2935 "multicodec" : rtd->codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002936
Liam Girdwood01d75842012-04-25 12:12:49 +01002937 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2938 capture, &pcm);
2939 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002940 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002941 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002942 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002943 return ret;
2944 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002945 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002946
2947 /* DAPM dai link stream work */
2948 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2949
Vinod Koul48c76992015-02-12 09:59:53 +05302950 pcm->nonatomic = rtd->dai_link->nonatomic;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002951 rtd->pcm = pcm;
2952 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002953
2954 if (rtd->dai_link->no_pcm) {
2955 if (playback)
2956 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2957 if (capture)
2958 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2959 goto out;
2960 }
2961
2962 /* ASoC PCM operations */
2963 if (rtd->dai_link->dynamic) {
2964 rtd->ops.open = dpcm_fe_dai_open;
2965 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2966 rtd->ops.prepare = dpcm_fe_dai_prepare;
2967 rtd->ops.trigger = dpcm_fe_dai_trigger;
2968 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2969 rtd->ops.close = dpcm_fe_dai_close;
2970 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002971 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002972 } else {
2973 rtd->ops.open = soc_pcm_open;
2974 rtd->ops.hw_params = soc_pcm_hw_params;
2975 rtd->ops.prepare = soc_pcm_prepare;
2976 rtd->ops.trigger = soc_pcm_trigger;
2977 rtd->ops.hw_free = soc_pcm_hw_free;
2978 rtd->ops.close = soc_pcm_close;
2979 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002980 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002981 }
2982
Kuninori Morimotob8135862017-10-11 01:37:23 +00002983 for_each_rtdcom(rtd, rtdcom) {
2984 const struct snd_pcm_ops *ops = rtdcom->component->driver->ops;
2985
2986 if (!ops)
2987 continue;
2988
2989 if (ops->ack)
2990 rtd->ops.ack = soc_rtdcom_ack;
2991 if (ops->copy_user)
2992 rtd->ops.copy_user = soc_rtdcom_copy_user;
2993 if (ops->copy_kernel)
2994 rtd->ops.copy_kernel = soc_rtdcom_copy_kernel;
2995 if (ops->fill_silence)
2996 rtd->ops.fill_silence = soc_rtdcom_fill_silence;
2997 if (ops->page)
2998 rtd->ops.page = soc_rtdcom_page;
2999 if (ops->mmap)
3000 rtd->ops.mmap = soc_rtdcom_mmap;
3001 }
3002
Liam Girdwoodddee6272011-06-09 14:45:53 +01003003 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01003004 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003005
3006 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01003007 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003008
Kuninori Morimotof523ace2017-09-26 01:00:53 +00003009 for_each_rtdcom(rtd, rtdcom) {
3010 component = rtdcom->component;
3011
Kuninori Morimoto11fb14f2018-05-08 03:19:49 +00003012 if (!component->driver->pcm_new)
Kuninori Morimotof523ace2017-09-26 01:00:53 +00003013 continue;
3014
Kuninori Morimoto11fb14f2018-05-08 03:19:49 +00003015 ret = component->driver->pcm_new(rtd);
Johan Hovoldc641e5b2017-07-12 17:55:29 +02003016 if (ret < 0) {
Kuninori Morimotof523ace2017-09-26 01:00:53 +00003017 dev_err(component->dev,
Johan Hovoldc641e5b2017-07-12 17:55:29 +02003018 "ASoC: pcm constructor failed: %d\n",
3019 ret);
3020 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003021 }
3022 }
Johan Hovoldc641e5b2017-07-12 17:55:29 +02003023
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02003024 pcm->private_free = soc_pcm_private_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01003025out:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02003026 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3027 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3028 cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003029 return ret;
3030}
Liam Girdwood01d75842012-04-25 12:12:49 +01003031
3032/* is the current PCM operation for this FE ? */
3033int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3034{
3035 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3036 return 1;
3037 return 0;
3038}
3039EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3040
3041/* is the current PCM operation for this BE ? */
3042int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3043 struct snd_soc_pcm_runtime *be, int stream)
3044{
3045 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3046 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3047 be->dpcm[stream].runtime_update))
3048 return 1;
3049 return 0;
3050}
3051EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3052
3053/* get the substream for this BE */
3054struct snd_pcm_substream *
3055 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3056{
3057 return be->pcm->streams[stream].substream;
3058}
3059EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3060
3061/* get the BE runtime state */
3062enum snd_soc_dpcm_state
3063 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3064{
3065 return be->dpcm[stream].state;
3066}
3067EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3068
3069/* set the BE runtime state */
3070void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3071 int stream, enum snd_soc_dpcm_state state)
3072{
3073 be->dpcm[stream].state = state;
3074}
3075EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3076
3077/*
3078 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3079 * are not running, paused or suspended for the specified stream direction.
3080 */
3081int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3082 struct snd_soc_pcm_runtime *be, int stream)
3083{
3084 struct snd_soc_dpcm *dpcm;
3085 int state;
3086
3087 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3088
3089 if (dpcm->fe == fe)
3090 continue;
3091
3092 state = dpcm->fe->dpcm[stream].state;
3093 if (state == SND_SOC_DPCM_STATE_START ||
3094 state == SND_SOC_DPCM_STATE_PAUSED ||
3095 state == SND_SOC_DPCM_STATE_SUSPEND)
3096 return 0;
3097 }
3098
3099 /* it's safe to free/stop this BE DAI */
3100 return 1;
3101}
3102EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3103
3104/*
3105 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3106 * running, paused or suspended for the specified stream direction.
3107 */
3108int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3109 struct snd_soc_pcm_runtime *be, int stream)
3110{
3111 struct snd_soc_dpcm *dpcm;
3112 int state;
3113
3114 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3115
3116 if (dpcm->fe == fe)
3117 continue;
3118
3119 state = dpcm->fe->dpcm[stream].state;
3120 if (state == SND_SOC_DPCM_STATE_START ||
3121 state == SND_SOC_DPCM_STATE_PAUSED ||
3122 state == SND_SOC_DPCM_STATE_SUSPEND ||
3123 state == SND_SOC_DPCM_STATE_PREPARE)
3124 return 0;
3125 }
3126
3127 /* it's safe to change hw_params */
3128 return 1;
3129}
3130EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003131
3132#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen852801412016-11-22 11:29:14 +01003133static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003134{
3135 switch (state) {
3136 case SND_SOC_DPCM_STATE_NEW:
3137 return "new";
3138 case SND_SOC_DPCM_STATE_OPEN:
3139 return "open";
3140 case SND_SOC_DPCM_STATE_HW_PARAMS:
3141 return "hw_params";
3142 case SND_SOC_DPCM_STATE_PREPARE:
3143 return "prepare";
3144 case SND_SOC_DPCM_STATE_START:
3145 return "start";
3146 case SND_SOC_DPCM_STATE_STOP:
3147 return "stop";
3148 case SND_SOC_DPCM_STATE_SUSPEND:
3149 return "suspend";
3150 case SND_SOC_DPCM_STATE_PAUSED:
3151 return "paused";
3152 case SND_SOC_DPCM_STATE_HW_FREE:
3153 return "hw_free";
3154 case SND_SOC_DPCM_STATE_CLOSE:
3155 return "close";
3156 }
3157
3158 return "unknown";
3159}
3160
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003161static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3162 int stream, char *buf, size_t size)
3163{
3164 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3165 struct snd_soc_dpcm *dpcm;
3166 ssize_t offset = 0;
3167
3168 /* FE state */
3169 offset += snprintf(buf + offset, size - offset,
3170 "[%s - %s]\n", fe->dai_link->name,
3171 stream ? "Capture" : "Playback");
3172
3173 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3174 dpcm_state_string(fe->dpcm[stream].state));
3175
3176 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3177 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3178 offset += snprintf(buf + offset, size - offset,
3179 "Hardware Params: "
3180 "Format = %s, Channels = %d, Rate = %d\n",
3181 snd_pcm_format_name(params_format(params)),
3182 params_channels(params),
3183 params_rate(params));
3184
3185 /* BEs state */
3186 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3187
3188 if (list_empty(&fe->dpcm[stream].be_clients)) {
3189 offset += snprintf(buf + offset, size - offset,
3190 " No active DSP links\n");
3191 goto out;
3192 }
3193
3194 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
3195 struct snd_soc_pcm_runtime *be = dpcm->be;
3196 params = &dpcm->hw_params;
3197
3198 offset += snprintf(buf + offset, size - offset,
3199 "- %s\n", be->dai_link->name);
3200
3201 offset += snprintf(buf + offset, size - offset,
3202 " State: %s\n",
3203 dpcm_state_string(be->dpcm[stream].state));
3204
3205 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3206 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3207 offset += snprintf(buf + offset, size - offset,
3208 " Hardware Params: "
3209 "Format = %s, Channels = %d, Rate = %d\n",
3210 snd_pcm_format_name(params_format(params)),
3211 params_channels(params),
3212 params_rate(params));
3213 }
3214
3215out:
3216 return offset;
3217}
3218
3219static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3220 size_t count, loff_t *ppos)
3221{
3222 struct snd_soc_pcm_runtime *fe = file->private_data;
3223 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3224 char *buf;
3225
3226 buf = kmalloc(out_count, GFP_KERNEL);
3227 if (!buf)
3228 return -ENOMEM;
3229
3230 if (fe->cpu_dai->driver->playback.channels_min)
3231 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3232 buf + offset, out_count - offset);
3233
3234 if (fe->cpu_dai->driver->capture.channels_min)
3235 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3236 buf + offset, out_count - offset);
3237
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003238 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003239
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003240 kfree(buf);
3241 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003242}
3243
3244static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003245 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003246 .read = dpcm_state_read_file,
3247 .llseek = default_llseek,
3248};
3249
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003250void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003251{
Mark Brownb3bba9a2012-05-08 10:33:47 +01003252 if (!rtd->dai_link)
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003253 return;
Mark Brownb3bba9a2012-05-08 10:33:47 +01003254
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003255 if (!rtd->card->debugfs_card_root)
3256 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003257
3258 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3259 rtd->card->debugfs_card_root);
3260 if (!rtd->debugfs_dpcm_root) {
3261 dev_dbg(rtd->dev,
3262 "ASoC: Failed to create dpcm debugfs directory %s\n",
3263 rtd->dai_link->name);
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003264 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003265 }
3266
Fabio Estevamf1e3f402017-07-29 11:40:55 -03003267 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3268 rtd, &dpcm_state_fops);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003269}
3270#endif