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