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