blob: d62d6a5340a85c7ff8b84bba17badbfa8879bdb4 [file] [log] [blame]
Liam Girdwoodddee6272011-06-09 14:45:53 +01001/*
2 * soc-pcm.c -- ALSA SoC PCM
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
8 *
9 * Authors: Liam Girdwood <lrg@ti.com>
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +020010 * Mark Brown <broonie@opensource.wolfsonmicro.com>
Liam Girdwoodddee6272011-06-09 14:45:53 +010011 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 */
18
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/delay.h>
Nicolin Chen988e8cc2013-11-04 14:57:31 +080022#include <linux/pinctrl/consumer.h>
Mark Brownd6652ef2011-12-03 20:14:31 +000023#include <linux/pm_runtime.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010024#include <linux/slab.h>
25#include <linux/workqueue.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010026#include <linux/export.h>
Liam Girdwoodf86dcef2012-04-25 12:12:50 +010027#include <linux/debugfs.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010028#include <sound/core.h>
29#include <sound/pcm.h>
30#include <sound/pcm_params.h>
31#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010032#include <sound/soc-dpcm.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010033#include <sound/initval.h>
34
Liam Girdwood01d75842012-04-25 12:12:49 +010035#define DPCM_MAX_BE_USERS 8
36
Lars-Peter Clausen90996f42013-05-14 11:05:30 +020037/**
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010038 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
39 * @rtd: ASoC PCM runtime that is activated
40 * @stream: Direction of the PCM stream
41 *
42 * Increments the active count for all the DAIs and components attached to a PCM
43 * runtime. Should typically be called when a stream is opened.
44 *
45 * Must be called with the rtd->pcm_mutex being held
46 */
47void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
48{
49 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020050 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010051
52 lockdep_assert_held(&rtd->pcm_mutex);
53
54 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
55 cpu_dai->playback_active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020056 for (i = 0; i < rtd->num_codecs; i++)
57 rtd->codec_dais[i]->playback_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010058 } else {
59 cpu_dai->capture_active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020060 for (i = 0; i < rtd->num_codecs; i++)
61 rtd->codec_dais[i]->capture_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010062 }
63
64 cpu_dai->active++;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +010065 cpu_dai->component->active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020066 for (i = 0; i < rtd->num_codecs; i++) {
67 rtd->codec_dais[i]->active++;
68 rtd->codec_dais[i]->component->active++;
69 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010070}
71
72/**
73 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
74 * @rtd: ASoC PCM runtime that is deactivated
75 * @stream: Direction of the PCM stream
76 *
77 * Decrements the active count for all the DAIs and components attached to a PCM
78 * runtime. Should typically be called when a stream is closed.
79 *
80 * Must be called with the rtd->pcm_mutex being held
81 */
82void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
83{
84 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020085 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010086
87 lockdep_assert_held(&rtd->pcm_mutex);
88
89 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
90 cpu_dai->playback_active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020091 for (i = 0; i < rtd->num_codecs; i++)
92 rtd->codec_dais[i]->playback_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010093 } else {
94 cpu_dai->capture_active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020095 for (i = 0; i < rtd->num_codecs; i++)
96 rtd->codec_dais[i]->capture_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010097 }
98
99 cpu_dai->active--;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +0100100 cpu_dai->component->active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200101 for (i = 0; i < rtd->num_codecs; i++) {
102 rtd->codec_dais[i]->component->active--;
103 rtd->codec_dais[i]->active--;
104 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100105}
106
107/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100108 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
109 * @rtd: The ASoC PCM runtime that should be checked.
110 *
111 * This function checks whether the power down delay should be ignored for a
112 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
113 * been configured to ignore the delay, or if none of the components benefits
114 * from having the delay.
115 */
116bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
117{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200118 int i;
119 bool ignore = true;
120
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100121 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
122 return true;
123
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200124 for (i = 0; i < rtd->num_codecs; i++)
125 ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
126
127 return rtd->cpu_dai->component->ignore_pmdown_time && ignore;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100128}
129
130/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200131 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
132 * @substream: the pcm substream
133 * @hw: the hardware parameters
134 *
135 * Sets the substream runtime hardware parameters.
136 */
137int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
138 const struct snd_pcm_hardware *hw)
139{
140 struct snd_pcm_runtime *runtime = substream->runtime;
141 runtime->hw.info = hw->info;
142 runtime->hw.formats = hw->formats;
143 runtime->hw.period_bytes_min = hw->period_bytes_min;
144 runtime->hw.period_bytes_max = hw->period_bytes_max;
145 runtime->hw.periods_min = hw->periods_min;
146 runtime->hw.periods_max = hw->periods_max;
147 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
148 runtime->hw.fifo_size = hw->fifo_size;
149 return 0;
150}
151EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
152
Liam Girdwood01d75842012-04-25 12:12:49 +0100153/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000154int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100155 int event)
156{
157 struct snd_soc_dpcm *dpcm;
158
159 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
160
161 struct snd_soc_pcm_runtime *be = dpcm->be;
162
Liam Girdwood103d84a2012-11-19 14:39:15 +0000163 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100164 be->dai_link->name, event, dir);
165
166 snd_soc_dapm_stream_event(be, dir, event);
167 }
168
169 snd_soc_dapm_stream_event(fe, dir, event);
170
171 return 0;
172}
173
Dong Aisheng17841022011-08-29 17:15:14 +0800174static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
175 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100176{
177 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100178 int ret;
179
Nicolin Chen3635bf02013-11-13 18:56:24 +0800180 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
181 rtd->dai_link->symmetric_rates)) {
182 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
183 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100184
Nicolin Chen3635bf02013-11-13 18:56:24 +0800185 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
186 SNDRV_PCM_HW_PARAM_RATE,
187 soc_dai->rate, soc_dai->rate);
188 if (ret < 0) {
189 dev_err(soc_dai->dev,
190 "ASoC: Unable to apply rate constraint: %d\n",
191 ret);
192 return ret;
193 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100194 }
195
Nicolin Chen3635bf02013-11-13 18:56:24 +0800196 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
197 rtd->dai_link->symmetric_channels)) {
198 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
199 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100200
Nicolin Chen3635bf02013-11-13 18:56:24 +0800201 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
202 SNDRV_PCM_HW_PARAM_CHANNELS,
203 soc_dai->channels,
204 soc_dai->channels);
205 if (ret < 0) {
206 dev_err(soc_dai->dev,
207 "ASoC: Unable to apply channel symmetry constraint: %d\n",
208 ret);
209 return ret;
210 }
211 }
212
213 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
214 rtd->dai_link->symmetric_samplebits)) {
215 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
216 soc_dai->sample_bits);
217
218 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
219 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
220 soc_dai->sample_bits,
221 soc_dai->sample_bits);
222 if (ret < 0) {
223 dev_err(soc_dai->dev,
224 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
225 ret);
226 return ret;
227 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100228 }
229
230 return 0;
231}
232
Nicolin Chen3635bf02013-11-13 18:56:24 +0800233static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
234 struct snd_pcm_hw_params *params)
235{
236 struct snd_soc_pcm_runtime *rtd = substream->private_data;
237 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200238 unsigned int rate, channels, sample_bits, symmetry, i;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800239
240 rate = params_rate(params);
241 channels = params_channels(params);
242 sample_bits = snd_pcm_format_physical_width(params_format(params));
243
244 /* reject unmatched parameters when applying symmetry */
245 symmetry = cpu_dai->driver->symmetric_rates ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800246 rtd->dai_link->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200247
248 for (i = 0; i < rtd->num_codecs; i++)
249 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
250
Nicolin Chen3635bf02013-11-13 18:56:24 +0800251 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
252 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
253 cpu_dai->rate, rate);
254 return -EINVAL;
255 }
256
257 symmetry = cpu_dai->driver->symmetric_channels ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800258 rtd->dai_link->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200259
260 for (i = 0; i < rtd->num_codecs; i++)
261 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
262
Nicolin Chen3635bf02013-11-13 18:56:24 +0800263 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
264 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
265 cpu_dai->channels, channels);
266 return -EINVAL;
267 }
268
269 symmetry = cpu_dai->driver->symmetric_samplebits ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800270 rtd->dai_link->symmetric_samplebits;
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_samplebits;
274
Nicolin Chen3635bf02013-11-13 18:56:24 +0800275 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
276 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
277 cpu_dai->sample_bits, sample_bits);
278 return -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100279 }
280
281 return 0;
282}
283
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100284static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
285{
286 struct snd_soc_pcm_runtime *rtd = substream->private_data;
287 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100288 struct snd_soc_dai_link *link = rtd->dai_link;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200289 unsigned int symmetry, i;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100290
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200291 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
292 cpu_driver->symmetric_channels || link->symmetric_channels ||
293 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
294
295 for (i = 0; i < rtd->num_codecs; i++)
296 symmetry = symmetry ||
297 rtd->codec_dais[i]->driver->symmetric_rates ||
298 rtd->codec_dais[i]->driver->symmetric_channels ||
299 rtd->codec_dais[i]->driver->symmetric_samplebits;
300
301 return symmetry;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100302}
303
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200304static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000305{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200306 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200307 int ret, i;
Mark Brown58ba9b22012-01-16 18:38:51 +0000308
309 if (!bits)
310 return;
311
Lars-Peter Clausen0e2a3752014-12-29 18:43:38 +0100312 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
313 if (ret != 0)
314 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
315 bits, ret);
Mark Brown58ba9b22012-01-16 18:38:51 +0000316}
317
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200318static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200319{
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200320 struct snd_soc_pcm_runtime *rtd = substream->private_data;
321 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200322 struct snd_soc_dai *codec_dai;
323 int i;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200324 unsigned int bits = 0, cpu_bits;
325
326 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200327 for (i = 0; i < rtd->num_codecs; i++) {
328 codec_dai = rtd->codec_dais[i];
329 if (codec_dai->driver->playback.sig_bits == 0) {
330 bits = 0;
331 break;
332 }
333 bits = max(codec_dai->driver->playback.sig_bits, bits);
334 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200335 cpu_bits = cpu_dai->driver->playback.sig_bits;
336 } else {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200337 for (i = 0; i < rtd->num_codecs; i++) {
338 codec_dai = rtd->codec_dais[i];
Daniel Mack5e63dfc2014-10-07 14:33:46 +0200339 if (codec_dai->driver->capture.sig_bits == 0) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200340 bits = 0;
341 break;
342 }
343 bits = max(codec_dai->driver->capture.sig_bits, bits);
344 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200345 cpu_bits = cpu_dai->driver->capture.sig_bits;
346 }
347
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200348 soc_pcm_set_msb(substream, bits);
349 soc_pcm_set_msb(substream, cpu_bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200350}
351
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200352static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200353{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200354 struct snd_pcm_runtime *runtime = substream->runtime;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100355 struct snd_pcm_hardware *hw = &runtime->hw;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200356 struct snd_soc_pcm_runtime *rtd = substream->private_data;
357 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
358 struct snd_soc_dai_driver *codec_dai_drv;
359 struct snd_soc_pcm_stream *codec_stream;
360 struct snd_soc_pcm_stream *cpu_stream;
361 unsigned int chan_min = 0, chan_max = UINT_MAX;
362 unsigned int rate_min = 0, rate_max = UINT_MAX;
363 unsigned int rates = UINT_MAX;
364 u64 formats = ULLONG_MAX;
365 int i;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100366
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200367 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
368 cpu_stream = &cpu_dai_drv->playback;
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100369 else
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200370 cpu_stream = &cpu_dai_drv->capture;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100371
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200372 /* first calculate min/max only for CODECs in the DAI link */
373 for (i = 0; i < rtd->num_codecs; i++) {
374 codec_dai_drv = rtd->codec_dais[i]->driver;
375 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
376 codec_stream = &codec_dai_drv->playback;
377 else
378 codec_stream = &codec_dai_drv->capture;
379 chan_min = max(chan_min, codec_stream->channels_min);
380 chan_max = min(chan_max, codec_stream->channels_max);
381 rate_min = max(rate_min, codec_stream->rate_min);
382 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
383 formats &= codec_stream->formats;
384 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
385 }
386
387 /*
388 * chan min/max cannot be enforced if there are multiple CODEC DAIs
389 * connected to a single CPU DAI, use CPU DAI's directly and let
390 * channel allocation be fixed up later
391 */
392 if (rtd->num_codecs > 1) {
393 chan_min = cpu_stream->channels_min;
394 chan_max = cpu_stream->channels_max;
395 }
396
397 hw->channels_min = max(chan_min, cpu_stream->channels_min);
398 hw->channels_max = min(chan_max, cpu_stream->channels_max);
399 if (hw->formats)
400 hw->formats &= formats & cpu_stream->formats;
401 else
402 hw->formats = formats & cpu_stream->formats;
403 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100404
405 snd_pcm_limit_hw_rates(runtime);
406
407 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200408 hw->rate_min = max(hw->rate_min, rate_min);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100409 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200410 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200411}
412
Mark Brown58ba9b22012-01-16 18:38:51 +0000413/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100414 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
415 * then initialized and any private data can be allocated. This also calls
416 * startup for the cpu DAI, platform, machine and codec DAI.
417 */
418static int soc_pcm_open(struct snd_pcm_substream *substream)
419{
420 struct snd_soc_pcm_runtime *rtd = substream->private_data;
421 struct snd_pcm_runtime *runtime = substream->runtime;
422 struct snd_soc_platform *platform = rtd->platform;
423 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200424 struct snd_soc_dai *codec_dai;
425 const char *codec_dai_name = "multicodec";
426 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100427
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800428 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200429 for (i = 0; i < rtd->num_codecs; i++)
430 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000431 pm_runtime_get_sync(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200432 for (i = 0; i < rtd->num_codecs; i++)
433 pm_runtime_get_sync(rtd->codec_dais[i]->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000434 pm_runtime_get_sync(platform->dev);
435
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100436 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100437
438 /* startup the audio subsystem */
Mark Brownc5914b02013-10-30 17:47:39 -0700439 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100440 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
441 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000442 dev_err(cpu_dai->dev, "ASoC: can't open interface"
443 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100444 goto out;
445 }
446 }
447
448 if (platform->driver->ops && platform->driver->ops->open) {
449 ret = platform->driver->ops->open(substream);
450 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000451 dev_err(platform->dev, "ASoC: can't open platform"
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200452 " %s: %d\n", platform->component.name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100453 goto platform_err;
454 }
455 }
456
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200457 for (i = 0; i < rtd->num_codecs; i++) {
458 codec_dai = rtd->codec_dais[i];
459 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
460 ret = codec_dai->driver->ops->startup(substream,
461 codec_dai);
462 if (ret < 0) {
463 dev_err(codec_dai->dev,
464 "ASoC: can't open codec %s: %d\n",
465 codec_dai->name, ret);
466 goto codec_dai_err;
467 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100468 }
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200469
470 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
471 codec_dai->tx_mask = 0;
472 else
473 codec_dai->rx_mask = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100474 }
475
476 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
477 ret = rtd->dai_link->ops->startup(substream);
478 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000479 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000480 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100481 goto machine_err;
482 }
483 }
484
Liam Girdwood01d75842012-04-25 12:12:49 +0100485 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
486 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
487 goto dynamic;
488
Liam Girdwoodddee6272011-06-09 14:45:53 +0100489 /* Check that the codec and cpu DAIs are compatible */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200490 soc_pcm_init_runtime_hw(substream);
491
492 if (rtd->num_codecs == 1)
493 codec_dai_name = rtd->codec_dai->name;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100494
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100495 if (soc_pcm_has_symmetry(substream))
496 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
497
Liam Girdwoodddee6272011-06-09 14:45:53 +0100498 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100499 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000500 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200501 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100502 goto config_err;
503 }
504 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000505 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200506 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100507 goto config_err;
508 }
509 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
510 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000511 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200512 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100513 goto config_err;
514 }
515
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200516 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000517
Liam Girdwoodddee6272011-06-09 14:45:53 +0100518 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800519 if (cpu_dai->active) {
520 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
521 if (ret != 0)
522 goto config_err;
523 }
524
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200525 for (i = 0; i < rtd->num_codecs; i++) {
526 if (rtd->codec_dais[i]->active) {
527 ret = soc_pcm_apply_symmetry(substream,
528 rtd->codec_dais[i]);
529 if (ret != 0)
530 goto config_err;
531 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100532 }
533
Liam Girdwood103d84a2012-11-19 14:39:15 +0000534 pr_debug("ASoC: %s <-> %s info:\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200535 codec_dai_name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000536 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
537 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100538 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000539 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100540 runtime->hw.rate_max);
541
Liam Girdwood01d75842012-04-25 12:12:49 +0100542dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100543
544 snd_soc_runtime_activate(rtd, substream->stream);
545
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100546 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100547 return 0;
548
549config_err:
550 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
551 rtd->dai_link->ops->shutdown(substream);
552
553machine_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200554 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100555
556codec_dai_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200557 while (--i >= 0) {
558 codec_dai = rtd->codec_dais[i];
559 if (codec_dai->driver->ops->shutdown)
560 codec_dai->driver->ops->shutdown(substream, codec_dai);
561 }
562
Liam Girdwoodddee6272011-06-09 14:45:53 +0100563 if (platform->driver->ops && platform->driver->ops->close)
564 platform->driver->ops->close(substream);
565
566platform_err:
567 if (cpu_dai->driver->ops->shutdown)
568 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
569out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100570 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000571
572 pm_runtime_put(platform->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200573 for (i = 0; i < rtd->num_codecs; i++)
574 pm_runtime_put(rtd->codec_dais[i]->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000575 pm_runtime_put(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200576 for (i = 0; i < rtd->num_codecs; i++) {
577 if (!rtd->codec_dais[i]->active)
578 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
579 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800580 if (!cpu_dai->active)
581 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000582
Liam Girdwoodddee6272011-06-09 14:45:53 +0100583 return ret;
584}
585
586/*
587 * Power down the audio subsystem pmdown_time msecs after close is called.
588 * This is to ensure there are no pops or clicks in between any music tracks
589 * due to DAPM power cycling.
590 */
591static void close_delayed_work(struct work_struct *work)
592{
593 struct snd_soc_pcm_runtime *rtd =
594 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200595 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
Liam Girdwoodddee6272011-06-09 14:45:53 +0100596
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100597 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100598
Liam Girdwood103d84a2012-11-19 14:39:15 +0000599 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100600 codec_dai->driver->playback.stream_name,
601 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600602 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100603
604 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600605 if (rtd->pop_wait == 1) {
606 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800607 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000608 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100609 }
610
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100611 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100612}
613
614/*
615 * Called by ALSA when a PCM substream is closed. Private data can be
616 * freed here. The cpu DAI, codec DAI, machine and platform are also
617 * shutdown.
618 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100619static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100620{
621 struct snd_soc_pcm_runtime *rtd = substream->private_data;
622 struct snd_soc_platform *platform = rtd->platform;
623 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200624 struct snd_soc_dai *codec_dai;
625 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100626
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100627 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100628
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100629 snd_soc_runtime_deactivate(rtd, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100630
Dong Aisheng17841022011-08-29 17:15:14 +0800631 /* clear the corresponding DAIs rate when inactive */
632 if (!cpu_dai->active)
633 cpu_dai->rate = 0;
634
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200635 for (i = 0; i < rtd->num_codecs; i++) {
636 codec_dai = rtd->codec_dais[i];
637 if (!codec_dai->active)
638 codec_dai->rate = 0;
639 }
Sascha Hauer25b76792011-08-17 09:20:01 +0200640
Ramesh Babuae116012014-10-15 12:34:59 +0530641 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
642
Liam Girdwoodddee6272011-06-09 14:45:53 +0100643 if (cpu_dai->driver->ops->shutdown)
644 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
645
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200646 for (i = 0; i < rtd->num_codecs; i++) {
647 codec_dai = rtd->codec_dais[i];
648 if (codec_dai->driver->ops->shutdown)
649 codec_dai->driver->ops->shutdown(substream, codec_dai);
650 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100651
652 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
653 rtd->dai_link->ops->shutdown(substream);
654
655 if (platform->driver->ops && platform->driver->ops->close)
656 platform->driver->ops->close(substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100657
658 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100659 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300660 /* powered down playback stream now */
661 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800662 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800663 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300664 } else {
665 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600666 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100667 queue_delayed_work(system_power_efficient_wq,
668 &rtd->delayed_work,
669 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300670 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100671 } else {
672 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800673 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000674 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100675 }
676
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100677 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000678
679 pm_runtime_put(platform->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200680 for (i = 0; i < rtd->num_codecs; i++)
681 pm_runtime_put(rtd->codec_dais[i]->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000682 pm_runtime_put(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200683 for (i = 0; i < rtd->num_codecs; i++) {
684 if (!rtd->codec_dais[i]->active)
685 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
686 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800687 if (!cpu_dai->active)
688 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000689
Liam Girdwoodddee6272011-06-09 14:45:53 +0100690 return 0;
691}
692
693/*
694 * Called by ALSA when the PCM substream is prepared, can set format, sample
695 * rate, etc. This function is non atomic and can be called multiple times,
696 * it can refer to the runtime info.
697 */
698static int soc_pcm_prepare(struct snd_pcm_substream *substream)
699{
700 struct snd_soc_pcm_runtime *rtd = substream->private_data;
701 struct snd_soc_platform *platform = rtd->platform;
702 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200703 struct snd_soc_dai *codec_dai;
704 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100705
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100706 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100707
708 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
709 ret = rtd->dai_link->ops->prepare(substream);
710 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000711 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
712 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100713 goto out;
714 }
715 }
716
717 if (platform->driver->ops && platform->driver->ops->prepare) {
718 ret = platform->driver->ops->prepare(substream);
719 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000720 dev_err(platform->dev, "ASoC: platform prepare error:"
721 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100722 goto out;
723 }
724 }
725
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200726 for (i = 0; i < rtd->num_codecs; i++) {
727 codec_dai = rtd->codec_dais[i];
728 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
729 ret = codec_dai->driver->ops->prepare(substream,
730 codec_dai);
731 if (ret < 0) {
732 dev_err(codec_dai->dev,
733 "ASoC: DAI prepare error: %d\n", ret);
734 goto out;
735 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100736 }
737 }
738
Mark Brownc5914b02013-10-30 17:47:39 -0700739 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100740 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
741 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000742 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000743 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100744 goto out;
745 }
746 }
747
748 /* cancel any delayed stream shutdown that is pending */
749 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600750 rtd->pop_wait) {
751 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100752 cancel_delayed_work(&rtd->delayed_work);
753 }
754
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000755 snd_soc_dapm_stream_event(rtd, substream->stream,
756 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100757
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200758 for (i = 0; i < rtd->num_codecs; i++)
759 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
760 substream->stream);
Ramesh Babuae116012014-10-15 12:34:59 +0530761 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100762
763out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100764 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100765 return ret;
766}
767
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200768static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
769 unsigned int mask)
770{
771 struct snd_interval *interval;
772 int channels = hweight_long(mask);
773
774 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
775 interval->min = channels;
776 interval->max = channels;
777}
778
Benoit Cousson93e69582014-07-08 23:19:38 +0200779int soc_dai_hw_params(struct snd_pcm_substream *substream,
780 struct snd_pcm_hw_params *params,
781 struct snd_soc_dai *dai)
782{
783 int ret;
784
785 if (dai->driver->ops && dai->driver->ops->hw_params) {
786 ret = dai->driver->ops->hw_params(substream, params, dai);
787 if (ret < 0) {
788 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
789 dai->name, ret);
790 return ret;
791 }
792 }
793
794 return 0;
795}
796
Liam Girdwoodddee6272011-06-09 14:45:53 +0100797/*
798 * Called by ALSA when the hardware params are set by application. This
799 * function can also be called multiple times and can allocate buffers
800 * (using snd_pcm_lib_* ). It's non-atomic.
801 */
802static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
803 struct snd_pcm_hw_params *params)
804{
805 struct snd_soc_pcm_runtime *rtd = substream->private_data;
806 struct snd_soc_platform *platform = rtd->platform;
807 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200808 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100809
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100810 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100811
Nicolin Chen3635bf02013-11-13 18:56:24 +0800812 ret = soc_pcm_params_symmetry(substream, params);
813 if (ret)
814 goto out;
815
Liam Girdwoodddee6272011-06-09 14:45:53 +0100816 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
817 ret = rtd->dai_link->ops->hw_params(substream, params);
818 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000819 dev_err(rtd->card->dev, "ASoC: machine hw_params"
820 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100821 goto out;
822 }
823 }
824
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200825 for (i = 0; i < rtd->num_codecs; i++) {
826 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
827 struct snd_pcm_hw_params codec_params;
828
829 /* copy params for each codec */
830 codec_params = *params;
831
832 /* fixup params based on TDM slot masks */
833 if (codec_dai->tx_mask)
834 soc_pcm_codec_params_fixup(&codec_params,
835 codec_dai->tx_mask);
836 if (codec_dai->rx_mask)
837 soc_pcm_codec_params_fixup(&codec_params,
838 codec_dai->rx_mask);
839
Benoit Cousson93e69582014-07-08 23:19:38 +0200840 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
841 if(ret < 0)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100842 goto codec_err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200843
844 codec_dai->rate = params_rate(&codec_params);
845 codec_dai->channels = params_channels(&codec_params);
846 codec_dai->sample_bits = snd_pcm_format_physical_width(
847 params_format(&codec_params));
Liam Girdwoodddee6272011-06-09 14:45:53 +0100848 }
849
Benoit Cousson93e69582014-07-08 23:19:38 +0200850 ret = soc_dai_hw_params(substream, params, cpu_dai);
851 if (ret < 0)
852 goto interface_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100853
854 if (platform->driver->ops && platform->driver->ops->hw_params) {
855 ret = platform->driver->ops->hw_params(substream, params);
856 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000857 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200858 platform->component.name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100859 goto platform_err;
860 }
861 }
862
Nicolin Chen3635bf02013-11-13 18:56:24 +0800863 /* store the parameters for each DAIs */
Dong Aisheng17841022011-08-29 17:15:14 +0800864 cpu_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800865 cpu_dai->channels = params_channels(params);
866 cpu_dai->sample_bits =
867 snd_pcm_format_physical_width(params_format(params));
868
Liam Girdwoodddee6272011-06-09 14:45:53 +0100869out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100870 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100871 return ret;
872
873platform_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700874 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100875 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
876
877interface_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200878 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100879
880codec_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200881 while (--i >= 0) {
882 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
883 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
884 codec_dai->driver->ops->hw_free(substream, codec_dai);
885 codec_dai->rate = 0;
886 }
887
Liam Girdwoodddee6272011-06-09 14:45:53 +0100888 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
889 rtd->dai_link->ops->hw_free(substream);
890
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100891 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100892 return ret;
893}
894
895/*
896 * Frees resources allocated by hw_params, can be called multiple times
897 */
898static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
899{
900 struct snd_soc_pcm_runtime *rtd = substream->private_data;
901 struct snd_soc_platform *platform = rtd->platform;
902 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200903 struct snd_soc_dai *codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +0800904 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200905 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100906
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100907 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100908
Nicolin Chend3383422013-11-20 18:37:09 +0800909 /* clear the corresponding DAIs parameters when going to be inactive */
910 if (cpu_dai->active == 1) {
911 cpu_dai->rate = 0;
912 cpu_dai->channels = 0;
913 cpu_dai->sample_bits = 0;
914 }
915
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200916 for (i = 0; i < rtd->num_codecs; i++) {
917 codec_dai = rtd->codec_dais[i];
918 if (codec_dai->active == 1) {
919 codec_dai->rate = 0;
920 codec_dai->channels = 0;
921 codec_dai->sample_bits = 0;
922 }
Nicolin Chend3383422013-11-20 18:37:09 +0800923 }
924
Liam Girdwoodddee6272011-06-09 14:45:53 +0100925 /* apply codec digital mute */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200926 for (i = 0; i < rtd->num_codecs; i++) {
927 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
928 (!playback && rtd->codec_dais[i]->capture_active == 1))
929 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
930 substream->stream);
931 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100932
933 /* free any machine hw params */
934 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
935 rtd->dai_link->ops->hw_free(substream);
936
937 /* free any DMA resources */
938 if (platform->driver->ops && platform->driver->ops->hw_free)
939 platform->driver->ops->hw_free(substream);
940
941 /* now free hw params for the DAIs */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200942 for (i = 0; i < rtd->num_codecs; i++) {
943 codec_dai = rtd->codec_dais[i];
944 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
945 codec_dai->driver->ops->hw_free(substream, codec_dai);
946 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100947
Mark Brownc5914b02013-10-30 17:47:39 -0700948 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100949 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
950
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100951 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100952 return 0;
953}
954
955static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
956{
957 struct snd_soc_pcm_runtime *rtd = substream->private_data;
958 struct snd_soc_platform *platform = rtd->platform;
959 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200960 struct snd_soc_dai *codec_dai;
961 int i, ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100962
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200963 for (i = 0; i < rtd->num_codecs; i++) {
964 codec_dai = rtd->codec_dais[i];
965 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
966 ret = codec_dai->driver->ops->trigger(substream,
967 cmd, codec_dai);
968 if (ret < 0)
969 return ret;
970 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100971 }
972
973 if (platform->driver->ops && platform->driver->ops->trigger) {
974 ret = platform->driver->ops->trigger(substream, cmd);
975 if (ret < 0)
976 return ret;
977 }
978
Mark Brownc5914b02013-10-30 17:47:39 -0700979 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100980 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
981 if (ret < 0)
982 return ret;
983 }
Jarkko Nikula4792b0d2014-04-28 14:17:52 +0200984
985 if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
986 ret = rtd->dai_link->ops->trigger(substream, cmd);
987 if (ret < 0)
988 return ret;
989 }
990
Liam Girdwoodddee6272011-06-09 14:45:53 +0100991 return 0;
992}
993
Mark Brown45c0a182012-05-09 21:46:27 +0100994static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
995 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100996{
997 struct snd_soc_pcm_runtime *rtd = substream->private_data;
998 struct snd_soc_platform *platform = rtd->platform;
999 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001000 struct snd_soc_dai *codec_dai;
1001 int i, ret;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001002
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001003 for (i = 0; i < rtd->num_codecs; i++) {
1004 codec_dai = rtd->codec_dais[i];
1005 if (codec_dai->driver->ops &&
1006 codec_dai->driver->ops->bespoke_trigger) {
1007 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1008 cmd, codec_dai);
1009 if (ret < 0)
1010 return ret;
1011 }
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001012 }
1013
Jean-Francois Moinedcf0fa22014-01-03 09:19:18 +01001014 if (platform->driver->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001015 ret = platform->driver->bespoke_trigger(substream, cmd);
1016 if (ret < 0)
1017 return ret;
1018 }
1019
Mark Brownc5914b02013-10-30 17:47:39 -07001020 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001021 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1022 if (ret < 0)
1023 return ret;
1024 }
1025 return 0;
1026}
Liam Girdwoodddee6272011-06-09 14:45:53 +01001027/*
1028 * soc level wrapper for pointer callback
1029 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1030 * the runtime->delay will be updated accordingly.
1031 */
1032static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1033{
1034 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1035 struct snd_soc_platform *platform = rtd->platform;
1036 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001037 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001038 struct snd_pcm_runtime *runtime = substream->runtime;
1039 snd_pcm_uframes_t offset = 0;
1040 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001041 snd_pcm_sframes_t codec_delay = 0;
1042 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001043
1044 if (platform->driver->ops && platform->driver->ops->pointer)
1045 offset = platform->driver->ops->pointer(substream);
1046
Mark Brownc5914b02013-10-30 17:47:39 -07001047 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001048 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1049
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001050 for (i = 0; i < rtd->num_codecs; i++) {
1051 codec_dai = rtd->codec_dais[i];
1052 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
1053 codec_delay = max(codec_delay,
1054 codec_dai->driver->ops->delay(substream,
1055 codec_dai));
1056 }
1057 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001058
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001059 /*
1060 * None of the existing platform drivers implement delay(), so
1061 * for now the codec_dai of first multicodec entry is used
1062 */
Liam Girdwoodddee6272011-06-09 14:45:53 +01001063 if (platform->driver->delay)
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001064 delay += platform->driver->delay(substream, rtd->codec_dais[0]);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001065
1066 runtime->delay = delay;
1067
1068 return offset;
1069}
1070
Liam Girdwood01d75842012-04-25 12:12:49 +01001071/* connect a FE and BE */
1072static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1073 struct snd_soc_pcm_runtime *be, int stream)
1074{
1075 struct snd_soc_dpcm *dpcm;
1076
1077 /* only add new dpcms */
1078 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1079 if (dpcm->be == be && dpcm->fe == fe)
1080 return 0;
1081 }
1082
1083 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1084 if (!dpcm)
1085 return -ENOMEM;
1086
1087 dpcm->be = be;
1088 dpcm->fe = fe;
1089 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1090 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1091 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1092 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1093
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001094 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001095 stream ? "capture" : "playback", fe->dai_link->name,
1096 stream ? "<-" : "->", be->dai_link->name);
1097
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001098#ifdef CONFIG_DEBUG_FS
1099 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1100 fe->debugfs_dpcm_root, &dpcm->state);
1101#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001102 return 1;
1103}
1104
1105/* reparent a BE onto another FE */
1106static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1107 struct snd_soc_pcm_runtime *be, int stream)
1108{
1109 struct snd_soc_dpcm *dpcm;
1110 struct snd_pcm_substream *fe_substream, *be_substream;
1111
1112 /* reparent if BE is connected to other FEs */
1113 if (!be->dpcm[stream].users)
1114 return;
1115
1116 be_substream = snd_soc_dpcm_get_substream(be, stream);
1117
1118 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1119 if (dpcm->fe == fe)
1120 continue;
1121
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001122 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001123 stream ? "capture" : "playback",
1124 dpcm->fe->dai_link->name,
1125 stream ? "<-" : "->", dpcm->be->dai_link->name);
1126
1127 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1128 be_substream->runtime = fe_substream->runtime;
1129 break;
1130 }
1131}
1132
1133/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001134void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001135{
1136 struct snd_soc_dpcm *dpcm, *d;
1137
1138 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001139 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001140 stream ? "capture" : "playback",
1141 dpcm->be->dai_link->name);
1142
1143 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1144 continue;
1145
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001146 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001147 stream ? "capture" : "playback", fe->dai_link->name,
1148 stream ? "<-" : "->", dpcm->be->dai_link->name);
1149
1150 /* BEs still alive need new FE */
1151 dpcm_be_reparent(fe, dpcm->be, stream);
1152
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001153#ifdef CONFIG_DEBUG_FS
1154 debugfs_remove(dpcm->debugfs_state);
1155#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001156 list_del(&dpcm->list_be);
1157 list_del(&dpcm->list_fe);
1158 kfree(dpcm);
1159 }
1160}
1161
1162/* get BE for DAI widget and stream */
1163static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1164 struct snd_soc_dapm_widget *widget, int stream)
1165{
1166 struct snd_soc_pcm_runtime *be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001167 int i, j;
Liam Girdwood01d75842012-04-25 12:12:49 +01001168
1169 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1170 for (i = 0; i < card->num_links; i++) {
1171 be = &card->rtd[i];
1172
Liam Girdwood35ea0652012-06-05 19:26:59 +01001173 if (!be->dai_link->no_pcm)
1174 continue;
1175
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001176 if (be->cpu_dai->playback_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001177 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001178
1179 for (j = 0; j < be->num_codecs; j++) {
1180 struct snd_soc_dai *dai = be->codec_dais[j];
1181 if (dai->playback_widget == widget)
1182 return be;
1183 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001184 }
1185 } else {
1186
1187 for (i = 0; i < card->num_links; i++) {
1188 be = &card->rtd[i];
1189
Liam Girdwood35ea0652012-06-05 19:26:59 +01001190 if (!be->dai_link->no_pcm)
1191 continue;
1192
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001193 if (be->cpu_dai->capture_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001194 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001195
1196 for (j = 0; j < be->num_codecs; j++) {
1197 struct snd_soc_dai *dai = be->codec_dais[j];
1198 if (dai->capture_widget == widget)
1199 return be;
1200 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001201 }
1202 }
1203
Liam Girdwood103d84a2012-11-19 14:39:15 +00001204 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001205 stream ? "capture" : "playback", widget->name);
1206 return NULL;
1207}
1208
1209static inline struct snd_soc_dapm_widget *
Benoit Cousson37018612014-04-24 14:01:45 +02001210 dai_get_widget(struct snd_soc_dai *dai, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001211{
1212 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Benoit Cousson37018612014-04-24 14:01:45 +02001213 return dai->playback_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001214 else
Benoit Cousson37018612014-04-24 14:01:45 +02001215 return dai->capture_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001216}
1217
1218static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1219 struct snd_soc_dapm_widget *widget)
1220{
1221 int i;
1222
1223 for (i = 0; i < list->num_widgets; i++) {
1224 if (widget == list->widgets[i])
1225 return 1;
1226 }
1227
1228 return 0;
1229}
1230
Liam Girdwood23607022014-01-17 17:03:55 +00001231int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001232 int stream, struct snd_soc_dapm_widget_list **list_)
1233{
1234 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1235 struct snd_soc_dapm_widget_list *list;
1236 int paths;
1237
1238 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
1239 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
1240 if (list == NULL)
1241 return -ENOMEM;
1242
1243 /* get number of valid DAI paths and their widgets */
1244 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
1245
Liam Girdwood103d84a2012-11-19 14:39:15 +00001246 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001247 stream ? "capture" : "playback");
1248
1249 *list_ = list;
1250 return paths;
1251}
1252
Liam Girdwood01d75842012-04-25 12:12:49 +01001253static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1254 struct snd_soc_dapm_widget_list **list_)
1255{
1256 struct snd_soc_dpcm *dpcm;
1257 struct snd_soc_dapm_widget_list *list = *list_;
1258 struct snd_soc_dapm_widget *widget;
1259 int prune = 0;
1260
1261 /* Destroy any old FE <--> BE connections */
1262 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001263 unsigned int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001264
1265 /* is there a valid CPU DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001266 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001267
1268 /* prune the BE if it's no longer in our active list */
1269 if (widget && widget_in_list(list, widget))
1270 continue;
1271
1272 /* is there a valid CODEC DAI widget for this BE */
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001273 for (i = 0; i < dpcm->be->num_codecs; i++) {
1274 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1275 widget = dai_get_widget(dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001276
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001277 /* prune the BE if it's no longer in our active list */
1278 if (widget && widget_in_list(list, widget))
1279 continue;
1280 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001281
Liam Girdwood103d84a2012-11-19 14:39:15 +00001282 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001283 stream ? "capture" : "playback",
1284 dpcm->be->dai_link->name, fe->dai_link->name);
1285 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1286 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1287 prune++;
1288 }
1289
Liam Girdwood103d84a2012-11-19 14:39:15 +00001290 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001291 return prune;
1292}
1293
1294static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1295 struct snd_soc_dapm_widget_list **list_)
1296{
1297 struct snd_soc_card *card = fe->card;
1298 struct snd_soc_dapm_widget_list *list = *list_;
1299 struct snd_soc_pcm_runtime *be;
1300 int i, new = 0, err;
1301
1302 /* Create any new FE <--> BE connections */
1303 for (i = 0; i < list->num_widgets; i++) {
1304
Mark Brown46162742013-06-05 19:36:11 +01001305 switch (list->widgets[i]->id) {
1306 case snd_soc_dapm_dai_in:
1307 case snd_soc_dapm_dai_out:
1308 break;
1309 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001310 continue;
Mark Brown46162742013-06-05 19:36:11 +01001311 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001312
1313 /* is there a valid BE rtd for this widget */
1314 be = dpcm_get_be(card, list->widgets[i], stream);
1315 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001316 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001317 list->widgets[i]->name);
1318 continue;
1319 }
1320
1321 /* make sure BE is a real BE */
1322 if (!be->dai_link->no_pcm)
1323 continue;
1324
1325 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001326 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001327 continue;
1328
1329 /* newly connected FE and BE */
1330 err = dpcm_be_connect(fe, be, stream);
1331 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001332 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001333 list->widgets[i]->name);
1334 break;
1335 } else if (err == 0) /* already connected */
1336 continue;
1337
1338 /* new */
1339 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1340 new++;
1341 }
1342
Liam Girdwood103d84a2012-11-19 14:39:15 +00001343 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001344 return new;
1345}
1346
1347/*
1348 * Find the corresponding BE DAIs that source or sink audio to this
1349 * FE substream.
1350 */
Liam Girdwood23607022014-01-17 17:03:55 +00001351int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001352 int stream, struct snd_soc_dapm_widget_list **list, int new)
1353{
1354 if (new)
1355 return dpcm_add_paths(fe, stream, list);
1356 else
1357 return dpcm_prune_paths(fe, stream, list);
1358}
1359
Liam Girdwood23607022014-01-17 17:03:55 +00001360void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001361{
1362 struct snd_soc_dpcm *dpcm;
1363
1364 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1365 dpcm->be->dpcm[stream].runtime_update =
1366 SND_SOC_DPCM_UPDATE_NO;
1367}
1368
1369static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1370 int stream)
1371{
1372 struct snd_soc_dpcm *dpcm;
1373
1374 /* disable any enabled and non active backends */
1375 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1376
1377 struct snd_soc_pcm_runtime *be = dpcm->be;
1378 struct snd_pcm_substream *be_substream =
1379 snd_soc_dpcm_get_substream(be, stream);
1380
1381 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001382 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001383 stream ? "capture" : "playback",
1384 be->dpcm[stream].state);
1385
1386 if (--be->dpcm[stream].users != 0)
1387 continue;
1388
1389 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1390 continue;
1391
1392 soc_pcm_close(be_substream);
1393 be_substream->runtime = NULL;
1394 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1395 }
1396}
1397
Liam Girdwood23607022014-01-17 17:03:55 +00001398int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001399{
1400 struct snd_soc_dpcm *dpcm;
1401 int err, count = 0;
1402
1403 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1404 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1405
1406 struct snd_soc_pcm_runtime *be = dpcm->be;
1407 struct snd_pcm_substream *be_substream =
1408 snd_soc_dpcm_get_substream(be, stream);
1409
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001410 if (!be_substream) {
1411 dev_err(be->dev, "ASoC: no backend %s stream\n",
1412 stream ? "capture" : "playback");
1413 continue;
1414 }
1415
Liam Girdwood01d75842012-04-25 12:12:49 +01001416 /* is this op for this BE ? */
1417 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1418 continue;
1419
1420 /* first time the dpcm is open ? */
1421 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001422 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001423 stream ? "capture" : "playback",
1424 be->dpcm[stream].state);
1425
1426 if (be->dpcm[stream].users++ != 0)
1427 continue;
1428
1429 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1430 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1431 continue;
1432
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001433 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1434 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001435
1436 be_substream->runtime = be->dpcm[stream].runtime;
1437 err = soc_pcm_open(be_substream);
1438 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001439 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001440 be->dpcm[stream].users--;
1441 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001442 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001443 stream ? "capture" : "playback",
1444 be->dpcm[stream].state);
1445
1446 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1447 goto unwind;
1448 }
1449
1450 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1451 count++;
1452 }
1453
1454 return count;
1455
1456unwind:
1457 /* disable any enabled and non active backends */
1458 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1459 struct snd_soc_pcm_runtime *be = dpcm->be;
1460 struct snd_pcm_substream *be_substream =
1461 snd_soc_dpcm_get_substream(be, stream);
1462
1463 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1464 continue;
1465
1466 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001467 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001468 stream ? "capture" : "playback",
1469 be->dpcm[stream].state);
1470
1471 if (--be->dpcm[stream].users != 0)
1472 continue;
1473
1474 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1475 continue;
1476
1477 soc_pcm_close(be_substream);
1478 be_substream->runtime = NULL;
1479 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1480 }
1481
1482 return err;
1483}
1484
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001485static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1486 struct snd_soc_pcm_stream *stream)
1487{
1488 runtime->hw.rate_min = stream->rate_min;
1489 runtime->hw.rate_max = stream->rate_max;
1490 runtime->hw.channels_min = stream->channels_min;
1491 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001492 if (runtime->hw.formats)
1493 runtime->hw.formats &= stream->formats;
1494 else
1495 runtime->hw.formats = stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001496 runtime->hw.rates = stream->rates;
1497}
1498
Mark Brown45c0a182012-05-09 21:46:27 +01001499static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001500{
1501 struct snd_pcm_runtime *runtime = substream->runtime;
1502 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1503 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1504 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1505
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001506 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1507 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1508 else
1509 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
Liam Girdwood01d75842012-04-25 12:12:49 +01001510}
1511
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001512static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1513
1514/* Set FE's runtime_update state; the state is protected via PCM stream lock
1515 * for avoiding the race with trigger callback.
1516 * If the state is unset and a trigger is pending while the previous operation,
1517 * process the pending trigger action here.
1518 */
1519static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1520 int stream, enum snd_soc_dpcm_update state)
1521{
1522 struct snd_pcm_substream *substream =
1523 snd_soc_dpcm_get_substream(fe, stream);
1524
1525 snd_pcm_stream_lock_irq(substream);
1526 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1527 dpcm_fe_dai_do_trigger(substream,
1528 fe->dpcm[stream].trigger_pending - 1);
1529 fe->dpcm[stream].trigger_pending = 0;
1530 }
1531 fe->dpcm[stream].runtime_update = state;
1532 snd_pcm_stream_unlock_irq(substream);
1533}
1534
Liam Girdwood01d75842012-04-25 12:12:49 +01001535static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1536{
1537 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1538 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1539 int stream = fe_substream->stream, ret = 0;
1540
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001541 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001542
1543 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1544 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001545 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001546 goto be_err;
1547 }
1548
Liam Girdwood103d84a2012-11-19 14:39:15 +00001549 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001550
1551 /* start the DAI frontend */
1552 ret = soc_pcm_open(fe_substream);
1553 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001554 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001555 goto unwind;
1556 }
1557
1558 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1559
1560 dpcm_set_fe_runtime(fe_substream);
1561 snd_pcm_limit_hw_rates(runtime);
1562
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001563 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001564 return 0;
1565
1566unwind:
1567 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1568be_err:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001569 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001570 return ret;
1571}
1572
Liam Girdwood23607022014-01-17 17:03:55 +00001573int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001574{
1575 struct snd_soc_dpcm *dpcm;
1576
1577 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1578 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1579
1580 struct snd_soc_pcm_runtime *be = dpcm->be;
1581 struct snd_pcm_substream *be_substream =
1582 snd_soc_dpcm_get_substream(be, stream);
1583
1584 /* is this op for this BE ? */
1585 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1586 continue;
1587
1588 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001589 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001590 stream ? "capture" : "playback",
1591 be->dpcm[stream].state);
1592
1593 if (--be->dpcm[stream].users != 0)
1594 continue;
1595
1596 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1597 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1598 continue;
1599
Liam Girdwood103d84a2012-11-19 14:39:15 +00001600 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001601 dpcm->fe->dai_link->name);
1602
1603 soc_pcm_close(be_substream);
1604 be_substream->runtime = NULL;
1605
1606 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1607 }
1608 return 0;
1609}
1610
1611static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1612{
1613 struct snd_soc_pcm_runtime *fe = substream->private_data;
1614 int stream = substream->stream;
1615
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001616 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001617
1618 /* shutdown the BEs */
1619 dpcm_be_dai_shutdown(fe, substream->stream);
1620
Liam Girdwood103d84a2012-11-19 14:39:15 +00001621 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001622
1623 /* now shutdown the frontend */
1624 soc_pcm_close(substream);
1625
1626 /* run the stream event for each BE */
1627 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1628
1629 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001630 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001631 return 0;
1632}
1633
Liam Girdwood23607022014-01-17 17:03:55 +00001634int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001635{
1636 struct snd_soc_dpcm *dpcm;
1637
1638 /* only hw_params backends that are either sinks or sources
1639 * to this frontend DAI */
1640 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1641
1642 struct snd_soc_pcm_runtime *be = dpcm->be;
1643 struct snd_pcm_substream *be_substream =
1644 snd_soc_dpcm_get_substream(be, stream);
1645
1646 /* is this op for this BE ? */
1647 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1648 continue;
1649
1650 /* only free hw when no longer used - check all FEs */
1651 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1652 continue;
1653
Qiao Zhou36fba622014-12-03 10:13:43 +08001654 /* do not free hw if this BE is used by other FE */
1655 if (be->dpcm[stream].users > 1)
1656 continue;
1657
Liam Girdwood01d75842012-04-25 12:12:49 +01001658 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1659 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1660 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001661 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001662 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1663 continue;
1664
Liam Girdwood103d84a2012-11-19 14:39:15 +00001665 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001666 dpcm->fe->dai_link->name);
1667
1668 soc_pcm_hw_free(be_substream);
1669
1670 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1671 }
1672
1673 return 0;
1674}
1675
Mark Brown45c0a182012-05-09 21:46:27 +01001676static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001677{
1678 struct snd_soc_pcm_runtime *fe = substream->private_data;
1679 int err, stream = substream->stream;
1680
1681 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001682 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001683
Liam Girdwood103d84a2012-11-19 14:39:15 +00001684 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001685
1686 /* call hw_free on the frontend */
1687 err = soc_pcm_hw_free(substream);
1688 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001689 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001690 fe->dai_link->name);
1691
1692 /* only hw_params backends that are either sinks or sources
1693 * to this frontend DAI */
1694 err = dpcm_be_dai_hw_free(fe, stream);
1695
1696 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001697 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001698
1699 mutex_unlock(&fe->card->mutex);
1700 return 0;
1701}
1702
Liam Girdwood23607022014-01-17 17:03:55 +00001703int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001704{
1705 struct snd_soc_dpcm *dpcm;
1706 int ret;
1707
1708 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1709
1710 struct snd_soc_pcm_runtime *be = dpcm->be;
1711 struct snd_pcm_substream *be_substream =
1712 snd_soc_dpcm_get_substream(be, stream);
1713
1714 /* is this op for this BE ? */
1715 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1716 continue;
1717
1718 /* only allow hw_params() if no connected FEs are running */
1719 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1720 continue;
1721
1722 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1723 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1724 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1725 continue;
1726
Liam Girdwood103d84a2012-11-19 14:39:15 +00001727 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001728 dpcm->fe->dai_link->name);
1729
1730 /* copy params for each dpcm */
1731 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1732 sizeof(struct snd_pcm_hw_params));
1733
1734 /* perform any hw_params fixups */
1735 if (be->dai_link->be_hw_params_fixup) {
1736 ret = be->dai_link->be_hw_params_fixup(be,
1737 &dpcm->hw_params);
1738 if (ret < 0) {
1739 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001740 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001741 ret);
1742 goto unwind;
1743 }
1744 }
1745
1746 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1747 if (ret < 0) {
1748 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001749 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001750 goto unwind;
1751 }
1752
1753 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1754 }
1755 return 0;
1756
1757unwind:
1758 /* disable any enabled and non active backends */
1759 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1760 struct snd_soc_pcm_runtime *be = dpcm->be;
1761 struct snd_pcm_substream *be_substream =
1762 snd_soc_dpcm_get_substream(be, stream);
1763
1764 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1765 continue;
1766
1767 /* only allow hw_free() if no connected FEs are running */
1768 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1769 continue;
1770
1771 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1772 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1773 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1774 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1775 continue;
1776
1777 soc_pcm_hw_free(be_substream);
1778 }
1779
1780 return ret;
1781}
1782
Mark Brown45c0a182012-05-09 21:46:27 +01001783static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1784 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001785{
1786 struct snd_soc_pcm_runtime *fe = substream->private_data;
1787 int ret, stream = substream->stream;
1788
1789 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001790 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001791
1792 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1793 sizeof(struct snd_pcm_hw_params));
1794 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1795 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001796 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001797 goto out;
1798 }
1799
Liam Girdwood103d84a2012-11-19 14:39:15 +00001800 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001801 fe->dai_link->name, params_rate(params),
1802 params_channels(params), params_format(params));
1803
1804 /* call hw_params on the frontend */
1805 ret = soc_pcm_hw_params(substream, params);
1806 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001807 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001808 dpcm_be_dai_hw_free(fe, stream);
1809 } else
1810 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1811
1812out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001813 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001814 mutex_unlock(&fe->card->mutex);
1815 return ret;
1816}
1817
1818static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1819 struct snd_pcm_substream *substream, int cmd)
1820{
1821 int ret;
1822
Liam Girdwood103d84a2012-11-19 14:39:15 +00001823 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001824 dpcm->fe->dai_link->name, cmd);
1825
1826 ret = soc_pcm_trigger(substream, cmd);
1827 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001828 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001829
1830 return ret;
1831}
1832
Liam Girdwood23607022014-01-17 17:03:55 +00001833int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01001834 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001835{
1836 struct snd_soc_dpcm *dpcm;
1837 int ret = 0;
1838
1839 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1840
1841 struct snd_soc_pcm_runtime *be = dpcm->be;
1842 struct snd_pcm_substream *be_substream =
1843 snd_soc_dpcm_get_substream(be, stream);
1844
1845 /* is this op for this BE ? */
1846 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1847 continue;
1848
1849 switch (cmd) {
1850 case SNDRV_PCM_TRIGGER_START:
1851 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1852 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1853 continue;
1854
1855 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1856 if (ret)
1857 return ret;
1858
1859 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1860 break;
1861 case SNDRV_PCM_TRIGGER_RESUME:
1862 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1863 continue;
1864
1865 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1866 if (ret)
1867 return ret;
1868
1869 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1870 break;
1871 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1872 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1873 continue;
1874
1875 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1876 if (ret)
1877 return ret;
1878
1879 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1880 break;
1881 case SNDRV_PCM_TRIGGER_STOP:
1882 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1883 continue;
1884
1885 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1886 continue;
1887
1888 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1889 if (ret)
1890 return ret;
1891
1892 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1893 break;
1894 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08001895 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01001896 continue;
1897
1898 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1899 continue;
1900
1901 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1902 if (ret)
1903 return ret;
1904
1905 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1906 break;
1907 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1908 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1909 continue;
1910
1911 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1912 continue;
1913
1914 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1915 if (ret)
1916 return ret;
1917
1918 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1919 break;
1920 }
1921 }
1922
1923 return ret;
1924}
1925EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1926
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001927static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001928{
1929 struct snd_soc_pcm_runtime *fe = substream->private_data;
1930 int stream = substream->stream, ret;
1931 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1932
1933 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1934
1935 switch (trigger) {
1936 case SND_SOC_DPCM_TRIGGER_PRE:
1937 /* call trigger on the frontend before the backend. */
1938
Liam Girdwood103d84a2012-11-19 14:39:15 +00001939 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001940 fe->dai_link->name, cmd);
1941
1942 ret = soc_pcm_trigger(substream, cmd);
1943 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001944 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001945 goto out;
1946 }
1947
1948 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1949 break;
1950 case SND_SOC_DPCM_TRIGGER_POST:
1951 /* call trigger on the frontend after the backend. */
1952
1953 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1954 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001955 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001956 goto out;
1957 }
1958
Liam Girdwood103d84a2012-11-19 14:39:15 +00001959 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001960 fe->dai_link->name, cmd);
1961
1962 ret = soc_pcm_trigger(substream, cmd);
1963 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001964 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1965 /* bespoke trigger() - handles both FE and BEs */
1966
Liam Girdwood103d84a2012-11-19 14:39:15 +00001967 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001968 fe->dai_link->name, cmd);
1969
1970 ret = soc_pcm_bespoke_trigger(substream, cmd);
1971 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001972 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001973 goto out;
1974 }
1975 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01001976 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00001977 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01001978 fe->dai_link->name);
1979 ret = -EINVAL;
1980 goto out;
1981 }
1982
1983 switch (cmd) {
1984 case SNDRV_PCM_TRIGGER_START:
1985 case SNDRV_PCM_TRIGGER_RESUME:
1986 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1987 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1988 break;
1989 case SNDRV_PCM_TRIGGER_STOP:
1990 case SNDRV_PCM_TRIGGER_SUSPEND:
1991 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1992 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1993 break;
1994 }
1995
1996out:
1997 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1998 return ret;
1999}
2000
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002001static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2002{
2003 struct snd_soc_pcm_runtime *fe = substream->private_data;
2004 int stream = substream->stream;
2005
2006 /* if FE's runtime_update is already set, we're in race;
2007 * process this trigger later at exit
2008 */
2009 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2010 fe->dpcm[stream].trigger_pending = cmd + 1;
2011 return 0; /* delayed, assuming it's successful */
2012 }
2013
2014 /* we're alone, let's trigger */
2015 return dpcm_fe_dai_do_trigger(substream, cmd);
2016}
2017
Liam Girdwood23607022014-01-17 17:03:55 +00002018int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002019{
2020 struct snd_soc_dpcm *dpcm;
2021 int ret = 0;
2022
2023 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2024
2025 struct snd_soc_pcm_runtime *be = dpcm->be;
2026 struct snd_pcm_substream *be_substream =
2027 snd_soc_dpcm_get_substream(be, stream);
2028
2029 /* is this op for this BE ? */
2030 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2031 continue;
2032
2033 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2034 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2035 continue;
2036
Liam Girdwood103d84a2012-11-19 14:39:15 +00002037 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002038 dpcm->fe->dai_link->name);
2039
2040 ret = soc_pcm_prepare(be_substream);
2041 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002042 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002043 ret);
2044 break;
2045 }
2046
2047 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2048 }
2049 return ret;
2050}
2051
Mark Brown45c0a182012-05-09 21:46:27 +01002052static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002053{
2054 struct snd_soc_pcm_runtime *fe = substream->private_data;
2055 int stream = substream->stream, ret = 0;
2056
2057 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2058
Liam Girdwood103d84a2012-11-19 14:39:15 +00002059 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002060
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002061 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002062
2063 /* there is no point preparing this FE if there are no BEs */
2064 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002065 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002066 fe->dai_link->name);
2067 ret = -EINVAL;
2068 goto out;
2069 }
2070
2071 ret = dpcm_be_dai_prepare(fe, substream->stream);
2072 if (ret < 0)
2073 goto out;
2074
2075 /* call prepare on the frontend */
2076 ret = soc_pcm_prepare(substream);
2077 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002078 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002079 fe->dai_link->name);
2080 goto out;
2081 }
2082
2083 /* run the stream event for each BE */
2084 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2085 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
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
2091 return ret;
2092}
2093
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002094static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2095 unsigned int cmd, void *arg)
2096{
2097 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2098 struct snd_soc_platform *platform = rtd->platform;
2099
Mark Brownc5914b02013-10-30 17:47:39 -07002100 if (platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002101 return platform->driver->ops->ioctl(substream, cmd, arg);
2102 return snd_pcm_lib_ioctl(substream, cmd, arg);
2103}
2104
Liam Girdwood618dae12012-04-25 12:12:51 +01002105static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2106{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002107 struct snd_pcm_substream *substream =
2108 snd_soc_dpcm_get_substream(fe, stream);
2109 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002110 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01002111
Liam Girdwood103d84a2012-11-19 14:39:15 +00002112 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002113 stream ? "capture" : "playback", fe->dai_link->name);
2114
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002115 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2116 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002117 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002118 fe->dai_link->name);
2119
2120 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2121 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002122 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002123 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002124 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002125 fe->dai_link->name);
2126
2127 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2128 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002129 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002130 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002131
2132 err = dpcm_be_dai_hw_free(fe, stream);
2133 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002134 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002135
2136 err = dpcm_be_dai_shutdown(fe, stream);
2137 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002138 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002139
2140 /* run the stream event for each BE */
2141 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2142
2143 return 0;
2144}
2145
2146static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2147{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002148 struct snd_pcm_substream *substream =
2149 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002150 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002151 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002152 int ret;
2153
Liam Girdwood103d84a2012-11-19 14:39:15 +00002154 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002155 stream ? "capture" : "playback", fe->dai_link->name);
2156
2157 /* Only start the BE if the FE is ready */
2158 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2159 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2160 return -EINVAL;
2161
2162 /* startup must always be called for new BEs */
2163 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002164 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002165 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01002166
2167 /* keep going if FE state is > open */
2168 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2169 return 0;
2170
2171 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002172 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002173 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01002174
2175 /* keep going if FE state is > hw_params */
2176 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2177 return 0;
2178
2179
2180 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002181 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002182 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002183
2184 /* run the stream event for each BE */
2185 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2186
2187 /* keep going if FE state is > prepare */
2188 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2189 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2190 return 0;
2191
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002192 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2193 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002194 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002195 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01002196
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002197 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2198 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002199 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002200 goto hw_free;
2201 }
2202 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002203 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002204 fe->dai_link->name);
2205
2206 ret = dpcm_be_dai_trigger(fe, stream,
2207 SNDRV_PCM_TRIGGER_START);
2208 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002209 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002210 goto hw_free;
2211 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002212 }
2213
2214 return 0;
2215
2216hw_free:
2217 dpcm_be_dai_hw_free(fe, stream);
2218close:
2219 dpcm_be_dai_shutdown(fe, stream);
2220disconnect:
2221 /* disconnect any non started BEs */
2222 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2223 struct snd_soc_pcm_runtime *be = dpcm->be;
2224 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2225 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2226 }
2227
2228 return ret;
2229}
2230
2231static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2232{
2233 int ret;
2234
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002235 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002236 ret = dpcm_run_update_startup(fe, stream);
2237 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002238 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002239 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002240
2241 return ret;
2242}
2243
2244static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2245{
2246 int ret;
2247
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002248 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002249 ret = dpcm_run_update_shutdown(fe, stream);
2250 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002251 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002252 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002253
2254 return ret;
2255}
2256
2257/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2258 * any DAI links.
2259 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002260int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002261{
Liam Girdwood618dae12012-04-25 12:12:51 +01002262 int i, old, new, paths;
2263
Liam Girdwood618dae12012-04-25 12:12:51 +01002264 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2265 for (i = 0; i < card->num_rtd; i++) {
2266 struct snd_soc_dapm_widget_list *list;
2267 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2268
2269 /* make sure link is FE */
2270 if (!fe->dai_link->dynamic)
2271 continue;
2272
2273 /* only check active links */
2274 if (!fe->cpu_dai->active)
2275 continue;
2276
2277 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002278 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002279 fe->dai_link->name);
2280
2281 /* skip if FE doesn't have playback capability */
Qiao Zhou075207d2014-11-17 16:02:57 +08002282 if (!fe->cpu_dai->driver->playback.channels_min
2283 || !fe->codec_dai->driver->playback.channels_min)
2284 goto capture;
2285
2286 /* skip if FE isn't currently playing */
2287 if (!fe->cpu_dai->playback_active
2288 || !fe->codec_dai->playback_active)
Liam Girdwood618dae12012-04-25 12:12:51 +01002289 goto capture;
2290
2291 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2292 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002293 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002294 fe->dai_link->name, "playback");
2295 mutex_unlock(&card->mutex);
2296 return paths;
2297 }
2298
2299 /* update any new playback paths */
2300 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2301 if (new) {
2302 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2303 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2304 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2305 }
2306
2307 /* update any old playback paths */
2308 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2309 if (old) {
2310 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2311 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2312 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2313 }
2314
Qiao Zhou7ed9de72014-06-04 19:42:06 +08002315 dpcm_path_put(&list);
Liam Girdwood618dae12012-04-25 12:12:51 +01002316capture:
2317 /* skip if FE doesn't have capture capability */
Qiao Zhou075207d2014-11-17 16:02:57 +08002318 if (!fe->cpu_dai->driver->capture.channels_min
2319 || !fe->codec_dai->driver->capture.channels_min)
2320 continue;
2321
2322 /* skip if FE isn't currently capturing */
2323 if (!fe->cpu_dai->capture_active
2324 || !fe->codec_dai->capture_active)
Liam Girdwood618dae12012-04-25 12:12:51 +01002325 continue;
2326
2327 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2328 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002329 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002330 fe->dai_link->name, "capture");
2331 mutex_unlock(&card->mutex);
2332 return paths;
2333 }
2334
2335 /* update any new capture paths */
2336 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2337 if (new) {
2338 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2339 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2340 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2341 }
2342
2343 /* update any old capture paths */
2344 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2345 if (old) {
2346 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2347 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2348 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2349 }
2350
2351 dpcm_path_put(&list);
2352 }
2353
2354 mutex_unlock(&card->mutex);
2355 return 0;
2356}
Liam Girdwood01d75842012-04-25 12:12:49 +01002357int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2358{
2359 struct snd_soc_dpcm *dpcm;
2360 struct list_head *clients =
2361 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2362
2363 list_for_each_entry(dpcm, clients, list_be) {
2364
2365 struct snd_soc_pcm_runtime *be = dpcm->be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002366 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01002367
2368 if (be->dai_link->ignore_suspend)
2369 continue;
2370
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002371 for (i = 0; i < be->num_codecs; i++) {
2372 struct snd_soc_dai *dai = be->codec_dais[i];
2373 struct snd_soc_dai_driver *drv = dai->driver;
Liam Girdwood01d75842012-04-25 12:12:49 +01002374
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002375 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2376 be->dai_link->name);
2377
2378 if (drv->ops && drv->ops->digital_mute &&
2379 dai->playback_active)
2380 drv->ops->digital_mute(dai, mute);
2381 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002382 }
2383
2384 return 0;
2385}
2386
Mark Brown45c0a182012-05-09 21:46:27 +01002387static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002388{
2389 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2390 struct snd_soc_dpcm *dpcm;
2391 struct snd_soc_dapm_widget_list *list;
2392 int ret;
2393 int stream = fe_substream->stream;
2394
2395 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2396 fe->dpcm[stream].runtime = fe_substream->runtime;
2397
Qiao Zhou8f70e512014-09-10 17:54:07 +08002398 ret = dpcm_path_get(fe, stream, &list);
2399 if (ret < 0) {
2400 mutex_unlock(&fe->card->mutex);
2401 return ret;
2402 } else if (ret == 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002403 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002404 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002405 }
2406
2407 /* calculate valid and active FE <-> BE dpcms */
2408 dpcm_process_paths(fe, stream, &list, 1);
2409
2410 ret = dpcm_fe_dai_startup(fe_substream);
2411 if (ret < 0) {
2412 /* clean up all links */
2413 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2414 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2415
2416 dpcm_be_disconnect(fe, stream);
2417 fe->dpcm[stream].runtime = NULL;
2418 }
2419
2420 dpcm_clear_pending_state(fe, stream);
2421 dpcm_path_put(&list);
2422 mutex_unlock(&fe->card->mutex);
2423 return ret;
2424}
2425
Mark Brown45c0a182012-05-09 21:46:27 +01002426static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002427{
2428 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2429 struct snd_soc_dpcm *dpcm;
2430 int stream = fe_substream->stream, ret;
2431
2432 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2433 ret = dpcm_fe_dai_shutdown(fe_substream);
2434
2435 /* mark FE's links ready to prune */
2436 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2437 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2438
2439 dpcm_be_disconnect(fe, stream);
2440
2441 fe->dpcm[stream].runtime = NULL;
2442 mutex_unlock(&fe->card->mutex);
2443 return ret;
2444}
2445
Liam Girdwoodddee6272011-06-09 14:45:53 +01002446/* create a new pcm */
2447int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2448{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002449 struct snd_soc_platform *platform = rtd->platform;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002450 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002451 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2452 struct snd_pcm *pcm;
2453 char new_name[64];
2454 int ret = 0, playback = 0, capture = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002455 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002456
Liam Girdwood01d75842012-04-25 12:12:49 +01002457 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002458 playback = rtd->dai_link->dpcm_playback;
2459 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002460 } else {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002461 for (i = 0; i < rtd->num_codecs; i++) {
2462 codec_dai = rtd->codec_dais[i];
2463 if (codec_dai->driver->playback.channels_min)
2464 playback = 1;
2465 if (codec_dai->driver->capture.channels_min)
2466 capture = 1;
2467 }
2468
2469 capture = capture && cpu_dai->driver->capture.channels_min;
2470 playback = playback && cpu_dai->driver->playback.channels_min;
Liam Girdwood01d75842012-04-25 12:12:49 +01002471 }
Sangsu Parka5002312012-01-02 17:15:10 +09002472
Fabio Estevamd6bead02013-08-29 10:32:13 -03002473 if (rtd->dai_link->playback_only) {
2474 playback = 1;
2475 capture = 0;
2476 }
2477
2478 if (rtd->dai_link->capture_only) {
2479 playback = 0;
2480 capture = 1;
2481 }
2482
Liam Girdwood01d75842012-04-25 12:12:49 +01002483 /* create the PCM */
2484 if (rtd->dai_link->no_pcm) {
2485 snprintf(new_name, sizeof(new_name), "(%s)",
2486 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002487
Liam Girdwood01d75842012-04-25 12:12:49 +01002488 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2489 playback, capture, &pcm);
2490 } else {
2491 if (rtd->dai_link->dynamic)
2492 snprintf(new_name, sizeof(new_name), "%s (*)",
2493 rtd->dai_link->stream_name);
2494 else
2495 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002496 rtd->dai_link->stream_name,
2497 (rtd->num_codecs > 1) ?
2498 "multicodec" : rtd->codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002499
Liam Girdwood01d75842012-04-25 12:12:49 +01002500 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2501 capture, &pcm);
2502 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002503 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002504 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002505 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002506 return ret;
2507 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002508 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002509
2510 /* DAPM dai link stream work */
2511 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2512
2513 rtd->pcm = pcm;
2514 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002515
2516 if (rtd->dai_link->no_pcm) {
2517 if (playback)
2518 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2519 if (capture)
2520 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2521 goto out;
2522 }
2523
2524 /* ASoC PCM operations */
2525 if (rtd->dai_link->dynamic) {
2526 rtd->ops.open = dpcm_fe_dai_open;
2527 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2528 rtd->ops.prepare = dpcm_fe_dai_prepare;
2529 rtd->ops.trigger = dpcm_fe_dai_trigger;
2530 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2531 rtd->ops.close = dpcm_fe_dai_close;
2532 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002533 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002534 } else {
2535 rtd->ops.open = soc_pcm_open;
2536 rtd->ops.hw_params = soc_pcm_hw_params;
2537 rtd->ops.prepare = soc_pcm_prepare;
2538 rtd->ops.trigger = soc_pcm_trigger;
2539 rtd->ops.hw_free = soc_pcm_hw_free;
2540 rtd->ops.close = soc_pcm_close;
2541 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002542 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002543 }
2544
Liam Girdwoodddee6272011-06-09 14:45:53 +01002545 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002546 rtd->ops.ack = platform->driver->ops->ack;
2547 rtd->ops.copy = platform->driver->ops->copy;
2548 rtd->ops.silence = platform->driver->ops->silence;
2549 rtd->ops.page = platform->driver->ops->page;
2550 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002551 }
2552
2553 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002554 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002555
2556 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002557 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002558
2559 if (platform->driver->pcm_new) {
2560 ret = platform->driver->pcm_new(rtd);
2561 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002562 dev_err(platform->dev,
2563 "ASoC: pcm constructor failed: %d\n",
2564 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002565 return ret;
2566 }
2567 }
2568
2569 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002570out:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002571 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2572 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2573 cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002574 return ret;
2575}
Liam Girdwood01d75842012-04-25 12:12:49 +01002576
2577/* is the current PCM operation for this FE ? */
2578int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2579{
2580 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2581 return 1;
2582 return 0;
2583}
2584EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2585
2586/* is the current PCM operation for this BE ? */
2587int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2588 struct snd_soc_pcm_runtime *be, int stream)
2589{
2590 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2591 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2592 be->dpcm[stream].runtime_update))
2593 return 1;
2594 return 0;
2595}
2596EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2597
2598/* get the substream for this BE */
2599struct snd_pcm_substream *
2600 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2601{
2602 return be->pcm->streams[stream].substream;
2603}
2604EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2605
2606/* get the BE runtime state */
2607enum snd_soc_dpcm_state
2608 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2609{
2610 return be->dpcm[stream].state;
2611}
2612EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2613
2614/* set the BE runtime state */
2615void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2616 int stream, enum snd_soc_dpcm_state state)
2617{
2618 be->dpcm[stream].state = state;
2619}
2620EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2621
2622/*
2623 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2624 * are not running, paused or suspended for the specified stream direction.
2625 */
2626int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2627 struct snd_soc_pcm_runtime *be, int stream)
2628{
2629 struct snd_soc_dpcm *dpcm;
2630 int state;
2631
2632 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2633
2634 if (dpcm->fe == fe)
2635 continue;
2636
2637 state = dpcm->fe->dpcm[stream].state;
2638 if (state == SND_SOC_DPCM_STATE_START ||
2639 state == SND_SOC_DPCM_STATE_PAUSED ||
2640 state == SND_SOC_DPCM_STATE_SUSPEND)
2641 return 0;
2642 }
2643
2644 /* it's safe to free/stop this BE DAI */
2645 return 1;
2646}
2647EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2648
2649/*
2650 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2651 * running, paused or suspended for the specified stream direction.
2652 */
2653int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2654 struct snd_soc_pcm_runtime *be, int stream)
2655{
2656 struct snd_soc_dpcm *dpcm;
2657 int state;
2658
2659 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2660
2661 if (dpcm->fe == fe)
2662 continue;
2663
2664 state = dpcm->fe->dpcm[stream].state;
2665 if (state == SND_SOC_DPCM_STATE_START ||
2666 state == SND_SOC_DPCM_STATE_PAUSED ||
2667 state == SND_SOC_DPCM_STATE_SUSPEND ||
2668 state == SND_SOC_DPCM_STATE_PREPARE)
2669 return 0;
2670 }
2671
2672 /* it's safe to change hw_params */
2673 return 1;
2674}
2675EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002676
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002677int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2678 int cmd, struct snd_soc_platform *platform)
2679{
Mark Brownc5914b02013-10-30 17:47:39 -07002680 if (platform->driver->ops && platform->driver->ops->trigger)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002681 return platform->driver->ops->trigger(substream, cmd);
2682 return 0;
2683}
2684EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2685
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002686#ifdef CONFIG_DEBUG_FS
2687static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2688{
2689 switch (state) {
2690 case SND_SOC_DPCM_STATE_NEW:
2691 return "new";
2692 case SND_SOC_DPCM_STATE_OPEN:
2693 return "open";
2694 case SND_SOC_DPCM_STATE_HW_PARAMS:
2695 return "hw_params";
2696 case SND_SOC_DPCM_STATE_PREPARE:
2697 return "prepare";
2698 case SND_SOC_DPCM_STATE_START:
2699 return "start";
2700 case SND_SOC_DPCM_STATE_STOP:
2701 return "stop";
2702 case SND_SOC_DPCM_STATE_SUSPEND:
2703 return "suspend";
2704 case SND_SOC_DPCM_STATE_PAUSED:
2705 return "paused";
2706 case SND_SOC_DPCM_STATE_HW_FREE:
2707 return "hw_free";
2708 case SND_SOC_DPCM_STATE_CLOSE:
2709 return "close";
2710 }
2711
2712 return "unknown";
2713}
2714
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002715static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2716 int stream, char *buf, size_t size)
2717{
2718 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2719 struct snd_soc_dpcm *dpcm;
2720 ssize_t offset = 0;
2721
2722 /* FE state */
2723 offset += snprintf(buf + offset, size - offset,
2724 "[%s - %s]\n", fe->dai_link->name,
2725 stream ? "Capture" : "Playback");
2726
2727 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2728 dpcm_state_string(fe->dpcm[stream].state));
2729
2730 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2731 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2732 offset += snprintf(buf + offset, size - offset,
2733 "Hardware Params: "
2734 "Format = %s, Channels = %d, Rate = %d\n",
2735 snd_pcm_format_name(params_format(params)),
2736 params_channels(params),
2737 params_rate(params));
2738
2739 /* BEs state */
2740 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2741
2742 if (list_empty(&fe->dpcm[stream].be_clients)) {
2743 offset += snprintf(buf + offset, size - offset,
2744 " No active DSP links\n");
2745 goto out;
2746 }
2747
2748 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2749 struct snd_soc_pcm_runtime *be = dpcm->be;
2750 params = &dpcm->hw_params;
2751
2752 offset += snprintf(buf + offset, size - offset,
2753 "- %s\n", be->dai_link->name);
2754
2755 offset += snprintf(buf + offset, size - offset,
2756 " State: %s\n",
2757 dpcm_state_string(be->dpcm[stream].state));
2758
2759 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2760 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2761 offset += snprintf(buf + offset, size - offset,
2762 " Hardware Params: "
2763 "Format = %s, Channels = %d, Rate = %d\n",
2764 snd_pcm_format_name(params_format(params)),
2765 params_channels(params),
2766 params_rate(params));
2767 }
2768
2769out:
2770 return offset;
2771}
2772
2773static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2774 size_t count, loff_t *ppos)
2775{
2776 struct snd_soc_pcm_runtime *fe = file->private_data;
2777 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2778 char *buf;
2779
2780 buf = kmalloc(out_count, GFP_KERNEL);
2781 if (!buf)
2782 return -ENOMEM;
2783
2784 if (fe->cpu_dai->driver->playback.channels_min)
2785 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2786 buf + offset, out_count - offset);
2787
2788 if (fe->cpu_dai->driver->capture.channels_min)
2789 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2790 buf + offset, out_count - offset);
2791
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002792 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002793
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002794 kfree(buf);
2795 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002796}
2797
2798static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002799 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002800 .read = dpcm_state_read_file,
2801 .llseek = default_llseek,
2802};
2803
2804int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2805{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002806 if (!rtd->dai_link)
2807 return 0;
2808
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002809 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2810 rtd->card->debugfs_card_root);
2811 if (!rtd->debugfs_dpcm_root) {
2812 dev_dbg(rtd->dev,
2813 "ASoC: Failed to create dpcm debugfs directory %s\n",
2814 rtd->dai_link->name);
2815 return -EINVAL;
2816 }
2817
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002818 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002819 rtd->debugfs_dpcm_root,
2820 rtd, &dpcm_state_fops);
2821
2822 return 0;
2823}
2824#endif