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