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