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