Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * soc-pcm.c -- ALSA SoC PCM |
| 3 | * |
| 4 | * Copyright 2005 Wolfson Microelectronics PLC. |
| 5 | * Copyright 2005 Openedhand Ltd. |
| 6 | * Copyright (C) 2010 Slimlogic Ltd. |
| 7 | * Copyright (C) 2010 Texas Instruments Inc. |
| 8 | * |
| 9 | * Authors: Liam Girdwood <lrg@ti.com> |
| 10 | * Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify it |
| 13 | * under the terms of the GNU General Public License as published by the |
| 14 | * Free Software Foundation; either version 2 of the License, or (at your |
| 15 | * option) any later version. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/delay.h> |
Mark Brown | d6652ef | 2011-12-03 20:14:31 +0000 | [diff] [blame] | 22 | #include <linux/pm_runtime.h> |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 23 | #include <linux/slab.h> |
| 24 | #include <linux/workqueue.h> |
| 25 | #include <sound/core.h> |
| 26 | #include <sound/pcm.h> |
| 27 | #include <sound/pcm_params.h> |
| 28 | #include <sound/soc.h> |
| 29 | #include <sound/initval.h> |
| 30 | |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 31 | static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream, |
| 32 | struct snd_soc_dai *soc_dai) |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 33 | { |
| 34 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 35 | int ret; |
| 36 | |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 37 | if (!soc_dai->driver->symmetric_rates && |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 38 | !rtd->dai_link->symmetric_rates) |
| 39 | return 0; |
| 40 | |
| 41 | /* This can happen if multiple streams are starting simultaneously - |
| 42 | * the second can need to get its constraints before the first has |
| 43 | * picked a rate. Complain and allow the application to carry on. |
| 44 | */ |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 45 | if (!soc_dai->rate) { |
| 46 | dev_warn(soc_dai->dev, |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 47 | "Not enforcing symmetric_rates due to race\n"); |
| 48 | return 0; |
| 49 | } |
| 50 | |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 51 | dev_dbg(soc_dai->dev, "Symmetry forces %dHz rate\n", soc_dai->rate); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 52 | |
| 53 | ret = snd_pcm_hw_constraint_minmax(substream->runtime, |
| 54 | SNDRV_PCM_HW_PARAM_RATE, |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 55 | soc_dai->rate, soc_dai->rate); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 56 | if (ret < 0) { |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 57 | dev_err(soc_dai->dev, |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 58 | "Unable to apply rate symmetry constraint: %d\n", ret); |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | /* |
Mark Brown | 58ba9b2 | 2012-01-16 18:38:51 +0000 | [diff] [blame^] | 66 | * List of sample sizes that might go over the bus for parameter |
| 67 | * application. There ought to be a wildcard sample size for things |
| 68 | * like the DAC/ADC resolution to use but there isn't right now. |
| 69 | */ |
| 70 | static int sample_sizes[] = { |
| 71 | 8, 16, 24, 32, |
| 72 | }; |
| 73 | |
| 74 | static void soc_pcm_apply_msb(struct snd_pcm_substream *substream, |
| 75 | struct snd_soc_dai *dai) |
| 76 | { |
| 77 | int ret, i, bits; |
| 78 | |
| 79 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 80 | bits = dai->driver->playback.sig_bits; |
| 81 | else |
| 82 | bits = dai->driver->capture.sig_bits; |
| 83 | |
| 84 | if (!bits) |
| 85 | return; |
| 86 | |
| 87 | for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) { |
| 88 | ret = snd_pcm_hw_constraint_msbits(substream->runtime, |
| 89 | 0, sample_sizes[i], |
| 90 | bits); |
| 91 | if (ret != 0) |
| 92 | dev_warn(dai->dev, |
| 93 | "Failed to set MSB %d/%d: %d\n", |
| 94 | bits, sample_sizes[i], ret); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /* |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 99 | * Called by ALSA when a PCM substream is opened, the runtime->hw record is |
| 100 | * then initialized and any private data can be allocated. This also calls |
| 101 | * startup for the cpu DAI, platform, machine and codec DAI. |
| 102 | */ |
| 103 | static int soc_pcm_open(struct snd_pcm_substream *substream) |
| 104 | { |
| 105 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 106 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 107 | struct snd_soc_platform *platform = rtd->platform; |
| 108 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 109 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 110 | struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver; |
| 111 | struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver; |
| 112 | int ret = 0; |
| 113 | |
Mark Brown | d6652ef | 2011-12-03 20:14:31 +0000 | [diff] [blame] | 114 | pm_runtime_get_sync(cpu_dai->dev); |
| 115 | pm_runtime_get_sync(codec_dai->dev); |
| 116 | pm_runtime_get_sync(platform->dev); |
| 117 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 118 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 119 | |
| 120 | /* startup the audio subsystem */ |
| 121 | if (cpu_dai->driver->ops->startup) { |
| 122 | ret = cpu_dai->driver->ops->startup(substream, cpu_dai); |
| 123 | if (ret < 0) { |
| 124 | printk(KERN_ERR "asoc: can't open interface %s\n", |
| 125 | cpu_dai->name); |
| 126 | goto out; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (platform->driver->ops && platform->driver->ops->open) { |
| 131 | ret = platform->driver->ops->open(substream); |
| 132 | if (ret < 0) { |
| 133 | printk(KERN_ERR "asoc: can't open platform %s\n", platform->name); |
| 134 | goto platform_err; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if (codec_dai->driver->ops->startup) { |
| 139 | ret = codec_dai->driver->ops->startup(substream, codec_dai); |
| 140 | if (ret < 0) { |
| 141 | printk(KERN_ERR "asoc: can't open codec %s\n", |
| 142 | codec_dai->name); |
| 143 | goto codec_dai_err; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if (rtd->dai_link->ops && rtd->dai_link->ops->startup) { |
| 148 | ret = rtd->dai_link->ops->startup(substream); |
| 149 | if (ret < 0) { |
| 150 | printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name); |
| 151 | goto machine_err; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /* Check that the codec and cpu DAIs are compatible */ |
| 156 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 157 | runtime->hw.rate_min = |
| 158 | max(codec_dai_drv->playback.rate_min, |
| 159 | cpu_dai_drv->playback.rate_min); |
| 160 | runtime->hw.rate_max = |
| 161 | min(codec_dai_drv->playback.rate_max, |
| 162 | cpu_dai_drv->playback.rate_max); |
| 163 | runtime->hw.channels_min = |
| 164 | max(codec_dai_drv->playback.channels_min, |
| 165 | cpu_dai_drv->playback.channels_min); |
| 166 | runtime->hw.channels_max = |
| 167 | min(codec_dai_drv->playback.channels_max, |
| 168 | cpu_dai_drv->playback.channels_max); |
| 169 | runtime->hw.formats = |
| 170 | codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats; |
| 171 | runtime->hw.rates = |
| 172 | codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates; |
| 173 | if (codec_dai_drv->playback.rates |
| 174 | & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) |
| 175 | runtime->hw.rates |= cpu_dai_drv->playback.rates; |
| 176 | if (cpu_dai_drv->playback.rates |
| 177 | & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) |
| 178 | runtime->hw.rates |= codec_dai_drv->playback.rates; |
| 179 | } else { |
| 180 | runtime->hw.rate_min = |
| 181 | max(codec_dai_drv->capture.rate_min, |
| 182 | cpu_dai_drv->capture.rate_min); |
| 183 | runtime->hw.rate_max = |
| 184 | min(codec_dai_drv->capture.rate_max, |
| 185 | cpu_dai_drv->capture.rate_max); |
| 186 | runtime->hw.channels_min = |
| 187 | max(codec_dai_drv->capture.channels_min, |
| 188 | cpu_dai_drv->capture.channels_min); |
| 189 | runtime->hw.channels_max = |
| 190 | min(codec_dai_drv->capture.channels_max, |
| 191 | cpu_dai_drv->capture.channels_max); |
| 192 | runtime->hw.formats = |
| 193 | codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats; |
| 194 | runtime->hw.rates = |
| 195 | codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates; |
| 196 | if (codec_dai_drv->capture.rates |
| 197 | & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) |
| 198 | runtime->hw.rates |= cpu_dai_drv->capture.rates; |
| 199 | if (cpu_dai_drv->capture.rates |
| 200 | & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) |
| 201 | runtime->hw.rates |= codec_dai_drv->capture.rates; |
| 202 | } |
| 203 | |
| 204 | ret = -EINVAL; |
| 205 | snd_pcm_limit_hw_rates(runtime); |
| 206 | if (!runtime->hw.rates) { |
| 207 | printk(KERN_ERR "asoc: %s <-> %s No matching rates\n", |
| 208 | codec_dai->name, cpu_dai->name); |
| 209 | goto config_err; |
| 210 | } |
| 211 | if (!runtime->hw.formats) { |
| 212 | printk(KERN_ERR "asoc: %s <-> %s No matching formats\n", |
| 213 | codec_dai->name, cpu_dai->name); |
| 214 | goto config_err; |
| 215 | } |
| 216 | if (!runtime->hw.channels_min || !runtime->hw.channels_max || |
| 217 | runtime->hw.channels_min > runtime->hw.channels_max) { |
| 218 | printk(KERN_ERR "asoc: %s <-> %s No matching channels\n", |
| 219 | codec_dai->name, cpu_dai->name); |
| 220 | goto config_err; |
| 221 | } |
| 222 | |
Mark Brown | 58ba9b2 | 2012-01-16 18:38:51 +0000 | [diff] [blame^] | 223 | soc_pcm_apply_msb(substream, codec_dai); |
| 224 | soc_pcm_apply_msb(substream, cpu_dai); |
| 225 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 226 | /* Symmetry only applies if we've already got an active stream. */ |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 227 | if (cpu_dai->active) { |
| 228 | ret = soc_pcm_apply_symmetry(substream, cpu_dai); |
| 229 | if (ret != 0) |
| 230 | goto config_err; |
| 231 | } |
| 232 | |
| 233 | if (codec_dai->active) { |
| 234 | ret = soc_pcm_apply_symmetry(substream, codec_dai); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 235 | if (ret != 0) |
| 236 | goto config_err; |
| 237 | } |
| 238 | |
| 239 | pr_debug("asoc: %s <-> %s info:\n", |
| 240 | codec_dai->name, cpu_dai->name); |
| 241 | pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates); |
| 242 | pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min, |
| 243 | runtime->hw.channels_max); |
| 244 | pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min, |
| 245 | runtime->hw.rate_max); |
| 246 | |
| 247 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 248 | cpu_dai->playback_active++; |
| 249 | codec_dai->playback_active++; |
| 250 | } else { |
| 251 | cpu_dai->capture_active++; |
| 252 | codec_dai->capture_active++; |
| 253 | } |
| 254 | cpu_dai->active++; |
| 255 | codec_dai->active++; |
| 256 | rtd->codec->active++; |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 257 | mutex_unlock(&rtd->pcm_mutex); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 258 | return 0; |
| 259 | |
| 260 | config_err: |
| 261 | if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown) |
| 262 | rtd->dai_link->ops->shutdown(substream); |
| 263 | |
| 264 | machine_err: |
| 265 | if (codec_dai->driver->ops->shutdown) |
| 266 | codec_dai->driver->ops->shutdown(substream, codec_dai); |
| 267 | |
| 268 | codec_dai_err: |
| 269 | if (platform->driver->ops && platform->driver->ops->close) |
| 270 | platform->driver->ops->close(substream); |
| 271 | |
| 272 | platform_err: |
| 273 | if (cpu_dai->driver->ops->shutdown) |
| 274 | cpu_dai->driver->ops->shutdown(substream, cpu_dai); |
| 275 | out: |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 276 | mutex_unlock(&rtd->pcm_mutex); |
Mark Brown | d6652ef | 2011-12-03 20:14:31 +0000 | [diff] [blame] | 277 | |
| 278 | pm_runtime_put(platform->dev); |
| 279 | pm_runtime_put(codec_dai->dev); |
| 280 | pm_runtime_put(cpu_dai->dev); |
| 281 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 282 | return ret; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * Power down the audio subsystem pmdown_time msecs after close is called. |
| 287 | * This is to ensure there are no pops or clicks in between any music tracks |
| 288 | * due to DAPM power cycling. |
| 289 | */ |
| 290 | static void close_delayed_work(struct work_struct *work) |
| 291 | { |
| 292 | struct snd_soc_pcm_runtime *rtd = |
| 293 | container_of(work, struct snd_soc_pcm_runtime, delayed_work.work); |
| 294 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 295 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 296 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 297 | |
| 298 | pr_debug("pop wq checking: %s status: %s waiting: %s\n", |
| 299 | codec_dai->driver->playback.stream_name, |
| 300 | codec_dai->playback_active ? "active" : "inactive", |
| 301 | codec_dai->pop_wait ? "yes" : "no"); |
| 302 | |
| 303 | /* are we waiting on this codec DAI stream */ |
| 304 | if (codec_dai->pop_wait == 1) { |
| 305 | codec_dai->pop_wait = 0; |
| 306 | snd_soc_dapm_stream_event(rtd, |
| 307 | codec_dai->driver->playback.stream_name, |
| 308 | SND_SOC_DAPM_STREAM_STOP); |
| 309 | } |
| 310 | |
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 | } |
| 313 | |
| 314 | /* |
| 315 | * Called by ALSA when a PCM substream is closed. Private data can be |
| 316 | * freed here. The cpu DAI, codec DAI, machine and platform are also |
| 317 | * shutdown. |
| 318 | */ |
Liam Girdwood | 91d5e6b | 2011-06-09 17:04:59 +0100 | [diff] [blame] | 319 | static int soc_pcm_close(struct snd_pcm_substream *substream) |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 320 | { |
| 321 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 322 | struct snd_soc_platform *platform = rtd->platform; |
| 323 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 324 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 325 | struct snd_soc_codec *codec = rtd->codec; |
| 326 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 327 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 328 | |
| 329 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 330 | cpu_dai->playback_active--; |
| 331 | codec_dai->playback_active--; |
| 332 | } else { |
| 333 | cpu_dai->capture_active--; |
| 334 | codec_dai->capture_active--; |
| 335 | } |
| 336 | |
| 337 | cpu_dai->active--; |
| 338 | codec_dai->active--; |
| 339 | codec->active--; |
| 340 | |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 341 | /* clear the corresponding DAIs rate when inactive */ |
| 342 | if (!cpu_dai->active) |
| 343 | cpu_dai->rate = 0; |
| 344 | |
| 345 | if (!codec_dai->active) |
| 346 | codec_dai->rate = 0; |
Sascha Hauer | 25b7679 | 2011-08-17 09:20:01 +0200 | [diff] [blame] | 347 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 348 | /* Muting the DAC suppresses artifacts caused during digital |
| 349 | * shutdown, for example from stopping clocks. |
| 350 | */ |
| 351 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 352 | snd_soc_dai_digital_mute(codec_dai, 1); |
| 353 | |
| 354 | if (cpu_dai->driver->ops->shutdown) |
| 355 | cpu_dai->driver->ops->shutdown(substream, cpu_dai); |
| 356 | |
| 357 | if (codec_dai->driver->ops->shutdown) |
| 358 | codec_dai->driver->ops->shutdown(substream, codec_dai); |
| 359 | |
| 360 | if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown) |
| 361 | rtd->dai_link->ops->shutdown(substream); |
| 362 | |
| 363 | if (platform->driver->ops && platform->driver->ops->close) |
| 364 | platform->driver->ops->close(substream); |
| 365 | cpu_dai->runtime = NULL; |
| 366 | |
| 367 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
Mark Brown | 5b6247a | 2011-10-27 12:11:26 +0200 | [diff] [blame] | 368 | if (codec->ignore_pmdown_time || |
| 369 | rtd->dai_link->ignore_pmdown_time) { |
Peter Ujfalusi | 1d69c5c | 2011-10-14 14:43:33 +0300 | [diff] [blame] | 370 | /* powered down playback stream now */ |
| 371 | snd_soc_dapm_stream_event(rtd, |
| 372 | codec_dai->driver->playback.stream_name, |
| 373 | SND_SOC_DAPM_STREAM_STOP); |
| 374 | } else { |
| 375 | /* start delayed pop wq here for playback streams */ |
| 376 | codec_dai->pop_wait = 1; |
| 377 | schedule_delayed_work(&rtd->delayed_work, |
| 378 | msecs_to_jiffies(rtd->pmdown_time)); |
| 379 | } |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 380 | } else { |
| 381 | /* capture streams can be powered down now */ |
| 382 | snd_soc_dapm_stream_event(rtd, |
| 383 | codec_dai->driver->capture.stream_name, |
| 384 | SND_SOC_DAPM_STREAM_STOP); |
| 385 | } |
| 386 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 387 | mutex_unlock(&rtd->pcm_mutex); |
Mark Brown | d6652ef | 2011-12-03 20:14:31 +0000 | [diff] [blame] | 388 | |
| 389 | pm_runtime_put(platform->dev); |
| 390 | pm_runtime_put(codec_dai->dev); |
| 391 | pm_runtime_put(cpu_dai->dev); |
| 392 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | /* |
| 397 | * Called by ALSA when the PCM substream is prepared, can set format, sample |
| 398 | * rate, etc. This function is non atomic and can be called multiple times, |
| 399 | * it can refer to the runtime info. |
| 400 | */ |
| 401 | static int soc_pcm_prepare(struct snd_pcm_substream *substream) |
| 402 | { |
| 403 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 404 | struct snd_soc_platform *platform = rtd->platform; |
| 405 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 406 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 407 | int ret = 0; |
| 408 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 409 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 410 | |
| 411 | if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) { |
| 412 | ret = rtd->dai_link->ops->prepare(substream); |
| 413 | if (ret < 0) { |
| 414 | printk(KERN_ERR "asoc: machine prepare error\n"); |
| 415 | goto out; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | if (platform->driver->ops && platform->driver->ops->prepare) { |
| 420 | ret = platform->driver->ops->prepare(substream); |
| 421 | if (ret < 0) { |
| 422 | printk(KERN_ERR "asoc: platform prepare error\n"); |
| 423 | goto out; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | if (codec_dai->driver->ops->prepare) { |
| 428 | ret = codec_dai->driver->ops->prepare(substream, codec_dai); |
| 429 | if (ret < 0) { |
| 430 | printk(KERN_ERR "asoc: codec DAI prepare error\n"); |
| 431 | goto out; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | if (cpu_dai->driver->ops->prepare) { |
| 436 | ret = cpu_dai->driver->ops->prepare(substream, cpu_dai); |
| 437 | if (ret < 0) { |
| 438 | printk(KERN_ERR "asoc: cpu DAI prepare error\n"); |
| 439 | goto out; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | /* cancel any delayed stream shutdown that is pending */ |
| 444 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
| 445 | codec_dai->pop_wait) { |
| 446 | codec_dai->pop_wait = 0; |
| 447 | cancel_delayed_work(&rtd->delayed_work); |
| 448 | } |
| 449 | |
| 450 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 451 | snd_soc_dapm_stream_event(rtd, |
| 452 | codec_dai->driver->playback.stream_name, |
| 453 | SND_SOC_DAPM_STREAM_START); |
| 454 | else |
| 455 | snd_soc_dapm_stream_event(rtd, |
| 456 | codec_dai->driver->capture.stream_name, |
| 457 | SND_SOC_DAPM_STREAM_START); |
| 458 | |
| 459 | snd_soc_dai_digital_mute(codec_dai, 0); |
| 460 | |
| 461 | out: |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 462 | mutex_unlock(&rtd->pcm_mutex); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 463 | return ret; |
| 464 | } |
| 465 | |
| 466 | /* |
| 467 | * Called by ALSA when the hardware params are set by application. This |
| 468 | * function can also be called multiple times and can allocate buffers |
| 469 | * (using snd_pcm_lib_* ). It's non-atomic. |
| 470 | */ |
| 471 | static int soc_pcm_hw_params(struct snd_pcm_substream *substream, |
| 472 | struct snd_pcm_hw_params *params) |
| 473 | { |
| 474 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 475 | struct snd_soc_platform *platform = rtd->platform; |
| 476 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 477 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 478 | int ret = 0; |
| 479 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 480 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 481 | |
| 482 | if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) { |
| 483 | ret = rtd->dai_link->ops->hw_params(substream, params); |
| 484 | if (ret < 0) { |
| 485 | printk(KERN_ERR "asoc: machine hw_params failed\n"); |
| 486 | goto out; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | if (codec_dai->driver->ops->hw_params) { |
| 491 | ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai); |
| 492 | if (ret < 0) { |
| 493 | printk(KERN_ERR "asoc: can't set codec %s hw params\n", |
| 494 | codec_dai->name); |
| 495 | goto codec_err; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | if (cpu_dai->driver->ops->hw_params) { |
| 500 | ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai); |
| 501 | if (ret < 0) { |
| 502 | printk(KERN_ERR "asoc: interface %s hw params failed\n", |
| 503 | cpu_dai->name); |
| 504 | goto interface_err; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | if (platform->driver->ops && platform->driver->ops->hw_params) { |
| 509 | ret = platform->driver->ops->hw_params(substream, params); |
| 510 | if (ret < 0) { |
| 511 | printk(KERN_ERR "asoc: platform %s hw params failed\n", |
| 512 | platform->name); |
| 513 | goto platform_err; |
| 514 | } |
| 515 | } |
| 516 | |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 517 | /* store the rate for each DAIs */ |
| 518 | cpu_dai->rate = params_rate(params); |
| 519 | codec_dai->rate = params_rate(params); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 520 | |
| 521 | out: |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 522 | mutex_unlock(&rtd->pcm_mutex); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 523 | return ret; |
| 524 | |
| 525 | platform_err: |
| 526 | if (cpu_dai->driver->ops->hw_free) |
| 527 | cpu_dai->driver->ops->hw_free(substream, cpu_dai); |
| 528 | |
| 529 | interface_err: |
| 530 | if (codec_dai->driver->ops->hw_free) |
| 531 | codec_dai->driver->ops->hw_free(substream, codec_dai); |
| 532 | |
| 533 | codec_err: |
| 534 | if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free) |
| 535 | rtd->dai_link->ops->hw_free(substream); |
| 536 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 537 | mutex_unlock(&rtd->pcm_mutex); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 538 | return ret; |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * Frees resources allocated by hw_params, can be called multiple times |
| 543 | */ |
| 544 | static int soc_pcm_hw_free(struct snd_pcm_substream *substream) |
| 545 | { |
| 546 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 547 | struct snd_soc_platform *platform = rtd->platform; |
| 548 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 549 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 550 | struct snd_soc_codec *codec = rtd->codec; |
| 551 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 552 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 553 | |
| 554 | /* apply codec digital mute */ |
| 555 | if (!codec->active) |
| 556 | snd_soc_dai_digital_mute(codec_dai, 1); |
| 557 | |
| 558 | /* free any machine hw params */ |
| 559 | if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free) |
| 560 | rtd->dai_link->ops->hw_free(substream); |
| 561 | |
| 562 | /* free any DMA resources */ |
| 563 | if (platform->driver->ops && platform->driver->ops->hw_free) |
| 564 | platform->driver->ops->hw_free(substream); |
| 565 | |
| 566 | /* now free hw params for the DAIs */ |
| 567 | if (codec_dai->driver->ops->hw_free) |
| 568 | codec_dai->driver->ops->hw_free(substream, codec_dai); |
| 569 | |
| 570 | if (cpu_dai->driver->ops->hw_free) |
| 571 | cpu_dai->driver->ops->hw_free(substream, cpu_dai); |
| 572 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 573 | mutex_unlock(&rtd->pcm_mutex); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 578 | { |
| 579 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 580 | struct snd_soc_platform *platform = rtd->platform; |
| 581 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 582 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 583 | int ret; |
| 584 | |
| 585 | if (codec_dai->driver->ops->trigger) { |
| 586 | ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai); |
| 587 | if (ret < 0) |
| 588 | return ret; |
| 589 | } |
| 590 | |
| 591 | if (platform->driver->ops && platform->driver->ops->trigger) { |
| 592 | ret = platform->driver->ops->trigger(substream, cmd); |
| 593 | if (ret < 0) |
| 594 | return ret; |
| 595 | } |
| 596 | |
| 597 | if (cpu_dai->driver->ops->trigger) { |
| 598 | ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai); |
| 599 | if (ret < 0) |
| 600 | return ret; |
| 601 | } |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | * soc level wrapper for pointer callback |
| 607 | * If cpu_dai, codec_dai, platform driver has the delay callback, than |
| 608 | * the runtime->delay will be updated accordingly. |
| 609 | */ |
| 610 | static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream) |
| 611 | { |
| 612 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 613 | struct snd_soc_platform *platform = rtd->platform; |
| 614 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 615 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 616 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 617 | snd_pcm_uframes_t offset = 0; |
| 618 | snd_pcm_sframes_t delay = 0; |
| 619 | |
| 620 | if (platform->driver->ops && platform->driver->ops->pointer) |
| 621 | offset = platform->driver->ops->pointer(substream); |
| 622 | |
| 623 | if (cpu_dai->driver->ops->delay) |
| 624 | delay += cpu_dai->driver->ops->delay(substream, cpu_dai); |
| 625 | |
| 626 | if (codec_dai->driver->ops->delay) |
| 627 | delay += codec_dai->driver->ops->delay(substream, codec_dai); |
| 628 | |
| 629 | if (platform->driver->delay) |
| 630 | delay += platform->driver->delay(substream, codec_dai); |
| 631 | |
| 632 | runtime->delay = delay; |
| 633 | |
| 634 | return offset; |
| 635 | } |
| 636 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 637 | /* create a new pcm */ |
| 638 | int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num) |
| 639 | { |
| 640 | struct snd_soc_codec *codec = rtd->codec; |
| 641 | struct snd_soc_platform *platform = rtd->platform; |
| 642 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 643 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Sangsu Park | a500231 | 2012-01-02 17:15:10 +0900 | [diff] [blame] | 644 | struct snd_pcm_ops *soc_pcm_ops = &rtd->ops; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 645 | struct snd_pcm *pcm; |
| 646 | char new_name[64]; |
| 647 | int ret = 0, playback = 0, capture = 0; |
| 648 | |
Sangsu Park | a500231 | 2012-01-02 17:15:10 +0900 | [diff] [blame] | 649 | soc_pcm_ops->open = soc_pcm_open; |
| 650 | soc_pcm_ops->close = soc_pcm_close; |
| 651 | soc_pcm_ops->hw_params = soc_pcm_hw_params; |
| 652 | soc_pcm_ops->hw_free = soc_pcm_hw_free; |
| 653 | soc_pcm_ops->prepare = soc_pcm_prepare; |
| 654 | soc_pcm_ops->trigger = soc_pcm_trigger; |
| 655 | soc_pcm_ops->pointer = soc_pcm_pointer; |
| 656 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 657 | /* check client and interface hw capabilities */ |
| 658 | snprintf(new_name, sizeof(new_name), "%s %s-%d", |
| 659 | rtd->dai_link->stream_name, codec_dai->name, num); |
| 660 | |
| 661 | if (codec_dai->driver->playback.channels_min) |
| 662 | playback = 1; |
| 663 | if (codec_dai->driver->capture.channels_min) |
| 664 | capture = 1; |
| 665 | |
| 666 | dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name); |
| 667 | ret = snd_pcm_new(rtd->card->snd_card, new_name, |
| 668 | num, playback, capture, &pcm); |
| 669 | if (ret < 0) { |
| 670 | printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name); |
| 671 | return ret; |
| 672 | } |
| 673 | |
| 674 | /* DAPM dai link stream work */ |
| 675 | INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work); |
| 676 | |
| 677 | rtd->pcm = pcm; |
| 678 | pcm->private_data = rtd; |
| 679 | if (platform->driver->ops) { |
Sangsu Park | a500231 | 2012-01-02 17:15:10 +0900 | [diff] [blame] | 680 | soc_pcm_ops->mmap = platform->driver->ops->mmap; |
| 681 | soc_pcm_ops->pointer = platform->driver->ops->pointer; |
| 682 | soc_pcm_ops->ioctl = platform->driver->ops->ioctl; |
| 683 | soc_pcm_ops->copy = platform->driver->ops->copy; |
| 684 | soc_pcm_ops->silence = platform->driver->ops->silence; |
| 685 | soc_pcm_ops->ack = platform->driver->ops->ack; |
| 686 | soc_pcm_ops->page = platform->driver->ops->page; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | if (playback) |
Sangsu Park | a500231 | 2012-01-02 17:15:10 +0900 | [diff] [blame] | 690 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, soc_pcm_ops); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 691 | |
| 692 | if (capture) |
Sangsu Park | a500231 | 2012-01-02 17:15:10 +0900 | [diff] [blame] | 693 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, soc_pcm_ops); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 694 | |
| 695 | if (platform->driver->pcm_new) { |
| 696 | ret = platform->driver->pcm_new(rtd); |
| 697 | if (ret < 0) { |
| 698 | pr_err("asoc: platform pcm constructor failed\n"); |
| 699 | return ret; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | pcm->private_free = platform->driver->pcm_free; |
| 704 | printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name, |
| 705 | cpu_dai->name); |
| 706 | return ret; |
| 707 | } |