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