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