Linux-libre 3.16.41-gnu
[librecmc/linux-libre.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
822         if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
823                 ret = rtd->dai_link->ops->trigger(substream, cmd);
824                 if (ret < 0)
825                         return ret;
826         }
827
828         return 0;
829 }
830
831 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
832                                    int cmd)
833 {
834         struct snd_soc_pcm_runtime *rtd = substream->private_data;
835         struct snd_soc_platform *platform = rtd->platform;
836         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
837         struct snd_soc_dai *codec_dai = rtd->codec_dai;
838         int ret;
839
840         if (codec_dai->driver->ops &&
841             codec_dai->driver->ops->bespoke_trigger) {
842                 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
843                 if (ret < 0)
844                         return ret;
845         }
846
847         if (platform->driver->bespoke_trigger) {
848                 ret = platform->driver->bespoke_trigger(substream, cmd);
849                 if (ret < 0)
850                         return ret;
851         }
852
853         if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
854                 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
855                 if (ret < 0)
856                         return ret;
857         }
858         return 0;
859 }
860 /*
861  * soc level wrapper for pointer callback
862  * If cpu_dai, codec_dai, platform driver has the delay callback, than
863  * the runtime->delay will be updated accordingly.
864  */
865 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
866 {
867         struct snd_soc_pcm_runtime *rtd = substream->private_data;
868         struct snd_soc_platform *platform = rtd->platform;
869         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
870         struct snd_soc_dai *codec_dai = rtd->codec_dai;
871         struct snd_pcm_runtime *runtime = substream->runtime;
872         snd_pcm_uframes_t offset = 0;
873         snd_pcm_sframes_t delay = 0;
874
875         if (platform->driver->ops && platform->driver->ops->pointer)
876                 offset = platform->driver->ops->pointer(substream);
877
878         if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
879                 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
880
881         if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
882                 delay += codec_dai->driver->ops->delay(substream, codec_dai);
883
884         if (platform->driver->delay)
885                 delay += platform->driver->delay(substream, codec_dai);
886
887         runtime->delay = delay;
888
889         return offset;
890 }
891
892 /* connect a FE and BE */
893 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
894                 struct snd_soc_pcm_runtime *be, int stream)
895 {
896         struct snd_soc_dpcm *dpcm;
897
898         /* only add new dpcms */
899         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
900                 if (dpcm->be == be && dpcm->fe == fe)
901                         return 0;
902         }
903
904         dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
905         if (!dpcm)
906                 return -ENOMEM;
907
908         dpcm->be = be;
909         dpcm->fe = fe;
910         be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
911         dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
912         list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
913         list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
914
915         dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
916                         stream ? "capture" : "playback",  fe->dai_link->name,
917                         stream ? "<-" : "->", be->dai_link->name);
918
919 #ifdef CONFIG_DEBUG_FS
920         dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
921                         fe->debugfs_dpcm_root, &dpcm->state);
922 #endif
923         return 1;
924 }
925
926 /* reparent a BE onto another FE */
927 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
928                         struct snd_soc_pcm_runtime *be, int stream)
929 {
930         struct snd_soc_dpcm *dpcm;
931         struct snd_pcm_substream *fe_substream, *be_substream;
932
933         /* reparent if BE is connected to other FEs */
934         if (!be->dpcm[stream].users)
935                 return;
936
937         be_substream = snd_soc_dpcm_get_substream(be, stream);
938
939         list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
940                 if (dpcm->fe == fe)
941                         continue;
942
943                 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
944                         stream ? "capture" : "playback",
945                         dpcm->fe->dai_link->name,
946                         stream ? "<-" : "->", dpcm->be->dai_link->name);
947
948                 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
949                 be_substream->runtime = fe_substream->runtime;
950                 break;
951         }
952 }
953
954 /* disconnect a BE and FE */
955 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
956 {
957         struct snd_soc_dpcm *dpcm, *d;
958
959         list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
960                 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
961                                 stream ? "capture" : "playback",
962                                 dpcm->be->dai_link->name);
963
964                 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
965                         continue;
966
967                 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
968                         stream ? "capture" : "playback", fe->dai_link->name,
969                         stream ? "<-" : "->", dpcm->be->dai_link->name);
970
971                 /* BEs still alive need new FE */
972                 dpcm_be_reparent(fe, dpcm->be, stream);
973
974 #ifdef CONFIG_DEBUG_FS
975                 debugfs_remove(dpcm->debugfs_state);
976 #endif
977                 list_del(&dpcm->list_be);
978                 list_del(&dpcm->list_fe);
979                 kfree(dpcm);
980         }
981 }
982
983 /* get BE for DAI widget and stream */
984 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
985                 struct snd_soc_dapm_widget *widget, int stream)
986 {
987         struct snd_soc_pcm_runtime *be;
988         int i;
989
990         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
991                 for (i = 0; i < card->num_links; i++) {
992                         be = &card->rtd[i];
993
994                         if (!be->dai_link->no_pcm)
995                                 continue;
996
997                         if (be->cpu_dai->playback_widget == widget ||
998                                 be->codec_dai->playback_widget == widget)
999                                 return be;
1000                 }
1001         } else {
1002
1003                 for (i = 0; i < card->num_links; i++) {
1004                         be = &card->rtd[i];
1005
1006                         if (!be->dai_link->no_pcm)
1007                                 continue;
1008
1009                         if (be->cpu_dai->capture_widget == widget ||
1010                                 be->codec_dai->capture_widget == widget)
1011                                 return be;
1012                 }
1013         }
1014
1015         dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1016                 stream ? "capture" : "playback", widget->name);
1017         return NULL;
1018 }
1019
1020 static inline struct snd_soc_dapm_widget *
1021         dai_get_widget(struct snd_soc_dai *dai, int stream)
1022 {
1023         if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1024                 return dai->playback_widget;
1025         else
1026                 return dai->capture_widget;
1027 }
1028
1029 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1030                 struct snd_soc_dapm_widget *widget)
1031 {
1032         int i;
1033
1034         for (i = 0; i < list->num_widgets; i++) {
1035                 if (widget == list->widgets[i])
1036                         return 1;
1037         }
1038
1039         return 0;
1040 }
1041
1042 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1043         int stream, struct snd_soc_dapm_widget_list **list_)
1044 {
1045         struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1046         struct snd_soc_dapm_widget_list *list;
1047         int paths;
1048
1049         list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
1050                         sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
1051         if (list == NULL)
1052                 return -ENOMEM;
1053
1054         /* get number of valid DAI paths and their widgets */
1055         paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
1056
1057         dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1058                         stream ? "capture" : "playback");
1059
1060         *list_ = list;
1061         return paths;
1062 }
1063
1064 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1065         struct snd_soc_dapm_widget_list **list_)
1066 {
1067         struct snd_soc_dpcm *dpcm;
1068         struct snd_soc_dapm_widget_list *list = *list_;
1069         struct snd_soc_dapm_widget *widget;
1070         int prune = 0;
1071
1072         /* Destroy any old FE <--> BE connections */
1073         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1074
1075                 /* is there a valid CPU DAI widget for this BE */
1076                 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1077
1078                 /* prune the BE if it's no longer in our active list */
1079                 if (widget && widget_in_list(list, widget))
1080                         continue;
1081
1082                 /* is there a valid CODEC DAI widget for this BE */
1083                 widget = dai_get_widget(dpcm->be->codec_dai, stream);
1084
1085                 /* prune the BE if it's no longer in our active list */
1086                 if (widget && widget_in_list(list, widget))
1087                         continue;
1088
1089                 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1090                         stream ? "capture" : "playback",
1091                         dpcm->be->dai_link->name, fe->dai_link->name);
1092                 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1093                 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1094                 prune++;
1095         }
1096
1097         dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1098         return prune;
1099 }
1100
1101 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1102         struct snd_soc_dapm_widget_list **list_)
1103 {
1104         struct snd_soc_card *card = fe->card;
1105         struct snd_soc_dapm_widget_list *list = *list_;
1106         struct snd_soc_pcm_runtime *be;
1107         int i, new = 0, err;
1108
1109         /* Create any new FE <--> BE connections */
1110         for (i = 0; i < list->num_widgets; i++) {
1111
1112                 switch (list->widgets[i]->id) {
1113                 case snd_soc_dapm_dai_in:
1114                 case snd_soc_dapm_dai_out:
1115                         break;
1116                 default:
1117                         continue;
1118                 }
1119
1120                 /* is there a valid BE rtd for this widget */
1121                 be = dpcm_get_be(card, list->widgets[i], stream);
1122                 if (!be) {
1123                         dev_err(fe->dev, "ASoC: no BE found for %s\n",
1124                                         list->widgets[i]->name);
1125                         continue;
1126                 }
1127
1128                 /* make sure BE is a real BE */
1129                 if (!be->dai_link->no_pcm)
1130                         continue;
1131
1132                 /* don't connect if FE is not running */
1133                 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1134                         continue;
1135
1136                 /* newly connected FE and BE */
1137                 err = dpcm_be_connect(fe, be, stream);
1138                 if (err < 0) {
1139                         dev_err(fe->dev, "ASoC: can't connect %s\n",
1140                                 list->widgets[i]->name);
1141                         break;
1142                 } else if (err == 0) /* already connected */
1143                         continue;
1144
1145                 /* new */
1146                 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1147                 new++;
1148         }
1149
1150         dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1151         return new;
1152 }
1153
1154 /*
1155  * Find the corresponding BE DAIs that source or sink audio to this
1156  * FE substream.
1157  */
1158 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1159         int stream, struct snd_soc_dapm_widget_list **list, int new)
1160 {
1161         if (new)
1162                 return dpcm_add_paths(fe, stream, list);
1163         else
1164                 return dpcm_prune_paths(fe, stream, list);
1165 }
1166
1167 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1168 {
1169         struct snd_soc_dpcm *dpcm;
1170
1171         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1172                 dpcm->be->dpcm[stream].runtime_update =
1173                                                 SND_SOC_DPCM_UPDATE_NO;
1174 }
1175
1176 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1177         int stream)
1178 {
1179         struct snd_soc_dpcm *dpcm;
1180
1181         /* disable any enabled and non active backends */
1182         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1183
1184                 struct snd_soc_pcm_runtime *be = dpcm->be;
1185                 struct snd_pcm_substream *be_substream =
1186                         snd_soc_dpcm_get_substream(be, stream);
1187
1188                 if (be->dpcm[stream].users == 0)
1189                         dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1190                                 stream ? "capture" : "playback",
1191                                 be->dpcm[stream].state);
1192
1193                 if (--be->dpcm[stream].users != 0)
1194                         continue;
1195
1196                 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1197                         continue;
1198
1199                 soc_pcm_close(be_substream);
1200                 be_substream->runtime = NULL;
1201                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1202         }
1203 }
1204
1205 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1206 {
1207         struct snd_soc_dpcm *dpcm;
1208         int err, count = 0;
1209
1210         /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1211         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1212
1213                 struct snd_soc_pcm_runtime *be = dpcm->be;
1214                 struct snd_pcm_substream *be_substream =
1215                         snd_soc_dpcm_get_substream(be, stream);
1216
1217                 if (!be_substream) {
1218                         dev_err(be->dev, "ASoC: no backend %s stream\n",
1219                                 stream ? "capture" : "playback");
1220                         continue;
1221                 }
1222
1223                 /* is this op for this BE ? */
1224                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1225                         continue;
1226
1227                 /* first time the dpcm is open ? */
1228                 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1229                         dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1230                                 stream ? "capture" : "playback",
1231                                 be->dpcm[stream].state);
1232
1233                 if (be->dpcm[stream].users++ != 0)
1234                         continue;
1235
1236                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1237                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1238                         continue;
1239
1240                 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1241                         stream ? "capture" : "playback", be->dai_link->name);
1242
1243                 be_substream->runtime = be->dpcm[stream].runtime;
1244                 err = soc_pcm_open(be_substream);
1245                 if (err < 0) {
1246                         dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1247                         be->dpcm[stream].users--;
1248                         if (be->dpcm[stream].users < 0)
1249                                 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1250                                         stream ? "capture" : "playback",
1251                                         be->dpcm[stream].state);
1252
1253                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1254                         goto unwind;
1255                 }
1256
1257                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1258                 count++;
1259         }
1260
1261         return count;
1262
1263 unwind:
1264         /* disable any enabled and non active backends */
1265         list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1266                 struct snd_soc_pcm_runtime *be = dpcm->be;
1267                 struct snd_pcm_substream *be_substream =
1268                         snd_soc_dpcm_get_substream(be, stream);
1269
1270                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1271                         continue;
1272
1273                 if (be->dpcm[stream].users == 0)
1274                         dev_err(be->dev, "ASoC: no users %s at close %d\n",
1275                                 stream ? "capture" : "playback",
1276                                 be->dpcm[stream].state);
1277
1278                 if (--be->dpcm[stream].users != 0)
1279                         continue;
1280
1281                 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1282                         continue;
1283
1284                 soc_pcm_close(be_substream);
1285                 be_substream->runtime = NULL;
1286                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1287         }
1288
1289         return err;
1290 }
1291
1292 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1293         struct snd_soc_pcm_stream *stream)
1294 {
1295         runtime->hw.rate_min = stream->rate_min;
1296         runtime->hw.rate_max = stream->rate_max;
1297         runtime->hw.channels_min = stream->channels_min;
1298         runtime->hw.channels_max = stream->channels_max;
1299         if (runtime->hw.formats)
1300                 runtime->hw.formats &= stream->formats;
1301         else
1302                 runtime->hw.formats = stream->formats;
1303         runtime->hw.rates = stream->rates;
1304 }
1305
1306 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1307 {
1308         struct snd_pcm_runtime *runtime = substream->runtime;
1309         struct snd_soc_pcm_runtime *rtd = substream->private_data;
1310         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1311         struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1312
1313         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1314                 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1315         else
1316                 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1317 }
1318
1319 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1320
1321 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1322  * for avoiding the race with trigger callback.
1323  * If the state is unset and a trigger is pending while the previous operation,
1324  * process the pending trigger action here.
1325  */
1326 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1327                                      int stream, enum snd_soc_dpcm_update state)
1328 {
1329         struct snd_pcm_substream *substream =
1330                 snd_soc_dpcm_get_substream(fe, stream);
1331
1332         snd_pcm_stream_lock_irq(substream);
1333         if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1334                 dpcm_fe_dai_do_trigger(substream,
1335                                        fe->dpcm[stream].trigger_pending - 1);
1336                 fe->dpcm[stream].trigger_pending = 0;
1337         }
1338         fe->dpcm[stream].runtime_update = state;
1339         snd_pcm_stream_unlock_irq(substream);
1340 }
1341
1342 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1343 {
1344         struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1345         struct snd_pcm_runtime *runtime = fe_substream->runtime;
1346         int stream = fe_substream->stream, ret = 0;
1347
1348         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1349
1350         ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1351         if (ret < 0) {
1352                 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1353                 goto be_err;
1354         }
1355
1356         dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1357
1358         /* start the DAI frontend */
1359         ret = soc_pcm_open(fe_substream);
1360         if (ret < 0) {
1361                 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1362                 goto unwind;
1363         }
1364
1365         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1366
1367         dpcm_set_fe_runtime(fe_substream);
1368         snd_pcm_limit_hw_rates(runtime);
1369
1370         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1371         return 0;
1372
1373 unwind:
1374         dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1375 be_err:
1376         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1377         return ret;
1378 }
1379
1380 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1381 {
1382         struct snd_soc_dpcm *dpcm;
1383
1384         /* only shutdown BEs that are either sinks or sources to this FE DAI */
1385         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1386
1387                 struct snd_soc_pcm_runtime *be = dpcm->be;
1388                 struct snd_pcm_substream *be_substream =
1389                         snd_soc_dpcm_get_substream(be, stream);
1390
1391                 /* is this op for this BE ? */
1392                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1393                         continue;
1394
1395                 if (be->dpcm[stream].users == 0)
1396                         dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1397                                 stream ? "capture" : "playback",
1398                                 be->dpcm[stream].state);
1399
1400                 if (--be->dpcm[stream].users != 0)
1401                         continue;
1402
1403                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1404                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1405                         continue;
1406
1407                 dev_dbg(be->dev, "ASoC: close BE %s\n",
1408                         dpcm->fe->dai_link->name);
1409
1410                 soc_pcm_close(be_substream);
1411                 be_substream->runtime = NULL;
1412
1413                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1414         }
1415         return 0;
1416 }
1417
1418 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1419 {
1420         struct snd_soc_pcm_runtime *fe = substream->private_data;
1421         int stream = substream->stream;
1422
1423         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1424
1425         /* shutdown the BEs */
1426         dpcm_be_dai_shutdown(fe, substream->stream);
1427
1428         dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1429
1430         /* now shutdown the frontend */
1431         soc_pcm_close(substream);
1432
1433         /* run the stream event for each BE */
1434         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1435
1436         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1437         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1438         return 0;
1439 }
1440
1441 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1442 {
1443         struct snd_soc_dpcm *dpcm;
1444
1445         /* only hw_params backends that are either sinks or sources
1446          * to this frontend DAI */
1447         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1448
1449                 struct snd_soc_pcm_runtime *be = dpcm->be;
1450                 struct snd_pcm_substream *be_substream =
1451                         snd_soc_dpcm_get_substream(be, stream);
1452
1453                 /* is this op for this BE ? */
1454                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1455                         continue;
1456
1457                 /* only free hw when no longer used - check all FEs */
1458                 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1459                                 continue;
1460
1461                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1462                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1463                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1464                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1465                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1466                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1467                         continue;
1468
1469                 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1470                         dpcm->fe->dai_link->name);
1471
1472                 soc_pcm_hw_free(be_substream);
1473
1474                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1475         }
1476
1477         return 0;
1478 }
1479
1480 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1481 {
1482         struct snd_soc_pcm_runtime *fe = substream->private_data;
1483         int err, stream = substream->stream;
1484
1485         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1486         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1487
1488         dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1489
1490         /* call hw_free on the frontend */
1491         err = soc_pcm_hw_free(substream);
1492         if (err < 0)
1493                 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1494                         fe->dai_link->name);
1495
1496         /* only hw_params backends that are either sinks or sources
1497          * to this frontend DAI */
1498         err = dpcm_be_dai_hw_free(fe, stream);
1499
1500         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1501         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1502
1503         mutex_unlock(&fe->card->mutex);
1504         return 0;
1505 }
1506
1507 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1508 {
1509         struct snd_soc_dpcm *dpcm;
1510         int ret;
1511
1512         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1513
1514                 struct snd_soc_pcm_runtime *be = dpcm->be;
1515                 struct snd_pcm_substream *be_substream =
1516                         snd_soc_dpcm_get_substream(be, stream);
1517
1518                 /* is this op for this BE ? */
1519                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1520                         continue;
1521
1522                 /* only allow hw_params() if no connected FEs are running */
1523                 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1524                         continue;
1525
1526                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1527                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1528                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1529                         continue;
1530
1531                 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1532                         dpcm->fe->dai_link->name);
1533
1534                 /* copy params for each dpcm */
1535                 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1536                                 sizeof(struct snd_pcm_hw_params));
1537
1538                 /* perform any hw_params fixups */
1539                 if (be->dai_link->be_hw_params_fixup) {
1540                         ret = be->dai_link->be_hw_params_fixup(be,
1541                                         &dpcm->hw_params);
1542                         if (ret < 0) {
1543                                 dev_err(be->dev,
1544                                         "ASoC: hw_params BE fixup failed %d\n",
1545                                         ret);
1546                                 goto unwind;
1547                         }
1548                 }
1549
1550                 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1551                 if (ret < 0) {
1552                         dev_err(dpcm->be->dev,
1553                                 "ASoC: hw_params BE failed %d\n", ret);
1554                         goto unwind;
1555                 }
1556
1557                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1558         }
1559         return 0;
1560
1561 unwind:
1562         /* disable any enabled and non active backends */
1563         list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1564                 struct snd_soc_pcm_runtime *be = dpcm->be;
1565                 struct snd_pcm_substream *be_substream =
1566                         snd_soc_dpcm_get_substream(be, stream);
1567
1568                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1569                         continue;
1570
1571                 /* only allow hw_free() if no connected FEs are running */
1572                 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1573                         continue;
1574
1575                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1576                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1577                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1578                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1579                         continue;
1580
1581                 soc_pcm_hw_free(be_substream);
1582         }
1583
1584         return ret;
1585 }
1586
1587 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1588                                  struct snd_pcm_hw_params *params)
1589 {
1590         struct snd_soc_pcm_runtime *fe = substream->private_data;
1591         int ret, stream = substream->stream;
1592
1593         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1594         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1595
1596         memcpy(&fe->dpcm[substream->stream].hw_params, params,
1597                         sizeof(struct snd_pcm_hw_params));
1598         ret = dpcm_be_dai_hw_params(fe, substream->stream);
1599         if (ret < 0) {
1600                 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
1601                 goto out;
1602         }
1603
1604         dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1605                         fe->dai_link->name, params_rate(params),
1606                         params_channels(params), params_format(params));
1607
1608         /* call hw_params on the frontend */
1609         ret = soc_pcm_hw_params(substream, params);
1610         if (ret < 0) {
1611                 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
1612                 dpcm_be_dai_hw_free(fe, stream);
1613          } else
1614                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1615
1616 out:
1617         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1618         mutex_unlock(&fe->card->mutex);
1619         return ret;
1620 }
1621
1622 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1623                 struct snd_pcm_substream *substream, int cmd)
1624 {
1625         int ret;
1626
1627         dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
1628                         dpcm->fe->dai_link->name, cmd);
1629
1630         ret = soc_pcm_trigger(substream, cmd);
1631         if (ret < 0)
1632                 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
1633
1634         return ret;
1635 }
1636
1637 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1638                                int cmd)
1639 {
1640         struct snd_soc_dpcm *dpcm;
1641         int ret = 0;
1642
1643         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1644
1645                 struct snd_soc_pcm_runtime *be = dpcm->be;
1646                 struct snd_pcm_substream *be_substream =
1647                         snd_soc_dpcm_get_substream(be, stream);
1648
1649                 /* is this op for this BE ? */
1650                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1651                         continue;
1652
1653                 switch (cmd) {
1654                 case SNDRV_PCM_TRIGGER_START:
1655                         if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1656                             (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1657                                 continue;
1658
1659                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1660                         if (ret)
1661                                 return ret;
1662
1663                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1664                         break;
1665                 case SNDRV_PCM_TRIGGER_RESUME:
1666                         if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1667                                 continue;
1668
1669                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1670                         if (ret)
1671                                 return ret;
1672
1673                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1674                         break;
1675                 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1676                         if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1677                                 continue;
1678
1679                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1680                         if (ret)
1681                                 return ret;
1682
1683                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1684                         break;
1685                 case SNDRV_PCM_TRIGGER_STOP:
1686                         if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1687                                 continue;
1688
1689                         if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1690                                 continue;
1691
1692                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1693                         if (ret)
1694                                 return ret;
1695
1696                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1697                         break;
1698                 case SNDRV_PCM_TRIGGER_SUSPEND:
1699                         if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1700                                 continue;
1701
1702                         if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1703                                 continue;
1704
1705                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1706                         if (ret)
1707                                 return ret;
1708
1709                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1710                         break;
1711                 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1712                         if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1713                                 continue;
1714
1715                         if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1716                                 continue;
1717
1718                         ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1719                         if (ret)
1720                                 return ret;
1721
1722                         be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1723                         break;
1724                 }
1725         }
1726
1727         return ret;
1728 }
1729 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1730
1731 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
1732 {
1733         struct snd_soc_pcm_runtime *fe = substream->private_data;
1734         int stream = substream->stream, ret;
1735         enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1736
1737         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1738
1739         switch (trigger) {
1740         case SND_SOC_DPCM_TRIGGER_PRE:
1741                 /* call trigger on the frontend before the backend. */
1742
1743                 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
1744                                 fe->dai_link->name, cmd);
1745
1746                 ret = soc_pcm_trigger(substream, cmd);
1747                 if (ret < 0) {
1748                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1749                         goto out;
1750                 }
1751
1752                 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1753                 break;
1754         case SND_SOC_DPCM_TRIGGER_POST:
1755                 /* call trigger on the frontend after the backend. */
1756
1757                 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1758                 if (ret < 0) {
1759                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1760                         goto out;
1761                 }
1762
1763                 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
1764                                 fe->dai_link->name, cmd);
1765
1766                 ret = soc_pcm_trigger(substream, cmd);
1767                 break;
1768         case SND_SOC_DPCM_TRIGGER_BESPOKE:
1769                 /* bespoke trigger() - handles both FE and BEs */
1770
1771                 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
1772                                 fe->dai_link->name, cmd);
1773
1774                 ret = soc_pcm_bespoke_trigger(substream, cmd);
1775                 if (ret < 0) {
1776                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1777                         goto out;
1778                 }
1779                 break;
1780         default:
1781                 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
1782                                 fe->dai_link->name);
1783                 ret = -EINVAL;
1784                 goto out;
1785         }
1786
1787         switch (cmd) {
1788         case SNDRV_PCM_TRIGGER_START:
1789         case SNDRV_PCM_TRIGGER_RESUME:
1790         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1791                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1792                 break;
1793         case SNDRV_PCM_TRIGGER_STOP:
1794         case SNDRV_PCM_TRIGGER_SUSPEND:
1795         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1796                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1797                 break;
1798         }
1799
1800 out:
1801         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1802         return ret;
1803 }
1804
1805 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
1806 {
1807         struct snd_soc_pcm_runtime *fe = substream->private_data;
1808         int stream = substream->stream;
1809
1810         /* if FE's runtime_update is already set, we're in race;
1811          * process this trigger later at exit
1812          */
1813         if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
1814                 fe->dpcm[stream].trigger_pending = cmd + 1;
1815                 return 0; /* delayed, assuming it's successful */
1816         }
1817
1818         /* we're alone, let's trigger */
1819         return dpcm_fe_dai_do_trigger(substream, cmd);
1820 }
1821
1822 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1823 {
1824         struct snd_soc_dpcm *dpcm;
1825         int ret = 0;
1826
1827         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1828
1829                 struct snd_soc_pcm_runtime *be = dpcm->be;
1830                 struct snd_pcm_substream *be_substream =
1831                         snd_soc_dpcm_get_substream(be, stream);
1832
1833                 /* is this op for this BE ? */
1834                 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1835                         continue;
1836
1837                 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1838                     (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1839                         continue;
1840
1841                 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
1842                         dpcm->fe->dai_link->name);
1843
1844                 ret = soc_pcm_prepare(be_substream);
1845                 if (ret < 0) {
1846                         dev_err(be->dev, "ASoC: backend prepare failed %d\n",
1847                                 ret);
1848                         break;
1849                 }
1850
1851                 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1852         }
1853         return ret;
1854 }
1855
1856 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
1857 {
1858         struct snd_soc_pcm_runtime *fe = substream->private_data;
1859         int stream = substream->stream, ret = 0;
1860
1861         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1862
1863         dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
1864
1865         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1866
1867         /* there is no point preparing this FE if there are no BEs */
1868         if (list_empty(&fe->dpcm[stream].be_clients)) {
1869                 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
1870                                 fe->dai_link->name);
1871                 ret = -EINVAL;
1872                 goto out;
1873         }
1874
1875         ret = dpcm_be_dai_prepare(fe, substream->stream);
1876         if (ret < 0)
1877                 goto out;
1878
1879         /* call prepare on the frontend */
1880         ret = soc_pcm_prepare(substream);
1881         if (ret < 0) {
1882                 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
1883                         fe->dai_link->name);
1884                 goto out;
1885         }
1886
1887         /* run the stream event for each BE */
1888         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1889         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1890
1891 out:
1892         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1893         mutex_unlock(&fe->card->mutex);
1894
1895         return ret;
1896 }
1897
1898 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1899                      unsigned int cmd, void *arg)
1900 {
1901         struct snd_soc_pcm_runtime *rtd = substream->private_data;
1902         struct snd_soc_platform *platform = rtd->platform;
1903
1904         if (platform->driver->ops && platform->driver->ops->ioctl)
1905                 return platform->driver->ops->ioctl(substream, cmd, arg);
1906         return snd_pcm_lib_ioctl(substream, cmd, arg);
1907 }
1908
1909 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1910 {
1911         struct snd_pcm_substream *substream =
1912                 snd_soc_dpcm_get_substream(fe, stream);
1913         enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1914         int err;
1915
1916         dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
1917                         stream ? "capture" : "playback", fe->dai_link->name);
1918
1919         if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1920                 /* call bespoke trigger - FE takes care of all BE triggers */
1921                 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
1922                                 fe->dai_link->name);
1923
1924                 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1925                 if (err < 0)
1926                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1927         } else {
1928                 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
1929                         fe->dai_link->name);
1930
1931                 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1932                 if (err < 0)
1933                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1934         }
1935
1936         err = dpcm_be_dai_hw_free(fe, stream);
1937         if (err < 0)
1938                 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
1939
1940         err = dpcm_be_dai_shutdown(fe, stream);
1941         if (err < 0)
1942                 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
1943
1944         /* run the stream event for each BE */
1945         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1946
1947         return 0;
1948 }
1949
1950 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1951 {
1952         struct snd_pcm_substream *substream =
1953                 snd_soc_dpcm_get_substream(fe, stream);
1954         struct snd_soc_dpcm *dpcm;
1955         enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1956         int ret;
1957
1958         dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
1959                         stream ? "capture" : "playback", fe->dai_link->name);
1960
1961         /* Only start the BE if the FE is ready */
1962         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1963                 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1964                 return -EINVAL;
1965
1966         /* startup must always be called for new BEs */
1967         ret = dpcm_be_dai_startup(fe, stream);
1968         if (ret < 0)
1969                 goto disconnect;
1970
1971         /* keep going if FE state is > open */
1972         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1973                 return 0;
1974
1975         ret = dpcm_be_dai_hw_params(fe, stream);
1976         if (ret < 0)
1977                 goto close;
1978
1979         /* keep going if FE state is > hw_params */
1980         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1981                 return 0;
1982
1983
1984         ret = dpcm_be_dai_prepare(fe, stream);
1985         if (ret < 0)
1986                 goto hw_free;
1987
1988         /* run the stream event for each BE */
1989         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1990
1991         /* keep going if FE state is > prepare */
1992         if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1993                 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1994                 return 0;
1995
1996         if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1997                 /* call trigger on the frontend - FE takes care of all BE triggers */
1998                 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
1999                                 fe->dai_link->name);
2000
2001                 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2002                 if (ret < 0) {
2003                         dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2004                         goto hw_free;
2005                 }
2006         } else {
2007                 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2008                         fe->dai_link->name);
2009
2010                 ret = dpcm_be_dai_trigger(fe, stream,
2011                                         SNDRV_PCM_TRIGGER_START);
2012                 if (ret < 0) {
2013                         dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2014                         goto hw_free;
2015                 }
2016         }
2017
2018         return 0;
2019
2020 hw_free:
2021         dpcm_be_dai_hw_free(fe, stream);
2022 close:
2023         dpcm_be_dai_shutdown(fe, stream);
2024 disconnect:
2025         /* disconnect any non started BEs */
2026         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2027                 struct snd_soc_pcm_runtime *be = dpcm->be;
2028                 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2029                                 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2030         }
2031
2032         return ret;
2033 }
2034
2035 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2036 {
2037         int ret;
2038
2039         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2040         ret = dpcm_run_update_startup(fe, stream);
2041         if (ret < 0)
2042                 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2043         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2044
2045         return ret;
2046 }
2047
2048 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2049 {
2050         int ret;
2051
2052         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2053         ret = dpcm_run_update_shutdown(fe, stream);
2054         if (ret < 0)
2055                 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2056         dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2057
2058         return ret;
2059 }
2060
2061 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2062  * any DAI links.
2063  */
2064 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2065 {
2066         int i, old, new, paths;
2067
2068         mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2069         for (i = 0; i < card->num_rtd; i++) {
2070                 struct snd_soc_dapm_widget_list *list;
2071                 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2072
2073                 /* make sure link is FE */
2074                 if (!fe->dai_link->dynamic)
2075                         continue;
2076
2077                 /* only check active links */
2078                 if (!fe->cpu_dai->active)
2079                         continue;
2080
2081                 /* DAPM sync will call this to update DSP paths */
2082                 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
2083                         fe->dai_link->name);
2084
2085                 /* skip if FE doesn't have playback capability */
2086                 if (!fe->cpu_dai->driver->playback.channels_min)
2087                         goto capture;
2088
2089                 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2090                 if (paths < 0) {
2091                         dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2092                                         fe->dai_link->name,  "playback");
2093                         mutex_unlock(&card->mutex);
2094                         return paths;
2095                 }
2096
2097                 /* update any new playback paths */
2098                 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2099                 if (new) {
2100                         dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2101                         dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2102                         dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2103                 }
2104
2105                 /* update any old playback paths */
2106                 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2107                 if (old) {
2108                         dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2109                         dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2110                         dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2111                 }
2112
2113                 dpcm_path_put(&list);
2114 capture:
2115                 /* skip if FE doesn't have capture capability */
2116                 if (!fe->cpu_dai->driver->capture.channels_min)
2117                         continue;
2118
2119                 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2120                 if (paths < 0) {
2121                         dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2122                                         fe->dai_link->name,  "capture");
2123                         mutex_unlock(&card->mutex);
2124                         return paths;
2125                 }
2126
2127                 /* update any new capture paths */
2128                 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2129                 if (new) {
2130                         dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2131                         dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2132                         dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2133                 }
2134
2135                 /* update any old capture paths */
2136                 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2137                 if (old) {
2138                         dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2139                         dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2140                         dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2141                 }
2142
2143                 dpcm_path_put(&list);
2144         }
2145
2146         mutex_unlock(&card->mutex);
2147         return 0;
2148 }
2149 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2150 {
2151         struct snd_soc_dpcm *dpcm;
2152         struct list_head *clients =
2153                 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2154
2155         list_for_each_entry(dpcm, clients, list_be) {
2156
2157                 struct snd_soc_pcm_runtime *be = dpcm->be;
2158                 struct snd_soc_dai *dai = be->codec_dai;
2159                 struct snd_soc_dai_driver *drv = dai->driver;
2160
2161                 if (be->dai_link->ignore_suspend)
2162                         continue;
2163
2164                 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
2165
2166                 if (drv->ops && drv->ops->digital_mute && dai->playback_active)
2167                         drv->ops->digital_mute(dai, mute);
2168         }
2169
2170         return 0;
2171 }
2172
2173 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2174 {
2175         struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2176         struct snd_soc_dpcm *dpcm;
2177         struct snd_soc_dapm_widget_list *list;
2178         int ret;
2179         int stream = fe_substream->stream;
2180
2181         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2182         fe->dpcm[stream].runtime = fe_substream->runtime;
2183
2184         if (dpcm_path_get(fe, stream, &list) <= 0) {
2185                 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2186                         fe->dai_link->name, stream ? "capture" : "playback");
2187         }
2188
2189         /* calculate valid and active FE <-> BE dpcms */
2190         dpcm_process_paths(fe, stream, &list, 1);
2191
2192         ret = dpcm_fe_dai_startup(fe_substream);
2193         if (ret < 0) {
2194                 /* clean up all links */
2195                 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2196                         dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2197
2198                 dpcm_be_disconnect(fe, stream);
2199                 fe->dpcm[stream].runtime = NULL;
2200         }
2201
2202         dpcm_clear_pending_state(fe, stream);
2203         dpcm_path_put(&list);
2204         mutex_unlock(&fe->card->mutex);
2205         return ret;
2206 }
2207
2208 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2209 {
2210         struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2211         struct snd_soc_dpcm *dpcm;
2212         int stream = fe_substream->stream, ret;
2213
2214         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2215         ret = dpcm_fe_dai_shutdown(fe_substream);
2216
2217         /* mark FE's links ready to prune */
2218         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2219                 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2220
2221         dpcm_be_disconnect(fe, stream);
2222
2223         fe->dpcm[stream].runtime = NULL;
2224         mutex_unlock(&fe->card->mutex);
2225         return ret;
2226 }
2227
2228 /* create a new pcm */
2229 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2230 {
2231         struct snd_soc_platform *platform = rtd->platform;
2232         struct snd_soc_dai *codec_dai = rtd->codec_dai;
2233         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2234         struct snd_pcm *pcm;
2235         char new_name[64];
2236         int ret = 0, playback = 0, capture = 0;
2237
2238         if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2239                 playback = rtd->dai_link->dpcm_playback;
2240                 capture = rtd->dai_link->dpcm_capture;
2241         } else {
2242                 if (codec_dai->driver->playback.channels_min &&
2243                     cpu_dai->driver->playback.channels_min)
2244                         playback = 1;
2245                 if (codec_dai->driver->capture.channels_min &&
2246                     cpu_dai->driver->capture.channels_min)
2247                         capture = 1;
2248         }
2249
2250         if (rtd->dai_link->playback_only) {
2251                 playback = 1;
2252                 capture = 0;
2253         }
2254
2255         if (rtd->dai_link->capture_only) {
2256                 playback = 0;
2257                 capture = 1;
2258         }
2259
2260         /* create the PCM */
2261         if (rtd->dai_link->no_pcm) {
2262                 snprintf(new_name, sizeof(new_name), "(%s)",
2263                         rtd->dai_link->stream_name);
2264
2265                 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2266                                 playback, capture, &pcm);
2267         } else {
2268                 if (rtd->dai_link->dynamic)
2269                         snprintf(new_name, sizeof(new_name), "%s (*)",
2270                                 rtd->dai_link->stream_name);
2271                 else
2272                         snprintf(new_name, sizeof(new_name), "%s %s-%d",
2273                                 rtd->dai_link->stream_name, codec_dai->name, num);
2274
2275                 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2276                         capture, &pcm);
2277         }
2278         if (ret < 0) {
2279                 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2280                         rtd->dai_link->name);
2281                 return ret;
2282         }
2283         dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2284
2285         /* DAPM dai link stream work */
2286         INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2287
2288         rtd->pcm = pcm;
2289         pcm->private_data = rtd;
2290
2291         if (rtd->dai_link->no_pcm) {
2292                 if (playback)
2293                         pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2294                 if (capture)
2295                         pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2296                 goto out;
2297         }
2298
2299         /* ASoC PCM operations */
2300         if (rtd->dai_link->dynamic) {
2301                 rtd->ops.open           = dpcm_fe_dai_open;
2302                 rtd->ops.hw_params      = dpcm_fe_dai_hw_params;
2303                 rtd->ops.prepare        = dpcm_fe_dai_prepare;
2304                 rtd->ops.trigger        = dpcm_fe_dai_trigger;
2305                 rtd->ops.hw_free        = dpcm_fe_dai_hw_free;
2306                 rtd->ops.close          = dpcm_fe_dai_close;
2307                 rtd->ops.pointer        = soc_pcm_pointer;
2308                 rtd->ops.ioctl          = soc_pcm_ioctl;
2309         } else {
2310                 rtd->ops.open           = soc_pcm_open;
2311                 rtd->ops.hw_params      = soc_pcm_hw_params;
2312                 rtd->ops.prepare        = soc_pcm_prepare;
2313                 rtd->ops.trigger        = soc_pcm_trigger;
2314                 rtd->ops.hw_free        = soc_pcm_hw_free;
2315                 rtd->ops.close          = soc_pcm_close;
2316                 rtd->ops.pointer        = soc_pcm_pointer;
2317                 rtd->ops.ioctl          = soc_pcm_ioctl;
2318         }
2319
2320         if (platform->driver->ops) {
2321                 rtd->ops.ack            = platform->driver->ops->ack;
2322                 rtd->ops.copy           = platform->driver->ops->copy;
2323                 rtd->ops.silence        = platform->driver->ops->silence;
2324                 rtd->ops.page           = platform->driver->ops->page;
2325                 rtd->ops.mmap           = platform->driver->ops->mmap;
2326         }
2327
2328         if (playback)
2329                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2330
2331         if (capture)
2332                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2333
2334         if (platform->driver->pcm_new) {
2335                 ret = platform->driver->pcm_new(rtd);
2336                 if (ret < 0) {
2337                         dev_err(platform->dev,
2338                                 "ASoC: pcm constructor failed: %d\n",
2339                                 ret);
2340                         return ret;
2341                 }
2342         }
2343
2344         pcm->private_free = platform->driver->pcm_free;
2345 out:
2346         dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name,
2347                 cpu_dai->name);
2348         return ret;
2349 }
2350
2351 /* is the current PCM operation for this FE ? */
2352 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2353 {
2354         if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2355                 return 1;
2356         return 0;
2357 }
2358 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2359
2360 /* is the current PCM operation for this BE ? */
2361 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2362                 struct snd_soc_pcm_runtime *be, int stream)
2363 {
2364         if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2365            ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2366                   be->dpcm[stream].runtime_update))
2367                 return 1;
2368         return 0;
2369 }
2370 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2371
2372 /* get the substream for this BE */
2373 struct snd_pcm_substream *
2374         snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2375 {
2376         return be->pcm->streams[stream].substream;
2377 }
2378 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2379
2380 /* get the BE runtime state */
2381 enum snd_soc_dpcm_state
2382         snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2383 {
2384         return be->dpcm[stream].state;
2385 }
2386 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2387
2388 /* set the BE runtime state */
2389 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2390                 int stream, enum snd_soc_dpcm_state state)
2391 {
2392         be->dpcm[stream].state = state;
2393 }
2394 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2395
2396 /*
2397  * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2398  * are not running, paused or suspended for the specified stream direction.
2399  */
2400 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2401                 struct snd_soc_pcm_runtime *be, int stream)
2402 {
2403         struct snd_soc_dpcm *dpcm;
2404         int state;
2405
2406         list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2407
2408                 if (dpcm->fe == fe)
2409                         continue;
2410
2411                 state = dpcm->fe->dpcm[stream].state;
2412                 if (state == SND_SOC_DPCM_STATE_START ||
2413                         state == SND_SOC_DPCM_STATE_PAUSED ||
2414                         state == SND_SOC_DPCM_STATE_SUSPEND)
2415                         return 0;
2416         }
2417
2418         /* it's safe to free/stop this BE DAI */
2419         return 1;
2420 }
2421 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2422
2423 /*
2424  * We can only change hw params a BE DAI if any of it's FE are not prepared,
2425  * running, paused or suspended for the specified stream direction.
2426  */
2427 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2428                 struct snd_soc_pcm_runtime *be, int stream)
2429 {
2430         struct snd_soc_dpcm *dpcm;
2431         int state;
2432
2433         list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2434
2435                 if (dpcm->fe == fe)
2436                         continue;
2437
2438                 state = dpcm->fe->dpcm[stream].state;
2439                 if (state == SND_SOC_DPCM_STATE_START ||
2440                         state == SND_SOC_DPCM_STATE_PAUSED ||
2441                         state == SND_SOC_DPCM_STATE_SUSPEND ||
2442                         state == SND_SOC_DPCM_STATE_PREPARE)
2443                         return 0;
2444         }
2445
2446         /* it's safe to change hw_params */
2447         return 1;
2448 }
2449 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2450
2451 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2452                 int cmd, struct snd_soc_platform *platform)
2453 {
2454         if (platform->driver->ops && platform->driver->ops->trigger)
2455                 return platform->driver->ops->trigger(substream, cmd);
2456         return 0;
2457 }
2458 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2459
2460 #ifdef CONFIG_DEBUG_FS
2461 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2462 {
2463         switch (state) {
2464         case SND_SOC_DPCM_STATE_NEW:
2465                 return "new";
2466         case SND_SOC_DPCM_STATE_OPEN:
2467                 return "open";
2468         case SND_SOC_DPCM_STATE_HW_PARAMS:
2469                 return "hw_params";
2470         case SND_SOC_DPCM_STATE_PREPARE:
2471                 return "prepare";
2472         case SND_SOC_DPCM_STATE_START:
2473                 return "start";
2474         case SND_SOC_DPCM_STATE_STOP:
2475                 return "stop";
2476         case SND_SOC_DPCM_STATE_SUSPEND:
2477                 return "suspend";
2478         case SND_SOC_DPCM_STATE_PAUSED:
2479                 return "paused";
2480         case SND_SOC_DPCM_STATE_HW_FREE:
2481                 return "hw_free";
2482         case SND_SOC_DPCM_STATE_CLOSE:
2483                 return "close";
2484         }
2485
2486         return "unknown";
2487 }
2488
2489 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2490                                 int stream, char *buf, size_t size)
2491 {
2492         struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2493         struct snd_soc_dpcm *dpcm;
2494         ssize_t offset = 0;
2495
2496         /* FE state */
2497         offset += snprintf(buf + offset, size - offset,
2498                         "[%s - %s]\n", fe->dai_link->name,
2499                         stream ? "Capture" : "Playback");
2500
2501         offset += snprintf(buf + offset, size - offset, "State: %s\n",
2502                         dpcm_state_string(fe->dpcm[stream].state));
2503
2504         if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2505             (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2506                 offset += snprintf(buf + offset, size - offset,
2507                                 "Hardware Params: "
2508                                 "Format = %s, Channels = %d, Rate = %d\n",
2509                                 snd_pcm_format_name(params_format(params)),
2510                                 params_channels(params),
2511                                 params_rate(params));
2512
2513         /* BEs state */
2514         offset += snprintf(buf + offset, size - offset, "Backends:\n");
2515
2516         if (list_empty(&fe->dpcm[stream].be_clients)) {
2517                 offset += snprintf(buf + offset, size - offset,
2518                                 " No active DSP links\n");
2519                 goto out;
2520         }
2521
2522         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2523                 struct snd_soc_pcm_runtime *be = dpcm->be;
2524                 params = &dpcm->hw_params;
2525
2526                 offset += snprintf(buf + offset, size - offset,
2527                                 "- %s\n", be->dai_link->name);
2528
2529                 offset += snprintf(buf + offset, size - offset,
2530                                 "   State: %s\n",
2531                                 dpcm_state_string(be->dpcm[stream].state));
2532
2533                 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2534                     (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2535                         offset += snprintf(buf + offset, size - offset,
2536                                 "   Hardware Params: "
2537                                 "Format = %s, Channels = %d, Rate = %d\n",
2538                                 snd_pcm_format_name(params_format(params)),
2539                                 params_channels(params),
2540                                 params_rate(params));
2541         }
2542
2543 out:
2544         return offset;
2545 }
2546
2547 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2548                                 size_t count, loff_t *ppos)
2549 {
2550         struct snd_soc_pcm_runtime *fe = file->private_data;
2551         ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2552         char *buf;
2553
2554         buf = kmalloc(out_count, GFP_KERNEL);
2555         if (!buf)
2556                 return -ENOMEM;
2557
2558         if (fe->cpu_dai->driver->playback.channels_min)
2559                 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2560                                         buf + offset, out_count - offset);
2561
2562         if (fe->cpu_dai->driver->capture.channels_min)
2563                 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2564                                         buf + offset, out_count - offset);
2565
2566         ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2567
2568         kfree(buf);
2569         return ret;
2570 }
2571
2572 static const struct file_operations dpcm_state_fops = {
2573         .open = simple_open,
2574         .read = dpcm_state_read_file,
2575         .llseek = default_llseek,
2576 };
2577
2578 int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2579 {
2580         if (!rtd->dai_link)
2581                 return 0;
2582
2583         rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2584                         rtd->card->debugfs_card_root);
2585         if (!rtd->debugfs_dpcm_root) {
2586                 dev_dbg(rtd->dev,
2587                          "ASoC: Failed to create dpcm debugfs directory %s\n",
2588                          rtd->dai_link->name);
2589                 return -EINVAL;
2590         }
2591
2592         rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2593                                                 rtd->debugfs_dpcm_root,
2594                                                 rtd, &dpcm_state_fops);
2595
2596         return 0;
2597 }
2598 #endif