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