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