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