OSDN Git Service

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