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