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