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