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