~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/soc/soc-pcm.c

Version: ~ [ linux-5.4-rc3 ] ~ [ linux-5.3.6 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.79 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.149 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.196 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.196 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.75 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp