blob: de98febeb48fbb62c018fd9b137a555e18bf31a4 [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 Brownf0e8ed82011-09-20 11:41:54 +010033#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070035#include <linux/of.h>
Marek Vasut474828a2009-07-22 13:01:03 +020036#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020037#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000038#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020039#include <sound/pcm.h>
40#include <sound/pcm_params.h>
41#include <sound/soc.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070042#include <sound/soc-dpcm.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020043#include <sound/initval.h>
44
Mark Browna8b1d342010-11-03 18:05:58 -040045#define CREATE_TRACE_POINTS
46#include <trace/events/asoc.h>
47
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000048#define NAME_SIZE 32
49
Frank Mandarinodb2a4162006-10-06 18:31:09 +020050static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
51
Mark Brown384c89e2008-12-03 17:34:03 +000052#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000053struct dentry *snd_soc_debugfs_root;
54EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000055#endif
56
Mark Brownc5af3a22008-11-28 13:29:45 +000057static DEFINE_MUTEX(client_mutex);
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
Steve Mucklef132c6c2012-06-06 18:30:57 -070062int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
63int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd);
64int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute);
65int soc_dpcm_be_ac97_cpu_dai_suspend(struct snd_soc_pcm_runtime *fe);
66int soc_dpcm_be_ac97_cpu_dai_resume(struct snd_soc_pcm_runtime *fe);
67int soc_dpcm_be_cpu_dai_resume(struct snd_soc_pcm_runtime *fe);
68int soc_dpcm_be_cpu_dai_suspend(struct snd_soc_pcm_runtime *fe);
69int soc_dpcm_be_platform_suspend(struct snd_soc_pcm_runtime *fe);
70int soc_dpcm_be_platform_resume(struct snd_soc_pcm_runtime *fe);
Mark Brownc5af3a22008-11-28 13:29:45 +000071
Frank Mandarinodb2a4162006-10-06 18:31:09 +020072/*
73 * This is a timeout to do a DAPM powerdown after a stream is closed().
74 * It can be used to eliminate pops between different playback streams, e.g.
75 * between two audio tracks.
76 */
Patrick Lai95c1e672012-04-13 21:52:20 -070077static int pmdown_time;
Frank Mandarinodb2a4162006-10-06 18:31:09 +020078module_param(pmdown_time, int, 0);
79MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
80
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000081/* returns the minimum number of bytes needed to represent
82 * a particular given value */
83static int min_bytes_needed(unsigned long val)
84{
85 int c = 0;
86 int i;
87
88 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
89 if (val & (1UL << i))
90 break;
91 c = (sizeof val * 8) - c;
92 if (!c || (c % 8))
93 c = (c + 8) / 8;
94 else
95 c /= 8;
96 return c;
97}
98
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000099/* fill buf which is 'len' bytes with a formatted
100 * string of the form 'reg: value\n' */
101static int format_register_str(struct snd_soc_codec *codec,
102 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +0000103{
Stephen Warren00b317a2011-04-01 14:50:44 -0600104 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
105 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000106 int ret;
107 char tmpbuf[len + 1];
108 char regbuf[regsize + 1];
109
110 /* since tmpbuf is allocated on the stack, warn the callers if they
111 * try to abuse this function */
112 WARN_ON(len > 63);
113
114 /* +2 for ': ' and + 1 for '\n' */
115 if (wordsize + regsize + 2 + 1 != len)
116 return -EINVAL;
117
Mark Brown25032c12011-08-01 13:52:48 +0900118 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000119 if (ret < 0) {
120 memset(regbuf, 'X', regsize);
121 regbuf[regsize] = '\0';
122 } else {
123 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
124 }
125
126 /* prepare the buffer */
127 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
128 /* copy it back to the caller without the '\0' */
129 memcpy(buf, tmpbuf, len);
130
131 return 0;
132}
133
134/* codec register dump */
135static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
136 size_t count, loff_t pos)
137{
138 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000139 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000140 int len;
141 size_t total = 0;
142 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000143
Stephen Warren00b317a2011-04-01 14:50:44 -0600144 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
145 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000146
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000147 len = wordsize + regsize + 2 + 1;
148
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000149 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000150 return 0;
151
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000152 if (codec->driver->reg_cache_step)
153 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000154
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000155 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausenb92d1502011-08-27 18:24:14 +0200156 if (!snd_soc_codec_readable_register(codec, i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000157 continue;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000158 if (codec->driver->display_register) {
159 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000160 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100161 } else {
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000162 /* only support larger than PAGE_SIZE bytes debugfs
163 * entries for the default case */
164 if (p >= pos) {
165 if (total + len >= count - 1)
166 break;
167 format_register_str(codec, i, buf + total, len);
168 total += len;
169 }
170 p += len;
Mark Brown5164d742010-07-14 16:14:33 +0100171 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000172 }
173
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000174 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000175
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000176 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000177}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000178
Mark Brown2624d5f2009-11-03 21:56:13 +0000179static ssize_t codec_reg_show(struct device *dev,
180 struct device_attribute *attr, char *buf)
181{
Mark Brown36ae1a92012-01-06 17:12:45 -0800182 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000183
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000184 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000185}
186
187static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
188
Mark Browndbe21402010-02-12 11:37:24 +0000189static ssize_t pmdown_time_show(struct device *dev,
190 struct device_attribute *attr, char *buf)
191{
Mark Brown36ae1a92012-01-06 17:12:45 -0800192 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000193
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000194 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000195}
196
197static ssize_t pmdown_time_set(struct device *dev,
198 struct device_attribute *attr,
199 const char *buf, size_t count)
200{
Mark Brown36ae1a92012-01-06 17:12:45 -0800201 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700202 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000203
Mark Brownc593b522010-10-27 20:11:17 -0700204 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
205 if (ret)
206 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000207
208 return count;
209}
210
211static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
212
Mark Brown2624d5f2009-11-03 21:56:13 +0000213#ifdef CONFIG_DEBUG_FS
214static int codec_reg_open_file(struct inode *inode, struct file *file)
215{
216 file->private_data = inode->i_private;
217 return 0;
218}
219
220static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000221 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000222{
223 ssize_t ret;
224 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000225 char *buf;
226
227 if (*ppos < 0 || !count)
228 return -EINVAL;
229
230 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000231 if (!buf)
232 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000233
234 ret = soc_codec_reg_show(codec, buf, count, *ppos);
235 if (ret >= 0) {
236 if (copy_to_user(user_buf, buf, ret)) {
237 kfree(buf);
238 return -EFAULT;
239 }
240 *ppos += ret;
241 }
242
Mark Brown2624d5f2009-11-03 21:56:13 +0000243 kfree(buf);
244 return ret;
245}
246
247static ssize_t codec_reg_write_file(struct file *file,
248 const char __user *user_buf, size_t count, loff_t *ppos)
249{
250 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700251 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000252 char *start = buf;
253 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000254 struct snd_soc_codec *codec = file->private_data;
255
256 buf_size = min(count, (sizeof(buf)-1));
257 if (copy_from_user(buf, user_buf, buf_size))
258 return -EFAULT;
259 buf[buf_size] = 0;
260
Mark Brown2624d5f2009-11-03 21:56:13 +0000261 while (*start == ' ')
262 start++;
263 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000264 while (*start == ' ')
265 start++;
266 if (strict_strtoul(start, 16, &value))
267 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000268
269 /* Userspace has been fiddling around behind the kernel's back */
270 add_taint(TAINT_USER);
271
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000272 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000273 return buf_size;
274}
275
276static const struct file_operations codec_reg_fops = {
277 .open = codec_reg_open_file,
278 .read = codec_reg_read_file,
279 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200280 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000281};
282
283static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
284{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200285 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
286
287 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
288 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000289 if (!codec->debugfs_codec_root) {
290 printk(KERN_WARNING
291 "ASoC: Failed to create codec debugfs directory\n");
292 return;
293 }
294
Mark Brownaaee8ef2011-01-26 20:53:50 +0000295 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
296 &codec->cache_sync);
297 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
298 &codec->cache_only);
299
Mark Brown2624d5f2009-11-03 21:56:13 +0000300 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
301 codec->debugfs_codec_root,
302 codec, &codec_reg_fops);
303 if (!codec->debugfs_reg)
304 printk(KERN_WARNING
305 "ASoC: Failed to create codec register debugfs file\n");
306
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200307 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000308}
309
310static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
311{
312 debugfs_remove_recursive(codec->debugfs_codec_root);
313}
314
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000315static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
316{
317 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
318
319 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
320 debugfs_card_root);
321 if (!platform->debugfs_platform_root) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700322 printk(KERN_WARNING
323 "ASoC: Failed to create platform debugfs directory\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000324 return;
325 }
326
Steve Mucklef132c6c2012-06-06 18:30:57 -0700327 snd_soc_dapm_debugfs_init(&platform->dapm, platform->debugfs_platform_root);
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000328}
329
330static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
331{
332 debugfs_remove_recursive(platform->debugfs_platform_root);
333}
334
Mark Brownc3c5a192010-09-15 18:15:14 +0100335static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
336 size_t count, loff_t *ppos)
337{
338 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100339 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100340 struct snd_soc_codec *codec;
341
342 if (!buf)
343 return -ENOMEM;
344
Mark Brown2b194f9d2010-10-13 10:52:16 +0100345 list_for_each_entry(codec, &codec_list, list) {
346 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
347 codec->name);
348 if (len >= 0)
349 ret += len;
350 if (ret > PAGE_SIZE) {
351 ret = PAGE_SIZE;
352 break;
353 }
354 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100355
356 if (ret >= 0)
357 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
358
359 kfree(buf);
360
361 return ret;
362}
363
364static const struct file_operations codec_list_fops = {
365 .read = codec_list_read_file,
366 .llseek = default_llseek,/* read accesses f_pos */
367};
368
Mark Brownf3208782010-09-15 18:19:07 +0100369static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
370 size_t count, loff_t *ppos)
371{
372 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100373 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100374 struct snd_soc_dai *dai;
375
376 if (!buf)
377 return -ENOMEM;
378
Mark Brown2b194f9d2010-10-13 10:52:16 +0100379 list_for_each_entry(dai, &dai_list, list) {
380 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
381 if (len >= 0)
382 ret += len;
383 if (ret > PAGE_SIZE) {
384 ret = PAGE_SIZE;
385 break;
386 }
387 }
Mark Brownf3208782010-09-15 18:19:07 +0100388
Mark Brown2b194f9d2010-10-13 10:52:16 +0100389 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100390
391 kfree(buf);
392
393 return ret;
394}
395
396static const struct file_operations dai_list_fops = {
397 .read = dai_list_read_file,
398 .llseek = default_llseek,/* read accesses f_pos */
399};
400
Mark Brown19c7ac22010-09-15 18:22:40 +0100401static ssize_t platform_list_read_file(struct file *file,
402 char __user *user_buf,
403 size_t count, loff_t *ppos)
404{
405 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100406 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100407 struct snd_soc_platform *platform;
408
409 if (!buf)
410 return -ENOMEM;
411
Mark Brown2b194f9d2010-10-13 10:52:16 +0100412 list_for_each_entry(platform, &platform_list, list) {
413 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
414 platform->name);
415 if (len >= 0)
416 ret += len;
417 if (ret > PAGE_SIZE) {
418 ret = PAGE_SIZE;
419 break;
420 }
421 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100422
Mark Brown2b194f9d2010-10-13 10:52:16 +0100423 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100424
425 kfree(buf);
426
427 return ret;
428}
429
430static const struct file_operations platform_list_fops = {
431 .read = platform_list_read_file,
432 .llseek = default_llseek,/* read accesses f_pos */
433};
434
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200435static void soc_init_card_debugfs(struct snd_soc_card *card)
436{
437 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000438 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200439 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200440 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100441 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200442 return;
443 }
444
445 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
446 card->debugfs_card_root,
447 &card->pop_time);
448 if (!card->debugfs_pop_time)
449 dev_warn(card->dev,
450 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200451}
452
453static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
454{
455 debugfs_remove_recursive(card->debugfs_card_root);
456}
457
Mark Brown2624d5f2009-11-03 21:56:13 +0000458#else
459
460static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
461{
462}
463
464static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
465{
466}
Axel Linb95fccb2010-11-09 17:06:44 +0800467
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000468static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
469{
470}
471
472static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
473{
474}
475
Axel Linb95fccb2010-11-09 17:06:44 +0800476static inline void soc_init_card_debugfs(struct snd_soc_card *card)
477{
478}
479
480static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
481{
482}
Mark Brown2624d5f2009-11-03 21:56:13 +0000483#endif
484
Steve Mucklef132c6c2012-06-06 18:30:57 -0700485struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
486 const char *dai_link, int stream)
487{
488 int i;
489
490 for (i = 0; i < card->num_links; i++) {
491 if (card->rtd[i].dai_link->no_pcm &&
492 !strcmp(card->rtd[i].dai_link->name, dai_link))
493 return card->rtd[i].pcm->streams[stream].substream;
494 }
495 dev_dbg(card->dev, "failed to find dai link %s\n", dai_link);
496 return NULL;
497}
498EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
499
500struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
501 const char *dai_link)
502{
503 int i;
504
505 for (i = 0; i < card->num_links; i++) {
506 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
507 return &card->rtd[i];
508 }
509 dev_dbg(card->dev, "failed to find rtd %s\n", dai_link);
510 return NULL;
511}
512EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
513
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200514#ifdef CONFIG_SND_SOC_AC97_BUS
515/* unregister ac97 codec */
516static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
517{
518 if (codec->ac97->dev.bus)
519 device_unregister(&codec->ac97->dev);
520 return 0;
521}
522
523/* stop no dev release warning */
524static void soc_ac97_device_release(struct device *dev){}
525
526/* register ac97 codec to bus */
527static int soc_ac97_dev_register(struct snd_soc_codec *codec)
528{
529 int err;
530
531 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100532 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200533 codec->ac97->dev.release = soc_ac97_device_release;
534
Kay Sieversbb072bf2008-11-02 03:50:35 +0100535 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000536 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200537 err = device_register(&codec->ac97->dev);
538 if (err < 0) {
539 snd_printk(KERN_ERR "Can't register ac97 bus\n");
540 codec->ac97->dev.bus = NULL;
541 return err;
542 }
543 return 0;
544}
545#endif
546
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000547#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200548/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000549int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200550{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000551 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200552 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200553 int i;
554
Daniel Macke3509ff2009-06-03 17:44:49 +0200555 /* If the initialization of this soc device failed, there is no codec
556 * associated with it. Just bail out in this case.
557 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200559 return 0;
560
Andy Green6ed25972008-06-13 16:24:05 +0100561 /* Due to the resume being scheduled into a workqueue we could
562 * suspend before that's finished - wait for it to complete.
563 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000564 snd_power_lock(card->snd_card);
565 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
566 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100567
568 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100570
Mark Browna00f90f2010-12-02 16:24:24 +0000571 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 for (i = 0; i < card->num_rtd; i++) {
573 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
574 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100575
Liam Girdwood13e13a32011-01-31 21:30:52 +0000576 if (card->rtd[i].dai_link->ignore_suspend ||
577 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +0100578 continue;
579
Liam Girdwood13e13a32011-01-31 21:30:52 +0000580 if (card->rtd[i].dai_link->dynamic)
Steve Mucklef132c6c2012-06-06 18:30:57 -0700581 soc_dpcm_be_digital_mute(&card->rtd[i], 1);
Liam Girdwood13e13a32011-01-31 21:30:52 +0000582 else {
583 if (drv->ops->digital_mute && dai->playback_active)
584 drv->ops->digital_mute(dai, 1);
585 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200586 }
587
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100588 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000589 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwood13e13a32011-01-31 21:30:52 +0000590 if (card->rtd[i].dai_link->ignore_suspend ||
591 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +0100592 continue;
593
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000594 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100595 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100596
Mark Brown87506542008-11-18 20:50:34 +0000597 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000598 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200599
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000600 for (i = 0; i < card->num_rtd; i++) {
601 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
602 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100603
Liam Girdwood13e13a32011-01-31 21:30:52 +0000604 if (card->rtd[i].dai_link->ignore_suspend ||
605 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +0100606 continue;
607
Liam Girdwood13e13a32011-01-31 21:30:52 +0000608 if (card->rtd[i].dai_link->dynamic) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700609 soc_dpcm_be_cpu_dai_suspend(&card->rtd[i]);
610 soc_dpcm_be_platform_suspend(&card->rtd[i]);
Liam Girdwood13e13a32011-01-31 21:30:52 +0000611 } else {
612 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
613 cpu_dai->driver->suspend(cpu_dai);
614 if (platform->driver->suspend && !platform->suspended) {
615 platform->driver->suspend(cpu_dai);
616 platform->suspended = 1;
617 }
Mark Brown1547aba2010-05-07 21:11:40 +0100618 }
619 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200620
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 /* close any waiting streams and save state */
622 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +0100623 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200624 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000625 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100626
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000627 for (i = 0; i < card->num_rtd; i++) {
628 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
629
Liam Girdwood13e13a32011-01-31 21:30:52 +0000630 if (card->rtd[i].dai_link->ignore_suspend ||
631 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +0100632 continue;
633
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000634 if (driver->playback.stream_name != NULL)
635 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
636 SND_SOC_DAPM_STREAM_SUSPEND);
637
638 if (driver->capture.stream_name != NULL)
639 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
640 SND_SOC_DAPM_STREAM_SUSPEND);
641 }
642
643 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200644 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000645 /* If there are paths active then the CODEC will be held with
646 * bias _ON and should not be suspended. */
647 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200648 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000649 case SND_SOC_BIAS_STANDBY:
650 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100651 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000652 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900653 codec->cache_sync = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000654 break;
655 default:
656 dev_dbg(codec->dev, "CODEC is on over suspend\n");
657 break;
658 }
659 }
660 }
661
662 for (i = 0; i < card->num_rtd; i++) {
663 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
664
Liam Girdwood13e13a32011-01-31 21:30:52 +0000665 if (card->rtd[i].dai_link->ignore_suspend ||
666 card->rtd[i].dai_link->no_pcm)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000667 continue;
668
Liam Girdwood13e13a32011-01-31 21:30:52 +0000669 if (card->rtd[i].dai_link->dynamic)
Steve Mucklef132c6c2012-06-06 18:30:57 -0700670 soc_dpcm_be_ac97_cpu_dai_suspend(&card->rtd[i]);
Liam Girdwood13e13a32011-01-31 21:30:52 +0000671 else
672 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
673 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200674 }
675
Mark Brown87506542008-11-18 20:50:34 +0000676 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000677 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200678
679 return 0;
680}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000681EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200682
Andy Green6ed25972008-06-13 16:24:05 +0100683/* deferred resume work, so resume can complete before we finished
684 * setting our codec back up, which can be very slow on I2C
685 */
686static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200687{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000688 struct snd_soc_card *card =
689 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200690 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200691 int i;
692
Andy Green6ed25972008-06-13 16:24:05 +0100693 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
694 * so userspace apps are blocked from touching us
695 */
696
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000697 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100698
Mark Brown99497882010-05-07 20:24:05 +0100699 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000700 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100701
Mark Brown87506542008-11-18 20:50:34 +0000702 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000703 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200704
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000705 /* resume AC97 DAIs */
706 for (i = 0; i < card->num_rtd; i++) {
707 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100708
Liam Girdwood13e13a32011-01-31 21:30:52 +0000709 if (card->rtd[i].dai_link->ignore_suspend ||
710 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +0100711 continue;
712
Liam Girdwood13e13a32011-01-31 21:30:52 +0000713 if (card->rtd[i].dai_link->dynamic)
Steve Mucklef132c6c2012-06-06 18:30:57 -0700714 soc_dpcm_be_ac97_cpu_dai_resume(&card->rtd[i]);
Liam Girdwood13e13a32011-01-31 21:30:52 +0000715 else
716 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
717 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200718 }
719
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200720 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000721 /* If the CODEC was idle over suspend then it will have been
722 * left with bias OFF or STANDBY and suspended so we must now
723 * resume. Otherwise the suspend was suppressed.
724 */
725 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200726 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000727 case SND_SOC_BIAS_STANDBY:
728 case SND_SOC_BIAS_OFF:
729 codec->driver->resume(codec);
730 codec->suspended = 0;
731 break;
732 default:
733 dev_dbg(codec->dev, "CODEC was on over suspend\n");
734 break;
735 }
Mark Brown1547aba2010-05-07 21:11:40 +0100736 }
737 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200738
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000739 for (i = 0; i < card->num_rtd; i++) {
740 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100741
Liam Girdwood13e13a32011-01-31 21:30:52 +0000742 if (card->rtd[i].dai_link->ignore_suspend ||
743 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +0100744 continue;
745
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000746 if (driver->playback.stream_name != NULL)
747 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200748 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000749
750 if (driver->capture.stream_name != NULL)
751 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200752 SND_SOC_DAPM_STREAM_RESUME);
753 }
754
Mark Brown3ff3f642008-05-19 12:32:25 +0200755 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000756 for (i = 0; i < card->num_rtd; i++) {
757 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
758 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100759
Liam Girdwood13e13a32011-01-31 21:30:52 +0000760 if (card->rtd[i].dai_link->ignore_suspend ||
761 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +0100762 continue;
763
Liam Girdwood13e13a32011-01-31 21:30:52 +0000764 if (card->rtd[i].dai_link->dynamic)
Steve Mucklef132c6c2012-06-06 18:30:57 -0700765 soc_dpcm_be_digital_mute(&card->rtd[i], 0);
Liam Girdwood13e13a32011-01-31 21:30:52 +0000766 else {
767 if (drv->ops->digital_mute && dai->playback_active)
768 drv->ops->digital_mute(dai, 0);
769 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200770 }
771
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000772 for (i = 0; i < card->num_rtd; i++) {
773 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
774 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100775
Liam Girdwood13e13a32011-01-31 21:30:52 +0000776 if (card->rtd[i].dai_link->ignore_suspend ||
777 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +0100778 continue;
779
Liam Girdwood13e13a32011-01-31 21:30:52 +0000780 if (card->rtd[i].dai_link->dynamic) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700781 soc_dpcm_be_cpu_dai_resume(&card->rtd[i]);
782 soc_dpcm_be_platform_resume(&card->rtd[i]);
Liam Girdwood13e13a32011-01-31 21:30:52 +0000783 } else {
784 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
785 cpu_dai->driver->resume(cpu_dai);
786 if (platform->driver->resume && platform->suspended) {
787 platform->driver->resume(cpu_dai);
788 platform->suspended = 0;
789 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000790 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200791 }
792
Mark Brown87506542008-11-18 20:50:34 +0000793 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000794 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200795
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000796 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100797
798 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000799 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100800}
801
802/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000803int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100804{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000805 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600806 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200807
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800808 /* If the initialization of this soc device failed, there is no codec
809 * associated with it. Just bail out in this case.
810 */
811 if (list_empty(&card->codec_dev_list))
812 return 0;
813
Mark Brown64ab9ba2009-03-31 11:27:03 +0100814 /* AC97 devices might have other drivers hanging off them so
815 * need to resume immediately. Other drivers don't have that
816 * problem and may take a substantial amount of time to resume
817 * due to I/O costs and anti-pop so handle them out of line.
818 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000819 for (i = 0; i < card->num_rtd; i++) {
820 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600821 ac97_control |= cpu_dai->driver->ac97_control;
822 }
823 if (ac97_control) {
824 dev_dbg(dev, "Resuming AC97 immediately\n");
825 soc_resume_deferred(&card->deferred_resume_work);
826 } else {
827 dev_dbg(dev, "Scheduling resume work\n");
828 if (!schedule_work(&card->deferred_resume_work))
829 dev_err(dev, "resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100830 }
Andy Green6ed25972008-06-13 16:24:05 +0100831
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200832 return 0;
833}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000834EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200835#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000836#define snd_soc_suspend NULL
837#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200838#endif
839
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100840static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800841};
842
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000843static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200844{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000845 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
846 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000847 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000848 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000849 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100850 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200851
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000852 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000853
Mark Brown87ba7da2012-03-14 21:18:39 +0000854 /* Find CPU DAI from registered DAIs*/
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000855 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700856 if (dai_link->cpu_dai_of_node) {
857 if (cpu_dai->dev->of_node != dai_link->cpu_dai_of_node)
858 continue;
859 } else {
860 if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
861 continue;
862 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700863
864 rtd->cpu_dai = cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000865 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000866
Mark Brown87ba7da2012-03-14 21:18:39 +0000867 if (!rtd->cpu_dai) {
868 dev_dbg(card->dev, "CPU DAI %s not registered\n",
869 dai_link->cpu_dai_name);
870 return -EPROBE_DEFER;
871
Mark Brownc5af3a22008-11-28 13:29:45 +0000872 }
873
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000874 /* no, then find CODEC from registered CODECs*/
875 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700876 if (dai_link->codec_of_node) {
877 if (codec->dev->of_node != dai_link->codec_of_node)
878 continue;
879 } else {
880 if (strcmp(codec->name, dai_link->codec_name))
881 continue;
882 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000883
Stephen Warren2610ab72011-12-07 13:58:27 -0700884 rtd->codec = codec;
885
886 /*
887 * CODEC found, so find CODEC DAI from registered DAIs from
888 * this CODEC
889 */
890 list_for_each_entry(codec_dai, &dai_list, list) {
891 if (codec->dev == codec_dai->dev &&
892 !strcmp(codec_dai->name,
893 dai_link->codec_dai_name)) {
894
895 rtd->codec_dai = codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000896 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000897 }
Mark Brown87ba7da2012-03-14 21:18:39 +0000898 if (!rtd->codec_dai) {
899 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
Stephen Warren2610ab72011-12-07 13:58:27 -0700900 dai_link->codec_dai_name);
Mark Brown87ba7da2012-03-14 21:18:39 +0000901 return -EPROBE_DEFER;
902 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700903
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000904 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000905
Mark Brown87ba7da2012-03-14 21:18:39 +0000906 if (!rtd->codec) {
907 dev_dbg(card->dev, "CODEC %s not registered\n",
908 dai_link->codec_name);
909 return -EPROBE_DEFER;
910 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100911
912 /* if there's no platform we match on the empty platform */
913 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700914 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100915 platform_name = "snd-soc-dummy";
916
917 /* no, then find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000918 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700919 if (dai_link->platform_of_node) {
920 if (platform->dev->of_node !=
921 dai_link->platform_of_node)
922 continue;
923 } else {
924 if (strcmp(platform->name, platform_name))
925 continue;
926 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700927
928 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000929 }
Mark Brown87ba7da2012-03-14 21:18:39 +0000930 if (!rtd->platform) {
931 dev_dbg(card->dev, "platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000932 dai_link->platform_name);
Mark Brown87ba7da2012-03-14 21:18:39 +0000933
934 return -EPROBE_DEFER;
935 }
936 card->num_rtd++;
937
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000938 return 0;
939
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000940}
941
Jarkko Nikula589c3562010-12-06 16:27:07 +0200942static void soc_remove_codec(struct snd_soc_codec *codec)
943{
944 int err;
945
946 if (codec->driver->remove) {
947 err = codec->driver->remove(codec);
948 if (err < 0)
949 dev_err(codec->dev,
950 "asoc: failed to remove %s: %d\n",
951 codec->name, err);
952 }
953
954 /* Make sure all DAPM widgets are freed */
955 snd_soc_dapm_free(&codec->dapm);
956
957 soc_cleanup_codec_debugfs(codec);
958 codec->probed = 0;
959 list_del(&codec->card_list);
960 module_put(codec->dev->driver->owner);
961}
962
Liam Girdwood0168bf02011-06-07 16:08:05 +0100963static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000964{
965 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
966 struct snd_soc_codec *codec = rtd->codec;
967 struct snd_soc_platform *platform = rtd->platform;
968 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
969 int err;
970
971 /* unregister the rtd device */
972 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800973 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
974 device_remove_file(rtd->dev, &dev_attr_codec_reg);
975 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000976 rtd->dev_registered = 0;
977 }
978
979 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100980 if (codec_dai && codec_dai->probed &&
981 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000982 if (codec_dai->driver->remove) {
983 err = codec_dai->driver->remove(codec_dai);
984 if (err < 0)
985 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
986 }
987 codec_dai->probed = 0;
988 list_del(&codec_dai->card_list);
989 }
990
991 /* remove the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100992 if (platform && platform->probed &&
993 platform->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000994 if (platform->driver->remove) {
995 err = platform->driver->remove(platform);
996 if (err < 0)
997 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
998 }
Liam Girdwood675c4962012-01-16 15:25:37 +0000999 /* Make sure all DAPM widgets are freed */
1000 snd_soc_dapm_free(&platform->dapm);
1001
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001002 soc_cleanup_platform_debugfs(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001003 platform->probed = 0;
1004 list_del(&platform->card_list);
1005 module_put(platform->dev->driver->owner);
1006 }
1007
1008 /* remove the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001009 if (codec && codec->probed &&
1010 codec->driver->remove_order == order)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001011 soc_remove_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001012
1013 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001014 if (cpu_dai && cpu_dai->probed &&
1015 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001016 if (cpu_dai->driver->remove) {
1017 err = cpu_dai->driver->remove(cpu_dai);
1018 if (err < 0)
1019 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1020 }
1021 cpu_dai->probed = 0;
1022 list_del(&cpu_dai->card_list);
1023 module_put(cpu_dai->dev->driver->owner);
1024 }
1025}
1026
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001027static void soc_remove_dai_links(struct snd_soc_card *card)
1028{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001029 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001030
Liam Girdwood0168bf02011-06-07 16:08:05 +01001031 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1032 order++) {
1033 for (dai = 0; dai < card->num_rtd; dai++)
1034 soc_remove_dai_link(card, dai, order);
1035 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001036 card->num_rtd = 0;
1037}
1038
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001039static void soc_set_name_prefix(struct snd_soc_card *card,
1040 struct snd_soc_codec *codec)
1041{
1042 int i;
1043
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001044 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001045 return;
1046
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001047 for (i = 0; i < card->num_configs; i++) {
1048 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001049 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1050 codec->name_prefix = map->name_prefix;
1051 break;
1052 }
1053 }
1054}
1055
Jarkko Nikula589c3562010-12-06 16:27:07 +02001056static int soc_probe_codec(struct snd_soc_card *card,
1057 struct snd_soc_codec *codec)
1058{
1059 int ret = 0;
Mark Brown89b95ac2011-03-07 16:38:44 +00001060 const struct snd_soc_codec_driver *driver = codec->driver;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001061
1062 codec->card = card;
1063 codec->dapm.card = card;
1064 soc_set_name_prefix(card, codec);
1065
Jarkko Nikula70d293312011-01-27 16:24:22 +02001066 if (!try_module_get(codec->dev->driver->owner))
1067 return -ENODEV;
1068
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001069 soc_init_codec_debugfs(codec);
1070
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001071 if (driver->dapm_widgets)
1072 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1073 driver->num_dapm_widgets);
1074
Mark Brown33c5f962011-08-22 18:40:30 +01001075 codec->dapm.idle_bias_off = driver->idle_bias_off;
1076
Mark Brown89b95ac2011-03-07 16:38:44 +00001077 if (driver->probe) {
1078 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001079 if (ret < 0) {
1080 dev_err(codec->dev,
1081 "asoc: failed to probe CODEC %s: %d\n",
1082 codec->name, ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001083 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001084 }
1085 }
1086
Mark Brownb7af1da2011-04-07 19:18:44 +09001087 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001088 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001089 driver->num_controls);
Mark Brown89b95ac2011-03-07 16:38:44 +00001090 if (driver->dapm_routes)
1091 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1092 driver->num_dapm_routes);
1093
Jarkko Nikula589c3562010-12-06 16:27:07 +02001094 /* mark codec as probed and add to card codec list */
1095 codec->probed = 1;
1096 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001097 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001098
Jarkko Nikula70d293312011-01-27 16:24:22 +02001099 return 0;
1100
1101err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001102 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001103 module_put(codec->dev->driver->owner);
1104
Jarkko Nikula589c3562010-12-06 16:27:07 +02001105 return ret;
1106}
1107
Liam Girdwood956245e2011-07-01 16:54:08 +01001108static int soc_probe_platform(struct snd_soc_card *card,
1109 struct snd_soc_platform *platform)
1110{
1111 int ret = 0;
1112 const struct snd_soc_platform_driver *driver = platform->driver;
1113
1114 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001115 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001116
1117 if (!try_module_get(platform->dev->driver->owner))
1118 return -ENODEV;
1119
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001120 soc_init_platform_debugfs(platform);
1121
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001122 if (driver->dapm_widgets)
1123 snd_soc_dapm_new_controls(&platform->dapm,
1124 driver->dapm_widgets, driver->num_dapm_widgets);
1125
Liam Girdwood956245e2011-07-01 16:54:08 +01001126 if (driver->probe) {
1127 ret = driver->probe(platform);
1128 if (ret < 0) {
1129 dev_err(platform->dev,
1130 "asoc: failed to probe platform %s: %d\n",
1131 platform->name, ret);
1132 goto err_probe;
1133 }
1134 }
1135
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001136 if (driver->controls)
1137 snd_soc_add_platform_controls(platform, driver->controls,
1138 driver->num_controls);
1139 if (driver->dapm_routes)
1140 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1141 driver->num_dapm_routes);
1142
Liam Girdwood956245e2011-07-01 16:54:08 +01001143 /* mark platform as probed and add to card platform list */
1144 platform->probed = 1;
1145 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001146 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001147
1148 return 0;
1149
1150err_probe:
1151 module_put(platform->dev->driver->owner);
1152
1153 return ret;
1154}
1155
Mark Brown36ae1a92012-01-06 17:12:45 -08001156static void rtd_release(struct device *dev)
1157{
1158 kfree(dev);
1159}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001160
Jarkko Nikula589c3562010-12-06 16:27:07 +02001161static int soc_post_component_init(struct snd_soc_card *card,
1162 struct snd_soc_codec *codec,
1163 int num, int dailess)
1164{
1165 struct snd_soc_dai_link *dai_link = NULL;
1166 struct snd_soc_aux_dev *aux_dev = NULL;
1167 struct snd_soc_pcm_runtime *rtd;
1168 const char *temp, *name;
1169 int ret = 0;
1170
1171 if (!dailess) {
1172 dai_link = &card->dai_link[num];
1173 rtd = &card->rtd[num];
1174 name = dai_link->name;
1175 } else {
1176 aux_dev = &card->aux_dev[num];
1177 rtd = &card->rtd_aux[num];
1178 name = aux_dev->name;
1179 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001180 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001181
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001182 /* Make sure all DAPM widgets are instantiated */
1183 snd_soc_dapm_new_widgets(&codec->dapm);
1184
Jarkko Nikula589c3562010-12-06 16:27:07 +02001185 /* machine controls, routes and widgets are not prefixed */
1186 temp = codec->name_prefix;
1187 codec->name_prefix = NULL;
1188
1189 /* do machine specific initialization */
1190 if (!dailess && dai_link->init)
1191 ret = dai_link->init(rtd);
1192 else if (dailess && aux_dev->init)
1193 ret = aux_dev->init(&codec->dapm);
1194 if (ret < 0) {
1195 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1196 return ret;
1197 }
1198 codec->name_prefix = temp;
1199
Jarkko Nikula589c3562010-12-06 16:27:07 +02001200 /* register the rtd device */
1201 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001202
1203 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1204 if (!rtd->dev)
1205 return -ENOMEM;
1206 device_initialize(rtd->dev);
1207 rtd->dev->parent = card->dev;
1208 rtd->dev->release = rtd_release;
1209 rtd->dev->init_name = name;
1210 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001211 mutex_init(&rtd->pcm_mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001212 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1213 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1214 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1215 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001216 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001217 if (ret < 0) {
1218 dev_err(card->dev,
1219 "asoc: failed to register runtime device: %d\n", ret);
1220 return ret;
1221 }
1222 rtd->dev_registered = 1;
1223
1224 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001225 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001226 if (ret < 0)
1227 dev_err(codec->dev,
1228 "asoc: failed to add codec dapm sysfs entries: %d\n",
1229 ret);
1230
1231 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001232 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001233 if (ret < 0)
1234 dev_err(codec->dev,
1235 "asoc: failed to add codec sysfs files: %d\n", ret);
1236
Liam Girdwood13e13a32011-01-31 21:30:52 +00001237#ifdef CONFIG_DEBUG_FS
1238 /* add DSP sysfs entries */
1239 if (!dai_link->dynamic)
1240 goto out;
1241
Steve Mucklef132c6c2012-06-06 18:30:57 -07001242 ret = soc_dpcm_debugfs_add(rtd);
Liam Girdwood13e13a32011-01-31 21:30:52 +00001243 if (ret < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001244 dev_err(rtd->dev, "asoc: failed to add dpcm sysfs entries: %d\n", ret);
Liam Girdwood13e13a32011-01-31 21:30:52 +00001245
1246out:
1247#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001248 return 0;
1249}
1250
Liam Girdwood0168bf02011-06-07 16:08:05 +01001251static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001252{
1253 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1254 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1255 struct snd_soc_codec *codec = rtd->codec;
1256 struct snd_soc_platform *platform = rtd->platform;
1257 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1258 int ret;
1259
Liam Girdwood0168bf02011-06-07 16:08:05 +01001260 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1261 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001262
1263 /* config components */
1264 codec_dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001265 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001266 codec_dai->card = card;
1267 cpu_dai->card = card;
1268
1269 /* set default power off timeout */
1270 rtd->pmdown_time = pmdown_time;
1271
1272 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001273 if (!cpu_dai->probed &&
1274 cpu_dai->driver->probe_order == order) {
Liam Girdwood61b61e32011-05-24 13:57:43 +01001275 if (!try_module_get(cpu_dai->dev->driver->owner))
1276 return -ENODEV;
1277
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001278 if (cpu_dai->driver->probe) {
1279 ret = cpu_dai->driver->probe(cpu_dai);
1280 if (ret < 0) {
1281 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1282 cpu_dai->name);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001283 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001284 return ret;
1285 }
1286 }
1287 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001288 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001289 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1290 }
1291
1292 /* probe the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001293 if (!codec->probed &&
1294 codec->driver->probe_order == order) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001295 ret = soc_probe_codec(card, codec);
1296 if (ret < 0)
1297 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001298 }
1299
1300 /* probe the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001301 if (!platform->probed &&
1302 platform->driver->probe_order == order) {
Liam Girdwood956245e2011-07-01 16:54:08 +01001303 ret = soc_probe_platform(card, platform);
1304 if (ret < 0)
1305 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001306 }
1307
1308 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001309 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001310 if (codec_dai->driver->probe) {
1311 ret = codec_dai->driver->probe(codec_dai);
1312 if (ret < 0) {
1313 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1314 codec_dai->name);
1315 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001316 }
1317 }
1318
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001319 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001320 codec_dai->probed = 1;
1321 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001322 }
1323
Liam Girdwood0168bf02011-06-07 16:08:05 +01001324 /* complete DAI probe during last probe */
1325 if (order != SND_SOC_COMP_ORDER_LAST)
1326 return 0;
1327
Jarkko Nikula589c3562010-12-06 16:27:07 +02001328 ret = soc_post_component_init(card, codec, num, 0);
1329 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001330 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001331
Mark Brown36ae1a92012-01-06 17:12:45 -08001332 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001333 if (ret < 0)
1334 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1335
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001336 /* create the pcm */
1337 ret = soc_new_pcm(rtd, num);
1338 if (ret < 0) {
1339 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1340 return ret;
1341 }
1342
1343 /* add platform data for AC97 devices */
1344 if (rtd->codec_dai->driver->ac97_control)
1345 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1346
1347 return 0;
1348}
1349
1350#ifdef CONFIG_SND_SOC_AC97_BUS
1351static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1352{
1353 int ret;
1354
1355 /* Only instantiate AC97 if not already done by the adaptor
1356 * for the generic AC97 subsystem.
1357 */
1358 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001359 /*
1360 * It is possible that the AC97 device is already registered to
1361 * the device subsystem. This happens when the device is created
1362 * via snd_ac97_mixer(). Currently only SoC codec that does so
1363 * is the generic AC97 glue but others migh emerge.
1364 *
1365 * In those cases we don't try to register the device again.
1366 */
1367 if (!rtd->codec->ac97_created)
1368 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001369
1370 ret = soc_ac97_dev_register(rtd->codec);
1371 if (ret < 0) {
1372 printk(KERN_ERR "asoc: AC97 device register failed\n");
1373 return ret;
1374 }
1375
1376 rtd->codec->ac97_registered = 1;
1377 }
1378 return 0;
1379}
1380
1381static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1382{
1383 if (codec->ac97_registered) {
1384 soc_ac97_dev_unregister(codec);
1385 codec->ac97_registered = 0;
1386 }
1387}
1388#endif
1389
Mark Brown87ba7da2012-03-14 21:18:39 +00001390static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1391{
1392 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1393 struct snd_soc_codec *codec;
1394
1395 /* find CODEC from registered CODECs*/
1396 list_for_each_entry(codec, &codec_list, list) {
1397 if (!strcmp(codec->name, aux_dev->codec_name))
1398 return 0;
1399 }
1400
1401 return -EPROBE_DEFER;
1402}
1403
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001404static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1405{
1406 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001407 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001408 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001409
1410 /* find CODEC from registered CODECs*/
1411 list_for_each_entry(codec, &codec_list, list) {
1412 if (!strcmp(codec->name, aux_dev->codec_name)) {
1413 if (codec->probed) {
1414 dev_err(codec->dev,
1415 "asoc: codec already probed");
1416 ret = -EBUSY;
1417 goto out;
1418 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001419 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001420 }
1421 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001422 /* codec not found */
1423 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
Mark Brown87ba7da2012-03-14 21:18:39 +00001424 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001425
Jarkko Nikula676ad982010-12-03 09:18:22 +02001426found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001427 ret = soc_probe_codec(card, codec);
1428 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001429 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001430
Jarkko Nikula589c3562010-12-06 16:27:07 +02001431 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001432
1433out:
1434 return ret;
1435}
1436
1437static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1438{
1439 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1440 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001441
1442 /* unregister the rtd device */
1443 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001444 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1445 device_del(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001446 rtd->dev_registered = 0;
1447 }
1448
Jarkko Nikula589c3562010-12-06 16:27:07 +02001449 if (codec && codec->probed)
1450 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001451}
1452
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001453static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1454 enum snd_soc_compress_type compress_type)
1455{
1456 int ret;
1457
1458 if (codec->cache_init)
1459 return 0;
1460
1461 /* override the compress_type if necessary */
1462 if (compress_type && codec->compress_type != compress_type)
1463 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001464 ret = snd_soc_cache_init(codec);
1465 if (ret < 0) {
1466 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1467 ret);
1468 return ret;
1469 }
1470 codec->cache_init = 1;
1471 return 0;
1472}
1473
Steve Mucklef132c6c2012-06-06 18:30:57 -07001474static void soc_init_dai_aif_channel_map(struct snd_soc_card *card,
1475 struct snd_soc_dai *dai, int stream)
1476{
1477 struct snd_soc_dapm_widget *w = NULL;
1478 const char *aif_name;
1479
1480 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1481 aif_name = dai->driver->playback.aif_name;
1482 else
1483 aif_name = dai->driver->capture.aif_name;
1484
1485 if (dai->codec) {
1486 struct snd_soc_codec *codec;
1487
1488 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
1489 w = snd_soc_get_codec_widget(card, codec, aif_name);
1490 if (w)
1491 break;
1492 }
1493 } else if (dai->platform) {
1494 struct snd_soc_platform *platform;
1495
1496 list_for_each_entry(platform, &card->platform_dev_list, card_list) {
1497 w = snd_soc_get_platform_widget(card, platform, aif_name);
1498 if (w)
1499 break;
1500 }
1501 }
1502
1503 if (w) {
1504 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1505 dai->playback_aif = w;
1506 else
1507 dai->capture_aif = w;
1508 } else
1509 dev_err(dai->dev, "unable to find %s DAI AIF %s\n",
1510 stream ? "capture" : "playback", aif_name);
1511
1512 dai->channel_map_instanciated = 1;
1513}
1514
1515static int soc_is_dai_pcm(struct snd_soc_card *card, struct snd_soc_dai *dai)
1516{
1517 int i;
1518
1519 for (i = 0; i < card->num_rtd; i++) {
1520 if (card->rtd[i].cpu_dai == dai && !card->rtd[i].dai_link->no_pcm)
1521 return 1;
1522 }
1523 return 0;
1524}
1525
1526static void soc_init_card_aif_channel_map(struct snd_soc_card *card)
1527{
1528 struct snd_soc_dai *dai;
1529
1530 list_for_each_entry(dai, &card->dai_dev_list, card_list) {
1531
1532 /* only process DAIs that use the new API until
1533 * the old "stream name" API is fully deprecated */
1534 if (!dai->driver->playback.aif_name && !dai->driver->capture.aif_name)
1535 continue;
1536
1537 /* channels are only mapped from PCM DAIs */
1538 if (!soc_is_dai_pcm(card, dai))
1539 continue;
1540
1541 /* skip if already instanciated */
1542 if (dai->channel_map_instanciated)
1543 continue;
1544
1545 /* create unique channels masks for each DAI in the sound card */
1546 dai->playback_channel_map =
1547 ((1 << dai->driver->playback.channels_max) - 1)
1548 << card->num_playback_channels;
1549 card->num_playback_channels += dai->driver->playback.channels_max;
1550
1551 dai->capture_channel_map =
1552 ((1 << dai->driver->capture.channels_max) - 1)
1553 << card->num_capture_channels;
1554 card->num_capture_channels += dai->driver->capture.channels_max;
1555
1556 if (dai->driver->playback.channels_max)
1557 soc_init_dai_aif_channel_map(card, dai, SNDRV_PCM_STREAM_PLAYBACK);
1558 if (dai->driver->capture.channels_max)
1559 soc_init_dai_aif_channel_map(card, dai, SNDRV_PCM_STREAM_CAPTURE);
1560 }
1561}
1562
1563
Mark Brown87ba7da2012-03-14 21:18:39 +00001564static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001565{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001566 struct snd_soc_codec *codec;
1567 struct snd_soc_codec_conf *codec_conf;
1568 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001569 struct snd_soc_dai_link *dai_link;
Liam Girdwood0168bf02011-06-07 16:08:05 +01001570 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001571
1572 mutex_lock(&card->mutex);
1573
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001574
1575 /* bind DAIs */
Mark Brown87ba7da2012-03-14 21:18:39 +00001576 for (i = 0; i < card->num_links; i++) {
1577 ret = soc_bind_dai_link(card, i);
1578 if (ret != 0)
1579 goto base_error;
1580 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001581
Mark Brown87ba7da2012-03-14 21:18:39 +00001582 /* check aux_devs too */
1583 for (i = 0; i < card->num_aux_devs; i++) {
1584 ret = soc_check_aux_dev(card, i);
1585 if (ret != 0)
1586 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001587 }
1588
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001589 /* initialize the register cache for each available codec */
1590 list_for_each_entry(codec, &codec_list, list) {
1591 if (codec->cache_init)
1592 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001593 /* by default we don't override the compress_type */
1594 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001595 /* check to see if we need to override the compress_type */
1596 for (i = 0; i < card->num_configs; ++i) {
1597 codec_conf = &card->codec_conf[i];
1598 if (!strcmp(codec->name, codec_conf->dev_name)) {
1599 compress_type = codec_conf->compress_type;
1600 if (compress_type && compress_type
1601 != codec->compress_type)
1602 break;
1603 }
1604 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001605 ret = snd_soc_init_codec_cache(codec, compress_type);
Mark Brown87ba7da2012-03-14 21:18:39 +00001606 if (ret < 0)
1607 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001608 }
1609
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001610 /* card bind complete so register a sound card */
1611 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1612 card->owner, 0, &card->snd_card);
1613 if (ret < 0) {
1614 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1615 card->name);
Mark Brown87ba7da2012-03-14 21:18:39 +00001616 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001617 }
1618 card->snd_card->dev = card->dev;
1619
Mark Browne37a4972011-03-02 18:21:57 +00001620 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1621 card->dapm.dev = card->dev;
1622 card->dapm.card = card;
1623 list_add(&card->dapm.list, &card->dapm_list);
1624
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001625#ifdef CONFIG_DEBUG_FS
1626 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1627#endif
1628
Mark Brown88ee1c62011-02-02 10:43:26 +00001629#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001630 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001631 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001632#endif
Andy Green6ed25972008-06-13 16:24:05 +01001633
Mark Brown9a841eb2011-04-12 17:51:37 -07001634 if (card->dapm_widgets)
1635 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1636 card->num_dapm_widgets);
1637
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001638 /* initialise the sound card only once */
1639 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001640 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001641 if (ret < 0)
1642 goto card_probe_error;
1643 }
1644
Liam Girdwood0168bf02011-06-07 16:08:05 +01001645 /* early DAI link probe */
1646 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1647 order++) {
1648 for (i = 0; i < card->num_links; i++) {
1649 ret = soc_probe_dai_link(card, i, order);
1650 if (ret < 0) {
1651 pr_err("asoc: failed to instantiate card %s: %d\n",
Mark Brown321de0d2010-09-21 15:09:37 +01001652 card->name, ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001653 goto probe_dai_err;
1654 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001655 }
1656 }
1657
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001658 for (i = 0; i < card->num_aux_devs; i++) {
1659 ret = soc_probe_aux_dev(card, i);
1660 if (ret < 0) {
1661 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1662 card->name, ret);
1663 goto probe_aux_dev_err;
1664 }
1665 }
1666
Mark Brownb7af1da2011-04-07 19:18:44 +09001667 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001668 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001669
Mark Brownb8ad29d2011-03-02 18:35:51 +00001670 if (card->dapm_routes)
1671 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1672 card->num_dapm_routes);
1673
Mark Brownb90d2f92011-10-10 13:38:06 +01001674 snd_soc_dapm_new_widgets(&card->dapm);
1675
Mark Brown75d9ac42011-09-27 16:41:01 +01001676 for (i = 0; i < card->num_links; i++) {
1677 dai_link = &card->dai_link[i];
1678
1679 if (dai_link->dai_fmt) {
1680 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1681 dai_link->dai_fmt);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001682 if (ret != 0)
Mark Brown75d9ac42011-09-27 16:41:01 +01001683 dev_warn(card->rtd[i].codec_dai->dev,
1684 "Failed to set DAI format: %d\n",
1685 ret);
1686
1687 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1688 dai_link->dai_fmt);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001689 if (ret != 0)
Mark Brown75d9ac42011-09-27 16:41:01 +01001690 dev_warn(card->rtd[i].cpu_dai->dev,
1691 "Failed to set DAI format: %d\n",
1692 ret);
1693 }
1694 }
1695
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001696 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001697 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001698 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1699 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001700 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1701 "%s", card->driver_name ? card->driver_name : card->name);
1702 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1703 switch (card->snd_card->driver[i]) {
1704 case '_':
1705 case '-':
1706 case '\0':
1707 break;
1708 default:
1709 if (!isalnum(card->snd_card->driver[i]))
1710 card->snd_card->driver[i] = '_';
1711 break;
1712 }
1713 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001714
Mark Brown28e9ad92011-03-02 18:36:34 +00001715 if (card->late_probe) {
1716 ret = card->late_probe(card);
1717 if (ret < 0) {
1718 dev_err(card->dev, "%s late_probe() failed: %d\n",
1719 card->name, ret);
1720 goto probe_aux_dev_err;
1721 }
1722 }
1723
Mark Brown2dc00212011-10-08 13:59:44 +01001724 snd_soc_dapm_new_widgets(&card->dapm);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001725 soc_init_card_aif_channel_map(card);
Mark Brown2dc00212011-10-08 13:59:44 +01001726
Stephen Warren16332812011-11-23 12:42:04 -07001727 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001728 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001729 snd_soc_dapm_auto_nc_codec_pins(codec);
1730
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001731 ret = snd_card_register(card->snd_card);
1732 if (ret < 0) {
1733 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
Axel Lin6b3ed782010-12-07 16:12:29 +08001734 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001735 }
1736
1737#ifdef CONFIG_SND_SOC_AC97_BUS
1738 /* register any AC97 codecs */
1739 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001740 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1741 if (ret < 0) {
1742 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1743 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001744 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001745 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001746 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001747 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001748#endif
1749
Mark Brown435c5e22008-12-04 15:32:53 +00001750 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001751 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001752 mutex_unlock(&card->mutex);
Mark Brown87ba7da2012-03-14 21:18:39 +00001753 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001754
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001755probe_aux_dev_err:
1756 for (i = 0; i < card->num_aux_devs; i++)
1757 soc_remove_aux_dev(card, i);
1758
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001759probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001760 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001761
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001762card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001763 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001764 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001765
1766 snd_card_free(card->snd_card);
Mark Brown87ba7da2012-03-14 21:18:39 +00001767base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001768 mutex_unlock(&card->mutex);
Mark Brown87ba7da2012-03-14 21:18:39 +00001769 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001770}
1771
1772/* probes a new socdev */
1773static int soc_probe(struct platform_device *pdev)
1774{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001775 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001776 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001777
Vinod Koul70a7ca32011-01-14 19:22:48 +05301778 /*
1779 * no card, so machine driver should be registering card
1780 * we should not be here in that case so ret error
1781 */
1782 if (!card)
1783 return -EINVAL;
1784
Mark Brown435c5e22008-12-04 15:32:53 +00001785 /* Bodge while we unpick instantiation */
1786 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001787
Mark Brown435c5e22008-12-04 15:32:53 +00001788 ret = snd_soc_register_card(card);
1789 if (ret != 0) {
1790 dev_err(&pdev->dev, "Failed to register card\n");
1791 return ret;
1792 }
1793
1794 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001795}
1796
Vinod Koulb0e26482011-01-13 22:48:02 +05301797static int soc_cleanup_card_resources(struct snd_soc_card *card)
1798{
Vinod Koulb0e26482011-01-13 22:48:02 +05301799 int i;
1800
1801 /* make sure any delayed work runs */
1802 for (i = 0; i < card->num_rtd; i++) {
1803 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1804 flush_delayed_work_sync(&rtd->delayed_work);
1805 }
1806
1807 /* remove auxiliary devices */
1808 for (i = 0; i < card->num_aux_devs; i++)
1809 soc_remove_aux_dev(card, i);
1810
1811 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001812 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301813
1814 soc_cleanup_card_debugfs(card);
1815
1816 /* remove the card */
1817 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001818 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301819
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001820 snd_soc_dapm_free(&card->dapm);
1821
Vinod Koulb0e26482011-01-13 22:48:02 +05301822 kfree(card->rtd);
1823 snd_card_free(card->snd_card);
1824 return 0;
1825
1826}
1827
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001828/* removes a socdev */
1829static int soc_remove(struct platform_device *pdev)
1830{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001831 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001832
Mark Brownc5af3a22008-11-28 13:29:45 +00001833 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001834 return 0;
1835}
1836
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001837int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001838{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001839 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001840 int i;
Mark Brown51737472009-06-22 13:16:51 +01001841
1842 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001843 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001844
1845 /* Flush out pmdown_time work - we actually do want to run it
1846 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001847 for (i = 0; i < card->num_rtd; i++) {
1848 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001849 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001850 }
Mark Brown51737472009-06-22 13:16:51 +01001851
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001852 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001853
1854 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001855}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001856EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001857
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001858const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301859 .suspend = snd_soc_suspend,
1860 .resume = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001861 .poweroff = snd_soc_poweroff,
Mark Brown416356f2009-06-30 19:05:15 +01001862};
Stephen Warrendeb26072011-04-05 19:35:30 -06001863EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001864
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001865/* ASoC platform driver */
1866static struct platform_driver soc_driver = {
1867 .driver = {
1868 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001869 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001870 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001871 },
1872 .probe = soc_probe,
1873 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001874};
1875
Mark Brown096e49d2009-07-05 15:12:22 +01001876/**
1877 * snd_soc_codec_volatile_register: Report if a register is volatile.
1878 *
1879 * @codec: CODEC to query.
1880 * @reg: Register to query.
1881 *
1882 * Boolean function indiciating if a CODEC register is volatile.
1883 */
Mark Brown181e0552011-01-24 14:05:25 +00001884int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1885 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001886{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001887 if (codec->volatile_register)
1888 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001889 else
1890 return 0;
1891}
1892EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1893
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001894/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001895 * snd_soc_codec_readable_register: Report if a register is readable.
1896 *
1897 * @codec: CODEC to query.
1898 * @reg: Register to query.
1899 *
1900 * Boolean function indicating if a CODEC register is readable.
1901 */
1902int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1903 unsigned int reg)
1904{
1905 if (codec->readable_register)
1906 return codec->readable_register(codec, reg);
1907 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001908 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001909}
1910EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1911
1912/**
1913 * snd_soc_codec_writable_register: Report if a register is writable.
1914 *
1915 * @codec: CODEC to query.
1916 * @reg: Register to query.
1917 *
1918 * Boolean function indicating if a CODEC register is writable.
1919 */
1920int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1921 unsigned int reg)
1922{
1923 if (codec->writable_register)
1924 return codec->writable_register(codec, reg);
1925 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001926 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001927}
1928EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1929
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001930int snd_soc_platform_read(struct snd_soc_platform *platform,
1931 unsigned int reg)
1932{
1933 unsigned int ret;
1934
1935 if (!platform->driver->read) {
1936 dev_err(platform->dev, "platform has no read back\n");
1937 return -1;
1938 }
1939
1940 ret = platform->driver->read(platform, reg);
1941 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001942 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001943
1944 return ret;
1945}
1946EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1947
1948int snd_soc_platform_write(struct snd_soc_platform *platform,
1949 unsigned int reg, unsigned int val)
1950{
1951 if (!platform->driver->write) {
1952 dev_err(platform->dev, "platform has no write back\n");
1953 return -1;
1954 }
1955
1956 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001957 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001958 return platform->driver->write(platform, reg, val);
1959}
1960EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1961
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001962/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001963 * snd_soc_new_ac97_codec - initailise AC97 device
1964 * @codec: audio codec
1965 * @ops: AC97 bus operations
1966 * @num: AC97 codec number
1967 *
1968 * Initialises AC97 codec resources for use by ad-hoc devices only.
1969 */
1970int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1971 struct snd_ac97_bus_ops *ops, int num)
1972{
1973 mutex_lock(&codec->mutex);
1974
1975 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1976 if (codec->ac97 == NULL) {
1977 mutex_unlock(&codec->mutex);
1978 return -ENOMEM;
1979 }
1980
1981 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1982 if (codec->ac97->bus == NULL) {
1983 kfree(codec->ac97);
1984 codec->ac97 = NULL;
1985 mutex_unlock(&codec->mutex);
1986 return -ENOMEM;
1987 }
1988
1989 codec->ac97->bus->ops = ops;
1990 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001991
1992 /*
1993 * Mark the AC97 device to be created by us. This way we ensure that the
1994 * device will be registered with the device subsystem later on.
1995 */
1996 codec->ac97_created = 1;
1997
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001998 mutex_unlock(&codec->mutex);
1999 return 0;
2000}
2001EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2002
2003/**
2004 * snd_soc_free_ac97_codec - free AC97 codec device
2005 * @codec: audio codec
2006 *
2007 * Frees AC97 codec device resources.
2008 */
2009void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2010{
2011 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002012#ifdef CONFIG_SND_SOC_AC97_BUS
2013 soc_unregister_ac97_dai_link(codec);
2014#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002015 kfree(codec->ac97->bus);
2016 kfree(codec->ac97);
2017 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002018 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002019 mutex_unlock(&codec->mutex);
2020}
2021EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2022
Mark Brownc3753702010-11-01 15:41:57 -04002023unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2024{
2025 unsigned int ret;
2026
Joonwoo Park6fb64392013-08-07 18:59:09 -07002027 if (unlikely(!snd_card_is_online_state(codec->card->snd_card))) {
2028 dev_err(codec->dev, "read 0x%02x while offline\n", reg);
2029 return -ENODEV;
2030 }
Mark Brownc3acec22010-12-02 16:15:29 +00002031 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002032 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002033 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002034
2035 return ret;
2036}
2037EXPORT_SYMBOL_GPL(snd_soc_read);
2038
2039unsigned int snd_soc_write(struct snd_soc_codec *codec,
2040 unsigned int reg, unsigned int val)
2041{
Joonwoo Park6fb64392013-08-07 18:59:09 -07002042 if (unlikely(!snd_card_is_online_state(codec->card->snd_card))) {
2043 dev_err(codec->dev, "write 0x%02x while offline\n", reg);
2044 return -ENODEV;
2045 }
Mark Brownc3753702010-11-01 15:41:57 -04002046 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002047 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002048 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002049}
2050EXPORT_SYMBOL_GPL(snd_soc_write);
2051
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00002052unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2053 unsigned int reg, const void *data, size_t len)
2054{
2055 return codec->bulk_write_raw(codec, reg, data, len);
2056}
2057EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2058
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002059/**
2060 * snd_soc_update_bits - update codec register bits
2061 * @codec: audio codec
2062 * @reg: codec register
2063 * @mask: register mask
2064 * @value: new value
2065 *
2066 * Writes new register value.
2067 *
Timur Tabi180c3292011-01-10 15:58:13 -06002068 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002069 */
2070int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002071 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002072{
Mark Brown8a713da2011-12-03 12:33:55 +00002073 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002074 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002075 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002076
Mark Brown8a713da2011-12-03 12:33:55 +00002077 if (codec->using_regmap) {
2078 ret = regmap_update_bits_check(codec->control_data, reg,
2079 mask, value, &change);
2080 } else {
2081 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06002082 if (ret < 0)
2083 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00002084
2085 old = ret;
2086 new = (old & ~mask) | (value & mask);
2087 change = old != new;
2088 if (change)
2089 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06002090 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002091
Mark Brown8a713da2011-12-03 12:33:55 +00002092 if (ret < 0)
2093 return ret;
2094
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002095 return change;
2096}
2097EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2098
2099/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002100 * snd_soc_update_bits_locked - update codec register bits
2101 * @codec: audio codec
2102 * @reg: codec register
2103 * @mask: register mask
2104 * @value: new value
2105 *
2106 * Writes new register value, and takes the codec mutex.
2107 *
2108 * Returns 1 for change else 0.
2109 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002110int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2111 unsigned short reg, unsigned int mask,
2112 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002113{
2114 int change;
2115
2116 mutex_lock(&codec->mutex);
2117 change = snd_soc_update_bits(codec, reg, mask, value);
2118 mutex_unlock(&codec->mutex);
2119
2120 return change;
2121}
Mark Browndd1b3d52009-12-04 14:22:03 +00002122EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002123
2124/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002125 * snd_soc_test_bits - test register for change
2126 * @codec: audio codec
2127 * @reg: codec register
2128 * @mask: register mask
2129 * @value: new value
2130 *
2131 * Tests a register with a new value and checks if the new value is
2132 * different from the old value.
2133 *
2134 * Returns 1 for change else 0.
2135 */
2136int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002137 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002138{
2139 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002140 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002141
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002142 old = snd_soc_read(codec, reg);
2143 new = (old & ~mask) | value;
2144 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002145
2146 return change;
2147}
2148EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2149
2150/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002151 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2152 * @substream: the pcm substream
2153 * @hw: the hardware parameters
2154 *
2155 * Sets the substream runtime hardware parameters.
2156 */
2157int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2158 const struct snd_pcm_hardware *hw)
2159{
2160 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwood13e13a32011-01-31 21:30:52 +00002161 if (!runtime)
2162 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002163 runtime->hw.info = hw->info;
2164 runtime->hw.formats = hw->formats;
2165 runtime->hw.period_bytes_min = hw->period_bytes_min;
2166 runtime->hw.period_bytes_max = hw->period_bytes_max;
2167 runtime->hw.periods_min = hw->periods_min;
2168 runtime->hw.periods_max = hw->periods_max;
2169 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2170 runtime->hw.fifo_size = hw->fifo_size;
2171 return 0;
2172}
2173EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2174
2175/**
2176 * snd_soc_cnew - create new control
2177 * @_template: control template
2178 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002179 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002180 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002181 *
2182 * Create a new mixer control from a template control.
2183 *
2184 * Returns 0 for success, else error.
2185 */
2186struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brownefb7ac32011-03-08 17:23:24 +00002187 void *data, char *long_name,
2188 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002189{
2190 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002191 struct snd_kcontrol *kcontrol;
2192 char *name = NULL;
2193 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002194
2195 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002196 template.index = 0;
2197
Mark Brownefb7ac32011-03-08 17:23:24 +00002198 if (!long_name)
2199 long_name = template.name;
2200
2201 if (prefix) {
2202 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08002203 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00002204 if (!name)
2205 return NULL;
2206
2207 snprintf(name, name_len, "%s %s", prefix, long_name);
2208
2209 template.name = name;
2210 } else {
2211 template.name = long_name;
2212 }
2213
2214 kcontrol = snd_ctl_new1(&template, data);
2215
2216 kfree(name);
2217
2218 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002219}
2220EXPORT_SYMBOL_GPL(snd_soc_cnew);
2221
Liam Girdwood022658b2012-02-03 17:43:09 +00002222static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2223 const struct snd_kcontrol_new *controls, int num_controls,
2224 const char *prefix, void *data)
2225{
2226 int err, i;
2227
2228 for (i = 0; i < num_controls; i++) {
2229 const struct snd_kcontrol_new *control = &controls[i];
2230 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2231 control->name, prefix));
2232 if (err < 0) {
2233 dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2234 return err;
2235 }
2236 }
2237
2238 return 0;
2239}
2240
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002241/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002242 * snd_soc_add_codec_controls - add an array of controls to a codec.
2243 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002244 * duplicating this code.
2245 *
2246 * @codec: codec to add controls to
2247 * @controls: array of controls to add
2248 * @num_controls: number of elements in the array
2249 *
2250 * Return 0 for success, else error.
2251 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002252int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002253 const struct snd_kcontrol_new *controls, int num_controls)
2254{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002255 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002256
Liam Girdwood022658b2012-02-03 17:43:09 +00002257 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2258 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002259}
Liam Girdwood022658b2012-02-03 17:43:09 +00002260EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002261
2262/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002263 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002264 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002265 *
2266 * @platform: platform to add controls to
2267 * @controls: array of controls to add
2268 * @num_controls: number of elements in the array
2269 *
2270 * Return 0 for success, else error.
2271 */
2272int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2273 const struct snd_kcontrol_new *controls, int num_controls)
2274{
2275 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002276
Liam Girdwood022658b2012-02-03 17:43:09 +00002277 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2278 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002279}
2280EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2281
2282/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002283 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2284 * Convenience function to add a list of controls.
2285 *
2286 * @soc_card: SoC card to add controls to
2287 * @controls: array of controls to add
2288 * @num_controls: number of elements in the array
2289 *
2290 * Return 0 for success, else error.
2291 */
2292int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2293 const struct snd_kcontrol_new *controls, int num_controls)
2294{
2295 struct snd_card *card = soc_card->snd_card;
2296
2297 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2298 NULL, soc_card);
2299}
2300EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2301
2302/**
2303 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2304 * Convienience function to add a list of controls.
2305 *
2306 * @dai: DAI to add controls to
2307 * @controls: array of controls to add
2308 * @num_controls: number of elements in the array
2309 *
2310 * Return 0 for success, else error.
2311 */
2312int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2313 const struct snd_kcontrol_new *controls, int num_controls)
2314{
2315 struct snd_card *card = dai->card->snd_card;
2316
2317 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2318 NULL, dai);
2319}
2320EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2321
2322/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002323 * snd_soc_info_enum_double - enumerated double mixer info callback
2324 * @kcontrol: mixer control
2325 * @uinfo: control element information
2326 *
2327 * Callback to provide information about a double enumerated
2328 * mixer control.
2329 *
2330 * Returns 0 for success.
2331 */
2332int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2333 struct snd_ctl_elem_info *uinfo)
2334{
2335 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2336
2337 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2338 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002339 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002340
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002341 if (uinfo->value.enumerated.item > e->max - 1)
2342 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002343 strcpy(uinfo->value.enumerated.name,
Liam Girdwooda8f13d82011-05-18 18:39:21 +01002344 snd_soc_get_enum_text(e, uinfo->value.enumerated.item));
2345
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002346 return 0;
2347}
2348EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2349
2350/**
2351 * snd_soc_get_enum_double - enumerated double mixer get callback
2352 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002353 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002354 *
2355 * Callback to get the value of a double enumerated mixer.
2356 *
2357 * Returns 0 for success.
2358 */
2359int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2360 struct snd_ctl_elem_value *ucontrol)
2361{
2362 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2363 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002364 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002365
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002366 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002367 ;
2368 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002369 ucontrol->value.enumerated.item[0]
2370 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002371 if (e->shift_l != e->shift_r)
2372 ucontrol->value.enumerated.item[1] =
2373 (val >> e->shift_r) & (bitmask - 1);
2374
2375 return 0;
2376}
2377EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2378
2379/**
2380 * snd_soc_put_enum_double - enumerated double mixer put callback
2381 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002382 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002383 *
2384 * Callback to set the value of a double enumerated mixer.
2385 *
2386 * Returns 0 for success.
2387 */
2388int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2389 struct snd_ctl_elem_value *ucontrol)
2390{
2391 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2392 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002393 unsigned int val;
2394 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002395
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002396 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002397 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002398 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002399 return -EINVAL;
2400 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2401 mask = (bitmask - 1) << e->shift_l;
2402 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002403 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002404 return -EINVAL;
2405 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2406 mask |= (bitmask - 1) << e->shift_r;
2407 }
2408
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002409 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002410}
2411EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2412
2413/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002414 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2415 * @kcontrol: mixer control
2416 * @ucontrol: control element information
2417 *
2418 * Callback to get the value of a double semi enumerated mixer.
2419 *
2420 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2421 * used for handling bitfield coded enumeration for example.
2422 *
2423 * Returns 0 for success.
2424 */
2425int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2426 struct snd_ctl_elem_value *ucontrol)
2427{
2428 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002429 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002430 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002431
2432 reg_val = snd_soc_read(codec, e->reg);
2433 val = (reg_val >> e->shift_l) & e->mask;
2434 for (mux = 0; mux < e->max; mux++) {
2435 if (val == e->values[mux])
2436 break;
2437 }
2438 ucontrol->value.enumerated.item[0] = mux;
2439 if (e->shift_l != e->shift_r) {
2440 val = (reg_val >> e->shift_r) & e->mask;
2441 for (mux = 0; mux < e->max; mux++) {
2442 if (val == e->values[mux])
2443 break;
2444 }
2445 ucontrol->value.enumerated.item[1] = mux;
2446 }
2447
2448 return 0;
2449}
2450EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2451
2452/**
2453 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2454 * @kcontrol: mixer control
2455 * @ucontrol: control element information
2456 *
2457 * Callback to set the value of a double semi enumerated mixer.
2458 *
2459 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2460 * used for handling bitfield coded enumeration for example.
2461 *
2462 * Returns 0 for success.
2463 */
2464int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2465 struct snd_ctl_elem_value *ucontrol)
2466{
2467 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002468 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002469 unsigned int val;
2470 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002471
2472 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2473 return -EINVAL;
2474 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2475 mask = e->mask << e->shift_l;
2476 if (e->shift_l != e->shift_r) {
2477 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2478 return -EINVAL;
2479 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2480 mask |= e->mask << e->shift_r;
2481 }
2482
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002483 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002484}
2485EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2486
2487/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002488 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2489 * @kcontrol: mixer control
2490 * @uinfo: control element information
2491 *
2492 * Callback to provide information about an external enumerated
2493 * single mixer.
2494 *
2495 * Returns 0 for success.
2496 */
2497int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2498 struct snd_ctl_elem_info *uinfo)
2499{
2500 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2501
2502 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2503 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002504 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002505
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002506 if (uinfo->value.enumerated.item > e->max - 1)
2507 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002508 strcpy(uinfo->value.enumerated.name,
Liam Girdwooda8f13d82011-05-18 18:39:21 +01002509 snd_soc_get_enum_text(e, uinfo->value.enumerated.item));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002510 return 0;
2511}
2512EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2513
2514/**
2515 * snd_soc_info_volsw_ext - external single mixer info callback
2516 * @kcontrol: mixer control
2517 * @uinfo: control element information
2518 *
2519 * Callback to provide information about a single external mixer control.
2520 *
2521 * Returns 0 for success.
2522 */
2523int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2524 struct snd_ctl_elem_info *uinfo)
2525{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002526 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002527
Mark Brownfd5dfad2009-04-15 21:37:46 +01002528 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002529 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2530 else
2531 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2532
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002533 uinfo->count = 1;
2534 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002535 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002536 return 0;
2537}
2538EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2539
2540/**
Ben Romberger8f1dc4c2011-09-22 13:51:35 -07002541 * snd_soc_info_multi_ext - external single mixer info callback
2542 * @kcontrol: mixer control
2543 * @uinfo: control element information
2544 *
2545 * Callback to provide information about a single external mixer control.
2546 * that accepts multiple input.
2547 *
2548 * Returns 0 for success.
2549 */
2550int snd_soc_info_multi_ext(struct snd_kcontrol *kcontrol,
2551 struct snd_ctl_elem_info *uinfo)
2552{
2553 struct soc_multi_mixer_control *mc =
2554 (struct soc_multi_mixer_control *)kcontrol->private_value;
2555 int platform_max;
2556
2557 if (!mc->platform_max)
2558 mc->platform_max = mc->max;
2559 platform_max = mc->platform_max;
2560
2561 if (platform_max == 1 && !strnstr(kcontrol->id.name, " Volume", 30))
2562 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2563 else
2564 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2565
2566 uinfo->count = mc->count;
2567 uinfo->value.integer.min = 0;
2568 uinfo->value.integer.max = platform_max;
2569 return 0;
2570}
2571EXPORT_SYMBOL_GPL(snd_soc_info_multi_ext);
2572
2573/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002574 * snd_soc_info_volsw - single mixer info callback
2575 * @kcontrol: mixer control
2576 * @uinfo: control element information
2577 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002578 * Callback to provide information about a single mixer control, or a double
2579 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002580 *
2581 * Returns 0 for success.
2582 */
2583int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2584 struct snd_ctl_elem_info *uinfo)
2585{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002586 struct soc_mixer_control *mc =
2587 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002588 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002589
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002590 if (!mc->platform_max)
2591 mc->platform_max = mc->max;
2592 platform_max = mc->platform_max;
2593
2594 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002595 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2596 else
2597 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2598
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002599 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002600 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002601 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002602 return 0;
2603}
2604EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2605
2606/**
2607 * snd_soc_get_volsw - single mixer get callback
2608 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002609 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002610 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002611 * Callback to get the value of a single mixer control, or a double mixer
2612 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002613 *
2614 * Returns 0 for success.
2615 */
2616int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2617 struct snd_ctl_elem_value *ucontrol)
2618{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002619 struct soc_mixer_control *mc =
2620 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002621 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002622 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002623 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf82008-07-29 10:22:24 -04002624 unsigned int shift = mc->shift;
2625 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002626 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002627 unsigned int mask = (1 << fls(max)) - 1;
2628 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002629
2630 ucontrol->value.integer.value[0] =
2631 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002632 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002633 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002634 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002635
2636 if (snd_soc_volsw_is_stereo(mc)) {
2637 if (reg == reg2)
2638 ucontrol->value.integer.value[1] =
2639 (snd_soc_read(codec, reg) >> rshift) & mask;
2640 else
2641 ucontrol->value.integer.value[1] =
2642 (snd_soc_read(codec, reg2) >> shift) & mask;
2643 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002644 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002645 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002646 }
2647
2648 return 0;
2649}
2650EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2651
2652/**
2653 * snd_soc_put_volsw - single mixer put callback
2654 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002655 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002656 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002657 * Callback to set the value of a single mixer control, or a double mixer
2658 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002659 *
2660 * Returns 0 for success.
2661 */
2662int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2663 struct snd_ctl_elem_value *ucontrol)
2664{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002665 struct soc_mixer_control *mc =
2666 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002667 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002668 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002669 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf82008-07-29 10:22:24 -04002670 unsigned int shift = mc->shift;
2671 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002672 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002673 unsigned int mask = (1 << fls(max)) - 1;
2674 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002675 int err;
2676 bool type_2r = 0;
2677 unsigned int val2 = 0;
2678 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002679
2680 val = (ucontrol->value.integer.value[0] & mask);
2681 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002682 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002683 val_mask = mask << shift;
2684 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002685 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002686 val2 = (ucontrol->value.integer.value[1] & mask);
2687 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002688 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002689 if (reg == reg2) {
2690 val_mask |= mask << rshift;
2691 val |= val2 << rshift;
2692 } else {
2693 val2 = val2 << shift;
2694 type_2r = 1;
2695 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002696 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002697 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002698 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002699 return err;
2700
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002701 if (type_2r)
2702 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2703
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002704 return err;
2705}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002706EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002707
Mark Browne13ac2e2008-05-28 17:58:05 +01002708/**
2709 * snd_soc_info_volsw_s8 - signed mixer info callback
2710 * @kcontrol: mixer control
2711 * @uinfo: control element information
2712 *
2713 * Callback to provide information about a signed mixer control.
2714 *
2715 * Returns 0 for success.
2716 */
2717int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2718 struct snd_ctl_elem_info *uinfo)
2719{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002720 struct soc_mixer_control *mc =
2721 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002722 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002723 int min = mc->min;
Bradley Rubin572c3b42011-07-07 18:23:44 -07002724 unsigned int shift = mc->shift;
2725 unsigned int rshift = mc->rshift;
Mark Browne13ac2e2008-05-28 17:58:05 +01002726
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002727 if (!mc->platform_max)
2728 mc->platform_max = mc->max;
2729 platform_max = mc->platform_max;
2730
Mark Browne13ac2e2008-05-28 17:58:05 +01002731 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Bradley Rubin572c3b42011-07-07 18:23:44 -07002732 uinfo->count = shift == rshift ? 1 : 2;
Mark Browne13ac2e2008-05-28 17:58:05 +01002733 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002734 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002735 return 0;
2736}
2737EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2738
2739/**
2740 * snd_soc_get_volsw_s8 - signed mixer get callback
2741 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002742 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002743 *
2744 * Callback to get the value of a signed mixer control.
2745 *
2746 * Returns 0 for success.
2747 */
2748int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2749 struct snd_ctl_elem_value *ucontrol)
2750{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002751 struct soc_mixer_control *mc =
2752 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002753 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002754 unsigned int reg = mc->reg;
Bradley Rubin572c3b42011-07-07 18:23:44 -07002755 unsigned int shift = mc->shift;
2756 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002757 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002758 int val = snd_soc_read(codec, reg);
2759
2760 ucontrol->value.integer.value[0] =
Bradley Rubin572c3b42011-07-07 18:23:44 -07002761 ((signed char)((val >> shift) & 0xff))-min;
2762 if (shift != rshift)
2763 ucontrol->value.integer.value[1] =
2764 ((signed char)((val >> rshift) & 0xff))-min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002765 return 0;
2766}
2767EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2768
2769/**
2770 * snd_soc_put_volsw_sgn - signed mixer put callback
2771 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002772 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002773 *
2774 * Callback to set the value of a signed mixer control.
2775 *
2776 * Returns 0 for success.
2777 */
2778int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2779 struct snd_ctl_elem_value *ucontrol)
2780{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002781 struct soc_mixer_control *mc =
2782 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002783 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002784 unsigned int reg = mc->reg;
Bradley Rubin572c3b42011-07-07 18:23:44 -07002785 unsigned int shift = mc->shift;
2786 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002787 int min = mc->min;
Bradley Rubin572c3b42011-07-07 18:23:44 -07002788 unsigned int val, val2, val_mask;
Mark Browne13ac2e2008-05-28 17:58:05 +01002789
Bradley Rubin572c3b42011-07-07 18:23:44 -07002790 val = ((ucontrol->value.integer.value[0]+min) & 0xff) << shift;
2791 val_mask = 0xff << shift;
2792 if (shift != rshift) {
2793 val2 = (ucontrol->value.integer.value[1]+min) & 0xff;
2794 val |= val2 << rshift;
2795 val_mask |= 0xff << rshift;
2796 }
Mark Browne13ac2e2008-05-28 17:58:05 +01002797
Bradley Rubin572c3b42011-07-07 18:23:44 -07002798 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002799}
2800EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2801
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002802/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002803 * snd_soc_limit_volume - Set new limit to an existing volume control.
2804 *
2805 * @codec: where to look for the control
2806 * @name: Name of the control
2807 * @max: new maximum limit
2808 *
2809 * Return 0 for success, else error.
2810 */
2811int snd_soc_limit_volume(struct snd_soc_codec *codec,
2812 const char *name, int max)
2813{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002814 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002815 struct snd_kcontrol *kctl;
2816 struct soc_mixer_control *mc;
2817 int found = 0;
2818 int ret = -EINVAL;
2819
2820 /* Sanity check for name and max */
2821 if (unlikely(!name || max <= 0))
2822 return -EINVAL;
2823
2824 list_for_each_entry(kctl, &card->controls, list) {
2825 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2826 found = 1;
2827 break;
2828 }
2829 }
2830 if (found) {
2831 mc = (struct soc_mixer_control *)kctl->private_value;
2832 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002833 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002834 ret = 0;
2835 }
2836 }
2837 return ret;
2838}
2839EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2840
2841/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002842 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2843 * mixer info callback
2844 * @kcontrol: mixer control
2845 * @uinfo: control element information
2846 *
2847 * Returns 0 for success.
2848 */
2849int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2850 struct snd_ctl_elem_info *uinfo)
2851{
2852 struct soc_mixer_control *mc =
2853 (struct soc_mixer_control *)kcontrol->private_value;
2854 int max = mc->max;
2855 int min = mc->min;
2856
2857 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2858 uinfo->count = 2;
2859 uinfo->value.integer.min = 0;
2860 uinfo->value.integer.max = max-min;
2861
2862 return 0;
2863}
2864EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2865
2866/**
2867 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2868 * mixer get callback
2869 * @kcontrol: mixer control
2870 * @uinfo: control element information
2871 *
2872 * Returns 0 for success.
2873 */
2874int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2875 struct snd_ctl_elem_value *ucontrol)
2876{
2877 struct soc_mixer_control *mc =
2878 (struct soc_mixer_control *)kcontrol->private_value;
2879 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2880 unsigned int mask = (1<<mc->shift)-1;
2881 int min = mc->min;
2882 int val = snd_soc_read(codec, mc->reg) & mask;
2883 int valr = snd_soc_read(codec, mc->rreg) & mask;
2884
Stuart Longland20630c72010-06-18 12:56:10 +10002885 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2886 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002887 return 0;
2888}
2889EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2890
2891/**
2892 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2893 * mixer put callback
2894 * @kcontrol: mixer control
2895 * @uinfo: control element information
2896 *
2897 * Returns 0 for success.
2898 */
2899int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2900 struct snd_ctl_elem_value *ucontrol)
2901{
2902 struct soc_mixer_control *mc =
2903 (struct soc_mixer_control *)kcontrol->private_value;
2904 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2905 unsigned int mask = (1<<mc->shift)-1;
2906 int min = mc->min;
2907 int ret;
2908 unsigned int val, valr, oval, ovalr;
2909
2910 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2911 val &= mask;
2912 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2913 valr &= mask;
2914
2915 oval = snd_soc_read(codec, mc->reg) & mask;
2916 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2917
2918 ret = 0;
2919 if (oval != val) {
2920 ret = snd_soc_write(codec, mc->reg, val);
2921 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002922 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002923 }
2924 if (ovalr != valr) {
2925 ret = snd_soc_write(codec, mc->rreg, valr);
2926 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002927 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002928 }
2929
2930 return 0;
2931}
2932EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2933
2934/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002935 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2936 * @dai: DAI
2937 * @clk_id: DAI specific clock ID
2938 * @freq: new clock frequency in Hz
2939 * @dir: new clock direction - input/output.
2940 *
2941 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2942 */
2943int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2944 unsigned int freq, int dir)
2945{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002946 if (dai->driver && dai->driver->ops->set_sysclk)
2947 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002948 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002949 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00002950 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002951 else
2952 return -EINVAL;
2953}
2954EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2955
2956/**
Mark Brownec4ee522011-03-07 20:58:11 +00002957 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2958 * @codec: CODEC
2959 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01002960 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00002961 * @freq: new clock frequency in Hz
2962 * @dir: new clock direction - input/output.
2963 *
2964 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2965 */
2966int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01002967 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00002968{
2969 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002970 return codec->driver->set_sysclk(codec, clk_id, source,
2971 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002972 else
2973 return -EINVAL;
2974}
2975EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2976
2977/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002978 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2979 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002980 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002981 * @div: new clock divisor.
2982 *
2983 * Configures the clock dividers. This is used to derive the best DAI bit and
2984 * frame clocks from the system or master clock. It's best to set the DAI bit
2985 * and frame clocks as low as possible to save system power.
2986 */
2987int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2988 int div_id, int div)
2989{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002990 if (dai->driver && dai->driver->ops->set_clkdiv)
2991 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002992 else
2993 return -EINVAL;
2994}
2995EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2996
2997/**
2998 * snd_soc_dai_set_pll - configure DAI PLL.
2999 * @dai: DAI
3000 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003001 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003002 * @freq_in: PLL input clock frequency in Hz
3003 * @freq_out: requested PLL output clock frequency in Hz
3004 *
3005 * Configures and enables PLL to generate output clock based on input clock.
3006 */
Mark Brown85488032009-09-05 18:52:16 +01003007int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3008 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003009{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003010 if (dai->driver && dai->driver->ops->set_pll)
3011 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003012 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003013 else if (dai->codec && dai->codec->driver->set_pll)
3014 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3015 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003016 else
3017 return -EINVAL;
3018}
3019EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3020
Mark Brownec4ee522011-03-07 20:58:11 +00003021/*
3022 * snd_soc_codec_set_pll - configure codec PLL.
3023 * @codec: CODEC
3024 * @pll_id: DAI specific PLL ID
3025 * @source: DAI specific source for the PLL
3026 * @freq_in: PLL input clock frequency in Hz
3027 * @freq_out: requested PLL output clock frequency in Hz
3028 *
3029 * Configures and enables PLL to generate output clock based on input clock.
3030 */
3031int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3032 unsigned int freq_in, unsigned int freq_out)
3033{
3034 if (codec->driver->set_pll)
3035 return codec->driver->set_pll(codec, pll_id, source,
3036 freq_in, freq_out);
3037 else
3038 return -EINVAL;
3039}
3040EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3041
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003042/**
3043 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3044 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003045 * @fmt: SND_SOC_DAIFMT_ format value.
3046 *
3047 * Configures the DAI hardware format and clocking.
3048 */
3049int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3050{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003051 if (dai->driver && dai->driver->ops->set_fmt)
3052 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003053 else
3054 return -EINVAL;
3055}
3056EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3057
3058/**
3059 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3060 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003061 * @tx_mask: bitmask representing active TX slots.
3062 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003063 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003064 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003065 *
3066 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3067 * specific.
3068 */
3069int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003070 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003071{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003072 if (dai->driver && dai->driver->ops->set_tdm_slot)
3073 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003074 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003075 else
3076 return -EINVAL;
3077}
3078EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3079
3080/**
Barry Song472df3c2009-09-12 01:16:29 +08003081 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3082 * @dai: DAI
3083 * @tx_num: how many TX channels
3084 * @tx_slot: pointer to an array which imply the TX slot number channel
3085 * 0~num-1 uses
3086 * @rx_num: how many RX channels
3087 * @rx_slot: pointer to an array which imply the RX slot number channel
3088 * 0~num-1 uses
3089 *
3090 * configure the relationship between channel number and TDM slot number.
3091 */
3092int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3093 unsigned int tx_num, unsigned int *tx_slot,
3094 unsigned int rx_num, unsigned int *rx_slot)
3095{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003096 if (dai->driver && dai->driver->ops->set_channel_map)
3097 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003098 rx_num, rx_slot);
3099 else
3100 return -EINVAL;
3101}
3102EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3103
3104/**
Bharath Ramachandramurthy9c79f132011-11-28 11:18:57 -08003105 * snd_soc_dai_get_channel_map - configure DAI audio channel map
3106 * @dai: DAI
3107 * @tx_num: how many TX channels
3108 * @tx_slot: pointer to an array which imply the TX slot number channel
3109 * 0~num-1 uses
3110 * @rx_num: how many RX channels
3111 * @rx_slot: pointer to an array which imply the RX slot number channel
3112 * 0~num-1 uses
3113 *
3114 * configure the relationship between channel number and TDM slot number.
3115 */
3116int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
3117 unsigned int *tx_num, unsigned int *tx_slot,
3118 unsigned int *rx_num, unsigned int *rx_slot)
3119{
3120 if (dai->driver && dai->driver->ops->get_channel_map)
3121 return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
3122 rx_num, rx_slot);
3123 else
3124 return -EINVAL;
3125}
3126EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
3127/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003128 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3129 * @dai: DAI
3130 * @tristate: tristate enable
3131 *
3132 * Tristates the DAI so that others can use it.
3133 */
3134int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3135{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003136 if (dai->driver && dai->driver->ops->set_tristate)
3137 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003138 else
3139 return -EINVAL;
3140}
3141EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3142
3143/**
3144 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3145 * @dai: DAI
3146 * @mute: mute enable
3147 *
3148 * Mutes the DAI DAC.
3149 */
3150int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3151{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003152 if (dai->driver && dai->driver->ops->digital_mute)
3153 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003154 else
3155 return -EINVAL;
3156}
3157EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3158
Mark Brownc5af3a22008-11-28 13:29:45 +00003159/**
3160 * snd_soc_register_card - Register a card with the ASoC core
3161 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003162 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003163 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003164 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303165int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003166{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003167 int i;
Liam Girdwood13e13a32011-01-31 21:30:52 +00003168 int ret = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003169
Mark Brownc5af3a22008-11-28 13:29:45 +00003170 if (!card->name || !card->dev)
3171 return -EINVAL;
3172
Stephen Warren5a504962011-12-21 10:40:59 -07003173 for (i = 0; i < card->num_links; i++) {
3174 struct snd_soc_dai_link *link = &card->dai_link[i];
3175
3176 /*
3177 * Codec must be specified by 1 of name or OF node,
3178 * not both or neither.
3179 */
3180 if (!!link->codec_name == !!link->codec_of_node) {
3181 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003182 "Neither/both codec name/of_node are set for %s\n",
3183 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003184 return -EINVAL;
3185 }
3186
3187 /*
3188 * Platform may be specified by either name or OF node, but
3189 * can be left unspecified, and a dummy platform will be used.
3190 */
3191 if (link->platform_name && link->platform_of_node) {
3192 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003193 "Both platform name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003194 return -EINVAL;
3195 }
3196
3197 /*
3198 * CPU DAI must be specified by 1 of name or OF node,
3199 * not both or neither.
3200 */
3201 if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
3202 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003203 "Neither/both cpu_dai name/of_node are set for %s\n",
3204 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003205 return -EINVAL;
3206 }
3207 }
3208
Mark Browned77cc12011-05-03 18:25:34 +01003209 dev_set_drvdata(card->dev, card);
3210
Stephen Warren111c6412011-01-28 14:26:35 -07003211 snd_soc_initialize_card_lists(card);
3212
Vinod Koul150dd2f2011-01-13 22:48:32 +05303213 soc_init_card_debugfs(card);
3214
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003215 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
3216 (card->num_links + card->num_aux_devs),
3217 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003218 if (card->rtd == NULL)
3219 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003220 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003221 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003222
3223 for (i = 0; i < card->num_links; i++)
3224 card->rtd[i].dai_link = &card->dai_link[i];
3225
Mark Brownc5af3a22008-11-28 13:29:45 +00003226 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003227 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003228 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003229 mutex_init(&card->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003230 mutex_init(&card->dpcm_mutex);
Sriranjan Srikantamfd7f1ad2012-08-14 17:42:06 -07003231 mutex_init(&card->dapm_power_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003232
Mark Brown87ba7da2012-03-14 21:18:39 +00003233 ret = snd_soc_instantiate_card(card);
3234 if (ret != 0)
3235 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003236
Mark Brownc5af3a22008-11-28 13:29:45 +00003237
Liam Girdwood13e13a32011-01-31 21:30:52 +00003238 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003239}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303240EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003241
3242/**
3243 * snd_soc_unregister_card - Unregister a card with the ASoC core
3244 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003245 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003246 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003247 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303248int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003249{
Vinod Koulb0e26482011-01-13 22:48:02 +05303250 if (card->instantiated)
3251 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003252 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3253
3254 return 0;
3255}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303256EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003257
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003258/*
3259 * Simplify DAI link configuration by removing ".-1" from device names
3260 * and sanitizing names.
3261 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003262static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003263{
3264 char *found, name[NAME_SIZE];
3265 int id1, id2;
3266
3267 if (dev_name(dev) == NULL)
3268 return NULL;
3269
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003270 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003271
3272 /* are we a "%s.%d" name (platform and SPI components) */
3273 found = strstr(name, dev->driver->name);
3274 if (found) {
3275 /* get ID */
3276 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3277
3278 /* discard ID from name if ID == -1 */
3279 if (*id == -1)
3280 found[strlen(dev->driver->name)] = '\0';
3281 }
3282
3283 } else {
3284 /* I2C component devices are named "bus-addr" */
3285 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3286 char tmp[NAME_SIZE];
3287
3288 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003289 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003290
3291 /* sanitize component name for DAI link creation */
3292 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003293 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003294 } else
3295 *id = 0;
3296 }
3297
3298 return kstrdup(name, GFP_KERNEL);
3299}
3300
3301/*
3302 * Simplify DAI link naming for single devices with multiple DAIs by removing
3303 * any ".-1" and using the DAI name (instead of device name).
3304 */
3305static inline char *fmt_multiple_name(struct device *dev,
3306 struct snd_soc_dai_driver *dai_drv)
3307{
3308 if (dai_drv->name == NULL) {
3309 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3310 dev_name(dev));
3311 return NULL;
3312 }
3313
3314 return kstrdup(dai_drv->name, GFP_KERNEL);
3315}
3316
Mark Brown91151712008-11-30 23:31:24 +00003317/**
3318 * snd_soc_register_dai - Register a DAI with the ASoC core
3319 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003320 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003321 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003322int snd_soc_register_dai(struct device *dev,
3323 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003324{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003325 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003326
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003327 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003328
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003329 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3330 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003331 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003332
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003333 /* create DAI component name */
3334 dai->name = fmt_single_name(dev, &dai->id);
3335 if (dai->name == NULL) {
3336 kfree(dai);
3337 return -ENOMEM;
3338 }
3339
3340 dai->dev = dev;
3341 dai->driver = dai_drv;
3342 if (!dai->driver->ops)
3343 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003344
3345 mutex_lock(&client_mutex);
3346 list_add(&dai->list, &dai_list);
3347 mutex_unlock(&client_mutex);
3348
3349 pr_debug("Registered DAI '%s'\n", dai->name);
3350
3351 return 0;
3352}
3353EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3354
3355/**
3356 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3357 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003358 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003359 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003360void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003361{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003362 struct snd_soc_dai *dai;
3363
3364 list_for_each_entry(dai, &dai_list, list) {
3365 if (dev == dai->dev)
3366 goto found;
3367 }
3368 return;
3369
3370found:
Mark Brown91151712008-11-30 23:31:24 +00003371 mutex_lock(&client_mutex);
3372 list_del(&dai->list);
3373 mutex_unlock(&client_mutex);
3374
3375 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003376 kfree(dai->name);
3377 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003378}
3379EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3380
3381/**
3382 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3383 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003384 * @dai: Array of DAIs to register
3385 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003386 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003387int snd_soc_register_dais(struct device *dev,
3388 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003389{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003390 struct snd_soc_dai *dai;
3391 int i, ret = 0;
3392
Liam Girdwood720ffa42010-08-18 00:30:30 +01003393 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003394
3395 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003396
3397 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003398 if (dai == NULL) {
3399 ret = -ENOMEM;
3400 goto err;
3401 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003402
3403 /* create DAI component name */
3404 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3405 if (dai->name == NULL) {
3406 kfree(dai);
3407 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003408 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003409 }
3410
3411 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003412 dai->driver = &dai_drv[i];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003413
Mark Brown0f9141c2010-10-12 15:43:21 +01003414 if (dai->driver->id)
3415 dai->id = dai->driver->id;
3416 else
3417 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003418 if (!dai->driver->ops)
3419 dai->driver->ops = &null_dai_ops;
3420
3421 mutex_lock(&client_mutex);
3422 list_add(&dai->list, &dai_list);
3423 mutex_unlock(&client_mutex);
3424
3425 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003426 }
3427
3428 return 0;
3429
3430err:
3431 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003432 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003433
3434 return ret;
3435}
3436EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3437
3438/**
3439 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3440 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003441 * @dai: Array of DAIs to unregister
3442 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003443 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003444void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003445{
3446 int i;
3447
3448 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003449 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003450}
3451EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3452
Mark Brown12a48a8c2008-12-03 19:40:30 +00003453/**
3454 * snd_soc_register_platform - Register a platform with the ASoC core
3455 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003456 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003457 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003458int snd_soc_register_platform(struct device *dev,
3459 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003460{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003461 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003462
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003463 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3464
3465 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3466 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003467 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003468
3469 /* create platform component name */
3470 platform->name = fmt_single_name(dev, &platform->id);
3471 if (platform->name == NULL) {
3472 kfree(platform);
3473 return -ENOMEM;
3474 }
3475
3476 platform->dev = dev;
3477 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003478 platform->dapm.dev = dev;
3479 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003480 platform->dapm.stream_event = platform_drv->stream_event;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003481
3482 mutex_lock(&client_mutex);
3483 list_add(&platform->list, &platform_list);
3484 mutex_unlock(&client_mutex);
3485
3486 pr_debug("Registered platform '%s'\n", platform->name);
3487
3488 return 0;
3489}
3490EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3491
3492/**
3493 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3494 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003495 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003496 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003497void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003498{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003499 struct snd_soc_platform *platform;
3500
3501 list_for_each_entry(platform, &platform_list, list) {
3502 if (dev == platform->dev)
3503 goto found;
3504 }
3505 return;
3506
3507found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003508 mutex_lock(&client_mutex);
3509 list_del(&platform->list);
3510 mutex_unlock(&client_mutex);
3511
3512 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003513 kfree(platform->name);
3514 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003515}
3516EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3517
Mark Brown151ab222009-05-09 16:22:58 +01003518static u64 codec_format_map[] = {
3519 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3520 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3521 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3522 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3523 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3524 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3525 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3526 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3527 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3528 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3529 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3530 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3531 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3532 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3533 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3534 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3535};
3536
3537/* Fix up the DAI formats for endianness: codecs don't actually see
3538 * the endianness of the data but we're using the CPU format
3539 * definitions which do need to include endianness so we ensure that
3540 * codec DAIs always have both big and little endian variants set.
3541 */
3542static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3543{
3544 int i;
3545
3546 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3547 if (stream->formats & codec_format_map[i])
3548 stream->formats |= codec_format_map[i];
3549}
3550
Mark Brown0d0cf002008-12-10 14:32:45 +00003551/**
Joonwoo Park6fb64392013-08-07 18:59:09 -07003552 * snd_soc_card_change_online_state - Mark if soc card is online/offline
3553 *
3554 * @soc_card : soc_card to mark
3555 */
3556void snd_soc_card_change_online_state(struct snd_soc_card *soc_card, int online)
3557{
3558 snd_card_change_online_state(soc_card->snd_card, online);
3559}
3560EXPORT_SYMBOL(snd_soc_card_change_online_state);
3561
3562/**
Mark Brown0d0cf002008-12-10 14:32:45 +00003563 * snd_soc_register_codec - Register a codec with the ASoC core
3564 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003565 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003566 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003567int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003568 const struct snd_soc_codec_driver *codec_drv,
3569 struct snd_soc_dai_driver *dai_drv,
3570 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003571{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003572 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003573 struct snd_soc_codec *codec;
3574 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003575
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003576 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003577
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003578 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3579 if (codec == NULL)
3580 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003581
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003582 /* create CODEC component name */
3583 codec->name = fmt_single_name(dev, &codec->id);
3584 if (codec->name == NULL) {
3585 kfree(codec);
3586 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003587 }
3588
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003589 if (codec_drv->compress_type)
3590 codec->compress_type = codec_drv->compress_type;
3591 else
3592 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3593
Mark Brownc3acec22010-12-02 16:15:29 +00003594 codec->write = codec_drv->write;
3595 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003596 codec->volatile_register = codec_drv->volatile_register;
3597 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003598 codec->writable_register = codec_drv->writable_register;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003599 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3600 codec->dapm.dev = dev;
3601 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003602 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003603 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003604 codec->dev = dev;
3605 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003606 codec->num_dai = num_dai;
3607 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003608
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003609 /* allocate CODEC register cache */
3610 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003611 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003612 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003613 /* it is necessary to make a copy of the default register cache
3614 * because in the case of using a compression type that requires
3615 * the default register cache to be marked as __devinitconst the
3616 * kernel might have freed the array by the time we initialize
3617 * the cache.
3618 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003619 if (codec_drv->reg_cache_default) {
3620 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3621 reg_size, GFP_KERNEL);
3622 if (!codec->reg_def_copy) {
3623 ret = -ENOMEM;
3624 goto fail;
3625 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003626 }
3627 }
3628
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003629 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3630 if (!codec->volatile_register)
3631 codec->volatile_register = snd_soc_default_volatile_register;
3632 if (!codec->readable_register)
3633 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003634 if (!codec->writable_register)
3635 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003636 }
3637
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003638 for (i = 0; i < num_dai; i++) {
3639 fixup_codec_formats(&dai_drv[i].playback);
3640 fixup_codec_formats(&dai_drv[i].capture);
3641 }
3642
Mark Brown26b01cc2010-08-18 20:20:55 +01003643 /* register any DAIs */
3644 if (num_dai) {
3645 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3646 if (ret < 0)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003647 goto fail;
Mark Brown26b01cc2010-08-18 20:20:55 +01003648 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003649
Mark Brown0d0cf002008-12-10 14:32:45 +00003650 mutex_lock(&client_mutex);
3651 list_add(&codec->list, &codec_list);
Mark Brown0d0cf002008-12-10 14:32:45 +00003652 mutex_unlock(&client_mutex);
3653
3654 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003655 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003656
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003657fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003658 kfree(codec->reg_def_copy);
3659 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003660 kfree(codec->name);
3661 kfree(codec);
3662 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003663}
3664EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3665
3666/**
3667 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3668 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003669 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003670 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003671void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003672{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003673 struct snd_soc_codec *codec;
3674 int i;
3675
3676 list_for_each_entry(codec, &codec_list, list) {
3677 if (dev == codec->dev)
3678 goto found;
3679 }
3680 return;
3681
3682found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003683 if (codec->num_dai)
3684 for (i = 0; i < codec->num_dai; i++)
3685 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003686
Mark Brown0d0cf002008-12-10 14:32:45 +00003687 mutex_lock(&client_mutex);
3688 list_del(&codec->list);
3689 mutex_unlock(&client_mutex);
3690
3691 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003692
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003693 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003694 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003695 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003696 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003697}
3698EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3699
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003700/* Retrieve a card's name from device tree */
3701int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3702 const char *propname)
3703{
3704 struct device_node *np = card->dev->of_node;
3705 int ret;
3706
3707 ret = of_property_read_string_index(np, propname, 0, &card->name);
3708 /*
3709 * EINVAL means the property does not exist. This is fine providing
3710 * card->name was previously set, which is checked later in
3711 * snd_soc_register_card.
3712 */
3713 if (ret < 0 && ret != -EINVAL) {
3714 dev_err(card->dev,
3715 "Property '%s' could not be read: %d\n",
3716 propname, ret);
3717 return ret;
3718 }
3719
3720 return 0;
3721}
3722EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3723
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003724int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3725 const char *propname)
3726{
3727 struct device_node *np = card->dev->of_node;
3728 int num_routes;
3729 struct snd_soc_dapm_route *routes;
3730 int i, ret;
3731
3732 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003733 if (num_routes < 0 || num_routes & 1) {
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003734 dev_err(card->dev,
Richard Zhaoc34ce322012-04-24 15:24:43 +08003735 "Property '%s' does not exist or its length is not even\n",
3736 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003737 return -EINVAL;
3738 }
3739 num_routes /= 2;
3740 if (!num_routes) {
3741 dev_err(card->dev,
3742 "Property '%s's length is zero\n",
3743 propname);
3744 return -EINVAL;
3745 }
3746
3747 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3748 GFP_KERNEL);
3749 if (!routes) {
3750 dev_err(card->dev,
3751 "Could not allocate DAPM route table\n");
3752 return -EINVAL;
3753 }
3754
3755 for (i = 0; i < num_routes; i++) {
3756 ret = of_property_read_string_index(np, propname,
3757 2 * i, &routes[i].sink);
3758 if (ret) {
3759 dev_err(card->dev,
3760 "Property '%s' index %d could not be read: %d\n",
3761 propname, 2 * i, ret);
3762 return -EINVAL;
3763 }
3764 ret = of_property_read_string_index(np, propname,
3765 (2 * i) + 1, &routes[i].source);
3766 if (ret) {
3767 dev_err(card->dev,
3768 "Property '%s' index %d could not be read: %d\n",
3769 propname, (2 * i) + 1, ret);
3770 return -EINVAL;
3771 }
3772 }
3773
3774 card->num_dapm_routes = num_routes;
3775 card->dapm_routes = routes;
3776
3777 return 0;
3778}
3779EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3780
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003781static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003782{
Mark Brown384c89e2008-12-03 17:34:03 +00003783#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003784 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3785 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Mark Brown384c89e2008-12-03 17:34:03 +00003786 printk(KERN_WARNING
3787 "ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00003788 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00003789 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003790
Mark Brown8a9dab12011-01-10 22:25:21 +00003791 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01003792 &codec_list_fops))
3793 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3794
Mark Brown8a9dab12011-01-10 22:25:21 +00003795 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01003796 &dai_list_fops))
3797 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003798
Mark Brown8a9dab12011-01-10 22:25:21 +00003799 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01003800 &platform_list_fops))
3801 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003802#endif
3803
Mark Brownfb257892011-04-28 10:57:54 +01003804 snd_soc_util_init();
3805
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003806 return platform_driver_register(&soc_driver);
3807}
Mark Brown4abe8e12010-10-12 17:41:03 +01003808module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003809
Mark Brown7d8c16a2008-11-30 22:11:24 +00003810static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003811{
Mark Brownfb257892011-04-28 10:57:54 +01003812 snd_soc_util_exit();
3813
Mark Brown384c89e2008-12-03 17:34:03 +00003814#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003815 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00003816#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003817 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003818}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003819module_exit(snd_soc_exit);
3820
3821/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003822MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003823MODULE_DESCRIPTION("ALSA SoC Core");
3824MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003825MODULE_ALIAS("platform:soc-audio");