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