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