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