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