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