blob: 41926093539e4c92a05feef68fb3f64e754480b7 [file] [log] [blame]
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwood0664d882006-12-18 14:39:02 +01005 * Copyright 2005 Openedhand Ltd.
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00006 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
Liam Girdwood0664d882006-12-18 14:39:02 +01008 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01009 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood0664d882006-12-18 14:39:02 +010010 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020012 *
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 Mandarinodb2a4162006-10-06 18:31:09 +020018 * 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 Kisky12ef1932008-10-13 17:42:14 -070031#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020032#include <linux/platform_device.h>
Mark Brown10f672f2011-09-20 11:41:54 +010033#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Marek Vasut474828a2009-07-22 13:01:03 +020035#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020036#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000037#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020038#include <sound/pcm.h>
39#include <sound/pcm_params.h>
40#include <sound/soc.h>
Liam Girdwood13e13a32011-01-31 21:30:52 +000041#include <sound/soc-dsp.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020042#include <sound/initval.h>
43
Mark Browna8b1d342010-11-03 18:05:58 -040044#define CREATE_TRACE_POINTS
45#include <trace/events/asoc.h>
46
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000047#define NAME_SIZE 32
48
Frank Mandarinodb2a4162006-10-06 18:31:09 +020049static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
50
Mark Brown384c89e2008-12-03 17:34:03 +000051#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000052struct dentry *snd_soc_debugfs_root;
53EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000054#endif
55
Mark Brownc5af3a22008-11-28 13:29:45 +000056static DEFINE_MUTEX(client_mutex);
57static LIST_HEAD(card_list);
Mark Brown91151712008-11-30 23:31:24 +000058static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000059static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000060static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000061
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000062static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
Liam Girdwood13e13a32011-01-31 21:30:52 +000063int soc_dsp_debugfs_add(struct snd_soc_pcm_runtime *rtd);
Mark Brownc5af3a22008-11-28 13:29:45 +000064
Frank Mandarinodb2a4162006-10-06 18:31:09 +020065/*
66 * This is a timeout to do a DAPM powerdown after a stream is closed().
67 * It can be used to eliminate pops between different playback streams, e.g.
68 * between two audio tracks.
69 */
Patrick Lai95c1e672012-04-13 21:52:20 -070070static int pmdown_time;
Frank Mandarinodb2a4162006-10-06 18:31:09 +020071module_param(pmdown_time, int, 0);
72MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
73
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000074/* returns the minimum number of bytes needed to represent
75 * a particular given value */
76static int min_bytes_needed(unsigned long val)
77{
78 int c = 0;
79 int i;
80
81 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
82 if (val & (1UL << i))
83 break;
84 c = (sizeof val * 8) - c;
85 if (!c || (c % 8))
86 c = (c + 8) / 8;
87 else
88 c /= 8;
89 return c;
90}
91
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000092/* fill buf which is 'len' bytes with a formatted
93 * string of the form 'reg: value\n' */
94static int format_register_str(struct snd_soc_codec *codec,
95 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +000096{
Stephen Warren00b317a2011-04-01 14:50:44 -060097 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
98 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000099 int ret;
100 char tmpbuf[len + 1];
101 char regbuf[regsize + 1];
102
103 /* since tmpbuf is allocated on the stack, warn the callers if they
104 * try to abuse this function */
105 WARN_ON(len > 63);
106
107 /* +2 for ': ' and + 1 for '\n' */
108 if (wordsize + regsize + 2 + 1 != len)
109 return -EINVAL;
110
111 ret = snd_soc_read(codec , reg);
112 if (ret < 0) {
113 memset(regbuf, 'X', regsize);
114 regbuf[regsize] = '\0';
115 } else {
116 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
117 }
118
119 /* prepare the buffer */
120 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
121 /* copy it back to the caller without the '\0' */
122 memcpy(buf, tmpbuf, len);
123
124 return 0;
125}
126
Liam Girdwoodf320fdd2011-02-01 21:31:50 +0000127/* ASoC no host IO hardware.
128 * TODO: fine tune these values for all host less transfers.
129 */
130static const struct snd_pcm_hardware no_host_hardware = {
131 .info = SNDRV_PCM_INFO_MMAP |
132 SNDRV_PCM_INFO_MMAP_VALID |
133 SNDRV_PCM_INFO_INTERLEAVED |
134 SNDRV_PCM_INFO_PAUSE |
135 SNDRV_PCM_INFO_RESUME,
136 .formats = SNDRV_PCM_FMTBIT_S16_LE |
137 SNDRV_PCM_FMTBIT_S32_LE,
138 .period_bytes_min = PAGE_SIZE >> 2,
139 .period_bytes_max = PAGE_SIZE >> 1,
140 .periods_min = 2,
141 .periods_max = 4,
142 .buffer_bytes_max = PAGE_SIZE,
143};
144
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000145/* codec register dump */
146static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
147 size_t count, loff_t pos)
148{
149 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000150 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000151 int len;
152 size_t total = 0;
153 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000154
Stephen Warren00b317a2011-04-01 14:50:44 -0600155 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
156 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000157
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000158 len = wordsize + regsize + 2 + 1;
159
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000160 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000161 return 0;
162
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000163 if (codec->driver->reg_cache_step)
164 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000165
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000166 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +0000167 if (codec->readable_register && !codec->readable_register(codec, i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000168 continue;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000169 if (codec->driver->display_register) {
170 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000171 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100172 } else {
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000173 /* only support larger than PAGE_SIZE bytes debugfs
174 * entries for the default case */
175 if (p >= pos) {
176 if (total + len >= count - 1)
177 break;
178 format_register_str(codec, i, buf + total, len);
179 total += len;
180 }
181 p += len;
Mark Brown5164d742010-07-14 16:14:33 +0100182 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000183 }
184
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000185 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000186
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000187 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000188}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000189
Mark Brown2624d5f2009-11-03 21:56:13 +0000190static ssize_t codec_reg_show(struct device *dev,
191 struct device_attribute *attr, char *buf)
192{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000193 struct snd_soc_pcm_runtime *rtd =
194 container_of(dev, struct snd_soc_pcm_runtime, dev);
195
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000196 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000197}
198
199static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
200
Mark Browndbe21402010-02-12 11:37:24 +0000201static ssize_t pmdown_time_show(struct device *dev,
202 struct device_attribute *attr, char *buf)
203{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000204 struct snd_soc_pcm_runtime *rtd =
205 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Browndbe21402010-02-12 11:37:24 +0000206
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000207 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000208}
209
210static ssize_t pmdown_time_set(struct device *dev,
211 struct device_attribute *attr,
212 const char *buf, size_t count)
213{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000214 struct snd_soc_pcm_runtime *rtd =
215 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Brownc593b522010-10-27 20:11:17 -0700216 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000217
Mark Brownc593b522010-10-27 20:11:17 -0700218 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
219 if (ret)
220 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000221
222 return count;
223}
224
225static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
226
Mark Brown2624d5f2009-11-03 21:56:13 +0000227#ifdef CONFIG_DEBUG_FS
228static int codec_reg_open_file(struct inode *inode, struct file *file)
229{
230 file->private_data = inode->i_private;
231 return 0;
232}
233
234static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000235 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000236{
237 ssize_t ret;
238 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000239 char *buf;
240
241 if (*ppos < 0 || !count)
242 return -EINVAL;
243
244 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000245 if (!buf)
246 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000247
248 ret = soc_codec_reg_show(codec, buf, count, *ppos);
249 if (ret >= 0) {
250 if (copy_to_user(user_buf, buf, ret)) {
251 kfree(buf);
252 return -EFAULT;
253 }
254 *ppos += ret;
255 }
256
Mark Brown2624d5f2009-11-03 21:56:13 +0000257 kfree(buf);
258 return ret;
259}
260
261static ssize_t codec_reg_write_file(struct file *file,
262 const char __user *user_buf, size_t count, loff_t *ppos)
263{
264 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700265 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000266 char *start = buf;
267 unsigned long reg, value;
268 int step = 1;
269 struct snd_soc_codec *codec = file->private_data;
270
271 buf_size = min(count, (sizeof(buf)-1));
272 if (copy_from_user(buf, user_buf, buf_size))
273 return -EFAULT;
274 buf[buf_size] = 0;
275
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000276 if (codec->driver->reg_cache_step)
277 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000278
279 while (*start == ' ')
280 start++;
281 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000282 while (*start == ' ')
283 start++;
284 if (strict_strtoul(start, 16, &value))
285 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000286
287 /* Userspace has been fiddling around behind the kernel's back */
288 add_taint(TAINT_USER);
289
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000290 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000291 return buf_size;
292}
293
294static const struct file_operations codec_reg_fops = {
295 .open = codec_reg_open_file,
296 .read = codec_reg_read_file,
297 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200298 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000299};
300
301static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
302{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200303 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
304
305 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
306 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000307 if (!codec->debugfs_codec_root) {
308 printk(KERN_WARNING
309 "ASoC: Failed to create codec debugfs directory\n");
310 return;
311 }
312
Mark Brownaaee8ef2011-01-26 20:53:50 +0000313 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
314 &codec->cache_sync);
315 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
316 &codec->cache_only);
317
Mark Brown2624d5f2009-11-03 21:56:13 +0000318 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
319 codec->debugfs_codec_root,
320 codec, &codec_reg_fops);
321 if (!codec->debugfs_reg)
322 printk(KERN_WARNING
323 "ASoC: Failed to create codec register debugfs file\n");
324
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200325 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000326}
327
328static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
329{
330 debugfs_remove_recursive(codec->debugfs_codec_root);
331}
332
Mark Brownc3c5a192010-09-15 18:15:14 +0100333static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
334 size_t count, loff_t *ppos)
335{
336 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100337 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100338 struct snd_soc_codec *codec;
339
340 if (!buf)
341 return -ENOMEM;
342
Mark Brown2b194f9d2010-10-13 10:52:16 +0100343 list_for_each_entry(codec, &codec_list, list) {
344 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
345 codec->name);
346 if (len >= 0)
347 ret += len;
348 if (ret > PAGE_SIZE) {
349 ret = PAGE_SIZE;
350 break;
351 }
352 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100353
354 if (ret >= 0)
355 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
356
357 kfree(buf);
358
359 return ret;
360}
361
362static const struct file_operations codec_list_fops = {
363 .read = codec_list_read_file,
364 .llseek = default_llseek,/* read accesses f_pos */
365};
366
Mark Brownf3208782010-09-15 18:19:07 +0100367static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
368 size_t count, loff_t *ppos)
369{
370 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100371 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100372 struct snd_soc_dai *dai;
373
374 if (!buf)
375 return -ENOMEM;
376
Mark Brown2b194f9d2010-10-13 10:52:16 +0100377 list_for_each_entry(dai, &dai_list, list) {
378 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
379 if (len >= 0)
380 ret += len;
381 if (ret > PAGE_SIZE) {
382 ret = PAGE_SIZE;
383 break;
384 }
385 }
Mark Brownf3208782010-09-15 18:19:07 +0100386
Mark Brown2b194f9d2010-10-13 10:52:16 +0100387 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100388
389 kfree(buf);
390
391 return ret;
392}
393
394static const struct file_operations dai_list_fops = {
395 .read = dai_list_read_file,
396 .llseek = default_llseek,/* read accesses f_pos */
397};
398
Mark Brown19c7ac22010-09-15 18:22:40 +0100399static ssize_t platform_list_read_file(struct file *file,
400 char __user *user_buf,
401 size_t count, loff_t *ppos)
402{
403 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100404 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100405 struct snd_soc_platform *platform;
406
407 if (!buf)
408 return -ENOMEM;
409
Mark Brown2b194f9d2010-10-13 10:52:16 +0100410 list_for_each_entry(platform, &platform_list, list) {
411 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
412 platform->name);
413 if (len >= 0)
414 ret += len;
415 if (ret > PAGE_SIZE) {
416 ret = PAGE_SIZE;
417 break;
418 }
419 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100420
Mark Brown2b194f9d2010-10-13 10:52:16 +0100421 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100422
423 kfree(buf);
424
425 return ret;
426}
427
428static const struct file_operations platform_list_fops = {
429 .read = platform_list_read_file,
430 .llseek = default_llseek,/* read accesses f_pos */
431};
432
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200433static void soc_init_card_debugfs(struct snd_soc_card *card)
434{
435 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000436 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200437 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200438 dev_warn(card->dev,
439 "ASoC: Failed to create codec debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200440 return;
441 }
442
443 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
444 card->debugfs_card_root,
445 &card->pop_time);
446 if (!card->debugfs_pop_time)
447 dev_warn(card->dev,
448 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200449}
450
451static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
452{
453 debugfs_remove_recursive(card->debugfs_card_root);
454}
455
Mark Brown2624d5f2009-11-03 21:56:13 +0000456#else
457
458static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
459{
460}
461
462static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
463{
464}
Axel Linb95fccb2010-11-09 17:06:44 +0800465
466static inline void soc_init_card_debugfs(struct snd_soc_card *card)
467{
468}
469
470static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
471{
472}
Mark Brown2624d5f2009-11-03 21:56:13 +0000473#endif
474
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200475#ifdef CONFIG_SND_SOC_AC97_BUS
476/* unregister ac97 codec */
477static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
478{
479 if (codec->ac97->dev.bus)
480 device_unregister(&codec->ac97->dev);
481 return 0;
482}
483
484/* stop no dev release warning */
485static void soc_ac97_device_release(struct device *dev){}
486
487/* register ac97 codec to bus */
488static int soc_ac97_dev_register(struct snd_soc_codec *codec)
489{
490 int err;
491
492 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100493 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200494 codec->ac97->dev.release = soc_ac97_device_release;
495
Kay Sieversbb072bf2008-11-02 03:50:35 +0100496 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000497 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200498 err = device_register(&codec->ac97->dev);
499 if (err < 0) {
500 snd_printk(KERN_ERR "Can't register ac97 bus\n");
501 codec->ac97->dev.bus = NULL;
502 return err;
503 }
504 return 0;
505}
506#endif
507
Mark Brown06f409d2009-04-07 18:10:13 +0100508static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
509{
510 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000511 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
512 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Mark Brown06f409d2009-04-07 18:10:13 +0100513 int ret;
514
Mark Brown4f333b22011-03-08 00:11:15 +0000515 if (!codec_dai->driver->symmetric_rates &&
516 !cpu_dai->driver->symmetric_rates &&
517 !rtd->dai_link->symmetric_rates)
518 return 0;
Mark Brown06f409d2009-04-07 18:10:13 +0100519
Mark Brownc4ef8782011-03-08 00:17:56 +0000520 /* This can happen if multiple streams are starting simultaneously -
521 * the second can need to get its constraints before the first has
522 * picked a rate. Complain and allow the application to carry on.
523 */
524 if (!rtd->rate) {
525 dev_warn(&rtd->dev,
526 "Not enforcing symmetric_rates due to race\n");
527 return 0;
528 }
529
Mark Brown4f333b22011-03-08 00:11:15 +0000530 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n", rtd->rate);
531
532 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
533 SNDRV_PCM_HW_PARAM_RATE,
534 rtd->rate, rtd->rate);
535 if (ret < 0) {
536 dev_err(&rtd->dev,
537 "Unable to apply rate symmetry constraint: %d\n", ret);
538 return ret;
Mark Brown06f409d2009-04-07 18:10:13 +0100539 }
540
541 return 0;
542}
543
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200544/*
545 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
546 * then initialized and any private data can be allocated. This also calls
547 * startup for the cpu DAI, platform, machine and codec DAI.
548 */
Liam Girdwood0f8bae92011-01-27 19:35:01 +0000549int soc_pcm_open(struct snd_pcm_substream *substream)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200550{
551 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200552 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000553 struct snd_soc_platform *platform = rtd->platform;
554 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
555 struct snd_soc_dai *codec_dai = rtd->codec_dai;
556 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
557 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200558 int ret = 0;
559
Liam Girdwood0f8bae92011-01-27 19:35:01 +0000560 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200561
Liam Girdwoodf320fdd2011-02-01 21:31:50 +0000562 if (rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST)
563 snd_soc_set_runtime_hwparams(substream, &no_host_hardware);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200564
565 /* startup the audio subsystem */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000566 if (cpu_dai->driver->ops->startup) {
567 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200568 if (ret < 0) {
569 printk(KERN_ERR "asoc: can't open interface %s\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100570 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200571 goto out;
572 }
573 }
574
Mark Brown8842c722011-04-27 18:58:17 +0100575 if (platform->driver->ops && platform->driver->ops->open) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000576 ret = platform->driver->ops->open(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200577 if (ret < 0) {
578 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
579 goto platform_err;
580 }
581 }
582
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000583 if (codec_dai->driver->ops->startup) {
584 ret = codec_dai->driver->ops->startup(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100585 if (ret < 0) {
586 printk(KERN_ERR "asoc: can't open codec %s\n",
587 codec_dai->name);
588 goto codec_dai_err;
589 }
590 }
591
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000592 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
593 ret = rtd->dai_link->ops->startup(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200594 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000595 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200596 goto machine_err;
597 }
598 }
599
Liam Girdwood13e13a32011-01-31 21:30:52 +0000600 /* DSP DAI links compat checks are different */
601 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
602 goto dynamic;
603
Mark Browna00f90f2010-12-02 16:24:24 +0000604 /* Check that the codec and cpu DAIs are compatible */
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200605 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
606 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000607 max(codec_dai_drv->playback.rate_min,
608 cpu_dai_drv->playback.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200609 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000610 min(codec_dai_drv->playback.rate_max,
611 cpu_dai_drv->playback.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200612 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000613 max(codec_dai_drv->playback.channels_min,
614 cpu_dai_drv->playback.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200615 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000616 min(codec_dai_drv->playback.channels_max,
617 cpu_dai_drv->playback.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100618 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000619 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100620 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
622 if (codec_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900623 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000624 runtime->hw.rates |= cpu_dai_drv->playback.rates;
625 if (cpu_dai_drv->playback.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900626 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000627 runtime->hw.rates |= codec_dai_drv->playback.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200628 } else {
629 runtime->hw.rate_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000630 max(codec_dai_drv->capture.rate_min,
631 cpu_dai_drv->capture.rate_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200632 runtime->hw.rate_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000633 min(codec_dai_drv->capture.rate_max,
634 cpu_dai_drv->capture.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200635 runtime->hw.channels_min =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000636 max(codec_dai_drv->capture.channels_min,
637 cpu_dai_drv->capture.channels_min);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200638 runtime->hw.channels_max =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639 min(codec_dai_drv->capture.channels_max,
640 cpu_dai_drv->capture.channels_max);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100641 runtime->hw.formats =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000642 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100643 runtime->hw.rates =
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000644 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
645 if (codec_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900646 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000647 runtime->hw.rates |= cpu_dai_drv->capture.rates;
648 if (cpu_dai_drv->capture.rates
Jassi Brard9ad6292010-03-12 13:38:52 +0900649 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000650 runtime->hw.rates |= codec_dai_drv->capture.rates;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200651 }
652
Lu Guanqunc51def62011-04-06 23:25:21 +0800653 ret = -EINVAL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200654 snd_pcm_limit_hw_rates(runtime);
655 if (!runtime->hw.rates) {
656 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100657 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900658 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200659 }
660 if (!runtime->hw.formats) {
661 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100662 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900663 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200664 }
Lu Guanqunb04cfcf2011-04-06 23:25:11 +0800665 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
666 runtime->hw.channels_min > runtime->hw.channels_max) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200667 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000668 codec_dai->name, cpu_dai->name);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900669 goto config_err;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200670 }
671
Mark Brown06f409d2009-04-07 18:10:13 +0100672 /* Symmetry only applies if we've already got an active stream. */
673 if (cpu_dai->active || codec_dai->active) {
674 ret = soc_pcm_apply_symmetry(substream);
675 if (ret != 0)
Jassi Brarbb1c0472010-02-25 11:24:53 +0900676 goto config_err;
Mark Brown06f409d2009-04-07 18:10:13 +0100677 }
678
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000679 pr_debug("asoc: %s <-> %s info:\n",
680 codec_dai->name, cpu_dai->name);
Mark Brownf24368c2008-10-21 21:45:08 +0100681 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
682 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
683 runtime->hw.channels_max);
684 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
685 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200686
Liam Girdwood13e13a32011-01-31 21:30:52 +0000687dynamic:
Jassi Brar14dc5732010-02-26 09:12:32 +0900688 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000689 cpu_dai->playback_active++;
690 codec_dai->playback_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900691 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000692 cpu_dai->capture_active++;
693 codec_dai->capture_active++;
Jassi Brar14dc5732010-02-26 09:12:32 +0900694 }
695 cpu_dai->active++;
696 codec_dai->active++;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000697 rtd->codec->active++;
Liam Girdwood0f8bae92011-01-27 19:35:01 +0000698 mutex_unlock(&rtd->pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200699 return 0;
700
Jassi Brarbb1c0472010-02-25 11:24:53 +0900701config_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000702 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
703 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200704
Jassi Brarbb1c0472010-02-25 11:24:53 +0900705machine_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000706 if (codec_dai->driver->ops->shutdown)
707 codec_dai->driver->ops->shutdown(substream, codec_dai);
Jassi Brarbb1c0472010-02-25 11:24:53 +0900708
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100709codec_dai_err:
Mark Brown8842c722011-04-27 18:58:17 +0100710 if (platform->driver->ops && platform->driver->ops->close)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000711 platform->driver->ops->close(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200712
713platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000714 if (cpu_dai->driver->ops->shutdown)
715 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200716out:
Liam Girdwood0f8bae92011-01-27 19:35:01 +0000717 mutex_unlock(&rtd->pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200718 return ret;
719}
720
721/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200722 * Power down the audio subsystem pmdown_time msecs after close is called.
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200723 * This is to ensure there are no pops or clicks in between any music tracks
724 * due to DAPM power cycling.
725 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100726static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200727{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000728 struct snd_soc_pcm_runtime *rtd =
729 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
730 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200731
Liam Girdwood0f8bae92011-01-27 19:35:01 +0000732 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200733
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000734 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
735 codec_dai->driver->playback.stream_name,
736 codec_dai->playback_active ? "active" : "inactive",
737 codec_dai->pop_wait ? "yes" : "no");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200738
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000739 /* are we waiting on this codec DAI stream */
740 if (codec_dai->pop_wait == 1) {
741 codec_dai->pop_wait = 0;
742 snd_soc_dapm_stream_event(rtd,
743 codec_dai->driver->playback.stream_name,
744 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200745 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000746
Liam Girdwood0f8bae92011-01-27 19:35:01 +0000747 mutex_unlock(&rtd->pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200748}
749
750/*
751 * Called by ALSA when a PCM substream is closed. Private data can be
752 * freed here. The cpu DAI, codec DAI, machine and platform are also
753 * shutdown.
754 */
Liam Girdwood0f8bae92011-01-27 19:35:01 +0000755int soc_pcm_close(struct snd_pcm_substream *substream)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200756{
757 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000758 struct snd_soc_platform *platform = rtd->platform;
759 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
760 struct snd_soc_dai *codec_dai = rtd->codec_dai;
761 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200762
Liam Girdwood0f8bae92011-01-27 19:35:01 +0000763 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200764
Jassi Brar14dc5732010-02-26 09:12:32 +0900765 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000766 cpu_dai->playback_active--;
767 codec_dai->playback_active--;
Jassi Brar14dc5732010-02-26 09:12:32 +0900768 } else {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000769 cpu_dai->capture_active--;
770 codec_dai->capture_active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200771 }
Jassi Brar14dc5732010-02-26 09:12:32 +0900772
773 cpu_dai->active--;
774 codec_dai->active--;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200775 codec->active--;
776
Mark Brown6010b2d2008-09-06 18:33:24 +0100777 /* Muting the DAC suppresses artifacts caused during digital
778 * shutdown, for example from stopping clocks.
779 */
780 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
781 snd_soc_dai_digital_mute(codec_dai, 1);
782
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000783 if (cpu_dai->driver->ops->shutdown)
784 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200785
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000786 if (codec_dai->driver->ops->shutdown)
787 codec_dai->driver->ops->shutdown(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200788
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000789 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
790 rtd->dai_link->ops->shutdown(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200791
Mark Brown8842c722011-04-27 18:58:17 +0100792 if (platform->driver->ops && platform->driver->ops->close)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000793 platform->driver->ops->close(substream);
794 cpu_dai->runtime = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200795
796 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
797 /* start delayed pop wq here for playback streams */
Kiran Kandi28ef14e2011-09-16 15:45:47 -0700798 if (rtd->pmdown_time) {
799 codec_dai->pop_wait = 1;
800 schedule_delayed_work(&rtd->delayed_work,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000801 msecs_to_jiffies(rtd->pmdown_time));
Kiran Kandi28ef14e2011-09-16 15:45:47 -0700802 } else {
803 snd_soc_dapm_stream_event(rtd,
804 codec_dai->driver->playback.stream_name,
805 SND_SOC_DAPM_STREAM_STOP);
806 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200807 } else {
808 /* capture streams can be powered down now */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000809 snd_soc_dapm_stream_event(rtd,
810 codec_dai->driver->capture.stream_name,
Liam Girdwood0b4d2212008-01-10 14:36:20 +0100811 SND_SOC_DAPM_STREAM_STOP);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200812 }
813
Liam Girdwood0f8bae92011-01-27 19:35:01 +0000814 mutex_unlock(&rtd->pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200815 return 0;
816}
817
818/*
819 * Called by ALSA when the PCM substream is prepared, can set format, sample
820 * rate, etc. This function is non atomic and can be called multiple times,
821 * it can refer to the runtime info.
822 */
Liam Girdwood0f8bae92011-01-27 19:35:01 +0000823int soc_pcm_prepare(struct snd_pcm_substream *substream)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200824{
825 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000826 struct snd_soc_platform *platform = rtd->platform;
827 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
828 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200829 int ret = 0;
830
Liam Girdwood0f8bae92011-01-27 19:35:01 +0000831 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100832
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000833 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
834 ret = rtd->dai_link->ops->prepare(substream);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100835 if (ret < 0) {
836 printk(KERN_ERR "asoc: machine prepare error\n");
837 goto out;
838 }
839 }
840
Mark Brown8842c722011-04-27 18:58:17 +0100841 if (platform->driver->ops && platform->driver->ops->prepare) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000842 ret = platform->driver->ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200843 if (ret < 0) {
844 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200845 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200846 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200847 }
848
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000849 if (codec_dai->driver->ops->prepare) {
850 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200851 if (ret < 0) {
852 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200853 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200854 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200855 }
856
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000857 if (cpu_dai->driver->ops->prepare) {
858 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100859 if (ret < 0) {
860 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
861 goto out;
862 }
863 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200864
Mark Brownd45f6212008-10-14 13:58:36 +0100865 /* cancel any delayed stream shutdown that is pending */
866 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
867 codec_dai->pop_wait) {
868 codec_dai->pop_wait = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000869 cancel_delayed_work(&rtd->delayed_work);
Mark Brownd45f6212008-10-14 13:58:36 +0100870 }
871
Mark Brown452c5ea2009-05-17 21:41:23 +0100872 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000873 snd_soc_dapm_stream_event(rtd,
874 codec_dai->driver->playback.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100875 SND_SOC_DAPM_STREAM_START);
876 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000877 snd_soc_dapm_stream_event(rtd,
878 codec_dai->driver->capture.stream_name,
Mark Brown452c5ea2009-05-17 21:41:23 +0100879 SND_SOC_DAPM_STREAM_START);
Mark Brownd45f6212008-10-14 13:58:36 +0100880
Mark Brown452c5ea2009-05-17 21:41:23 +0100881 snd_soc_dai_digital_mute(codec_dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200882
883out:
Liam Girdwood0f8bae92011-01-27 19:35:01 +0000884 mutex_unlock(&rtd->pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200885 return ret;
886}
887
888/*
889 * Called by ALSA when the hardware params are set by application. This
890 * function can also be called multiple times and can allocate buffers
891 * (using snd_pcm_lib_* ). It's non-atomic.
892 */
Liam Girdwood0f8bae92011-01-27 19:35:01 +0000893int soc_pcm_hw_params(struct snd_pcm_substream *substream,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200894 struct snd_pcm_hw_params *params)
895{
896 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000897 struct snd_soc_platform *platform = rtd->platform;
898 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
899 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200900 int ret = 0;
901
Liam Girdwood0f8bae92011-01-27 19:35:01 +0000902 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200903
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000904 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
905 ret = rtd->dai_link->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200906 if (ret < 0) {
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100907 printk(KERN_ERR "asoc: machine hw_params failed\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200908 goto out;
909 }
910 }
911
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000912 if (codec_dai->driver->ops->hw_params) {
913 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100914 if (ret < 0) {
915 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
916 codec_dai->name);
917 goto codec_err;
918 }
919 }
920
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000921 if (cpu_dai->driver->ops->hw_params) {
922 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200923 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200924 printk(KERN_ERR "asoc: interface %s hw params failed\n",
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100925 cpu_dai->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200926 goto interface_err;
927 }
928 }
929
Mark Brown8842c722011-04-27 18:58:17 +0100930 if (platform->driver->ops && platform->driver->ops->hw_params) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000931 ret = platform->driver->ops->hw_params(substream, params);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200932 if (ret < 0) {
Mark Brown3ff3f642008-05-19 12:32:25 +0200933 printk(KERN_ERR "asoc: platform %s hw params failed\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200934 platform->name);
935 goto platform_err;
936 }
937 }
938
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000939 rtd->rate = params_rate(params);
Mark Brown06f409d2009-04-07 18:10:13 +0100940
Liam Girdwoodf320fdd2011-02-01 21:31:50 +0000941 /* malloc a page for hostless IO.
942 * FIXME: rework with alsa-lib changes so that this malloc is not required.
943 */
944 if (rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST) {
945 substream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV;
946 substream->dma_buffer.dev.dev = &rtd->dev;
947 substream->dma_buffer.dev.dev->coherent_dma_mask = ISA_DMA_THRESHOLD;
948 substream->dma_buffer.private_data = NULL;
949
950 ret = snd_pcm_lib_malloc_pages(substream, PAGE_SIZE);
951 if (ret < 0)
952 goto platform_err;
953 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200954out:
Liam Girdwood0f8bae92011-01-27 19:35:01 +0000955 mutex_unlock(&rtd->pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200956 return ret;
957
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200958platform_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000959 if (cpu_dai->driver->ops->hw_free)
960 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200961
962interface_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000963 if (codec_dai->driver->ops->hw_free)
964 codec_dai->driver->ops->hw_free(substream, codec_dai);
Liam Girdwoodcb666e52007-02-02 17:13:49 +0100965
966codec_err:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000967 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
968 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200969
Liam Girdwood0f8bae92011-01-27 19:35:01 +0000970 mutex_unlock(&rtd->pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200971 return ret;
972}
973
974/*
Mark Browna00f90f2010-12-02 16:24:24 +0000975 * Frees resources allocated by hw_params, can be called multiple times
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200976 */
Liam Girdwood0f8bae92011-01-27 19:35:01 +0000977int soc_pcm_hw_free(struct snd_pcm_substream *substream)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200978{
979 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000980 struct snd_soc_platform *platform = rtd->platform;
981 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
982 struct snd_soc_dai *codec_dai = rtd->codec_dai;
983 struct snd_soc_codec *codec = rtd->codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200984
Liam Girdwood0f8bae92011-01-27 19:35:01 +0000985 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200986
987 /* apply codec digital mute */
Liam Girdwood8c6529d2008-07-08 13:19:13 +0100988 if (!codec->active)
989 snd_soc_dai_digital_mute(codec_dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200990
991 /* free any machine hw params */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000992 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
993 rtd->dai_link->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200994
995 /* free any DMA resources */
Mark Brown8842c722011-04-27 18:58:17 +0100996 if (platform->driver->ops && platform->driver->ops->hw_free)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000997 platform->driver->ops->hw_free(substream);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200998
Mark Browna00f90f2010-12-02 16:24:24 +0000999 /* now free hw params for the DAIs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001000 if (codec_dai->driver->ops->hw_free)
1001 codec_dai->driver->ops->hw_free(substream, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001002
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001003 if (cpu_dai->driver->ops->hw_free)
1004 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001005
Liam Girdwoodf320fdd2011-02-01 21:31:50 +00001006 if (rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST)
1007 snd_pcm_lib_free_pages(substream);
Liam Girdwood0f8bae92011-01-27 19:35:01 +00001008 mutex_unlock(&rtd->pcm_mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001009 return 0;
1010}
1011
Liam Girdwood0f8bae92011-01-27 19:35:01 +00001012int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001013{
1014 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001015 struct snd_soc_platform *platform = rtd->platform;
1016 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1017 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001018 int ret;
1019
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001020 if (codec_dai->driver->ops->trigger) {
1021 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001022 if (ret < 0)
1023 return ret;
1024 }
1025
Mark Brown8842c722011-04-27 18:58:17 +01001026 if (platform->driver->ops && platform->driver->ops->trigger) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001027 ret = platform->driver->ops->trigger(substream, cmd);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001028 if (ret < 0)
1029 return ret;
1030 }
1031
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001032 if (cpu_dai->driver->ops->trigger) {
1033 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001034 if (ret < 0)
1035 return ret;
1036 }
1037 return 0;
1038}
1039
Liam Girdwood59c96f12011-03-08 15:11:02 +00001040int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream, int cmd)
1041{
1042 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1043 struct snd_soc_platform *platform = rtd->platform;
1044 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1045 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1046 int ret;
1047
1048 if (codec_dai->driver->ops->bespoke_trigger) {
1049 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
1050 if (ret < 0)
1051 return ret;
1052 }
1053
1054 if (platform->driver->bespoke_trigger) {
1055 ret = platform->driver->bespoke_trigger(substream, cmd);
1056 if (ret < 0)
1057 return ret;
1058 }
1059
1060 if (cpu_dai->driver->ops->bespoke_trigger) {
1061 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1062 if (ret < 0)
1063 return ret;
1064 }
1065 return 0;
1066}
1067
Peter Ujfalusi377b6f62010-03-03 15:08:06 +02001068/*
1069 * soc level wrapper for pointer callback
Peter Ujfalusi258020d2010-03-03 15:08:07 +02001070 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1071 * the runtime->delay will be updated accordingly.
Peter Ujfalusi377b6f62010-03-03 15:08:06 +02001072 */
Liam Girdwood0f8bae92011-01-27 19:35:01 +00001073snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
Peter Ujfalusi377b6f62010-03-03 15:08:06 +02001074{
1075 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001076 struct snd_soc_platform *platform = rtd->platform;
1077 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1078 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Peter Ujfalusi258020d2010-03-03 15:08:07 +02001079 struct snd_pcm_runtime *runtime = substream->runtime;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +02001080 snd_pcm_uframes_t offset = 0;
Peter Ujfalusi258020d2010-03-03 15:08:07 +02001081 snd_pcm_sframes_t delay = 0;
Peter Ujfalusi377b6f62010-03-03 15:08:06 +02001082
Mark Brown8842c722011-04-27 18:58:17 +01001083 if (platform->driver->ops && platform->driver->ops->pointer)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001084 offset = platform->driver->ops->pointer(substream);
Peter Ujfalusi377b6f62010-03-03 15:08:06 +02001085
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001086 if (cpu_dai->driver->ops->delay)
1087 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +02001088
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001089 if (codec_dai->driver->ops->delay)
1090 delay += codec_dai->driver->ops->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +02001091
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001092 if (platform->driver->delay)
1093 delay += platform->driver->delay(substream, codec_dai);
Peter Ujfalusi258020d2010-03-03 15:08:07 +02001094
1095 runtime->delay = delay;
1096
Peter Ujfalusi377b6f62010-03-03 15:08:06 +02001097 return offset;
1098}
1099
Liam Girdwood13e13a32011-01-31 21:30:52 +00001100static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1101 unsigned int cmd, void *arg)
1102{
1103 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1104 struct snd_soc_platform *platform = rtd->platform;
1105
1106 if (platform->driver->ops->ioctl)
1107 return platform->driver->ops->ioctl(substream, cmd, arg);
1108 return snd_pcm_lib_ioctl(substream, cmd, arg);
1109}
1110
1111struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
1112 const char *dai_link, int stream)
1113{
1114 int i;
1115
1116 for (i = 0; i < card->num_links; i++) {
1117 if (card->rtd[i].dai_link->no_pcm &&
1118 !strcmp(card->rtd[i].dai_link->name, dai_link))
1119 return card->rtd[i].pcm->streams[stream].substream;
1120 }
1121 dev_dbg(card->dev, "failed to find dai link %s\n", dai_link);
1122 return NULL;
1123}
1124EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
1125
1126struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
1127 const char *dai_link)
1128{
1129 int i;
1130
1131 for (i = 0; i < card->num_links; i++) {
1132 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
1133 return &card->rtd[i];
1134 }
1135 dev_dbg(card->dev, "failed to find rtd %s\n", dai_link);
1136 return NULL;
1137}
1138EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001139
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001140#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001141/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001142int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001143{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001144 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001145 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001146 int i;
1147
Kuirong Wang5ab054f2012-02-29 16:09:09 -08001148 if (!card->instantiated) {
1149 dev_dbg(card->dev, "uninsantiated card found card->name = %s\n",
1150 card->name);
1151 return 0;
1152 }
Daniel Macke3509ff2009-06-03 17:44:49 +02001153 /* If the initialization of this soc device failed, there is no codec
1154 * associated with it. Just bail out in this case.
1155 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001156 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +02001157 return 0;
1158
Andy Green6ed25972008-06-13 16:24:05 +01001159 /* Due to the resume being scheduled into a workqueue we could
1160 * suspend before that's finished - wait for it to complete.
1161 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001162 snd_power_lock(card->snd_card);
1163 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
1164 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +01001165
1166 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001167 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +01001168
Mark Browna00f90f2010-12-02 16:24:24 +00001169 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001170 for (i = 0; i < card->num_rtd; i++) {
1171 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1172 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001173
Liam Girdwood13e13a32011-01-31 21:30:52 +00001174 if (card->rtd[i].dai_link->ignore_suspend ||
1175 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +01001176 continue;
1177
Liam Girdwood13e13a32011-01-31 21:30:52 +00001178 if (card->rtd[i].dai_link->dynamic)
1179 soc_dsp_be_digital_mute(&card->rtd[i], 1);
1180 else {
1181 if (drv->ops->digital_mute && dai->playback_active)
1182 drv->ops->digital_mute(dai, 1);
1183 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001184 }
1185
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001186 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001187 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwood13e13a32011-01-31 21:30:52 +00001188 if (card->rtd[i].dai_link->ignore_suspend ||
1189 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +01001190 continue;
1191
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001192 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +01001193 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +01001194
Mark Brown87506542008-11-18 20:50:34 +00001195 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +00001196 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001197
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001198 for (i = 0; i < card->num_rtd; i++) {
1199 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1200 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001201
Liam Girdwood13e13a32011-01-31 21:30:52 +00001202 if (card->rtd[i].dai_link->ignore_suspend ||
1203 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +01001204 continue;
1205
Liam Girdwood13e13a32011-01-31 21:30:52 +00001206 if (card->rtd[i].dai_link->dynamic) {
1207 soc_dsp_be_cpu_dai_suspend(&card->rtd[i]);
1208 soc_dsp_be_platform_suspend(&card->rtd[i]);
1209 } else {
1210 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1211 cpu_dai->driver->suspend(cpu_dai);
1212 if (platform->driver->suspend && !platform->suspended) {
1213 platform->driver->suspend(cpu_dai);
1214 platform->suspended = 1;
1215 }
Mark Brown1547aba2010-05-07 21:11:40 +01001216 }
1217 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001218
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001219 /* close any waiting streams and save state */
1220 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +01001221 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001222 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001223 }
Mark Brown3efab7d2010-05-09 13:25:43 +01001224
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001225 for (i = 0; i < card->num_rtd; i++) {
1226 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1227
Liam Girdwood13e13a32011-01-31 21:30:52 +00001228 if (card->rtd[i].dai_link->ignore_suspend ||
1229 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +01001230 continue;
1231
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001232 if (driver->playback.stream_name != NULL)
1233 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1234 SND_SOC_DAPM_STREAM_SUSPEND);
1235
1236 if (driver->capture.stream_name != NULL)
1237 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1238 SND_SOC_DAPM_STREAM_SUSPEND);
1239 }
1240
1241 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001242 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001243 /* If there are paths active then the CODEC will be held with
1244 * bias _ON and should not be suspended. */
1245 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001246 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001247 case SND_SOC_BIAS_STANDBY:
1248 case SND_SOC_BIAS_OFF:
1249 codec->driver->suspend(codec, PMSG_SUSPEND);
1250 codec->suspended = 1;
Mark Brownd2f9cb32011-07-18 13:17:13 +09001251 codec->cache_sync = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001252 break;
1253 default:
1254 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1255 break;
1256 }
1257 }
1258 }
1259
1260 for (i = 0; i < card->num_rtd; i++) {
1261 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1262
Liam Girdwood13e13a32011-01-31 21:30:52 +00001263 if (card->rtd[i].dai_link->ignore_suspend ||
1264 card->rtd[i].dai_link->no_pcm)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001265 continue;
1266
Liam Girdwood13e13a32011-01-31 21:30:52 +00001267 if (card->rtd[i].dai_link->dynamic)
1268 soc_dsp_be_ac97_cpu_dai_suspend(&card->rtd[i]);
1269 else
1270 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1271 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001272 }
1273
Mark Brown87506542008-11-18 20:50:34 +00001274 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +00001275 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001276
1277 return 0;
1278}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001279EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001280
Andy Green6ed25972008-06-13 16:24:05 +01001281/* deferred resume work, so resume can complete before we finished
1282 * setting our codec back up, which can be very slow on I2C
1283 */
1284static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001285{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001286 struct snd_soc_card *card =
1287 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001288 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001289 int i;
1290
Andy Green6ed25972008-06-13 16:24:05 +01001291 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1292 * so userspace apps are blocked from touching us
1293 */
1294
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001295 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +01001296
Mark Brown99497882010-05-07 20:24:05 +01001297 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001298 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +01001299
Mark Brown87506542008-11-18 20:50:34 +00001300 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +00001301 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001302
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001303 /* resume AC97 DAIs */
1304 for (i = 0; i < card->num_rtd; i++) {
1305 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +01001306
Liam Girdwood13e13a32011-01-31 21:30:52 +00001307 if (card->rtd[i].dai_link->ignore_suspend ||
1308 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +01001309 continue;
1310
Liam Girdwood13e13a32011-01-31 21:30:52 +00001311 if (card->rtd[i].dai_link->dynamic)
1312 soc_dsp_be_ac97_cpu_dai_resume(&card->rtd[i]);
1313 else
1314 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1315 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001316 }
1317
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001318 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001319 /* If the CODEC was idle over suspend then it will have been
1320 * left with bias OFF or STANDBY and suspended so we must now
1321 * resume. Otherwise the suspend was suppressed.
1322 */
1323 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001324 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001325 case SND_SOC_BIAS_STANDBY:
1326 case SND_SOC_BIAS_OFF:
1327 codec->driver->resume(codec);
1328 codec->suspended = 0;
1329 break;
1330 default:
1331 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1332 break;
1333 }
Mark Brown1547aba2010-05-07 21:11:40 +01001334 }
1335 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001336
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001337 for (i = 0; i < card->num_rtd; i++) {
1338 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001339
Liam Girdwood13e13a32011-01-31 21:30:52 +00001340 if (card->rtd[i].dai_link->ignore_suspend ||
1341 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +01001342 continue;
1343
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001344 if (driver->playback.stream_name != NULL)
1345 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001346 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001347
1348 if (driver->capture.stream_name != NULL)
1349 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001350 SND_SOC_DAPM_STREAM_RESUME);
1351 }
1352
Mark Brown3ff3f642008-05-19 12:32:25 +02001353 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001354 for (i = 0; i < card->num_rtd; i++) {
1355 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1356 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +01001357
Liam Girdwood13e13a32011-01-31 21:30:52 +00001358 if (card->rtd[i].dai_link->ignore_suspend ||
1359 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +01001360 continue;
1361
Liam Girdwood13e13a32011-01-31 21:30:52 +00001362 if (card->rtd[i].dai_link->dynamic)
1363 soc_dsp_be_digital_mute(&card->rtd[i], 0);
1364 else {
1365 if (drv->ops->digital_mute && dai->playback_active)
1366 drv->ops->digital_mute(dai, 0);
1367 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001368 }
1369
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001370 for (i = 0; i < card->num_rtd; i++) {
1371 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1372 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +01001373
Liam Girdwood13e13a32011-01-31 21:30:52 +00001374 if (card->rtd[i].dai_link->ignore_suspend ||
1375 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +01001376 continue;
1377
Liam Girdwood13e13a32011-01-31 21:30:52 +00001378 if (card->rtd[i].dai_link->dynamic) {
1379 soc_dsp_be_cpu_dai_resume(&card->rtd[i]);
1380 soc_dsp_be_platform_resume(&card->rtd[i]);
1381 } else {
1382 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1383 cpu_dai->driver->resume(cpu_dai);
1384 if (platform->driver->resume && platform->suspended) {
1385 platform->driver->resume(cpu_dai);
1386 platform->suspended = 0;
1387 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001388 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001389 }
1390
Mark Brown87506542008-11-18 20:50:34 +00001391 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +00001392 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001393
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001394 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +01001395
1396 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001397 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +01001398}
1399
1400/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001401int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +01001402{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001403 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren390f2022011-06-10 20:15:21 -07001404 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +02001405
Kuirong Wang5ab054f2012-02-29 16:09:09 -08001406 if (!card->instantiated) {
1407 dev_dbg(card->dev, "uninsantiated card found card->name = %s\n",
1408 card->name);
1409 return 0;
1410 }
Mark Brown64ab9ba2009-03-31 11:27:03 +01001411 /* AC97 devices might have other drivers hanging off them so
1412 * need to resume immediately. Other drivers don't have that
1413 * problem and may take a substantial amount of time to resume
1414 * due to I/O costs and anti-pop so handle them out of line.
1415 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001416 for (i = 0; i < card->num_rtd; i++) {
1417 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren390f2022011-06-10 20:15:21 -07001418 ac97_control |= cpu_dai->driver->ac97_control;
1419 }
1420 if (ac97_control) {
1421 dev_dbg(dev, "Resuming AC97 immediately\n");
1422 soc_resume_deferred(&card->deferred_resume_work);
1423 } else {
1424 dev_dbg(dev, "Scheduling resume work\n");
1425 if (!schedule_work(&card->deferred_resume_work))
1426 dev_err(dev, "resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +01001427 }
Andy Green6ed25972008-06-13 16:24:05 +01001428
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001429 return 0;
1430}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001431EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001432#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001433#define snd_soc_suspend NULL
1434#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001435#endif
1436
Liam Girdwood13e13a32011-01-31 21:30:52 +00001437#define NULL_FORMATS \
1438 (SNDRV_PCM_FMTBIT_S16 | SNDRV_PCM_FMTBIT_U16 |\
1439 SNDRV_PCM_FMTBIT_S24 | SNDRV_PCM_FMTBIT_U24 |\
1440 SNDRV_PCM_FMTBIT_S32 | SNDRV_PCM_FMTBIT_U32)
1441
Barry Song02a06d32009-10-16 18:13:38 +08001442static struct snd_soc_dai_ops null_dai_ops = {
1443};
Liam Girdwood13e13a32011-01-31 21:30:52 +00001444static struct snd_soc_dai_driver null_codec_dai_drv = {
1445 .name = "null-codec-dai",
1446 .ops = &null_dai_ops,
1447 .capture = {
1448 .channels_min = 1 ,
1449 .channels_max = 16,
1450 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1451 .formats = NULL_FORMATS,
1452 },
1453 .playback = {
1454 .channels_min = 1 ,
1455 .channels_max = 16,
1456 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1457 .formats = NULL_FORMATS,
1458 },
1459};
1460static struct snd_soc_codec_driver null_codec_drv = {};
Barry Song02a06d32009-10-16 18:13:38 +08001461
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001462static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001463{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001464 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1465 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +00001466 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001467 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001468 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +01001469 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001470
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001471 if (rtd->complete)
1472 return 1;
1473 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +00001474
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001475 /* do we already have the CPU DAI for this link ? */
1476 if (rtd->cpu_dai) {
1477 goto find_codec;
1478 }
1479 /* no, then find CPU DAI from registered DAIs*/
1480 list_for_each_entry(cpu_dai, &dai_list, list) {
1481 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001482 rtd->cpu_dai = cpu_dai;
1483 goto find_codec;
Mark Brown435c5e22008-12-04 15:32:53 +00001484 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001485 }
1486 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1487 dai_link->cpu_dai_name);
1488
1489find_codec:
1490 /* do we already have the CODEC for this link ? */
1491 if (rtd->codec) {
1492 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +00001493 }
1494
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001495 /* no, then find CODEC from registered CODECs*/
1496 list_for_each_entry(codec, &codec_list, list) {
1497 if (!strcmp(codec->name, dai_link->codec_name)) {
1498 rtd->codec = codec;
Mark Brown6b05eda2008-12-08 19:26:48 +00001499
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001500 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1501 list_for_each_entry(codec_dai, &dai_list, list) {
Liam Girdwoodbfe7dc42011-01-31 21:26:35 +00001502 if ((codec->dev == codec_dai->dev || codec->driver == &null_codec_drv) &&
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001503 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1504 rtd->codec_dai = codec_dai;
1505 goto find_platform;
Mark Brown6b05eda2008-12-08 19:26:48 +00001506 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001507 }
1508 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1509 dai_link->codec_dai_name);
1510
1511 goto find_platform;
1512 }
1513 }
1514 dev_dbg(card->dev, "CODEC %s not registered\n",
1515 dai_link->codec_name);
1516
1517find_platform:
Mark Brown848dd8b2011-04-27 18:16:32 +01001518 /* do we need a platform? */
1519 if (rtd->platform)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001520 goto out;
Mark Brown848dd8b2011-04-27 18:16:32 +01001521
1522 /* if there's no platform we match on the empty platform */
1523 platform_name = dai_link->platform_name;
1524 if (!platform_name)
1525 platform_name = "snd-soc-dummy";
1526
1527 /* no, then find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001528 list_for_each_entry(platform, &platform_list, list) {
Mark Brown848dd8b2011-04-27 18:16:32 +01001529 if (!strcmp(platform->name, platform_name)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001530 rtd->platform = platform;
1531 goto out;
1532 }
1533 }
1534
1535 dev_dbg(card->dev, "platform %s not registered\n",
1536 dai_link->platform_name);
1537 return 0;
1538
1539out:
1540 /* mark rtd as complete if we found all 4 of our client devices */
1541 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1542 rtd->complete = 1;
1543 card->num_rtd++;
1544 }
1545 return 1;
1546}
1547
Jarkko Nikula589c3562010-12-06 16:27:07 +02001548static void soc_remove_codec(struct snd_soc_codec *codec)
1549{
1550 int err;
1551
1552 if (codec->driver->remove) {
1553 err = codec->driver->remove(codec);
1554 if (err < 0)
1555 dev_err(codec->dev,
1556 "asoc: failed to remove %s: %d\n",
1557 codec->name, err);
1558 }
1559
1560 /* Make sure all DAPM widgets are freed */
1561 snd_soc_dapm_free(&codec->dapm);
1562
1563 soc_cleanup_codec_debugfs(codec);
1564 codec->probed = 0;
1565 list_del(&codec->card_list);
1566 module_put(codec->dev->driver->owner);
1567}
1568
Liam Girdwood6e30b2e2011-06-03 18:13:02 +01001569static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001570{
1571 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1572 struct snd_soc_codec *codec = rtd->codec;
1573 struct snd_soc_platform *platform = rtd->platform;
1574 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1575 int err;
1576
1577 /* unregister the rtd device */
1578 if (rtd->dev_registered) {
1579 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001580 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001581 device_unregister(&rtd->dev);
1582 rtd->dev_registered = 0;
1583 }
1584
1585 /* remove the CODEC DAI */
Liam Girdwood02184082011-03-30 23:26:53 +01001586 if (codec_dai && codec_dai->probed &&
Liam Girdwood6e30b2e2011-06-03 18:13:02 +01001587 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001588 if (codec_dai->driver->remove) {
1589 err = codec_dai->driver->remove(codec_dai);
1590 if (err < 0)
1591 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1592 }
1593 codec_dai->probed = 0;
1594 list_del(&codec_dai->card_list);
Liam Girdwoodc0b79722011-03-05 18:30:34 +00001595 module_put(codec_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001596 }
1597
1598 /* remove the platform */
Liam Girdwood02184082011-03-30 23:26:53 +01001599 if (platform && platform->probed &&
Liam Girdwood6e30b2e2011-06-03 18:13:02 +01001600 platform->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001601 if (platform->driver->remove) {
1602 err = platform->driver->remove(platform);
1603 if (err < 0)
1604 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1605 }
1606 platform->probed = 0;
1607 list_del(&platform->card_list);
1608 module_put(platform->dev->driver->owner);
1609 }
1610
1611 /* remove the CODEC */
Liam Girdwood02184082011-03-30 23:26:53 +01001612 if (codec && codec->probed &&
Liam Girdwood6e30b2e2011-06-03 18:13:02 +01001613 codec->driver->remove_order == order)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001614 soc_remove_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001615
1616 /* remove the cpu_dai */
Liam Girdwood02184082011-03-30 23:26:53 +01001617 if (cpu_dai && cpu_dai->probed &&
Liam Girdwood6e30b2e2011-06-03 18:13:02 +01001618 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001619 if (cpu_dai->driver->remove) {
1620 err = cpu_dai->driver->remove(cpu_dai);
1621 if (err < 0)
1622 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1623 }
1624 cpu_dai->probed = 0;
1625 list_del(&cpu_dai->card_list);
1626 module_put(cpu_dai->dev->driver->owner);
1627 }
1628}
1629
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001630static void soc_remove_dai_links(struct snd_soc_card *card)
1631{
Liam Girdwood6e30b2e2011-06-03 18:13:02 +01001632 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001633
Liam Girdwood6e30b2e2011-06-03 18:13:02 +01001634 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1635 order++) {
1636 for (dai = 0; dai < card->num_rtd; dai++)
1637 soc_remove_dai_link(card, dai, order);
1638 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001639 card->num_rtd = 0;
1640}
1641
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001642static void soc_set_name_prefix(struct snd_soc_card *card,
1643 struct snd_soc_codec *codec)
1644{
1645 int i;
1646
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001647 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001648 return;
1649
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001650 for (i = 0; i < card->num_configs; i++) {
1651 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001652 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1653 codec->name_prefix = map->name_prefix;
1654 break;
1655 }
1656 }
1657}
1658
Jarkko Nikula589c3562010-12-06 16:27:07 +02001659static int soc_probe_codec(struct snd_soc_card *card,
1660 struct snd_soc_codec *codec)
1661{
1662 int ret = 0;
Mark Brown89b95ac2011-03-07 16:38:44 +00001663 const struct snd_soc_codec_driver *driver = codec->driver;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001664
1665 codec->card = card;
1666 codec->dapm.card = card;
1667 soc_set_name_prefix(card, codec);
1668
Jarkko Nikula70d293312011-01-27 16:24:22 +02001669 if (!try_module_get(codec->dev->driver->owner))
1670 return -ENODEV;
1671
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001672 soc_init_codec_debugfs(codec);
1673
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001674 if (driver->dapm_widgets)
1675 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1676 driver->num_dapm_widgets);
1677
Mark Brown89b95ac2011-03-07 16:38:44 +00001678 if (driver->probe) {
1679 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001680 if (ret < 0) {
1681 dev_err(codec->dev,
1682 "asoc: failed to probe CODEC %s: %d\n",
1683 codec->name, ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001684 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001685 }
1686 }
1687
Mark Brownb7af1da2011-04-07 19:18:44 +09001688 if (driver->controls)
1689 snd_soc_add_controls(codec, driver->controls,
1690 driver->num_controls);
Mark Brown89b95ac2011-03-07 16:38:44 +00001691 if (driver->dapm_routes)
1692 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1693 driver->num_dapm_routes);
1694
Jarkko Nikula589c3562010-12-06 16:27:07 +02001695 /* mark codec as probed and add to card codec list */
1696 codec->probed = 1;
1697 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001698 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001699
Jarkko Nikula70d293312011-01-27 16:24:22 +02001700 return 0;
1701
1702err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001703 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001704 module_put(codec->dev->driver->owner);
1705
Jarkko Nikula589c3562010-12-06 16:27:07 +02001706 return ret;
1707}
1708
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001709static void rtd_release(struct device *dev) {}
1710
Jarkko Nikula589c3562010-12-06 16:27:07 +02001711static int soc_post_component_init(struct snd_soc_card *card,
1712 struct snd_soc_codec *codec,
1713 int num, int dailess)
1714{
1715 struct snd_soc_dai_link *dai_link = NULL;
1716 struct snd_soc_aux_dev *aux_dev = NULL;
1717 struct snd_soc_pcm_runtime *rtd;
1718 const char *temp, *name;
1719 int ret = 0;
1720
1721 if (!dailess) {
1722 dai_link = &card->dai_link[num];
1723 rtd = &card->rtd[num];
1724 name = dai_link->name;
1725 } else {
1726 aux_dev = &card->aux_dev[num];
1727 rtd = &card->rtd_aux[num];
1728 name = aux_dev->name;
1729 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001730 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001731
1732 /* machine controls, routes and widgets are not prefixed */
1733 temp = codec->name_prefix;
1734 codec->name_prefix = NULL;
1735
1736 /* do machine specific initialization */
1737 if (!dailess && dai_link->init)
1738 ret = dai_link->init(rtd);
1739 else if (dailess && aux_dev->init)
1740 ret = aux_dev->init(&codec->dapm);
1741 if (ret < 0) {
1742 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1743 return ret;
1744 }
1745 codec->name_prefix = temp;
1746
1747 /* Make sure all DAPM widgets are instantiated */
1748 snd_soc_dapm_new_widgets(&codec->dapm);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001749
1750 /* register the rtd device */
1751 rtd->codec = codec;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001752 rtd->dev.parent = card->dev;
1753 rtd->dev.release = rtd_release;
1754 rtd->dev.init_name = name;
Liam Girdwood0f8bae92011-01-27 19:35:01 +00001755 mutex_init(&rtd->pcm_mutex);
Liam Girdwood13e13a32011-01-31 21:30:52 +00001756 INIT_LIST_HEAD(&rtd->dsp[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1757 INIT_LIST_HEAD(&rtd->dsp[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1758 INIT_LIST_HEAD(&rtd->dsp[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1759 INIT_LIST_HEAD(&rtd->dsp[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001760 ret = device_register(&rtd->dev);
1761 if (ret < 0) {
1762 dev_err(card->dev,
1763 "asoc: failed to register runtime device: %d\n", ret);
1764 return ret;
1765 }
1766 rtd->dev_registered = 1;
1767
1768 /* add DAPM sysfs entries for this codec */
1769 ret = snd_soc_dapm_sys_add(&rtd->dev);
1770 if (ret < 0)
1771 dev_err(codec->dev,
1772 "asoc: failed to add codec dapm sysfs entries: %d\n",
1773 ret);
1774
1775 /* add codec sysfs entries */
1776 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1777 if (ret < 0)
1778 dev_err(codec->dev,
1779 "asoc: failed to add codec sysfs files: %d\n", ret);
1780
Liam Girdwood13e13a32011-01-31 21:30:52 +00001781#ifdef CONFIG_DEBUG_FS
1782 /* add DSP sysfs entries */
1783 if (!dai_link->dynamic)
1784 goto out;
1785
1786 ret = soc_dsp_debugfs_add(rtd);
1787 if (ret < 0)
1788 dev_err(&rtd->dev, "asoc: failed to add dsp sysfs entries: %d\n", ret);
1789
1790out:
1791#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001792 return 0;
1793}
1794
Liam Girdwood6e30b2e2011-06-03 18:13:02 +01001795static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001796{
1797 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1798 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1799 struct snd_soc_codec *codec = rtd->codec;
1800 struct snd_soc_platform *platform = rtd->platform;
1801 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1802 int ret;
1803
Liam Girdwood6e30b2e2011-06-03 18:13:02 +01001804 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1805 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001806
1807 /* config components */
1808 codec_dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001809 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001810 codec_dai->card = card;
1811 cpu_dai->card = card;
Liam Girdwood7987a112011-01-31 19:52:42 +00001812 codec->dapm.card = platform->dapm.card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001813
1814 /* set default power off timeout */
1815 rtd->pmdown_time = pmdown_time;
1816
1817 /* probe the cpu_dai */
Liam Girdwood02184082011-03-30 23:26:53 +01001818 if (!cpu_dai->probed &&
Liam Girdwood6e30b2e2011-06-03 18:13:02 +01001819 cpu_dai->driver->probe_order == order) {
Liam Girdwood61b61e32011-05-24 13:57:43 +01001820 if (!try_module_get(cpu_dai->dev->driver->owner))
1821 return -ENODEV;
1822
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001823 if (cpu_dai->driver->probe) {
1824 ret = cpu_dai->driver->probe(cpu_dai);
1825 if (ret < 0) {
1826 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1827 cpu_dai->name);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001828 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001829 return ret;
1830 }
1831 }
1832 cpu_dai->probed = 1;
1833 /* mark cpu_dai as probed and add to card cpu_dai list */
1834 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1835 }
1836
1837 /* probe the CODEC */
Liam Girdwood02184082011-03-30 23:26:53 +01001838 if (!codec->probed &&
Liam Girdwood6e30b2e2011-06-03 18:13:02 +01001839 codec->driver->probe_order == order) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001840 ret = soc_probe_codec(card, codec);
1841 if (ret < 0)
1842 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001843 }
1844
1845 /* probe the platform */
Liam Girdwood02184082011-03-30 23:26:53 +01001846 if (!platform->probed &&
Liam Girdwood6e30b2e2011-06-03 18:13:02 +01001847 platform->driver->probe_order == order) {
Jarkko Nikula70d293312011-01-27 16:24:22 +02001848 if (!try_module_get(platform->dev->driver->owner))
1849 return -ENODEV;
1850
Liam Girdwood7987a112011-01-31 19:52:42 +00001851 platform->card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001852 if (platform->driver->probe) {
1853 ret = platform->driver->probe(platform);
1854 if (ret < 0) {
1855 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1856 platform->name);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001857 module_put(platform->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001858 return ret;
1859 }
1860 }
1861 /* mark platform as probed and add to card platform list */
1862 platform->probed = 1;
1863 list_add(&platform->card_list, &card->platform_dev_list);
1864 }
1865
1866 /* probe the CODEC DAI */
Liam Girdwood6e30b2e2011-06-03 18:13:02 +01001867 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodc0b79722011-03-05 18:30:34 +00001868 if (!try_module_get(codec_dai->dev->driver->owner))
1869 return -ENODEV;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001870 if (codec_dai->driver->probe) {
1871 ret = codec_dai->driver->probe(codec_dai);
1872 if (ret < 0) {
1873 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1874 codec_dai->name);
Liam Girdwoodc0b79722011-03-05 18:30:34 +00001875 module_put(codec_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001876 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001877 }
1878 }
1879
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001880 /* mark cpu_dai as probed and add to card cpu_dai list */
1881 codec_dai->probed = 1;
1882 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001883 }
1884
Liam Girdwood6e30b2e2011-06-03 18:13:02 +01001885 /* complete DAI probe during last probe */
1886 if (order != SND_SOC_COMP_ORDER_LAST)
Liam Girdwood02184082011-03-30 23:26:53 +01001887 return 0;
1888
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001889 /* DAPM dai link stream work */
1890 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1891
Jarkko Nikula589c3562010-12-06 16:27:07 +02001892 ret = soc_post_component_init(card, codec, num, 0);
1893 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001894 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001895
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001896 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1897 if (ret < 0)
1898 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1899
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001900 /* create the pcm */
1901 ret = soc_new_pcm(rtd, num);
1902 if (ret < 0) {
1903 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1904 return ret;
1905 }
1906
1907 /* add platform data for AC97 devices */
1908 if (rtd->codec_dai->driver->ac97_control)
1909 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1910
1911 return 0;
1912}
1913
1914#ifdef CONFIG_SND_SOC_AC97_BUS
1915static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1916{
1917 int ret;
1918
1919 /* Only instantiate AC97 if not already done by the adaptor
1920 * for the generic AC97 subsystem.
1921 */
1922 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001923 /*
1924 * It is possible that the AC97 device is already registered to
1925 * the device subsystem. This happens when the device is created
1926 * via snd_ac97_mixer(). Currently only SoC codec that does so
1927 * is the generic AC97 glue but others migh emerge.
1928 *
1929 * In those cases we don't try to register the device again.
1930 */
1931 if (!rtd->codec->ac97_created)
1932 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001933
1934 ret = soc_ac97_dev_register(rtd->codec);
1935 if (ret < 0) {
1936 printk(KERN_ERR "asoc: AC97 device register failed\n");
1937 return ret;
1938 }
1939
1940 rtd->codec->ac97_registered = 1;
1941 }
1942 return 0;
1943}
1944
1945static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1946{
1947 if (codec->ac97_registered) {
1948 soc_ac97_dev_unregister(codec);
1949 codec->ac97_registered = 0;
1950 }
1951}
1952#endif
1953
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001954static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1955{
1956 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001957 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001958 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001959
1960 /* find CODEC from registered CODECs*/
1961 list_for_each_entry(codec, &codec_list, list) {
1962 if (!strcmp(codec->name, aux_dev->codec_name)) {
1963 if (codec->probed) {
1964 dev_err(codec->dev,
1965 "asoc: codec already probed");
1966 ret = -EBUSY;
1967 goto out;
1968 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001969 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001970 }
1971 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001972 /* codec not found */
1973 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1974 goto out;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001975
Jarkko Nikula676ad982010-12-03 09:18:22 +02001976found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001977 ret = soc_probe_codec(card, codec);
1978 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001979 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001980
Jarkko Nikula589c3562010-12-06 16:27:07 +02001981 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001982
1983out:
1984 return ret;
1985}
1986
1987static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1988{
1989 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1990 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001991
1992 /* unregister the rtd device */
1993 if (rtd->dev_registered) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001994 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001995 device_unregister(&rtd->dev);
1996 rtd->dev_registered = 0;
1997 }
1998
Jarkko Nikula589c3562010-12-06 16:27:07 +02001999 if (codec && codec->probed)
2000 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002001}
2002
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00002003static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
2004 enum snd_soc_compress_type compress_type)
2005{
2006 int ret;
2007
2008 if (codec->cache_init)
2009 return 0;
2010
2011 /* override the compress_type if necessary */
2012 if (compress_type && codec->compress_type != compress_type)
2013 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00002014 ret = snd_soc_cache_init(codec);
2015 if (ret < 0) {
2016 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
2017 ret);
2018 return ret;
2019 }
2020 codec->cache_init = 1;
2021 return 0;
2022}
2023
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002024static void snd_soc_instantiate_card(struct snd_soc_card *card)
2025{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00002026 struct snd_soc_codec *codec;
2027 struct snd_soc_codec_conf *codec_conf;
2028 enum snd_soc_compress_type compress_type;
Liam Girdwood6e30b2e2011-06-03 18:13:02 +01002029 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002030
2031 mutex_lock(&card->mutex);
2032
2033 if (card->instantiated) {
2034 mutex_unlock(&card->mutex);
2035 return;
2036 }
2037
2038 /* bind DAIs */
2039 for (i = 0; i < card->num_links; i++)
2040 soc_bind_dai_link(card, i);
2041
2042 /* bind completed ? */
2043 if (card->num_rtd != card->num_links) {
2044 mutex_unlock(&card->mutex);
2045 return;
2046 }
2047
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00002048 /* initialize the register cache for each available codec */
2049 list_for_each_entry(codec, &codec_list, list) {
2050 if (codec->cache_init)
2051 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00002052 /* by default we don't override the compress_type */
2053 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00002054 /* check to see if we need to override the compress_type */
2055 for (i = 0; i < card->num_configs; ++i) {
2056 codec_conf = &card->codec_conf[i];
2057 if (!strcmp(codec->name, codec_conf->dev_name)) {
2058 compress_type = codec_conf->compress_type;
2059 if (compress_type && compress_type
2060 != codec->compress_type)
2061 break;
2062 }
2063 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00002064 ret = snd_soc_init_codec_cache(codec, compress_type);
2065 if (ret < 0) {
2066 mutex_unlock(&card->mutex);
2067 return;
2068 }
2069 }
2070
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002071 /* card bind complete so register a sound card */
2072 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2073 card->owner, 0, &card->snd_card);
2074 if (ret < 0) {
2075 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
2076 card->name);
2077 mutex_unlock(&card->mutex);
2078 return;
2079 }
2080 card->snd_card->dev = card->dev;
2081
Mark Browne37a4972011-03-02 18:21:57 +00002082 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2083 card->dapm.dev = card->dev;
2084 card->dapm.card = card;
2085 list_add(&card->dapm.list, &card->dapm_list);
2086
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02002087#ifdef CONFIG_DEBUG_FS
2088 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2089#endif
2090
Mark Brown88ee1c62011-02-02 10:43:26 +00002091#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01002092 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00002093 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01002094#endif
Andy Green6ed25972008-06-13 16:24:05 +01002095
Mark Brown9a841eb2011-04-12 17:51:37 -07002096 if (card->dapm_widgets)
2097 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2098 card->num_dapm_widgets);
2099
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002100 /* initialise the sound card only once */
2101 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00002102 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002103 if (ret < 0)
2104 goto card_probe_error;
2105 }
2106
Liam Girdwood02184082011-03-30 23:26:53 +01002107 /* early DAI link probe */
Liam Girdwood6e30b2e2011-06-03 18:13:02 +01002108 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2109 order++) {
2110 for (i = 0; i < card->num_links; i++) {
2111 ret = soc_probe_dai_link(card, i, order);
2112 if (ret < 0) {
2113 pr_err("asoc: failed to instantiate card %s: %d\n",
Mark Brown321de0d2010-09-21 15:09:37 +01002114 card->name, ret);
Liam Girdwood6e30b2e2011-06-03 18:13:02 +01002115 goto probe_dai_err;
2116 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002117 }
2118 }
2119
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002120 for (i = 0; i < card->num_aux_devs; i++) {
2121 ret = soc_probe_aux_dev(card, i);
2122 if (ret < 0) {
2123 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
2124 card->name, ret);
2125 goto probe_aux_dev_err;
2126 }
2127 }
2128
Mark Brownb7af1da2011-04-07 19:18:44 +09002129 /* We should have a non-codec control add function but we don't */
2130 if (card->controls)
2131 snd_soc_add_controls(list_first_entry(&card->codec_dev_list,
2132 struct snd_soc_codec,
2133 card_list),
2134 card->controls,
2135 card->num_controls);
2136
Mark Brownb8ad29d2011-03-02 18:35:51 +00002137 if (card->dapm_routes)
2138 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2139 card->num_dapm_routes);
2140
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002141 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002142 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01002143 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2144 "%s", card->long_name ? card->long_name : card->name);
Mark Brown10f672f2011-09-20 11:41:54 +01002145 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2146 "%s", card->driver_name ? card->driver_name : card->name);
2147 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2148 switch (card->snd_card->driver[i]) {
2149 case '_':
2150 case '-':
2151 case '\0':
2152 break;
2153 default:
2154 if (!isalnum(card->snd_card->driver[i]))
2155 card->snd_card->driver[i] = '_';
2156 break;
2157 }
2158 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002159
Mark Brown28e9ad92011-03-02 18:36:34 +00002160 if (card->late_probe) {
2161 ret = card->late_probe(card);
2162 if (ret < 0) {
2163 dev_err(card->dev, "%s late_probe() failed: %d\n",
2164 card->name, ret);
2165 goto probe_aux_dev_err;
2166 }
2167 }
2168
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002169 ret = snd_card_register(card->snd_card);
2170 if (ret < 0) {
2171 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
Axel Lin6b3ed782010-12-07 16:12:29 +08002172 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002173 }
2174
2175#ifdef CONFIG_SND_SOC_AC97_BUS
2176 /* register any AC97 codecs */
2177 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08002178 ret = soc_register_ac97_dai_link(&card->rtd[i]);
2179 if (ret < 0) {
2180 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
2181 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00002182 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08002183 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00002184 }
Axel Lin6b3ed782010-12-07 16:12:29 +08002185 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00002186#endif
2187
Mark Brown435c5e22008-12-04 15:32:53 +00002188 card->instantiated = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002189 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00002190 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002191
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002192probe_aux_dev_err:
2193 for (i = 0; i < card->num_aux_devs; i++)
2194 soc_remove_aux_dev(card, i);
2195
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002196probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002197 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00002198
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002199card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00002200 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002201 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002202
2203 snd_card_free(card->snd_card);
2204
2205 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00002206}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002207
Mark Brown435c5e22008-12-04 15:32:53 +00002208/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02002209 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00002210 * client_mutex.
2211 */
2212static void snd_soc_instantiate_cards(void)
2213{
2214 struct snd_soc_card *card;
2215 list_for_each_entry(card, &card_list, list)
2216 snd_soc_instantiate_card(card);
2217}
2218
2219/* probes a new socdev */
2220static int soc_probe(struct platform_device *pdev)
2221{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002222 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00002223 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00002224
Vinod Koul70a7ca32011-01-14 19:22:48 +05302225 /*
2226 * no card, so machine driver should be registering card
2227 * we should not be here in that case so ret error
2228 */
2229 if (!card)
2230 return -EINVAL;
2231
Mark Brown435c5e22008-12-04 15:32:53 +00002232 /* Bodge while we unpick instantiation */
2233 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002234
Mark Brown435c5e22008-12-04 15:32:53 +00002235 ret = snd_soc_register_card(card);
2236 if (ret != 0) {
2237 dev_err(&pdev->dev, "Failed to register card\n");
2238 return ret;
2239 }
2240
2241 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002242}
2243
Vinod Koulb0e26482011-01-13 22:48:02 +05302244static int soc_cleanup_card_resources(struct snd_soc_card *card)
2245{
Vinod Koulb0e26482011-01-13 22:48:02 +05302246 int i;
2247
2248 /* make sure any delayed work runs */
2249 for (i = 0; i < card->num_rtd; i++) {
2250 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
2251 flush_delayed_work_sync(&rtd->delayed_work);
2252 }
2253
2254 /* remove auxiliary devices */
2255 for (i = 0; i < card->num_aux_devs; i++)
2256 soc_remove_aux_dev(card, i);
2257
2258 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002259 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302260
2261 soc_cleanup_card_debugfs(card);
2262
2263 /* remove the card */
2264 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002265 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302266
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02002267 snd_soc_dapm_free(&card->dapm);
2268
Vinod Koulb0e26482011-01-13 22:48:02 +05302269 kfree(card->rtd);
2270 snd_card_free(card->snd_card);
2271 return 0;
2272
2273}
2274
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002275/* removes a socdev */
2276static int soc_remove(struct platform_device *pdev)
2277{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002278 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002279
Mark Brownc5af3a22008-11-28 13:29:45 +00002280 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002281 return 0;
2282}
2283
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002284int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002285{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002286 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002287 int i;
Mark Brown51737472009-06-22 13:16:51 +01002288
2289 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002290 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002291
2292 /* Flush out pmdown_time work - we actually do want to run it
2293 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002294 for (i = 0; i < card->num_rtd; i++) {
2295 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01002296 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002297 }
Mark Brown51737472009-06-22 13:16:51 +01002298
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002299 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002300
2301 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002302}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002303EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002304
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002305const struct dev_pm_ops snd_soc_pm_ops = {
2306 .suspend = snd_soc_suspend,
2307 .resume = snd_soc_resume,
2308 .poweroff = snd_soc_poweroff,
Mark Brown416356f2009-06-30 19:05:15 +01002309};
Stephen Warrendeb26072011-04-05 19:35:30 -06002310EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002311
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002312/* ASoC platform driver */
2313static struct platform_driver soc_driver = {
2314 .driver = {
2315 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02002316 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002317 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002318 },
2319 .probe = soc_probe,
2320 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002321};
2322
2323/* create a new pcm */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002324static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002325{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002326 struct snd_soc_codec *codec = rtd->codec;
2327 struct snd_soc_platform *platform = rtd->platform;
2328 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2329 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwood13e13a32011-01-31 21:30:52 +00002330 struct snd_pcm_substream *substream[2];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002331 struct snd_pcm *pcm;
2332 char new_name[64];
2333 int ret = 0, playback = 0, capture = 0;
2334
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002335 /* check client and interface hw capabilities */
Mark Brown40ca1142009-12-24 13:44:28 +00002336 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002337 rtd->dai_link->stream_name, codec_dai->name, num);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002338
Liam Girdwood13e13a32011-01-31 21:30:52 +00002339 if (rtd->dai_link->dynamic) {
Liam Girdwooda42d64d2011-06-03 14:26:07 +01002340 playback = rtd->dai_link->dsp_link->playback;
2341 capture = rtd->dai_link->dsp_link->capture;
Liam Girdwood13e13a32011-01-31 21:30:52 +00002342 } else {
2343 if (codec_dai->driver->playback.channels_min)
2344 playback = 1;
2345 if (codec_dai->driver->capture.channels_min)
2346 capture = 1;
2347 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002348
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002349 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
2350 ret = snd_pcm_new(rtd->card->snd_card, new_name,
2351 num, playback, capture, &pcm);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002352 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002353 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002354 return ret;
2355 }
2356
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002357 rtd->pcm = pcm;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002358 pcm->private_data = rtd;
Liam Girdwood13e13a32011-01-31 21:30:52 +00002359
2360 substream[SNDRV_PCM_STREAM_PLAYBACK] =
2361 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
2362 substream[SNDRV_PCM_STREAM_CAPTURE] =
2363 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
2364
2365 if (rtd->dai_link->no_pcm) {
2366 if (playback)
2367 substream[SNDRV_PCM_STREAM_PLAYBACK]->private_data = rtd;
2368 if (capture)
2369 substream[SNDRV_PCM_STREAM_CAPTURE]->private_data = rtd;
2370 goto out;
2371 }
2372
Liam Girdwoodf320fdd2011-02-01 21:31:50 +00002373 /* setup any hostless PCMs - i.e. no host IO is performed */
2374 if (rtd->dai_link->no_host_mode) {
Patrick Lai3f78c4b2011-05-25 17:07:53 -07002375 if (substream[SNDRV_PCM_STREAM_PLAYBACK]) {
2376 substream[SNDRV_PCM_STREAM_PLAYBACK]->hw_no_buffer = 1;
2377 snd_soc_set_runtime_hwparams(
2378 substream[SNDRV_PCM_STREAM_PLAYBACK],
Liam Girdwoodf320fdd2011-02-01 21:31:50 +00002379 &no_host_hardware);
Patrick Lai3f78c4b2011-05-25 17:07:53 -07002380 }
2381 if (substream[SNDRV_PCM_STREAM_CAPTURE]) {
2382 substream[SNDRV_PCM_STREAM_CAPTURE]->hw_no_buffer = 1;
2383 snd_soc_set_runtime_hwparams(
2384 substream[SNDRV_PCM_STREAM_CAPTURE],
Liam Girdwoodf320fdd2011-02-01 21:31:50 +00002385 &no_host_hardware);
Patrick Lai3f78c4b2011-05-25 17:07:53 -07002386 }
Liam Girdwoodf320fdd2011-02-01 21:31:50 +00002387 }
2388
Liam Girdwood13e13a32011-01-31 21:30:52 +00002389 /* ASoC PCM operations */
2390 if (rtd->dai_link->dynamic) {
2391 rtd->ops.open = soc_dsp_fe_dai_open;
2392 rtd->ops.hw_params = soc_dsp_fe_dai_hw_params;
2393 rtd->ops.prepare = soc_dsp_fe_dai_prepare;
2394 rtd->ops.trigger = soc_dsp_fe_dai_trigger;
2395 rtd->ops.hw_free = soc_dsp_fe_dai_hw_free;
2396 rtd->ops.close = soc_dsp_fe_dai_close;
2397 rtd->ops.pointer = soc_pcm_pointer;
2398 rtd->ops.ioctl = soc_pcm_ioctl;
2399 } else {
2400 rtd->ops.open = soc_pcm_open;
2401 rtd->ops.hw_params = soc_pcm_hw_params;
2402 rtd->ops.prepare = soc_pcm_prepare;
2403 rtd->ops.trigger = soc_pcm_trigger;
2404 rtd->ops.hw_free = soc_pcm_hw_free;
2405 rtd->ops.close = soc_pcm_close;
2406 rtd->ops.pointer = soc_pcm_pointer;
2407 rtd->ops.ioctl = soc_pcm_ioctl;
2408 }
2409
Mark Brown8842c722011-04-27 18:58:17 +01002410 if (platform->driver->ops) {
Liam Girdwood13e13a32011-01-31 21:30:52 +00002411 rtd->ops.ack = platform->driver->ops->ack;
2412 rtd->ops.copy = platform->driver->ops->copy;
2413 rtd->ops.silence = platform->driver->ops->silence;
2414 rtd->ops.page = platform->driver->ops->page;
2415 rtd->ops.mmap = platform->driver->ops->mmap;
Mark Brown8842c722011-04-27 18:58:17 +01002416 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002417
2418 if (playback)
Liam Girdwood13e13a32011-01-31 21:30:52 +00002419 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002420
2421 if (capture)
Liam Girdwood13e13a32011-01-31 21:30:52 +00002422 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002423
Mark Brown8842c722011-04-27 18:58:17 +01002424 if (platform->driver->pcm_new) {
Liam Girdwoodc6ac9842011-02-04 22:12:30 +00002425 ret = platform->driver->pcm_new(rtd);
Mark Brown8842c722011-04-27 18:58:17 +01002426 if (ret < 0) {
2427 pr_err("asoc: platform pcm constructor failed\n");
2428 return ret;
2429 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002430 }
2431
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002432 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood13e13a32011-01-31 21:30:52 +00002433out:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002434 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
2435 cpu_dai->name);
2436 return ret;
2437}
2438
Mark Brown096e49d2009-07-05 15:12:22 +01002439/**
2440 * snd_soc_codec_volatile_register: Report if a register is volatile.
2441 *
2442 * @codec: CODEC to query.
2443 * @reg: Register to query.
2444 *
2445 * Boolean function indiciating if a CODEC register is volatile.
2446 */
Mark Brown181e0552011-01-24 14:05:25 +00002447int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
2448 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01002449{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00002450 if (codec->volatile_register)
2451 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01002452 else
2453 return 0;
2454}
2455EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
2456
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002457/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002458 * snd_soc_codec_readable_register: Report if a register is readable.
2459 *
2460 * @codec: CODEC to query.
2461 * @reg: Register to query.
2462 *
2463 * Boolean function indicating if a CODEC register is readable.
2464 */
2465int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
2466 unsigned int reg)
2467{
2468 if (codec->readable_register)
2469 return codec->readable_register(codec, reg);
2470 else
2471 return 0;
2472}
2473EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
2474
2475/**
2476 * snd_soc_codec_writable_register: Report if a register is writable.
2477 *
2478 * @codec: CODEC to query.
2479 * @reg: Register to query.
2480 *
2481 * Boolean function indicating if a CODEC register is writable.
2482 */
2483int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
2484 unsigned int reg)
2485{
2486 if (codec->writable_register)
2487 return codec->writable_register(codec, reg);
2488 else
2489 return 0;
2490}
2491EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2492
Liam Girdwood7987a112011-01-31 19:52:42 +00002493unsigned int snd_soc_platform_read(struct snd_soc_platform *platform,
2494 unsigned int reg)
2495{
2496 unsigned int ret;
2497
2498 ret = platform->driver->read(platform, reg);
2499 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
2500 trace_snd_soc_preg_read(platform, reg, ret);
2501
2502 return ret;
2503}
2504EXPORT_SYMBOL_GPL(snd_soc_platform_read);
2505
2506unsigned int snd_soc_platform_write(struct snd_soc_platform *platform,
2507 unsigned int reg, unsigned int val)
2508{
2509 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
2510 trace_snd_soc_preg_write(platform, reg, val);
2511 return platform->driver->write(platform, reg, val);
2512}
2513EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2514
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002515/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002516 * snd_soc_new_ac97_codec - initailise AC97 device
2517 * @codec: audio codec
2518 * @ops: AC97 bus operations
2519 * @num: AC97 codec number
2520 *
2521 * Initialises AC97 codec resources for use by ad-hoc devices only.
2522 */
2523int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2524 struct snd_ac97_bus_ops *ops, int num)
2525{
2526 mutex_lock(&codec->mutex);
2527
2528 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2529 if (codec->ac97 == NULL) {
2530 mutex_unlock(&codec->mutex);
2531 return -ENOMEM;
2532 }
2533
2534 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2535 if (codec->ac97->bus == NULL) {
2536 kfree(codec->ac97);
2537 codec->ac97 = NULL;
2538 mutex_unlock(&codec->mutex);
2539 return -ENOMEM;
2540 }
2541
2542 codec->ac97->bus->ops = ops;
2543 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002544
2545 /*
2546 * Mark the AC97 device to be created by us. This way we ensure that the
2547 * device will be registered with the device subsystem later on.
2548 */
2549 codec->ac97_created = 1;
2550
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002551 mutex_unlock(&codec->mutex);
2552 return 0;
2553}
2554EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2555
2556/**
2557 * snd_soc_free_ac97_codec - free AC97 codec device
2558 * @codec: audio codec
2559 *
2560 * Frees AC97 codec device resources.
2561 */
2562void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2563{
2564 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002565#ifdef CONFIG_SND_SOC_AC97_BUS
2566 soc_unregister_ac97_dai_link(codec);
2567#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002568 kfree(codec->ac97->bus);
2569 kfree(codec->ac97);
2570 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002571 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002572 mutex_unlock(&codec->mutex);
2573}
2574EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2575
Mark Brownc3753702010-11-01 15:41:57 -04002576unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2577{
2578 unsigned int ret;
2579
Mark Brownc3acec22010-12-02 16:15:29 +00002580 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002581 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002582 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002583
2584 return ret;
2585}
2586EXPORT_SYMBOL_GPL(snd_soc_read);
2587
2588unsigned int snd_soc_write(struct snd_soc_codec *codec,
2589 unsigned int reg, unsigned int val)
2590{
2591 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002592 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002593 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002594}
2595EXPORT_SYMBOL_GPL(snd_soc_write);
2596
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00002597unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2598 unsigned int reg, const void *data, size_t len)
2599{
2600 return codec->bulk_write_raw(codec, reg, data, len);
2601}
2602EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2603
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002604/**
2605 * snd_soc_update_bits - update codec register bits
2606 * @codec: audio codec
2607 * @reg: codec register
2608 * @mask: register mask
2609 * @value: new value
2610 *
2611 * Writes new register value.
2612 *
Timur Tabi180c3292011-01-10 15:58:13 -06002613 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002614 */
2615int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002616 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002617{
2618 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002619 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002620 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002621
Timur Tabi180c3292011-01-10 15:58:13 -06002622 ret = snd_soc_read(codec, reg);
2623 if (ret < 0)
2624 return ret;
2625
2626 old = ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002627 new = (old & ~mask) | value;
2628 change = old != new;
Timur Tabi180c3292011-01-10 15:58:13 -06002629 if (change) {
2630 ret = snd_soc_write(codec, reg, new);
2631 if (ret < 0)
2632 return ret;
2633 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002634
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002635 return change;
2636}
2637EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2638
2639/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002640 * snd_soc_update_bits_locked - update codec register bits
2641 * @codec: audio codec
2642 * @reg: codec register
2643 * @mask: register mask
2644 * @value: new value
2645 *
2646 * Writes new register value, and takes the codec mutex.
2647 *
2648 * Returns 1 for change else 0.
2649 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002650int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2651 unsigned short reg, unsigned int mask,
2652 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002653{
2654 int change;
2655
2656 mutex_lock(&codec->mutex);
2657 change = snd_soc_update_bits(codec, reg, mask, value);
2658 mutex_unlock(&codec->mutex);
2659
2660 return change;
2661}
Mark Browndd1b3d52009-12-04 14:22:03 +00002662EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002663
2664/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002665 * snd_soc_test_bits - test register for change
2666 * @codec: audio codec
2667 * @reg: codec register
2668 * @mask: register mask
2669 * @value: new value
2670 *
2671 * Tests a register with a new value and checks if the new value is
2672 * different from the old value.
2673 *
2674 * Returns 1 for change else 0.
2675 */
2676int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002677 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002678{
2679 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002680 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002681
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002682 old = snd_soc_read(codec, reg);
2683 new = (old & ~mask) | value;
2684 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002685
2686 return change;
2687}
2688EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2689
2690/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002691 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2692 * @substream: the pcm substream
2693 * @hw: the hardware parameters
2694 *
2695 * Sets the substream runtime hardware parameters.
2696 */
2697int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2698 const struct snd_pcm_hardware *hw)
2699{
2700 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwood13e13a32011-01-31 21:30:52 +00002701 if (!runtime)
2702 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002703 runtime->hw.info = hw->info;
2704 runtime->hw.formats = hw->formats;
2705 runtime->hw.period_bytes_min = hw->period_bytes_min;
2706 runtime->hw.period_bytes_max = hw->period_bytes_max;
2707 runtime->hw.periods_min = hw->periods_min;
2708 runtime->hw.periods_max = hw->periods_max;
2709 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2710 runtime->hw.fifo_size = hw->fifo_size;
2711 return 0;
2712}
2713EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2714
2715/**
2716 * snd_soc_cnew - create new control
2717 * @_template: control template
2718 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002719 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002720 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002721 *
2722 * Create a new mixer control from a template control.
2723 *
2724 * Returns 0 for success, else error.
2725 */
2726struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brownefb7ac32011-03-08 17:23:24 +00002727 void *data, char *long_name,
2728 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002729{
2730 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002731 struct snd_kcontrol *kcontrol;
2732 char *name = NULL;
2733 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002734
2735 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002736 template.index = 0;
2737
Mark Brownefb7ac32011-03-08 17:23:24 +00002738 if (!long_name)
2739 long_name = template.name;
2740
2741 if (prefix) {
2742 name_len = strlen(long_name) + strlen(prefix) + 2;
2743 name = kmalloc(name_len, GFP_ATOMIC);
2744 if (!name)
2745 return NULL;
2746
2747 snprintf(name, name_len, "%s %s", prefix, long_name);
2748
2749 template.name = name;
2750 } else {
2751 template.name = long_name;
2752 }
2753
2754 kcontrol = snd_ctl_new1(&template, data);
2755
2756 kfree(name);
2757
2758 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002759}
2760EXPORT_SYMBOL_GPL(snd_soc_cnew);
2761
2762/**
Ian Molton3e8e1952009-01-09 00:23:21 +00002763 * snd_soc_add_controls - add an array of controls to a codec.
2764 * Convienience function to add a list of controls. Many codecs were
2765 * duplicating this code.
2766 *
2767 * @codec: codec to add controls to
2768 * @controls: array of controls to add
2769 * @num_controls: number of elements in the array
2770 *
2771 * Return 0 for success, else error.
2772 */
2773int snd_soc_add_controls(struct snd_soc_codec *codec,
2774 const struct snd_kcontrol_new *controls, int num_controls)
2775{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002776 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002777 int err, i;
2778
2779 for (i = 0; i < num_controls; i++) {
2780 const struct snd_kcontrol_new *control = &controls[i];
Mark Brownefb7ac32011-03-08 17:23:24 +00002781 err = snd_ctl_add(card, snd_soc_cnew(control, codec,
2782 control->name,
2783 codec->name_prefix));
Ian Molton3e8e1952009-01-09 00:23:21 +00002784 if (err < 0) {
Mark Brown082100d2010-09-20 19:03:28 +01002785 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
Mark Brownefb7ac32011-03-08 17:23:24 +00002786 codec->name, control->name, err);
Ian Molton3e8e1952009-01-09 00:23:21 +00002787 return err;
2788 }
2789 }
2790
2791 return 0;
2792}
2793EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2794
2795/**
Liam Girdwood7987a112011-01-31 19:52:42 +00002796 * snd_soc_add_platform_controls - add an array of controls to a platform.
2797 * Convienience function to add a list of controls.
2798 *
2799 * @platform: platform to add controls to
2800 * @controls: array of controls to add
2801 * @num_controls: number of elements in the array
2802 *
2803 * Return 0 for success, else error.
2804 */
2805int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2806 const struct snd_kcontrol_new *controls, int num_controls)
2807{
2808 struct snd_card *card = platform->card->snd_card;
2809 int err, i;
2810
2811 for (i = 0; i < num_controls; i++) {
2812 const struct snd_kcontrol_new *control = &controls[i];
2813 err = snd_ctl_add(card, snd_soc_cnew(control, platform,
2814 control->name, NULL));
2815 if (err < 0) {
2816 dev_err(platform->dev, "Failed to add %s %d\n",control->name, err);
2817 return err;
2818 }
2819 }
2820
2821 return 0;
2822}
2823EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2824
2825/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002826 * snd_soc_info_enum_double - enumerated double mixer info callback
2827 * @kcontrol: mixer control
2828 * @uinfo: control element information
2829 *
2830 * Callback to provide information about a double enumerated
2831 * mixer control.
2832 *
2833 * Returns 0 for success.
2834 */
2835int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2836 struct snd_ctl_elem_info *uinfo)
2837{
2838 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2839
2840 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2841 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002842 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002843
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002844 if (uinfo->value.enumerated.item > e->max - 1)
2845 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002846 strcpy(uinfo->value.enumerated.name,
Liam Girdwooda8f13d82011-05-18 18:39:21 +01002847 snd_soc_get_enum_text(e, uinfo->value.enumerated.item));
2848
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002849 return 0;
2850}
2851EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2852
2853/**
2854 * snd_soc_get_enum_double - enumerated double mixer get callback
2855 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002856 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002857 *
2858 * Callback to get the value of a double enumerated mixer.
2859 *
2860 * Returns 0 for success.
2861 */
2862int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2863 struct snd_ctl_elem_value *ucontrol)
2864{
2865 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2866 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002867 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002868
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002869 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002870 ;
2871 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002872 ucontrol->value.enumerated.item[0]
2873 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002874 if (e->shift_l != e->shift_r)
2875 ucontrol->value.enumerated.item[1] =
2876 (val >> e->shift_r) & (bitmask - 1);
2877
2878 return 0;
2879}
2880EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2881
2882/**
2883 * snd_soc_put_enum_double - enumerated double mixer put callback
2884 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002885 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002886 *
2887 * Callback to set the value of a double enumerated mixer.
2888 *
2889 * Returns 0 for success.
2890 */
2891int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2892 struct snd_ctl_elem_value *ucontrol)
2893{
2894 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2895 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002896 unsigned int val;
2897 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002898
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002899 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002900 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002901 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002902 return -EINVAL;
2903 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2904 mask = (bitmask - 1) << e->shift_l;
2905 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002906 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002907 return -EINVAL;
2908 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2909 mask |= (bitmask - 1) << e->shift_r;
2910 }
2911
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002912 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002913}
2914EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2915
2916/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002917 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2918 * @kcontrol: mixer control
2919 * @ucontrol: control element information
2920 *
2921 * Callback to get the value of a double semi enumerated mixer.
2922 *
2923 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2924 * used for handling bitfield coded enumeration for example.
2925 *
2926 * Returns 0 for success.
2927 */
2928int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2929 struct snd_ctl_elem_value *ucontrol)
2930{
2931 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002932 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002933 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002934
2935 reg_val = snd_soc_read(codec, e->reg);
2936 val = (reg_val >> e->shift_l) & e->mask;
2937 for (mux = 0; mux < e->max; mux++) {
2938 if (val == e->values[mux])
2939 break;
2940 }
2941 ucontrol->value.enumerated.item[0] = mux;
2942 if (e->shift_l != e->shift_r) {
2943 val = (reg_val >> e->shift_r) & e->mask;
2944 for (mux = 0; mux < e->max; mux++) {
2945 if (val == e->values[mux])
2946 break;
2947 }
2948 ucontrol->value.enumerated.item[1] = mux;
2949 }
2950
2951 return 0;
2952}
2953EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2954
2955/**
2956 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2957 * @kcontrol: mixer control
2958 * @ucontrol: control element information
2959 *
2960 * Callback to set the value of a double semi enumerated mixer.
2961 *
2962 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2963 * used for handling bitfield coded enumeration for example.
2964 *
2965 * Returns 0 for success.
2966 */
2967int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2968 struct snd_ctl_elem_value *ucontrol)
2969{
2970 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002971 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002972 unsigned int val;
2973 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002974
2975 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2976 return -EINVAL;
2977 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2978 mask = e->mask << e->shift_l;
2979 if (e->shift_l != e->shift_r) {
2980 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2981 return -EINVAL;
2982 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2983 mask |= e->mask << e->shift_r;
2984 }
2985
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002986 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002987}
2988EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2989
2990/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002991 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2992 * @kcontrol: mixer control
2993 * @uinfo: control element information
2994 *
2995 * Callback to provide information about an external enumerated
2996 * single mixer.
2997 *
2998 * Returns 0 for success.
2999 */
3000int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
3001 struct snd_ctl_elem_info *uinfo)
3002{
3003 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
3004
3005 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3006 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01003007 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003008
Jon Smirlf8ba0b72008-07-29 11:42:27 +01003009 if (uinfo->value.enumerated.item > e->max - 1)
3010 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003011 strcpy(uinfo->value.enumerated.name,
Liam Girdwooda8f13d82011-05-18 18:39:21 +01003012 snd_soc_get_enum_text(e, uinfo->value.enumerated.item));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003013 return 0;
3014}
3015EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
3016
3017/**
3018 * snd_soc_info_volsw_ext - external single mixer info callback
3019 * @kcontrol: mixer control
3020 * @uinfo: control element information
3021 *
3022 * Callback to provide information about a single external mixer control.
3023 *
3024 * Returns 0 for success.
3025 */
3026int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
3027 struct snd_ctl_elem_info *uinfo)
3028{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003029 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003030
Mark Brownfd5dfad2009-04-15 21:37:46 +01003031 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003032 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3033 else
3034 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3035
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003036 uinfo->count = 1;
3037 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003038 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003039 return 0;
3040}
3041EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
3042
3043/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003044 * snd_soc_info_volsw - single mixer info callback
3045 * @kcontrol: mixer control
3046 * @uinfo: control element information
3047 *
3048 * Callback to provide information about a single mixer control.
3049 *
3050 * Returns 0 for success.
3051 */
3052int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
3053 struct snd_ctl_elem_info *uinfo)
3054{
Jon Smirl4eaa9812008-07-29 11:42:26 +01003055 struct soc_mixer_control *mc =
3056 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003057 int platform_max;
Mark Brown762b8df2008-10-30 12:37:08 +00003058 unsigned int shift = mc->shift;
Jon Smirl815ecf82008-07-29 10:22:24 -04003059 unsigned int rshift = mc->rshift;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003060
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003061 if (!mc->platform_max)
3062 mc->platform_max = mc->max;
3063 platform_max = mc->platform_max;
3064
3065 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003066 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3067 else
3068 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3069
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003070 uinfo->count = shift == rshift ? 1 : 2;
3071 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003072 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003073 return 0;
3074}
3075EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
3076
3077/**
Ben Romberger8f1dc4c2011-09-22 13:51:35 -07003078 * snd_soc_info_multi_ext - external single mixer info callback
3079 * @kcontrol: mixer control
3080 * @uinfo: control element information
3081 *
3082 * Callback to provide information about a single external mixer control.
3083 * that accepts multiple input.
3084 *
3085 * Returns 0 for success.
3086 */
3087int snd_soc_info_multi_ext(struct snd_kcontrol *kcontrol,
3088 struct snd_ctl_elem_info *uinfo)
3089{
3090 struct soc_multi_mixer_control *mc =
3091 (struct soc_multi_mixer_control *)kcontrol->private_value;
3092 int platform_max;
3093
3094 if (!mc->platform_max)
3095 mc->platform_max = mc->max;
3096 platform_max = mc->platform_max;
3097
3098 if (platform_max == 1 && !strnstr(kcontrol->id.name, " Volume", 30))
3099 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3100 else
3101 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3102
3103 uinfo->count = mc->count;
3104 uinfo->value.integer.min = 0;
3105 uinfo->value.integer.max = platform_max;
3106 return 0;
3107}
3108EXPORT_SYMBOL_GPL(snd_soc_info_multi_ext);
3109
3110/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003111 * snd_soc_get_volsw - single mixer get callback
3112 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003113 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003114 *
3115 * Callback to get the value of a single mixer control.
3116 *
3117 * Returns 0 for success.
3118 */
3119int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
3120 struct snd_ctl_elem_value *ucontrol)
3121{
Jon Smirl4eaa9812008-07-29 11:42:26 +01003122 struct soc_mixer_control *mc =
3123 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003124 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04003125 unsigned int reg = mc->reg;
3126 unsigned int shift = mc->shift;
3127 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003128 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04003129 unsigned int mask = (1 << fls(max)) - 1;
3130 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003131
3132 ucontrol->value.integer.value[0] =
3133 (snd_soc_read(codec, reg) >> shift) & mask;
3134 if (shift != rshift)
3135 ucontrol->value.integer.value[1] =
3136 (snd_soc_read(codec, reg) >> rshift) & mask;
3137 if (invert) {
3138 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003139 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003140 if (shift != rshift)
3141 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003142 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003143 }
3144
3145 return 0;
3146}
3147EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
3148
3149/**
3150 * snd_soc_put_volsw - single mixer put callback
3151 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003152 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003153 *
3154 * Callback to set the value of a single mixer control.
3155 *
3156 * Returns 0 for success.
3157 */
3158int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
3159 struct snd_ctl_elem_value *ucontrol)
3160{
Jon Smirl4eaa9812008-07-29 11:42:26 +01003161 struct soc_mixer_control *mc =
3162 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003163 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04003164 unsigned int reg = mc->reg;
3165 unsigned int shift = mc->shift;
3166 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003167 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04003168 unsigned int mask = (1 << fls(max)) - 1;
3169 unsigned int invert = mc->invert;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003170 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003171
3172 val = (ucontrol->value.integer.value[0] & mask);
3173 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003174 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003175 val_mask = mask << shift;
3176 val = val << shift;
3177 if (shift != rshift) {
3178 val2 = (ucontrol->value.integer.value[1] & mask);
3179 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003180 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003181 val_mask |= mask << rshift;
3182 val |= val2 << rshift;
3183 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02003184 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003185}
3186EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
3187
3188/**
3189 * snd_soc_info_volsw_2r - double mixer info callback
3190 * @kcontrol: mixer control
3191 * @uinfo: control element information
3192 *
3193 * Callback to provide information about a double mixer control that
3194 * spans 2 codec registers.
3195 *
3196 * Returns 0 for success.
3197 */
3198int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
3199 struct snd_ctl_elem_info *uinfo)
3200{
Jon Smirl4eaa9812008-07-29 11:42:26 +01003201 struct soc_mixer_control *mc =
3202 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003203 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003204
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003205 if (!mc->platform_max)
3206 mc->platform_max = mc->max;
3207 platform_max = mc->platform_max;
3208
3209 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003210 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3211 else
3212 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3213
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003214 uinfo->count = 2;
3215 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003216 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003217 return 0;
3218}
3219EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
3220
3221/**
3222 * snd_soc_get_volsw_2r - double mixer get callback
3223 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003224 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003225 *
3226 * Callback to get the value of a double mixer control that spans 2 registers.
3227 *
3228 * Returns 0 for success.
3229 */
3230int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
3231 struct snd_ctl_elem_value *ucontrol)
3232{
Jon Smirl4eaa9812008-07-29 11:42:26 +01003233 struct soc_mixer_control *mc =
3234 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003235 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04003236 unsigned int reg = mc->reg;
3237 unsigned int reg2 = mc->rreg;
3238 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003239 int max = mc->max;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003240 unsigned int mask = (1 << fls(max)) - 1;
Jon Smirl815ecf82008-07-29 10:22:24 -04003241 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003242
3243 ucontrol->value.integer.value[0] =
3244 (snd_soc_read(codec, reg) >> shift) & mask;
3245 ucontrol->value.integer.value[1] =
3246 (snd_soc_read(codec, reg2) >> shift) & mask;
3247 if (invert) {
3248 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003249 max - ucontrol->value.integer.value[0];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003250 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003251 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003252 }
3253
3254 return 0;
3255}
3256EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
3257
3258/**
3259 * snd_soc_put_volsw_2r - double mixer set callback
3260 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003261 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003262 *
3263 * Callback to set the value of a double mixer control that spans 2 registers.
3264 *
3265 * Returns 0 for success.
3266 */
3267int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
3268 struct snd_ctl_elem_value *ucontrol)
3269{
Jon Smirl4eaa9812008-07-29 11:42:26 +01003270 struct soc_mixer_control *mc =
3271 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003272 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04003273 unsigned int reg = mc->reg;
3274 unsigned int reg2 = mc->rreg;
3275 unsigned int shift = mc->shift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003276 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04003277 unsigned int mask = (1 << fls(max)) - 1;
3278 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003279 int err;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003280 unsigned int val, val2, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003281
3282 val_mask = mask << shift;
3283 val = (ucontrol->value.integer.value[0] & mask);
3284 val2 = (ucontrol->value.integer.value[1] & mask);
3285
3286 if (invert) {
Philipp Zabela7a4ac82008-01-10 14:37:42 +01003287 val = max - val;
3288 val2 = max - val2;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003289 }
3290
3291 val = val << shift;
3292 val2 = val2 << shift;
3293
Eero Nurkkala6c508c62009-10-30 13:34:03 +02003294 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02003295 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003296 return err;
3297
Eero Nurkkala6c508c62009-10-30 13:34:03 +02003298 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003299 return err;
3300}
3301EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
3302
Mark Browne13ac2e2008-05-28 17:58:05 +01003303/**
3304 * snd_soc_info_volsw_s8 - signed mixer info callback
3305 * @kcontrol: mixer control
3306 * @uinfo: control element information
3307 *
3308 * Callback to provide information about a signed mixer control.
3309 *
3310 * Returns 0 for success.
3311 */
3312int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
3313 struct snd_ctl_elem_info *uinfo)
3314{
Jon Smirl4eaa9812008-07-29 11:42:26 +01003315 struct soc_mixer_control *mc =
3316 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003317 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003318 int min = mc->min;
Bradley Rubin572c3b42011-07-07 18:23:44 -07003319 unsigned int shift = mc->shift;
3320 unsigned int rshift = mc->rshift;
Mark Browne13ac2e2008-05-28 17:58:05 +01003321
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003322 if (!mc->platform_max)
3323 mc->platform_max = mc->max;
3324 platform_max = mc->platform_max;
3325
Mark Browne13ac2e2008-05-28 17:58:05 +01003326 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Bradley Rubin572c3b42011-07-07 18:23:44 -07003327 uinfo->count = shift == rshift ? 1 : 2;
Mark Browne13ac2e2008-05-28 17:58:05 +01003328 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003329 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01003330 return 0;
3331}
3332EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
3333
3334/**
3335 * snd_soc_get_volsw_s8 - signed mixer get callback
3336 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003337 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01003338 *
3339 * Callback to get the value of a signed mixer control.
3340 *
3341 * Returns 0 for success.
3342 */
3343int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
3344 struct snd_ctl_elem_value *ucontrol)
3345{
Jon Smirl4eaa9812008-07-29 11:42:26 +01003346 struct soc_mixer_control *mc =
3347 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01003348 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04003349 unsigned int reg = mc->reg;
Bradley Rubin572c3b42011-07-07 18:23:44 -07003350 unsigned int shift = mc->shift;
3351 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003352 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01003353 int val = snd_soc_read(codec, reg);
3354
3355 ucontrol->value.integer.value[0] =
Bradley Rubin572c3b42011-07-07 18:23:44 -07003356 ((signed char)((val >> shift) & 0xff))-min;
3357 if (shift != rshift)
3358 ucontrol->value.integer.value[1] =
3359 ((signed char)((val >> rshift) & 0xff))-min;
Mark Browne13ac2e2008-05-28 17:58:05 +01003360 return 0;
3361}
3362EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
3363
3364/**
3365 * snd_soc_put_volsw_sgn - signed mixer put callback
3366 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003367 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01003368 *
3369 * Callback to set the value of a signed mixer control.
3370 *
3371 * Returns 0 for success.
3372 */
3373int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
3374 struct snd_ctl_elem_value *ucontrol)
3375{
Jon Smirl4eaa9812008-07-29 11:42:26 +01003376 struct soc_mixer_control *mc =
3377 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01003378 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04003379 unsigned int reg = mc->reg;
Bradley Rubin572c3b42011-07-07 18:23:44 -07003380 unsigned int shift = mc->shift;
3381 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003382 int min = mc->min;
Bradley Rubin572c3b42011-07-07 18:23:44 -07003383 unsigned int val, val2, val_mask;
Mark Browne13ac2e2008-05-28 17:58:05 +01003384
Bradley Rubin572c3b42011-07-07 18:23:44 -07003385 val = ((ucontrol->value.integer.value[0]+min) & 0xff) << shift;
3386 val_mask = 0xff << shift;
3387 if (shift != rshift) {
3388 val2 = (ucontrol->value.integer.value[1]+min) & 0xff;
3389 val |= val2 << rshift;
3390 val_mask |= 0xff << rshift;
3391 }
Mark Browne13ac2e2008-05-28 17:58:05 +01003392
Bradley Rubin572c3b42011-07-07 18:23:44 -07003393 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01003394}
3395EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
3396
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003397/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003398 * snd_soc_limit_volume - Set new limit to an existing volume control.
3399 *
3400 * @codec: where to look for the control
3401 * @name: Name of the control
3402 * @max: new maximum limit
3403 *
3404 * Return 0 for success, else error.
3405 */
3406int snd_soc_limit_volume(struct snd_soc_codec *codec,
3407 const char *name, int max)
3408{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003409 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003410 struct snd_kcontrol *kctl;
3411 struct soc_mixer_control *mc;
3412 int found = 0;
3413 int ret = -EINVAL;
3414
3415 /* Sanity check for name and max */
3416 if (unlikely(!name || max <= 0))
3417 return -EINVAL;
3418
3419 list_for_each_entry(kctl, &card->controls, list) {
3420 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3421 found = 1;
3422 break;
3423 }
3424 }
3425 if (found) {
3426 mc = (struct soc_mixer_control *)kctl->private_value;
3427 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003428 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003429 ret = 0;
3430 }
3431 }
3432 return ret;
3433}
3434EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3435
3436/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003437 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
3438 * mixer info callback
3439 * @kcontrol: mixer control
3440 * @uinfo: control element information
3441 *
3442 * Returns 0 for success.
3443 */
3444int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
3445 struct snd_ctl_elem_info *uinfo)
3446{
3447 struct soc_mixer_control *mc =
3448 (struct soc_mixer_control *)kcontrol->private_value;
3449 int max = mc->max;
3450 int min = mc->min;
3451
3452 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3453 uinfo->count = 2;
3454 uinfo->value.integer.min = 0;
3455 uinfo->value.integer.max = max-min;
3456
3457 return 0;
3458}
3459EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
3460
3461/**
3462 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
3463 * mixer get callback
3464 * @kcontrol: mixer control
3465 * @uinfo: control element information
3466 *
3467 * Returns 0 for success.
3468 */
3469int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
3470 struct snd_ctl_elem_value *ucontrol)
3471{
3472 struct soc_mixer_control *mc =
3473 (struct soc_mixer_control *)kcontrol->private_value;
3474 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3475 unsigned int mask = (1<<mc->shift)-1;
3476 int min = mc->min;
3477 int val = snd_soc_read(codec, mc->reg) & mask;
3478 int valr = snd_soc_read(codec, mc->rreg) & mask;
3479
Stuart Longland20630c72010-06-18 12:56:10 +10003480 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
3481 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003482 return 0;
3483}
3484EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
3485
3486/**
3487 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
3488 * mixer put callback
3489 * @kcontrol: mixer control
3490 * @uinfo: control element information
3491 *
3492 * Returns 0 for success.
3493 */
3494int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
3495 struct snd_ctl_elem_value *ucontrol)
3496{
3497 struct soc_mixer_control *mc =
3498 (struct soc_mixer_control *)kcontrol->private_value;
3499 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3500 unsigned int mask = (1<<mc->shift)-1;
3501 int min = mc->min;
3502 int ret;
3503 unsigned int val, valr, oval, ovalr;
3504
3505 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
3506 val &= mask;
3507 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
3508 valr &= mask;
3509
3510 oval = snd_soc_read(codec, mc->reg) & mask;
3511 ovalr = snd_soc_read(codec, mc->rreg) & mask;
3512
3513 ret = 0;
3514 if (oval != val) {
3515 ret = snd_soc_write(codec, mc->reg, val);
3516 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01003517 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003518 }
3519 if (ovalr != valr) {
3520 ret = snd_soc_write(codec, mc->rreg, valr);
3521 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01003522 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003523 }
3524
3525 return 0;
3526}
3527EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
3528
3529/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003530 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3531 * @dai: DAI
3532 * @clk_id: DAI specific clock ID
3533 * @freq: new clock frequency in Hz
3534 * @dir: new clock direction - input/output.
3535 *
3536 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3537 */
3538int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3539 unsigned int freq, int dir)
3540{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003541 if (dai->driver && dai->driver->ops->set_sysclk)
3542 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003543 else if (dai->codec && dai->codec->driver->set_sysclk)
3544 return dai->codec->driver->set_sysclk(dai->codec, clk_id,
3545 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003546 else
3547 return -EINVAL;
3548}
3549EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3550
3551/**
Mark Brownec4ee522011-03-07 20:58:11 +00003552 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3553 * @codec: CODEC
3554 * @clk_id: DAI specific clock ID
3555 * @freq: new clock frequency in Hz
3556 * @dir: new clock direction - input/output.
3557 *
3558 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3559 */
3560int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3561 unsigned int freq, int dir)
3562{
3563 if (codec->driver->set_sysclk)
3564 return codec->driver->set_sysclk(codec, clk_id, freq, dir);
3565 else
3566 return -EINVAL;
3567}
3568EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3569
3570/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003571 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3572 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003573 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003574 * @div: new clock divisor.
3575 *
3576 * Configures the clock dividers. This is used to derive the best DAI bit and
3577 * frame clocks from the system or master clock. It's best to set the DAI bit
3578 * and frame clocks as low as possible to save system power.
3579 */
3580int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3581 int div_id, int div)
3582{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003583 if (dai->driver && dai->driver->ops->set_clkdiv)
3584 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003585 else
3586 return -EINVAL;
3587}
3588EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3589
3590/**
3591 * snd_soc_dai_set_pll - configure DAI PLL.
3592 * @dai: DAI
3593 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003594 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003595 * @freq_in: PLL input clock frequency in Hz
3596 * @freq_out: requested PLL output clock frequency in Hz
3597 *
3598 * Configures and enables PLL to generate output clock based on input clock.
3599 */
Mark Brown85488032009-09-05 18:52:16 +01003600int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3601 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003602{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003603 if (dai->driver && dai->driver->ops->set_pll)
3604 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003605 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003606 else if (dai->codec && dai->codec->driver->set_pll)
3607 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3608 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003609 else
3610 return -EINVAL;
3611}
3612EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3613
Mark Brownec4ee522011-03-07 20:58:11 +00003614/*
3615 * snd_soc_codec_set_pll - configure codec PLL.
3616 * @codec: CODEC
3617 * @pll_id: DAI specific PLL ID
3618 * @source: DAI specific source for the PLL
3619 * @freq_in: PLL input clock frequency in Hz
3620 * @freq_out: requested PLL output clock frequency in Hz
3621 *
3622 * Configures and enables PLL to generate output clock based on input clock.
3623 */
3624int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3625 unsigned int freq_in, unsigned int freq_out)
3626{
3627 if (codec->driver->set_pll)
3628 return codec->driver->set_pll(codec, pll_id, source,
3629 freq_in, freq_out);
3630 else
3631 return -EINVAL;
3632}
3633EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3634
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003635/**
3636 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3637 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003638 * @fmt: SND_SOC_DAIFMT_ format value.
3639 *
3640 * Configures the DAI hardware format and clocking.
3641 */
3642int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3643{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003644 if (dai->driver && dai->driver->ops->set_fmt)
3645 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003646 else
3647 return -EINVAL;
3648}
3649EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3650
3651/**
3652 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3653 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003654 * @tx_mask: bitmask representing active TX slots.
3655 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003656 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003657 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003658 *
3659 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3660 * specific.
3661 */
3662int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003663 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003664{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003665 if (dai->driver && dai->driver->ops->set_tdm_slot)
3666 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003667 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003668 else
3669 return -EINVAL;
3670}
3671EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3672
3673/**
Barry Song472df3c2009-09-12 01:16:29 +08003674 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3675 * @dai: DAI
3676 * @tx_num: how many TX channels
3677 * @tx_slot: pointer to an array which imply the TX slot number channel
3678 * 0~num-1 uses
3679 * @rx_num: how many RX channels
3680 * @rx_slot: pointer to an array which imply the RX slot number channel
3681 * 0~num-1 uses
3682 *
3683 * configure the relationship between channel number and TDM slot number.
3684 */
3685int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3686 unsigned int tx_num, unsigned int *tx_slot,
3687 unsigned int rx_num, unsigned int *rx_slot)
3688{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003689 if (dai->driver && dai->driver->ops->set_channel_map)
3690 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003691 rx_num, rx_slot);
3692 else
3693 return -EINVAL;
3694}
3695EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3696
3697/**
Bharath Ramachandramurthy9c79f132011-11-28 11:18:57 -08003698 * snd_soc_dai_get_channel_map - configure DAI audio channel map
3699 * @dai: DAI
3700 * @tx_num: how many TX channels
3701 * @tx_slot: pointer to an array which imply the TX slot number channel
3702 * 0~num-1 uses
3703 * @rx_num: how many RX channels
3704 * @rx_slot: pointer to an array which imply the RX slot number channel
3705 * 0~num-1 uses
3706 *
3707 * configure the relationship between channel number and TDM slot number.
3708 */
3709int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
3710 unsigned int *tx_num, unsigned int *tx_slot,
3711 unsigned int *rx_num, unsigned int *rx_slot)
3712{
3713 if (dai->driver && dai->driver->ops->get_channel_map)
3714 return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
3715 rx_num, rx_slot);
3716 else
3717 return -EINVAL;
3718}
3719EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
3720/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003721 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3722 * @dai: DAI
3723 * @tristate: tristate enable
3724 *
3725 * Tristates the DAI so that others can use it.
3726 */
3727int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3728{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003729 if (dai->driver && dai->driver->ops->set_tristate)
3730 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003731 else
3732 return -EINVAL;
3733}
3734EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3735
3736/**
3737 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3738 * @dai: DAI
3739 * @mute: mute enable
3740 *
3741 * Mutes the DAI DAC.
3742 */
3743int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3744{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003745 if (dai->driver && dai->driver->ops->digital_mute)
3746 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003747 else
3748 return -EINVAL;
3749}
3750EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3751
Mark Brownc5af3a22008-11-28 13:29:45 +00003752/**
3753 * snd_soc_register_card - Register a card with the ASoC core
3754 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003755 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003756 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003757 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303758int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003759{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003760 int i;
Liam Girdwood13e13a32011-01-31 21:30:52 +00003761 int ret = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003762
Mark Brownc5af3a22008-11-28 13:29:45 +00003763 if (!card->name || !card->dev)
3764 return -EINVAL;
3765
Mark Browned77cc12011-05-03 18:25:34 +01003766 dev_set_drvdata(card->dev, card);
3767
Stephen Warren111c6412011-01-28 14:26:35 -07003768 snd_soc_initialize_card_lists(card);
3769
Vinod Koul150dd2f2011-01-13 22:48:32 +05303770 soc_init_card_debugfs(card);
3771
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003772 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
3773 (card->num_links + card->num_aux_devs),
3774 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003775 if (card->rtd == NULL)
3776 return -ENOMEM;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003777 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003778
Liam Girdwood13e13a32011-01-31 21:30:52 +00003779 for (i = 0; i < card->num_links; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003780 card->rtd[i].dai_link = &card->dai_link[i];
Liam Girdwood13e13a32011-01-31 21:30:52 +00003781 if (card->rtd[i].dai_link->dynamic) {
3782
3783 card->rtd[i].dai_link->codec_name = "null-codec";
3784 card->rtd[i].dai_link->codec_dai_name = "null-codec-dai";
3785
3786 ret = snd_soc_register_codec(card->dev, &null_codec_drv,
3787 &null_codec_dai_drv, 1);
3788 if (ret < 0) {
3789 printk(KERN_ERR "%s: failed to register dynamic DAI link %d\n",
3790 __func__, ret);
3791 goto out;
3792 }
3793
3794 continue;
3795 }
3796 if (card->rtd[i].dai_link->no_codec) {
3797 card->rtd[i].dai_link->codec_name = "null-codec";
3798
3799 ret = snd_soc_register_codec(card->dev, &null_codec_drv,
3800 &null_codec_dai_drv, 1);
3801 if (ret < 0) {
3802 printk(KERN_ERR "%s: failed to register dynamic DAI link %d\n",
3803 __func__, ret);
3804 goto out;
3805 }
3806 continue;
3807 }
3808 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003809
Mark Brownc5af3a22008-11-28 13:29:45 +00003810 INIT_LIST_HEAD(&card->list);
3811 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003812 mutex_init(&card->mutex);
Liam Girdwoodecc1a0d2011-02-16 16:24:17 +00003813 mutex_init(&card->dapm_mutex);
Jayasena Sangaraboina247d36e2012-04-17 10:16:18 -07003814 mutex_init(&card->dapm_power_mutex);
Liam Girdwood13e13a32011-01-31 21:30:52 +00003815 mutex_init(&card->dsp_mutex);
Jayasena Sangaraboinabac60622012-04-09 10:21:43 -07003816 spin_lock_init(&card->dsp_spinlock);
Mark Brownc5af3a22008-11-28 13:29:45 +00003817
3818 mutex_lock(&client_mutex);
3819 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003820 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00003821 mutex_unlock(&client_mutex);
3822
3823 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
3824
Liam Girdwood13e13a32011-01-31 21:30:52 +00003825out:
3826 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003827}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303828EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003829
3830/**
3831 * snd_soc_unregister_card - Unregister a card with the ASoC core
3832 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003833 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003834 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003835 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303836int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003837{
Vinod Koulb0e26482011-01-13 22:48:02 +05303838 if (card->instantiated)
3839 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003840 mutex_lock(&client_mutex);
3841 list_del(&card->list);
3842 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003843 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3844
3845 return 0;
3846}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303847EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003848
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003849/*
3850 * Simplify DAI link configuration by removing ".-1" from device names
3851 * and sanitizing names.
3852 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003853static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003854{
3855 char *found, name[NAME_SIZE];
3856 int id1, id2;
3857
3858 if (dev_name(dev) == NULL)
3859 return NULL;
3860
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003861 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003862
3863 /* are we a "%s.%d" name (platform and SPI components) */
3864 found = strstr(name, dev->driver->name);
3865 if (found) {
3866 /* get ID */
3867 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3868
3869 /* discard ID from name if ID == -1 */
3870 if (*id == -1)
3871 found[strlen(dev->driver->name)] = '\0';
3872 }
3873
3874 } else {
3875 /* I2C component devices are named "bus-addr" */
3876 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3877 char tmp[NAME_SIZE];
3878
3879 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003880 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003881
3882 /* sanitize component name for DAI link creation */
3883 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003884 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003885 } else
3886 *id = 0;
3887 }
3888
3889 return kstrdup(name, GFP_KERNEL);
3890}
3891
3892/*
3893 * Simplify DAI link naming for single devices with multiple DAIs by removing
3894 * any ".-1" and using the DAI name (instead of device name).
3895 */
3896static inline char *fmt_multiple_name(struct device *dev,
3897 struct snd_soc_dai_driver *dai_drv)
3898{
3899 if (dai_drv->name == NULL) {
3900 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3901 dev_name(dev));
3902 return NULL;
3903 }
3904
3905 return kstrdup(dai_drv->name, GFP_KERNEL);
3906}
3907
Mark Brown91151712008-11-30 23:31:24 +00003908/**
3909 * snd_soc_register_dai - Register a DAI with the ASoC core
3910 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003911 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003912 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003913int snd_soc_register_dai(struct device *dev,
3914 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003915{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003916 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003917
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003918 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003919
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003920 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3921 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003922 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003923
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003924 /* create DAI component name */
3925 dai->name = fmt_single_name(dev, &dai->id);
3926 if (dai->name == NULL) {
3927 kfree(dai);
3928 return -ENOMEM;
3929 }
3930
3931 dai->dev = dev;
3932 dai->driver = dai_drv;
3933 if (!dai->driver->ops)
3934 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003935
3936 mutex_lock(&client_mutex);
3937 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003938 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003939 mutex_unlock(&client_mutex);
3940
3941 pr_debug("Registered DAI '%s'\n", dai->name);
3942
3943 return 0;
3944}
3945EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3946
3947/**
3948 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3949 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003950 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003951 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003952void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003953{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003954 struct snd_soc_dai *dai;
3955
3956 list_for_each_entry(dai, &dai_list, list) {
3957 if (dev == dai->dev)
3958 goto found;
3959 }
3960 return;
3961
3962found:
Mark Brown91151712008-11-30 23:31:24 +00003963 mutex_lock(&client_mutex);
3964 list_del(&dai->list);
3965 mutex_unlock(&client_mutex);
3966
3967 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003968 kfree(dai->name);
3969 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003970}
3971EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3972
3973/**
3974 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3975 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003976 * @dai: Array of DAIs to register
3977 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003978 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003979int snd_soc_register_dais(struct device *dev,
3980 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003981{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003982 struct snd_soc_dai *dai;
3983 int i, ret = 0;
3984
Liam Girdwood720ffa42010-08-18 00:30:30 +01003985 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003986
3987 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003988
3989 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003990 if (dai == NULL) {
3991 ret = -ENOMEM;
3992 goto err;
3993 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003994
3995 /* create DAI component name */
3996 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3997 if (dai->name == NULL) {
3998 kfree(dai);
3999 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00004000 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004001 }
4002
4003 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004004 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01004005 if (dai->driver->id)
4006 dai->id = dai->driver->id;
4007 else
4008 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004009 if (!dai->driver->ops)
4010 dai->driver->ops = &null_dai_ops;
4011
4012 mutex_lock(&client_mutex);
4013 list_add(&dai->list, &dai_list);
4014 mutex_unlock(&client_mutex);
4015
4016 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00004017 }
4018
Axel Lin1dcb4f32010-12-06 16:48:03 +08004019 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004020 snd_soc_instantiate_cards();
Axel Lin1dcb4f32010-12-06 16:48:03 +08004021 mutex_unlock(&client_mutex);
Mark Brown91151712008-11-30 23:31:24 +00004022 return 0;
4023
4024err:
4025 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004026 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00004027
4028 return ret;
4029}
4030EXPORT_SYMBOL_GPL(snd_soc_register_dais);
4031
4032/**
4033 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
4034 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004035 * @dai: Array of DAIs to unregister
4036 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00004037 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004038void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00004039{
4040 int i;
4041
4042 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004043 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00004044}
4045EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
4046
Mark Brown12a48a8c2008-12-03 19:40:30 +00004047/**
4048 * snd_soc_register_platform - Register a platform with the ASoC core
4049 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004050 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00004051 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004052int snd_soc_register_platform(struct device *dev,
4053 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004054{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004055 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00004056
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004057 dev_dbg(dev, "platform register %s\n", dev_name(dev));
4058
4059 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4060 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08004061 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004062
4063 /* create platform component name */
4064 platform->name = fmt_single_name(dev, &platform->id);
4065 if (platform->name == NULL) {
4066 kfree(platform);
4067 return -ENOMEM;
4068 }
4069
4070 platform->dev = dev;
Liam Girdwood7987a112011-01-31 19:52:42 +00004071 platform->dapm.platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004072 platform->driver = platform_drv;
Liam Girdwoodaff46a02011-03-30 23:25:37 +01004073 platform->dapm.dev = dev;
Liam Girdwood670ff442011-03-30 23:27:27 +01004074 platform->dapm.stream_event = platform_drv->stream_event;
Mark Brown12a48a8c2008-12-03 19:40:30 +00004075
4076 mutex_lock(&client_mutex);
4077 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00004078 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00004079 mutex_unlock(&client_mutex);
4080
4081 pr_debug("Registered platform '%s'\n", platform->name);
4082
4083 return 0;
4084}
4085EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4086
4087/**
4088 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4089 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004090 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004091 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004092void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004093{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004094 struct snd_soc_platform *platform;
4095
4096 list_for_each_entry(platform, &platform_list, list) {
4097 if (dev == platform->dev)
4098 goto found;
4099 }
4100 return;
4101
4102found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00004103 mutex_lock(&client_mutex);
4104 list_del(&platform->list);
4105 mutex_unlock(&client_mutex);
4106
4107 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004108 kfree(platform->name);
4109 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004110}
4111EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4112
Mark Brown151ab222009-05-09 16:22:58 +01004113static u64 codec_format_map[] = {
4114 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4115 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4116 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4117 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4118 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4119 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4120 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4121 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4122 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4123 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4124 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4125 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4126 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4127 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4128 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4129 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4130};
4131
4132/* Fix up the DAI formats for endianness: codecs don't actually see
4133 * the endianness of the data but we're using the CPU format
4134 * definitions which do need to include endianness so we ensure that
4135 * codec DAIs always have both big and little endian variants set.
4136 */
4137static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4138{
4139 int i;
4140
4141 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4142 if (stream->formats & codec_format_map[i])
4143 stream->formats |= codec_format_map[i];
4144}
4145
Mark Brown0d0cf002008-12-10 14:32:45 +00004146/**
4147 * snd_soc_register_codec - Register a codec with the ASoC core
4148 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004149 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004150 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004151int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004152 const struct snd_soc_codec_driver *codec_drv,
4153 struct snd_soc_dai_driver *dai_drv,
4154 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004155{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004156 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004157 struct snd_soc_codec *codec;
4158 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004159
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004160 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004161
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004162 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4163 if (codec == NULL)
4164 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004165
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004166 /* create CODEC component name */
Liam Girdwood13e13a32011-01-31 21:30:52 +00004167 if (codec_drv == &null_codec_drv)
4168 codec->name = kstrdup("null-codec", GFP_KERNEL);
4169 else
4170 codec->name = fmt_single_name(dev, &codec->id);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004171 if (codec->name == NULL) {
4172 kfree(codec);
4173 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01004174 }
4175
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00004176 if (codec_drv->compress_type)
4177 codec->compress_type = codec_drv->compress_type;
4178 else
4179 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
4180
Mark Brownc3acec22010-12-02 16:15:29 +00004181 codec->write = codec_drv->write;
4182 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004183 codec->volatile_register = codec_drv->volatile_register;
4184 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004185 codec->writable_register = codec_drv->writable_register;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004186 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4187 codec->dapm.dev = dev;
4188 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00004189 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood670ff442011-03-30 23:27:27 +01004190 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004191 codec->dev = dev;
4192 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004193 codec->num_dai = num_dai;
4194 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004195
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004196 /* allocate CODEC register cache */
4197 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004198 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00004199 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004200 /* it is necessary to make a copy of the default register cache
4201 * because in the case of using a compression type that requires
4202 * the default register cache to be marked as __devinitconst the
4203 * kernel might have freed the array by the time we initialize
4204 * the cache.
4205 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00004206 if (codec_drv->reg_cache_default) {
4207 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
4208 reg_size, GFP_KERNEL);
4209 if (!codec->reg_def_copy) {
4210 ret = -ENOMEM;
4211 goto fail;
4212 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004213 }
4214 }
4215
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004216 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
4217 if (!codec->volatile_register)
4218 codec->volatile_register = snd_soc_default_volatile_register;
4219 if (!codec->readable_register)
4220 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004221 if (!codec->writable_register)
4222 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004223 }
4224
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004225 for (i = 0; i < num_dai; i++) {
4226 fixup_codec_formats(&dai_drv[i].playback);
4227 fixup_codec_formats(&dai_drv[i].capture);
4228 }
4229
Mark Brown26b01cc2010-08-18 20:20:55 +01004230 /* register any DAIs */
4231 if (num_dai) {
4232 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4233 if (ret < 0)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00004234 goto fail;
Mark Brown26b01cc2010-08-18 20:20:55 +01004235 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004236
Mark Brown0d0cf002008-12-10 14:32:45 +00004237 mutex_lock(&client_mutex);
4238 list_add(&codec->list, &codec_list);
4239 snd_soc_instantiate_cards();
4240 mutex_unlock(&client_mutex);
4241
4242 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004243 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004244
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00004245fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004246 kfree(codec->reg_def_copy);
4247 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004248 kfree(codec->name);
4249 kfree(codec);
4250 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004251}
4252EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4253
4254/**
4255 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4256 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004257 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004258 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004259void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004260{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004261 struct snd_soc_codec *codec;
4262 int i;
4263
4264 list_for_each_entry(codec, &codec_list, list) {
4265 if (dev == codec->dev)
4266 goto found;
4267 }
4268 return;
4269
4270found:
Mark Brown26b01cc2010-08-18 20:20:55 +01004271 if (codec->num_dai)
4272 for (i = 0; i < codec->num_dai; i++)
4273 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004274
Mark Brown0d0cf002008-12-10 14:32:45 +00004275 mutex_lock(&client_mutex);
4276 list_del(&codec->list);
4277 mutex_unlock(&client_mutex);
4278
4279 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004280
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004281 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004282 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01004283 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004284 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004285}
4286EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4287
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004288static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004289{
Mark Brown384c89e2008-12-03 17:34:03 +00004290#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004291 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4292 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Mark Brown384c89e2008-12-03 17:34:03 +00004293 printk(KERN_WARNING
4294 "ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004295 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004296 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004297
Mark Brown8a9dab12011-01-10 22:25:21 +00004298 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004299 &codec_list_fops))
4300 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4301
Mark Brown8a9dab12011-01-10 22:25:21 +00004302 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004303 &dai_list_fops))
4304 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004305
Mark Brown8a9dab12011-01-10 22:25:21 +00004306 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004307 &platform_list_fops))
4308 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004309#endif
4310
Mark Brownfb257892011-04-28 10:57:54 +01004311 snd_soc_util_init();
4312
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004313 return platform_driver_register(&soc_driver);
4314}
Mark Brown4abe8e12010-10-12 17:41:03 +01004315module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004316
Mark Brown7d8c16a2008-11-30 22:11:24 +00004317static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004318{
Mark Brownfb257892011-04-28 10:57:54 +01004319 snd_soc_util_exit();
4320
Mark Brown384c89e2008-12-03 17:34:03 +00004321#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004322 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004323#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004324 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004325}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004326module_exit(snd_soc_exit);
4327
4328/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004329MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004330MODULE_DESCRIPTION("ALSA SoC Core");
4331MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004332MODULE_ALIAS("platform:soc-audio");