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