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