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