Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1 | /* |
| 2 | * soc-core.c -- ALSA SoC Audio Layer |
| 3 | * |
| 4 | * Copyright 2005 Wolfson Microelectronics PLC. |
| 5 | * Author: Liam Girdwood |
| 6 | * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | * |
| 13 | * Revision history |
| 14 | * 12th Aug 2005 Initial version. |
| 15 | * 25th Oct 2005 Working Codec, Interface and Platform registration. |
| 16 | * |
| 17 | * TODO: |
| 18 | * o Add hw rules to enforce rates, etc. |
| 19 | * o More testing with other codecs/machines. |
| 20 | * o Add more codecs and platforms to ensure good API coverage. |
| 21 | * o Support TDM on PCM and I2S |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/moduleparam.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/pm.h> |
| 29 | #include <linux/bitops.h> |
| 30 | #include <linux/platform_device.h> |
| 31 | #include <sound/driver.h> |
| 32 | #include <sound/core.h> |
| 33 | #include <sound/pcm.h> |
| 34 | #include <sound/pcm_params.h> |
| 35 | #include <sound/soc.h> |
| 36 | #include <sound/soc-dapm.h> |
| 37 | #include <sound/initval.h> |
| 38 | |
| 39 | /* debug */ |
| 40 | #define SOC_DEBUG 0 |
| 41 | #if SOC_DEBUG |
| 42 | #define dbg(format, arg...) printk(format, ## arg) |
| 43 | #else |
| 44 | #define dbg(format, arg...) |
| 45 | #endif |
| 46 | /* debug DAI capabilities matching */ |
| 47 | #define SOC_DEBUG_DAI 0 |
| 48 | #if SOC_DEBUG_DAI |
| 49 | #define dbgc(format, arg...) printk(format, ## arg) |
| 50 | #else |
| 51 | #define dbgc(format, arg...) |
| 52 | #endif |
| 53 | |
| 54 | static DEFINE_MUTEX(pcm_mutex); |
| 55 | static DEFINE_MUTEX(io_mutex); |
| 56 | static struct workqueue_struct *soc_workq; |
| 57 | static struct work_struct soc_stream_work; |
| 58 | static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq); |
| 59 | |
| 60 | /* supported sample rates */ |
| 61 | /* ATTENTION: these values depend on the definition in pcm.h! */ |
| 62 | static const unsigned int rates[] = { |
| 63 | 5512, 8000, 11025, 16000, 22050, 32000, 44100, |
| 64 | 48000, 64000, 88200, 96000, 176400, 192000 |
| 65 | }; |
| 66 | |
| 67 | /* |
| 68 | * This is a timeout to do a DAPM powerdown after a stream is closed(). |
| 69 | * It can be used to eliminate pops between different playback streams, e.g. |
| 70 | * between two audio tracks. |
| 71 | */ |
| 72 | static int pmdown_time = 5000; |
| 73 | module_param(pmdown_time, int, 0); |
| 74 | MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)"); |
| 75 | |
| 76 | #ifdef CONFIG_SND_SOC_AC97_BUS |
| 77 | /* unregister ac97 codec */ |
| 78 | static int soc_ac97_dev_unregister(struct snd_soc_codec *codec) |
| 79 | { |
| 80 | if (codec->ac97->dev.bus) |
| 81 | device_unregister(&codec->ac97->dev); |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | /* stop no dev release warning */ |
| 86 | static void soc_ac97_device_release(struct device *dev){} |
| 87 | |
| 88 | /* register ac97 codec to bus */ |
| 89 | static int soc_ac97_dev_register(struct snd_soc_codec *codec) |
| 90 | { |
| 91 | int err; |
| 92 | |
| 93 | codec->ac97->dev.bus = &ac97_bus_type; |
| 94 | codec->ac97->dev.parent = NULL; |
| 95 | codec->ac97->dev.release = soc_ac97_device_release; |
| 96 | |
| 97 | snprintf(codec->ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s", |
| 98 | codec->card->number, 0, codec->name); |
| 99 | err = device_register(&codec->ac97->dev); |
| 100 | if (err < 0) { |
| 101 | snd_printk(KERN_ERR "Can't register ac97 bus\n"); |
| 102 | codec->ac97->dev.bus = NULL; |
| 103 | return err; |
| 104 | } |
| 105 | return 0; |
| 106 | } |
| 107 | #endif |
| 108 | |
| 109 | static inline const char* get_dai_name(int type) |
| 110 | { |
| 111 | switch(type) { |
| 112 | case SND_SOC_DAI_AC97: |
| 113 | return "AC97"; |
| 114 | case SND_SOC_DAI_I2S: |
| 115 | return "I2S"; |
| 116 | case SND_SOC_DAI_PCM: |
| 117 | return "PCM"; |
| 118 | } |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | /* get rate format from rate */ |
| 123 | static inline int soc_get_rate_format(int rate) |
| 124 | { |
| 125 | int i; |
| 126 | |
| 127 | for (i = 0; i < ARRAY_SIZE(rates); i++) { |
| 128 | if (rates[i] == rate) |
| 129 | return 1 << i; |
| 130 | } |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | /* gets the audio system mclk/sysclk for the given parameters */ |
| 135 | static unsigned inline int soc_get_mclk(struct snd_soc_pcm_runtime *rtd, |
| 136 | struct snd_soc_clock_info *info) |
| 137 | { |
| 138 | struct snd_soc_device *socdev = rtd->socdev; |
| 139 | struct snd_soc_machine *machine = socdev->machine; |
| 140 | int i; |
| 141 | |
| 142 | /* find the matching machine config and get it's mclk for the given |
| 143 | * sample rate and hardware format */ |
| 144 | for(i = 0; i < machine->num_links; i++) { |
| 145 | if (machine->dai_link[i].cpu_dai == rtd->cpu_dai && |
| 146 | machine->dai_link[i].config_sysclk) |
| 147 | return machine->dai_link[i].config_sysclk(rtd, info); |
| 148 | } |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | /* changes a bitclk multiplier mask to a divider mask */ |
| 153 | static u16 soc_bfs_mult_to_div(u16 bfs, int rate, unsigned int mclk, |
| 154 | unsigned int pcmfmt, unsigned int chn) |
| 155 | { |
| 156 | int i, j; |
| 157 | u16 bfs_ = 0; |
| 158 | int size = snd_pcm_format_physical_width(pcmfmt), min = 0; |
| 159 | |
| 160 | if (size <= 0) |
| 161 | return 0; |
| 162 | |
| 163 | /* the minimum bit clock that has enough bandwidth */ |
| 164 | min = size * rate * chn; |
| 165 | dbgc("mult --> div min bclk %d with mclk %d\n", min, mclk); |
| 166 | |
| 167 | for (i = 0; i < 16; i++) { |
| 168 | if ((bfs >> i) & 0x1) { |
| 169 | j = rate * SND_SOC_FSB_REAL(1<<i); |
| 170 | |
| 171 | if (j >= min) { |
| 172 | bfs_ |= SND_SOC_FSBD(mclk/j); |
| 173 | dbgc("mult --> div support mult %d\n", |
| 174 | SND_SOC_FSB_REAL(1<<i)); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return bfs_; |
| 180 | } |
| 181 | |
| 182 | /* changes a bitclk divider mask to a multiplier mask */ |
| 183 | static u16 soc_bfs_div_to_mult(u16 bfs, int rate, unsigned int mclk, |
| 184 | unsigned int pcmfmt, unsigned int chn) |
| 185 | { |
| 186 | int i, j; |
| 187 | u16 bfs_ = 0; |
| 188 | |
| 189 | int size = snd_pcm_format_physical_width(pcmfmt), min = 0; |
| 190 | |
| 191 | if (size <= 0) |
| 192 | return 0; |
| 193 | |
| 194 | /* the minimum bit clock that has enough bandwidth */ |
| 195 | min = size * rate * chn; |
| 196 | dbgc("div to mult min bclk %d with mclk %d\n", min, mclk); |
| 197 | |
| 198 | for (i = 0; i < 16; i++) { |
| 199 | if ((bfs >> i) & 0x1) { |
| 200 | j = mclk / (SND_SOC_FSBD_REAL(1<<i)); |
| 201 | if (j >= min) { |
| 202 | bfs_ |= SND_SOC_FSB(j/rate); |
| 203 | dbgc("div --> mult support div %d\n", |
| 204 | SND_SOC_FSBD_REAL(1<<i)); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | return bfs_; |
| 210 | } |
| 211 | |
| 212 | /* Matches codec DAI and SoC CPU DAI hardware parameters */ |
| 213 | static int soc_hw_match_params(struct snd_pcm_substream *substream, |
| 214 | struct snd_pcm_hw_params *params) |
| 215 | { |
| 216 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 217 | struct snd_soc_dai_mode *codec_dai_mode = NULL; |
| 218 | struct snd_soc_dai_mode *cpu_dai_mode = NULL; |
| 219 | struct snd_soc_clock_info clk_info; |
| 220 | unsigned int fs, mclk, codec_bfs, cpu_bfs, rate = params_rate(params), |
| 221 | chn, j, k, cpu_bclk, codec_bclk, pcmrate; |
| 222 | u16 fmt = 0; |
| 223 | |
| 224 | dbg("asoc: match version %s\n", SND_SOC_VERSION); |
| 225 | clk_info.rate = rate; |
| 226 | pcmrate = soc_get_rate_format(rate); |
| 227 | |
| 228 | /* try and find a match from the codec and cpu DAI capabilities */ |
| 229 | for (j = 0; j < rtd->codec_dai->caps.num_modes; j++) { |
| 230 | for (k = 0; k < rtd->cpu_dai->caps.num_modes; k++) { |
| 231 | codec_dai_mode = &rtd->codec_dai->caps.mode[j]; |
| 232 | cpu_dai_mode = &rtd->cpu_dai->caps.mode[k]; |
| 233 | |
| 234 | if (!(codec_dai_mode->pcmrate & cpu_dai_mode->pcmrate & |
| 235 | pcmrate)) { |
| 236 | dbgc("asoc: DAI[%d:%d] failed to match rate\n", j, k); |
| 237 | continue; |
| 238 | } |
| 239 | |
| 240 | fmt = codec_dai_mode->fmt & cpu_dai_mode->fmt; |
| 241 | if (!(fmt & SND_SOC_DAIFMT_FORMAT_MASK)) { |
| 242 | dbgc("asoc: DAI[%d:%d] failed to match format\n", j, k); |
| 243 | continue; |
| 244 | } |
| 245 | |
| 246 | if (!(fmt & SND_SOC_DAIFMT_CLOCK_MASK)) { |
| 247 | dbgc("asoc: DAI[%d:%d] failed to match clock masters\n", |
| 248 | j, k); |
| 249 | continue; |
| 250 | } |
| 251 | |
| 252 | if (!(fmt & SND_SOC_DAIFMT_INV_MASK)) { |
| 253 | dbgc("asoc: DAI[%d:%d] failed to match invert\n", j, k); |
| 254 | continue; |
| 255 | } |
| 256 | |
| 257 | if (!(codec_dai_mode->pcmfmt & cpu_dai_mode->pcmfmt)) { |
| 258 | dbgc("asoc: DAI[%d:%d] failed to match pcm format\n", j, k); |
| 259 | continue; |
| 260 | } |
| 261 | |
| 262 | if (!(codec_dai_mode->pcmdir & cpu_dai_mode->pcmdir)) { |
| 263 | dbgc("asoc: DAI[%d:%d] failed to match direction\n", j, k); |
| 264 | continue; |
| 265 | } |
| 266 | |
| 267 | /* todo - still need to add tdm selection */ |
| 268 | rtd->cpu_dai->dai_runtime.fmt = |
| 269 | rtd->codec_dai->dai_runtime.fmt = |
| 270 | 1 << (ffs(fmt & SND_SOC_DAIFMT_FORMAT_MASK) -1) | |
| 271 | 1 << (ffs(fmt & SND_SOC_DAIFMT_CLOCK_MASK) - 1) | |
| 272 | 1 << (ffs(fmt & SND_SOC_DAIFMT_INV_MASK) - 1); |
| 273 | clk_info.bclk_master = |
| 274 | rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK; |
| 275 | |
| 276 | /* make sure the ratio between rate and master |
| 277 | * clock is acceptable*/ |
| 278 | fs = (cpu_dai_mode->fs & codec_dai_mode->fs); |
| 279 | if (fs == 0) { |
| 280 | dbgc("asoc: DAI[%d:%d] failed to match FS\n", j, k); |
| 281 | continue; |
| 282 | } |
| 283 | clk_info.fs = rtd->cpu_dai->dai_runtime.fs = |
| 284 | rtd->codec_dai->dai_runtime.fs = fs; |
| 285 | |
| 286 | /* calculate audio system clocking using slowest clocks possible*/ |
| 287 | mclk = soc_get_mclk(rtd, &clk_info); |
| 288 | if (mclk == 0) { |
| 289 | dbgc("asoc: DAI[%d:%d] configuration not clockable\n", j, k); |
| 290 | dbgc("asoc: rate %d fs %d master %x\n", rate, fs, |
| 291 | clk_info.bclk_master); |
| 292 | continue; |
| 293 | } |
| 294 | |
| 295 | /* calculate word size (per channel) and frame size */ |
| 296 | rtd->codec_dai->dai_runtime.pcmfmt = |
| 297 | rtd->cpu_dai->dai_runtime.pcmfmt = |
| 298 | 1 << params_format(params); |
| 299 | |
| 300 | chn = params_channels(params); |
| 301 | /* i2s always has left and right */ |
| 302 | if (params_channels(params) == 1 && |
| 303 | rtd->cpu_dai->dai_runtime.fmt & (SND_SOC_DAIFMT_I2S | |
| 304 | SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_LEFT_J)) |
| 305 | chn <<= 1; |
| 306 | |
| 307 | /* Calculate bfs - the ratio between bitclock and the sample rate |
| 308 | * We must take into consideration the dividers and multipliers |
| 309 | * used in the codec and cpu DAI modes. We always choose the |
| 310 | * lowest possible clocks to reduce power. |
| 311 | */ |
| 312 | if (codec_dai_mode->flags & cpu_dai_mode->flags & |
| 313 | SND_SOC_DAI_BFS_DIV) { |
| 314 | /* cpu & codec bfs dividers */ |
| 315 | rtd->cpu_dai->dai_runtime.bfs = |
| 316 | rtd->codec_dai->dai_runtime.bfs = |
| 317 | 1 << (fls(codec_dai_mode->bfs & cpu_dai_mode->bfs) - 1); |
| 318 | } else if (codec_dai_mode->flags & SND_SOC_DAI_BFS_DIV) { |
| 319 | /* normalise bfs codec divider & cpu mult */ |
| 320 | codec_bfs = soc_bfs_div_to_mult(codec_dai_mode->bfs, rate, |
| 321 | mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn); |
| 322 | rtd->cpu_dai->dai_runtime.bfs = |
| 323 | 1 << (ffs(codec_bfs & cpu_dai_mode->bfs) - 1); |
| 324 | cpu_bfs = soc_bfs_mult_to_div(cpu_dai_mode->bfs, rate, mclk, |
| 325 | rtd->codec_dai->dai_runtime.pcmfmt, chn); |
| 326 | rtd->codec_dai->dai_runtime.bfs = |
| 327 | 1 << (fls(codec_dai_mode->bfs & cpu_bfs) - 1); |
| 328 | } else if (cpu_dai_mode->flags & SND_SOC_DAI_BFS_DIV) { |
| 329 | /* normalise bfs codec mult & cpu divider */ |
| 330 | codec_bfs = soc_bfs_mult_to_div(codec_dai_mode->bfs, rate, |
| 331 | mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn); |
| 332 | rtd->cpu_dai->dai_runtime.bfs = |
| 333 | 1 << (fls(codec_bfs & cpu_dai_mode->bfs) -1); |
| 334 | cpu_bfs = soc_bfs_div_to_mult(cpu_dai_mode->bfs, rate, mclk, |
| 335 | rtd->codec_dai->dai_runtime.pcmfmt, chn); |
| 336 | rtd->codec_dai->dai_runtime.bfs = |
| 337 | 1 << (ffs(codec_dai_mode->bfs & cpu_bfs) -1); |
| 338 | } else { |
| 339 | /* codec & cpu bfs rate multipliers */ |
| 340 | rtd->cpu_dai->dai_runtime.bfs = |
| 341 | rtd->codec_dai->dai_runtime.bfs = |
| 342 | 1 << (ffs(codec_dai_mode->bfs & cpu_dai_mode->bfs) -1); |
| 343 | } |
| 344 | |
| 345 | /* make sure the bit clock speed is acceptable */ |
| 346 | if (!rtd->cpu_dai->dai_runtime.bfs || |
| 347 | !rtd->codec_dai->dai_runtime.bfs) { |
| 348 | dbgc("asoc: DAI[%d:%d] failed to match BFS\n", j, k); |
| 349 | dbgc("asoc: cpu_dai %x codec %x\n", |
| 350 | rtd->cpu_dai->dai_runtime.bfs, |
| 351 | rtd->codec_dai->dai_runtime.bfs); |
| 352 | dbgc("asoc: mclk %d hwfmt %x\n", mclk, fmt); |
| 353 | continue; |
| 354 | } |
| 355 | |
| 356 | goto found; |
| 357 | } |
| 358 | } |
| 359 | printk(KERN_ERR "asoc: no matching DAI found between codec and CPU\n"); |
| 360 | return -EINVAL; |
| 361 | |
| 362 | found: |
| 363 | /* we have matching DAI's, so complete the runtime info */ |
| 364 | rtd->codec_dai->dai_runtime.pcmrate = |
| 365 | rtd->cpu_dai->dai_runtime.pcmrate = |
| 366 | soc_get_rate_format(rate); |
| 367 | |
| 368 | rtd->codec_dai->dai_runtime.priv = codec_dai_mode->priv; |
| 369 | rtd->cpu_dai->dai_runtime.priv = cpu_dai_mode->priv; |
| 370 | rtd->codec_dai->dai_runtime.flags = codec_dai_mode->flags; |
| 371 | rtd->cpu_dai->dai_runtime.flags = cpu_dai_mode->flags; |
| 372 | |
| 373 | /* for debug atm */ |
| 374 | dbg("asoc: DAI[%d:%d] Match OK\n", j, k); |
| 375 | if (rtd->codec_dai->dai_runtime.flags == SND_SOC_DAI_BFS_DIV) { |
| 376 | codec_bclk = (rtd->codec_dai->dai_runtime.fs * params_rate(params)) / |
| 377 | SND_SOC_FSBD_REAL(rtd->codec_dai->dai_runtime.bfs); |
| 378 | dbg("asoc: codec fs %d mclk %d bfs div %d bclk %d\n", |
| 379 | rtd->codec_dai->dai_runtime.fs, mclk, |
| 380 | SND_SOC_FSBD_REAL(rtd->codec_dai->dai_runtime.bfs), codec_bclk); |
| 381 | } else { |
| 382 | codec_bclk = params_rate(params) * |
| 383 | SND_SOC_FSB_REAL(rtd->codec_dai->dai_runtime.bfs); |
| 384 | dbg("asoc: codec fs %d mclk %d bfs mult %d bclk %d\n", |
| 385 | rtd->codec_dai->dai_runtime.fs, mclk, |
| 386 | SND_SOC_FSB_REAL(rtd->codec_dai->dai_runtime.bfs), codec_bclk); |
| 387 | } |
| 388 | if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_DIV) { |
| 389 | cpu_bclk = (rtd->cpu_dai->dai_runtime.fs * params_rate(params)) / |
| 390 | SND_SOC_FSBD_REAL(rtd->cpu_dai->dai_runtime.bfs); |
| 391 | dbg("asoc: cpu fs %d mclk %d bfs div %d bclk %d\n", |
| 392 | rtd->cpu_dai->dai_runtime.fs, mclk, |
| 393 | SND_SOC_FSBD_REAL(rtd->cpu_dai->dai_runtime.bfs), cpu_bclk); |
| 394 | } else { |
| 395 | cpu_bclk = params_rate(params) * |
| 396 | SND_SOC_FSB_REAL(rtd->cpu_dai->dai_runtime.bfs); |
| 397 | dbg("asoc: cpu fs %d mclk %d bfs mult %d bclk %d\n", |
| 398 | rtd->cpu_dai->dai_runtime.fs, mclk, |
| 399 | SND_SOC_FSB_REAL(rtd->cpu_dai->dai_runtime.bfs), cpu_bclk); |
| 400 | } |
| 401 | |
| 402 | /* |
| 403 | * Check we have matching bitclocks. If we don't then it means the |
| 404 | * sysclock returned by either the codec or cpu DAI (selected by the |
| 405 | * machine sysclock function) is wrong compared with the supported DAI |
| 406 | * modes for the codec or cpu DAI. |
| 407 | */ |
| 408 | if (cpu_bclk != codec_bclk){ |
| 409 | printk(KERN_ERR |
| 410 | "asoc: codec and cpu bitclocks differ, audio may be wrong speed\n" |
| 411 | ); |
| 412 | printk(KERN_ERR "asoc: codec %d != cpu %d\n", codec_bclk, cpu_bclk); |
| 413 | } |
| 414 | |
| 415 | switch(rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK) { |
| 416 | case SND_SOC_DAIFMT_CBM_CFM: |
| 417 | dbg("asoc: DAI codec BCLK master, LRC master\n"); |
| 418 | break; |
| 419 | case SND_SOC_DAIFMT_CBS_CFM: |
| 420 | dbg("asoc: DAI codec BCLK slave, LRC master\n"); |
| 421 | break; |
| 422 | case SND_SOC_DAIFMT_CBM_CFS: |
| 423 | dbg("asoc: DAI codec BCLK master, LRC slave\n"); |
| 424 | break; |
| 425 | case SND_SOC_DAIFMT_CBS_CFS: |
| 426 | dbg("asoc: DAI codec BCLK slave, LRC slave\n"); |
| 427 | break; |
| 428 | } |
| 429 | dbg("asoc: mode %x, invert %x\n", |
| 430 | rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_FORMAT_MASK, |
| 431 | rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_INV_MASK); |
| 432 | dbg("asoc: audio rate %d chn %d fmt %x\n", params_rate(params), |
| 433 | params_channels(params), params_format(params)); |
| 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static inline u32 get_rates(struct snd_soc_dai_mode *modes, int nmodes) |
| 439 | { |
| 440 | int i; |
| 441 | u32 rates = 0; |
| 442 | |
| 443 | for(i = 0; i < nmodes; i++) |
| 444 | rates |= modes[i].pcmrate; |
| 445 | |
| 446 | return rates; |
| 447 | } |
| 448 | |
| 449 | static inline u64 get_formats(struct snd_soc_dai_mode *modes, int nmodes) |
| 450 | { |
| 451 | int i; |
| 452 | u64 formats = 0; |
| 453 | |
| 454 | for(i = 0; i < nmodes; i++) |
| 455 | formats |= modes[i].pcmfmt; |
| 456 | |
| 457 | return formats; |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * Called by ALSA when a PCM substream is opened, the runtime->hw record is |
| 462 | * then initialized and any private data can be allocated. This also calls |
| 463 | * startup for the cpu DAI, platform, machine and codec DAI. |
| 464 | */ |
| 465 | static int soc_pcm_open(struct snd_pcm_substream *substream) |
| 466 | { |
| 467 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 468 | struct snd_soc_device *socdev = rtd->socdev; |
| 469 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 470 | struct snd_soc_machine *machine = socdev->machine; |
| 471 | struct snd_soc_platform *platform = socdev->platform; |
| 472 | struct snd_soc_codec_dai *codec_dai = rtd->codec_dai; |
| 473 | struct snd_soc_cpu_dai *cpu_dai = rtd->cpu_dai; |
| 474 | int ret = 0; |
| 475 | |
| 476 | mutex_lock(&pcm_mutex); |
| 477 | |
| 478 | /* startup the audio subsystem */ |
| 479 | if (rtd->cpu_dai->ops.startup) { |
| 480 | ret = rtd->cpu_dai->ops.startup(substream); |
| 481 | if (ret < 0) { |
| 482 | printk(KERN_ERR "asoc: can't open interface %s\n", |
| 483 | rtd->cpu_dai->name); |
| 484 | goto out; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | if (platform->pcm_ops->open) { |
| 489 | ret = platform->pcm_ops->open(substream); |
| 490 | if (ret < 0) { |
| 491 | printk(KERN_ERR "asoc: can't open platform %s\n", platform->name); |
| 492 | goto platform_err; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | if (machine->ops && machine->ops->startup) { |
| 497 | ret = machine->ops->startup(substream); |
| 498 | if (ret < 0) { |
| 499 | printk(KERN_ERR "asoc: %s startup failed\n", machine->name); |
| 500 | goto machine_err; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | if (rtd->codec_dai->ops.startup) { |
| 505 | ret = rtd->codec_dai->ops.startup(substream); |
| 506 | if (ret < 0) { |
| 507 | printk(KERN_ERR "asoc: can't open codec %s\n", |
| 508 | rtd->codec_dai->name); |
| 509 | goto codec_dai_err; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | /* create runtime params from DMA, codec and cpu DAI */ |
| 514 | if (runtime->hw.rates) |
| 515 | runtime->hw.rates &= |
| 516 | get_rates(codec_dai->caps.mode, codec_dai->caps.num_modes) & |
| 517 | get_rates(cpu_dai->caps.mode, cpu_dai->caps.num_modes); |
| 518 | else |
| 519 | runtime->hw.rates = |
| 520 | get_rates(codec_dai->caps.mode, codec_dai->caps.num_modes) & |
| 521 | get_rates(cpu_dai->caps.mode, cpu_dai->caps.num_modes); |
| 522 | if (runtime->hw.formats) |
| 523 | runtime->hw.formats &= |
| 524 | get_formats(codec_dai->caps.mode, codec_dai->caps.num_modes) & |
| 525 | get_formats(cpu_dai->caps.mode, cpu_dai->caps.num_modes); |
| 526 | else |
| 527 | runtime->hw.formats = |
| 528 | get_formats(codec_dai->caps.mode, codec_dai->caps.num_modes) & |
| 529 | get_formats(cpu_dai->caps.mode, cpu_dai->caps.num_modes); |
| 530 | |
| 531 | /* Check that the codec and cpu DAI's are compatible */ |
| 532 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 533 | runtime->hw.rate_min = |
| 534 | max(rtd->codec_dai->playback.rate_min, |
| 535 | rtd->cpu_dai->playback.rate_min); |
| 536 | runtime->hw.rate_max = |
| 537 | min(rtd->codec_dai->playback.rate_max, |
| 538 | rtd->cpu_dai->playback.rate_max); |
| 539 | runtime->hw.channels_min = |
| 540 | max(rtd->codec_dai->playback.channels_min, |
| 541 | rtd->cpu_dai->playback.channels_min); |
| 542 | runtime->hw.channels_max = |
| 543 | min(rtd->codec_dai->playback.channels_max, |
| 544 | rtd->cpu_dai->playback.channels_max); |
| 545 | } else { |
| 546 | runtime->hw.rate_min = |
| 547 | max(rtd->codec_dai->capture.rate_min, |
| 548 | rtd->cpu_dai->capture.rate_min); |
| 549 | runtime->hw.rate_max = |
| 550 | min(rtd->codec_dai->capture.rate_max, |
| 551 | rtd->cpu_dai->capture.rate_max); |
| 552 | runtime->hw.channels_min = |
| 553 | max(rtd->codec_dai->capture.channels_min, |
| 554 | rtd->cpu_dai->capture.channels_min); |
| 555 | runtime->hw.channels_max = |
| 556 | min(rtd->codec_dai->capture.channels_max, |
| 557 | rtd->cpu_dai->capture.channels_max); |
| 558 | } |
| 559 | |
| 560 | snd_pcm_limit_hw_rates(runtime); |
| 561 | if (!runtime->hw.rates) { |
| 562 | printk(KERN_ERR "asoc: %s <-> %s No matching rates\n", |
| 563 | rtd->codec_dai->name, rtd->cpu_dai->name); |
| 564 | goto codec_dai_err; |
| 565 | } |
| 566 | if (!runtime->hw.formats) { |
| 567 | printk(KERN_ERR "asoc: %s <-> %s No matching formats\n", |
| 568 | rtd->codec_dai->name, rtd->cpu_dai->name); |
| 569 | goto codec_dai_err; |
| 570 | } |
| 571 | if (!runtime->hw.channels_min || !runtime->hw.channels_max) { |
| 572 | printk(KERN_ERR "asoc: %s <-> %s No matching channels\n", |
| 573 | rtd->codec_dai->name, rtd->cpu_dai->name); |
| 574 | goto codec_dai_err; |
| 575 | } |
| 576 | |
| 577 | dbg("asoc: %s <-> %s info:\n", rtd->codec_dai->name, rtd->cpu_dai->name); |
Liam Girdwood | b5c5fd2 | 2006-10-13 19:13:41 +0200 | [diff] [blame^] | 578 | dbg("asoc: rate mask 0x%x\n", runtime->hw.rates); |
| 579 | dbg("asoc: min ch %d max ch %d\n", runtime->hw.channels_min, |
| 580 | runtime->hw.channels_max); |
| 581 | dbg("asoc: min rate %d max rate %d\n", runtime->hw.rate_min, |
| 582 | runtime->hw.rate_max); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 583 | |
| 584 | |
| 585 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 586 | rtd->cpu_dai->playback.active = rtd->codec_dai->playback.active = 1; |
| 587 | else |
| 588 | rtd->cpu_dai->capture.active = rtd->codec_dai->capture.active = 1; |
| 589 | rtd->cpu_dai->active = rtd->codec_dai->active = 1; |
| 590 | rtd->cpu_dai->runtime = runtime; |
| 591 | socdev->codec->active++; |
| 592 | mutex_unlock(&pcm_mutex); |
| 593 | return 0; |
| 594 | |
| 595 | codec_dai_err: |
| 596 | if (machine->ops && machine->ops->shutdown) |
| 597 | machine->ops->shutdown(substream); |
| 598 | |
| 599 | machine_err: |
| 600 | if (platform->pcm_ops->close) |
| 601 | platform->pcm_ops->close(substream); |
| 602 | |
| 603 | platform_err: |
| 604 | if (rtd->cpu_dai->ops.shutdown) |
| 605 | rtd->cpu_dai->ops.shutdown(substream); |
| 606 | out: |
| 607 | mutex_unlock(&pcm_mutex); |
| 608 | return ret; |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | * Power down the audio subsytem pmdown_time msecs after close is called. |
| 613 | * This is to ensure there are no pops or clicks in between any music tracks |
| 614 | * due to DAPM power cycling. |
| 615 | */ |
| 616 | static void close_delayed_work(void *data) |
| 617 | { |
| 618 | struct snd_soc_device *socdev = data; |
| 619 | struct snd_soc_codec *codec = socdev->codec; |
| 620 | struct snd_soc_codec_dai *codec_dai; |
| 621 | int i; |
| 622 | |
| 623 | mutex_lock(&pcm_mutex); |
| 624 | for(i = 0; i < codec->num_dai; i++) { |
| 625 | codec_dai = &codec->dai[i]; |
| 626 | |
| 627 | dbg("pop wq checking: %s status: %s waiting: %s\n", |
| 628 | codec_dai->playback.stream_name, |
| 629 | codec_dai->playback.active ? "active" : "inactive", |
| 630 | codec_dai->pop_wait ? "yes" : "no"); |
| 631 | |
| 632 | /* are we waiting on this codec DAI stream */ |
| 633 | if (codec_dai->pop_wait == 1) { |
| 634 | |
| 635 | codec_dai->pop_wait = 0; |
| 636 | snd_soc_dapm_stream_event(codec, codec_dai->playback.stream_name, |
| 637 | SND_SOC_DAPM_STREAM_STOP); |
| 638 | |
| 639 | /* power down the codec power domain if no longer active */ |
| 640 | if (codec->active == 0) { |
| 641 | dbg("pop wq D3 %s %s\n", codec->name, |
| 642 | codec_dai->playback.stream_name); |
| 643 | if (codec->dapm_event) |
| 644 | codec->dapm_event(codec, SNDRV_CTL_POWER_D3hot); |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | mutex_unlock(&pcm_mutex); |
| 649 | } |
| 650 | |
| 651 | /* |
| 652 | * Called by ALSA when a PCM substream is closed. Private data can be |
| 653 | * freed here. The cpu DAI, codec DAI, machine and platform are also |
| 654 | * shutdown. |
| 655 | */ |
| 656 | static int soc_codec_close(struct snd_pcm_substream *substream) |
| 657 | { |
| 658 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 659 | struct snd_soc_device *socdev = rtd->socdev; |
| 660 | struct snd_soc_machine *machine = socdev->machine; |
| 661 | struct snd_soc_platform *platform = socdev->platform; |
| 662 | struct snd_soc_codec *codec = socdev->codec; |
| 663 | |
| 664 | mutex_lock(&pcm_mutex); |
| 665 | |
| 666 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 667 | rtd->cpu_dai->playback.active = rtd->codec_dai->playback.active = 0; |
| 668 | else |
| 669 | rtd->cpu_dai->capture.active = rtd->codec_dai->capture.active = 0; |
| 670 | |
| 671 | if (rtd->codec_dai->playback.active == 0 && |
| 672 | rtd->codec_dai->capture.active == 0) { |
| 673 | rtd->cpu_dai->active = rtd->codec_dai->active = 0; |
| 674 | } |
| 675 | codec->active--; |
| 676 | |
| 677 | if (rtd->cpu_dai->ops.shutdown) |
| 678 | rtd->cpu_dai->ops.shutdown(substream); |
| 679 | |
| 680 | if (rtd->codec_dai->ops.shutdown) |
| 681 | rtd->codec_dai->ops.shutdown(substream); |
| 682 | |
| 683 | if (machine->ops && machine->ops->shutdown) |
| 684 | machine->ops->shutdown(substream); |
| 685 | |
| 686 | if (platform->pcm_ops->close) |
| 687 | platform->pcm_ops->close(substream); |
| 688 | rtd->cpu_dai->runtime = NULL; |
| 689 | |
| 690 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 691 | /* start delayed pop wq here for playback streams */ |
| 692 | rtd->codec_dai->pop_wait = 1; |
| 693 | queue_delayed_work(soc_workq, &soc_stream_work, |
| 694 | msecs_to_jiffies(pmdown_time)); |
| 695 | } else { |
| 696 | /* capture streams can be powered down now */ |
| 697 | snd_soc_dapm_stream_event(codec, rtd->codec_dai->capture.stream_name, |
| 698 | SND_SOC_DAPM_STREAM_STOP); |
| 699 | |
| 700 | if (codec->active == 0 && rtd->codec_dai->pop_wait == 0){ |
| 701 | if (codec->dapm_event) |
| 702 | codec->dapm_event(codec, SNDRV_CTL_POWER_D3hot); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | mutex_unlock(&pcm_mutex); |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | /* |
| 711 | * Called by ALSA when the PCM substream is prepared, can set format, sample |
| 712 | * rate, etc. This function is non atomic and can be called multiple times, |
| 713 | * it can refer to the runtime info. |
| 714 | */ |
| 715 | static int soc_pcm_prepare(struct snd_pcm_substream *substream) |
| 716 | { |
| 717 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 718 | struct snd_soc_device *socdev = rtd->socdev; |
| 719 | struct snd_soc_platform *platform = socdev->platform; |
| 720 | struct snd_soc_codec *codec = socdev->codec; |
| 721 | int ret = 0; |
| 722 | |
| 723 | mutex_lock(&pcm_mutex); |
| 724 | if (platform->pcm_ops->prepare) { |
| 725 | ret = platform->pcm_ops->prepare(substream); |
| 726 | if (ret < 0) |
| 727 | goto out; |
| 728 | } |
| 729 | |
| 730 | if (rtd->codec_dai->ops.prepare) { |
| 731 | ret = rtd->codec_dai->ops.prepare(substream); |
| 732 | if (ret < 0) |
| 733 | goto out; |
| 734 | } |
| 735 | |
| 736 | if (rtd->cpu_dai->ops.prepare) |
| 737 | ret = rtd->cpu_dai->ops.prepare(substream); |
| 738 | |
| 739 | /* we only want to start a DAPM playback stream if we are not waiting |
| 740 | * on an existing one stopping */ |
| 741 | if (rtd->codec_dai->pop_wait) { |
| 742 | /* we are waiting for the delayed work to start */ |
| 743 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 744 | snd_soc_dapm_stream_event(codec, |
| 745 | rtd->codec_dai->capture.stream_name, |
| 746 | SND_SOC_DAPM_STREAM_START); |
| 747 | else { |
| 748 | rtd->codec_dai->pop_wait = 0; |
| 749 | cancel_delayed_work(&soc_stream_work); |
| 750 | if (rtd->codec_dai->digital_mute) |
| 751 | rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0); |
| 752 | } |
| 753 | } else { |
| 754 | /* no delayed work - do we need to power up codec */ |
| 755 | if (codec->dapm_state != SNDRV_CTL_POWER_D0) { |
| 756 | |
| 757 | if (codec->dapm_event) |
| 758 | codec->dapm_event(codec, SNDRV_CTL_POWER_D1); |
| 759 | |
| 760 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 761 | snd_soc_dapm_stream_event(codec, |
| 762 | rtd->codec_dai->playback.stream_name, |
| 763 | SND_SOC_DAPM_STREAM_START); |
| 764 | else |
| 765 | snd_soc_dapm_stream_event(codec, |
| 766 | rtd->codec_dai->capture.stream_name, |
| 767 | SND_SOC_DAPM_STREAM_START); |
| 768 | |
| 769 | if (codec->dapm_event) |
| 770 | codec->dapm_event(codec, SNDRV_CTL_POWER_D0); |
| 771 | if (rtd->codec_dai->digital_mute) |
| 772 | rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0); |
| 773 | |
| 774 | } else { |
| 775 | /* codec already powered - power on widgets */ |
| 776 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 777 | snd_soc_dapm_stream_event(codec, |
| 778 | rtd->codec_dai->playback.stream_name, |
| 779 | SND_SOC_DAPM_STREAM_START); |
| 780 | else |
| 781 | snd_soc_dapm_stream_event(codec, |
| 782 | rtd->codec_dai->capture.stream_name, |
| 783 | SND_SOC_DAPM_STREAM_START); |
| 784 | if (rtd->codec_dai->digital_mute) |
| 785 | rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | out: |
| 790 | mutex_unlock(&pcm_mutex); |
| 791 | return ret; |
| 792 | } |
| 793 | |
| 794 | /* |
| 795 | * Called by ALSA when the hardware params are set by application. This |
| 796 | * function can also be called multiple times and can allocate buffers |
| 797 | * (using snd_pcm_lib_* ). It's non-atomic. |
| 798 | */ |
| 799 | static int soc_pcm_hw_params(struct snd_pcm_substream *substream, |
| 800 | struct snd_pcm_hw_params *params) |
| 801 | { |
| 802 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 803 | struct snd_soc_device *socdev = rtd->socdev; |
| 804 | struct snd_soc_platform *platform = socdev->platform; |
| 805 | struct snd_soc_machine *machine = socdev->machine; |
| 806 | int ret = 0; |
| 807 | |
| 808 | mutex_lock(&pcm_mutex); |
| 809 | |
| 810 | /* we don't need to match any AC97 params */ |
| 811 | if (rtd->cpu_dai->type != SND_SOC_DAI_AC97) { |
| 812 | ret = soc_hw_match_params(substream, params); |
| 813 | if (ret < 0) |
| 814 | goto out; |
| 815 | } else { |
| 816 | struct snd_soc_clock_info clk_info; |
| 817 | clk_info.rate = params_rate(params); |
| 818 | ret = soc_get_mclk(rtd, &clk_info); |
| 819 | if (ret < 0) |
| 820 | goto out; |
| 821 | } |
| 822 | |
| 823 | if (rtd->codec_dai->ops.hw_params) { |
| 824 | ret = rtd->codec_dai->ops.hw_params(substream, params); |
| 825 | if (ret < 0) { |
| 826 | printk(KERN_ERR "asoc: can't set codec %s hw params\n", |
| 827 | rtd->codec_dai->name); |
| 828 | goto out; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | if (rtd->cpu_dai->ops.hw_params) { |
| 833 | ret = rtd->cpu_dai->ops.hw_params(substream, params); |
| 834 | if (ret < 0) { |
| 835 | printk(KERN_ERR "asoc: can't set interface %s hw params\n", |
| 836 | rtd->cpu_dai->name); |
| 837 | goto interface_err; |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | if (platform->pcm_ops->hw_params) { |
| 842 | ret = platform->pcm_ops->hw_params(substream, params); |
| 843 | if (ret < 0) { |
| 844 | printk(KERN_ERR "asoc: can't set platform %s hw params\n", |
| 845 | platform->name); |
| 846 | goto platform_err; |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | if (machine->ops && machine->ops->hw_params) { |
| 851 | ret = machine->ops->hw_params(substream, params); |
| 852 | if (ret < 0) { |
| 853 | printk(KERN_ERR "asoc: machine hw_params failed\n"); |
| 854 | goto machine_err; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | out: |
| 859 | mutex_unlock(&pcm_mutex); |
| 860 | return ret; |
| 861 | |
| 862 | machine_err: |
| 863 | if (platform->pcm_ops->hw_free) |
| 864 | platform->pcm_ops->hw_free(substream); |
| 865 | |
| 866 | platform_err: |
| 867 | if (rtd->cpu_dai->ops.hw_free) |
| 868 | rtd->cpu_dai->ops.hw_free(substream); |
| 869 | |
| 870 | interface_err: |
| 871 | if (rtd->codec_dai->ops.hw_free) |
| 872 | rtd->codec_dai->ops.hw_free(substream); |
| 873 | |
| 874 | mutex_unlock(&pcm_mutex); |
| 875 | return ret; |
| 876 | } |
| 877 | |
| 878 | /* |
| 879 | * Free's resources allocated by hw_params, can be called multiple times |
| 880 | */ |
| 881 | static int soc_pcm_hw_free(struct snd_pcm_substream *substream) |
| 882 | { |
| 883 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 884 | struct snd_soc_device *socdev = rtd->socdev; |
| 885 | struct snd_soc_platform *platform = socdev->platform; |
| 886 | struct snd_soc_codec *codec = socdev->codec; |
| 887 | struct snd_soc_machine *machine = socdev->machine; |
| 888 | |
| 889 | mutex_lock(&pcm_mutex); |
| 890 | |
| 891 | /* apply codec digital mute */ |
| 892 | if (!codec->active && rtd->codec_dai->digital_mute) |
| 893 | rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 1); |
| 894 | |
| 895 | /* free any machine hw params */ |
| 896 | if (machine->ops && machine->ops->hw_free) |
| 897 | machine->ops->hw_free(substream); |
| 898 | |
| 899 | /* free any DMA resources */ |
| 900 | if (platform->pcm_ops->hw_free) |
| 901 | platform->pcm_ops->hw_free(substream); |
| 902 | |
| 903 | /* now free hw params for the DAI's */ |
| 904 | if (rtd->codec_dai->ops.hw_free) |
| 905 | rtd->codec_dai->ops.hw_free(substream); |
| 906 | |
| 907 | if (rtd->cpu_dai->ops.hw_free) |
| 908 | rtd->cpu_dai->ops.hw_free(substream); |
| 909 | |
| 910 | mutex_unlock(&pcm_mutex); |
| 911 | return 0; |
| 912 | } |
| 913 | |
| 914 | static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 915 | { |
| 916 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 917 | struct snd_soc_device *socdev = rtd->socdev; |
| 918 | struct snd_soc_platform *platform = socdev->platform; |
| 919 | int ret; |
| 920 | |
| 921 | if (rtd->codec_dai->ops.trigger) { |
| 922 | ret = rtd->codec_dai->ops.trigger(substream, cmd); |
| 923 | if (ret < 0) |
| 924 | return ret; |
| 925 | } |
| 926 | |
| 927 | if (platform->pcm_ops->trigger) { |
| 928 | ret = platform->pcm_ops->trigger(substream, cmd); |
| 929 | if (ret < 0) |
| 930 | return ret; |
| 931 | } |
| 932 | |
| 933 | if (rtd->cpu_dai->ops.trigger) { |
| 934 | ret = rtd->cpu_dai->ops.trigger(substream, cmd); |
| 935 | if (ret < 0) |
| 936 | return ret; |
| 937 | } |
| 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | /* ASoC PCM operations */ |
| 942 | static struct snd_pcm_ops soc_pcm_ops = { |
| 943 | .open = soc_pcm_open, |
| 944 | .close = soc_codec_close, |
| 945 | .hw_params = soc_pcm_hw_params, |
| 946 | .hw_free = soc_pcm_hw_free, |
| 947 | .prepare = soc_pcm_prepare, |
| 948 | .trigger = soc_pcm_trigger, |
| 949 | }; |
| 950 | |
| 951 | #ifdef CONFIG_PM |
| 952 | /* powers down audio subsystem for suspend */ |
| 953 | static int soc_suspend(struct platform_device *pdev, pm_message_t state) |
| 954 | { |
| 955 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 956 | struct snd_soc_machine *machine = socdev->machine; |
| 957 | struct snd_soc_platform *platform = socdev->platform; |
| 958 | struct snd_soc_codec_device *codec_dev = socdev->codec_dev; |
| 959 | struct snd_soc_codec *codec = socdev->codec; |
| 960 | int i; |
| 961 | |
| 962 | /* mute any active DAC's */ |
| 963 | for(i = 0; i < machine->num_links; i++) { |
| 964 | struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai; |
| 965 | if (dai->digital_mute && dai->playback.active) |
| 966 | dai->digital_mute(codec, dai, 1); |
| 967 | } |
| 968 | |
| 969 | if (machine->suspend_pre) |
| 970 | machine->suspend_pre(pdev, state); |
| 971 | |
| 972 | for(i = 0; i < machine->num_links; i++) { |
| 973 | struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai; |
| 974 | if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97) |
| 975 | cpu_dai->suspend(pdev, cpu_dai); |
| 976 | if (platform->suspend) |
| 977 | platform->suspend(pdev, cpu_dai); |
| 978 | } |
| 979 | |
| 980 | /* close any waiting streams and save state */ |
| 981 | flush_workqueue(soc_workq); |
| 982 | codec->suspend_dapm_state = codec->dapm_state; |
| 983 | |
| 984 | for(i = 0; i < codec->num_dai; i++) { |
| 985 | char *stream = codec->dai[i].playback.stream_name; |
| 986 | if (stream != NULL) |
| 987 | snd_soc_dapm_stream_event(codec, stream, |
| 988 | SND_SOC_DAPM_STREAM_SUSPEND); |
| 989 | stream = codec->dai[i].capture.stream_name; |
| 990 | if (stream != NULL) |
| 991 | snd_soc_dapm_stream_event(codec, stream, |
| 992 | SND_SOC_DAPM_STREAM_SUSPEND); |
| 993 | } |
| 994 | |
| 995 | if (codec_dev->suspend) |
| 996 | codec_dev->suspend(pdev, state); |
| 997 | |
| 998 | for(i = 0; i < machine->num_links; i++) { |
| 999 | struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai; |
| 1000 | if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97) |
| 1001 | cpu_dai->suspend(pdev, cpu_dai); |
| 1002 | } |
| 1003 | |
| 1004 | if (machine->suspend_post) |
| 1005 | machine->suspend_post(pdev, state); |
| 1006 | |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
| 1010 | /* powers up audio subsystem after a suspend */ |
| 1011 | static int soc_resume(struct platform_device *pdev) |
| 1012 | { |
| 1013 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 1014 | struct snd_soc_machine *machine = socdev->machine; |
| 1015 | struct snd_soc_platform *platform = socdev->platform; |
| 1016 | struct snd_soc_codec_device *codec_dev = socdev->codec_dev; |
| 1017 | struct snd_soc_codec *codec = socdev->codec; |
| 1018 | int i; |
| 1019 | |
| 1020 | if (machine->resume_pre) |
| 1021 | machine->resume_pre(pdev); |
| 1022 | |
| 1023 | for(i = 0; i < machine->num_links; i++) { |
| 1024 | struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai; |
| 1025 | if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97) |
| 1026 | cpu_dai->resume(pdev, cpu_dai); |
| 1027 | } |
| 1028 | |
| 1029 | if (codec_dev->resume) |
| 1030 | codec_dev->resume(pdev); |
| 1031 | |
| 1032 | for(i = 0; i < codec->num_dai; i++) { |
| 1033 | char* stream = codec->dai[i].playback.stream_name; |
| 1034 | if (stream != NULL) |
| 1035 | snd_soc_dapm_stream_event(codec, stream, |
| 1036 | SND_SOC_DAPM_STREAM_RESUME); |
| 1037 | stream = codec->dai[i].capture.stream_name; |
| 1038 | if (stream != NULL) |
| 1039 | snd_soc_dapm_stream_event(codec, stream, |
| 1040 | SND_SOC_DAPM_STREAM_RESUME); |
| 1041 | } |
| 1042 | |
| 1043 | /* unmute any active DAC's */ |
| 1044 | for(i = 0; i < machine->num_links; i++) { |
| 1045 | struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai; |
| 1046 | if (dai->digital_mute && dai->playback.active) |
| 1047 | dai->digital_mute(codec, dai, 0); |
| 1048 | } |
| 1049 | |
| 1050 | for(i = 0; i < machine->num_links; i++) { |
| 1051 | struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai; |
| 1052 | if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97) |
| 1053 | cpu_dai->resume(pdev, cpu_dai); |
| 1054 | if (platform->resume) |
| 1055 | platform->resume(pdev, cpu_dai); |
| 1056 | } |
| 1057 | |
| 1058 | if (machine->resume_post) |
| 1059 | machine->resume_post(pdev); |
| 1060 | |
| 1061 | return 0; |
| 1062 | } |
| 1063 | |
| 1064 | #else |
| 1065 | #define soc_suspend NULL |
| 1066 | #define soc_resume NULL |
| 1067 | #endif |
| 1068 | |
| 1069 | /* probes a new socdev */ |
| 1070 | static int soc_probe(struct platform_device *pdev) |
| 1071 | { |
| 1072 | int ret = 0, i; |
| 1073 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 1074 | struct snd_soc_machine *machine = socdev->machine; |
| 1075 | struct snd_soc_platform *platform = socdev->platform; |
| 1076 | struct snd_soc_codec_device *codec_dev = socdev->codec_dev; |
| 1077 | |
| 1078 | if (machine->probe) { |
| 1079 | ret = machine->probe(pdev); |
| 1080 | if(ret < 0) |
| 1081 | return ret; |
| 1082 | } |
| 1083 | |
| 1084 | for (i = 0; i < machine->num_links; i++) { |
| 1085 | struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai; |
| 1086 | if (cpu_dai->probe) { |
| 1087 | ret = cpu_dai->probe(pdev); |
| 1088 | if(ret < 0) |
| 1089 | goto cpu_dai_err; |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | if (codec_dev->probe) { |
| 1094 | ret = codec_dev->probe(pdev); |
| 1095 | if(ret < 0) |
| 1096 | goto cpu_dai_err; |
| 1097 | } |
| 1098 | |
| 1099 | if (platform->probe) { |
| 1100 | ret = platform->probe(pdev); |
| 1101 | if(ret < 0) |
| 1102 | goto platform_err; |
| 1103 | } |
| 1104 | |
| 1105 | /* DAPM stream work */ |
| 1106 | soc_workq = create_workqueue("kdapm"); |
| 1107 | if (soc_workq == NULL) |
| 1108 | goto work_err; |
| 1109 | INIT_WORK(&soc_stream_work, close_delayed_work, socdev); |
| 1110 | return 0; |
| 1111 | |
| 1112 | work_err: |
| 1113 | if (platform->remove) |
| 1114 | platform->remove(pdev); |
| 1115 | |
| 1116 | platform_err: |
| 1117 | if (codec_dev->remove) |
| 1118 | codec_dev->remove(pdev); |
| 1119 | |
| 1120 | cpu_dai_err: |
| 1121 | for (i--; i > 0; i--) { |
| 1122 | struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai; |
| 1123 | if (cpu_dai->remove) |
| 1124 | cpu_dai->remove(pdev); |
| 1125 | } |
| 1126 | |
| 1127 | if (machine->remove) |
| 1128 | machine->remove(pdev); |
| 1129 | |
| 1130 | return ret; |
| 1131 | } |
| 1132 | |
| 1133 | /* removes a socdev */ |
| 1134 | static int soc_remove(struct platform_device *pdev) |
| 1135 | { |
| 1136 | int i; |
| 1137 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 1138 | struct snd_soc_machine *machine = socdev->machine; |
| 1139 | struct snd_soc_platform *platform = socdev->platform; |
| 1140 | struct snd_soc_codec_device *codec_dev = socdev->codec_dev; |
| 1141 | |
| 1142 | if (soc_workq) |
| 1143 | destroy_workqueue(soc_workq); |
| 1144 | |
| 1145 | if (platform->remove) |
| 1146 | platform->remove(pdev); |
| 1147 | |
| 1148 | if (codec_dev->remove) |
| 1149 | codec_dev->remove(pdev); |
| 1150 | |
| 1151 | for (i = 0; i < machine->num_links; i++) { |
| 1152 | struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai; |
| 1153 | if (cpu_dai->remove) |
| 1154 | cpu_dai->remove(pdev); |
| 1155 | } |
| 1156 | |
| 1157 | if (machine->remove) |
| 1158 | machine->remove(pdev); |
| 1159 | |
| 1160 | return 0; |
| 1161 | } |
| 1162 | |
| 1163 | /* ASoC platform driver */ |
| 1164 | static struct platform_driver soc_driver = { |
| 1165 | .driver = { |
| 1166 | .name = "soc-audio", |
| 1167 | }, |
| 1168 | .probe = soc_probe, |
| 1169 | .remove = soc_remove, |
| 1170 | .suspend = soc_suspend, |
| 1171 | .resume = soc_resume, |
| 1172 | }; |
| 1173 | |
| 1174 | /* create a new pcm */ |
| 1175 | static int soc_new_pcm(struct snd_soc_device *socdev, |
| 1176 | struct snd_soc_dai_link *dai_link, int num) |
| 1177 | { |
| 1178 | struct snd_soc_codec *codec = socdev->codec; |
| 1179 | struct snd_soc_codec_dai *codec_dai = dai_link->codec_dai; |
| 1180 | struct snd_soc_cpu_dai *cpu_dai = dai_link->cpu_dai; |
| 1181 | struct snd_soc_pcm_runtime *rtd; |
| 1182 | struct snd_pcm *pcm; |
| 1183 | char new_name[64]; |
| 1184 | int ret = 0, playback = 0, capture = 0; |
| 1185 | |
| 1186 | rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL); |
| 1187 | if (rtd == NULL) |
| 1188 | return -ENOMEM; |
| 1189 | rtd->cpu_dai = cpu_dai; |
| 1190 | rtd->codec_dai = codec_dai; |
| 1191 | rtd->socdev = socdev; |
| 1192 | |
| 1193 | /* check client and interface hw capabilities */ |
| 1194 | sprintf(new_name, "%s %s-%s-%d",dai_link->stream_name, codec_dai->name, |
| 1195 | get_dai_name(cpu_dai->type), num); |
| 1196 | |
| 1197 | if (codec_dai->playback.channels_min) |
| 1198 | playback = 1; |
| 1199 | if (codec_dai->capture.channels_min) |
| 1200 | capture = 1; |
| 1201 | |
| 1202 | ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback, |
| 1203 | capture, &pcm); |
| 1204 | if (ret < 0) { |
| 1205 | printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name); |
| 1206 | kfree(rtd); |
| 1207 | return ret; |
| 1208 | } |
| 1209 | |
| 1210 | pcm->private_data = rtd; |
| 1211 | soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap; |
| 1212 | soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer; |
| 1213 | soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl; |
| 1214 | soc_pcm_ops.copy = socdev->platform->pcm_ops->copy; |
| 1215 | soc_pcm_ops.silence = socdev->platform->pcm_ops->silence; |
| 1216 | soc_pcm_ops.ack = socdev->platform->pcm_ops->ack; |
| 1217 | soc_pcm_ops.page = socdev->platform->pcm_ops->page; |
| 1218 | |
| 1219 | if (playback) |
| 1220 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops); |
| 1221 | |
| 1222 | if (capture) |
| 1223 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops); |
| 1224 | |
| 1225 | ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm); |
| 1226 | if (ret < 0) { |
| 1227 | printk(KERN_ERR "asoc: platform pcm constructor failed\n"); |
| 1228 | kfree(rtd); |
| 1229 | return ret; |
| 1230 | } |
| 1231 | |
| 1232 | pcm->private_free = socdev->platform->pcm_free; |
| 1233 | printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name, |
| 1234 | cpu_dai->name); |
| 1235 | return ret; |
| 1236 | } |
| 1237 | |
| 1238 | /* codec register dump */ |
| 1239 | static ssize_t codec_reg_show(struct device *dev, |
| 1240 | struct device_attribute *attr, char *buf) |
| 1241 | { |
| 1242 | struct snd_soc_device *devdata = dev_get_drvdata(dev); |
| 1243 | struct snd_soc_codec *codec = devdata->codec; |
| 1244 | int i, step = 1, count = 0; |
| 1245 | |
| 1246 | if (!codec->reg_cache_size) |
| 1247 | return 0; |
| 1248 | |
| 1249 | if (codec->reg_cache_step) |
| 1250 | step = codec->reg_cache_step; |
| 1251 | |
| 1252 | count += sprintf(buf, "%s registers\n", codec->name); |
| 1253 | for(i = 0; i < codec->reg_cache_size; i += step) |
| 1254 | count += sprintf(buf + count, "%2x: %4x\n", i, codec->read(codec, i)); |
| 1255 | |
| 1256 | return count; |
| 1257 | } |
| 1258 | static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL); |
| 1259 | |
| 1260 | /** |
| 1261 | * snd_soc_new_ac97_codec - initailise AC97 device |
| 1262 | * @codec: audio codec |
| 1263 | * @ops: AC97 bus operations |
| 1264 | * @num: AC97 codec number |
| 1265 | * |
| 1266 | * Initialises AC97 codec resources for use by ad-hoc devices only. |
| 1267 | */ |
| 1268 | int snd_soc_new_ac97_codec(struct snd_soc_codec *codec, |
| 1269 | struct snd_ac97_bus_ops *ops, int num) |
| 1270 | { |
| 1271 | mutex_lock(&codec->mutex); |
| 1272 | |
| 1273 | codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL); |
| 1274 | if (codec->ac97 == NULL) { |
| 1275 | mutex_unlock(&codec->mutex); |
| 1276 | return -ENOMEM; |
| 1277 | } |
| 1278 | |
| 1279 | codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL); |
| 1280 | if (codec->ac97->bus == NULL) { |
| 1281 | kfree(codec->ac97); |
| 1282 | codec->ac97 = NULL; |
| 1283 | mutex_unlock(&codec->mutex); |
| 1284 | return -ENOMEM; |
| 1285 | } |
| 1286 | |
| 1287 | codec->ac97->bus->ops = ops; |
| 1288 | codec->ac97->num = num; |
| 1289 | mutex_unlock(&codec->mutex); |
| 1290 | return 0; |
| 1291 | } |
| 1292 | EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec); |
| 1293 | |
| 1294 | /** |
| 1295 | * snd_soc_free_ac97_codec - free AC97 codec device |
| 1296 | * @codec: audio codec |
| 1297 | * |
| 1298 | * Frees AC97 codec device resources. |
| 1299 | */ |
| 1300 | void snd_soc_free_ac97_codec(struct snd_soc_codec *codec) |
| 1301 | { |
| 1302 | mutex_lock(&codec->mutex); |
| 1303 | kfree(codec->ac97->bus); |
| 1304 | kfree(codec->ac97); |
| 1305 | codec->ac97 = NULL; |
| 1306 | mutex_unlock(&codec->mutex); |
| 1307 | } |
| 1308 | EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec); |
| 1309 | |
| 1310 | /** |
| 1311 | * snd_soc_update_bits - update codec register bits |
| 1312 | * @codec: audio codec |
| 1313 | * @reg: codec register |
| 1314 | * @mask: register mask |
| 1315 | * @value: new value |
| 1316 | * |
| 1317 | * Writes new register value. |
| 1318 | * |
| 1319 | * Returns 1 for change else 0. |
| 1320 | */ |
| 1321 | int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg, |
| 1322 | unsigned short mask, unsigned short value) |
| 1323 | { |
| 1324 | int change; |
| 1325 | unsigned short old, new; |
| 1326 | |
| 1327 | mutex_lock(&io_mutex); |
| 1328 | old = snd_soc_read(codec, reg); |
| 1329 | new = (old & ~mask) | value; |
| 1330 | change = old != new; |
| 1331 | if (change) |
| 1332 | snd_soc_write(codec, reg, new); |
| 1333 | |
| 1334 | mutex_unlock(&io_mutex); |
| 1335 | return change; |
| 1336 | } |
| 1337 | EXPORT_SYMBOL_GPL(snd_soc_update_bits); |
| 1338 | |
| 1339 | /** |
| 1340 | * snd_soc_test_bits - test register for change |
| 1341 | * @codec: audio codec |
| 1342 | * @reg: codec register |
| 1343 | * @mask: register mask |
| 1344 | * @value: new value |
| 1345 | * |
| 1346 | * Tests a register with a new value and checks if the new value is |
| 1347 | * different from the old value. |
| 1348 | * |
| 1349 | * Returns 1 for change else 0. |
| 1350 | */ |
| 1351 | int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg, |
| 1352 | unsigned short mask, unsigned short value) |
| 1353 | { |
| 1354 | int change; |
| 1355 | unsigned short old, new; |
| 1356 | |
| 1357 | mutex_lock(&io_mutex); |
| 1358 | old = snd_soc_read(codec, reg); |
| 1359 | new = (old & ~mask) | value; |
| 1360 | change = old != new; |
| 1361 | mutex_unlock(&io_mutex); |
| 1362 | |
| 1363 | return change; |
| 1364 | } |
| 1365 | EXPORT_SYMBOL_GPL(snd_soc_test_bits); |
| 1366 | |
| 1367 | /** |
| 1368 | * snd_soc_get_rate - get int sample rate |
| 1369 | * @hwpcmrate: the hardware pcm rate |
| 1370 | * |
| 1371 | * Returns the audio rate integaer value, else 0. |
| 1372 | */ |
| 1373 | int snd_soc_get_rate(int hwpcmrate) |
| 1374 | { |
| 1375 | int rate = ffs(hwpcmrate) - 1; |
| 1376 | |
| 1377 | if (rate > ARRAY_SIZE(rates)) |
| 1378 | return 0; |
| 1379 | return rates[rate]; |
| 1380 | } |
| 1381 | EXPORT_SYMBOL_GPL(snd_soc_get_rate); |
| 1382 | |
| 1383 | /** |
| 1384 | * snd_soc_new_pcms - create new sound card and pcms |
| 1385 | * @socdev: the SoC audio device |
| 1386 | * |
| 1387 | * Create a new sound card based upon the codec and interface pcms. |
| 1388 | * |
| 1389 | * Returns 0 for success, else error. |
| 1390 | */ |
| 1391 | int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char * xid) |
| 1392 | { |
| 1393 | struct snd_soc_codec *codec = socdev->codec; |
| 1394 | struct snd_soc_machine *machine = socdev->machine; |
| 1395 | int ret = 0, i; |
| 1396 | |
| 1397 | mutex_lock(&codec->mutex); |
| 1398 | |
| 1399 | /* register a sound card */ |
| 1400 | codec->card = snd_card_new(idx, xid, codec->owner, 0); |
| 1401 | if (!codec->card) { |
| 1402 | printk(KERN_ERR "asoc: can't create sound card for codec %s\n", |
| 1403 | codec->name); |
| 1404 | mutex_unlock(&codec->mutex); |
| 1405 | return -ENODEV; |
| 1406 | } |
| 1407 | |
| 1408 | codec->card->dev = socdev->dev; |
| 1409 | codec->card->private_data = codec; |
| 1410 | strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver)); |
| 1411 | |
| 1412 | /* create the pcms */ |
| 1413 | for(i = 0; i < machine->num_links; i++) { |
| 1414 | ret = soc_new_pcm(socdev, &machine->dai_link[i], i); |
| 1415 | if (ret < 0) { |
| 1416 | printk(KERN_ERR "asoc: can't create pcm %s\n", |
| 1417 | machine->dai_link[i].stream_name); |
| 1418 | mutex_unlock(&codec->mutex); |
| 1419 | return ret; |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | mutex_unlock(&codec->mutex); |
| 1424 | return ret; |
| 1425 | } |
| 1426 | EXPORT_SYMBOL_GPL(snd_soc_new_pcms); |
| 1427 | |
| 1428 | /** |
| 1429 | * snd_soc_register_card - register sound card |
| 1430 | * @socdev: the SoC audio device |
| 1431 | * |
| 1432 | * Register a SoC sound card. Also registers an AC97 device if the |
| 1433 | * codec is AC97 for ad hoc devices. |
| 1434 | * |
| 1435 | * Returns 0 for success, else error. |
| 1436 | */ |
| 1437 | int snd_soc_register_card(struct snd_soc_device *socdev) |
| 1438 | { |
| 1439 | struct snd_soc_codec *codec = socdev->codec; |
| 1440 | struct snd_soc_machine *machine = socdev->machine; |
| 1441 | int ret = 0, i, ac97 = 0; |
| 1442 | |
| 1443 | mutex_lock(&codec->mutex); |
| 1444 | for(i = 0; i < machine->num_links; i++) { |
| 1445 | if (socdev->machine->dai_link[i].init) |
| 1446 | socdev->machine->dai_link[i].init(codec); |
| 1447 | if (socdev->machine->dai_link[i].cpu_dai->type == SND_SOC_DAI_AC97) |
| 1448 | ac97 = 1; |
| 1449 | } |
| 1450 | snprintf(codec->card->shortname, sizeof(codec->card->shortname), |
| 1451 | "%s", machine->name); |
| 1452 | snprintf(codec->card->longname, sizeof(codec->card->longname), |
| 1453 | "%s (%s)", machine->name, codec->name); |
| 1454 | |
| 1455 | ret = snd_card_register(codec->card); |
| 1456 | if (ret < 0) { |
| 1457 | printk(KERN_ERR "asoc: failed to register soundcard for codec %s\n", |
| 1458 | codec->name); |
| 1459 | mutex_unlock(&codec->mutex); |
| 1460 | return ret; |
| 1461 | } |
| 1462 | |
| 1463 | #ifdef CONFIG_SND_SOC_AC97_BUS |
| 1464 | if (ac97) |
| 1465 | soc_ac97_dev_register(codec); |
| 1466 | #endif |
| 1467 | |
| 1468 | snd_soc_dapm_sys_add(socdev->dev); |
| 1469 | device_create_file(socdev->dev, &dev_attr_codec_reg); |
| 1470 | mutex_unlock(&codec->mutex); |
| 1471 | return ret; |
| 1472 | } |
| 1473 | EXPORT_SYMBOL_GPL(snd_soc_register_card); |
| 1474 | |
| 1475 | /** |
| 1476 | * snd_soc_free_pcms - free sound card and pcms |
| 1477 | * @socdev: the SoC audio device |
| 1478 | * |
| 1479 | * Frees sound card and pcms associated with the socdev. |
| 1480 | * Also unregister the codec if it is an AC97 device. |
| 1481 | */ |
| 1482 | void snd_soc_free_pcms(struct snd_soc_device *socdev) |
| 1483 | { |
| 1484 | struct snd_soc_codec *codec = socdev->codec; |
| 1485 | |
| 1486 | mutex_lock(&codec->mutex); |
| 1487 | #ifdef CONFIG_SND_SOC_AC97_BUS |
| 1488 | if (codec->ac97) |
| 1489 | soc_ac97_dev_unregister(codec); |
| 1490 | #endif |
| 1491 | |
| 1492 | if (codec->card) |
| 1493 | snd_card_free(codec->card); |
| 1494 | device_remove_file(socdev->dev, &dev_attr_codec_reg); |
| 1495 | mutex_unlock(&codec->mutex); |
| 1496 | } |
| 1497 | EXPORT_SYMBOL_GPL(snd_soc_free_pcms); |
| 1498 | |
| 1499 | /** |
| 1500 | * snd_soc_set_runtime_hwparams - set the runtime hardware parameters |
| 1501 | * @substream: the pcm substream |
| 1502 | * @hw: the hardware parameters |
| 1503 | * |
| 1504 | * Sets the substream runtime hardware parameters. |
| 1505 | */ |
| 1506 | int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream, |
| 1507 | const struct snd_pcm_hardware *hw) |
| 1508 | { |
| 1509 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1510 | runtime->hw.info = hw->info; |
| 1511 | runtime->hw.formats = hw->formats; |
| 1512 | runtime->hw.period_bytes_min = hw->period_bytes_min; |
| 1513 | runtime->hw.period_bytes_max = hw->period_bytes_max; |
| 1514 | runtime->hw.periods_min = hw->periods_min; |
| 1515 | runtime->hw.periods_max = hw->periods_max; |
| 1516 | runtime->hw.buffer_bytes_max = hw->buffer_bytes_max; |
| 1517 | runtime->hw.fifo_size = hw->fifo_size; |
| 1518 | return 0; |
| 1519 | } |
| 1520 | EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams); |
| 1521 | |
| 1522 | /** |
| 1523 | * snd_soc_cnew - create new control |
| 1524 | * @_template: control template |
| 1525 | * @data: control private data |
| 1526 | * @lnng_name: control long name |
| 1527 | * |
| 1528 | * Create a new mixer control from a template control. |
| 1529 | * |
| 1530 | * Returns 0 for success, else error. |
| 1531 | */ |
| 1532 | struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, |
| 1533 | void *data, char *long_name) |
| 1534 | { |
| 1535 | struct snd_kcontrol_new template; |
| 1536 | |
| 1537 | memcpy(&template, _template, sizeof(template)); |
| 1538 | if (long_name) |
| 1539 | template.name = long_name; |
| 1540 | template.access = SNDRV_CTL_ELEM_ACCESS_READWRITE; |
| 1541 | template.index = 0; |
| 1542 | |
| 1543 | return snd_ctl_new1(&template, data); |
| 1544 | } |
| 1545 | EXPORT_SYMBOL_GPL(snd_soc_cnew); |
| 1546 | |
| 1547 | /** |
| 1548 | * snd_soc_info_enum_double - enumerated double mixer info callback |
| 1549 | * @kcontrol: mixer control |
| 1550 | * @uinfo: control element information |
| 1551 | * |
| 1552 | * Callback to provide information about a double enumerated |
| 1553 | * mixer control. |
| 1554 | * |
| 1555 | * Returns 0 for success. |
| 1556 | */ |
| 1557 | int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol, |
| 1558 | struct snd_ctl_elem_info *uinfo) |
| 1559 | { |
| 1560 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
| 1561 | |
| 1562 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 1563 | uinfo->count = e->shift_l == e->shift_r ? 1 : 2; |
| 1564 | uinfo->value.enumerated.items = e->mask; |
| 1565 | |
| 1566 | if (uinfo->value.enumerated.item > e->mask - 1) |
| 1567 | uinfo->value.enumerated.item = e->mask - 1; |
| 1568 | strcpy(uinfo->value.enumerated.name, |
| 1569 | e->texts[uinfo->value.enumerated.item]); |
| 1570 | return 0; |
| 1571 | } |
| 1572 | EXPORT_SYMBOL_GPL(snd_soc_info_enum_double); |
| 1573 | |
| 1574 | /** |
| 1575 | * snd_soc_get_enum_double - enumerated double mixer get callback |
| 1576 | * @kcontrol: mixer control |
| 1577 | * @uinfo: control element information |
| 1578 | * |
| 1579 | * Callback to get the value of a double enumerated mixer. |
| 1580 | * |
| 1581 | * Returns 0 for success. |
| 1582 | */ |
| 1583 | int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol, |
| 1584 | struct snd_ctl_elem_value *ucontrol) |
| 1585 | { |
| 1586 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1587 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
| 1588 | unsigned short val, bitmask; |
| 1589 | |
| 1590 | for (bitmask = 1; bitmask < e->mask; bitmask <<= 1) |
| 1591 | ; |
| 1592 | val = snd_soc_read(codec, e->reg); |
| 1593 | ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1); |
| 1594 | if (e->shift_l != e->shift_r) |
| 1595 | ucontrol->value.enumerated.item[1] = |
| 1596 | (val >> e->shift_r) & (bitmask - 1); |
| 1597 | |
| 1598 | return 0; |
| 1599 | } |
| 1600 | EXPORT_SYMBOL_GPL(snd_soc_get_enum_double); |
| 1601 | |
| 1602 | /** |
| 1603 | * snd_soc_put_enum_double - enumerated double mixer put callback |
| 1604 | * @kcontrol: mixer control |
| 1605 | * @uinfo: control element information |
| 1606 | * |
| 1607 | * Callback to set the value of a double enumerated mixer. |
| 1608 | * |
| 1609 | * Returns 0 for success. |
| 1610 | */ |
| 1611 | int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol, |
| 1612 | struct snd_ctl_elem_value *ucontrol) |
| 1613 | { |
| 1614 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1615 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
| 1616 | unsigned short val; |
| 1617 | unsigned short mask, bitmask; |
| 1618 | |
| 1619 | for (bitmask = 1; bitmask < e->mask; bitmask <<= 1) |
| 1620 | ; |
| 1621 | if (ucontrol->value.enumerated.item[0] > e->mask - 1) |
| 1622 | return -EINVAL; |
| 1623 | val = ucontrol->value.enumerated.item[0] << e->shift_l; |
| 1624 | mask = (bitmask - 1) << e->shift_l; |
| 1625 | if (e->shift_l != e->shift_r) { |
| 1626 | if (ucontrol->value.enumerated.item[1] > e->mask - 1) |
| 1627 | return -EINVAL; |
| 1628 | val |= ucontrol->value.enumerated.item[1] << e->shift_r; |
| 1629 | mask |= (bitmask - 1) << e->shift_r; |
| 1630 | } |
| 1631 | |
| 1632 | return snd_soc_update_bits(codec, e->reg, mask, val); |
| 1633 | } |
| 1634 | EXPORT_SYMBOL_GPL(snd_soc_put_enum_double); |
| 1635 | |
| 1636 | /** |
| 1637 | * snd_soc_info_enum_ext - external enumerated single mixer info callback |
| 1638 | * @kcontrol: mixer control |
| 1639 | * @uinfo: control element information |
| 1640 | * |
| 1641 | * Callback to provide information about an external enumerated |
| 1642 | * single mixer. |
| 1643 | * |
| 1644 | * Returns 0 for success. |
| 1645 | */ |
| 1646 | int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol, |
| 1647 | struct snd_ctl_elem_info *uinfo) |
| 1648 | { |
| 1649 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
| 1650 | |
| 1651 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 1652 | uinfo->count = 1; |
| 1653 | uinfo->value.enumerated.items = e->mask; |
| 1654 | |
| 1655 | if (uinfo->value.enumerated.item > e->mask - 1) |
| 1656 | uinfo->value.enumerated.item = e->mask - 1; |
| 1657 | strcpy(uinfo->value.enumerated.name, |
| 1658 | e->texts[uinfo->value.enumerated.item]); |
| 1659 | return 0; |
| 1660 | } |
| 1661 | EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext); |
| 1662 | |
| 1663 | /** |
| 1664 | * snd_soc_info_volsw_ext - external single mixer info callback |
| 1665 | * @kcontrol: mixer control |
| 1666 | * @uinfo: control element information |
| 1667 | * |
| 1668 | * Callback to provide information about a single external mixer control. |
| 1669 | * |
| 1670 | * Returns 0 for success. |
| 1671 | */ |
| 1672 | int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol, |
| 1673 | struct snd_ctl_elem_info *uinfo) |
| 1674 | { |
| 1675 | int mask = kcontrol->private_value; |
| 1676 | |
| 1677 | uinfo->type = |
| 1678 | mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 1679 | uinfo->count = 1; |
| 1680 | uinfo->value.integer.min = 0; |
| 1681 | uinfo->value.integer.max = mask; |
| 1682 | return 0; |
| 1683 | } |
| 1684 | EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext); |
| 1685 | |
| 1686 | /** |
| 1687 | * snd_soc_info_bool_ext - external single boolean mixer info callback |
| 1688 | * @kcontrol: mixer control |
| 1689 | * @uinfo: control element information |
| 1690 | * |
| 1691 | * Callback to provide information about a single boolean external mixer control. |
| 1692 | * |
| 1693 | * Returns 0 for success. |
| 1694 | */ |
| 1695 | int snd_soc_info_bool_ext(struct snd_kcontrol *kcontrol, |
| 1696 | struct snd_ctl_elem_info *uinfo) |
| 1697 | { |
| 1698 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 1699 | uinfo->count = 1; |
| 1700 | uinfo->value.integer.min = 0; |
| 1701 | uinfo->value.integer.max = 1; |
| 1702 | return 0; |
| 1703 | } |
| 1704 | EXPORT_SYMBOL_GPL(snd_soc_info_bool_ext); |
| 1705 | |
| 1706 | /** |
| 1707 | * snd_soc_info_volsw - single mixer info callback |
| 1708 | * @kcontrol: mixer control |
| 1709 | * @uinfo: control element information |
| 1710 | * |
| 1711 | * Callback to provide information about a single mixer control. |
| 1712 | * |
| 1713 | * Returns 0 for success. |
| 1714 | */ |
| 1715 | int snd_soc_info_volsw(struct snd_kcontrol *kcontrol, |
| 1716 | struct snd_ctl_elem_info *uinfo) |
| 1717 | { |
| 1718 | int mask = (kcontrol->private_value >> 16) & 0xff; |
| 1719 | int shift = (kcontrol->private_value >> 8) & 0x0f; |
| 1720 | int rshift = (kcontrol->private_value >> 12) & 0x0f; |
| 1721 | |
| 1722 | uinfo->type = |
| 1723 | mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 1724 | uinfo->count = shift == rshift ? 1 : 2; |
| 1725 | uinfo->value.integer.min = 0; |
| 1726 | uinfo->value.integer.max = mask; |
| 1727 | return 0; |
| 1728 | } |
| 1729 | EXPORT_SYMBOL_GPL(snd_soc_info_volsw); |
| 1730 | |
| 1731 | /** |
| 1732 | * snd_soc_get_volsw - single mixer get callback |
| 1733 | * @kcontrol: mixer control |
| 1734 | * @uinfo: control element information |
| 1735 | * |
| 1736 | * Callback to get the value of a single mixer control. |
| 1737 | * |
| 1738 | * Returns 0 for success. |
| 1739 | */ |
| 1740 | int snd_soc_get_volsw(struct snd_kcontrol *kcontrol, |
| 1741 | struct snd_ctl_elem_value *ucontrol) |
| 1742 | { |
| 1743 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1744 | int reg = kcontrol->private_value & 0xff; |
| 1745 | int shift = (kcontrol->private_value >> 8) & 0x0f; |
| 1746 | int rshift = (kcontrol->private_value >> 12) & 0x0f; |
| 1747 | int mask = (kcontrol->private_value >> 16) & 0xff; |
| 1748 | int invert = (kcontrol->private_value >> 24) & 0x01; |
| 1749 | |
| 1750 | ucontrol->value.integer.value[0] = |
| 1751 | (snd_soc_read(codec, reg) >> shift) & mask; |
| 1752 | if (shift != rshift) |
| 1753 | ucontrol->value.integer.value[1] = |
| 1754 | (snd_soc_read(codec, reg) >> rshift) & mask; |
| 1755 | if (invert) { |
| 1756 | ucontrol->value.integer.value[0] = |
| 1757 | mask - ucontrol->value.integer.value[0]; |
| 1758 | if (shift != rshift) |
| 1759 | ucontrol->value.integer.value[1] = |
| 1760 | mask - ucontrol->value.integer.value[1]; |
| 1761 | } |
| 1762 | |
| 1763 | return 0; |
| 1764 | } |
| 1765 | EXPORT_SYMBOL_GPL(snd_soc_get_volsw); |
| 1766 | |
| 1767 | /** |
| 1768 | * snd_soc_put_volsw - single mixer put callback |
| 1769 | * @kcontrol: mixer control |
| 1770 | * @uinfo: control element information |
| 1771 | * |
| 1772 | * Callback to set the value of a single mixer control. |
| 1773 | * |
| 1774 | * Returns 0 for success. |
| 1775 | */ |
| 1776 | int snd_soc_put_volsw(struct snd_kcontrol *kcontrol, |
| 1777 | struct snd_ctl_elem_value *ucontrol) |
| 1778 | { |
| 1779 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1780 | int reg = kcontrol->private_value & 0xff; |
| 1781 | int shift = (kcontrol->private_value >> 8) & 0x0f; |
| 1782 | int rshift = (kcontrol->private_value >> 12) & 0x0f; |
| 1783 | int mask = (kcontrol->private_value >> 16) & 0xff; |
| 1784 | int invert = (kcontrol->private_value >> 24) & 0x01; |
| 1785 | int err; |
| 1786 | unsigned short val, val2, val_mask; |
| 1787 | |
| 1788 | val = (ucontrol->value.integer.value[0] & mask); |
| 1789 | if (invert) |
| 1790 | val = mask - val; |
| 1791 | val_mask = mask << shift; |
| 1792 | val = val << shift; |
| 1793 | if (shift != rshift) { |
| 1794 | val2 = (ucontrol->value.integer.value[1] & mask); |
| 1795 | if (invert) |
| 1796 | val2 = mask - val2; |
| 1797 | val_mask |= mask << rshift; |
| 1798 | val |= val2 << rshift; |
| 1799 | } |
| 1800 | err = snd_soc_update_bits(codec, reg, val_mask, val); |
| 1801 | return err; |
| 1802 | } |
| 1803 | EXPORT_SYMBOL_GPL(snd_soc_put_volsw); |
| 1804 | |
| 1805 | /** |
| 1806 | * snd_soc_info_volsw_2r - double mixer info callback |
| 1807 | * @kcontrol: mixer control |
| 1808 | * @uinfo: control element information |
| 1809 | * |
| 1810 | * Callback to provide information about a double mixer control that |
| 1811 | * spans 2 codec registers. |
| 1812 | * |
| 1813 | * Returns 0 for success. |
| 1814 | */ |
| 1815 | int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol, |
| 1816 | struct snd_ctl_elem_info *uinfo) |
| 1817 | { |
| 1818 | int mask = (kcontrol->private_value >> 12) & 0xff; |
| 1819 | |
| 1820 | uinfo->type = |
| 1821 | mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 1822 | uinfo->count = 2; |
| 1823 | uinfo->value.integer.min = 0; |
| 1824 | uinfo->value.integer.max = mask; |
| 1825 | return 0; |
| 1826 | } |
| 1827 | EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r); |
| 1828 | |
| 1829 | /** |
| 1830 | * snd_soc_get_volsw_2r - double mixer get callback |
| 1831 | * @kcontrol: mixer control |
| 1832 | * @uinfo: control element information |
| 1833 | * |
| 1834 | * Callback to get the value of a double mixer control that spans 2 registers. |
| 1835 | * |
| 1836 | * Returns 0 for success. |
| 1837 | */ |
| 1838 | int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol, |
| 1839 | struct snd_ctl_elem_value *ucontrol) |
| 1840 | { |
| 1841 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1842 | int reg = kcontrol->private_value & 0xff; |
| 1843 | int reg2 = (kcontrol->private_value >> 24) & 0xff; |
| 1844 | int shift = (kcontrol->private_value >> 8) & 0x0f; |
| 1845 | int mask = (kcontrol->private_value >> 12) & 0xff; |
| 1846 | int invert = (kcontrol->private_value >> 20) & 0x01; |
| 1847 | |
| 1848 | ucontrol->value.integer.value[0] = |
| 1849 | (snd_soc_read(codec, reg) >> shift) & mask; |
| 1850 | ucontrol->value.integer.value[1] = |
| 1851 | (snd_soc_read(codec, reg2) >> shift) & mask; |
| 1852 | if (invert) { |
| 1853 | ucontrol->value.integer.value[0] = |
| 1854 | mask - ucontrol->value.integer.value[0]; |
| 1855 | ucontrol->value.integer.value[1] = |
| 1856 | mask - ucontrol->value.integer.value[1]; |
| 1857 | } |
| 1858 | |
| 1859 | return 0; |
| 1860 | } |
| 1861 | EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r); |
| 1862 | |
| 1863 | /** |
| 1864 | * snd_soc_put_volsw_2r - double mixer set callback |
| 1865 | * @kcontrol: mixer control |
| 1866 | * @uinfo: control element information |
| 1867 | * |
| 1868 | * Callback to set the value of a double mixer control that spans 2 registers. |
| 1869 | * |
| 1870 | * Returns 0 for success. |
| 1871 | */ |
| 1872 | int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol, |
| 1873 | struct snd_ctl_elem_value *ucontrol) |
| 1874 | { |
| 1875 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1876 | int reg = kcontrol->private_value & 0xff; |
| 1877 | int reg2 = (kcontrol->private_value >> 24) & 0xff; |
| 1878 | int shift = (kcontrol->private_value >> 8) & 0x0f; |
| 1879 | int mask = (kcontrol->private_value >> 12) & 0xff; |
| 1880 | int invert = (kcontrol->private_value >> 20) & 0x01; |
| 1881 | int err; |
| 1882 | unsigned short val, val2, val_mask; |
| 1883 | |
| 1884 | val_mask = mask << shift; |
| 1885 | val = (ucontrol->value.integer.value[0] & mask); |
| 1886 | val2 = (ucontrol->value.integer.value[1] & mask); |
| 1887 | |
| 1888 | if (invert) { |
| 1889 | val = mask - val; |
| 1890 | val2 = mask - val2; |
| 1891 | } |
| 1892 | |
| 1893 | val = val << shift; |
| 1894 | val2 = val2 << shift; |
| 1895 | |
| 1896 | if ((err = snd_soc_update_bits(codec, reg, val_mask, val)) < 0) |
| 1897 | return err; |
| 1898 | |
| 1899 | err = snd_soc_update_bits(codec, reg2, val_mask, val2); |
| 1900 | return err; |
| 1901 | } |
| 1902 | EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r); |
| 1903 | |
| 1904 | static int __devinit snd_soc_init(void) |
| 1905 | { |
| 1906 | printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION); |
| 1907 | return platform_driver_register(&soc_driver); |
| 1908 | } |
| 1909 | |
| 1910 | static void snd_soc_exit(void) |
| 1911 | { |
| 1912 | platform_driver_unregister(&soc_driver); |
| 1913 | } |
| 1914 | |
| 1915 | module_init(snd_soc_init); |
| 1916 | module_exit(snd_soc_exit); |
| 1917 | |
| 1918 | /* Module information */ |
| 1919 | MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com"); |
| 1920 | MODULE_DESCRIPTION("ALSA SoC Core"); |
| 1921 | MODULE_LICENSE("GPL"); |