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