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. |
Liam Girdwood | 0664d88 | 2006-12-18 14:39:02 +0100 | [diff] [blame] | 5 | * Copyright 2005 Openedhand Ltd. |
| 6 | * |
Liam Girdwood | d331124 | 2008-10-12 13:17:36 +0100 | [diff] [blame] | 7 | * Author: Liam Girdwood <lrg@slimlogic.co.uk> |
Liam Girdwood | 0664d88 | 2006-12-18 14:39:02 +0100 | [diff] [blame] | 8 | * with code, comments and ideas from :- |
| 9 | * Richard Purdie <richard@openedhand.com> |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License as published by the |
| 13 | * Free Software Foundation; either version 2 of the License, or (at your |
| 14 | * option) any later version. |
| 15 | * |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 16 | * TODO: |
| 17 | * o Add hw rules to enforce rates, etc. |
| 18 | * o More testing with other codecs/machines. |
| 19 | * o Add more codecs and platforms to ensure good API coverage. |
| 20 | * o Support TDM on PCM and I2S |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/moduleparam.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/pm.h> |
| 28 | #include <linux/bitops.h> |
Troy Kisky | 12ef193 | 2008-10-13 17:42:14 -0700 | [diff] [blame] | 29 | #include <linux/debugfs.h> |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 30 | #include <linux/platform_device.h> |
Marek Vasut | 474828a | 2009-07-22 13:01:03 +0200 | [diff] [blame] | 31 | #include <sound/ac97_codec.h> |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 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 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 39 | static DEFINE_MUTEX(pcm_mutex); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 40 | static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq); |
| 41 | |
Mark Brown | 384c89e | 2008-12-03 17:34:03 +0000 | [diff] [blame] | 42 | #ifdef CONFIG_DEBUG_FS |
| 43 | static struct dentry *debugfs_root; |
| 44 | #endif |
| 45 | |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 46 | static DEFINE_MUTEX(client_mutex); |
| 47 | static LIST_HEAD(card_list); |
Mark Brown | 9115171 | 2008-11-30 23:31:24 +0000 | [diff] [blame] | 48 | static LIST_HEAD(dai_list); |
Mark Brown | 12a48a8c | 2008-12-03 19:40:30 +0000 | [diff] [blame] | 49 | static LIST_HEAD(platform_list); |
Mark Brown | 0d0cf00 | 2008-12-10 14:32:45 +0000 | [diff] [blame] | 50 | static LIST_HEAD(codec_list); |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 51 | |
| 52 | static int snd_soc_register_card(struct snd_soc_card *card); |
| 53 | static int snd_soc_unregister_card(struct snd_soc_card *card); |
| 54 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 55 | /* |
| 56 | * This is a timeout to do a DAPM powerdown after a stream is closed(). |
| 57 | * It can be used to eliminate pops between different playback streams, e.g. |
| 58 | * between two audio tracks. |
| 59 | */ |
| 60 | static int pmdown_time = 5000; |
| 61 | module_param(pmdown_time, int, 0); |
| 62 | MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)"); |
| 63 | |
Liam Girdwood | 965ac42 | 2007-01-31 14:14:57 +0100 | [diff] [blame] | 64 | /* |
| 65 | * This function forces any delayed work to be queued and run. |
| 66 | */ |
| 67 | static int run_delayed_work(struct delayed_work *dwork) |
| 68 | { |
| 69 | int ret; |
| 70 | |
| 71 | /* cancel any work waiting to be queued. */ |
| 72 | ret = cancel_delayed_work(dwork); |
| 73 | |
| 74 | /* if there was any work waiting then we run it now and |
| 75 | * wait for it's completion */ |
| 76 | if (ret) { |
| 77 | schedule_delayed_work(dwork, 0); |
| 78 | flush_scheduled_work(); |
| 79 | } |
| 80 | return ret; |
| 81 | } |
| 82 | |
Mark Brown | 2624d5f | 2009-11-03 21:56:13 +0000 | [diff] [blame] | 83 | /* codec register dump */ |
| 84 | static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf) |
| 85 | { |
| 86 | int i, step = 1, count = 0; |
| 87 | |
| 88 | if (!codec->reg_cache_size) |
| 89 | return 0; |
| 90 | |
| 91 | if (codec->reg_cache_step) |
| 92 | step = codec->reg_cache_step; |
| 93 | |
| 94 | count += sprintf(buf, "%s registers\n", codec->name); |
| 95 | for (i = 0; i < codec->reg_cache_size; i += step) { |
| 96 | if (codec->readable_register && !codec->readable_register(i)) |
| 97 | continue; |
| 98 | |
| 99 | count += sprintf(buf + count, "%2x: ", i); |
| 100 | if (count >= PAGE_SIZE - 1) |
| 101 | break; |
| 102 | |
| 103 | if (codec->display_register) |
| 104 | count += codec->display_register(codec, buf + count, |
| 105 | PAGE_SIZE - count, i); |
| 106 | else |
| 107 | count += snprintf(buf + count, PAGE_SIZE - count, |
| 108 | "%4x", codec->read(codec, i)); |
| 109 | |
| 110 | if (count >= PAGE_SIZE - 1) |
| 111 | break; |
| 112 | |
| 113 | count += snprintf(buf + count, PAGE_SIZE - count, "\n"); |
| 114 | if (count >= PAGE_SIZE - 1) |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | /* Truncate count; min() would cause a warning */ |
| 119 | if (count >= PAGE_SIZE) |
| 120 | count = PAGE_SIZE - 1; |
| 121 | |
| 122 | return count; |
| 123 | } |
| 124 | static ssize_t codec_reg_show(struct device *dev, |
| 125 | struct device_attribute *attr, char *buf) |
| 126 | { |
| 127 | struct snd_soc_device *devdata = dev_get_drvdata(dev); |
| 128 | return soc_codec_reg_show(devdata->card->codec, buf); |
| 129 | } |
| 130 | |
| 131 | static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL); |
| 132 | |
Mark Brown | dbe2140 | 2010-02-12 11:37:24 +0000 | [diff] [blame] | 133 | static ssize_t pmdown_time_show(struct device *dev, |
| 134 | struct device_attribute *attr, char *buf) |
| 135 | { |
| 136 | struct snd_soc_device *socdev = dev_get_drvdata(dev); |
| 137 | struct snd_soc_card *card = socdev->card; |
| 138 | |
Mark Brown | 6c5f1fe | 2010-02-17 14:30:44 +0000 | [diff] [blame] | 139 | return sprintf(buf, "%ld\n", card->pmdown_time); |
Mark Brown | dbe2140 | 2010-02-12 11:37:24 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static ssize_t pmdown_time_set(struct device *dev, |
| 143 | struct device_attribute *attr, |
| 144 | const char *buf, size_t count) |
| 145 | { |
| 146 | struct snd_soc_device *socdev = dev_get_drvdata(dev); |
| 147 | struct snd_soc_card *card = socdev->card; |
| 148 | |
| 149 | strict_strtol(buf, 10, &card->pmdown_time); |
| 150 | |
| 151 | return count; |
| 152 | } |
| 153 | |
| 154 | static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set); |
| 155 | |
Mark Brown | 2624d5f | 2009-11-03 21:56:13 +0000 | [diff] [blame] | 156 | #ifdef CONFIG_DEBUG_FS |
| 157 | static int codec_reg_open_file(struct inode *inode, struct file *file) |
| 158 | { |
| 159 | file->private_data = inode->i_private; |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf, |
| 164 | size_t count, loff_t *ppos) |
| 165 | { |
| 166 | ssize_t ret; |
| 167 | struct snd_soc_codec *codec = file->private_data; |
| 168 | char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 169 | if (!buf) |
| 170 | return -ENOMEM; |
| 171 | ret = soc_codec_reg_show(codec, buf); |
| 172 | if (ret >= 0) |
| 173 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); |
| 174 | kfree(buf); |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | static ssize_t codec_reg_write_file(struct file *file, |
| 179 | const char __user *user_buf, size_t count, loff_t *ppos) |
| 180 | { |
| 181 | char buf[32]; |
| 182 | int buf_size; |
| 183 | char *start = buf; |
| 184 | unsigned long reg, value; |
| 185 | int step = 1; |
| 186 | struct snd_soc_codec *codec = file->private_data; |
| 187 | |
| 188 | buf_size = min(count, (sizeof(buf)-1)); |
| 189 | if (copy_from_user(buf, user_buf, buf_size)) |
| 190 | return -EFAULT; |
| 191 | buf[buf_size] = 0; |
| 192 | |
| 193 | if (codec->reg_cache_step) |
| 194 | step = codec->reg_cache_step; |
| 195 | |
| 196 | while (*start == ' ') |
| 197 | start++; |
| 198 | reg = simple_strtoul(start, &start, 16); |
| 199 | if ((reg >= codec->reg_cache_size) || (reg % step)) |
| 200 | return -EINVAL; |
| 201 | while (*start == ' ') |
| 202 | start++; |
| 203 | if (strict_strtoul(start, 16, &value)) |
| 204 | return -EINVAL; |
| 205 | codec->write(codec, reg, value); |
| 206 | return buf_size; |
| 207 | } |
| 208 | |
| 209 | static const struct file_operations codec_reg_fops = { |
| 210 | .open = codec_reg_open_file, |
| 211 | .read = codec_reg_read_file, |
| 212 | .write = codec_reg_write_file, |
| 213 | }; |
| 214 | |
| 215 | static void soc_init_codec_debugfs(struct snd_soc_codec *codec) |
| 216 | { |
| 217 | char codec_root[128]; |
| 218 | |
| 219 | if (codec->dev) |
| 220 | snprintf(codec_root, sizeof(codec_root), |
| 221 | "%s.%s", codec->name, dev_name(codec->dev)); |
| 222 | else |
| 223 | snprintf(codec_root, sizeof(codec_root), |
| 224 | "%s", codec->name); |
| 225 | |
| 226 | codec->debugfs_codec_root = debugfs_create_dir(codec_root, |
| 227 | debugfs_root); |
| 228 | if (!codec->debugfs_codec_root) { |
| 229 | printk(KERN_WARNING |
| 230 | "ASoC: Failed to create codec debugfs directory\n"); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | codec->debugfs_reg = debugfs_create_file("codec_reg", 0644, |
| 235 | codec->debugfs_codec_root, |
| 236 | codec, &codec_reg_fops); |
| 237 | if (!codec->debugfs_reg) |
| 238 | printk(KERN_WARNING |
| 239 | "ASoC: Failed to create codec register debugfs file\n"); |
| 240 | |
| 241 | codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744, |
| 242 | codec->debugfs_codec_root, |
| 243 | &codec->pop_time); |
| 244 | if (!codec->debugfs_pop_time) |
| 245 | printk(KERN_WARNING |
| 246 | "Failed to create pop time debugfs file\n"); |
| 247 | |
| 248 | codec->debugfs_dapm = debugfs_create_dir("dapm", |
| 249 | codec->debugfs_codec_root); |
| 250 | if (!codec->debugfs_dapm) |
| 251 | printk(KERN_WARNING |
| 252 | "Failed to create DAPM debugfs directory\n"); |
| 253 | |
| 254 | snd_soc_dapm_debugfs_init(codec); |
| 255 | } |
| 256 | |
| 257 | static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) |
| 258 | { |
| 259 | debugfs_remove_recursive(codec->debugfs_codec_root); |
| 260 | } |
| 261 | |
| 262 | #else |
| 263 | |
| 264 | static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec) |
| 265 | { |
| 266 | } |
| 267 | |
| 268 | static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) |
| 269 | { |
| 270 | } |
| 271 | #endif |
| 272 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 273 | #ifdef CONFIG_SND_SOC_AC97_BUS |
| 274 | /* unregister ac97 codec */ |
| 275 | static int soc_ac97_dev_unregister(struct snd_soc_codec *codec) |
| 276 | { |
| 277 | if (codec->ac97->dev.bus) |
| 278 | device_unregister(&codec->ac97->dev); |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | /* stop no dev release warning */ |
| 283 | static void soc_ac97_device_release(struct device *dev){} |
| 284 | |
| 285 | /* register ac97 codec to bus */ |
| 286 | static int soc_ac97_dev_register(struct snd_soc_codec *codec) |
| 287 | { |
| 288 | int err; |
| 289 | |
| 290 | codec->ac97->dev.bus = &ac97_bus_type; |
Mark Brown | 4ac5c61 | 2009-04-01 19:35:01 +0100 | [diff] [blame] | 291 | codec->ac97->dev.parent = codec->card->dev; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 292 | codec->ac97->dev.release = soc_ac97_device_release; |
| 293 | |
Kay Sievers | bb072bf | 2008-11-02 03:50:35 +0100 | [diff] [blame] | 294 | dev_set_name(&codec->ac97->dev, "%d-%d:%s", |
| 295 | codec->card->number, 0, codec->name); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 296 | err = device_register(&codec->ac97->dev); |
| 297 | if (err < 0) { |
| 298 | snd_printk(KERN_ERR "Can't register ac97 bus\n"); |
| 299 | codec->ac97->dev.bus = NULL; |
| 300 | return err; |
| 301 | } |
| 302 | return 0; |
| 303 | } |
| 304 | #endif |
| 305 | |
Mark Brown | 06f409d | 2009-04-07 18:10:13 +0100 | [diff] [blame] | 306 | static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream) |
| 307 | { |
| 308 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 309 | struct snd_soc_device *socdev = rtd->socdev; |
| 310 | struct snd_soc_card *card = socdev->card; |
| 311 | struct snd_soc_dai_link *machine = rtd->dai; |
| 312 | struct snd_soc_dai *cpu_dai = machine->cpu_dai; |
| 313 | struct snd_soc_dai *codec_dai = machine->codec_dai; |
| 314 | int ret; |
| 315 | |
| 316 | if (codec_dai->symmetric_rates || cpu_dai->symmetric_rates || |
| 317 | machine->symmetric_rates) { |
| 318 | dev_dbg(card->dev, "Symmetry forces %dHz rate\n", |
| 319 | machine->rate); |
| 320 | |
| 321 | ret = snd_pcm_hw_constraint_minmax(substream->runtime, |
| 322 | SNDRV_PCM_HW_PARAM_RATE, |
| 323 | machine->rate, |
| 324 | machine->rate); |
| 325 | if (ret < 0) { |
| 326 | dev_err(card->dev, |
| 327 | "Unable to apply rate symmetry constraint: %d\n", ret); |
| 328 | return ret; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 335 | /* |
| 336 | * Called by ALSA when a PCM substream is opened, the runtime->hw record is |
| 337 | * then initialized and any private data can be allocated. This also calls |
| 338 | * startup for the cpu DAI, platform, machine and codec DAI. |
| 339 | */ |
| 340 | static int soc_pcm_open(struct snd_pcm_substream *substream) |
| 341 | { |
| 342 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 343 | struct snd_soc_device *socdev = rtd->socdev; |
Mark Brown | 87689d5 | 2008-12-02 16:01:14 +0000 | [diff] [blame] | 344 | struct snd_soc_card *card = socdev->card; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 345 | struct snd_pcm_runtime *runtime = substream->runtime; |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 346 | struct snd_soc_dai_link *machine = rtd->dai; |
Mark Brown | 87689d5 | 2008-12-02 16:01:14 +0000 | [diff] [blame] | 347 | struct snd_soc_platform *platform = card->platform; |
Liam Girdwood | 3c4b266 | 2008-07-07 16:07:17 +0100 | [diff] [blame] | 348 | struct snd_soc_dai *cpu_dai = machine->cpu_dai; |
| 349 | struct snd_soc_dai *codec_dai = machine->codec_dai; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 350 | int ret = 0; |
| 351 | |
| 352 | mutex_lock(&pcm_mutex); |
| 353 | |
| 354 | /* startup the audio subsystem */ |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 355 | if (cpu_dai->ops->startup) { |
| 356 | ret = cpu_dai->ops->startup(substream, cpu_dai); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 357 | if (ret < 0) { |
| 358 | printk(KERN_ERR "asoc: can't open interface %s\n", |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 359 | cpu_dai->name); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 360 | goto out; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | if (platform->pcm_ops->open) { |
| 365 | ret = platform->pcm_ops->open(substream); |
| 366 | if (ret < 0) { |
| 367 | printk(KERN_ERR "asoc: can't open platform %s\n", platform->name); |
| 368 | goto platform_err; |
| 369 | } |
| 370 | } |
| 371 | |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 372 | if (codec_dai->ops->startup) { |
| 373 | ret = codec_dai->ops->startup(substream, codec_dai); |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 374 | if (ret < 0) { |
| 375 | printk(KERN_ERR "asoc: can't open codec %s\n", |
| 376 | codec_dai->name); |
| 377 | goto codec_dai_err; |
| 378 | } |
| 379 | } |
| 380 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 381 | if (machine->ops && machine->ops->startup) { |
| 382 | ret = machine->ops->startup(substream); |
| 383 | if (ret < 0) { |
| 384 | printk(KERN_ERR "asoc: %s startup failed\n", machine->name); |
| 385 | goto machine_err; |
| 386 | } |
| 387 | } |
| 388 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 389 | /* Check that the codec and cpu DAI's are compatible */ |
| 390 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 391 | runtime->hw.rate_min = |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 392 | max(codec_dai->playback.rate_min, |
| 393 | cpu_dai->playback.rate_min); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 394 | runtime->hw.rate_max = |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 395 | min(codec_dai->playback.rate_max, |
| 396 | cpu_dai->playback.rate_max); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 397 | runtime->hw.channels_min = |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 398 | max(codec_dai->playback.channels_min, |
| 399 | cpu_dai->playback.channels_min); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 400 | runtime->hw.channels_max = |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 401 | min(codec_dai->playback.channels_max, |
| 402 | cpu_dai->playback.channels_max); |
| 403 | runtime->hw.formats = |
| 404 | codec_dai->playback.formats & cpu_dai->playback.formats; |
| 405 | runtime->hw.rates = |
| 406 | codec_dai->playback.rates & cpu_dai->playback.rates; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 407 | } else { |
| 408 | runtime->hw.rate_min = |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 409 | max(codec_dai->capture.rate_min, |
| 410 | cpu_dai->capture.rate_min); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 411 | runtime->hw.rate_max = |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 412 | min(codec_dai->capture.rate_max, |
| 413 | cpu_dai->capture.rate_max); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 414 | runtime->hw.channels_min = |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 415 | max(codec_dai->capture.channels_min, |
| 416 | cpu_dai->capture.channels_min); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 417 | runtime->hw.channels_max = |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 418 | min(codec_dai->capture.channels_max, |
| 419 | cpu_dai->capture.channels_max); |
| 420 | runtime->hw.formats = |
| 421 | codec_dai->capture.formats & cpu_dai->capture.formats; |
| 422 | runtime->hw.rates = |
| 423 | codec_dai->capture.rates & cpu_dai->capture.rates; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | snd_pcm_limit_hw_rates(runtime); |
| 427 | if (!runtime->hw.rates) { |
| 428 | printk(KERN_ERR "asoc: %s <-> %s No matching rates\n", |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 429 | codec_dai->name, cpu_dai->name); |
Jassi Brar | bb1c047 | 2010-02-25 11:24:53 +0900 | [diff] [blame^] | 430 | goto config_err; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 431 | } |
| 432 | if (!runtime->hw.formats) { |
| 433 | printk(KERN_ERR "asoc: %s <-> %s No matching formats\n", |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 434 | codec_dai->name, cpu_dai->name); |
Jassi Brar | bb1c047 | 2010-02-25 11:24:53 +0900 | [diff] [blame^] | 435 | goto config_err; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 436 | } |
| 437 | if (!runtime->hw.channels_min || !runtime->hw.channels_max) { |
| 438 | printk(KERN_ERR "asoc: %s <-> %s No matching channels\n", |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 439 | codec_dai->name, cpu_dai->name); |
Jassi Brar | bb1c047 | 2010-02-25 11:24:53 +0900 | [diff] [blame^] | 440 | goto config_err; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Mark Brown | 06f409d | 2009-04-07 18:10:13 +0100 | [diff] [blame] | 443 | /* Symmetry only applies if we've already got an active stream. */ |
| 444 | if (cpu_dai->active || codec_dai->active) { |
| 445 | ret = soc_pcm_apply_symmetry(substream); |
| 446 | if (ret != 0) |
Jassi Brar | bb1c047 | 2010-02-25 11:24:53 +0900 | [diff] [blame^] | 447 | goto config_err; |
Mark Brown | 06f409d | 2009-04-07 18:10:13 +0100 | [diff] [blame] | 448 | } |
| 449 | |
Mark Brown | f24368c | 2008-10-21 21:45:08 +0100 | [diff] [blame] | 450 | pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name); |
| 451 | pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates); |
| 452 | pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min, |
| 453 | runtime->hw.channels_max); |
| 454 | pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min, |
| 455 | runtime->hw.rate_max); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 456 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 457 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 458 | cpu_dai->playback.active = codec_dai->playback.active = 1; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 459 | else |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 460 | cpu_dai->capture.active = codec_dai->capture.active = 1; |
| 461 | cpu_dai->active = codec_dai->active = 1; |
| 462 | cpu_dai->runtime = runtime; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 463 | card->codec->active++; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 464 | mutex_unlock(&pcm_mutex); |
| 465 | return 0; |
| 466 | |
Jassi Brar | bb1c047 | 2010-02-25 11:24:53 +0900 | [diff] [blame^] | 467 | config_err: |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 468 | if (machine->ops && machine->ops->shutdown) |
| 469 | machine->ops->shutdown(substream); |
| 470 | |
Jassi Brar | bb1c047 | 2010-02-25 11:24:53 +0900 | [diff] [blame^] | 471 | machine_err: |
| 472 | if (codec_dai->ops->shutdown) |
| 473 | codec_dai->ops->shutdown(substream, codec_dai); |
| 474 | |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 475 | codec_dai_err: |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 476 | if (platform->pcm_ops->close) |
| 477 | platform->pcm_ops->close(substream); |
| 478 | |
| 479 | platform_err: |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 480 | if (cpu_dai->ops->shutdown) |
| 481 | cpu_dai->ops->shutdown(substream, cpu_dai); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 482 | out: |
| 483 | mutex_unlock(&pcm_mutex); |
| 484 | return ret; |
| 485 | } |
| 486 | |
| 487 | /* |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 488 | * Power down the audio subsystem pmdown_time msecs after close is called. |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 489 | * This is to ensure there are no pops or clicks in between any music tracks |
| 490 | * due to DAPM power cycling. |
| 491 | */ |
Andrew Morton | 4484bb2 | 2006-12-15 09:30:07 +0100 | [diff] [blame] | 492 | static void close_delayed_work(struct work_struct *work) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 493 | { |
Mark Brown | 6308419 | 2008-12-02 15:08:03 +0000 | [diff] [blame] | 494 | struct snd_soc_card *card = container_of(work, struct snd_soc_card, |
| 495 | delayed_work.work); |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 496 | struct snd_soc_codec *codec = card->codec; |
Liam Girdwood | 3c4b266 | 2008-07-07 16:07:17 +0100 | [diff] [blame] | 497 | struct snd_soc_dai *codec_dai; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 498 | int i; |
| 499 | |
| 500 | mutex_lock(&pcm_mutex); |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 501 | for (i = 0; i < codec->num_dai; i++) { |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 502 | codec_dai = &codec->dai[i]; |
| 503 | |
Mark Brown | f24368c | 2008-10-21 21:45:08 +0100 | [diff] [blame] | 504 | pr_debug("pop wq checking: %s status: %s waiting: %s\n", |
| 505 | codec_dai->playback.stream_name, |
| 506 | codec_dai->playback.active ? "active" : "inactive", |
| 507 | codec_dai->pop_wait ? "yes" : "no"); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 508 | |
| 509 | /* are we waiting on this codec DAI stream */ |
| 510 | if (codec_dai->pop_wait == 1) { |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 511 | codec_dai->pop_wait = 0; |
Liam Girdwood | 0b4d221 | 2008-01-10 14:36:20 +0100 | [diff] [blame] | 512 | snd_soc_dapm_stream_event(codec, |
| 513 | codec_dai->playback.stream_name, |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 514 | SND_SOC_DAPM_STREAM_STOP); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | mutex_unlock(&pcm_mutex); |
| 518 | } |
| 519 | |
| 520 | /* |
| 521 | * Called by ALSA when a PCM substream is closed. Private data can be |
| 522 | * freed here. The cpu DAI, codec DAI, machine and platform are also |
| 523 | * shutdown. |
| 524 | */ |
| 525 | static int soc_codec_close(struct snd_pcm_substream *substream) |
| 526 | { |
| 527 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 528 | struct snd_soc_device *socdev = rtd->socdev; |
Mark Brown | 6308419 | 2008-12-02 15:08:03 +0000 | [diff] [blame] | 529 | struct snd_soc_card *card = socdev->card; |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 530 | struct snd_soc_dai_link *machine = rtd->dai; |
Mark Brown | 87689d5 | 2008-12-02 16:01:14 +0000 | [diff] [blame] | 531 | struct snd_soc_platform *platform = card->platform; |
Liam Girdwood | 3c4b266 | 2008-07-07 16:07:17 +0100 | [diff] [blame] | 532 | struct snd_soc_dai *cpu_dai = machine->cpu_dai; |
| 533 | struct snd_soc_dai *codec_dai = machine->codec_dai; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 534 | struct snd_soc_codec *codec = card->codec; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 535 | |
| 536 | mutex_lock(&pcm_mutex); |
| 537 | |
| 538 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 539 | cpu_dai->playback.active = codec_dai->playback.active = 0; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 540 | else |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 541 | cpu_dai->capture.active = codec_dai->capture.active = 0; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 542 | |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 543 | if (codec_dai->playback.active == 0 && |
| 544 | codec_dai->capture.active == 0) { |
| 545 | cpu_dai->active = codec_dai->active = 0; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 546 | } |
| 547 | codec->active--; |
| 548 | |
Mark Brown | 6010b2d | 2008-09-06 18:33:24 +0100 | [diff] [blame] | 549 | /* Muting the DAC suppresses artifacts caused during digital |
| 550 | * shutdown, for example from stopping clocks. |
| 551 | */ |
| 552 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 553 | snd_soc_dai_digital_mute(codec_dai, 1); |
| 554 | |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 555 | if (cpu_dai->ops->shutdown) |
| 556 | cpu_dai->ops->shutdown(substream, cpu_dai); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 557 | |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 558 | if (codec_dai->ops->shutdown) |
| 559 | codec_dai->ops->shutdown(substream, codec_dai); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 560 | |
| 561 | if (machine->ops && machine->ops->shutdown) |
| 562 | machine->ops->shutdown(substream); |
| 563 | |
| 564 | if (platform->pcm_ops->close) |
| 565 | platform->pcm_ops->close(substream); |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 566 | cpu_dai->runtime = NULL; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 567 | |
| 568 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 569 | /* start delayed pop wq here for playback streams */ |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 570 | codec_dai->pop_wait = 1; |
Mark Brown | 6308419 | 2008-12-02 15:08:03 +0000 | [diff] [blame] | 571 | schedule_delayed_work(&card->delayed_work, |
Mark Brown | 96dd362 | 2010-02-12 11:05:44 +0000 | [diff] [blame] | 572 | msecs_to_jiffies(card->pmdown_time)); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 573 | } else { |
| 574 | /* capture streams can be powered down now */ |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 575 | snd_soc_dapm_stream_event(codec, |
Liam Girdwood | 0b4d221 | 2008-01-10 14:36:20 +0100 | [diff] [blame] | 576 | codec_dai->capture.stream_name, |
| 577 | SND_SOC_DAPM_STREAM_STOP); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | mutex_unlock(&pcm_mutex); |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | /* |
| 585 | * Called by ALSA when the PCM substream is prepared, can set format, sample |
| 586 | * rate, etc. This function is non atomic and can be called multiple times, |
| 587 | * it can refer to the runtime info. |
| 588 | */ |
| 589 | static int soc_pcm_prepare(struct snd_pcm_substream *substream) |
| 590 | { |
| 591 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 592 | struct snd_soc_device *socdev = rtd->socdev; |
Mark Brown | 6308419 | 2008-12-02 15:08:03 +0000 | [diff] [blame] | 593 | struct snd_soc_card *card = socdev->card; |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 594 | struct snd_soc_dai_link *machine = rtd->dai; |
Mark Brown | 87689d5 | 2008-12-02 16:01:14 +0000 | [diff] [blame] | 595 | struct snd_soc_platform *platform = card->platform; |
Liam Girdwood | 3c4b266 | 2008-07-07 16:07:17 +0100 | [diff] [blame] | 596 | struct snd_soc_dai *cpu_dai = machine->cpu_dai; |
| 597 | struct snd_soc_dai *codec_dai = machine->codec_dai; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 598 | struct snd_soc_codec *codec = card->codec; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 599 | int ret = 0; |
| 600 | |
| 601 | mutex_lock(&pcm_mutex); |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 602 | |
| 603 | if (machine->ops && machine->ops->prepare) { |
| 604 | ret = machine->ops->prepare(substream); |
| 605 | if (ret < 0) { |
| 606 | printk(KERN_ERR "asoc: machine prepare error\n"); |
| 607 | goto out; |
| 608 | } |
| 609 | } |
| 610 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 611 | if (platform->pcm_ops->prepare) { |
| 612 | ret = platform->pcm_ops->prepare(substream); |
Liam Girdwood | a71a468 | 2006-10-19 20:35:56 +0200 | [diff] [blame] | 613 | if (ret < 0) { |
| 614 | printk(KERN_ERR "asoc: platform prepare error\n"); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 615 | goto out; |
Liam Girdwood | a71a468 | 2006-10-19 20:35:56 +0200 | [diff] [blame] | 616 | } |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 617 | } |
| 618 | |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 619 | if (codec_dai->ops->prepare) { |
| 620 | ret = codec_dai->ops->prepare(substream, codec_dai); |
Liam Girdwood | a71a468 | 2006-10-19 20:35:56 +0200 | [diff] [blame] | 621 | if (ret < 0) { |
| 622 | printk(KERN_ERR "asoc: codec DAI prepare error\n"); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 623 | goto out; |
Liam Girdwood | a71a468 | 2006-10-19 20:35:56 +0200 | [diff] [blame] | 624 | } |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 625 | } |
| 626 | |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 627 | if (cpu_dai->ops->prepare) { |
| 628 | ret = cpu_dai->ops->prepare(substream, cpu_dai); |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 629 | if (ret < 0) { |
| 630 | printk(KERN_ERR "asoc: cpu DAI prepare error\n"); |
| 631 | goto out; |
| 632 | } |
| 633 | } |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 634 | |
Mark Brown | d45f621 | 2008-10-14 13:58:36 +0100 | [diff] [blame] | 635 | /* cancel any delayed stream shutdown that is pending */ |
| 636 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
| 637 | codec_dai->pop_wait) { |
| 638 | codec_dai->pop_wait = 0; |
Mark Brown | 6308419 | 2008-12-02 15:08:03 +0000 | [diff] [blame] | 639 | cancel_delayed_work(&card->delayed_work); |
Mark Brown | d45f621 | 2008-10-14 13:58:36 +0100 | [diff] [blame] | 640 | } |
| 641 | |
Mark Brown | 452c5ea | 2009-05-17 21:41:23 +0100 | [diff] [blame] | 642 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 643 | snd_soc_dapm_stream_event(codec, |
| 644 | codec_dai->playback.stream_name, |
| 645 | SND_SOC_DAPM_STREAM_START); |
| 646 | else |
| 647 | snd_soc_dapm_stream_event(codec, |
| 648 | codec_dai->capture.stream_name, |
| 649 | SND_SOC_DAPM_STREAM_START); |
Mark Brown | d45f621 | 2008-10-14 13:58:36 +0100 | [diff] [blame] | 650 | |
Mark Brown | 452c5ea | 2009-05-17 21:41:23 +0100 | [diff] [blame] | 651 | snd_soc_dai_digital_mute(codec_dai, 0); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 652 | |
| 653 | out: |
| 654 | mutex_unlock(&pcm_mutex); |
| 655 | return ret; |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * Called by ALSA when the hardware params are set by application. This |
| 660 | * function can also be called multiple times and can allocate buffers |
| 661 | * (using snd_pcm_lib_* ). It's non-atomic. |
| 662 | */ |
| 663 | static int soc_pcm_hw_params(struct snd_pcm_substream *substream, |
| 664 | struct snd_pcm_hw_params *params) |
| 665 | { |
| 666 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 667 | struct snd_soc_device *socdev = rtd->socdev; |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 668 | struct snd_soc_dai_link *machine = rtd->dai; |
Mark Brown | 87689d5 | 2008-12-02 16:01:14 +0000 | [diff] [blame] | 669 | struct snd_soc_card *card = socdev->card; |
| 670 | struct snd_soc_platform *platform = card->platform; |
Liam Girdwood | 3c4b266 | 2008-07-07 16:07:17 +0100 | [diff] [blame] | 671 | struct snd_soc_dai *cpu_dai = machine->cpu_dai; |
| 672 | struct snd_soc_dai *codec_dai = machine->codec_dai; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 673 | int ret = 0; |
| 674 | |
| 675 | mutex_lock(&pcm_mutex); |
| 676 | |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 677 | if (machine->ops && machine->ops->hw_params) { |
| 678 | ret = machine->ops->hw_params(substream, params); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 679 | if (ret < 0) { |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 680 | printk(KERN_ERR "asoc: machine hw_params failed\n"); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 681 | goto out; |
| 682 | } |
| 683 | } |
| 684 | |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 685 | if (codec_dai->ops->hw_params) { |
| 686 | ret = codec_dai->ops->hw_params(substream, params, codec_dai); |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 687 | if (ret < 0) { |
| 688 | printk(KERN_ERR "asoc: can't set codec %s hw params\n", |
| 689 | codec_dai->name); |
| 690 | goto codec_err; |
| 691 | } |
| 692 | } |
| 693 | |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 694 | if (cpu_dai->ops->hw_params) { |
| 695 | ret = cpu_dai->ops->hw_params(substream, params, cpu_dai); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 696 | if (ret < 0) { |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 697 | printk(KERN_ERR "asoc: interface %s hw params failed\n", |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 698 | cpu_dai->name); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 699 | goto interface_err; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | if (platform->pcm_ops->hw_params) { |
| 704 | ret = platform->pcm_ops->hw_params(substream, params); |
| 705 | if (ret < 0) { |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 706 | printk(KERN_ERR "asoc: platform %s hw params failed\n", |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 707 | platform->name); |
| 708 | goto platform_err; |
| 709 | } |
| 710 | } |
| 711 | |
Mark Brown | 06f409d | 2009-04-07 18:10:13 +0100 | [diff] [blame] | 712 | machine->rate = params_rate(params); |
| 713 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 714 | out: |
| 715 | mutex_unlock(&pcm_mutex); |
| 716 | return ret; |
| 717 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 718 | platform_err: |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 719 | if (cpu_dai->ops->hw_free) |
| 720 | cpu_dai->ops->hw_free(substream, cpu_dai); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 721 | |
| 722 | interface_err: |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 723 | if (codec_dai->ops->hw_free) |
| 724 | codec_dai->ops->hw_free(substream, codec_dai); |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 725 | |
| 726 | codec_err: |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 727 | if (machine->ops && machine->ops->hw_free) |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 728 | machine->ops->hw_free(substream); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 729 | |
| 730 | mutex_unlock(&pcm_mutex); |
| 731 | return ret; |
| 732 | } |
| 733 | |
| 734 | /* |
| 735 | * Free's resources allocated by hw_params, can be called multiple times |
| 736 | */ |
| 737 | static int soc_pcm_hw_free(struct snd_pcm_substream *substream) |
| 738 | { |
| 739 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 740 | struct snd_soc_device *socdev = rtd->socdev; |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 741 | struct snd_soc_dai_link *machine = rtd->dai; |
Mark Brown | 87689d5 | 2008-12-02 16:01:14 +0000 | [diff] [blame] | 742 | struct snd_soc_card *card = socdev->card; |
| 743 | struct snd_soc_platform *platform = card->platform; |
Liam Girdwood | 3c4b266 | 2008-07-07 16:07:17 +0100 | [diff] [blame] | 744 | struct snd_soc_dai *cpu_dai = machine->cpu_dai; |
| 745 | struct snd_soc_dai *codec_dai = machine->codec_dai; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 746 | struct snd_soc_codec *codec = card->codec; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 747 | |
| 748 | mutex_lock(&pcm_mutex); |
| 749 | |
| 750 | /* apply codec digital mute */ |
Liam Girdwood | 8c6529d | 2008-07-08 13:19:13 +0100 | [diff] [blame] | 751 | if (!codec->active) |
| 752 | snd_soc_dai_digital_mute(codec_dai, 1); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 753 | |
| 754 | /* free any machine hw params */ |
| 755 | if (machine->ops && machine->ops->hw_free) |
| 756 | machine->ops->hw_free(substream); |
| 757 | |
| 758 | /* free any DMA resources */ |
| 759 | if (platform->pcm_ops->hw_free) |
| 760 | platform->pcm_ops->hw_free(substream); |
| 761 | |
| 762 | /* now free hw params for the DAI's */ |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 763 | if (codec_dai->ops->hw_free) |
| 764 | codec_dai->ops->hw_free(substream, codec_dai); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 765 | |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 766 | if (cpu_dai->ops->hw_free) |
| 767 | cpu_dai->ops->hw_free(substream, cpu_dai); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 768 | |
| 769 | mutex_unlock(&pcm_mutex); |
| 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 774 | { |
| 775 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 776 | struct snd_soc_device *socdev = rtd->socdev; |
Mark Brown | 87689d5 | 2008-12-02 16:01:14 +0000 | [diff] [blame] | 777 | struct snd_soc_card *card= socdev->card; |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 778 | struct snd_soc_dai_link *machine = rtd->dai; |
Mark Brown | 87689d5 | 2008-12-02 16:01:14 +0000 | [diff] [blame] | 779 | struct snd_soc_platform *platform = card->platform; |
Liam Girdwood | 3c4b266 | 2008-07-07 16:07:17 +0100 | [diff] [blame] | 780 | struct snd_soc_dai *cpu_dai = machine->cpu_dai; |
| 781 | struct snd_soc_dai *codec_dai = machine->codec_dai; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 782 | int ret; |
| 783 | |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 784 | if (codec_dai->ops->trigger) { |
| 785 | ret = codec_dai->ops->trigger(substream, cmd, codec_dai); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 786 | if (ret < 0) |
| 787 | return ret; |
| 788 | } |
| 789 | |
| 790 | if (platform->pcm_ops->trigger) { |
| 791 | ret = platform->pcm_ops->trigger(substream, cmd); |
| 792 | if (ret < 0) |
| 793 | return ret; |
| 794 | } |
| 795 | |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 796 | if (cpu_dai->ops->trigger) { |
| 797 | ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 798 | if (ret < 0) |
| 799 | return ret; |
| 800 | } |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | /* ASoC PCM operations */ |
| 805 | static struct snd_pcm_ops soc_pcm_ops = { |
| 806 | .open = soc_pcm_open, |
| 807 | .close = soc_codec_close, |
| 808 | .hw_params = soc_pcm_hw_params, |
| 809 | .hw_free = soc_pcm_hw_free, |
| 810 | .prepare = soc_pcm_prepare, |
| 811 | .trigger = soc_pcm_trigger, |
| 812 | }; |
| 813 | |
| 814 | #ifdef CONFIG_PM |
| 815 | /* powers down audio subsystem for suspend */ |
Mark Brown | 416356f | 2009-06-30 19:05:15 +0100 | [diff] [blame] | 816 | static int soc_suspend(struct device *dev) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 817 | { |
Mark Brown | 416356f | 2009-06-30 19:05:15 +0100 | [diff] [blame] | 818 | struct platform_device *pdev = to_platform_device(dev); |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 819 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 820 | struct snd_soc_card *card = socdev->card; |
Mark Brown | 87689d5 | 2008-12-02 16:01:14 +0000 | [diff] [blame] | 821 | struct snd_soc_platform *platform = card->platform; |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 822 | struct snd_soc_codec_device *codec_dev = socdev->codec_dev; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 823 | struct snd_soc_codec *codec = card->codec; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 824 | int i; |
| 825 | |
Daniel Mack | e3509ff | 2009-06-03 17:44:49 +0200 | [diff] [blame] | 826 | /* If the initialization of this soc device failed, there is no codec |
| 827 | * associated with it. Just bail out in this case. |
| 828 | */ |
| 829 | if (!codec) |
| 830 | return 0; |
| 831 | |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 832 | /* Due to the resume being scheduled into a workqueue we could |
| 833 | * suspend before that's finished - wait for it to complete. |
| 834 | */ |
| 835 | snd_power_lock(codec->card); |
| 836 | snd_power_wait(codec->card, SNDRV_CTL_POWER_D0); |
| 837 | snd_power_unlock(codec->card); |
| 838 | |
| 839 | /* we're going to block userspace touching us until resume completes */ |
| 840 | snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot); |
| 841 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 842 | /* mute any active DAC's */ |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 843 | for (i = 0; i < card->num_links; i++) { |
| 844 | struct snd_soc_dai *dai = card->dai_link[i].codec_dai; |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 845 | if (dai->ops->digital_mute && dai->playback.active) |
| 846 | dai->ops->digital_mute(dai, 1); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 847 | } |
| 848 | |
Liam Girdwood | 4ccab3e | 2008-01-10 14:39:01 +0100 | [diff] [blame] | 849 | /* suspend all pcms */ |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 850 | for (i = 0; i < card->num_links; i++) |
| 851 | snd_pcm_suspend_all(card->dai_link[i].pcm); |
Liam Girdwood | 4ccab3e | 2008-01-10 14:39:01 +0100 | [diff] [blame] | 852 | |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 853 | if (card->suspend_pre) |
Mark Brown | 416356f | 2009-06-30 19:05:15 +0100 | [diff] [blame] | 854 | card->suspend_pre(pdev, PMSG_SUSPEND); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 855 | |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 856 | for (i = 0; i < card->num_links; i++) { |
| 857 | struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; |
Mark Brown | 3ba9e10a | 2008-11-24 18:01:05 +0000 | [diff] [blame] | 858 | if (cpu_dai->suspend && !cpu_dai->ac97_control) |
Mark Brown | dc7d7b8 | 2008-12-03 18:21:52 +0000 | [diff] [blame] | 859 | cpu_dai->suspend(cpu_dai); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 860 | if (platform->suspend) |
Mark Brown | 07c84d0 | 2008-12-03 18:17:28 +0000 | [diff] [blame] | 861 | platform->suspend(cpu_dai); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | /* close any waiting streams and save state */ |
Mark Brown | 6308419 | 2008-12-02 15:08:03 +0000 | [diff] [blame] | 865 | run_delayed_work(&card->delayed_work); |
Mark Brown | 0be9898 | 2008-05-19 12:31:28 +0200 | [diff] [blame] | 866 | codec->suspend_bias_level = codec->bias_level; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 867 | |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 868 | for (i = 0; i < codec->num_dai; i++) { |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 869 | char *stream = codec->dai[i].playback.stream_name; |
| 870 | if (stream != NULL) |
| 871 | snd_soc_dapm_stream_event(codec, stream, |
| 872 | SND_SOC_DAPM_STREAM_SUSPEND); |
| 873 | stream = codec->dai[i].capture.stream_name; |
| 874 | if (stream != NULL) |
| 875 | snd_soc_dapm_stream_event(codec, stream, |
| 876 | SND_SOC_DAPM_STREAM_SUSPEND); |
| 877 | } |
| 878 | |
| 879 | if (codec_dev->suspend) |
Mark Brown | 416356f | 2009-06-30 19:05:15 +0100 | [diff] [blame] | 880 | codec_dev->suspend(pdev, PMSG_SUSPEND); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 881 | |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 882 | for (i = 0; i < card->num_links; i++) { |
| 883 | struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; |
Mark Brown | 3ba9e10a | 2008-11-24 18:01:05 +0000 | [diff] [blame] | 884 | if (cpu_dai->suspend && cpu_dai->ac97_control) |
Mark Brown | dc7d7b8 | 2008-12-03 18:21:52 +0000 | [diff] [blame] | 885 | cpu_dai->suspend(cpu_dai); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 886 | } |
| 887 | |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 888 | if (card->suspend_post) |
Mark Brown | 416356f | 2009-06-30 19:05:15 +0100 | [diff] [blame] | 889 | card->suspend_post(pdev, PMSG_SUSPEND); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 890 | |
| 891 | return 0; |
| 892 | } |
| 893 | |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 894 | /* deferred resume work, so resume can complete before we finished |
| 895 | * setting our codec back up, which can be very slow on I2C |
| 896 | */ |
| 897 | static void soc_resume_deferred(struct work_struct *work) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 898 | { |
Mark Brown | 6308419 | 2008-12-02 15:08:03 +0000 | [diff] [blame] | 899 | struct snd_soc_card *card = container_of(work, |
| 900 | struct snd_soc_card, |
| 901 | deferred_resume_work); |
| 902 | struct snd_soc_device *socdev = card->socdev; |
Mark Brown | 87689d5 | 2008-12-02 16:01:14 +0000 | [diff] [blame] | 903 | struct snd_soc_platform *platform = card->platform; |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 904 | struct snd_soc_codec_device *codec_dev = socdev->codec_dev; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 905 | struct snd_soc_codec *codec = card->codec; |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 906 | struct platform_device *pdev = to_platform_device(socdev->dev); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 907 | int i; |
| 908 | |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 909 | /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time, |
| 910 | * so userspace apps are blocked from touching us |
| 911 | */ |
| 912 | |
Mark Brown | fde22f2 | 2008-11-24 18:08:18 +0000 | [diff] [blame] | 913 | dev_dbg(socdev->dev, "starting resume work\n"); |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 914 | |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 915 | if (card->resume_pre) |
| 916 | card->resume_pre(pdev); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 917 | |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 918 | for (i = 0; i < card->num_links; i++) { |
| 919 | struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; |
Mark Brown | 3ba9e10a | 2008-11-24 18:01:05 +0000 | [diff] [blame] | 920 | if (cpu_dai->resume && cpu_dai->ac97_control) |
Mark Brown | dc7d7b8 | 2008-12-03 18:21:52 +0000 | [diff] [blame] | 921 | cpu_dai->resume(cpu_dai); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | if (codec_dev->resume) |
| 925 | codec_dev->resume(pdev); |
| 926 | |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 927 | for (i = 0; i < codec->num_dai; i++) { |
| 928 | char *stream = codec->dai[i].playback.stream_name; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 929 | if (stream != NULL) |
| 930 | snd_soc_dapm_stream_event(codec, stream, |
| 931 | SND_SOC_DAPM_STREAM_RESUME); |
| 932 | stream = codec->dai[i].capture.stream_name; |
| 933 | if (stream != NULL) |
| 934 | snd_soc_dapm_stream_event(codec, stream, |
| 935 | SND_SOC_DAPM_STREAM_RESUME); |
| 936 | } |
| 937 | |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 938 | /* unmute any active DACs */ |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 939 | for (i = 0; i < card->num_links; i++) { |
| 940 | struct snd_soc_dai *dai = card->dai_link[i].codec_dai; |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 941 | if (dai->ops->digital_mute && dai->playback.active) |
| 942 | dai->ops->digital_mute(dai, 0); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 943 | } |
| 944 | |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 945 | for (i = 0; i < card->num_links; i++) { |
| 946 | struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; |
Mark Brown | 3ba9e10a | 2008-11-24 18:01:05 +0000 | [diff] [blame] | 947 | if (cpu_dai->resume && !cpu_dai->ac97_control) |
Mark Brown | dc7d7b8 | 2008-12-03 18:21:52 +0000 | [diff] [blame] | 948 | cpu_dai->resume(cpu_dai); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 949 | if (platform->resume) |
Mark Brown | 07c84d0 | 2008-12-03 18:17:28 +0000 | [diff] [blame] | 950 | platform->resume(cpu_dai); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 951 | } |
| 952 | |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 953 | if (card->resume_post) |
| 954 | card->resume_post(pdev); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 955 | |
Mark Brown | fde22f2 | 2008-11-24 18:08:18 +0000 | [diff] [blame] | 956 | dev_dbg(socdev->dev, "resume work completed\n"); |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 957 | |
| 958 | /* userspace can access us now we are back as we were before */ |
| 959 | snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0); |
| 960 | } |
| 961 | |
| 962 | /* powers up audio subsystem after a suspend */ |
Mark Brown | 416356f | 2009-06-30 19:05:15 +0100 | [diff] [blame] | 963 | static int soc_resume(struct device *dev) |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 964 | { |
Mark Brown | 416356f | 2009-06-30 19:05:15 +0100 | [diff] [blame] | 965 | struct platform_device *pdev = to_platform_device(dev); |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 966 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
Mark Brown | 6308419 | 2008-12-02 15:08:03 +0000 | [diff] [blame] | 967 | struct snd_soc_card *card = socdev->card; |
Mark Brown | 64ab9ba | 2009-03-31 11:27:03 +0100 | [diff] [blame] | 968 | struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai; |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 969 | |
Peter Ujfalusi | b9dd94a | 2010-02-22 13:27:13 +0200 | [diff] [blame] | 970 | /* If the initialization of this soc device failed, there is no codec |
| 971 | * associated with it. Just bail out in this case. |
| 972 | */ |
| 973 | if (!card->codec) |
| 974 | return 0; |
| 975 | |
Mark Brown | 64ab9ba | 2009-03-31 11:27:03 +0100 | [diff] [blame] | 976 | /* AC97 devices might have other drivers hanging off them so |
| 977 | * need to resume immediately. Other drivers don't have that |
| 978 | * problem and may take a substantial amount of time to resume |
| 979 | * due to I/O costs and anti-pop so handle them out of line. |
| 980 | */ |
| 981 | if (cpu_dai->ac97_control) { |
| 982 | dev_dbg(socdev->dev, "Resuming AC97 immediately\n"); |
| 983 | soc_resume_deferred(&card->deferred_resume_work); |
| 984 | } else { |
| 985 | dev_dbg(socdev->dev, "Scheduling resume work\n"); |
| 986 | if (!schedule_work(&card->deferred_resume_work)) |
| 987 | dev_err(socdev->dev, "resume work item may be lost\n"); |
| 988 | } |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 989 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 990 | return 0; |
| 991 | } |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 992 | #else |
| 993 | #define soc_suspend NULL |
| 994 | #define soc_resume NULL |
| 995 | #endif |
| 996 | |
Barry Song | 02a06d3 | 2009-10-16 18:13:38 +0800 | [diff] [blame] | 997 | static struct snd_soc_dai_ops null_dai_ops = { |
| 998 | }; |
| 999 | |
Mark Brown | 435c5e2 | 2008-12-04 15:32:53 +0000 | [diff] [blame] | 1000 | static void snd_soc_instantiate_card(struct snd_soc_card *card) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1001 | { |
Mark Brown | 435c5e2 | 2008-12-04 15:32:53 +0000 | [diff] [blame] | 1002 | struct platform_device *pdev = container_of(card->dev, |
| 1003 | struct platform_device, |
| 1004 | dev); |
| 1005 | struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev; |
Mark Brown | fe3e78e | 2009-11-03 22:13:13 +0000 | [diff] [blame] | 1006 | struct snd_soc_codec *codec; |
Mark Brown | 435c5e2 | 2008-12-04 15:32:53 +0000 | [diff] [blame] | 1007 | struct snd_soc_platform *platform; |
| 1008 | struct snd_soc_dai *dai; |
Mark Brown | 6b05eda | 2008-12-08 19:26:48 +0000 | [diff] [blame] | 1009 | int i, found, ret, ac97; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1010 | |
Mark Brown | 435c5e2 | 2008-12-04 15:32:53 +0000 | [diff] [blame] | 1011 | if (card->instantiated) |
| 1012 | return; |
Mark Brown | 6308419 | 2008-12-02 15:08:03 +0000 | [diff] [blame] | 1013 | |
Mark Brown | 435c5e2 | 2008-12-04 15:32:53 +0000 | [diff] [blame] | 1014 | found = 0; |
| 1015 | list_for_each_entry(platform, &platform_list, list) |
| 1016 | if (card->platform == platform) { |
| 1017 | found = 1; |
| 1018 | break; |
| 1019 | } |
| 1020 | if (!found) { |
| 1021 | dev_dbg(card->dev, "Platform %s not registered\n", |
| 1022 | card->platform->name); |
| 1023 | return; |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Mark Brown | 6b05eda | 2008-12-08 19:26:48 +0000 | [diff] [blame] | 1026 | ac97 = 0; |
Mark Brown | 435c5e2 | 2008-12-04 15:32:53 +0000 | [diff] [blame] | 1027 | for (i = 0; i < card->num_links; i++) { |
| 1028 | found = 0; |
| 1029 | list_for_each_entry(dai, &dai_list, list) |
| 1030 | if (card->dai_link[i].cpu_dai == dai) { |
| 1031 | found = 1; |
| 1032 | break; |
| 1033 | } |
| 1034 | if (!found) { |
| 1035 | dev_dbg(card->dev, "DAI %s not registered\n", |
| 1036 | card->dai_link[i].cpu_dai->name); |
| 1037 | return; |
| 1038 | } |
Mark Brown | 6b05eda | 2008-12-08 19:26:48 +0000 | [diff] [blame] | 1039 | |
| 1040 | if (card->dai_link[i].cpu_dai->ac97_control) |
| 1041 | ac97 = 1; |
Mark Brown | 435c5e2 | 2008-12-04 15:32:53 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
Barry Song | 02a06d3 | 2009-10-16 18:13:38 +0800 | [diff] [blame] | 1044 | for (i = 0; i < card->num_links; i++) { |
| 1045 | if (!card->dai_link[i].codec_dai->ops) |
| 1046 | card->dai_link[i].codec_dai->ops = &null_dai_ops; |
| 1047 | } |
| 1048 | |
Mark Brown | 6b05eda | 2008-12-08 19:26:48 +0000 | [diff] [blame] | 1049 | /* If we have AC97 in the system then don't wait for the |
| 1050 | * codec. This will need revisiting if we have to handle |
| 1051 | * systems with mixed AC97 and non-AC97 parts. Only check for |
| 1052 | * DAIs currently; we can't do this per link since some AC97 |
| 1053 | * codecs have non-AC97 DAIs. |
| 1054 | */ |
| 1055 | if (!ac97) |
| 1056 | for (i = 0; i < card->num_links; i++) { |
| 1057 | found = 0; |
| 1058 | list_for_each_entry(dai, &dai_list, list) |
| 1059 | if (card->dai_link[i].codec_dai == dai) { |
| 1060 | found = 1; |
| 1061 | break; |
| 1062 | } |
| 1063 | if (!found) { |
| 1064 | dev_dbg(card->dev, "DAI %s not registered\n", |
| 1065 | card->dai_link[i].codec_dai->name); |
| 1066 | return; |
| 1067 | } |
| 1068 | } |
| 1069 | |
Mark Brown | 435c5e2 | 2008-12-04 15:32:53 +0000 | [diff] [blame] | 1070 | /* Note that we do not current check for codec components */ |
| 1071 | |
| 1072 | dev_dbg(card->dev, "All components present, instantiating\n"); |
| 1073 | |
| 1074 | /* Found everything, bring it up */ |
Mark Brown | 96dd362 | 2010-02-12 11:05:44 +0000 | [diff] [blame] | 1075 | card->pmdown_time = pmdown_time; |
| 1076 | |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 1077 | if (card->probe) { |
| 1078 | ret = card->probe(pdev); |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 1079 | if (ret < 0) |
Mark Brown | 435c5e2 | 2008-12-04 15:32:53 +0000 | [diff] [blame] | 1080 | return; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1081 | } |
| 1082 | |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 1083 | for (i = 0; i < card->num_links; i++) { |
| 1084 | struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1085 | if (cpu_dai->probe) { |
Mark Brown | bdb9287 | 2008-06-11 13:47:10 +0100 | [diff] [blame] | 1086 | ret = cpu_dai->probe(pdev, cpu_dai); |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 1087 | if (ret < 0) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1088 | goto cpu_dai_err; |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | if (codec_dev->probe) { |
| 1093 | ret = codec_dev->probe(pdev); |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 1094 | if (ret < 0) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1095 | goto cpu_dai_err; |
| 1096 | } |
Mark Brown | fe3e78e | 2009-11-03 22:13:13 +0000 | [diff] [blame] | 1097 | codec = card->codec; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1098 | |
| 1099 | if (platform->probe) { |
| 1100 | ret = platform->probe(pdev); |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 1101 | if (ret < 0) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1102 | goto platform_err; |
| 1103 | } |
| 1104 | |
| 1105 | /* DAPM stream work */ |
Mark Brown | 6308419 | 2008-12-02 15:08:03 +0000 | [diff] [blame] | 1106 | INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work); |
Randy Dunlap | 1301a96 | 2008-06-17 19:19:34 +0100 | [diff] [blame] | 1107 | #ifdef CONFIG_PM |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 1108 | /* deferred resume work */ |
Mark Brown | 6308419 | 2008-12-02 15:08:03 +0000 | [diff] [blame] | 1109 | INIT_WORK(&card->deferred_resume_work, soc_resume_deferred); |
Randy Dunlap | 1301a96 | 2008-06-17 19:19:34 +0100 | [diff] [blame] | 1110 | #endif |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 1111 | |
Mark Brown | fe3e78e | 2009-11-03 22:13:13 +0000 | [diff] [blame] | 1112 | for (i = 0; i < card->num_links; i++) { |
| 1113 | if (card->dai_link[i].init) { |
| 1114 | ret = card->dai_link[i].init(codec); |
| 1115 | if (ret < 0) { |
| 1116 | printk(KERN_ERR "asoc: failed to init %s\n", |
| 1117 | card->dai_link[i].stream_name); |
| 1118 | continue; |
| 1119 | } |
| 1120 | } |
Barry Song | f773205 | 2009-11-12 12:01:47 +0800 | [diff] [blame] | 1121 | if (card->dai_link[i].codec_dai->ac97_control) |
Mark Brown | fe3e78e | 2009-11-03 22:13:13 +0000 | [diff] [blame] | 1122 | ac97 = 1; |
Mark Brown | fe3e78e | 2009-11-03 22:13:13 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | snprintf(codec->card->shortname, sizeof(codec->card->shortname), |
| 1126 | "%s", card->name); |
| 1127 | snprintf(codec->card->longname, sizeof(codec->card->longname), |
| 1128 | "%s (%s)", card->name, codec->name); |
| 1129 | |
| 1130 | /* Make sure all DAPM widgets are instantiated */ |
| 1131 | snd_soc_dapm_new_widgets(codec); |
| 1132 | |
| 1133 | ret = snd_card_register(codec->card); |
| 1134 | if (ret < 0) { |
| 1135 | printk(KERN_ERR "asoc: failed to register soundcard for %s\n", |
| 1136 | codec->name); |
| 1137 | goto card_err; |
| 1138 | } |
| 1139 | |
| 1140 | mutex_lock(&codec->mutex); |
| 1141 | #ifdef CONFIG_SND_SOC_AC97_BUS |
| 1142 | /* Only instantiate AC97 if not already done by the adaptor |
| 1143 | * for the generic AC97 subsystem. |
| 1144 | */ |
| 1145 | if (ac97 && strcmp(codec->name, "AC97") != 0) { |
| 1146 | ret = soc_ac97_dev_register(codec); |
| 1147 | if (ret < 0) { |
| 1148 | printk(KERN_ERR "asoc: AC97 device register failed\n"); |
| 1149 | snd_card_free(codec->card); |
| 1150 | mutex_unlock(&codec->mutex); |
| 1151 | goto card_err; |
| 1152 | } |
| 1153 | } |
| 1154 | #endif |
| 1155 | |
| 1156 | ret = snd_soc_dapm_sys_add(card->socdev->dev); |
| 1157 | if (ret < 0) |
| 1158 | printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n"); |
| 1159 | |
Mark Brown | dbe2140 | 2010-02-12 11:37:24 +0000 | [diff] [blame] | 1160 | ret = device_create_file(card->socdev->dev, &dev_attr_pmdown_time); |
| 1161 | if (ret < 0) |
| 1162 | printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n"); |
| 1163 | |
Mark Brown | fe3e78e | 2009-11-03 22:13:13 +0000 | [diff] [blame] | 1164 | ret = device_create_file(card->socdev->dev, &dev_attr_codec_reg); |
| 1165 | if (ret < 0) |
| 1166 | printk(KERN_WARNING "asoc: failed to add codec sysfs files\n"); |
| 1167 | |
| 1168 | soc_init_codec_debugfs(codec); |
| 1169 | mutex_unlock(&codec->mutex); |
| 1170 | |
Mark Brown | 435c5e2 | 2008-12-04 15:32:53 +0000 | [diff] [blame] | 1171 | card->instantiated = 1; |
| 1172 | |
| 1173 | return; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1174 | |
Mark Brown | fe3e78e | 2009-11-03 22:13:13 +0000 | [diff] [blame] | 1175 | card_err: |
| 1176 | if (platform->remove) |
| 1177 | platform->remove(pdev); |
| 1178 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1179 | platform_err: |
| 1180 | if (codec_dev->remove) |
| 1181 | codec_dev->remove(pdev); |
| 1182 | |
| 1183 | cpu_dai_err: |
Liam Girdwood | 18b9b3d | 2007-01-30 17:18:45 +0100 | [diff] [blame] | 1184 | for (i--; i >= 0; i--) { |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 1185 | struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1186 | if (cpu_dai->remove) |
Mark Brown | bdb9287 | 2008-06-11 13:47:10 +0100 | [diff] [blame] | 1187 | cpu_dai->remove(pdev, cpu_dai); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1188 | } |
| 1189 | |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 1190 | if (card->remove) |
| 1191 | card->remove(pdev); |
Mark Brown | 435c5e2 | 2008-12-04 15:32:53 +0000 | [diff] [blame] | 1192 | } |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1193 | |
Mark Brown | 435c5e2 | 2008-12-04 15:32:53 +0000 | [diff] [blame] | 1194 | /* |
| 1195 | * Attempt to initialise any uninitalised cards. Must be called with |
| 1196 | * client_mutex. |
| 1197 | */ |
| 1198 | static void snd_soc_instantiate_cards(void) |
| 1199 | { |
| 1200 | struct snd_soc_card *card; |
| 1201 | list_for_each_entry(card, &card_list, list) |
| 1202 | snd_soc_instantiate_card(card); |
| 1203 | } |
| 1204 | |
| 1205 | /* probes a new socdev */ |
| 1206 | static int soc_probe(struct platform_device *pdev) |
| 1207 | { |
| 1208 | int ret = 0; |
| 1209 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 1210 | struct snd_soc_card *card = socdev->card; |
| 1211 | |
| 1212 | /* Bodge while we push things out of socdev */ |
| 1213 | card->socdev = socdev; |
| 1214 | |
| 1215 | /* Bodge while we unpick instantiation */ |
| 1216 | card->dev = &pdev->dev; |
| 1217 | ret = snd_soc_register_card(card); |
| 1218 | if (ret != 0) { |
| 1219 | dev_err(&pdev->dev, "Failed to register card\n"); |
| 1220 | return ret; |
| 1221 | } |
| 1222 | |
| 1223 | return 0; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | /* removes a socdev */ |
| 1227 | static int soc_remove(struct platform_device *pdev) |
| 1228 | { |
| 1229 | int i; |
| 1230 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 1231 | struct snd_soc_card *card = socdev->card; |
Mark Brown | 87689d5 | 2008-12-02 16:01:14 +0000 | [diff] [blame] | 1232 | struct snd_soc_platform *platform = card->platform; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1233 | struct snd_soc_codec_device *codec_dev = socdev->codec_dev; |
| 1234 | |
Mike Rapoport | 914dc18 | 2009-05-11 13:04:55 +0300 | [diff] [blame] | 1235 | if (!card->instantiated) |
| 1236 | return 0; |
| 1237 | |
Mark Brown | 6308419 | 2008-12-02 15:08:03 +0000 | [diff] [blame] | 1238 | run_delayed_work(&card->delayed_work); |
Liam Girdwood | 965ac42 | 2007-01-31 14:14:57 +0100 | [diff] [blame] | 1239 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1240 | if (platform->remove) |
| 1241 | platform->remove(pdev); |
| 1242 | |
| 1243 | if (codec_dev->remove) |
| 1244 | codec_dev->remove(pdev); |
| 1245 | |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 1246 | for (i = 0; i < card->num_links; i++) { |
| 1247 | struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1248 | if (cpu_dai->remove) |
Mark Brown | bdb9287 | 2008-06-11 13:47:10 +0100 | [diff] [blame] | 1249 | cpu_dai->remove(pdev, cpu_dai); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1250 | } |
| 1251 | |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 1252 | if (card->remove) |
| 1253 | card->remove(pdev); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1254 | |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 1255 | snd_soc_unregister_card(card); |
| 1256 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1257 | return 0; |
| 1258 | } |
| 1259 | |
Mark Brown | 416356f | 2009-06-30 19:05:15 +0100 | [diff] [blame] | 1260 | static int soc_poweroff(struct device *dev) |
Mark Brown | 5173747 | 2009-06-22 13:16:51 +0100 | [diff] [blame] | 1261 | { |
Mark Brown | 416356f | 2009-06-30 19:05:15 +0100 | [diff] [blame] | 1262 | struct platform_device *pdev = to_platform_device(dev); |
Mark Brown | 5173747 | 2009-06-22 13:16:51 +0100 | [diff] [blame] | 1263 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 1264 | struct snd_soc_card *card = socdev->card; |
| 1265 | |
| 1266 | if (!card->instantiated) |
Mark Brown | 416356f | 2009-06-30 19:05:15 +0100 | [diff] [blame] | 1267 | return 0; |
Mark Brown | 5173747 | 2009-06-22 13:16:51 +0100 | [diff] [blame] | 1268 | |
| 1269 | /* Flush out pmdown_time work - we actually do want to run it |
| 1270 | * now, we're shutting down so no imminent restart. */ |
| 1271 | run_delayed_work(&card->delayed_work); |
| 1272 | |
| 1273 | snd_soc_dapm_shutdown(socdev); |
Mark Brown | 416356f | 2009-06-30 19:05:15 +0100 | [diff] [blame] | 1274 | |
| 1275 | return 0; |
Mark Brown | 5173747 | 2009-06-22 13:16:51 +0100 | [diff] [blame] | 1276 | } |
| 1277 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 1278 | static const struct dev_pm_ops soc_pm_ops = { |
Mark Brown | 416356f | 2009-06-30 19:05:15 +0100 | [diff] [blame] | 1279 | .suspend = soc_suspend, |
| 1280 | .resume = soc_resume, |
| 1281 | .poweroff = soc_poweroff, |
| 1282 | }; |
| 1283 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1284 | /* ASoC platform driver */ |
| 1285 | static struct platform_driver soc_driver = { |
| 1286 | .driver = { |
| 1287 | .name = "soc-audio", |
Kay Sievers | 8b45a20 | 2008-04-14 13:33:36 +0200 | [diff] [blame] | 1288 | .owner = THIS_MODULE, |
Mark Brown | 416356f | 2009-06-30 19:05:15 +0100 | [diff] [blame] | 1289 | .pm = &soc_pm_ops, |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1290 | }, |
| 1291 | .probe = soc_probe, |
| 1292 | .remove = soc_remove, |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1293 | }; |
| 1294 | |
| 1295 | /* create a new pcm */ |
| 1296 | static int soc_new_pcm(struct snd_soc_device *socdev, |
| 1297 | struct snd_soc_dai_link *dai_link, int num) |
| 1298 | { |
Mark Brown | 87689d5 | 2008-12-02 16:01:14 +0000 | [diff] [blame] | 1299 | struct snd_soc_card *card = socdev->card; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 1300 | struct snd_soc_codec *codec = card->codec; |
Mark Brown | 87689d5 | 2008-12-02 16:01:14 +0000 | [diff] [blame] | 1301 | struct snd_soc_platform *platform = card->platform; |
Liam Girdwood | 3c4b266 | 2008-07-07 16:07:17 +0100 | [diff] [blame] | 1302 | struct snd_soc_dai *codec_dai = dai_link->codec_dai; |
| 1303 | struct snd_soc_dai *cpu_dai = dai_link->cpu_dai; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1304 | struct snd_soc_pcm_runtime *rtd; |
| 1305 | struct snd_pcm *pcm; |
| 1306 | char new_name[64]; |
| 1307 | int ret = 0, playback = 0, capture = 0; |
| 1308 | |
| 1309 | rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL); |
| 1310 | if (rtd == NULL) |
| 1311 | return -ENOMEM; |
Liam Girdwood | cb666e5 | 2007-02-02 17:13:49 +0100 | [diff] [blame] | 1312 | |
| 1313 | rtd->dai = dai_link; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1314 | rtd->socdev = socdev; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 1315 | codec_dai->codec = card->codec; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1316 | |
| 1317 | /* check client and interface hw capabilities */ |
Mark Brown | 40ca114 | 2009-12-24 13:44:28 +0000 | [diff] [blame] | 1318 | snprintf(new_name, sizeof(new_name), "%s %s-%d", |
| 1319 | dai_link->stream_name, codec_dai->name, num); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1320 | |
| 1321 | if (codec_dai->playback.channels_min) |
| 1322 | playback = 1; |
| 1323 | if (codec_dai->capture.channels_min) |
| 1324 | capture = 1; |
| 1325 | |
| 1326 | ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback, |
| 1327 | capture, &pcm); |
| 1328 | if (ret < 0) { |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 1329 | printk(KERN_ERR "asoc: can't create pcm for codec %s\n", |
| 1330 | codec->name); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1331 | kfree(rtd); |
| 1332 | return ret; |
| 1333 | } |
| 1334 | |
Liam Girdwood | 4ccab3e | 2008-01-10 14:39:01 +0100 | [diff] [blame] | 1335 | dai_link->pcm = pcm; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1336 | pcm->private_data = rtd; |
Mark Brown | 87689d5 | 2008-12-02 16:01:14 +0000 | [diff] [blame] | 1337 | soc_pcm_ops.mmap = platform->pcm_ops->mmap; |
| 1338 | soc_pcm_ops.pointer = platform->pcm_ops->pointer; |
| 1339 | soc_pcm_ops.ioctl = platform->pcm_ops->ioctl; |
| 1340 | soc_pcm_ops.copy = platform->pcm_ops->copy; |
| 1341 | soc_pcm_ops.silence = platform->pcm_ops->silence; |
| 1342 | soc_pcm_ops.ack = platform->pcm_ops->ack; |
| 1343 | soc_pcm_ops.page = platform->pcm_ops->page; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1344 | |
| 1345 | if (playback) |
| 1346 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops); |
| 1347 | |
| 1348 | if (capture) |
| 1349 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops); |
| 1350 | |
Mark Brown | 87689d5 | 2008-12-02 16:01:14 +0000 | [diff] [blame] | 1351 | ret = platform->pcm_new(codec->card, codec_dai, pcm); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1352 | if (ret < 0) { |
| 1353 | printk(KERN_ERR "asoc: platform pcm constructor failed\n"); |
| 1354 | kfree(rtd); |
| 1355 | return ret; |
| 1356 | } |
| 1357 | |
Mark Brown | 87689d5 | 2008-12-02 16:01:14 +0000 | [diff] [blame] | 1358 | pcm->private_free = platform->pcm_free; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1359 | printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name, |
| 1360 | cpu_dai->name); |
| 1361 | return ret; |
| 1362 | } |
| 1363 | |
Mark Brown | 096e49d | 2009-07-05 15:12:22 +0100 | [diff] [blame] | 1364 | /** |
| 1365 | * snd_soc_codec_volatile_register: Report if a register is volatile. |
| 1366 | * |
| 1367 | * @codec: CODEC to query. |
| 1368 | * @reg: Register to query. |
| 1369 | * |
| 1370 | * Boolean function indiciating if a CODEC register is volatile. |
| 1371 | */ |
| 1372 | int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg) |
| 1373 | { |
| 1374 | if (codec->volatile_register) |
| 1375 | return codec->volatile_register(reg); |
| 1376 | else |
| 1377 | return 0; |
| 1378 | } |
| 1379 | EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register); |
| 1380 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1381 | /** |
| 1382 | * snd_soc_new_ac97_codec - initailise AC97 device |
| 1383 | * @codec: audio codec |
| 1384 | * @ops: AC97 bus operations |
| 1385 | * @num: AC97 codec number |
| 1386 | * |
| 1387 | * Initialises AC97 codec resources for use by ad-hoc devices only. |
| 1388 | */ |
| 1389 | int snd_soc_new_ac97_codec(struct snd_soc_codec *codec, |
| 1390 | struct snd_ac97_bus_ops *ops, int num) |
| 1391 | { |
| 1392 | mutex_lock(&codec->mutex); |
| 1393 | |
| 1394 | codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL); |
| 1395 | if (codec->ac97 == NULL) { |
| 1396 | mutex_unlock(&codec->mutex); |
| 1397 | return -ENOMEM; |
| 1398 | } |
| 1399 | |
| 1400 | codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL); |
| 1401 | if (codec->ac97->bus == NULL) { |
| 1402 | kfree(codec->ac97); |
| 1403 | codec->ac97 = NULL; |
| 1404 | mutex_unlock(&codec->mutex); |
| 1405 | return -ENOMEM; |
| 1406 | } |
| 1407 | |
| 1408 | codec->ac97->bus->ops = ops; |
| 1409 | codec->ac97->num = num; |
Mark Brown | 2718625 | 2010-01-28 12:36:29 +0000 | [diff] [blame] | 1410 | codec->dev = &codec->ac97->dev; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1411 | mutex_unlock(&codec->mutex); |
| 1412 | return 0; |
| 1413 | } |
| 1414 | EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec); |
| 1415 | |
| 1416 | /** |
| 1417 | * snd_soc_free_ac97_codec - free AC97 codec device |
| 1418 | * @codec: audio codec |
| 1419 | * |
| 1420 | * Frees AC97 codec device resources. |
| 1421 | */ |
| 1422 | void snd_soc_free_ac97_codec(struct snd_soc_codec *codec) |
| 1423 | { |
| 1424 | mutex_lock(&codec->mutex); |
| 1425 | kfree(codec->ac97->bus); |
| 1426 | kfree(codec->ac97); |
| 1427 | codec->ac97 = NULL; |
| 1428 | mutex_unlock(&codec->mutex); |
| 1429 | } |
| 1430 | EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec); |
| 1431 | |
| 1432 | /** |
| 1433 | * snd_soc_update_bits - update codec register bits |
| 1434 | * @codec: audio codec |
| 1435 | * @reg: codec register |
| 1436 | * @mask: register mask |
| 1437 | * @value: new value |
| 1438 | * |
| 1439 | * Writes new register value. |
| 1440 | * |
| 1441 | * Returns 1 for change else 0. |
| 1442 | */ |
| 1443 | int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg, |
Daniel Ribeiro | 46f5822 | 2009-06-07 02:49:11 -0300 | [diff] [blame] | 1444 | unsigned int mask, unsigned int value) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1445 | { |
| 1446 | int change; |
Daniel Ribeiro | 46f5822 | 2009-06-07 02:49:11 -0300 | [diff] [blame] | 1447 | unsigned int old, new; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1448 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1449 | old = snd_soc_read(codec, reg); |
| 1450 | new = (old & ~mask) | value; |
| 1451 | change = old != new; |
| 1452 | if (change) |
| 1453 | snd_soc_write(codec, reg, new); |
| 1454 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1455 | return change; |
| 1456 | } |
| 1457 | EXPORT_SYMBOL_GPL(snd_soc_update_bits); |
| 1458 | |
| 1459 | /** |
Eero Nurkkala | 6c508c6 | 2009-10-30 13:34:03 +0200 | [diff] [blame] | 1460 | * snd_soc_update_bits_locked - update codec register bits |
| 1461 | * @codec: audio codec |
| 1462 | * @reg: codec register |
| 1463 | * @mask: register mask |
| 1464 | * @value: new value |
| 1465 | * |
| 1466 | * Writes new register value, and takes the codec mutex. |
| 1467 | * |
| 1468 | * Returns 1 for change else 0. |
| 1469 | */ |
Mark Brown | dd1b3d5 | 2009-12-04 14:22:03 +0000 | [diff] [blame] | 1470 | int snd_soc_update_bits_locked(struct snd_soc_codec *codec, |
| 1471 | unsigned short reg, unsigned int mask, |
| 1472 | unsigned int value) |
Eero Nurkkala | 6c508c6 | 2009-10-30 13:34:03 +0200 | [diff] [blame] | 1473 | { |
| 1474 | int change; |
| 1475 | |
| 1476 | mutex_lock(&codec->mutex); |
| 1477 | change = snd_soc_update_bits(codec, reg, mask, value); |
| 1478 | mutex_unlock(&codec->mutex); |
| 1479 | |
| 1480 | return change; |
| 1481 | } |
Mark Brown | dd1b3d5 | 2009-12-04 14:22:03 +0000 | [diff] [blame] | 1482 | EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked); |
Eero Nurkkala | 6c508c6 | 2009-10-30 13:34:03 +0200 | [diff] [blame] | 1483 | |
| 1484 | /** |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1485 | * snd_soc_test_bits - test register for change |
| 1486 | * @codec: audio codec |
| 1487 | * @reg: codec register |
| 1488 | * @mask: register mask |
| 1489 | * @value: new value |
| 1490 | * |
| 1491 | * Tests a register with a new value and checks if the new value is |
| 1492 | * different from the old value. |
| 1493 | * |
| 1494 | * Returns 1 for change else 0. |
| 1495 | */ |
| 1496 | int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg, |
Daniel Ribeiro | 46f5822 | 2009-06-07 02:49:11 -0300 | [diff] [blame] | 1497 | unsigned int mask, unsigned int value) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1498 | { |
| 1499 | int change; |
Daniel Ribeiro | 46f5822 | 2009-06-07 02:49:11 -0300 | [diff] [blame] | 1500 | unsigned int old, new; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1501 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1502 | old = snd_soc_read(codec, reg); |
| 1503 | new = (old & ~mask) | value; |
| 1504 | change = old != new; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1505 | |
| 1506 | return change; |
| 1507 | } |
| 1508 | EXPORT_SYMBOL_GPL(snd_soc_test_bits); |
| 1509 | |
| 1510 | /** |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1511 | * snd_soc_new_pcms - create new sound card and pcms |
| 1512 | * @socdev: the SoC audio device |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 1513 | * @idx: ALSA card index |
| 1514 | * @xid: card identification |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1515 | * |
| 1516 | * Create a new sound card based upon the codec and interface pcms. |
| 1517 | * |
| 1518 | * Returns 0 for success, else error. |
| 1519 | */ |
Liam Girdwood | bc7320c | 2007-02-01 12:26:07 +0100 | [diff] [blame] | 1520 | int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1521 | { |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 1522 | struct snd_soc_card *card = socdev->card; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 1523 | struct snd_soc_codec *codec = card->codec; |
Takashi Iwai | bd7dd77 | 2008-12-28 16:45:02 +0100 | [diff] [blame] | 1524 | int ret, i; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1525 | |
| 1526 | mutex_lock(&codec->mutex); |
| 1527 | |
| 1528 | /* register a sound card */ |
Takashi Iwai | bd7dd77 | 2008-12-28 16:45:02 +0100 | [diff] [blame] | 1529 | ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card); |
| 1530 | if (ret < 0) { |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1531 | printk(KERN_ERR "asoc: can't create sound card for codec %s\n", |
| 1532 | codec->name); |
| 1533 | mutex_unlock(&codec->mutex); |
Takashi Iwai | bd7dd77 | 2008-12-28 16:45:02 +0100 | [diff] [blame] | 1534 | return ret; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1535 | } |
| 1536 | |
Mark Brown | 452c5ea | 2009-05-17 21:41:23 +0100 | [diff] [blame] | 1537 | codec->socdev = socdev; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1538 | codec->card->dev = socdev->dev; |
| 1539 | codec->card->private_data = codec; |
| 1540 | strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver)); |
| 1541 | |
| 1542 | /* create the pcms */ |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 1543 | for (i = 0; i < card->num_links; i++) { |
| 1544 | ret = soc_new_pcm(socdev, &card->dai_link[i], i); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1545 | if (ret < 0) { |
| 1546 | printk(KERN_ERR "asoc: can't create pcm %s\n", |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 1547 | card->dai_link[i].stream_name); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1548 | mutex_unlock(&codec->mutex); |
| 1549 | return ret; |
| 1550 | } |
Barry Song | f773205 | 2009-11-12 12:01:47 +0800 | [diff] [blame] | 1551 | if (card->dai_link[i].codec_dai->ac97_control) { |
| 1552 | snd_ac97_dev_add_pdata(codec->ac97, |
| 1553 | card->dai_link[i].cpu_dai->ac97_pdata); |
| 1554 | } |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | mutex_unlock(&codec->mutex); |
| 1558 | return ret; |
| 1559 | } |
| 1560 | EXPORT_SYMBOL_GPL(snd_soc_new_pcms); |
| 1561 | |
| 1562 | /** |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1563 | * snd_soc_free_pcms - free sound card and pcms |
| 1564 | * @socdev: the SoC audio device |
| 1565 | * |
| 1566 | * Frees sound card and pcms associated with the socdev. |
| 1567 | * Also unregister the codec if it is an AC97 device. |
| 1568 | */ |
| 1569 | void snd_soc_free_pcms(struct snd_soc_device *socdev) |
| 1570 | { |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 1571 | struct snd_soc_codec *codec = socdev->card->codec; |
Liam Girdwood | a68660e | 2007-05-10 19:27:27 +0200 | [diff] [blame] | 1572 | #ifdef CONFIG_SND_SOC_AC97_BUS |
Liam Girdwood | 3c4b266 | 2008-07-07 16:07:17 +0100 | [diff] [blame] | 1573 | struct snd_soc_dai *codec_dai; |
Liam Girdwood | a68660e | 2007-05-10 19:27:27 +0200 | [diff] [blame] | 1574 | int i; |
| 1575 | #endif |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1576 | |
| 1577 | mutex_lock(&codec->mutex); |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 1578 | soc_cleanup_codec_debugfs(codec); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1579 | #ifdef CONFIG_SND_SOC_AC97_BUS |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 1580 | for (i = 0; i < codec->num_dai; i++) { |
Liam Girdwood | a68660e | 2007-05-10 19:27:27 +0200 | [diff] [blame] | 1581 | codec_dai = &codec->dai[i]; |
Atsushi Nemoto | d2314e0 | 2009-03-16 23:26:20 +0900 | [diff] [blame] | 1582 | if (codec_dai->ac97_control && codec->ac97 && |
| 1583 | strcmp(codec->name, "AC97") != 0) { |
Liam Girdwood | a68660e | 2007-05-10 19:27:27 +0200 | [diff] [blame] | 1584 | soc_ac97_dev_unregister(codec); |
| 1585 | goto free_card; |
| 1586 | } |
| 1587 | } |
| 1588 | free_card: |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1589 | #endif |
| 1590 | |
| 1591 | if (codec->card) |
| 1592 | snd_card_free(codec->card); |
| 1593 | device_remove_file(socdev->dev, &dev_attr_codec_reg); |
| 1594 | mutex_unlock(&codec->mutex); |
| 1595 | } |
| 1596 | EXPORT_SYMBOL_GPL(snd_soc_free_pcms); |
| 1597 | |
| 1598 | /** |
| 1599 | * snd_soc_set_runtime_hwparams - set the runtime hardware parameters |
| 1600 | * @substream: the pcm substream |
| 1601 | * @hw: the hardware parameters |
| 1602 | * |
| 1603 | * Sets the substream runtime hardware parameters. |
| 1604 | */ |
| 1605 | int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream, |
| 1606 | const struct snd_pcm_hardware *hw) |
| 1607 | { |
| 1608 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1609 | runtime->hw.info = hw->info; |
| 1610 | runtime->hw.formats = hw->formats; |
| 1611 | runtime->hw.period_bytes_min = hw->period_bytes_min; |
| 1612 | runtime->hw.period_bytes_max = hw->period_bytes_max; |
| 1613 | runtime->hw.periods_min = hw->periods_min; |
| 1614 | runtime->hw.periods_max = hw->periods_max; |
| 1615 | runtime->hw.buffer_bytes_max = hw->buffer_bytes_max; |
| 1616 | runtime->hw.fifo_size = hw->fifo_size; |
| 1617 | return 0; |
| 1618 | } |
| 1619 | EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams); |
| 1620 | |
| 1621 | /** |
| 1622 | * snd_soc_cnew - create new control |
| 1623 | * @_template: control template |
| 1624 | * @data: control private data |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 1625 | * @long_name: control long name |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1626 | * |
| 1627 | * Create a new mixer control from a template control. |
| 1628 | * |
| 1629 | * Returns 0 for success, else error. |
| 1630 | */ |
| 1631 | struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, |
| 1632 | void *data, char *long_name) |
| 1633 | { |
| 1634 | struct snd_kcontrol_new template; |
| 1635 | |
| 1636 | memcpy(&template, _template, sizeof(template)); |
| 1637 | if (long_name) |
| 1638 | template.name = long_name; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1639 | template.index = 0; |
| 1640 | |
| 1641 | return snd_ctl_new1(&template, data); |
| 1642 | } |
| 1643 | EXPORT_SYMBOL_GPL(snd_soc_cnew); |
| 1644 | |
| 1645 | /** |
Ian Molton | 3e8e195 | 2009-01-09 00:23:21 +0000 | [diff] [blame] | 1646 | * snd_soc_add_controls - add an array of controls to a codec. |
| 1647 | * Convienience function to add a list of controls. Many codecs were |
| 1648 | * duplicating this code. |
| 1649 | * |
| 1650 | * @codec: codec to add controls to |
| 1651 | * @controls: array of controls to add |
| 1652 | * @num_controls: number of elements in the array |
| 1653 | * |
| 1654 | * Return 0 for success, else error. |
| 1655 | */ |
| 1656 | int snd_soc_add_controls(struct snd_soc_codec *codec, |
| 1657 | const struct snd_kcontrol_new *controls, int num_controls) |
| 1658 | { |
| 1659 | struct snd_card *card = codec->card; |
| 1660 | int err, i; |
| 1661 | |
| 1662 | for (i = 0; i < num_controls; i++) { |
| 1663 | const struct snd_kcontrol_new *control = &controls[i]; |
| 1664 | err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL)); |
| 1665 | if (err < 0) { |
| 1666 | dev_err(codec->dev, "%s: Failed to add %s\n", |
| 1667 | codec->name, control->name); |
| 1668 | return err; |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | return 0; |
| 1673 | } |
| 1674 | EXPORT_SYMBOL_GPL(snd_soc_add_controls); |
| 1675 | |
| 1676 | /** |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1677 | * snd_soc_info_enum_double - enumerated double mixer info callback |
| 1678 | * @kcontrol: mixer control |
| 1679 | * @uinfo: control element information |
| 1680 | * |
| 1681 | * Callback to provide information about a double enumerated |
| 1682 | * mixer control. |
| 1683 | * |
| 1684 | * Returns 0 for success. |
| 1685 | */ |
| 1686 | int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol, |
| 1687 | struct snd_ctl_elem_info *uinfo) |
| 1688 | { |
| 1689 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
| 1690 | |
| 1691 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 1692 | uinfo->count = e->shift_l == e->shift_r ? 1 : 2; |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 1693 | uinfo->value.enumerated.items = e->max; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1694 | |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 1695 | if (uinfo->value.enumerated.item > e->max - 1) |
| 1696 | uinfo->value.enumerated.item = e->max - 1; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1697 | strcpy(uinfo->value.enumerated.name, |
| 1698 | e->texts[uinfo->value.enumerated.item]); |
| 1699 | return 0; |
| 1700 | } |
| 1701 | EXPORT_SYMBOL_GPL(snd_soc_info_enum_double); |
| 1702 | |
| 1703 | /** |
| 1704 | * snd_soc_get_enum_double - enumerated double mixer get callback |
| 1705 | * @kcontrol: mixer control |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 1706 | * @ucontrol: control element information |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1707 | * |
| 1708 | * Callback to get the value of a double enumerated mixer. |
| 1709 | * |
| 1710 | * Returns 0 for success. |
| 1711 | */ |
| 1712 | int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol, |
| 1713 | struct snd_ctl_elem_value *ucontrol) |
| 1714 | { |
| 1715 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1716 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
Daniel Ribeiro | 46f5822 | 2009-06-07 02:49:11 -0300 | [diff] [blame] | 1717 | unsigned int val, bitmask; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1718 | |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 1719 | for (bitmask = 1; bitmask < e->max; bitmask <<= 1) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1720 | ; |
| 1721 | val = snd_soc_read(codec, e->reg); |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 1722 | ucontrol->value.enumerated.item[0] |
| 1723 | = (val >> e->shift_l) & (bitmask - 1); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1724 | if (e->shift_l != e->shift_r) |
| 1725 | ucontrol->value.enumerated.item[1] = |
| 1726 | (val >> e->shift_r) & (bitmask - 1); |
| 1727 | |
| 1728 | return 0; |
| 1729 | } |
| 1730 | EXPORT_SYMBOL_GPL(snd_soc_get_enum_double); |
| 1731 | |
| 1732 | /** |
| 1733 | * snd_soc_put_enum_double - enumerated double mixer put callback |
| 1734 | * @kcontrol: mixer control |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 1735 | * @ucontrol: control element information |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1736 | * |
| 1737 | * Callback to set the value of a double enumerated mixer. |
| 1738 | * |
| 1739 | * Returns 0 for success. |
| 1740 | */ |
| 1741 | int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol, |
| 1742 | struct snd_ctl_elem_value *ucontrol) |
| 1743 | { |
| 1744 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 1745 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
Daniel Ribeiro | 46f5822 | 2009-06-07 02:49:11 -0300 | [diff] [blame] | 1746 | unsigned int val; |
| 1747 | unsigned int mask, bitmask; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1748 | |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 1749 | for (bitmask = 1; bitmask < e->max; bitmask <<= 1) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1750 | ; |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 1751 | if (ucontrol->value.enumerated.item[0] > e->max - 1) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1752 | return -EINVAL; |
| 1753 | val = ucontrol->value.enumerated.item[0] << e->shift_l; |
| 1754 | mask = (bitmask - 1) << e->shift_l; |
| 1755 | if (e->shift_l != e->shift_r) { |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 1756 | if (ucontrol->value.enumerated.item[1] > e->max - 1) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1757 | return -EINVAL; |
| 1758 | val |= ucontrol->value.enumerated.item[1] << e->shift_r; |
| 1759 | mask |= (bitmask - 1) << e->shift_r; |
| 1760 | } |
| 1761 | |
Eero Nurkkala | 6c508c6 | 2009-10-30 13:34:03 +0200 | [diff] [blame] | 1762 | return snd_soc_update_bits_locked(codec, e->reg, mask, val); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1763 | } |
| 1764 | EXPORT_SYMBOL_GPL(snd_soc_put_enum_double); |
| 1765 | |
| 1766 | /** |
Peter Ujfalusi | 2e72f8e | 2009-01-05 09:54:57 +0200 | [diff] [blame] | 1767 | * snd_soc_get_value_enum_double - semi enumerated double mixer get callback |
| 1768 | * @kcontrol: mixer control |
| 1769 | * @ucontrol: control element information |
| 1770 | * |
| 1771 | * Callback to get the value of a double semi enumerated mixer. |
| 1772 | * |
| 1773 | * Semi enumerated mixer: the enumerated items are referred as values. Can be |
| 1774 | * used for handling bitfield coded enumeration for example. |
| 1775 | * |
| 1776 | * Returns 0 for success. |
| 1777 | */ |
| 1778 | int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol, |
| 1779 | struct snd_ctl_elem_value *ucontrol) |
| 1780 | { |
| 1781 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
Peter Ujfalusi | 7415555 | 2009-01-08 13:34:29 +0200 | [diff] [blame] | 1782 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
Daniel Ribeiro | 46f5822 | 2009-06-07 02:49:11 -0300 | [diff] [blame] | 1783 | unsigned int reg_val, val, mux; |
Peter Ujfalusi | 2e72f8e | 2009-01-05 09:54:57 +0200 | [diff] [blame] | 1784 | |
| 1785 | reg_val = snd_soc_read(codec, e->reg); |
| 1786 | val = (reg_val >> e->shift_l) & e->mask; |
| 1787 | for (mux = 0; mux < e->max; mux++) { |
| 1788 | if (val == e->values[mux]) |
| 1789 | break; |
| 1790 | } |
| 1791 | ucontrol->value.enumerated.item[0] = mux; |
| 1792 | if (e->shift_l != e->shift_r) { |
| 1793 | val = (reg_val >> e->shift_r) & e->mask; |
| 1794 | for (mux = 0; mux < e->max; mux++) { |
| 1795 | if (val == e->values[mux]) |
| 1796 | break; |
| 1797 | } |
| 1798 | ucontrol->value.enumerated.item[1] = mux; |
| 1799 | } |
| 1800 | |
| 1801 | return 0; |
| 1802 | } |
| 1803 | EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double); |
| 1804 | |
| 1805 | /** |
| 1806 | * snd_soc_put_value_enum_double - semi enumerated double mixer put callback |
| 1807 | * @kcontrol: mixer control |
| 1808 | * @ucontrol: control element information |
| 1809 | * |
| 1810 | * Callback to set the value of a double semi enumerated mixer. |
| 1811 | * |
| 1812 | * Semi enumerated mixer: the enumerated items are referred as values. Can be |
| 1813 | * used for handling bitfield coded enumeration for example. |
| 1814 | * |
| 1815 | * Returns 0 for success. |
| 1816 | */ |
| 1817 | int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol, |
| 1818 | struct snd_ctl_elem_value *ucontrol) |
| 1819 | { |
| 1820 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
Peter Ujfalusi | 7415555 | 2009-01-08 13:34:29 +0200 | [diff] [blame] | 1821 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
Daniel Ribeiro | 46f5822 | 2009-06-07 02:49:11 -0300 | [diff] [blame] | 1822 | unsigned int val; |
| 1823 | unsigned int mask; |
Peter Ujfalusi | 2e72f8e | 2009-01-05 09:54:57 +0200 | [diff] [blame] | 1824 | |
| 1825 | if (ucontrol->value.enumerated.item[0] > e->max - 1) |
| 1826 | return -EINVAL; |
| 1827 | val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l; |
| 1828 | mask = e->mask << e->shift_l; |
| 1829 | if (e->shift_l != e->shift_r) { |
| 1830 | if (ucontrol->value.enumerated.item[1] > e->max - 1) |
| 1831 | return -EINVAL; |
| 1832 | val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r; |
| 1833 | mask |= e->mask << e->shift_r; |
| 1834 | } |
| 1835 | |
Eero Nurkkala | 6c508c6 | 2009-10-30 13:34:03 +0200 | [diff] [blame] | 1836 | return snd_soc_update_bits_locked(codec, e->reg, mask, val); |
Peter Ujfalusi | 2e72f8e | 2009-01-05 09:54:57 +0200 | [diff] [blame] | 1837 | } |
| 1838 | EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double); |
| 1839 | |
| 1840 | /** |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1841 | * snd_soc_info_enum_ext - external enumerated single mixer info callback |
| 1842 | * @kcontrol: mixer control |
| 1843 | * @uinfo: control element information |
| 1844 | * |
| 1845 | * Callback to provide information about an external enumerated |
| 1846 | * single mixer. |
| 1847 | * |
| 1848 | * Returns 0 for success. |
| 1849 | */ |
| 1850 | int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol, |
| 1851 | struct snd_ctl_elem_info *uinfo) |
| 1852 | { |
| 1853 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
| 1854 | |
| 1855 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 1856 | uinfo->count = 1; |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 1857 | uinfo->value.enumerated.items = e->max; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1858 | |
Jon Smirl | f8ba0b7 | 2008-07-29 11:42:27 +0100 | [diff] [blame] | 1859 | if (uinfo->value.enumerated.item > e->max - 1) |
| 1860 | uinfo->value.enumerated.item = e->max - 1; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1861 | strcpy(uinfo->value.enumerated.name, |
| 1862 | e->texts[uinfo->value.enumerated.item]); |
| 1863 | return 0; |
| 1864 | } |
| 1865 | EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext); |
| 1866 | |
| 1867 | /** |
| 1868 | * snd_soc_info_volsw_ext - external single mixer info callback |
| 1869 | * @kcontrol: mixer control |
| 1870 | * @uinfo: control element information |
| 1871 | * |
| 1872 | * Callback to provide information about a single external mixer control. |
| 1873 | * |
| 1874 | * Returns 0 for success. |
| 1875 | */ |
| 1876 | int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol, |
| 1877 | struct snd_ctl_elem_info *uinfo) |
| 1878 | { |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 1879 | int max = kcontrol->private_value; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1880 | |
Mark Brown | fd5dfad | 2009-04-15 21:37:46 +0100 | [diff] [blame] | 1881 | if (max == 1 && !strstr(kcontrol->id.name, " Volume")) |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 1882 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 1883 | else |
| 1884 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 1885 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1886 | uinfo->count = 1; |
| 1887 | uinfo->value.integer.min = 0; |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 1888 | uinfo->value.integer.max = max; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1889 | return 0; |
| 1890 | } |
| 1891 | EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext); |
| 1892 | |
| 1893 | /** |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1894 | * snd_soc_info_volsw - single mixer info callback |
| 1895 | * @kcontrol: mixer control |
| 1896 | * @uinfo: control element information |
| 1897 | * |
| 1898 | * Callback to provide information about a single mixer control. |
| 1899 | * |
| 1900 | * Returns 0 for success. |
| 1901 | */ |
| 1902 | int snd_soc_info_volsw(struct snd_kcontrol *kcontrol, |
| 1903 | struct snd_ctl_elem_info *uinfo) |
| 1904 | { |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 1905 | struct soc_mixer_control *mc = |
| 1906 | (struct soc_mixer_control *)kcontrol->private_value; |
| 1907 | int max = mc->max; |
Mark Brown | 762b8df | 2008-10-30 12:37:08 +0000 | [diff] [blame] | 1908 | unsigned int shift = mc->shift; |
Jon Smirl | 815ecf8d | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 1909 | unsigned int rshift = mc->rshift; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1910 | |
Mark Brown | fd5dfad | 2009-04-15 21:37:46 +0100 | [diff] [blame] | 1911 | if (max == 1 && !strstr(kcontrol->id.name, " Volume")) |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 1912 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 1913 | else |
| 1914 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 1915 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1916 | uinfo->count = shift == rshift ? 1 : 2; |
| 1917 | uinfo->value.integer.min = 0; |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 1918 | uinfo->value.integer.max = max; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1919 | return 0; |
| 1920 | } |
| 1921 | EXPORT_SYMBOL_GPL(snd_soc_info_volsw); |
| 1922 | |
| 1923 | /** |
| 1924 | * snd_soc_get_volsw - single mixer get callback |
| 1925 | * @kcontrol: mixer control |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 1926 | * @ucontrol: control element information |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1927 | * |
| 1928 | * Callback to get the value of a single mixer control. |
| 1929 | * |
| 1930 | * Returns 0 for success. |
| 1931 | */ |
| 1932 | int snd_soc_get_volsw(struct snd_kcontrol *kcontrol, |
| 1933 | struct snd_ctl_elem_value *ucontrol) |
| 1934 | { |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 1935 | struct soc_mixer_control *mc = |
| 1936 | (struct soc_mixer_control *)kcontrol->private_value; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1937 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
Jon Smirl | 815ecf8d | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 1938 | unsigned int reg = mc->reg; |
| 1939 | unsigned int shift = mc->shift; |
| 1940 | unsigned int rshift = mc->rshift; |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 1941 | int max = mc->max; |
Jon Smirl | 815ecf8d | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 1942 | unsigned int mask = (1 << fls(max)) - 1; |
| 1943 | unsigned int invert = mc->invert; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1944 | |
| 1945 | ucontrol->value.integer.value[0] = |
| 1946 | (snd_soc_read(codec, reg) >> shift) & mask; |
| 1947 | if (shift != rshift) |
| 1948 | ucontrol->value.integer.value[1] = |
| 1949 | (snd_soc_read(codec, reg) >> rshift) & mask; |
| 1950 | if (invert) { |
| 1951 | ucontrol->value.integer.value[0] = |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 1952 | max - ucontrol->value.integer.value[0]; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1953 | if (shift != rshift) |
| 1954 | ucontrol->value.integer.value[1] = |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 1955 | max - ucontrol->value.integer.value[1]; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | return 0; |
| 1959 | } |
| 1960 | EXPORT_SYMBOL_GPL(snd_soc_get_volsw); |
| 1961 | |
| 1962 | /** |
| 1963 | * snd_soc_put_volsw - single mixer put callback |
| 1964 | * @kcontrol: mixer control |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 1965 | * @ucontrol: control element information |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1966 | * |
| 1967 | * Callback to set the value of a single mixer control. |
| 1968 | * |
| 1969 | * Returns 0 for success. |
| 1970 | */ |
| 1971 | int snd_soc_put_volsw(struct snd_kcontrol *kcontrol, |
| 1972 | struct snd_ctl_elem_value *ucontrol) |
| 1973 | { |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 1974 | struct soc_mixer_control *mc = |
| 1975 | (struct soc_mixer_control *)kcontrol->private_value; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1976 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
Jon Smirl | 815ecf8d | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 1977 | unsigned int reg = mc->reg; |
| 1978 | unsigned int shift = mc->shift; |
| 1979 | unsigned int rshift = mc->rshift; |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 1980 | int max = mc->max; |
Jon Smirl | 815ecf8d | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 1981 | unsigned int mask = (1 << fls(max)) - 1; |
| 1982 | unsigned int invert = mc->invert; |
Daniel Ribeiro | 46f5822 | 2009-06-07 02:49:11 -0300 | [diff] [blame] | 1983 | unsigned int val, val2, val_mask; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1984 | |
| 1985 | val = (ucontrol->value.integer.value[0] & mask); |
| 1986 | if (invert) |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 1987 | val = max - val; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1988 | val_mask = mask << shift; |
| 1989 | val = val << shift; |
| 1990 | if (shift != rshift) { |
| 1991 | val2 = (ucontrol->value.integer.value[1] & mask); |
| 1992 | if (invert) |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 1993 | val2 = max - val2; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1994 | val_mask |= mask << rshift; |
| 1995 | val |= val2 << rshift; |
| 1996 | } |
Eero Nurkkala | 6c508c6 | 2009-10-30 13:34:03 +0200 | [diff] [blame] | 1997 | return snd_soc_update_bits_locked(codec, reg, val_mask, val); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 1998 | } |
| 1999 | EXPORT_SYMBOL_GPL(snd_soc_put_volsw); |
| 2000 | |
| 2001 | /** |
| 2002 | * snd_soc_info_volsw_2r - double mixer info callback |
| 2003 | * @kcontrol: mixer control |
| 2004 | * @uinfo: control element information |
| 2005 | * |
| 2006 | * Callback to provide information about a double mixer control that |
| 2007 | * spans 2 codec registers. |
| 2008 | * |
| 2009 | * Returns 0 for success. |
| 2010 | */ |
| 2011 | int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol, |
| 2012 | struct snd_ctl_elem_info *uinfo) |
| 2013 | { |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 2014 | struct soc_mixer_control *mc = |
| 2015 | (struct soc_mixer_control *)kcontrol->private_value; |
| 2016 | int max = mc->max; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2017 | |
Mark Brown | fd5dfad | 2009-04-15 21:37:46 +0100 | [diff] [blame] | 2018 | if (max == 1 && !strstr(kcontrol->id.name, " Volume")) |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 2019 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 2020 | else |
| 2021 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 2022 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2023 | uinfo->count = 2; |
| 2024 | uinfo->value.integer.min = 0; |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 2025 | uinfo->value.integer.max = max; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2026 | return 0; |
| 2027 | } |
| 2028 | EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r); |
| 2029 | |
| 2030 | /** |
| 2031 | * snd_soc_get_volsw_2r - double mixer get callback |
| 2032 | * @kcontrol: mixer control |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 2033 | * @ucontrol: control element information |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2034 | * |
| 2035 | * Callback to get the value of a double mixer control that spans 2 registers. |
| 2036 | * |
| 2037 | * Returns 0 for success. |
| 2038 | */ |
| 2039 | int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol, |
| 2040 | struct snd_ctl_elem_value *ucontrol) |
| 2041 | { |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 2042 | struct soc_mixer_control *mc = |
| 2043 | (struct soc_mixer_control *)kcontrol->private_value; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2044 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
Jon Smirl | 815ecf8d | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 2045 | unsigned int reg = mc->reg; |
| 2046 | unsigned int reg2 = mc->rreg; |
| 2047 | unsigned int shift = mc->shift; |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 2048 | int max = mc->max; |
Daniel Ribeiro | 46f5822 | 2009-06-07 02:49:11 -0300 | [diff] [blame] | 2049 | unsigned int mask = (1 << fls(max)) - 1; |
Jon Smirl | 815ecf8d | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 2050 | unsigned int invert = mc->invert; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2051 | |
| 2052 | ucontrol->value.integer.value[0] = |
| 2053 | (snd_soc_read(codec, reg) >> shift) & mask; |
| 2054 | ucontrol->value.integer.value[1] = |
| 2055 | (snd_soc_read(codec, reg2) >> shift) & mask; |
| 2056 | if (invert) { |
| 2057 | ucontrol->value.integer.value[0] = |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 2058 | max - ucontrol->value.integer.value[0]; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2059 | ucontrol->value.integer.value[1] = |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 2060 | max - ucontrol->value.integer.value[1]; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | return 0; |
| 2064 | } |
| 2065 | EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r); |
| 2066 | |
| 2067 | /** |
| 2068 | * snd_soc_put_volsw_2r - double mixer set callback |
| 2069 | * @kcontrol: mixer control |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 2070 | * @ucontrol: control element information |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2071 | * |
| 2072 | * Callback to set the value of a double mixer control that spans 2 registers. |
| 2073 | * |
| 2074 | * Returns 0 for success. |
| 2075 | */ |
| 2076 | int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol, |
| 2077 | struct snd_ctl_elem_value *ucontrol) |
| 2078 | { |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 2079 | struct soc_mixer_control *mc = |
| 2080 | (struct soc_mixer_control *)kcontrol->private_value; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2081 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
Jon Smirl | 815ecf8d | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 2082 | unsigned int reg = mc->reg; |
| 2083 | unsigned int reg2 = mc->rreg; |
| 2084 | unsigned int shift = mc->shift; |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 2085 | int max = mc->max; |
Jon Smirl | 815ecf8d | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 2086 | unsigned int mask = (1 << fls(max)) - 1; |
| 2087 | unsigned int invert = mc->invert; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2088 | int err; |
Daniel Ribeiro | 46f5822 | 2009-06-07 02:49:11 -0300 | [diff] [blame] | 2089 | unsigned int val, val2, val_mask; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2090 | |
| 2091 | val_mask = mask << shift; |
| 2092 | val = (ucontrol->value.integer.value[0] & mask); |
| 2093 | val2 = (ucontrol->value.integer.value[1] & mask); |
| 2094 | |
| 2095 | if (invert) { |
Philipp Zabel | a7a4ac8 | 2008-01-10 14:37:42 +0100 | [diff] [blame] | 2096 | val = max - val; |
| 2097 | val2 = max - val2; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | val = val << shift; |
| 2101 | val2 = val2 << shift; |
| 2102 | |
Eero Nurkkala | 6c508c6 | 2009-10-30 13:34:03 +0200 | [diff] [blame] | 2103 | err = snd_soc_update_bits_locked(codec, reg, val_mask, val); |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 2104 | if (err < 0) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2105 | return err; |
| 2106 | |
Eero Nurkkala | 6c508c6 | 2009-10-30 13:34:03 +0200 | [diff] [blame] | 2107 | err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2108 | return err; |
| 2109 | } |
| 2110 | EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r); |
| 2111 | |
Mark Brown | e13ac2e | 2008-05-28 17:58:05 +0100 | [diff] [blame] | 2112 | /** |
| 2113 | * snd_soc_info_volsw_s8 - signed mixer info callback |
| 2114 | * @kcontrol: mixer control |
| 2115 | * @uinfo: control element information |
| 2116 | * |
| 2117 | * Callback to provide information about a signed mixer control. |
| 2118 | * |
| 2119 | * Returns 0 for success. |
| 2120 | */ |
| 2121 | int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol, |
| 2122 | struct snd_ctl_elem_info *uinfo) |
| 2123 | { |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 2124 | struct soc_mixer_control *mc = |
| 2125 | (struct soc_mixer_control *)kcontrol->private_value; |
| 2126 | int max = mc->max; |
| 2127 | int min = mc->min; |
Mark Brown | e13ac2e | 2008-05-28 17:58:05 +0100 | [diff] [blame] | 2128 | |
| 2129 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 2130 | uinfo->count = 2; |
| 2131 | uinfo->value.integer.min = 0; |
| 2132 | uinfo->value.integer.max = max-min; |
| 2133 | return 0; |
| 2134 | } |
| 2135 | EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8); |
| 2136 | |
| 2137 | /** |
| 2138 | * snd_soc_get_volsw_s8 - signed mixer get callback |
| 2139 | * @kcontrol: mixer control |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 2140 | * @ucontrol: control element information |
Mark Brown | e13ac2e | 2008-05-28 17:58:05 +0100 | [diff] [blame] | 2141 | * |
| 2142 | * Callback to get the value of a signed mixer control. |
| 2143 | * |
| 2144 | * Returns 0 for success. |
| 2145 | */ |
| 2146 | int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol, |
| 2147 | struct snd_ctl_elem_value *ucontrol) |
| 2148 | { |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 2149 | struct soc_mixer_control *mc = |
| 2150 | (struct soc_mixer_control *)kcontrol->private_value; |
Mark Brown | e13ac2e | 2008-05-28 17:58:05 +0100 | [diff] [blame] | 2151 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
Jon Smirl | 815ecf8d | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 2152 | unsigned int reg = mc->reg; |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 2153 | int min = mc->min; |
Mark Brown | e13ac2e | 2008-05-28 17:58:05 +0100 | [diff] [blame] | 2154 | int val = snd_soc_read(codec, reg); |
| 2155 | |
| 2156 | ucontrol->value.integer.value[0] = |
| 2157 | ((signed char)(val & 0xff))-min; |
| 2158 | ucontrol->value.integer.value[1] = |
| 2159 | ((signed char)((val >> 8) & 0xff))-min; |
| 2160 | return 0; |
| 2161 | } |
| 2162 | EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8); |
| 2163 | |
| 2164 | /** |
| 2165 | * snd_soc_put_volsw_sgn - signed mixer put callback |
| 2166 | * @kcontrol: mixer control |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 2167 | * @ucontrol: control element information |
Mark Brown | e13ac2e | 2008-05-28 17:58:05 +0100 | [diff] [blame] | 2168 | * |
| 2169 | * Callback to set the value of a signed mixer control. |
| 2170 | * |
| 2171 | * Returns 0 for success. |
| 2172 | */ |
| 2173 | int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol, |
| 2174 | struct snd_ctl_elem_value *ucontrol) |
| 2175 | { |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 2176 | struct soc_mixer_control *mc = |
| 2177 | (struct soc_mixer_control *)kcontrol->private_value; |
Mark Brown | e13ac2e | 2008-05-28 17:58:05 +0100 | [diff] [blame] | 2178 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
Jon Smirl | 815ecf8d | 2008-07-29 10:22:24 -0400 | [diff] [blame] | 2179 | unsigned int reg = mc->reg; |
Jon Smirl | 4eaa981 | 2008-07-29 11:42:26 +0100 | [diff] [blame] | 2180 | int min = mc->min; |
Daniel Ribeiro | 46f5822 | 2009-06-07 02:49:11 -0300 | [diff] [blame] | 2181 | unsigned int val; |
Mark Brown | e13ac2e | 2008-05-28 17:58:05 +0100 | [diff] [blame] | 2182 | |
| 2183 | val = (ucontrol->value.integer.value[0]+min) & 0xff; |
| 2184 | val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8; |
| 2185 | |
Eero Nurkkala | 6c508c6 | 2009-10-30 13:34:03 +0200 | [diff] [blame] | 2186 | return snd_soc_update_bits_locked(codec, reg, 0xffff, val); |
Mark Brown | e13ac2e | 2008-05-28 17:58:05 +0100 | [diff] [blame] | 2187 | } |
| 2188 | EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8); |
| 2189 | |
Liam Girdwood | 8c6529d | 2008-07-08 13:19:13 +0100 | [diff] [blame] | 2190 | /** |
| 2191 | * snd_soc_dai_set_sysclk - configure DAI system or master clock. |
| 2192 | * @dai: DAI |
| 2193 | * @clk_id: DAI specific clock ID |
| 2194 | * @freq: new clock frequency in Hz |
| 2195 | * @dir: new clock direction - input/output. |
| 2196 | * |
| 2197 | * Configures the DAI master (MCLK) or system (SYSCLK) clocking. |
| 2198 | */ |
| 2199 | int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id, |
| 2200 | unsigned int freq, int dir) |
| 2201 | { |
Mark Brown | 3f1a4d8 | 2009-04-15 21:35:26 +0100 | [diff] [blame] | 2202 | if (dai->ops && dai->ops->set_sysclk) |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 2203 | return dai->ops->set_sysclk(dai, clk_id, freq, dir); |
Liam Girdwood | 8c6529d | 2008-07-08 13:19:13 +0100 | [diff] [blame] | 2204 | else |
| 2205 | return -EINVAL; |
| 2206 | } |
| 2207 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk); |
| 2208 | |
| 2209 | /** |
| 2210 | * snd_soc_dai_set_clkdiv - configure DAI clock dividers. |
| 2211 | * @dai: DAI |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 2212 | * @div_id: DAI specific clock divider ID |
Liam Girdwood | 8c6529d | 2008-07-08 13:19:13 +0100 | [diff] [blame] | 2213 | * @div: new clock divisor. |
| 2214 | * |
| 2215 | * Configures the clock dividers. This is used to derive the best DAI bit and |
| 2216 | * frame clocks from the system or master clock. It's best to set the DAI bit |
| 2217 | * and frame clocks as low as possible to save system power. |
| 2218 | */ |
| 2219 | int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai, |
| 2220 | int div_id, int div) |
| 2221 | { |
Mark Brown | 3f1a4d8 | 2009-04-15 21:35:26 +0100 | [diff] [blame] | 2222 | if (dai->ops && dai->ops->set_clkdiv) |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 2223 | return dai->ops->set_clkdiv(dai, div_id, div); |
Liam Girdwood | 8c6529d | 2008-07-08 13:19:13 +0100 | [diff] [blame] | 2224 | else |
| 2225 | return -EINVAL; |
| 2226 | } |
| 2227 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv); |
| 2228 | |
| 2229 | /** |
| 2230 | * snd_soc_dai_set_pll - configure DAI PLL. |
| 2231 | * @dai: DAI |
| 2232 | * @pll_id: DAI specific PLL ID |
Mark Brown | 8548803 | 2009-09-05 18:52:16 +0100 | [diff] [blame] | 2233 | * @source: DAI specific source for the PLL |
Liam Girdwood | 8c6529d | 2008-07-08 13:19:13 +0100 | [diff] [blame] | 2234 | * @freq_in: PLL input clock frequency in Hz |
| 2235 | * @freq_out: requested PLL output clock frequency in Hz |
| 2236 | * |
| 2237 | * Configures and enables PLL to generate output clock based on input clock. |
| 2238 | */ |
Mark Brown | 8548803 | 2009-09-05 18:52:16 +0100 | [diff] [blame] | 2239 | int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source, |
| 2240 | unsigned int freq_in, unsigned int freq_out) |
Liam Girdwood | 8c6529d | 2008-07-08 13:19:13 +0100 | [diff] [blame] | 2241 | { |
Mark Brown | 3f1a4d8 | 2009-04-15 21:35:26 +0100 | [diff] [blame] | 2242 | if (dai->ops && dai->ops->set_pll) |
Mark Brown | 8548803 | 2009-09-05 18:52:16 +0100 | [diff] [blame] | 2243 | return dai->ops->set_pll(dai, pll_id, source, |
| 2244 | freq_in, freq_out); |
Liam Girdwood | 8c6529d | 2008-07-08 13:19:13 +0100 | [diff] [blame] | 2245 | else |
| 2246 | return -EINVAL; |
| 2247 | } |
| 2248 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll); |
| 2249 | |
| 2250 | /** |
| 2251 | * snd_soc_dai_set_fmt - configure DAI hardware audio format. |
| 2252 | * @dai: DAI |
Liam Girdwood | 8c6529d | 2008-07-08 13:19:13 +0100 | [diff] [blame] | 2253 | * @fmt: SND_SOC_DAIFMT_ format value. |
| 2254 | * |
| 2255 | * Configures the DAI hardware format and clocking. |
| 2256 | */ |
| 2257 | int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) |
| 2258 | { |
Mark Brown | 3f1a4d8 | 2009-04-15 21:35:26 +0100 | [diff] [blame] | 2259 | if (dai->ops && dai->ops->set_fmt) |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 2260 | return dai->ops->set_fmt(dai, fmt); |
Liam Girdwood | 8c6529d | 2008-07-08 13:19:13 +0100 | [diff] [blame] | 2261 | else |
| 2262 | return -EINVAL; |
| 2263 | } |
| 2264 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt); |
| 2265 | |
| 2266 | /** |
| 2267 | * snd_soc_dai_set_tdm_slot - configure DAI TDM. |
| 2268 | * @dai: DAI |
Daniel Ribeiro | a5479e3 | 2009-06-15 21:44:31 -0300 | [diff] [blame] | 2269 | * @tx_mask: bitmask representing active TX slots. |
| 2270 | * @rx_mask: bitmask representing active RX slots. |
Liam Girdwood | 8c6529d | 2008-07-08 13:19:13 +0100 | [diff] [blame] | 2271 | * @slots: Number of slots in use. |
Daniel Ribeiro | a5479e3 | 2009-06-15 21:44:31 -0300 | [diff] [blame] | 2272 | * @slot_width: Width in bits for each slot. |
Liam Girdwood | 8c6529d | 2008-07-08 13:19:13 +0100 | [diff] [blame] | 2273 | * |
| 2274 | * Configures a DAI for TDM operation. Both mask and slots are codec and DAI |
| 2275 | * specific. |
| 2276 | */ |
| 2277 | int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai, |
Daniel Ribeiro | a5479e3 | 2009-06-15 21:44:31 -0300 | [diff] [blame] | 2278 | unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width) |
Liam Girdwood | 8c6529d | 2008-07-08 13:19:13 +0100 | [diff] [blame] | 2279 | { |
Mark Brown | 3f1a4d8 | 2009-04-15 21:35:26 +0100 | [diff] [blame] | 2280 | if (dai->ops && dai->ops->set_tdm_slot) |
Daniel Ribeiro | a5479e3 | 2009-06-15 21:44:31 -0300 | [diff] [blame] | 2281 | return dai->ops->set_tdm_slot(dai, tx_mask, rx_mask, |
| 2282 | slots, slot_width); |
Liam Girdwood | 8c6529d | 2008-07-08 13:19:13 +0100 | [diff] [blame] | 2283 | else |
| 2284 | return -EINVAL; |
| 2285 | } |
| 2286 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot); |
| 2287 | |
| 2288 | /** |
Barry Song | 472df3c | 2009-09-12 01:16:29 +0800 | [diff] [blame] | 2289 | * snd_soc_dai_set_channel_map - configure DAI audio channel map |
| 2290 | * @dai: DAI |
| 2291 | * @tx_num: how many TX channels |
| 2292 | * @tx_slot: pointer to an array which imply the TX slot number channel |
| 2293 | * 0~num-1 uses |
| 2294 | * @rx_num: how many RX channels |
| 2295 | * @rx_slot: pointer to an array which imply the RX slot number channel |
| 2296 | * 0~num-1 uses |
| 2297 | * |
| 2298 | * configure the relationship between channel number and TDM slot number. |
| 2299 | */ |
| 2300 | int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai, |
| 2301 | unsigned int tx_num, unsigned int *tx_slot, |
| 2302 | unsigned int rx_num, unsigned int *rx_slot) |
| 2303 | { |
| 2304 | if (dai->ops && dai->ops->set_channel_map) |
| 2305 | return dai->ops->set_channel_map(dai, tx_num, tx_slot, |
| 2306 | rx_num, rx_slot); |
| 2307 | else |
| 2308 | return -EINVAL; |
| 2309 | } |
| 2310 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map); |
| 2311 | |
| 2312 | /** |
Liam Girdwood | 8c6529d | 2008-07-08 13:19:13 +0100 | [diff] [blame] | 2313 | * snd_soc_dai_set_tristate - configure DAI system or master clock. |
| 2314 | * @dai: DAI |
| 2315 | * @tristate: tristate enable |
| 2316 | * |
| 2317 | * Tristates the DAI so that others can use it. |
| 2318 | */ |
| 2319 | int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate) |
| 2320 | { |
Mark Brown | 3f1a4d8 | 2009-04-15 21:35:26 +0100 | [diff] [blame] | 2321 | if (dai->ops && dai->ops->set_tristate) |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 2322 | return dai->ops->set_tristate(dai, tristate); |
Liam Girdwood | 8c6529d | 2008-07-08 13:19:13 +0100 | [diff] [blame] | 2323 | else |
| 2324 | return -EINVAL; |
| 2325 | } |
| 2326 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate); |
| 2327 | |
| 2328 | /** |
| 2329 | * snd_soc_dai_digital_mute - configure DAI system or master clock. |
| 2330 | * @dai: DAI |
| 2331 | * @mute: mute enable |
| 2332 | * |
| 2333 | * Mutes the DAI DAC. |
| 2334 | */ |
| 2335 | int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute) |
| 2336 | { |
Mark Brown | 3f1a4d8 | 2009-04-15 21:35:26 +0100 | [diff] [blame] | 2337 | if (dai->ops && dai->ops->digital_mute) |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 2338 | return dai->ops->digital_mute(dai, mute); |
Liam Girdwood | 8c6529d | 2008-07-08 13:19:13 +0100 | [diff] [blame] | 2339 | else |
| 2340 | return -EINVAL; |
| 2341 | } |
| 2342 | EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute); |
| 2343 | |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 2344 | /** |
| 2345 | * snd_soc_register_card - Register a card with the ASoC core |
| 2346 | * |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 2347 | * @card: Card to register |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 2348 | * |
| 2349 | * Note that currently this is an internal only function: it will be |
| 2350 | * exposed to machine drivers after further backporting of ASoC v2 |
| 2351 | * registration APIs. |
| 2352 | */ |
| 2353 | static int snd_soc_register_card(struct snd_soc_card *card) |
| 2354 | { |
| 2355 | if (!card->name || !card->dev) |
| 2356 | return -EINVAL; |
| 2357 | |
| 2358 | INIT_LIST_HEAD(&card->list); |
| 2359 | card->instantiated = 0; |
| 2360 | |
| 2361 | mutex_lock(&client_mutex); |
| 2362 | list_add(&card->list, &card_list); |
Mark Brown | 435c5e2 | 2008-12-04 15:32:53 +0000 | [diff] [blame] | 2363 | snd_soc_instantiate_cards(); |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 2364 | mutex_unlock(&client_mutex); |
| 2365 | |
| 2366 | dev_dbg(card->dev, "Registered card '%s'\n", card->name); |
| 2367 | |
| 2368 | return 0; |
| 2369 | } |
| 2370 | |
| 2371 | /** |
| 2372 | * snd_soc_unregister_card - Unregister a card with the ASoC core |
| 2373 | * |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 2374 | * @card: Card to unregister |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 2375 | * |
| 2376 | * Note that currently this is an internal only function: it will be |
| 2377 | * exposed to machine drivers after further backporting of ASoC v2 |
| 2378 | * registration APIs. |
| 2379 | */ |
| 2380 | static int snd_soc_unregister_card(struct snd_soc_card *card) |
| 2381 | { |
| 2382 | mutex_lock(&client_mutex); |
| 2383 | list_del(&card->list); |
| 2384 | mutex_unlock(&client_mutex); |
| 2385 | |
| 2386 | dev_dbg(card->dev, "Unregistered card '%s'\n", card->name); |
| 2387 | |
| 2388 | return 0; |
| 2389 | } |
| 2390 | |
Mark Brown | 9115171 | 2008-11-30 23:31:24 +0000 | [diff] [blame] | 2391 | /** |
| 2392 | * snd_soc_register_dai - Register a DAI with the ASoC core |
| 2393 | * |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 2394 | * @dai: DAI to register |
Mark Brown | 9115171 | 2008-11-30 23:31:24 +0000 | [diff] [blame] | 2395 | */ |
| 2396 | int snd_soc_register_dai(struct snd_soc_dai *dai) |
| 2397 | { |
| 2398 | if (!dai->name) |
| 2399 | return -EINVAL; |
| 2400 | |
| 2401 | /* The device should become mandatory over time */ |
| 2402 | if (!dai->dev) |
| 2403 | printk(KERN_WARNING "No device for DAI %s\n", dai->name); |
| 2404 | |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 2405 | if (!dai->ops) |
| 2406 | dai->ops = &null_dai_ops; |
| 2407 | |
Mark Brown | 9115171 | 2008-11-30 23:31:24 +0000 | [diff] [blame] | 2408 | INIT_LIST_HEAD(&dai->list); |
| 2409 | |
| 2410 | mutex_lock(&client_mutex); |
| 2411 | list_add(&dai->list, &dai_list); |
Mark Brown | 435c5e2 | 2008-12-04 15:32:53 +0000 | [diff] [blame] | 2412 | snd_soc_instantiate_cards(); |
Mark Brown | 9115171 | 2008-11-30 23:31:24 +0000 | [diff] [blame] | 2413 | mutex_unlock(&client_mutex); |
| 2414 | |
| 2415 | pr_debug("Registered DAI '%s'\n", dai->name); |
| 2416 | |
| 2417 | return 0; |
| 2418 | } |
| 2419 | EXPORT_SYMBOL_GPL(snd_soc_register_dai); |
| 2420 | |
| 2421 | /** |
| 2422 | * snd_soc_unregister_dai - Unregister a DAI from the ASoC core |
| 2423 | * |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 2424 | * @dai: DAI to unregister |
Mark Brown | 9115171 | 2008-11-30 23:31:24 +0000 | [diff] [blame] | 2425 | */ |
| 2426 | void snd_soc_unregister_dai(struct snd_soc_dai *dai) |
| 2427 | { |
| 2428 | mutex_lock(&client_mutex); |
| 2429 | list_del(&dai->list); |
| 2430 | mutex_unlock(&client_mutex); |
| 2431 | |
| 2432 | pr_debug("Unregistered DAI '%s'\n", dai->name); |
| 2433 | } |
| 2434 | EXPORT_SYMBOL_GPL(snd_soc_unregister_dai); |
| 2435 | |
| 2436 | /** |
| 2437 | * snd_soc_register_dais - Register multiple DAIs with the ASoC core |
| 2438 | * |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 2439 | * @dai: Array of DAIs to register |
| 2440 | * @count: Number of DAIs |
Mark Brown | 9115171 | 2008-11-30 23:31:24 +0000 | [diff] [blame] | 2441 | */ |
| 2442 | int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count) |
| 2443 | { |
| 2444 | int i, ret; |
| 2445 | |
| 2446 | for (i = 0; i < count; i++) { |
| 2447 | ret = snd_soc_register_dai(&dai[i]); |
| 2448 | if (ret != 0) |
| 2449 | goto err; |
| 2450 | } |
| 2451 | |
| 2452 | return 0; |
| 2453 | |
| 2454 | err: |
| 2455 | for (i--; i >= 0; i--) |
| 2456 | snd_soc_unregister_dai(&dai[i]); |
| 2457 | |
| 2458 | return ret; |
| 2459 | } |
| 2460 | EXPORT_SYMBOL_GPL(snd_soc_register_dais); |
| 2461 | |
| 2462 | /** |
| 2463 | * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core |
| 2464 | * |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 2465 | * @dai: Array of DAIs to unregister |
| 2466 | * @count: Number of DAIs |
Mark Brown | 9115171 | 2008-11-30 23:31:24 +0000 | [diff] [blame] | 2467 | */ |
| 2468 | void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count) |
| 2469 | { |
| 2470 | int i; |
| 2471 | |
| 2472 | for (i = 0; i < count; i++) |
| 2473 | snd_soc_unregister_dai(&dai[i]); |
| 2474 | } |
| 2475 | EXPORT_SYMBOL_GPL(snd_soc_unregister_dais); |
| 2476 | |
Mark Brown | 12a48a8c | 2008-12-03 19:40:30 +0000 | [diff] [blame] | 2477 | /** |
| 2478 | * snd_soc_register_platform - Register a platform with the ASoC core |
| 2479 | * |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 2480 | * @platform: platform to register |
Mark Brown | 12a48a8c | 2008-12-03 19:40:30 +0000 | [diff] [blame] | 2481 | */ |
| 2482 | int snd_soc_register_platform(struct snd_soc_platform *platform) |
| 2483 | { |
| 2484 | if (!platform->name) |
| 2485 | return -EINVAL; |
| 2486 | |
| 2487 | INIT_LIST_HEAD(&platform->list); |
| 2488 | |
| 2489 | mutex_lock(&client_mutex); |
| 2490 | list_add(&platform->list, &platform_list); |
Mark Brown | 435c5e2 | 2008-12-04 15:32:53 +0000 | [diff] [blame] | 2491 | snd_soc_instantiate_cards(); |
Mark Brown | 12a48a8c | 2008-12-03 19:40:30 +0000 | [diff] [blame] | 2492 | mutex_unlock(&client_mutex); |
| 2493 | |
| 2494 | pr_debug("Registered platform '%s'\n", platform->name); |
| 2495 | |
| 2496 | return 0; |
| 2497 | } |
| 2498 | EXPORT_SYMBOL_GPL(snd_soc_register_platform); |
| 2499 | |
| 2500 | /** |
| 2501 | * snd_soc_unregister_platform - Unregister a platform from the ASoC core |
| 2502 | * |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 2503 | * @platform: platform to unregister |
Mark Brown | 12a48a8c | 2008-12-03 19:40:30 +0000 | [diff] [blame] | 2504 | */ |
| 2505 | void snd_soc_unregister_platform(struct snd_soc_platform *platform) |
| 2506 | { |
| 2507 | mutex_lock(&client_mutex); |
| 2508 | list_del(&platform->list); |
| 2509 | mutex_unlock(&client_mutex); |
| 2510 | |
| 2511 | pr_debug("Unregistered platform '%s'\n", platform->name); |
| 2512 | } |
| 2513 | EXPORT_SYMBOL_GPL(snd_soc_unregister_platform); |
| 2514 | |
Mark Brown | 151ab22 | 2009-05-09 16:22:58 +0100 | [diff] [blame] | 2515 | static u64 codec_format_map[] = { |
| 2516 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE, |
| 2517 | SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE, |
| 2518 | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE, |
| 2519 | SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE, |
| 2520 | SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE, |
| 2521 | SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE, |
| 2522 | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, |
| 2523 | SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, |
| 2524 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE, |
| 2525 | SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE, |
| 2526 | SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE, |
| 2527 | SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE, |
| 2528 | SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE, |
| 2529 | SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE, |
| 2530 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE |
| 2531 | | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE, |
| 2532 | }; |
| 2533 | |
| 2534 | /* Fix up the DAI formats for endianness: codecs don't actually see |
| 2535 | * the endianness of the data but we're using the CPU format |
| 2536 | * definitions which do need to include endianness so we ensure that |
| 2537 | * codec DAIs always have both big and little endian variants set. |
| 2538 | */ |
| 2539 | static void fixup_codec_formats(struct snd_soc_pcm_stream *stream) |
| 2540 | { |
| 2541 | int i; |
| 2542 | |
| 2543 | for (i = 0; i < ARRAY_SIZE(codec_format_map); i++) |
| 2544 | if (stream->formats & codec_format_map[i]) |
| 2545 | stream->formats |= codec_format_map[i]; |
| 2546 | } |
| 2547 | |
Mark Brown | 0d0cf00 | 2008-12-10 14:32:45 +0000 | [diff] [blame] | 2548 | /** |
| 2549 | * snd_soc_register_codec - Register a codec with the ASoC core |
| 2550 | * |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 2551 | * @codec: codec to register |
Mark Brown | 0d0cf00 | 2008-12-10 14:32:45 +0000 | [diff] [blame] | 2552 | */ |
| 2553 | int snd_soc_register_codec(struct snd_soc_codec *codec) |
| 2554 | { |
Mark Brown | 151ab22 | 2009-05-09 16:22:58 +0100 | [diff] [blame] | 2555 | int i; |
| 2556 | |
Mark Brown | 0d0cf00 | 2008-12-10 14:32:45 +0000 | [diff] [blame] | 2557 | if (!codec->name) |
| 2558 | return -EINVAL; |
| 2559 | |
| 2560 | /* The device should become mandatory over time */ |
| 2561 | if (!codec->dev) |
| 2562 | printk(KERN_WARNING "No device for codec %s\n", codec->name); |
| 2563 | |
| 2564 | INIT_LIST_HEAD(&codec->list); |
| 2565 | |
Mark Brown | 151ab22 | 2009-05-09 16:22:58 +0100 | [diff] [blame] | 2566 | for (i = 0; i < codec->num_dai; i++) { |
| 2567 | fixup_codec_formats(&codec->dai[i].playback); |
| 2568 | fixup_codec_formats(&codec->dai[i].capture); |
| 2569 | } |
| 2570 | |
Mark Brown | 0d0cf00 | 2008-12-10 14:32:45 +0000 | [diff] [blame] | 2571 | mutex_lock(&client_mutex); |
| 2572 | list_add(&codec->list, &codec_list); |
| 2573 | snd_soc_instantiate_cards(); |
| 2574 | mutex_unlock(&client_mutex); |
| 2575 | |
| 2576 | pr_debug("Registered codec '%s'\n", codec->name); |
| 2577 | |
| 2578 | return 0; |
| 2579 | } |
| 2580 | EXPORT_SYMBOL_GPL(snd_soc_register_codec); |
| 2581 | |
| 2582 | /** |
| 2583 | * snd_soc_unregister_codec - Unregister a codec from the ASoC core |
| 2584 | * |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 2585 | * @codec: codec to unregister |
Mark Brown | 0d0cf00 | 2008-12-10 14:32:45 +0000 | [diff] [blame] | 2586 | */ |
| 2587 | void snd_soc_unregister_codec(struct snd_soc_codec *codec) |
| 2588 | { |
| 2589 | mutex_lock(&client_mutex); |
| 2590 | list_del(&codec->list); |
| 2591 | mutex_unlock(&client_mutex); |
| 2592 | |
| 2593 | pr_debug("Unregistered codec '%s'\n", codec->name); |
| 2594 | } |
| 2595 | EXPORT_SYMBOL_GPL(snd_soc_unregister_codec); |
| 2596 | |
Takashi Iwai | c9b3a40 | 2008-12-10 07:47:22 +0100 | [diff] [blame] | 2597 | static int __init snd_soc_init(void) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2598 | { |
Mark Brown | 384c89e | 2008-12-03 17:34:03 +0000 | [diff] [blame] | 2599 | #ifdef CONFIG_DEBUG_FS |
| 2600 | debugfs_root = debugfs_create_dir("asoc", NULL); |
| 2601 | if (IS_ERR(debugfs_root) || !debugfs_root) { |
| 2602 | printk(KERN_WARNING |
| 2603 | "ASoC: Failed to create debugfs directory\n"); |
| 2604 | debugfs_root = NULL; |
| 2605 | } |
| 2606 | #endif |
| 2607 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2608 | return platform_driver_register(&soc_driver); |
| 2609 | } |
| 2610 | |
Mark Brown | 7d8c16a | 2008-11-30 22:11:24 +0000 | [diff] [blame] | 2611 | static void __exit snd_soc_exit(void) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2612 | { |
Mark Brown | 384c89e | 2008-12-03 17:34:03 +0000 | [diff] [blame] | 2613 | #ifdef CONFIG_DEBUG_FS |
| 2614 | debugfs_remove_recursive(debugfs_root); |
| 2615 | #endif |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 2616 | platform_driver_unregister(&soc_driver); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2617 | } |
| 2618 | |
| 2619 | module_init(snd_soc_init); |
| 2620 | module_exit(snd_soc_exit); |
| 2621 | |
| 2622 | /* Module information */ |
Liam Girdwood | d331124 | 2008-10-12 13:17:36 +0100 | [diff] [blame] | 2623 | MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2624 | MODULE_DESCRIPTION("ALSA SoC Core"); |
| 2625 | MODULE_LICENSE("GPL"); |
Kay Sievers | 8b45a20 | 2008-04-14 13:33:36 +0200 | [diff] [blame] | 2626 | MODULE_ALIAS("platform:soc-audio"); |