]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - sound/soc/soc-pcm.c
Merge remote-tracking branches 'asoc/topic/nau8825' and 'asoc/topic/pxa' into asoc...
[karo-tx-linux.git] / sound / soc / soc-pcm.c
1 /*
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>
10  *          Mark Brown <broonie@opensource.wolfsonmicro.com>
11  *
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>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
26 #include <linux/export.h>
27 #include <linux/debugfs.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/soc-dpcm.h>
33 #include <sound/initval.h>
34
35 #define DPCM_MAX_BE_USERS       8
36
37 /*
38  * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
39  *
40  * Returns true if the DAI supports the indicated stream type.
41  */
42 static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
43 {
44         struct snd_soc_pcm_stream *codec_stream;
45
46         if (stream == SNDRV_PCM_STREAM_PLAYBACK)
47                 codec_stream = &dai->driver->playback;
48         else
49                 codec_stream = &dai->driver->capture;
50
51         /* If the codec specifies any rate at all, it supports the stream. */
52         return codec_stream->rates;
53 }
54
55 /**
56  * snd_soc_runtime_activate() - Increment active count for PCM runtime components
57  * @rtd: ASoC PCM runtime that is activated
58  * @stream: Direction of the PCM stream
59  *
60  * Increments the active count for all the DAIs and components attached to a PCM
61  * runtime. Should typically be called when a stream is opened.
62  *
63  * Must be called with the rtd->pcm_mutex being held
64  */
65 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
66 {
67         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
68         int i;
69
70         lockdep_assert_held(&rtd->pcm_mutex);
71
72         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
73                 cpu_dai->playback_active++;
74                 for (i = 0; i < rtd->num_codecs; i++)
75                         rtd->codec_dais[i]->playback_active++;
76         } else {
77                 cpu_dai->capture_active++;
78                 for (i = 0; i < rtd->num_codecs; i++)
79                         rtd->codec_dais[i]->capture_active++;
80         }
81
82         cpu_dai->active++;
83         cpu_dai->component->active++;
84         for (i = 0; i < rtd->num_codecs; i++) {
85                 rtd->codec_dais[i]->active++;
86                 rtd->codec_dais[i]->component->active++;
87         }
88 }
89
90 /**
91  * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
92  * @rtd: ASoC PCM runtime that is deactivated
93  * @stream: Direction of the PCM stream
94  *
95  * Decrements the active count for all the DAIs and components attached to a PCM
96  * runtime. Should typically be called when a stream is closed.
97  *
98  * Must be called with the rtd->pcm_mutex being held
99  */
100 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
101 {
102         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
103         int i;
104
105         lockdep_assert_held(&rtd->pcm_mutex);
106
107         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
108                 cpu_dai->playback_active--;
109                 for (i = 0; i < rtd->num_codecs; i++)
110                         rtd->codec_dais[i]->playback_active--;
111         } else {
112                 cpu_dai->capture_active--;
113                 for (i = 0; i < rtd->num_codecs; i++)
114                         rtd->codec_dais[i]->capture_active--;
115         }
116
117         cpu_dai->active--;
118         cpu_dai->component->active--;
119         for (i = 0; i < rtd->num_codecs; i++) {
120                 rtd->codec_dais[i]->component->active--;
121                 rtd->codec_dais[i]->active--;
122         }
123 }
124
125 /**
126  * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
127  * @rtd: The ASoC PCM runtime that should be checked.
128  *
129  * This function checks whether the power down delay should be ignored for a
130  * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
131  * been configured to ignore the delay, or if none of the components benefits
132  * from having the delay.
133  */
134 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
135 {
136         int i;
137         bool ignore = true;
138
139         if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
140                 return true;
141
142         for (i = 0; i < rtd->num_codecs; i++)
143                 ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
144
145         return rtd->cpu_dai->component->ignore_pmdown_time && ignore;
146 }
147
148 /**
149  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
150  * @substream: the pcm substream
151  * @hw: the hardware parameters
152  *
153  * Sets the substream runtime hardware parameters.
154  */
155 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
156         const struct snd_pcm_hardware *hw)
157 {
158         struct snd_pcm_runtime *runtime = substream->runtime;
159         runtime->hw.info = hw->info;
160         runtime->hw.formats = hw->formats;
161         runtime->hw.period_bytes_min = hw->period_bytes_min;
162         runtime->hw.period_bytes_max = hw->period_bytes_max;
163         runtime->hw.periods_min = hw->periods_min;
164         runtime->hw.periods_max = hw->periods_max;
165         runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
166         runtime->hw.fifo_size = hw->fifo_size;
167         return 0;
168 }
169 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
170
171 /* DPCM stream event, send event to FE and all active BEs. */
172 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
173         int event)
174 {
175         struct snd_soc_dpcm *dpcm;
176
177         list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
178
179                 struct snd_soc_pcm_runtime *be = dpcm->be;
180
181                 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
182                                 be->dai_link->name, event, dir);
183
184                 snd_soc_dapm_stream_event(be, dir, event);
185         }
186
187         snd_soc_dapm_stream_event(fe, dir, event);
188
189         return 0;
190 }
191
192 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
193                                         struct snd_soc_dai *soc_dai)
194 {
195         struct snd_soc_pcm_runtime *rtd = substream->private_data;
196         int ret;
197
198         if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
199                                 rtd->dai_link->symmetric_rates)) {
200                 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
201                                 soc_dai->rate);
202
203                 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
204                                                 SNDRV_PCM_HW_PARAM_RATE,
205                                                 soc_dai->rate, soc_dai->rate);
206                 if (ret < 0) {
207                         dev_err(soc_dai->dev,
208                                 "ASoC: Unable to apply rate constraint: %d\n",
209                                 ret);
210                         return ret;
211                 }
212         }
213
214         if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
215                                 rtd->dai_link->symmetric_channels)) {
216                 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
217                                 soc_dai->channels);
218
219                 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
220                                                 SNDRV_PCM_HW_PARAM_CHANNELS,
221                                                 soc_dai->channels,
222                                                 soc_dai->channels);
223                 if (ret < 0) {
224                         dev_err(soc_dai->dev,
225                                 "ASoC: Unable to apply channel symmetry constraint: %d\n",
226                                 ret);
227                         return ret;
228                 }
229         }
230
231         if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
232                                 rtd->dai_link->symmetric_samplebits)) {
233                 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
234                                 soc_dai->sample_bits);
235
236                 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
237                                                 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
238                                                 soc_dai->sample_bits,
239                                                 soc_dai->sample_bits);
240                 if (ret < 0) {
241                         dev_err(soc_dai->dev,
242                                 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
243                                 ret);
244                         return ret;
245                 }
246         }
247
248         return 0;
249 }
250
251 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
252                                 struct snd_pcm_hw_params *params)
253 {
254         struct snd_soc_pcm_runtime *rtd = substream->private_data;
255         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
256         unsigned int rate, channels, sample_bits, symmetry, i;
257
258         rate = params_rate(params);
259         channels = params_channels(params);
260         sample_bits = snd_pcm_format_physical_width(params_format(params));
261
262         /* reject unmatched parameters when applying symmetry */
263         symmetry = cpu_dai->driver->symmetric_rates ||
264                 rtd->dai_link->symmetric_rates;
265
266         for (i = 0; i < rtd->num_codecs; i++)
267                 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
268
269         if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
270                 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
271                                 cpu_dai->rate, rate);
272                 return -EINVAL;
273         }
274
275         symmetry = cpu_dai->driver->symmetric_channels ||
276                 rtd->dai_link->symmetric_channels;
277
278         for (i = 0; i < rtd->num_codecs; i++)
279                 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
280
281         if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
282                 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
283                                 cpu_dai->channels, channels);
284                 return -EINVAL;
285         }
286
287         symmetry = cpu_dai->driver->symmetric_samplebits ||
288                 rtd->dai_link->symmetric_samplebits;
289
290         for (i = 0; i < rtd->num_codecs; i++)
291                 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
292
293         if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
294                 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
295                                 cpu_dai->sample_bits, sample_bits);
296                 return -EINVAL;
297         }
298
299         return 0;
300 }
301
302 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
303 {
304         struct snd_soc_pcm_runtime *rtd = substream->private_data;
305         struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
306         struct snd_soc_dai_link *link = rtd->dai_link;
307         unsigned int symmetry, i;
308
309         symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
310                 cpu_driver->symmetric_channels || link->symmetric_channels ||
311                 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
312
313         for (i = 0; i < rtd->num_codecs; i++)
314                 symmetry = symmetry ||
315                         rtd->codec_dais[i]->driver->symmetric_rates ||
316                         rtd->codec_dais[i]->driver->symmetric_channels ||
317                         rtd->codec_dais[i]->driver->symmetric_samplebits;
318
319         return symmetry;
320 }
321
322 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
323 {
324         struct snd_soc_pcm_runtime *rtd = substream->private_data;
325         int ret;
326
327         if (!bits)
328                 return;
329
330         ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
331         if (ret != 0)
332                 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
333                                  bits, ret);
334 }
335
336 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
337 {
338         struct snd_soc_pcm_runtime *rtd = substream->private_data;
339         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
340         struct snd_soc_dai *codec_dai;
341         int i;
342         unsigned int bits = 0, cpu_bits;
343
344         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
345                 for (i = 0; i < rtd->num_codecs; i++) {
346                         codec_dai = rtd->codec_dais[i];
347                         if (codec_dai->driver->playback.sig_bits == 0) {
348                                 bits = 0;
349                                 break;
350                         }
351                         bits = max(codec_dai->driver->playback.sig_bits, bits);
352                 }
353                 cpu_bits = cpu_dai->driver->playback.sig_bits;
354         } else {
355                 for (i = 0; i < rtd->num_codecs; i++) {
356                         codec_dai = rtd->codec_dais[i];
357                         if (codec_dai->driver->capture.sig_bits == 0) {
358                                 bits = 0;
359                                 break;
360                         }
361                         bits = max(codec_dai->driver->capture.sig_bits, bits);
362                 }
363                 cpu_bits = cpu_dai->driver->capture.sig_bits;
364         }
365
366         soc_pcm_set_msb(substream, bits);
367         soc_pcm_set_msb(substream, cpu_bits);
368 }
369
370 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
371 {
372         struct snd_pcm_runtime *runtime = substream->runtime;
373         struct snd_pcm_hardware *hw = &runtime->hw;
374         struct snd_soc_pcm_runtime *rtd = substream->private_data;
375         struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
376         struct snd_soc_dai_driver *codec_dai_drv;
377         struct snd_soc_pcm_stream *codec_stream;
378         struct snd_soc_pcm_stream *cpu_stream;
379         unsigned int chan_min = 0, chan_max = UINT_MAX;
380         unsigned int rate_min = 0, rate_max = UINT_MAX;
381         unsigned int rates = UINT_MAX;
382         u64 formats = ULLONG_MAX;
383         int i;
384
385         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
386                 cpu_stream = &cpu_dai_drv->playback;
387         else
388                 cpu_stream = &cpu_dai_drv->capture;
389
390         /* first calculate min/max only for CODECs in the DAI link */
391         for (i = 0; i < rtd->num_codecs; i++) {
392
393                 /*
394                  * Skip CODECs which don't support the current stream type.
395                  * Otherwise, since the rate, channel, and format values will
396                  * zero in that case, we would have no usable settings left,
397                  * causing the resulting setup to fail.
398                  * At least one CODEC should match, otherwise we should have
399                  * bailed out on a higher level, since there would be no
400                  * CODEC to support the transfer direction in that case.
401                  */
402                 if (!snd_soc_dai_stream_valid(rtd->codec_dais[i],
403                                               substream->stream))
404                         continue;
405
406                 codec_dai_drv = rtd->codec_dais[i]->driver;
407                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
408                         codec_stream = &codec_dai_drv->playback;
409                 else
410                         codec_stream = &codec_dai_drv->capture;
411                 chan_min = max(chan_min, codec_stream->channels_min);
412                 chan_max = min(chan_max, codec_stream->channels_max);
413                 rate_min = max(rate_min, codec_stream->rate_min);
414                 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
415                 formats &= codec_stream->formats;
416                 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
417         }
418
419         /*
420          * chan min/max cannot be enforced if there are multiple CODEC DAIs
421          * connected to a single CPU DAI, use CPU DAI's directly and let
422          * channel allocation be fixed up later
423          */
424         if (rtd->num_codecs > 1) {
425                 chan_min = cpu_stream->channels_min;
426                 chan_max = cpu_stream->channels_max;
427         }
428
429         hw->channels_min = max(chan_min, cpu_stream->channels_min);
430         hw->channels_max = min(chan_max, cpu_stream->channels_max);
431         if (hw->formats)
432                 hw->formats &= formats & cpu_stream->formats;
433         else
434                 hw->formats = formats & cpu_stream->formats;
435         hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
436
437         snd_pcm_limit_hw_rates(runtime);
438
439         hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
440         hw->rate_min = max(hw->rate_min, rate_min);
441         hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
442         hw->rate_max = min_not_zero(hw->rate_max, rate_max);
443 }
444
445 /*
446  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
447  * then initialized and any private data can be allocated. This also calls
448  * startup for the cpu DAI, platform, machine and codec DAI.
449  */
450 static int soc_pcm_open(struct snd_pcm_substream *substream)
451 {
452         struct snd_soc_pcm_runtime *rtd = substream->private_data;
453         struct snd_pcm_runtime *runtime = substream->runtime;
454         struct snd_soc_platform *platform = rtd->platform;
455         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
456         struct snd_soc_dai *codec_dai;
457         const char *codec_dai_name = "multicodec";
458         int i, ret = 0;
459
460         pinctrl_pm_select_default_state(cpu_dai->dev);
461         for (i = 0; i < rtd->num_codecs; i++)
462                 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
463         pm_runtime_get_sync(cpu_dai->dev);
464         for (i = 0; i < rtd->num_codecs; i++)
465                 pm_runtime_get_sync(rtd->codec_dais[i]->dev);
466         pm_runtime_get_sync(platform->dev);
467
468         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
469
470         /* startup the audio subsystem */
471         if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
472                 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
473                 if (ret < 0) {
474                         dev_err(cpu_dai->dev, "ASoC: can't open interface"
475                                 " %s: %d\n", cpu_dai->name, ret);
476                         goto out;
477                 }
478         }
479
480         if (platform->driver->ops && platform->driver->ops->open) {
481                 ret = platform->driver->ops->open(substream);
482                 if (ret < 0) {
483                         dev_err(platform->dev, "ASoC: can't open platform"
484                                 " %s: %d\n", platform->component.name, ret);
485                         goto platform_err;
486                 }
487         }
488
489         for (i = 0; i < rtd->num_codecs; i++) {
490                 codec_dai = rtd->codec_dais[i];
491                 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
492                         ret = codec_dai->driver->ops->startup(substream,
493                                                               codec_dai);
494                         if (ret < 0) {
495                                 dev_err(codec_dai->dev,
496                                         "ASoC: can't open codec %s: %d\n",
497                                         codec_dai->name, ret);
498                                 goto codec_dai_err;
499                         }
500                 }
501
502                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
503                         codec_dai->tx_mask = 0;
504                 else
505                         codec_dai->rx_mask = 0;
506         }
507
508         if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
509                 ret = rtd->dai_link->ops->startup(substream);
510                 if (ret < 0) {
511                         pr_err("ASoC: %s startup failed: %d\n",
512                                rtd->dai_link->name, ret);
513                         goto machine_err;
514                 }
515         }
516
517         /* Dynamic PCM DAI links compat checks use dynamic capabilities */
518         if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
519                 goto dynamic;
520
521         /* Check that the codec and cpu DAIs are compatible */
522         soc_pcm_init_runtime_hw(substream);
523
524         if (rtd->num_codecs == 1)
525                 codec_dai_name = rtd->codec_dai->name;
526
527         if (soc_pcm_has_symmetry(substream))
528                 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
529
530         ret = -EINVAL;
531         if (!runtime->hw.rates) {
532                 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
533                         codec_dai_name, cpu_dai->name);
534                 goto config_err;
535         }
536         if (!runtime->hw.formats) {
537                 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
538                         codec_dai_name, cpu_dai->name);
539                 goto config_err;
540         }
541         if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
542             runtime->hw.channels_min > runtime->hw.channels_max) {
543                 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
544                                 codec_dai_name, cpu_dai->name);
545                 goto config_err;
546         }
547
548         soc_pcm_apply_msb(substream);
549
550         /* Symmetry only applies if we've already got an active stream. */
551         if (cpu_dai->active) {
552                 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
553                 if (ret != 0)
554                         goto config_err;
555         }
556
557         for (i = 0; i < rtd->num_codecs; i++) {
558                 if (rtd->codec_dais[i]->active) {
559                         ret = soc_pcm_apply_symmetry(substream,
560                                                      rtd->codec_dais[i]);
561                         if (ret != 0)
562                                 goto config_err;
563                 }
564         }
565
566         pr_debug("ASoC: %s <-> %s info:\n",
567                         codec_dai_name, cpu_dai->name);
568         pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
569         pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
570                  runtime->hw.channels_max);
571         pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
572                  runtime->hw.rate_max);
573
574 dynamic:
575
576         snd_soc_runtime_activate(rtd, substream->stream);
577
578         mutex_unlock(&rtd->pcm_mutex);
579         return 0;
580
581 config_err:
582         if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
583                 rtd->dai_link->ops->shutdown(substream);
584
585 machine_err:
586         i = rtd->num_codecs;
587
588 codec_dai_err:
589         while (--i >= 0) {
590                 codec_dai = rtd->codec_dais[i];
591                 if (codec_dai->driver->ops->shutdown)
592                         codec_dai->driver->ops->shutdown(substream, codec_dai);
593         }
594
595         if (platform->driver->ops && platform->driver->ops->close)
596                 platform->driver->ops->close(substream);
597
598 platform_err:
599         if (cpu_dai->driver->ops->shutdown)
600                 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
601 out:
602         mutex_unlock(&rtd->pcm_mutex);
603
604         pm_runtime_put(platform->dev);
605         for (i = 0; i < rtd->num_codecs; i++)
606                 pm_runtime_put(rtd->codec_dais[i]->dev);
607         pm_runtime_put(cpu_dai->dev);
608         for (i = 0; i < rtd->num_codecs; i++) {
609                 if (!rtd->codec_dais[i]->active)
610                         pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
611         }
612         if (!cpu_dai->active)
613                 pinctrl_pm_select_sleep_state(cpu_dai->dev);
614
615         return ret;
616 }
617
618 /*
619  * Power down the audio subsystem pmdown_time msecs after close is called.
620  * This is to ensure there are no pops or clicks in between any music tracks
621  * due to DAPM power cycling.
622  */
623 static void close_delayed_work(struct work_struct *work)
624 {
625         struct snd_soc_pcm_runtime *rtd =
626                         container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
627         struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
628
629         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
630
631         dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
632                  codec_dai->driver->playback.stream_name,
633                  codec_dai->playback_active ? "active" : "inactive",
634                  rtd->pop_wait ? "yes" : "no");
635
636         /* are we waiting on this codec DAI stream */
637         if (rtd->pop_wait == 1) {
638                 rtd->pop_wait = 0;
639                 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
640                                           SND_SOC_DAPM_STREAM_STOP);
641         }
642
643         mutex_unlock(&rtd->pcm_mutex);
644 }
645
646 /*
647  * Called by ALSA when a PCM substream is closed. Private data can be
648  * freed here. The cpu DAI, codec DAI, machine and platform are also
649  * shutdown.
650  */
651 static int soc_pcm_close(struct snd_pcm_substream *substream)
652 {
653         struct snd_soc_pcm_runtime *rtd = substream->private_data;
654         struct snd_soc_platform *platform = rtd->platform;
655         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
656         struct snd_soc_dai *codec_dai;
657         int i;
658
659         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
660
661         snd_soc_runtime_deactivate(rtd, substream->stream);
662
663         /* clear the corresponding DAIs rate when inactive */
664         if (!cpu_dai->active)
665                 cpu_dai->rate = 0;
666
667         for (i = 0; i < rtd->num_codecs; i++) {
668                 codec_dai = rtd->codec_dais[i];
669                 if (!codec_dai->active)
670                         codec_dai->rate = 0;
671         }
672
673         snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
674
675         if (cpu_dai->driver->ops->shutdown)
676                 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
677
678         for (i = 0; i < rtd->num_codecs; i++) {
679                 codec_dai = rtd->codec_dais[i];
680                 if (codec_dai->driver->ops->shutdown)
681                         codec_dai->driver->ops->shutdown(substream, codec_dai);
682         }
683
684         if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
685                 rtd->dai_link->ops->shutdown(substream);
686
687         if (platform->driver->ops && platform->driver->ops->close)
688                 platform->driver->ops->close(substream);
689
690         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
691                 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
692                         /* powered down playback stream now */
693                         snd_soc_dapm_stream_event(rtd,
694                                                   SNDRV_PCM_STREAM_PLAYBACK,
695                                                   SND_SOC_DAPM_STREAM_STOP);
696                 } else {
697                         /* start delayed pop wq here for playback streams */
698                         rtd->pop_wait = 1;
699                         queue_delayed_work(system_power_efficient_wq,
700                                            &rtd->delayed_work,
701                                            msecs_to_jiffies(rtd->pmdown_time));
702                 }
703         } else {
704                 /* capture streams can be powered down now */
705                 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
706                                           SND_SOC_DAPM_STREAM_STOP);
707         }
708
709         mutex_unlock(&rtd->pcm_mutex);
710
711         pm_runtime_put(platform->dev);
712         for (i = 0; i < rtd->num_codecs; i++)
713                 pm_runtime_put(rtd->codec_dais[i]->dev);
714         pm_runtime_put(cpu_dai->dev);
715         for (i = 0; i < rtd->num_codecs; i++) {
716                 if (!rtd->codec_dais[i]->active)
717                         pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
718         }
719         if (!cpu_dai->active)
720                 pinctrl_pm_select_sleep_state(cpu_dai->dev);
721
722         return 0;
723 }
724
725 /*
726  * Called by ALSA when the PCM substream is prepared, can set format, sample
727  * rate, etc.  This function is non atomic and can be called multiple times,
728  * it can refer to the runtime info.
729  */
730 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
731 {
732         struct snd_soc_pcm_runtime *rtd = substream->private_data;
733         struct snd_soc_platform *platform = rtd->platform;
734         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
735         struct snd_soc_dai *codec_dai;
736         int i, ret = 0;
737
738         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
739
740         if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
741                 ret = rtd->dai_link->ops->prepare(substream);
742                 if (ret < 0) {
743                         dev_err(rtd->card->dev, "ASoC: machine prepare error:"
744                                 " %d\n", ret);
745                         goto out;
746                 }
747         }
748
749         if (platform->driver->ops && platform->driver->ops->prepare) {
750                 ret = platform->driver->ops->prepare(substream);
751                 if (ret < 0) {
752                         dev_err(platform->dev, "ASoC: platform prepare error:"
753                                 " %d\n", ret);
754                         goto out;
755                 }
756         }
757
758         for (i = 0; i < rtd->num_codecs; i++) {
759                 codec_dai = rtd->codec_dais[i];
760                 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
761                         ret = codec_dai->driver->ops->prepare(substream,
762                                                               codec_dai);
763                         if (ret < 0) {
764                                 dev_err(codec_dai->dev,
765                                         "ASoC: codec DAI prepare error: %d\n",
766                                         ret);
767                                 goto out;
768                         }
769                 }
770         }
771
772         if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
773                 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
774                 if (ret < 0) {
775                         dev_err(cpu_dai->dev,
776                                 "ASoC: cpu DAI prepare error: %d\n", ret);
777                         goto out;
778                 }
779         }
780
781         /* cancel any delayed stream shutdown that is pending */
782         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
783             rtd->pop_wait) {
784                 rtd->pop_wait = 0;
785                 cancel_delayed_work(&rtd->delayed_work);
786         }
787
788         snd_soc_dapm_stream_event(rtd, substream->stream,
789                         SND_SOC_DAPM_STREAM_START);
790
791         for (i = 0; i < rtd->num_codecs; i++)
792                 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
793                                          substream->stream);
794         snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
795
796 out:
797         mutex_unlock(&rtd->pcm_mutex);
798         return ret;
799 }
800
801 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
802                                        unsigned int mask)
803 {
804         struct snd_interval *interval;
805         int channels = hweight_long(mask);
806
807         interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
808         interval->min = channels;
809         interval->max = channels;
810 }
811
812 int soc_dai_hw_params(struct snd_pcm_substream *substream,
813                       struct snd_pcm_hw_params *params,
814                       struct snd_soc_dai *dai)
815 {
816         int ret;
817
818         if (dai->driver->ops && dai->driver->ops->hw_params) {
819                 ret = dai->driver->ops->hw_params(substream, params, dai);
820                 if (ret < 0) {
821                         dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
822                                 dai->name, ret);
823                         return ret;
824                 }
825         }
826
827         return 0;
828 }
829
830 /*
831  * Called by ALSA when the hardware params are set by application. This
832  * function can also be called multiple times and can allocate buffers
833  * (using snd_pcm_lib_* ). It's non-atomic.
834  */
835 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
836                                 struct snd_pcm_hw_params *params)
837 {
838         struct snd_soc_pcm_runtime *rtd = substream->private_data;
839         struct snd_soc_platform *platform = rtd->platform;
840         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
841         int i, ret = 0;
842
843         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
844
845         ret = soc_pcm_params_symmetry(substream, params);
846         if (ret)
847                 goto out;
848
849         if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
850                 ret = rtd->dai_link->ops->hw_params(substream, params);
851                 if (ret < 0) {
852                         dev_err(rtd->card->dev, "ASoC: machine hw_params"
853                                 " failed: %d\n", ret);
854                         goto out;
855                 }
856         }
857
858         for (i = 0; i < rtd->num_codecs; i++) {
859                 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
860                 struct snd_pcm_hw_params codec_params;
861
862                 /*
863                  * Skip CODECs which don't support the current stream type,
864                  * the idea being that if a CODEC is not used for the currently
865                  * set up transfer direction, it should not need to be
866                  * configured, especially since the configuration used might
867                  * not even be supported by that CODEC. There may be cases
868                  * however where a CODEC needs to be set up although it is
869                  * actually not being used for the transfer, e.g. if a
870                  * capture-only CODEC is acting as an LRCLK and/or BCLK master
871                  * for the DAI link including a playback-only CODEC.
872                  * If this becomes necessary, we will have to augment the
873                  * machine driver setup with information on how to act, so
874                  * we can do the right thing here.
875                  */
876                 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
877                         continue;
878
879                 /* copy params for each codec */
880                 codec_params = *params;
881
882                 /* fixup params based on TDM slot masks */
883                 if (codec_dai->tx_mask)
884                         soc_pcm_codec_params_fixup(&codec_params,
885                                                    codec_dai->tx_mask);
886                 if (codec_dai->rx_mask)
887                         soc_pcm_codec_params_fixup(&codec_params,
888                                                    codec_dai->rx_mask);
889
890                 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
891                 if(ret < 0)
892                         goto codec_err;
893
894                 codec_dai->rate = params_rate(&codec_params);
895                 codec_dai->channels = params_channels(&codec_params);
896                 codec_dai->sample_bits = snd_pcm_format_physical_width(
897                                                 params_format(&codec_params));
898         }
899
900         ret = soc_dai_hw_params(substream, params, cpu_dai);
901         if (ret < 0)
902                 goto interface_err;
903
904         if (platform->driver->ops && platform->driver->ops->hw_params) {
905                 ret = platform->driver->ops->hw_params(substream, params);
906                 if (ret < 0) {
907                         dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
908                                platform->component.name, ret);
909                         goto platform_err;
910                 }
911         }
912
913         /* store the parameters for each DAIs */
914         cpu_dai->rate = params_rate(params);
915         cpu_dai->channels = params_channels(params);
916         cpu_dai->sample_bits =
917                 snd_pcm_format_physical_width(params_format(params));
918
919 out:
920         mutex_unlock(&rtd->pcm_mutex);
921         return ret;
922
923 platform_err:
924         if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
925                 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
926
927 interface_err:
928         i = rtd->num_codecs;
929
930 codec_err:
931         while (--i >= 0) {
932                 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
933                 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
934                         codec_dai->driver->ops->hw_free(substream, codec_dai);
935                 codec_dai->rate = 0;
936         }
937
938         if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
939                 rtd->dai_link->ops->hw_free(substream);
940
941         mutex_unlock(&rtd->pcm_mutex);
942         return ret;
943 }
944
945 /*
946  * Frees resources allocated by hw_params, can be called multiple times
947  */
948 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
949 {
950         struct snd_soc_pcm_runtime *rtd = substream->private_data;
951         struct snd_soc_platform *platform = rtd->platform;
952         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
953         struct snd_soc_dai *codec_dai;
954         bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
955         int i;
956
957         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
958
959         /* clear the corresponding DAIs parameters when going to be inactive */
960         if (cpu_dai->active == 1) {
961                 cpu_dai->rate = 0;
962                 cpu_dai->channels = 0;
963                 cpu_dai->sample_bits = 0;
964         }
965
966         for (i = 0; i < rtd->num_codecs; i++) {
967                 codec_dai = rtd->codec_dais[i];
968                 if (codec_dai->active == 1) {
969                         codec_dai->rate = 0;
970                         codec_dai->channels = 0;
971                         codec_dai->sample_bits = 0;
972                 }
973         }
974
975         /* apply codec digital mute */
976         for (i = 0; i < rtd->num_codecs; i++) {
977                 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
978                     (!playback && rtd->codec_dais[i]->capture_active == 1))
979                         snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
980                                                  substream->stream);
981         }
982
983         /* free any machine hw params */
984         if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
985                 rtd->dai_link->ops->hw_free(substream);
986
987         /* free any DMA resources */
988         if (platform->driver->ops && platform->driver->ops->hw_free)
989                 platform->driver->ops->hw_free(substream);
990
991         /* now free hw params for the DAIs  */
992         for (i = 0; i < rtd->num_codecs; i++) {
993                 codec_dai = rtd->codec_dais[i];
994                 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
995                         codec_dai->driver->ops->hw_free(substream, codec_dai);
996         }
997
998         if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
999                 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1000
1001         mutex_unlock(&rtd->pcm_mutex);
1002         return 0;
1003 }
1004
1005 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1006 {
1007         struct snd_soc_pcm_runtime *rtd = substream->private_data;
1008         struct snd_soc_platform *platform = rtd->platform;
1009         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1010         struct snd_soc_dai *codec_dai;
1011         int i, ret;
1012
1013         for (i = 0; i < rtd->num_codecs; i++) {
1014                 codec_dai = rtd->codec_dais[i];
1015                 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
1016                         ret = codec_dai->driver->ops->trigger(substream,
1017                                                               cmd, codec_dai);
1018                         if (ret < 0)
1019                                 return ret;
1020                 }
1021         }
1022
1023         if (platform->driver->ops && platform->driver->ops->trigger) {
1024                 ret = platform->driver->ops->trigger(substream, cmd);
1025                 if (ret < 0)
1026                         return ret;
1027         }
1028
1029         if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
1030                 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1031                 if (ret < 0)
1032                         return ret;
1033         }
1034
1035         if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
1036                 ret = rtd->dai_link->ops->trigger(substream, cmd);
1037                 if (ret < 0)
1038                         return ret;
1039         }
1040
1041         return 0;
1042 }
1043
1044 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1045                                    int cmd)
1046 {
1047         struct snd_soc_pcm_runtime *rtd = substream->private_data;
1048         struct snd_soc_platform *platform = rtd->platform;
1049         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1050         struct snd_soc_dai *codec_dai;
1051         int i, ret;
1052
1053         for (i = 0; i < rtd->num_codecs; i++) {
1054                 codec_dai = rtd->codec_dais[i];
1055                 if (codec_dai->driver->ops &&
1056                     codec_dai->driver->ops->bespoke_trigger) {
1057                         ret = codec_dai->driver->ops->bespoke_trigger(substream,
1058                                                                 cmd, codec_dai);
1059                         if (ret < 0)
1060                                 return ret;
1061                 }
1062         }
1063
1064         if (platform->driver->bespoke_trigger) {
1065                 ret = platform->driver->bespoke_trigger(substream, cmd);
1066                 if (ret < 0)
1067                         return ret;
1068         }
1069
1070         if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
1071                 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1072                 if (ret < 0)
1073                         return ret;
1074         }
1075         return 0;
1076 }
1077 /*
1078  * soc level wrapper for pointer callback
1079  * If cpu_dai, codec_dai, platform driver has the delay callback, than
1080  * the runtime->delay will be updated accordingly.
1081  */
1082 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1083 {
1084         struct snd_soc_pcm_runtime *rtd = substream->private_data;
1085         struct snd_soc_platform *platform = rtd->platform;
1086         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1087         struct snd_soc_dai *codec_dai;
1088         struct snd_pcm_runtime *runtime = substream->runtime;
1089         snd_pcm_uframes_t offset = 0;
1090         snd_pcm_sframes_t delay = 0;
1091         snd_pcm_sframes_t codec_delay = 0;
1092         int i;
1093
1094         if (platform->driver->ops && platform->driver->ops->pointer)
1095                 offset = platform->driver->ops->pointer(substream);
1096
1097         if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
1098                 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1099
1100         for (i = 0; i < rtd->num_codecs; i++) {
1101                 codec_dai = rtd->codec_dais[i];
1102                 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
1103                         codec_delay = max(codec_delay,
1104                                         codec_dai->driver->ops->delay(substream,
1105                                                                     codec_dai));
1106         }
1107         delay += codec_delay;
1108
1109         /*
1110          * None of the existing platform drivers implement delay(), so
1111          * for now the codec_dai of first multicodec entry is used
1112          */
1113         if (platform->driver->delay)
1114                 delay += platform->driver->delay(substream, rtd->codec_dais[0]);
1115
1116         runtime->delay = delay;
1117
1118         return offset;
1119 }
1120
1121 /* connect a FE and BE */
1122 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1123                 struct snd_soc_pcm_runtime *be, int stream)
1124 {
1125         struct snd_soc_dpcm *dpcm;
1126
1127         /* only add new dpcms */
1128         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1129                 if (dpcm->be == be && dpcm->fe == fe)
1130                         return 0;
1131         }
1132
1133         dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1134         if (!dpcm)
1135                 return -ENOMEM;
1136
1137         dpcm->be = be;
1138         dpcm->fe = fe;
1139         be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1140         dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1141         list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1142         list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1143
1144         dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1145                         stream ? "capture" : "playback",  fe->dai_link->name,
1146                         stream ? "<-" : "->", be->dai_link->name);
1147
1148 #ifdef CONFIG_DEBUG_FS
1149         if (fe->debugfs_dpcm_root)
1150                 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1151                                 fe->debugfs_dpcm_root, &dpcm->state);
1152 #endif
1153         return 1;
1154 }
1155
1156 /* reparent a BE onto another FE */
1157 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1158                         struct snd_soc_pcm_runtime *be, int stream)
1159 {
1160         struct snd_soc_dpcm *dpcm;
1161         struct snd_pcm_substream *fe_substream, *be_substream;
1162
1163         /* reparent if BE is connected to other FEs */
1164         if (!be->dpcm[stream].users)
1165                 return;
1166
1167         be_substream = snd_soc_dpcm_get_substream(be, stream);
1168
1169         list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1170                 if (dpcm->fe == fe)
1171                         continue;
1172
1173                 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1174                         stream ? "capture" : "playback",
1175                         dpcm->fe->dai_link->name,
1176                         stream ? "<-" : "->", dpcm->be->dai_link->name);
1177
1178                 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1179                 be_substream->runtime = fe_substream->runtime;
1180                 break;
1181         }
1182 }
1183
1184 /* disconnect a BE and FE */
1185 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1186 {
1187         struct snd_soc_dpcm *dpcm, *d;
1188
1189         list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1190                 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1191                                 stream ? "capture" : "playback",
1192                                 dpcm->be->dai_link->name);
1193
1194                 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1195                         continue;
1196
1197                 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1198                         stream ? "capture" : "playback", fe->dai_link->name,
1199                         stream ? "<-" : "->", dpcm->be->dai_link->name);
1200
1201                 /* BEs still alive need new FE */
1202                 dpcm_be_reparent(fe, dpcm->be, stream);
1203
1204 #ifdef CONFIG_DEBUG_FS
1205                 debugfs_remove(dpcm->debugfs_state);
1206 #endif
1207                 list_del(&dpcm->list_be);
1208                 list_del(&dpcm->list_fe);
1209                 kfree(dpcm);
1210         }
1211 }
1212
1213 /* get BE for DAI widget and stream */
1214 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1215                 struct snd_soc_dapm_widget *widget, int stream)
1216 {
1217         struct snd_soc_pcm_runtime *be;
1218         int i, j;
1219
1220         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1221                 for (i = 0; i < card->num_links; i++) {
1222                         be = &card->rtd[i];
1223
1224                         if (!be->dai_link->no_pcm)
1225                                 continue;
1226
1227                         if (be->cpu_dai->playback_widget == widget)
1228                                 return be;
1229
1230                         for (j = 0; j < be->num_codecs; j++) {
1231                                 struct snd_soc_dai *dai = be->codec_dais[j];
1232                                 if (dai->playback_widget == widget)
1233                                         return be;
1234                         }
1235                 }
1236         } else {
1237
1238                 for (i = 0; i < card->num_links; i++) {
1239                         be = &card->rtd[i];
1240
1241                         if (!be->dai_link->no_pcm)
1242                                 continue;
1243
1244                         if (be->cpu_dai->capture_widget == widget)
1245                                 return be;
1246
1247                         for (j = 0; j < be->num_codecs; j++) {
1248                                 struct snd_soc_dai *dai = be->codec_dais[j];
1249                                 if (dai->capture_widget == widget)
1250                                         return be;
1251                         }
1252                 }
1253         }
1254
1255         dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1256                 stream ? "capture" : "playback", widget->name);
1257         return NULL;
1258 }
1259
1260 static inline struct snd_soc_dapm_widget *
1261         dai_get_widget(struct snd_soc_dai *dai, int stream)
1262 {
1263         if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1264                 return dai->playback_widget;
1265         else
1266                 return dai->capture_widget;
1267 }
1268
1269 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1270                 struct snd_soc_dapm_widget *widget)
1271 {
1272         int i;
1273
1274         for (i = 0; i < list->num_widgets; i++) {
1275                 if (widget == list->widgets[i])
1276                         return 1;
1277         }
1278
1279         return 0;
1280 }
1281
1282 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1283         int stream, struct snd_soc_dapm_widget_list **list)
1284 {
1285         struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1286         int paths;
1287
1288         /* get number of valid DAI paths and their widgets */
1289         paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list);
1290
1291         dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1292                         stream ? "capture" : "playback");
1293
1294         return paths;
1295 }
1296
1297 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1298         struct snd_soc_dapm_widget_list **list_)
1299 {
1300         struct snd_soc_dpcm *dpcm;
1301         struct snd_soc_dapm_widget_list *list = *list_;
1302         struct snd_soc_dapm_widget *widget;
1303         int prune = 0;
1304
1305         /* Destroy any old FE <--> BE connections */
1306         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1307                 unsigned int i;
1308
1309                 /* is there a valid CPU DAI widget for this BE */
1310                 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1311
1312                 /* prune the BE if it's no longer in our active list */
1313                 if (widget && widget_in_list(list, widget))
1314                         continue;
1315
1316                 /* is there a valid CODEC DAI widget for this BE */
1317                 for (i = 0; i < dpcm->be->num_codecs; i++) {
1318                         struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1319                         widget = dai_get_widget(dai, stream);
1320
1321                         /* prune the BE if it's no longer in our active list */
1322                         if (widget && widget_in_list(list, widget))
1323                                 continue;
1324                 }
1325
1326                 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1327                         stream ? "capture" : "playback",
1328                         dpcm->be->dai_link->name, fe->dai_link->name);
1329                 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1330                 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1331                 prune++;
1332         }
1333
1334         dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1335         return prune;
1336 }
1337
1338 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1339         struct snd_soc_dapm_widget_list **list_)
1340 {
1341         struct snd_soc_card *card = fe->card;
1342         struct snd_soc_dapm_widget_list *list = *list_;
1343         struct snd_soc_pcm_runtime *be;
1344         int i, new = 0, err;
1345
1346         /* Create any new FE <--> BE connections */
1347         for (i = 0; i < list->num_widgets; i++) {
1348
1349                 switch (list->widgets[i]->id) {
1350                 case snd_soc_dapm_dai_in:
1351                         if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1352                                 continue;
1353                         break;
1354                 case snd_soc_dapm_dai_out:
1355                         if (stream != SNDRV_PCM_STREAM_CAPTURE)
1356                                 continue;
1357                         break;
1358                 default:
1359                         continue;
1360                 }
1361
1362                 /* is there a valid BE rtd for this widget */
1363                 be = dpcm_get_be(card, list->widgets[i], stream);
1364                 if (!be) {
1365                         dev_err(fe->dev, "ASoC: no BE found for %s\n",
1366                                         list->widgets[i]->name);
1367                         continue;
1368                 }
1369
1370                 /* make sure BE is a real BE */
1371                 if (!be->dai_link->no_pcm)
1372                         continue;
1373
1374                 /* don't connect if FE is not running */
1375                 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1376                         continue;
1377
1378                 /* newly connected FE and BE */
1379                 err = dpcm_be_connect(fe, be, stream);
1380                 if (err < 0) {
1381                         dev_err(fe->dev, "ASoC: can't connect %s\n",
1382                                 list->widgets[i]->name);
1383                         break;
1384                 } else if (err == 0) /* already connected */
1385                         continue;
1386
1387                 /* new */
1388                 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1389                 new++;
1390         }
1391
1392         dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1393         return new;
1394 }
1395
1396 /*
1397  * Find the corresponding BE DAIs that source or sink audio to this
1398  * FE substream.
1399  */
1400 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1401         int stream, struct snd_soc_dapm_widget_list **list, int new)
1402 {
1403         if (new)
1404                 return dpcm_add_paths(fe, stream, list);
1405         else
1406                 return dpcm_prune_paths(fe, stream, list);
1407 }
1408
1409 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1410 {
1411         struct snd_soc_dpcm *dpcm;
1412
1413         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1414                 dpcm->be->dpcm[stream].runtime_update =
1415                                                 SND_SOC_DPCM_UPDATE_NO;
1416 }
1417
1418 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1419         int stream)
1420 {
1421         struct snd_soc_dpcm *dpcm;
1422
1423         /* disable any enabled and non active backends */
1424         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1425
1426                 struct snd_soc_pcm_runtime *be = dpcm->be;
1427                 struct snd_pcm_substream *be_substream =
1428                         snd_soc_dpcm_get_substream(be, stream);
1429
1430                 if (be->dpcm[stream].users == 0)
1431                         dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1432                                 stream ? "capture" : "playback",
1433                                 be->dpcm[stream].state);
1434
1435                 if (--be->dpcm[stream].users != 0)
1436                         continue;
1437
1438                 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1439                         continue;
1440
1441                 soc_pcm_close(be_substream);
1442                 be_substream->runtime = NULL;
1443                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1444         }
1445 }
1446
1447 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1448 {
1449         struct snd_soc_dpcm *dpcm;
1450         int err, count = 0;
1451
1452         /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1453         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1454
1455                 struct snd_soc_pcm_runtime *be = dpcm->be;
1456                 struct snd_pcm_substream *be_substream =
1457                         snd_soc_dpcm_get_substream(be, stream);
1458
1459                 if (!be_substream) {
1460                         dev_err(be->dev, "ASoC: no backend %s stream\n",
1461                                 stream ? "capture" : "playback");
1462                         continue;
1463                 }
1464
1465                 /* is this op for this BE ? */
1466                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1467                         continue;
1468
1469                 /* first time the dpcm is open ? */
1470                 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1471                         dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1472                                 stream ? "capture" : "playback",
1473                                 be->dpcm[stream].state);
1474
1475                 if (be->dpcm[stream].users++ != 0)
1476                         continue;
1477
1478                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1479                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1480                         continue;
1481
1482                 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1483                         stream ? "capture" : "playback", be->dai_link->name);
1484
1485                 be_substream->runtime = be->dpcm[stream].runtime;
1486                 err = soc_pcm_open(be_substream);
1487                 if (err < 0) {
1488                         dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1489                         be->dpcm[stream].users--;
1490                         if (be->dpcm[stream].users < 0)
1491                                 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1492                                         stream ? "capture" : "playback",
1493                                         be->dpcm[stream].state);
1494
1495                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1496                         goto unwind;
1497                 }
1498
1499                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1500                 count++;
1501         }
1502
1503         return count;
1504
1505 unwind:
1506         /* disable any enabled and non active backends */
1507         list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1508                 struct snd_soc_pcm_runtime *be = dpcm->be;
1509                 struct snd_pcm_substream *be_substream =
1510                         snd_soc_dpcm_get_substream(be, stream);
1511
1512                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1513                         continue;
1514
1515                 if (be->dpcm[stream].users == 0)
1516                         dev_err(be->dev, "ASoC: no users %s at close %d\n",
1517                                 stream ? "capture" : "playback",
1518                                 be->dpcm[stream].state);
1519
1520                 if (--be->dpcm[stream].users != 0)
1521                         continue;
1522
1523                 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1524                         continue;
1525
1526                 soc_pcm_close(be_substream);
1527                 be_substream->runtime = NULL;
1528                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1529         }
1530
1531         return err;
1532 }
1533
1534 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1535                                  struct snd_soc_pcm_stream *stream,
1536                                  u64 formats)
1537 {
1538         runtime->hw.rate_min = stream->rate_min;
1539         runtime->hw.rate_max = stream->rate_max;
1540         runtime->hw.channels_min = stream->channels_min;
1541         runtime->hw.channels_max = stream->channels_max;
1542         if (runtime->hw.formats)
1543                 runtime->hw.formats &= formats & stream->formats;
1544         else
1545                 runtime->hw.formats = formats & stream->formats;
1546         runtime->hw.rates = stream->rates;
1547 }
1548
1549 static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1550 {
1551         struct snd_soc_pcm_runtime *fe = substream->private_data;
1552         struct snd_soc_dpcm *dpcm;
1553         u64 formats = ULLONG_MAX;
1554         int stream = substream->stream;
1555
1556         if (!fe->dai_link->dpcm_merged_format)
1557                 return formats;
1558
1559         /*
1560          * It returns merged BE codec format
1561          * if FE want to use it (= dpcm_merged_format)
1562          */
1563
1564         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1565                 struct snd_soc_pcm_runtime *be = dpcm->be;
1566                 struct snd_soc_dai_driver *codec_dai_drv;
1567                 struct snd_soc_pcm_stream *codec_stream;
1568                 int i;
1569
1570                 for (i = 0; i < be->num_codecs; i++) {
1571                         codec_dai_drv = be->codec_dais[i]->driver;
1572                         if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1573                                 codec_stream = &codec_dai_drv->playback;
1574                         else
1575                                 codec_stream = &codec_dai_drv->capture;
1576
1577                         formats &= codec_stream->formats;
1578                 }
1579         }
1580
1581         return formats;
1582 }
1583
1584 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1585 {
1586         struct snd_pcm_runtime *runtime = substream->runtime;
1587         struct snd_soc_pcm_runtime *rtd = substream->private_data;
1588         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1589         struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1590         u64 format = dpcm_runtime_base_format(substream);
1591
1592         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1593                 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
1594         else
1595                 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
1596 }
1597
1598 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1599
1600 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1601  * for avoiding the race with trigger callback.
1602  * If the state is unset and a trigger is pending while the previous operation,
1603  * process the pending trigger action here.
1604  */
1605 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1606                                      int stream, enum snd_soc_dpcm_update state)
1607 {
1608         struct snd_pcm_substream *substream =
1609                 snd_soc_dpcm_get_substream(fe, stream);
1610
1611         snd_pcm_stream_lock_irq(substream);
1612         if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1613                 dpcm_fe_dai_do_trigger(substream,
1614                                        fe->dpcm[stream].trigger_pending - 1);
1615                 fe->dpcm[stream].trigger_pending = 0;
1616         }
1617         fe->dpcm[stream].runtime_update = state;
1618         snd_pcm_stream_unlock_irq(substream);
1619 }
1620
1621 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1622 {
1623         struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1624         struct snd_pcm_runtime *runtime = fe_substream->runtime;
1625         int stream = fe_substream->stream, ret = 0;
1626
1627         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1628
1629         ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1630         if (ret < 0) {
1631                 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1632                 goto be_err;
1633         }
1634
1635         dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1636
1637         /* start the DAI frontend */
1638         ret = soc_pcm_open(fe_substream);
1639         if (ret < 0) {
1640                 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1641                 goto unwind;
1642         }
1643
1644         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1645
1646         dpcm_set_fe_runtime(fe_substream);
1647         snd_pcm_limit_hw_rates(runtime);
1648
1649         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1650         return 0;
1651
1652 unwind:
1653         dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1654 be_err:
1655         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1656         return ret;
1657 }
1658
1659 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1660 {
1661         struct snd_soc_dpcm *dpcm;
1662
1663         /* only shutdown BEs that are either sinks or sources to this FE DAI */
1664         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1665
1666                 struct snd_soc_pcm_runtime *be = dpcm->be;
1667                 struct snd_pcm_substream *be_substream =
1668                         snd_soc_dpcm_get_substream(be, stream);
1669
1670                 /* is this op for this BE ? */
1671                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1672                         continue;
1673
1674                 if (be->dpcm[stream].users == 0)
1675                         dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1676                                 stream ? "capture" : "playback",
1677                                 be->dpcm[stream].state);
1678
1679                 if (--be->dpcm[stream].users != 0)
1680                         continue;
1681
1682                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1683                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1684                         continue;
1685
1686                 dev_dbg(be->dev, "ASoC: close BE %s\n",
1687                         dpcm->fe->dai_link->name);
1688
1689                 soc_pcm_close(be_substream);
1690                 be_substream->runtime = NULL;
1691
1692                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1693         }
1694         return 0;
1695 }
1696
1697 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1698 {
1699         struct snd_soc_pcm_runtime *fe = substream->private_data;
1700         int stream = substream->stream;
1701
1702         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1703
1704         /* shutdown the BEs */
1705         dpcm_be_dai_shutdown(fe, substream->stream);
1706
1707         dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1708
1709         /* now shutdown the frontend */
1710         soc_pcm_close(substream);
1711
1712         /* run the stream event for each BE */
1713         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1714
1715         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1716         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1717         return 0;
1718 }
1719
1720 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1721 {
1722         struct snd_soc_dpcm *dpcm;
1723
1724         /* only hw_params backends that are either sinks or sources
1725          * to this frontend DAI */
1726         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1727
1728                 struct snd_soc_pcm_runtime *be = dpcm->be;
1729                 struct snd_pcm_substream *be_substream =
1730                         snd_soc_dpcm_get_substream(be, stream);
1731
1732                 /* is this op for this BE ? */
1733                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1734                         continue;
1735
1736                 /* only free hw when no longer used - check all FEs */
1737                 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1738                                 continue;
1739
1740                 /* do not free hw if this BE is used by other FE */
1741                 if (be->dpcm[stream].users > 1)
1742                         continue;
1743
1744                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1745                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1746                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1747                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1748                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1749                         continue;
1750
1751                 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1752                         dpcm->fe->dai_link->name);
1753
1754                 soc_pcm_hw_free(be_substream);
1755
1756                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1757         }
1758
1759         return 0;
1760 }
1761
1762 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1763 {
1764         struct snd_soc_pcm_runtime *fe = substream->private_data;
1765         int err, stream = substream->stream;
1766
1767         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1768         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1769
1770         dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1771
1772         /* call hw_free on the frontend */
1773         err = soc_pcm_hw_free(substream);
1774         if (err < 0)
1775                 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1776                         fe->dai_link->name);
1777
1778         /* only hw_params backends that are either sinks or sources
1779          * to this frontend DAI */
1780         err = dpcm_be_dai_hw_free(fe, stream);
1781
1782         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1783         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1784
1785         mutex_unlock(&fe->card->mutex);
1786         return 0;
1787 }
1788
1789 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1790 {
1791         struct snd_soc_dpcm *dpcm;
1792         int ret;
1793
1794         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1795
1796                 struct snd_soc_pcm_runtime *be = dpcm->be;
1797                 struct snd_pcm_substream *be_substream =
1798                         snd_soc_dpcm_get_substream(be, stream);
1799
1800                 /* is this op for this BE ? */
1801                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1802                         continue;
1803
1804                 /* only allow hw_params() if no connected FEs are running */
1805                 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1806                         continue;
1807
1808                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1809                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1810                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1811                         continue;
1812
1813                 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1814                         dpcm->fe->dai_link->name);
1815
1816                 /* copy params for each dpcm */
1817                 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1818                                 sizeof(struct snd_pcm_hw_params));
1819
1820                 /* perform any hw_params fixups */
1821                 if (be->dai_link->be_hw_params_fixup) {
1822                         ret = be->dai_link->be_hw_params_fixup(be,
1823                                         &dpcm->hw_params);
1824                         if (ret < 0) {
1825                                 dev_err(be->dev,
1826                                         "ASoC: hw_params BE fixup failed %d\n",
1827                                         ret);
1828                                 goto unwind;
1829                         }
1830                 }
1831
1832                 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1833                 if (ret < 0) {
1834                         dev_err(dpcm->be->dev,
1835                                 "ASoC: hw_params BE failed %d\n", ret);
1836                         goto unwind;
1837                 }
1838
1839                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1840         }
1841         return 0;
1842
1843 unwind:
1844         /* disable any enabled and non active backends */
1845         list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1846                 struct snd_soc_pcm_runtime *be = dpcm->be;
1847                 struct snd_pcm_substream *be_substream =
1848                         snd_soc_dpcm_get_substream(be, stream);
1849
1850                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1851                         continue;
1852
1853                 /* only allow hw_free() if no connected FEs are running */
1854                 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1855                         continue;
1856
1857                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1858                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1859                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1860                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1861                         continue;
1862
1863                 soc_pcm_hw_free(be_substream);
1864         }
1865
1866         return ret;
1867 }
1868
1869 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1870                                  struct snd_pcm_hw_params *params)
1871 {
1872         struct snd_soc_pcm_runtime *fe = substream->private_data;
1873         int ret, stream = substream->stream;
1874
1875         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1876         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1877
1878         memcpy(&fe->dpcm[substream->stream].hw_params, params,
1879                         sizeof(struct snd_pcm_hw_params));
1880         ret = dpcm_be_dai_hw_params(fe, substream->stream);
1881         if (ret < 0) {
1882                 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
1883                 goto out;
1884         }
1885
1886         dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1887                         fe->dai_link->name, params_rate(params),
1888                         params_channels(params), params_format(params));
1889
1890         /* call hw_params on the frontend */
1891         ret = soc_pcm_hw_params(substream, params);
1892         if (ret < 0) {
1893                 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
1894                 dpcm_be_dai_hw_free(fe, stream);
1895          } else
1896                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1897
1898 out:
1899         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1900         mutex_unlock(&fe->card->mutex);
1901         return ret;
1902 }
1903
1904 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1905                 struct snd_pcm_substream *substream, int cmd)
1906 {
1907         int ret;
1908
1909         dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
1910                         dpcm->fe->dai_link->name, cmd);
1911
1912         ret = soc_pcm_trigger(substream, cmd);
1913         if (ret < 0)
1914                 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
1915
1916         return ret;
1917 }
1918
1919 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1920                                int cmd)
1921 {
1922         struct snd_soc_dpcm *dpcm;
1923         int ret = 0;
1924
1925         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1926
1927                 struct snd_soc_pcm_runtime *be = dpcm->be;
1928                 struct snd_pcm_substream *be_substream =
1929                         snd_soc_dpcm_get_substream(be, stream);
1930
1931                 /* is this op for this BE ? */
1932                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1933                         continue;
1934
1935                 switch (cmd) {
1936                 case SNDRV_PCM_TRIGGER_START:
1937                         if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1938                             (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1939                                 continue;
1940
1941                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1942                         if (ret)
1943                                 return ret;
1944
1945                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1946                         break;
1947                 case SNDRV_PCM_TRIGGER_RESUME:
1948                         if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1949                                 continue;
1950
1951                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1952                         if (ret)
1953                                 return ret;
1954
1955                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1956                         break;
1957                 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1958                         if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1959                                 continue;
1960
1961                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1962                         if (ret)
1963                                 return ret;
1964
1965                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1966                         break;
1967                 case SNDRV_PCM_TRIGGER_STOP:
1968                         if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1969                                 continue;
1970
1971                         if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1972                                 continue;
1973
1974                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1975                         if (ret)
1976                                 return ret;
1977
1978                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1979                         break;
1980                 case SNDRV_PCM_TRIGGER_SUSPEND:
1981                         if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1982                                 continue;
1983
1984                         if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1985                                 continue;
1986
1987                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1988                         if (ret)
1989                                 return ret;
1990
1991                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1992                         break;
1993                 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1994                         if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1995                                 continue;
1996
1997                         if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1998                                 continue;
1999
2000                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2001                         if (ret)
2002                                 return ret;
2003
2004                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2005                         break;
2006                 }
2007         }
2008
2009         return ret;
2010 }
2011 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2012
2013 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2014 {
2015         struct snd_soc_pcm_runtime *fe = substream->private_data;
2016         int stream = substream->stream, ret;
2017         enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2018
2019         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2020
2021         switch (trigger) {
2022         case SND_SOC_DPCM_TRIGGER_PRE:
2023                 /* call trigger on the frontend before the backend. */
2024
2025                 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2026                                 fe->dai_link->name, cmd);
2027
2028                 ret = soc_pcm_trigger(substream, cmd);
2029                 if (ret < 0) {
2030                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2031                         goto out;
2032                 }
2033
2034                 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2035                 break;
2036         case SND_SOC_DPCM_TRIGGER_POST:
2037                 /* call trigger on the frontend after the backend. */
2038
2039                 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2040                 if (ret < 0) {
2041                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2042                         goto out;
2043                 }
2044
2045                 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2046                                 fe->dai_link->name, cmd);
2047
2048                 ret = soc_pcm_trigger(substream, cmd);
2049                 break;
2050         case SND_SOC_DPCM_TRIGGER_BESPOKE:
2051                 /* bespoke trigger() - handles both FE and BEs */
2052
2053                 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2054                                 fe->dai_link->name, cmd);
2055
2056                 ret = soc_pcm_bespoke_trigger(substream, cmd);
2057                 if (ret < 0) {
2058                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2059                         goto out;
2060                 }
2061                 break;
2062         default:
2063                 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2064                                 fe->dai_link->name);
2065                 ret = -EINVAL;
2066                 goto out;
2067         }
2068
2069         switch (cmd) {
2070         case SNDRV_PCM_TRIGGER_START:
2071         case SNDRV_PCM_TRIGGER_RESUME:
2072         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2073                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2074                 break;
2075         case SNDRV_PCM_TRIGGER_STOP:
2076         case SNDRV_PCM_TRIGGER_SUSPEND:
2077         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2078                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2079                 break;
2080         }
2081
2082 out:
2083         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2084         return ret;
2085 }
2086
2087 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2088 {
2089         struct snd_soc_pcm_runtime *fe = substream->private_data;
2090         int stream = substream->stream;
2091
2092         /* if FE's runtime_update is already set, we're in race;
2093          * process this trigger later at exit
2094          */
2095         if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2096                 fe->dpcm[stream].trigger_pending = cmd + 1;
2097                 return 0; /* delayed, assuming it's successful */
2098         }
2099
2100         /* we're alone, let's trigger */
2101         return dpcm_fe_dai_do_trigger(substream, cmd);
2102 }
2103
2104 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2105 {
2106         struct snd_soc_dpcm *dpcm;
2107         int ret = 0;
2108
2109         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2110
2111                 struct snd_soc_pcm_runtime *be = dpcm->be;
2112                 struct snd_pcm_substream *be_substream =
2113                         snd_soc_dpcm_get_substream(be, stream);
2114
2115                 /* is this op for this BE ? */
2116                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2117                         continue;
2118
2119                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2120                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2121                         continue;
2122
2123                 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2124                         dpcm->fe->dai_link->name);
2125
2126                 ret = soc_pcm_prepare(be_substream);
2127                 if (ret < 0) {
2128                         dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2129                                 ret);
2130                         break;
2131                 }
2132
2133                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2134         }
2135         return ret;
2136 }
2137
2138 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2139 {
2140         struct snd_soc_pcm_runtime *fe = substream->private_data;
2141         int stream = substream->stream, ret = 0;
2142
2143         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2144
2145         dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2146
2147         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2148
2149         /* there is no point preparing this FE if there are no BEs */
2150         if (list_empty(&fe->dpcm[stream].be_clients)) {
2151                 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2152                                 fe->dai_link->name);
2153                 ret = -EINVAL;
2154                 goto out;
2155         }
2156
2157         ret = dpcm_be_dai_prepare(fe, substream->stream);
2158         if (ret < 0)
2159                 goto out;
2160
2161         /* call prepare on the frontend */
2162         ret = soc_pcm_prepare(substream);
2163         if (ret < 0) {
2164                 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2165                         fe->dai_link->name);
2166                 goto out;
2167         }
2168
2169         /* run the stream event for each BE */
2170         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2171         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2172
2173 out:
2174         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2175         mutex_unlock(&fe->card->mutex);
2176
2177         return ret;
2178 }
2179
2180 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2181                      unsigned int cmd, void *arg)
2182 {
2183         struct snd_soc_pcm_runtime *rtd = substream->private_data;
2184         struct snd_soc_platform *platform = rtd->platform;
2185
2186         if (platform->driver->ops && platform->driver->ops->ioctl)
2187                 return platform->driver->ops->ioctl(substream, cmd, arg);
2188         return snd_pcm_lib_ioctl(substream, cmd, arg);
2189 }
2190
2191 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2192 {
2193         struct snd_pcm_substream *substream =
2194                 snd_soc_dpcm_get_substream(fe, stream);
2195         enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2196         int err;
2197
2198         dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2199                         stream ? "capture" : "playback", fe->dai_link->name);
2200
2201         if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2202                 /* call bespoke trigger - FE takes care of all BE triggers */
2203                 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2204                                 fe->dai_link->name);
2205
2206                 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2207                 if (err < 0)
2208                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2209         } else {
2210                 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2211                         fe->dai_link->name);
2212
2213                 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2214                 if (err < 0)
2215                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2216         }
2217
2218         err = dpcm_be_dai_hw_free(fe, stream);
2219         if (err < 0)
2220                 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2221
2222         err = dpcm_be_dai_shutdown(fe, stream);
2223         if (err < 0)
2224                 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2225
2226         /* run the stream event for each BE */
2227         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2228
2229         return 0;
2230 }
2231
2232 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2233 {
2234         struct snd_pcm_substream *substream =
2235                 snd_soc_dpcm_get_substream(fe, stream);
2236         struct snd_soc_dpcm *dpcm;
2237         enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2238         int ret;
2239
2240         dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2241                         stream ? "capture" : "playback", fe->dai_link->name);
2242
2243         /* Only start the BE if the FE is ready */
2244         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2245                 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2246                 return -EINVAL;
2247
2248         /* startup must always be called for new BEs */
2249         ret = dpcm_be_dai_startup(fe, stream);
2250         if (ret < 0)
2251                 goto disconnect;
2252
2253         /* keep going if FE state is > open */
2254         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2255                 return 0;
2256
2257         ret = dpcm_be_dai_hw_params(fe, stream);
2258         if (ret < 0)
2259                 goto close;
2260
2261         /* keep going if FE state is > hw_params */
2262         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2263                 return 0;
2264
2265
2266         ret = dpcm_be_dai_prepare(fe, stream);
2267         if (ret < 0)
2268                 goto hw_free;
2269
2270         /* run the stream event for each BE */
2271         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2272
2273         /* keep going if FE state is > prepare */
2274         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2275                 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2276                 return 0;
2277
2278         if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2279                 /* call trigger on the frontend - FE takes care of all BE triggers */
2280                 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2281                                 fe->dai_link->name);
2282
2283                 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2284                 if (ret < 0) {
2285                         dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2286                         goto hw_free;
2287                 }
2288         } else {
2289                 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2290                         fe->dai_link->name);
2291
2292                 ret = dpcm_be_dai_trigger(fe, stream,
2293                                         SNDRV_PCM_TRIGGER_START);
2294                 if (ret < 0) {
2295                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2296                         goto hw_free;
2297                 }
2298         }
2299
2300         return 0;
2301
2302 hw_free:
2303         dpcm_be_dai_hw_free(fe, stream);
2304 close:
2305         dpcm_be_dai_shutdown(fe, stream);
2306 disconnect:
2307         /* disconnect any non started BEs */
2308         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2309                 struct snd_soc_pcm_runtime *be = dpcm->be;
2310                 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2311                                 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2312         }
2313
2314         return ret;
2315 }
2316
2317 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2318 {
2319         int ret;
2320
2321         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2322         ret = dpcm_run_update_startup(fe, stream);
2323         if (ret < 0)
2324                 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2325         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2326
2327         return ret;
2328 }
2329
2330 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2331 {
2332         int ret;
2333
2334         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2335         ret = dpcm_run_update_shutdown(fe, stream);
2336         if (ret < 0)
2337                 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2338         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2339
2340         return ret;
2341 }
2342
2343 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2344  * any DAI links.
2345  */
2346 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2347 {
2348         int i, old, new, paths;
2349
2350         mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2351         for (i = 0; i < card->num_rtd; i++) {
2352                 struct snd_soc_dapm_widget_list *list;
2353                 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2354
2355                 /* make sure link is FE */
2356                 if (!fe->dai_link->dynamic)
2357                         continue;
2358
2359                 /* only check active links */
2360                 if (!fe->cpu_dai->active)
2361                         continue;
2362
2363                 /* DAPM sync will call this to update DSP paths */
2364                 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
2365                         fe->dai_link->name);
2366
2367                 /* skip if FE doesn't have playback capability */
2368                 if (!fe->cpu_dai->driver->playback.channels_min
2369                     || !fe->codec_dai->driver->playback.channels_min)
2370                         goto capture;
2371
2372                 /* skip if FE isn't currently playing */
2373                 if (!fe->cpu_dai->playback_active
2374                     || !fe->codec_dai->playback_active)
2375                         goto capture;
2376
2377                 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2378                 if (paths < 0) {
2379                         dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2380                                         fe->dai_link->name,  "playback");
2381                         mutex_unlock(&card->mutex);
2382                         return paths;
2383                 }
2384
2385                 /* update any new playback paths */
2386                 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2387                 if (new) {
2388                         dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2389                         dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2390                         dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2391                 }
2392
2393                 /* update any old playback paths */
2394                 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2395                 if (old) {
2396                         dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2397                         dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2398                         dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2399                 }
2400
2401                 dpcm_path_put(&list);
2402 capture:
2403                 /* skip if FE doesn't have capture capability */
2404                 if (!fe->cpu_dai->driver->capture.channels_min
2405                     || !fe->codec_dai->driver->capture.channels_min)
2406                         continue;
2407
2408                 /* skip if FE isn't currently capturing */
2409                 if (!fe->cpu_dai->capture_active
2410                     || !fe->codec_dai->capture_active)
2411                         continue;
2412
2413                 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2414                 if (paths < 0) {
2415                         dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2416                                         fe->dai_link->name,  "capture");
2417                         mutex_unlock(&card->mutex);
2418                         return paths;
2419                 }
2420
2421                 /* update any new capture paths */
2422                 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2423                 if (new) {
2424                         dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2425                         dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2426                         dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2427                 }
2428
2429                 /* update any old capture paths */
2430                 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2431                 if (old) {
2432                         dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2433                         dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2434                         dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2435                 }
2436
2437                 dpcm_path_put(&list);
2438         }
2439
2440         mutex_unlock(&card->mutex);
2441         return 0;
2442 }
2443 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2444 {
2445         struct snd_soc_dpcm *dpcm;
2446         struct list_head *clients =
2447                 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2448
2449         list_for_each_entry(dpcm, clients, list_be) {
2450
2451                 struct snd_soc_pcm_runtime *be = dpcm->be;
2452                 int i;
2453
2454                 if (be->dai_link->ignore_suspend)
2455                         continue;
2456
2457                 for (i = 0; i < be->num_codecs; i++) {
2458                         struct snd_soc_dai *dai = be->codec_dais[i];
2459                         struct snd_soc_dai_driver *drv = dai->driver;
2460
2461                         dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2462                                          be->dai_link->name);
2463
2464                         if (drv->ops && drv->ops->digital_mute &&
2465                                                         dai->playback_active)
2466                                 drv->ops->digital_mute(dai, mute);
2467                 }
2468         }
2469
2470         return 0;
2471 }
2472
2473 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2474 {
2475         struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2476         struct snd_soc_dpcm *dpcm;
2477         struct snd_soc_dapm_widget_list *list;
2478         int ret;
2479         int stream = fe_substream->stream;
2480
2481         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2482         fe->dpcm[stream].runtime = fe_substream->runtime;
2483
2484         ret = dpcm_path_get(fe, stream, &list);
2485         if (ret < 0) {
2486                 mutex_unlock(&fe->card->mutex);
2487                 return ret;
2488         } else if (ret == 0) {
2489                 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2490                         fe->dai_link->name, stream ? "capture" : "playback");
2491         }
2492
2493         /* calculate valid and active FE <-> BE dpcms */
2494         dpcm_process_paths(fe, stream, &list, 1);
2495
2496         ret = dpcm_fe_dai_startup(fe_substream);
2497         if (ret < 0) {
2498                 /* clean up all links */
2499                 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2500                         dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2501
2502                 dpcm_be_disconnect(fe, stream);
2503                 fe->dpcm[stream].runtime = NULL;
2504         }
2505
2506         dpcm_clear_pending_state(fe, stream);
2507         dpcm_path_put(&list);
2508         mutex_unlock(&fe->card->mutex);
2509         return ret;
2510 }
2511
2512 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2513 {
2514         struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2515         struct snd_soc_dpcm *dpcm;
2516         int stream = fe_substream->stream, ret;
2517
2518         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2519         ret = dpcm_fe_dai_shutdown(fe_substream);
2520
2521         /* mark FE's links ready to prune */
2522         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2523                 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2524
2525         dpcm_be_disconnect(fe, stream);
2526
2527         fe->dpcm[stream].runtime = NULL;
2528         mutex_unlock(&fe->card->mutex);
2529         return ret;
2530 }
2531
2532 /* create a new pcm */
2533 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2534 {
2535         struct snd_soc_platform *platform = rtd->platform;
2536         struct snd_soc_dai *codec_dai;
2537         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2538         struct snd_pcm *pcm;
2539         char new_name[64];
2540         int ret = 0, playback = 0, capture = 0;
2541         int i;
2542
2543         if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2544                 playback = rtd->dai_link->dpcm_playback;
2545                 capture = rtd->dai_link->dpcm_capture;
2546         } else {
2547                 for (i = 0; i < rtd->num_codecs; i++) {
2548                         codec_dai = rtd->codec_dais[i];
2549                         if (codec_dai->driver->playback.channels_min)
2550                                 playback = 1;
2551                         if (codec_dai->driver->capture.channels_min)
2552                                 capture = 1;
2553                 }
2554
2555                 capture = capture && cpu_dai->driver->capture.channels_min;
2556                 playback = playback && cpu_dai->driver->playback.channels_min;
2557         }
2558
2559         if (rtd->dai_link->playback_only) {
2560                 playback = 1;
2561                 capture = 0;
2562         }
2563
2564         if (rtd->dai_link->capture_only) {
2565                 playback = 0;
2566                 capture = 1;
2567         }
2568
2569         /* create the PCM */
2570         if (rtd->dai_link->no_pcm) {
2571                 snprintf(new_name, sizeof(new_name), "(%s)",
2572                         rtd->dai_link->stream_name);
2573
2574                 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2575                                 playback, capture, &pcm);
2576         } else {
2577                 if (rtd->dai_link->dynamic)
2578                         snprintf(new_name, sizeof(new_name), "%s (*)",
2579                                 rtd->dai_link->stream_name);
2580                 else
2581                         snprintf(new_name, sizeof(new_name), "%s %s-%d",
2582                                 rtd->dai_link->stream_name,
2583                                 (rtd->num_codecs > 1) ?
2584                                 "multicodec" : rtd->codec_dai->name, num);
2585
2586                 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2587                         capture, &pcm);
2588         }
2589         if (ret < 0) {
2590                 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2591                         rtd->dai_link->name);
2592                 return ret;
2593         }
2594         dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2595
2596         /* DAPM dai link stream work */
2597         INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2598
2599         pcm->nonatomic = rtd->dai_link->nonatomic;
2600         rtd->pcm = pcm;
2601         pcm->private_data = rtd;
2602
2603         if (rtd->dai_link->no_pcm) {
2604                 if (playback)
2605                         pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2606                 if (capture)
2607                         pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2608                 goto out;
2609         }
2610
2611         /* ASoC PCM operations */
2612         if (rtd->dai_link->dynamic) {
2613                 rtd->ops.open           = dpcm_fe_dai_open;
2614                 rtd->ops.hw_params      = dpcm_fe_dai_hw_params;
2615                 rtd->ops.prepare        = dpcm_fe_dai_prepare;
2616                 rtd->ops.trigger        = dpcm_fe_dai_trigger;
2617                 rtd->ops.hw_free        = dpcm_fe_dai_hw_free;
2618                 rtd->ops.close          = dpcm_fe_dai_close;
2619                 rtd->ops.pointer        = soc_pcm_pointer;
2620                 rtd->ops.ioctl          = soc_pcm_ioctl;
2621         } else {
2622                 rtd->ops.open           = soc_pcm_open;
2623                 rtd->ops.hw_params      = soc_pcm_hw_params;
2624                 rtd->ops.prepare        = soc_pcm_prepare;
2625                 rtd->ops.trigger        = soc_pcm_trigger;
2626                 rtd->ops.hw_free        = soc_pcm_hw_free;
2627                 rtd->ops.close          = soc_pcm_close;
2628                 rtd->ops.pointer        = soc_pcm_pointer;
2629                 rtd->ops.ioctl          = soc_pcm_ioctl;
2630         }
2631
2632         if (platform->driver->ops) {
2633                 rtd->ops.ack            = platform->driver->ops->ack;
2634                 rtd->ops.copy           = platform->driver->ops->copy;
2635                 rtd->ops.silence        = platform->driver->ops->silence;
2636                 rtd->ops.page           = platform->driver->ops->page;
2637                 rtd->ops.mmap           = platform->driver->ops->mmap;
2638         }
2639
2640         if (playback)
2641                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2642
2643         if (capture)
2644                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2645
2646         if (platform->driver->pcm_new) {
2647                 ret = platform->driver->pcm_new(rtd);
2648                 if (ret < 0) {
2649                         dev_err(platform->dev,
2650                                 "ASoC: pcm constructor failed: %d\n",
2651                                 ret);
2652                         return ret;
2653                 }
2654         }
2655
2656         pcm->private_free = platform->driver->pcm_free;
2657 out:
2658         dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2659                  (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2660                  cpu_dai->name);
2661         return ret;
2662 }
2663
2664 /* is the current PCM operation for this FE ? */
2665 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2666 {
2667         if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2668                 return 1;
2669         return 0;
2670 }
2671 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2672
2673 /* is the current PCM operation for this BE ? */
2674 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2675                 struct snd_soc_pcm_runtime *be, int stream)
2676 {
2677         if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2678            ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2679                   be->dpcm[stream].runtime_update))
2680                 return 1;
2681         return 0;
2682 }
2683 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2684
2685 /* get the substream for this BE */
2686 struct snd_pcm_substream *
2687         snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2688 {
2689         return be->pcm->streams[stream].substream;
2690 }
2691 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2692
2693 /* get the BE runtime state */
2694 enum snd_soc_dpcm_state
2695         snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2696 {
2697         return be->dpcm[stream].state;
2698 }
2699 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2700
2701 /* set the BE runtime state */
2702 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2703                 int stream, enum snd_soc_dpcm_state state)
2704 {
2705         be->dpcm[stream].state = state;
2706 }
2707 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2708
2709 /*
2710  * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2711  * are not running, paused or suspended for the specified stream direction.
2712  */
2713 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2714                 struct snd_soc_pcm_runtime *be, int stream)
2715 {
2716         struct snd_soc_dpcm *dpcm;
2717         int state;
2718
2719         list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2720
2721                 if (dpcm->fe == fe)
2722                         continue;
2723
2724                 state = dpcm->fe->dpcm[stream].state;
2725                 if (state == SND_SOC_DPCM_STATE_START ||
2726                         state == SND_SOC_DPCM_STATE_PAUSED ||
2727                         state == SND_SOC_DPCM_STATE_SUSPEND)
2728                         return 0;
2729         }
2730
2731         /* it's safe to free/stop this BE DAI */
2732         return 1;
2733 }
2734 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2735
2736 /*
2737  * We can only change hw params a BE DAI if any of it's FE are not prepared,
2738  * running, paused or suspended for the specified stream direction.
2739  */
2740 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2741                 struct snd_soc_pcm_runtime *be, int stream)
2742 {
2743         struct snd_soc_dpcm *dpcm;
2744         int state;
2745
2746         list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2747
2748                 if (dpcm->fe == fe)
2749                         continue;
2750
2751                 state = dpcm->fe->dpcm[stream].state;
2752                 if (state == SND_SOC_DPCM_STATE_START ||
2753                         state == SND_SOC_DPCM_STATE_PAUSED ||
2754                         state == SND_SOC_DPCM_STATE_SUSPEND ||
2755                         state == SND_SOC_DPCM_STATE_PREPARE)
2756                         return 0;
2757         }
2758
2759         /* it's safe to change hw_params */
2760         return 1;
2761 }
2762 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2763
2764 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2765                 int cmd, struct snd_soc_platform *platform)
2766 {
2767         if (platform->driver->ops && platform->driver->ops->trigger)
2768                 return platform->driver->ops->trigger(substream, cmd);
2769         return 0;
2770 }
2771 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2772
2773 #ifdef CONFIG_DEBUG_FS
2774 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2775 {
2776         switch (state) {
2777         case SND_SOC_DPCM_STATE_NEW:
2778                 return "new";
2779         case SND_SOC_DPCM_STATE_OPEN:
2780                 return "open";
2781         case SND_SOC_DPCM_STATE_HW_PARAMS:
2782                 return "hw_params";
2783         case SND_SOC_DPCM_STATE_PREPARE:
2784                 return "prepare";
2785         case SND_SOC_DPCM_STATE_START:
2786                 return "start";
2787         case SND_SOC_DPCM_STATE_STOP:
2788                 return "stop";
2789         case SND_SOC_DPCM_STATE_SUSPEND:
2790                 return "suspend";
2791         case SND_SOC_DPCM_STATE_PAUSED:
2792                 return "paused";
2793         case SND_SOC_DPCM_STATE_HW_FREE:
2794                 return "hw_free";
2795         case SND_SOC_DPCM_STATE_CLOSE:
2796                 return "close";
2797         }
2798
2799         return "unknown";
2800 }
2801
2802 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2803                                 int stream, char *buf, size_t size)
2804 {
2805         struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2806         struct snd_soc_dpcm *dpcm;
2807         ssize_t offset = 0;
2808
2809         /* FE state */
2810         offset += snprintf(buf + offset, size - offset,
2811                         "[%s - %s]\n", fe->dai_link->name,
2812                         stream ? "Capture" : "Playback");
2813
2814         offset += snprintf(buf + offset, size - offset, "State: %s\n",
2815                         dpcm_state_string(fe->dpcm[stream].state));
2816
2817         if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2818             (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2819                 offset += snprintf(buf + offset, size - offset,
2820                                 "Hardware Params: "
2821                                 "Format = %s, Channels = %d, Rate = %d\n",
2822                                 snd_pcm_format_name(params_format(params)),
2823                                 params_channels(params),
2824                                 params_rate(params));
2825
2826         /* BEs state */
2827         offset += snprintf(buf + offset, size - offset, "Backends:\n");
2828
2829         if (list_empty(&fe->dpcm[stream].be_clients)) {
2830                 offset += snprintf(buf + offset, size - offset,
2831                                 " No active DSP links\n");
2832                 goto out;
2833         }
2834
2835         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2836                 struct snd_soc_pcm_runtime *be = dpcm->be;
2837                 params = &dpcm->hw_params;
2838
2839                 offset += snprintf(buf + offset, size - offset,
2840                                 "- %s\n", be->dai_link->name);
2841
2842                 offset += snprintf(buf + offset, size - offset,
2843                                 "   State: %s\n",
2844                                 dpcm_state_string(be->dpcm[stream].state));
2845
2846                 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2847                     (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2848                         offset += snprintf(buf + offset, size - offset,
2849                                 "   Hardware Params: "
2850                                 "Format = %s, Channels = %d, Rate = %d\n",
2851                                 snd_pcm_format_name(params_format(params)),
2852                                 params_channels(params),
2853                                 params_rate(params));
2854         }
2855
2856 out:
2857         return offset;
2858 }
2859
2860 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2861                                 size_t count, loff_t *ppos)
2862 {
2863         struct snd_soc_pcm_runtime *fe = file->private_data;
2864         ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2865         char *buf;
2866
2867         buf = kmalloc(out_count, GFP_KERNEL);
2868         if (!buf)
2869                 return -ENOMEM;
2870
2871         if (fe->cpu_dai->driver->playback.channels_min)
2872                 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2873                                         buf + offset, out_count - offset);
2874
2875         if (fe->cpu_dai->driver->capture.channels_min)
2876                 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2877                                         buf + offset, out_count - offset);
2878
2879         ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2880
2881         kfree(buf);
2882         return ret;
2883 }
2884
2885 static const struct file_operations dpcm_state_fops = {
2886         .open = simple_open,
2887         .read = dpcm_state_read_file,
2888         .llseek = default_llseek,
2889 };
2890
2891 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2892 {
2893         if (!rtd->dai_link)
2894                 return;
2895
2896         if (!rtd->card->debugfs_card_root)
2897                 return;
2898
2899         rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2900                         rtd->card->debugfs_card_root);
2901         if (!rtd->debugfs_dpcm_root) {
2902                 dev_dbg(rtd->dev,
2903                          "ASoC: Failed to create dpcm debugfs directory %s\n",
2904                          rtd->dai_link->name);
2905                 return;
2906         }
2907
2908         rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2909                                                 rtd->debugfs_dpcm_root,
2910                                                 rtd, &dpcm_state_fops);
2911 }
2912 #endif