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