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