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