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