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