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