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