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