GNU Linux-libre 4.19.211-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
1225         /* only add new dpcms */
1226         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1227                 if (dpcm->be == be && dpcm->fe == fe)
1228                         return 0;
1229         }
1230
1231         dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1232         if (!dpcm)
1233                 return -ENOMEM;
1234
1235         dpcm->be = be;
1236         dpcm->fe = fe;
1237         be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1238         dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1239         list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1240         list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1241
1242         dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1243                         stream ? "capture" : "playback",  fe->dai_link->name,
1244                         stream ? "<-" : "->", be->dai_link->name);
1245
1246 #ifdef CONFIG_DEBUG_FS
1247         if (fe->debugfs_dpcm_root)
1248                 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1249                                 fe->debugfs_dpcm_root, &dpcm->state);
1250 #endif
1251         return 1;
1252 }
1253
1254 /* reparent a BE onto another FE */
1255 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1256                         struct snd_soc_pcm_runtime *be, int stream)
1257 {
1258         struct snd_soc_dpcm *dpcm;
1259         struct snd_pcm_substream *fe_substream, *be_substream;
1260
1261         /* reparent if BE is connected to other FEs */
1262         if (!be->dpcm[stream].users)
1263                 return;
1264
1265         be_substream = snd_soc_dpcm_get_substream(be, stream);
1266
1267         list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1268                 if (dpcm->fe == fe)
1269                         continue;
1270
1271                 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1272                         stream ? "capture" : "playback",
1273                         dpcm->fe->dai_link->name,
1274                         stream ? "<-" : "->", dpcm->be->dai_link->name);
1275
1276                 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1277                 be_substream->runtime = fe_substream->runtime;
1278                 break;
1279         }
1280 }
1281
1282 /* disconnect a BE and FE */
1283 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1284 {
1285         struct snd_soc_dpcm *dpcm, *d;
1286
1287         list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1288                 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1289                                 stream ? "capture" : "playback",
1290                                 dpcm->be->dai_link->name);
1291
1292                 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1293                         continue;
1294
1295                 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1296                         stream ? "capture" : "playback", fe->dai_link->name,
1297                         stream ? "<-" : "->", dpcm->be->dai_link->name);
1298
1299                 /* BEs still alive need new FE */
1300                 dpcm_be_reparent(fe, dpcm->be, stream);
1301
1302 #ifdef CONFIG_DEBUG_FS
1303                 debugfs_remove(dpcm->debugfs_state);
1304 #endif
1305                 list_del(&dpcm->list_be);
1306                 list_del(&dpcm->list_fe);
1307                 kfree(dpcm);
1308         }
1309 }
1310
1311 /* get BE for DAI widget and stream */
1312 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1313                 struct snd_soc_dapm_widget *widget, int stream)
1314 {
1315         struct snd_soc_pcm_runtime *be;
1316         int i;
1317
1318         dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1319
1320         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1321                 list_for_each_entry(be, &card->rtd_list, list) {
1322
1323                         if (!be->dai_link->no_pcm)
1324                                 continue;
1325
1326                         dev_dbg(card->dev, "ASoC: try BE : %s\n",
1327                                 be->cpu_dai->playback_widget ?
1328                                 be->cpu_dai->playback_widget->name : "(not set)");
1329
1330                         if (be->cpu_dai->playback_widget == widget)
1331                                 return be;
1332
1333                         for (i = 0; i < be->num_codecs; i++) {
1334                                 struct snd_soc_dai *dai = be->codec_dais[i];
1335                                 if (dai->playback_widget == widget)
1336                                         return be;
1337                         }
1338                 }
1339         } else {
1340
1341                 list_for_each_entry(be, &card->rtd_list, list) {
1342
1343                         if (!be->dai_link->no_pcm)
1344                                 continue;
1345
1346                         dev_dbg(card->dev, "ASoC: try BE %s\n",
1347                                 be->cpu_dai->capture_widget ?
1348                                 be->cpu_dai->capture_widget->name : "(not set)");
1349
1350                         if (be->cpu_dai->capture_widget == widget)
1351                                 return be;
1352
1353                         for (i = 0; i < be->num_codecs; i++) {
1354                                 struct snd_soc_dai *dai = be->codec_dais[i];
1355                                 if (dai->capture_widget == widget)
1356                                         return be;
1357                         }
1358                 }
1359         }
1360
1361         /* dai link name and stream name set correctly ? */
1362         dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1363                 stream ? "capture" : "playback", widget->name);
1364         return NULL;
1365 }
1366
1367 static inline struct snd_soc_dapm_widget *
1368         dai_get_widget(struct snd_soc_dai *dai, int stream)
1369 {
1370         if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1371                 return dai->playback_widget;
1372         else
1373                 return dai->capture_widget;
1374 }
1375
1376 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1377                 struct snd_soc_dapm_widget *widget)
1378 {
1379         int i;
1380
1381         for (i = 0; i < list->num_widgets; i++) {
1382                 if (widget == list->widgets[i])
1383                         return 1;
1384         }
1385
1386         return 0;
1387 }
1388
1389 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1390                 enum snd_soc_dapm_direction dir)
1391 {
1392         struct snd_soc_card *card = widget->dapm->card;
1393         struct snd_soc_pcm_runtime *rtd;
1394         int i;
1395
1396         if (dir == SND_SOC_DAPM_DIR_OUT) {
1397                 list_for_each_entry(rtd, &card->rtd_list, list) {
1398                         if (!rtd->dai_link->no_pcm)
1399                                 continue;
1400
1401                         if (rtd->cpu_dai->playback_widget == widget)
1402                                 return true;
1403
1404                         for (i = 0; i < rtd->num_codecs; ++i) {
1405                                 struct snd_soc_dai *dai = rtd->codec_dais[i];
1406                                 if (dai->playback_widget == widget)
1407                                         return true;
1408                         }
1409                 }
1410         } else { /* SND_SOC_DAPM_DIR_IN */
1411                 list_for_each_entry(rtd, &card->rtd_list, list) {
1412                         if (!rtd->dai_link->no_pcm)
1413                                 continue;
1414
1415                         if (rtd->cpu_dai->capture_widget == widget)
1416                                 return true;
1417
1418                         for (i = 0; i < rtd->num_codecs; ++i) {
1419                                 struct snd_soc_dai *dai = rtd->codec_dais[i];
1420                                 if (dai->capture_widget == widget)
1421                                         return true;
1422                         }
1423                 }
1424         }
1425
1426         return false;
1427 }
1428
1429 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1430         int stream, struct snd_soc_dapm_widget_list **list)
1431 {
1432         struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1433         int paths;
1434
1435         /* get number of valid DAI paths and their widgets */
1436         paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1437                         dpcm_end_walk_at_be);
1438
1439         dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1440                         stream ? "capture" : "playback");
1441
1442         return paths;
1443 }
1444
1445 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1446         struct snd_soc_dapm_widget_list **list_)
1447 {
1448         struct snd_soc_dpcm *dpcm;
1449         struct snd_soc_dapm_widget_list *list = *list_;
1450         struct snd_soc_dapm_widget *widget;
1451         int prune = 0;
1452
1453         /* Destroy any old FE <--> BE connections */
1454         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1455                 unsigned int i;
1456
1457                 /* is there a valid CPU DAI widget for this BE */
1458                 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1459
1460                 /* prune the BE if it's no longer in our active list */
1461                 if (widget && widget_in_list(list, widget))
1462                         continue;
1463
1464                 /* is there a valid CODEC DAI widget for this BE */
1465                 for (i = 0; i < dpcm->be->num_codecs; i++) {
1466                         struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1467                         widget = dai_get_widget(dai, stream);
1468
1469                         /* prune the BE if it's no longer in our active list */
1470                         if (widget && widget_in_list(list, widget))
1471                                 continue;
1472                 }
1473
1474                 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1475                         stream ? "capture" : "playback",
1476                         dpcm->be->dai_link->name, fe->dai_link->name);
1477                 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1478                 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1479                 prune++;
1480         }
1481
1482         dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1483         return prune;
1484 }
1485
1486 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1487         struct snd_soc_dapm_widget_list **list_)
1488 {
1489         struct snd_soc_card *card = fe->card;
1490         struct snd_soc_dapm_widget_list *list = *list_;
1491         struct snd_soc_pcm_runtime *be;
1492         int i, new = 0, err;
1493
1494         /* Create any new FE <--> BE connections */
1495         for (i = 0; i < list->num_widgets; i++) {
1496
1497                 switch (list->widgets[i]->id) {
1498                 case snd_soc_dapm_dai_in:
1499                         if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1500                                 continue;
1501                         break;
1502                 case snd_soc_dapm_dai_out:
1503                         if (stream != SNDRV_PCM_STREAM_CAPTURE)
1504                                 continue;
1505                         break;
1506                 default:
1507                         continue;
1508                 }
1509
1510                 /* is there a valid BE rtd for this widget */
1511                 be = dpcm_get_be(card, list->widgets[i], stream);
1512                 if (!be) {
1513                         dev_err(fe->dev, "ASoC: no BE found for %s\n",
1514                                         list->widgets[i]->name);
1515                         continue;
1516                 }
1517
1518                 /* make sure BE is a real BE */
1519                 if (!be->dai_link->no_pcm)
1520                         continue;
1521
1522                 /* don't connect if FE is not running */
1523                 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1524                         continue;
1525
1526                 /* newly connected FE and BE */
1527                 err = dpcm_be_connect(fe, be, stream);
1528                 if (err < 0) {
1529                         dev_err(fe->dev, "ASoC: can't connect %s\n",
1530                                 list->widgets[i]->name);
1531                         break;
1532                 } else if (err == 0) /* already connected */
1533                         continue;
1534
1535                 /* new */
1536                 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1537                 new++;
1538         }
1539
1540         dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1541         return new;
1542 }
1543
1544 /*
1545  * Find the corresponding BE DAIs that source or sink audio to this
1546  * FE substream.
1547  */
1548 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1549         int stream, struct snd_soc_dapm_widget_list **list, int new)
1550 {
1551         if (new)
1552                 return dpcm_add_paths(fe, stream, list);
1553         else
1554                 return dpcm_prune_paths(fe, stream, list);
1555 }
1556
1557 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1558 {
1559         struct snd_soc_dpcm *dpcm;
1560
1561         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1562                 dpcm->be->dpcm[stream].runtime_update =
1563                                                 SND_SOC_DPCM_UPDATE_NO;
1564 }
1565
1566 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1567         int stream)
1568 {
1569         struct snd_soc_dpcm *dpcm;
1570
1571         /* disable any enabled and non active backends */
1572         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1573
1574                 struct snd_soc_pcm_runtime *be = dpcm->be;
1575                 struct snd_pcm_substream *be_substream =
1576                         snd_soc_dpcm_get_substream(be, stream);
1577
1578                 if (be->dpcm[stream].users == 0)
1579                         dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1580                                 stream ? "capture" : "playback",
1581                                 be->dpcm[stream].state);
1582
1583                 if (--be->dpcm[stream].users != 0)
1584                         continue;
1585
1586                 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1587                         continue;
1588
1589                 soc_pcm_close(be_substream);
1590                 be_substream->runtime = NULL;
1591                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1592         }
1593 }
1594
1595 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1596 {
1597         struct snd_soc_dpcm *dpcm;
1598         int err, count = 0;
1599
1600         /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1601         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1602
1603                 struct snd_soc_pcm_runtime *be = dpcm->be;
1604                 struct snd_pcm_substream *be_substream =
1605                         snd_soc_dpcm_get_substream(be, stream);
1606
1607                 if (!be_substream) {
1608                         dev_err(be->dev, "ASoC: no backend %s stream\n",
1609                                 stream ? "capture" : "playback");
1610                         continue;
1611                 }
1612
1613                 /* is this op for this BE ? */
1614                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1615                         continue;
1616
1617                 /* first time the dpcm is open ? */
1618                 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1619                         dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1620                                 stream ? "capture" : "playback",
1621                                 be->dpcm[stream].state);
1622
1623                 if (be->dpcm[stream].users++ != 0)
1624                         continue;
1625
1626                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1627                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1628                         continue;
1629
1630                 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1631                         stream ? "capture" : "playback", be->dai_link->name);
1632
1633                 be_substream->runtime = be->dpcm[stream].runtime;
1634                 err = soc_pcm_open(be_substream);
1635                 if (err < 0) {
1636                         dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1637                         be->dpcm[stream].users--;
1638                         if (be->dpcm[stream].users < 0)
1639                                 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1640                                         stream ? "capture" : "playback",
1641                                         be->dpcm[stream].state);
1642
1643                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1644                         goto unwind;
1645                 }
1646
1647                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1648                 count++;
1649         }
1650
1651         return count;
1652
1653 unwind:
1654         /* disable any enabled and non active backends */
1655         list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1656                 struct snd_soc_pcm_runtime *be = dpcm->be;
1657                 struct snd_pcm_substream *be_substream =
1658                         snd_soc_dpcm_get_substream(be, stream);
1659
1660                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1661                         continue;
1662
1663                 if (be->dpcm[stream].users == 0)
1664                         dev_err(be->dev, "ASoC: no users %s at close %d\n",
1665                                 stream ? "capture" : "playback",
1666                                 be->dpcm[stream].state);
1667
1668                 if (--be->dpcm[stream].users != 0)
1669                         continue;
1670
1671                 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1672                         continue;
1673
1674                 soc_pcm_close(be_substream);
1675                 be_substream->runtime = NULL;
1676                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1677         }
1678
1679         return err;
1680 }
1681
1682 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1683                                  struct snd_soc_pcm_stream *stream)
1684 {
1685         runtime->hw.rate_min = stream->rate_min;
1686         runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
1687         runtime->hw.channels_min = stream->channels_min;
1688         runtime->hw.channels_max = stream->channels_max;
1689         if (runtime->hw.formats)
1690                 runtime->hw.formats &= stream->formats;
1691         else
1692                 runtime->hw.formats = stream->formats;
1693         runtime->hw.rates = stream->rates;
1694 }
1695
1696 static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1697                                       u64 *formats)
1698 {
1699         struct snd_soc_pcm_runtime *fe = substream->private_data;
1700         struct snd_soc_dpcm *dpcm;
1701         int stream = substream->stream;
1702
1703         if (!fe->dai_link->dpcm_merged_format)
1704                 return;
1705
1706         /*
1707          * It returns merged BE codec format
1708          * if FE want to use it (= dpcm_merged_format)
1709          */
1710
1711         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1712                 struct snd_soc_pcm_runtime *be = dpcm->be;
1713                 struct snd_soc_dai_driver *codec_dai_drv;
1714                 struct snd_soc_pcm_stream *codec_stream;
1715                 int i;
1716
1717                 for (i = 0; i < be->num_codecs; i++) {
1718                         /*
1719                          * Skip CODECs which don't support the current stream
1720                          * type. See soc_pcm_init_runtime_hw() for more details
1721                          */
1722                         if (!snd_soc_dai_stream_valid(be->codec_dais[i],
1723                                                       stream))
1724                                 continue;
1725
1726                         codec_dai_drv = be->codec_dais[i]->driver;
1727                         if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1728                                 codec_stream = &codec_dai_drv->playback;
1729                         else
1730                                 codec_stream = &codec_dai_drv->capture;
1731
1732                         *formats &= codec_stream->formats;
1733                 }
1734         }
1735 }
1736
1737 static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1738                                     unsigned int *channels_min,
1739                                     unsigned int *channels_max)
1740 {
1741         struct snd_soc_pcm_runtime *fe = substream->private_data;
1742         struct snd_soc_dpcm *dpcm;
1743         int stream = substream->stream;
1744
1745         if (!fe->dai_link->dpcm_merged_chan)
1746                 return;
1747
1748         /*
1749          * It returns merged BE codec channel;
1750          * if FE want to use it (= dpcm_merged_chan)
1751          */
1752
1753         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1754                 struct snd_soc_pcm_runtime *be = dpcm->be;
1755                 struct snd_soc_dai_driver *cpu_dai_drv =  be->cpu_dai->driver;
1756                 struct snd_soc_dai_driver *codec_dai_drv;
1757                 struct snd_soc_pcm_stream *codec_stream;
1758                 struct snd_soc_pcm_stream *cpu_stream;
1759
1760                 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1761                         cpu_stream = &cpu_dai_drv->playback;
1762                 else
1763                         cpu_stream = &cpu_dai_drv->capture;
1764
1765                 *channels_min = max(*channels_min, cpu_stream->channels_min);
1766                 *channels_max = min(*channels_max, cpu_stream->channels_max);
1767
1768                 /*
1769                  * chan min/max cannot be enforced if there are multiple CODEC
1770                  * DAIs connected to a single CPU DAI, use CPU DAI's directly
1771                  */
1772                 if (be->num_codecs == 1) {
1773                         codec_dai_drv = be->codec_dais[0]->driver;
1774
1775                         if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1776                                 codec_stream = &codec_dai_drv->playback;
1777                         else
1778                                 codec_stream = &codec_dai_drv->capture;
1779
1780                         *channels_min = max(*channels_min,
1781                                             codec_stream->channels_min);
1782                         *channels_max = min(*channels_max,
1783                                             codec_stream->channels_max);
1784                 }
1785         }
1786 }
1787
1788 static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1789                                     unsigned int *rates,
1790                                     unsigned int *rate_min,
1791                                     unsigned int *rate_max)
1792 {
1793         struct snd_soc_pcm_runtime *fe = substream->private_data;
1794         struct snd_soc_dpcm *dpcm;
1795         int stream = substream->stream;
1796
1797         if (!fe->dai_link->dpcm_merged_rate)
1798                 return;
1799
1800         /*
1801          * It returns merged BE codec channel;
1802          * if FE want to use it (= dpcm_merged_chan)
1803          */
1804
1805         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1806                 struct snd_soc_pcm_runtime *be = dpcm->be;
1807                 struct snd_soc_dai_driver *cpu_dai_drv =  be->cpu_dai->driver;
1808                 struct snd_soc_dai_driver *codec_dai_drv;
1809                 struct snd_soc_pcm_stream *codec_stream;
1810                 struct snd_soc_pcm_stream *cpu_stream;
1811                 int i;
1812
1813                 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1814                         cpu_stream = &cpu_dai_drv->playback;
1815                 else
1816                         cpu_stream = &cpu_dai_drv->capture;
1817
1818                 *rate_min = max(*rate_min, cpu_stream->rate_min);
1819                 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1820                 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1821
1822                 for (i = 0; i < be->num_codecs; i++) {
1823                         /*
1824                          * Skip CODECs which don't support the current stream
1825                          * type. See soc_pcm_init_runtime_hw() for more details
1826                          */
1827                         if (!snd_soc_dai_stream_valid(be->codec_dais[i],
1828                                                       stream))
1829                                 continue;
1830
1831                         codec_dai_drv = be->codec_dais[i]->driver;
1832                         if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1833                                 codec_stream = &codec_dai_drv->playback;
1834                         else
1835                                 codec_stream = &codec_dai_drv->capture;
1836
1837                         *rate_min = max(*rate_min, codec_stream->rate_min);
1838                         *rate_max = min_not_zero(*rate_max,
1839                                                  codec_stream->rate_max);
1840                         *rates = snd_pcm_rate_mask_intersect(*rates,
1841                                                 codec_stream->rates);
1842                 }
1843         }
1844 }
1845
1846 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1847 {
1848         struct snd_pcm_runtime *runtime = substream->runtime;
1849         struct snd_soc_pcm_runtime *rtd = substream->private_data;
1850         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1851         struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1852
1853         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1854                 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1855         else
1856                 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1857
1858         dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1859         dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1860                                 &runtime->hw.channels_max);
1861         dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1862                                 &runtime->hw.rate_min, &runtime->hw.rate_max);
1863 }
1864
1865 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1866
1867 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1868  * for avoiding the race with trigger callback.
1869  * If the state is unset and a trigger is pending while the previous operation,
1870  * process the pending trigger action here.
1871  */
1872 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1873                                      int stream, enum snd_soc_dpcm_update state)
1874 {
1875         struct snd_pcm_substream *substream =
1876                 snd_soc_dpcm_get_substream(fe, stream);
1877
1878         snd_pcm_stream_lock_irq(substream);
1879         if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1880                 dpcm_fe_dai_do_trigger(substream,
1881                                        fe->dpcm[stream].trigger_pending - 1);
1882                 fe->dpcm[stream].trigger_pending = 0;
1883         }
1884         fe->dpcm[stream].runtime_update = state;
1885         snd_pcm_stream_unlock_irq(substream);
1886 }
1887
1888 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1889                                int stream)
1890 {
1891         struct snd_soc_dpcm *dpcm;
1892         struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1893         struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1894         int err;
1895
1896         /* apply symmetry for FE */
1897         if (soc_pcm_has_symmetry(fe_substream))
1898                 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1899
1900         /* Symmetry only applies if we've got an active stream. */
1901         if (fe_cpu_dai->active) {
1902                 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1903                 if (err < 0)
1904                         return err;
1905         }
1906
1907         /* apply symmetry for BE */
1908         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1909                 struct snd_soc_pcm_runtime *be = dpcm->be;
1910                 struct snd_pcm_substream *be_substream =
1911                         snd_soc_dpcm_get_substream(be, stream);
1912                 struct snd_soc_pcm_runtime *rtd = be_substream->private_data;
1913                 int i;
1914
1915                 if (rtd->dai_link->be_hw_params_fixup)
1916                         continue;
1917
1918                 if (soc_pcm_has_symmetry(be_substream))
1919                         be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1920
1921                 /* Symmetry only applies if we've got an active stream. */
1922                 if (rtd->cpu_dai->active) {
1923                         err = soc_pcm_apply_symmetry(fe_substream,
1924                                                      rtd->cpu_dai);
1925                         if (err < 0)
1926                                 return err;
1927                 }
1928
1929                 for (i = 0; i < rtd->num_codecs; i++) {
1930                         if (rtd->codec_dais[i]->active) {
1931                                 err = soc_pcm_apply_symmetry(fe_substream,
1932                                                              rtd->codec_dais[i]);
1933                                 if (err < 0)
1934                                         return err;
1935                         }
1936                 }
1937         }
1938
1939         return 0;
1940 }
1941
1942 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1943 {
1944         struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1945         struct snd_pcm_runtime *runtime = fe_substream->runtime;
1946         int stream = fe_substream->stream, ret = 0;
1947
1948         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1949
1950         ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1951         if (ret < 0) {
1952                 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1953                 goto be_err;
1954         }
1955
1956         dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1957
1958         /* start the DAI frontend */
1959         ret = soc_pcm_open(fe_substream);
1960         if (ret < 0) {
1961                 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1962                 goto unwind;
1963         }
1964
1965         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1966
1967         dpcm_set_fe_runtime(fe_substream);
1968         snd_pcm_limit_hw_rates(runtime);
1969
1970         ret = dpcm_apply_symmetry(fe_substream, stream);
1971         if (ret < 0) {
1972                 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1973                         ret);
1974                 goto unwind;
1975         }
1976
1977         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1978         return 0;
1979
1980 unwind:
1981         dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1982 be_err:
1983         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1984         return ret;
1985 }
1986
1987 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1988 {
1989         struct snd_soc_dpcm *dpcm;
1990
1991         /* only shutdown BEs that are either sinks or sources to this FE DAI */
1992         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1993
1994                 struct snd_soc_pcm_runtime *be = dpcm->be;
1995                 struct snd_pcm_substream *be_substream =
1996                         snd_soc_dpcm_get_substream(be, stream);
1997
1998                 /* is this op for this BE ? */
1999                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2000                         continue;
2001
2002                 if (be->dpcm[stream].users == 0)
2003                         dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
2004                                 stream ? "capture" : "playback",
2005                                 be->dpcm[stream].state);
2006
2007                 if (--be->dpcm[stream].users != 0)
2008                         continue;
2009
2010                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2011                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
2012                         soc_pcm_hw_free(be_substream);
2013                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2014                 }
2015
2016                 dev_dbg(be->dev, "ASoC: close BE %s\n",
2017                         be->dai_link->name);
2018
2019                 soc_pcm_close(be_substream);
2020                 be_substream->runtime = NULL;
2021
2022                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2023         }
2024         return 0;
2025 }
2026
2027 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
2028 {
2029         struct snd_soc_pcm_runtime *fe = substream->private_data;
2030         int stream = substream->stream;
2031
2032         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2033
2034         /* shutdown the BEs */
2035         dpcm_be_dai_shutdown(fe, substream->stream);
2036
2037         dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
2038
2039         /* now shutdown the frontend */
2040         soc_pcm_close(substream);
2041
2042         /* run the stream event for each BE */
2043         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
2044
2045         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2046         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2047         return 0;
2048 }
2049
2050 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
2051 {
2052         struct snd_soc_dpcm *dpcm;
2053
2054         /* only hw_params backends that are either sinks or sources
2055          * to this frontend DAI */
2056         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2057
2058                 struct snd_soc_pcm_runtime *be = dpcm->be;
2059                 struct snd_pcm_substream *be_substream =
2060                         snd_soc_dpcm_get_substream(be, stream);
2061
2062                 /* is this op for this BE ? */
2063                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2064                         continue;
2065
2066                 /* only free hw when no longer used - check all FEs */
2067                 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2068                                 continue;
2069
2070                 /* do not free hw if this BE is used by other FE */
2071                 if (be->dpcm[stream].users > 1)
2072                         continue;
2073
2074                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2075                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2076                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2077                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
2078                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2079                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2080                         continue;
2081
2082                 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
2083                         be->dai_link->name);
2084
2085                 soc_pcm_hw_free(be_substream);
2086
2087                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2088         }
2089
2090         return 0;
2091 }
2092
2093 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
2094 {
2095         struct snd_soc_pcm_runtime *fe = substream->private_data;
2096         int err, stream = substream->stream;
2097
2098         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2099         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2100
2101         dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
2102
2103         /* call hw_free on the frontend */
2104         err = soc_pcm_hw_free(substream);
2105         if (err < 0)
2106                 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
2107                         fe->dai_link->name);
2108
2109         /* only hw_params backends that are either sinks or sources
2110          * to this frontend DAI */
2111         err = dpcm_be_dai_hw_free(fe, stream);
2112
2113         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2114         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2115
2116         mutex_unlock(&fe->card->mutex);
2117         return 0;
2118 }
2119
2120 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
2121 {
2122         struct snd_soc_dpcm *dpcm;
2123         int ret;
2124
2125         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2126
2127                 struct snd_soc_pcm_runtime *be = dpcm->be;
2128                 struct snd_pcm_substream *be_substream =
2129                         snd_soc_dpcm_get_substream(be, stream);
2130
2131                 /* is this op for this BE ? */
2132                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2133                         continue;
2134
2135                 /* copy params for each dpcm */
2136                 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2137                                 sizeof(struct snd_pcm_hw_params));
2138
2139                 /* perform any hw_params fixups */
2140                 if (be->dai_link->be_hw_params_fixup) {
2141                         ret = be->dai_link->be_hw_params_fixup(be,
2142                                         &dpcm->hw_params);
2143                         if (ret < 0) {
2144                                 dev_err(be->dev,
2145                                         "ASoC: hw_params BE fixup failed %d\n",
2146                                         ret);
2147                                 goto unwind;
2148                         }
2149                 }
2150
2151                 /* only allow hw_params() if no connected FEs are running */
2152                 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2153                         continue;
2154
2155                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2156                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2157                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2158                         continue;
2159
2160                 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2161                         be->dai_link->name);
2162
2163                 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2164                 if (ret < 0) {
2165                         dev_err(dpcm->be->dev,
2166                                 "ASoC: hw_params BE failed %d\n", ret);
2167                         goto unwind;
2168                 }
2169
2170                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2171         }
2172         return 0;
2173
2174 unwind:
2175         /* disable any enabled and non active backends */
2176         list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2177                 struct snd_soc_pcm_runtime *be = dpcm->be;
2178                 struct snd_pcm_substream *be_substream =
2179                         snd_soc_dpcm_get_substream(be, stream);
2180
2181                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2182                         continue;
2183
2184                 /* only allow hw_free() if no connected FEs are running */
2185                 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2186                         continue;
2187
2188                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2189                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2190                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2191                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2192                         continue;
2193
2194                 soc_pcm_hw_free(be_substream);
2195         }
2196
2197         return ret;
2198 }
2199
2200 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2201                                  struct snd_pcm_hw_params *params)
2202 {
2203         struct snd_soc_pcm_runtime *fe = substream->private_data;
2204         int ret, stream = substream->stream;
2205
2206         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2207         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2208
2209         memcpy(&fe->dpcm[substream->stream].hw_params, params,
2210                         sizeof(struct snd_pcm_hw_params));
2211         ret = dpcm_be_dai_hw_params(fe, substream->stream);
2212         if (ret < 0) {
2213                 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2214                 goto out;
2215         }
2216
2217         dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2218                         fe->dai_link->name, params_rate(params),
2219                         params_channels(params), params_format(params));
2220
2221         /* call hw_params on the frontend */
2222         ret = soc_pcm_hw_params(substream, params);
2223         if (ret < 0) {
2224                 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2225                 dpcm_be_dai_hw_free(fe, stream);
2226          } else
2227                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2228
2229 out:
2230         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2231         mutex_unlock(&fe->card->mutex);
2232         return ret;
2233 }
2234
2235 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2236                 struct snd_pcm_substream *substream, int cmd)
2237 {
2238         int ret;
2239
2240         dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2241                         dpcm->be->dai_link->name, cmd);
2242
2243         ret = soc_pcm_trigger(substream, cmd);
2244         if (ret < 0)
2245                 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2246
2247         return ret;
2248 }
2249
2250 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2251                                int cmd)
2252 {
2253         struct snd_soc_dpcm *dpcm;
2254         int ret = 0;
2255
2256         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2257
2258                 struct snd_soc_pcm_runtime *be = dpcm->be;
2259                 struct snd_pcm_substream *be_substream =
2260                         snd_soc_dpcm_get_substream(be, stream);
2261
2262                 /* is this op for this BE ? */
2263                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2264                         continue;
2265
2266                 switch (cmd) {
2267                 case SNDRV_PCM_TRIGGER_START:
2268                         if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2269                             (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2270                             (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2271                                 continue;
2272
2273                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2274                         if (ret)
2275                                 return ret;
2276
2277                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2278                         break;
2279                 case SNDRV_PCM_TRIGGER_RESUME:
2280                         if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2281                                 continue;
2282
2283                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2284                         if (ret)
2285                                 return ret;
2286
2287                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2288                         break;
2289                 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2290                         if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2291                                 continue;
2292
2293                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2294                         if (ret)
2295                                 return ret;
2296
2297                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2298                         break;
2299                 case SNDRV_PCM_TRIGGER_STOP:
2300                         if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2301                             (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2302                                 continue;
2303
2304                         if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2305                                 continue;
2306
2307                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2308                         if (ret)
2309                                 return ret;
2310
2311                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2312                         break;
2313                 case SNDRV_PCM_TRIGGER_SUSPEND:
2314                         if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2315                                 continue;
2316
2317                         if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2318                                 continue;
2319
2320                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2321                         if (ret)
2322                                 return ret;
2323
2324                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2325                         break;
2326                 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2327                         if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2328                                 continue;
2329
2330                         if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2331                                 continue;
2332
2333                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2334                         if (ret)
2335                                 return ret;
2336
2337                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2338                         break;
2339                 }
2340         }
2341
2342         return ret;
2343 }
2344 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2345
2346 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2347                                   int cmd, bool fe_first)
2348 {
2349         struct snd_soc_pcm_runtime *fe = substream->private_data;
2350         int ret;
2351
2352         /* call trigger on the frontend before the backend. */
2353         if (fe_first) {
2354                 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2355                         fe->dai_link->name, cmd);
2356
2357                 ret = soc_pcm_trigger(substream, cmd);
2358                 if (ret < 0)
2359                         return ret;
2360
2361                 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2362                 return ret;
2363         }
2364
2365         /* call trigger on the frontend after the backend. */
2366         ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2367         if (ret < 0)
2368                 return ret;
2369
2370         dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2371                 fe->dai_link->name, cmd);
2372
2373         ret = soc_pcm_trigger(substream, cmd);
2374
2375         return ret;
2376 }
2377
2378 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2379 {
2380         struct snd_soc_pcm_runtime *fe = substream->private_data;
2381         int stream = substream->stream;
2382         int ret = 0;
2383         enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2384
2385         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2386
2387         switch (trigger) {
2388         case SND_SOC_DPCM_TRIGGER_PRE:
2389                 switch (cmd) {
2390                 case SNDRV_PCM_TRIGGER_START:
2391                 case SNDRV_PCM_TRIGGER_RESUME:
2392                 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2393                 case SNDRV_PCM_TRIGGER_DRAIN:
2394                         ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2395                         break;
2396                 case SNDRV_PCM_TRIGGER_STOP:
2397                 case SNDRV_PCM_TRIGGER_SUSPEND:
2398                 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2399                         ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2400                         break;
2401                 default:
2402                         ret = -EINVAL;
2403                         break;
2404                 }
2405                 break;
2406         case SND_SOC_DPCM_TRIGGER_POST:
2407                 switch (cmd) {
2408                 case SNDRV_PCM_TRIGGER_START:
2409                 case SNDRV_PCM_TRIGGER_RESUME:
2410                 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2411                 case SNDRV_PCM_TRIGGER_DRAIN:
2412                         ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2413                         break;
2414                 case SNDRV_PCM_TRIGGER_STOP:
2415                 case SNDRV_PCM_TRIGGER_SUSPEND:
2416                 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2417                         ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2418                         break;
2419                 default:
2420                         ret = -EINVAL;
2421                         break;
2422                 }
2423                 break;
2424         case SND_SOC_DPCM_TRIGGER_BESPOKE:
2425                 /* bespoke trigger() - handles both FE and BEs */
2426
2427                 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2428                                 fe->dai_link->name, cmd);
2429
2430                 ret = soc_pcm_bespoke_trigger(substream, cmd);
2431                 break;
2432         default:
2433                 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2434                                 fe->dai_link->name);
2435                 ret = -EINVAL;
2436                 goto out;
2437         }
2438
2439         if (ret < 0) {
2440                 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2441                         cmd, ret);
2442                 goto out;
2443         }
2444
2445         switch (cmd) {
2446         case SNDRV_PCM_TRIGGER_START:
2447         case SNDRV_PCM_TRIGGER_RESUME:
2448         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2449                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2450                 break;
2451         case SNDRV_PCM_TRIGGER_STOP:
2452         case SNDRV_PCM_TRIGGER_SUSPEND:
2453                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2454                 break;
2455         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2456                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2457                 break;
2458         }
2459
2460 out:
2461         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2462         return ret;
2463 }
2464
2465 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2466 {
2467         struct snd_soc_pcm_runtime *fe = substream->private_data;
2468         int stream = substream->stream;
2469
2470         /* if FE's runtime_update is already set, we're in race;
2471          * process this trigger later at exit
2472          */
2473         if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2474                 fe->dpcm[stream].trigger_pending = cmd + 1;
2475                 return 0; /* delayed, assuming it's successful */
2476         }
2477
2478         /* we're alone, let's trigger */
2479         return dpcm_fe_dai_do_trigger(substream, cmd);
2480 }
2481
2482 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2483 {
2484         struct snd_soc_dpcm *dpcm;
2485         int ret = 0;
2486
2487         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2488
2489                 struct snd_soc_pcm_runtime *be = dpcm->be;
2490                 struct snd_pcm_substream *be_substream =
2491                         snd_soc_dpcm_get_substream(be, stream);
2492
2493                 /* is this op for this BE ? */
2494                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2495                         continue;
2496
2497                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2498                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2499                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2500                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2501                         continue;
2502
2503                 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2504                         be->dai_link->name);
2505
2506                 ret = soc_pcm_prepare(be_substream);
2507                 if (ret < 0) {
2508                         dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2509                                 ret);
2510                         break;
2511                 }
2512
2513                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2514         }
2515         return ret;
2516 }
2517
2518 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2519 {
2520         struct snd_soc_pcm_runtime *fe = substream->private_data;
2521         int stream = substream->stream, ret = 0;
2522
2523         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2524
2525         dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2526
2527         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2528
2529         /* there is no point preparing this FE if there are no BEs */
2530         if (list_empty(&fe->dpcm[stream].be_clients)) {
2531                 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2532                                 fe->dai_link->name);
2533                 ret = -EINVAL;
2534                 goto out;
2535         }
2536
2537         ret = dpcm_be_dai_prepare(fe, substream->stream);
2538         if (ret < 0)
2539                 goto out;
2540
2541         /* call prepare on the frontend */
2542         ret = soc_pcm_prepare(substream);
2543         if (ret < 0) {
2544                 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2545                         fe->dai_link->name);
2546                 goto out;
2547         }
2548
2549         /* run the stream event for each BE */
2550         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2551         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2552
2553 out:
2554         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2555         mutex_unlock(&fe->card->mutex);
2556
2557         return ret;
2558 }
2559
2560 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2561                      unsigned int cmd, void *arg)
2562 {
2563         struct snd_soc_pcm_runtime *rtd = substream->private_data;
2564         struct snd_soc_component *component;
2565         struct snd_soc_rtdcom_list *rtdcom;
2566
2567         for_each_rtdcom(rtd, rtdcom) {
2568                 component = rtdcom->component;
2569
2570                 if (!component->driver->ops ||
2571                     !component->driver->ops->ioctl)
2572                         continue;
2573
2574                 /* FIXME: use 1st ioctl */
2575                 return component->driver->ops->ioctl(substream, cmd, arg);
2576         }
2577
2578         return snd_pcm_lib_ioctl(substream, cmd, arg);
2579 }
2580
2581 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2582 {
2583         struct snd_pcm_substream *substream =
2584                 snd_soc_dpcm_get_substream(fe, stream);
2585         enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2586         int err;
2587
2588         dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2589                         stream ? "capture" : "playback", fe->dai_link->name);
2590
2591         if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2592                 /* call bespoke trigger - FE takes care of all BE triggers */
2593                 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2594                                 fe->dai_link->name);
2595
2596                 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2597                 if (err < 0)
2598                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2599         } else {
2600                 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2601                         fe->dai_link->name);
2602
2603                 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2604                 if (err < 0)
2605                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2606         }
2607
2608         err = dpcm_be_dai_hw_free(fe, stream);
2609         if (err < 0)
2610                 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2611
2612         err = dpcm_be_dai_shutdown(fe, stream);
2613         if (err < 0)
2614                 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2615
2616         /* run the stream event for each BE */
2617         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2618
2619         return 0;
2620 }
2621
2622 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2623 {
2624         struct snd_pcm_substream *substream =
2625                 snd_soc_dpcm_get_substream(fe, stream);
2626         struct snd_soc_dpcm *dpcm;
2627         enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2628         int ret;
2629
2630         dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2631                         stream ? "capture" : "playback", fe->dai_link->name);
2632
2633         /* Only start the BE if the FE is ready */
2634         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2635                 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2636                 return -EINVAL;
2637
2638         /* startup must always be called for new BEs */
2639         ret = dpcm_be_dai_startup(fe, stream);
2640         if (ret < 0)
2641                 goto disconnect;
2642
2643         /* keep going if FE state is > open */
2644         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2645                 return 0;
2646
2647         ret = dpcm_be_dai_hw_params(fe, stream);
2648         if (ret < 0)
2649                 goto close;
2650
2651         /* keep going if FE state is > hw_params */
2652         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2653                 return 0;
2654
2655
2656         ret = dpcm_be_dai_prepare(fe, stream);
2657         if (ret < 0)
2658                 goto hw_free;
2659
2660         /* run the stream event for each BE */
2661         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2662
2663         /* keep going if FE state is > prepare */
2664         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2665                 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2666                 return 0;
2667
2668         if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2669                 /* call trigger on the frontend - FE takes care of all BE triggers */
2670                 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2671                                 fe->dai_link->name);
2672
2673                 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2674                 if (ret < 0) {
2675                         dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2676                         goto hw_free;
2677                 }
2678         } else {
2679                 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2680                         fe->dai_link->name);
2681
2682                 ret = dpcm_be_dai_trigger(fe, stream,
2683                                         SNDRV_PCM_TRIGGER_START);
2684                 if (ret < 0) {
2685                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2686                         goto hw_free;
2687                 }
2688         }
2689
2690         return 0;
2691
2692 hw_free:
2693         dpcm_be_dai_hw_free(fe, stream);
2694 close:
2695         dpcm_be_dai_shutdown(fe, stream);
2696 disconnect:
2697         /* disconnect any non started BEs */
2698         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2699                 struct snd_soc_pcm_runtime *be = dpcm->be;
2700                 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2701                                 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2702         }
2703
2704         return ret;
2705 }
2706
2707 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2708 {
2709         int ret;
2710
2711         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2712         ret = dpcm_run_update_startup(fe, stream);
2713         if (ret < 0)
2714                 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2715         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2716
2717         return ret;
2718 }
2719
2720 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2721 {
2722         int ret;
2723
2724         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2725         ret = dpcm_run_update_shutdown(fe, stream);
2726         if (ret < 0)
2727                 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2728         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2729
2730         return ret;
2731 }
2732
2733 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2734 {
2735         struct snd_soc_dapm_widget_list *list;
2736         int count, paths;
2737
2738         if (!fe->dai_link->dynamic)
2739                 return 0;
2740
2741         /* only check active links */
2742         if (!fe->cpu_dai->active)
2743                 return 0;
2744
2745         /* DAPM sync will call this to update DSP paths */
2746         dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2747                 new ? "new" : "old", fe->dai_link->name);
2748
2749         /* skip if FE doesn't have playback capability */
2750         if (!fe->cpu_dai->driver->playback.channels_min ||
2751             !fe->codec_dai->driver->playback.channels_min)
2752                 goto capture;
2753
2754         /* skip if FE isn't currently playing */
2755         if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
2756                 goto capture;
2757
2758         paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2759         if (paths < 0) {
2760                 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2761                          fe->dai_link->name,  "playback");
2762                 return paths;
2763         }
2764
2765         /* update any playback paths */
2766         count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
2767         if (count) {
2768                 if (new)
2769                         dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2770                 else
2771                         dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2772
2773                 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2774                 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2775         }
2776
2777         dpcm_path_put(&list);
2778
2779 capture:
2780         /* skip if FE doesn't have capture capability */
2781         if (!fe->cpu_dai->driver->capture.channels_min ||
2782             !fe->codec_dai->driver->capture.channels_min)
2783                 return 0;
2784
2785         /* skip if FE isn't currently capturing */
2786         if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
2787                 return 0;
2788
2789         paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2790         if (paths < 0) {
2791                 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2792                          fe->dai_link->name,  "capture");
2793                 return paths;
2794         }
2795
2796         /* update any old capture paths */
2797         count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
2798         if (count) {
2799                 if (new)
2800                         dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2801                 else
2802                         dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2803
2804                 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2805                 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2806         }
2807
2808         dpcm_path_put(&list);
2809
2810         return 0;
2811 }
2812
2813 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2814  * any DAI links.
2815  */
2816 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2817 {
2818         struct snd_soc_pcm_runtime *fe;
2819         int ret = 0;
2820
2821         mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2822         /* shutdown all old paths first */
2823         list_for_each_entry(fe, &card->rtd_list, list) {
2824                 ret = soc_dpcm_fe_runtime_update(fe, 0);
2825                 if (ret)
2826                         goto out;
2827         }
2828
2829         /* bring new paths up */
2830         list_for_each_entry(fe, &card->rtd_list, list) {
2831                 ret = soc_dpcm_fe_runtime_update(fe, 1);
2832                 if (ret)
2833                         goto out;
2834         }
2835
2836 out:
2837         mutex_unlock(&card->mutex);
2838         return ret;
2839 }
2840 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2841 {
2842         struct snd_soc_dpcm *dpcm;
2843         struct list_head *clients =
2844                 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2845
2846         list_for_each_entry(dpcm, clients, list_be) {
2847
2848                 struct snd_soc_pcm_runtime *be = dpcm->be;
2849                 int i;
2850
2851                 if (be->dai_link->ignore_suspend)
2852                         continue;
2853
2854                 for (i = 0; i < be->num_codecs; i++) {
2855                         struct snd_soc_dai *dai = be->codec_dais[i];
2856                         struct snd_soc_dai_driver *drv = dai->driver;
2857
2858                         dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2859                                          be->dai_link->name);
2860
2861                         if (drv->ops && drv->ops->digital_mute &&
2862                                                         dai->playback_active)
2863                                 drv->ops->digital_mute(dai, mute);
2864                 }
2865         }
2866
2867         return 0;
2868 }
2869
2870 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2871 {
2872         struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2873         struct snd_soc_dpcm *dpcm;
2874         struct snd_soc_dapm_widget_list *list;
2875         int ret;
2876         int stream = fe_substream->stream;
2877
2878         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2879         fe->dpcm[stream].runtime = fe_substream->runtime;
2880
2881         ret = dpcm_path_get(fe, stream, &list);
2882         if (ret < 0) {
2883                 mutex_unlock(&fe->card->mutex);
2884                 return ret;
2885         } else if (ret == 0) {
2886                 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2887                         fe->dai_link->name, stream ? "capture" : "playback");
2888         }
2889
2890         /* calculate valid and active FE <-> BE dpcms */
2891         dpcm_process_paths(fe, stream, &list, 1);
2892
2893         ret = dpcm_fe_dai_startup(fe_substream);
2894         if (ret < 0) {
2895                 /* clean up all links */
2896                 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2897                         dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2898
2899                 dpcm_be_disconnect(fe, stream);
2900                 fe->dpcm[stream].runtime = NULL;
2901         }
2902
2903         dpcm_clear_pending_state(fe, stream);
2904         dpcm_path_put(&list);
2905         mutex_unlock(&fe->card->mutex);
2906         return ret;
2907 }
2908
2909 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2910 {
2911         struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2912         struct snd_soc_dpcm *dpcm;
2913         int stream = fe_substream->stream, ret;
2914
2915         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2916         ret = dpcm_fe_dai_shutdown(fe_substream);
2917
2918         /* mark FE's links ready to prune */
2919         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2920                 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2921
2922         dpcm_be_disconnect(fe, stream);
2923
2924         fe->dpcm[stream].runtime = NULL;
2925         mutex_unlock(&fe->card->mutex);
2926         return ret;
2927 }
2928
2929 static void soc_pcm_private_free(struct snd_pcm *pcm)
2930 {
2931         struct snd_soc_pcm_runtime *rtd = pcm->private_data;
2932         struct snd_soc_rtdcom_list *rtdcom;
2933         struct snd_soc_component *component;
2934
2935         /* need to sync the delayed work before releasing resources */
2936         flush_delayed_work(&rtd->delayed_work);
2937         for_each_rtdcom(rtd, rtdcom) {
2938                 component = rtdcom->component;
2939
2940                 if (component->driver->pcm_free)
2941                         component->driver->pcm_free(pcm);
2942         }
2943 }
2944
2945 static int soc_rtdcom_ack(struct snd_pcm_substream *substream)
2946 {
2947         struct snd_soc_pcm_runtime *rtd = substream->private_data;
2948         struct snd_soc_rtdcom_list *rtdcom;
2949         struct snd_soc_component *component;
2950
2951         for_each_rtdcom(rtd, rtdcom) {
2952                 component = rtdcom->component;
2953
2954                 if (!component->driver->ops ||
2955                     !component->driver->ops->ack)
2956                         continue;
2957
2958                 /* FIXME. it returns 1st ask now */
2959                 return component->driver->ops->ack(substream);
2960         }
2961
2962         return -EINVAL;
2963 }
2964
2965 static int soc_rtdcom_copy_user(struct snd_pcm_substream *substream, int channel,
2966                                 unsigned long pos, void __user *buf,
2967                                 unsigned long bytes)
2968 {
2969         struct snd_soc_pcm_runtime *rtd = substream->private_data;
2970         struct snd_soc_rtdcom_list *rtdcom;
2971         struct snd_soc_component *component;
2972
2973         for_each_rtdcom(rtd, rtdcom) {
2974                 component = rtdcom->component;
2975
2976                 if (!component->driver->ops ||
2977                     !component->driver->ops->copy_user)
2978                         continue;
2979
2980                 /* FIXME. it returns 1st copy now */
2981                 return component->driver->ops->copy_user(substream, channel,
2982                                                          pos, buf, bytes);
2983         }
2984
2985         return -EINVAL;
2986 }
2987
2988 static int soc_rtdcom_copy_kernel(struct snd_pcm_substream *substream, int channel,
2989                                   unsigned long pos, void *buf, unsigned long bytes)
2990 {
2991         struct snd_soc_pcm_runtime *rtd = substream->private_data;
2992         struct snd_soc_rtdcom_list *rtdcom;
2993         struct snd_soc_component *component;
2994
2995         for_each_rtdcom(rtd, rtdcom) {
2996                 component = rtdcom->component;
2997
2998                 if (!component->driver->ops ||
2999                     !component->driver->ops->copy_kernel)
3000                         continue;
3001
3002                 /* FIXME. it returns 1st copy now */
3003                 return component->driver->ops->copy_kernel(substream, channel,
3004                                                            pos, buf, bytes);
3005         }
3006
3007         return -EINVAL;
3008 }
3009
3010 static int soc_rtdcom_fill_silence(struct snd_pcm_substream *substream, int channel,
3011                                    unsigned long pos, unsigned long bytes)
3012 {
3013         struct snd_soc_pcm_runtime *rtd = substream->private_data;
3014         struct snd_soc_rtdcom_list *rtdcom;
3015         struct snd_soc_component *component;
3016
3017         for_each_rtdcom(rtd, rtdcom) {
3018                 component = rtdcom->component;
3019
3020                 if (!component->driver->ops ||
3021                     !component->driver->ops->fill_silence)
3022                         continue;
3023
3024                 /* FIXME. it returns 1st silence now */
3025                 return component->driver->ops->fill_silence(substream, channel,
3026                                                             pos, bytes);
3027         }
3028
3029         return -EINVAL;
3030 }
3031
3032 static struct page *soc_rtdcom_page(struct snd_pcm_substream *substream,
3033                                     unsigned long offset)
3034 {
3035         struct snd_soc_pcm_runtime *rtd = substream->private_data;
3036         struct snd_soc_rtdcom_list *rtdcom;
3037         struct snd_soc_component *component;
3038         struct page *page;
3039
3040         for_each_rtdcom(rtd, rtdcom) {
3041                 component = rtdcom->component;
3042
3043                 if (!component->driver->ops ||
3044                     !component->driver->ops->page)
3045                         continue;
3046
3047                 /* FIXME. it returns 1st page now */
3048                 page = component->driver->ops->page(substream, offset);
3049                 if (page)
3050                         return page;
3051         }
3052
3053         return NULL;
3054 }
3055
3056 static int soc_rtdcom_mmap(struct snd_pcm_substream *substream,
3057                            struct vm_area_struct *vma)
3058 {
3059         struct snd_soc_pcm_runtime *rtd = substream->private_data;
3060         struct snd_soc_rtdcom_list *rtdcom;
3061         struct snd_soc_component *component;
3062
3063         for_each_rtdcom(rtd, rtdcom) {
3064                 component = rtdcom->component;
3065
3066                 if (!component->driver->ops ||
3067                     !component->driver->ops->mmap)
3068                         continue;
3069
3070                 /* FIXME. it returns 1st mmap now */
3071                 return component->driver->ops->mmap(substream, vma);
3072         }
3073
3074         return -EINVAL;
3075 }
3076
3077 /* create a new pcm */
3078 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
3079 {
3080         struct snd_soc_dai *codec_dai;
3081         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3082         struct snd_soc_component *component;
3083         struct snd_soc_rtdcom_list *rtdcom;
3084         struct snd_pcm *pcm;
3085         char new_name[64];
3086         int ret = 0, playback = 0, capture = 0;
3087         int i;
3088
3089         if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
3090                 playback = rtd->dai_link->dpcm_playback;
3091                 capture = rtd->dai_link->dpcm_capture;
3092         } else {
3093                 for (i = 0; i < rtd->num_codecs; i++) {
3094                         codec_dai = rtd->codec_dais[i];
3095                         if (codec_dai->driver->playback.channels_min)
3096                                 playback = 1;
3097                         if (codec_dai->driver->capture.channels_min)
3098                                 capture = 1;
3099                 }
3100
3101                 capture = capture && cpu_dai->driver->capture.channels_min;
3102                 playback = playback && cpu_dai->driver->playback.channels_min;
3103         }
3104
3105         if (rtd->dai_link->playback_only) {
3106                 playback = 1;
3107                 capture = 0;
3108         }
3109
3110         if (rtd->dai_link->capture_only) {
3111                 playback = 0;
3112                 capture = 1;
3113         }
3114
3115         /* create the PCM */
3116         if (rtd->dai_link->no_pcm) {
3117                 snprintf(new_name, sizeof(new_name), "(%s)",
3118                         rtd->dai_link->stream_name);
3119
3120                 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
3121                                 playback, capture, &pcm);
3122         } else {
3123                 if (rtd->dai_link->dynamic)
3124                         snprintf(new_name, sizeof(new_name), "%s (*)",
3125                                 rtd->dai_link->stream_name);
3126                 else
3127                         snprintf(new_name, sizeof(new_name), "%s %s-%d",
3128                                 rtd->dai_link->stream_name,
3129                                 (rtd->num_codecs > 1) ?
3130                                 "multicodec" : rtd->codec_dai->name, num);
3131
3132                 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
3133                         capture, &pcm);
3134         }
3135         if (ret < 0) {
3136                 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
3137                         rtd->dai_link->name);
3138                 return ret;
3139         }
3140         dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
3141
3142         /* DAPM dai link stream work */
3143         INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
3144
3145         pcm->nonatomic = rtd->dai_link->nonatomic;
3146         rtd->pcm = pcm;
3147         pcm->private_data = rtd;
3148
3149         if (rtd->dai_link->no_pcm) {
3150                 if (playback)
3151                         pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
3152                 if (capture)
3153                         pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
3154                 goto out;
3155         }
3156
3157         /* ASoC PCM operations */
3158         if (rtd->dai_link->dynamic) {
3159                 rtd->ops.open           = dpcm_fe_dai_open;
3160                 rtd->ops.hw_params      = dpcm_fe_dai_hw_params;
3161                 rtd->ops.prepare        = dpcm_fe_dai_prepare;
3162                 rtd->ops.trigger        = dpcm_fe_dai_trigger;
3163                 rtd->ops.hw_free        = dpcm_fe_dai_hw_free;
3164                 rtd->ops.close          = dpcm_fe_dai_close;
3165                 rtd->ops.pointer        = soc_pcm_pointer;
3166                 rtd->ops.ioctl          = soc_pcm_ioctl;
3167         } else {
3168                 rtd->ops.open           = soc_pcm_open;
3169                 rtd->ops.hw_params      = soc_pcm_hw_params;
3170                 rtd->ops.prepare        = soc_pcm_prepare;
3171                 rtd->ops.trigger        = soc_pcm_trigger;
3172                 rtd->ops.hw_free        = soc_pcm_hw_free;
3173                 rtd->ops.close          = soc_pcm_close;
3174                 rtd->ops.pointer        = soc_pcm_pointer;
3175                 rtd->ops.ioctl          = soc_pcm_ioctl;
3176         }
3177
3178         for_each_rtdcom(rtd, rtdcom) {
3179                 const struct snd_pcm_ops *ops = rtdcom->component->driver->ops;
3180
3181                 if (!ops)
3182                         continue;
3183
3184                 if (ops->ack)
3185                         rtd->ops.ack            = soc_rtdcom_ack;
3186                 if (ops->copy_user)
3187                         rtd->ops.copy_user      = soc_rtdcom_copy_user;
3188                 if (ops->copy_kernel)
3189                         rtd->ops.copy_kernel    = soc_rtdcom_copy_kernel;
3190                 if (ops->fill_silence)
3191                         rtd->ops.fill_silence   = soc_rtdcom_fill_silence;
3192                 if (ops->page)
3193                         rtd->ops.page           = soc_rtdcom_page;
3194                 if (ops->mmap)
3195                         rtd->ops.mmap           = soc_rtdcom_mmap;
3196         }
3197
3198         if (playback)
3199                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3200
3201         if (capture)
3202                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3203
3204         for_each_rtdcom(rtd, rtdcom) {
3205                 component = rtdcom->component;
3206
3207                 if (!component->driver->pcm_new)
3208                         continue;
3209
3210                 ret = component->driver->pcm_new(rtd);
3211                 if (ret < 0) {
3212                         dev_err(component->dev,
3213                                 "ASoC: pcm constructor failed: %d\n",
3214                                 ret);
3215                         return ret;
3216                 }
3217         }
3218
3219         pcm->private_free = soc_pcm_private_free;
3220 out:
3221         dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3222                  (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3223                  cpu_dai->name);
3224         return ret;
3225 }
3226
3227 /* is the current PCM operation for this FE ? */
3228 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3229 {
3230         if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3231                 return 1;
3232         return 0;
3233 }
3234 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3235
3236 /* is the current PCM operation for this BE ? */
3237 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3238                 struct snd_soc_pcm_runtime *be, int stream)
3239 {
3240         if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3241            ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3242                   be->dpcm[stream].runtime_update))
3243                 return 1;
3244         return 0;
3245 }
3246 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3247
3248 /* get the substream for this BE */
3249 struct snd_pcm_substream *
3250         snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3251 {
3252         return be->pcm->streams[stream].substream;
3253 }
3254 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3255
3256 /* get the BE runtime state */
3257 enum snd_soc_dpcm_state
3258         snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3259 {
3260         return be->dpcm[stream].state;
3261 }
3262 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3263
3264 /* set the BE runtime state */
3265 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3266                 int stream, enum snd_soc_dpcm_state state)
3267 {
3268         be->dpcm[stream].state = state;
3269 }
3270 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3271
3272 /*
3273  * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3274  * are not running, paused or suspended for the specified stream direction.
3275  */
3276 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3277                 struct snd_soc_pcm_runtime *be, int stream)
3278 {
3279         struct snd_soc_dpcm *dpcm;
3280         int state;
3281
3282         list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3283
3284                 if (dpcm->fe == fe)
3285                         continue;
3286
3287                 state = dpcm->fe->dpcm[stream].state;
3288                 if (state == SND_SOC_DPCM_STATE_START ||
3289                         state == SND_SOC_DPCM_STATE_PAUSED ||
3290                         state == SND_SOC_DPCM_STATE_SUSPEND)
3291                         return 0;
3292         }
3293
3294         /* it's safe to free/stop this BE DAI */
3295         return 1;
3296 }
3297 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3298
3299 /*
3300  * We can only change hw params a BE DAI if any of it's FE are not prepared,
3301  * running, paused or suspended for the specified stream direction.
3302  */
3303 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3304                 struct snd_soc_pcm_runtime *be, int stream)
3305 {
3306         struct snd_soc_dpcm *dpcm;
3307         int state;
3308
3309         list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3310
3311                 if (dpcm->fe == fe)
3312                         continue;
3313
3314                 state = dpcm->fe->dpcm[stream].state;
3315                 if (state == SND_SOC_DPCM_STATE_START ||
3316                         state == SND_SOC_DPCM_STATE_PAUSED ||
3317                         state == SND_SOC_DPCM_STATE_SUSPEND ||
3318                         state == SND_SOC_DPCM_STATE_PREPARE)
3319                         return 0;
3320         }
3321
3322         /* it's safe to change hw_params */
3323         return 1;
3324 }
3325 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3326
3327 #ifdef CONFIG_DEBUG_FS
3328 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
3329 {
3330         switch (state) {
3331         case SND_SOC_DPCM_STATE_NEW:
3332                 return "new";
3333         case SND_SOC_DPCM_STATE_OPEN:
3334                 return "open";
3335         case SND_SOC_DPCM_STATE_HW_PARAMS:
3336                 return "hw_params";
3337         case SND_SOC_DPCM_STATE_PREPARE:
3338                 return "prepare";
3339         case SND_SOC_DPCM_STATE_START:
3340                 return "start";
3341         case SND_SOC_DPCM_STATE_STOP:
3342                 return "stop";
3343         case SND_SOC_DPCM_STATE_SUSPEND:
3344                 return "suspend";
3345         case SND_SOC_DPCM_STATE_PAUSED:
3346                 return "paused";
3347         case SND_SOC_DPCM_STATE_HW_FREE:
3348                 return "hw_free";
3349         case SND_SOC_DPCM_STATE_CLOSE:
3350                 return "close";
3351         }
3352
3353         return "unknown";
3354 }
3355
3356 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3357                                 int stream, char *buf, size_t size)
3358 {
3359         struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3360         struct snd_soc_dpcm *dpcm;
3361         ssize_t offset = 0;
3362
3363         /* FE state */
3364         offset += scnprintf(buf + offset, size - offset,
3365                         "[%s - %s]\n", fe->dai_link->name,
3366                         stream ? "Capture" : "Playback");
3367
3368         offset += scnprintf(buf + offset, size - offset, "State: %s\n",
3369                         dpcm_state_string(fe->dpcm[stream].state));
3370
3371         if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3372             (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3373                 offset += scnprintf(buf + offset, size - offset,
3374                                 "Hardware Params: "
3375                                 "Format = %s, Channels = %d, Rate = %d\n",
3376                                 snd_pcm_format_name(params_format(params)),
3377                                 params_channels(params),
3378                                 params_rate(params));
3379
3380         /* BEs state */
3381         offset += scnprintf(buf + offset, size - offset, "Backends:\n");
3382
3383         if (list_empty(&fe->dpcm[stream].be_clients)) {
3384                 offset += scnprintf(buf + offset, size - offset,
3385                                 " No active DSP links\n");
3386                 goto out;
3387         }
3388
3389         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
3390                 struct snd_soc_pcm_runtime *be = dpcm->be;
3391                 params = &dpcm->hw_params;
3392
3393                 offset += scnprintf(buf + offset, size - offset,
3394                                 "- %s\n", be->dai_link->name);
3395
3396                 offset += scnprintf(buf + offset, size - offset,
3397                                 "   State: %s\n",
3398                                 dpcm_state_string(be->dpcm[stream].state));
3399
3400                 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3401                     (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3402                         offset += scnprintf(buf + offset, size - offset,
3403                                 "   Hardware Params: "
3404                                 "Format = %s, Channels = %d, Rate = %d\n",
3405                                 snd_pcm_format_name(params_format(params)),
3406                                 params_channels(params),
3407                                 params_rate(params));
3408         }
3409
3410 out:
3411         return offset;
3412 }
3413
3414 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3415                                 size_t count, loff_t *ppos)
3416 {
3417         struct snd_soc_pcm_runtime *fe = file->private_data;
3418         ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3419         char *buf;
3420
3421         buf = kmalloc(out_count, GFP_KERNEL);
3422         if (!buf)
3423                 return -ENOMEM;
3424
3425         if (fe->cpu_dai->driver->playback.channels_min)
3426                 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3427                                         buf + offset, out_count - offset);
3428
3429         if (fe->cpu_dai->driver->capture.channels_min)
3430                 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3431                                         buf + offset, out_count - offset);
3432
3433         ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3434
3435         kfree(buf);
3436         return ret;
3437 }
3438
3439 static const struct file_operations dpcm_state_fops = {
3440         .open = simple_open,
3441         .read = dpcm_state_read_file,
3442         .llseek = default_llseek,
3443 };
3444
3445 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3446 {
3447         if (!rtd->dai_link)
3448                 return;
3449
3450         if (!rtd->card->debugfs_card_root)
3451                 return;
3452
3453         rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3454                         rtd->card->debugfs_card_root);
3455         if (!rtd->debugfs_dpcm_root) {
3456                 dev_dbg(rtd->dev,
3457                          "ASoC: Failed to create dpcm debugfs directory %s\n",
3458                          rtd->dai_link->name);
3459                 return;
3460         }
3461
3462         debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3463                             rtd, &dpcm_state_fops);
3464 }
3465 #endif