blob: 5f2a6b57062759f5e41d6a931d762f3b078ce9a1 [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
Namarta Kohli9f052a72012-08-16 17:10:41 +05301336 if (cpu_dai->driver->compress_dai) {
1337 /*create compress_device"*/
1338 ret = soc_new_compress(rtd, num);
1339 if (ret < 0) {
1340 printk(KERN_ERR "asoc: can't create compress %s\n",
1341 dai_link->stream_name);
1342 return ret;
1343 }
1344 } else {
1345 /* create the pcm */
1346 ret = soc_new_pcm(rtd, num);
1347 if (ret < 0) {
1348 printk(KERN_ERR "asoc: can't create pcm %s :%d\n",
1349 dai_link->stream_name, ret);
1350 return ret;
1351 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001352 }
1353
1354 /* add platform data for AC97 devices */
1355 if (rtd->codec_dai->driver->ac97_control)
1356 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1357
1358 return 0;
1359}
1360
1361#ifdef CONFIG_SND_SOC_AC97_BUS
1362static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1363{
1364 int ret;
1365
1366 /* Only instantiate AC97 if not already done by the adaptor
1367 * for the generic AC97 subsystem.
1368 */
1369 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001370 /*
1371 * It is possible that the AC97 device is already registered to
1372 * the device subsystem. This happens when the device is created
1373 * via snd_ac97_mixer(). Currently only SoC codec that does so
1374 * is the generic AC97 glue but others migh emerge.
1375 *
1376 * In those cases we don't try to register the device again.
1377 */
1378 if (!rtd->codec->ac97_created)
1379 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001380
1381 ret = soc_ac97_dev_register(rtd->codec);
1382 if (ret < 0) {
1383 printk(KERN_ERR "asoc: AC97 device register failed\n");
1384 return ret;
1385 }
1386
1387 rtd->codec->ac97_registered = 1;
1388 }
1389 return 0;
1390}
1391
1392static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1393{
1394 if (codec->ac97_registered) {
1395 soc_ac97_dev_unregister(codec);
1396 codec->ac97_registered = 0;
1397 }
1398}
1399#endif
1400
Mark Brown87ba7da2012-03-14 21:18:39 +00001401static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1402{
1403 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1404 struct snd_soc_codec *codec;
1405
1406 /* find CODEC from registered CODECs*/
1407 list_for_each_entry(codec, &codec_list, list) {
1408 if (!strcmp(codec->name, aux_dev->codec_name))
1409 return 0;
1410 }
1411
1412 return -EPROBE_DEFER;
1413}
1414
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001415static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1416{
1417 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001418 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001419 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001420
1421 /* find CODEC from registered CODECs*/
1422 list_for_each_entry(codec, &codec_list, list) {
1423 if (!strcmp(codec->name, aux_dev->codec_name)) {
1424 if (codec->probed) {
1425 dev_err(codec->dev,
1426 "asoc: codec already probed");
1427 ret = -EBUSY;
1428 goto out;
1429 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001430 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001431 }
1432 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001433 /* codec not found */
1434 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
Mark Brown87ba7da2012-03-14 21:18:39 +00001435 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001436
Jarkko Nikula676ad982010-12-03 09:18:22 +02001437found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001438 ret = soc_probe_codec(card, codec);
1439 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001440 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001441
Jarkko Nikula589c3562010-12-06 16:27:07 +02001442 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001443
1444out:
1445 return ret;
1446}
1447
1448static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1449{
1450 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1451 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001452
1453 /* unregister the rtd device */
1454 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001455 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1456 device_del(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001457 rtd->dev_registered = 0;
1458 }
1459
Jarkko Nikula589c3562010-12-06 16:27:07 +02001460 if (codec && codec->probed)
1461 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001462}
1463
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001464static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1465 enum snd_soc_compress_type compress_type)
1466{
1467 int ret;
1468
1469 if (codec->cache_init)
1470 return 0;
1471
1472 /* override the compress_type if necessary */
1473 if (compress_type && codec->compress_type != compress_type)
1474 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001475 ret = snd_soc_cache_init(codec);
1476 if (ret < 0) {
1477 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1478 ret);
1479 return ret;
1480 }
1481 codec->cache_init = 1;
1482 return 0;
1483}
1484
Steve Mucklef132c6c2012-06-06 18:30:57 -07001485static void soc_init_dai_aif_channel_map(struct snd_soc_card *card,
1486 struct snd_soc_dai *dai, int stream)
1487{
1488 struct snd_soc_dapm_widget *w = NULL;
1489 const char *aif_name;
1490
1491 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1492 aif_name = dai->driver->playback.aif_name;
1493 else
1494 aif_name = dai->driver->capture.aif_name;
1495
1496 if (dai->codec) {
1497 struct snd_soc_codec *codec;
1498
1499 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
1500 w = snd_soc_get_codec_widget(card, codec, aif_name);
1501 if (w)
1502 break;
1503 }
1504 } else if (dai->platform) {
1505 struct snd_soc_platform *platform;
1506
1507 list_for_each_entry(platform, &card->platform_dev_list, card_list) {
1508 w = snd_soc_get_platform_widget(card, platform, aif_name);
1509 if (w)
1510 break;
1511 }
1512 }
1513
1514 if (w) {
1515 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1516 dai->playback_aif = w;
1517 else
1518 dai->capture_aif = w;
1519 } else
1520 dev_err(dai->dev, "unable to find %s DAI AIF %s\n",
1521 stream ? "capture" : "playback", aif_name);
1522
1523 dai->channel_map_instanciated = 1;
1524}
1525
1526static int soc_is_dai_pcm(struct snd_soc_card *card, struct snd_soc_dai *dai)
1527{
1528 int i;
1529
1530 for (i = 0; i < card->num_rtd; i++) {
1531 if (card->rtd[i].cpu_dai == dai && !card->rtd[i].dai_link->no_pcm)
1532 return 1;
1533 }
1534 return 0;
1535}
1536
1537static void soc_init_card_aif_channel_map(struct snd_soc_card *card)
1538{
1539 struct snd_soc_dai *dai;
1540
1541 list_for_each_entry(dai, &card->dai_dev_list, card_list) {
1542
1543 /* only process DAIs that use the new API until
1544 * the old "stream name" API is fully deprecated */
1545 if (!dai->driver->playback.aif_name && !dai->driver->capture.aif_name)
1546 continue;
1547
1548 /* channels are only mapped from PCM DAIs */
1549 if (!soc_is_dai_pcm(card, dai))
1550 continue;
1551
1552 /* skip if already instanciated */
1553 if (dai->channel_map_instanciated)
1554 continue;
1555
1556 /* create unique channels masks for each DAI in the sound card */
1557 dai->playback_channel_map =
1558 ((1 << dai->driver->playback.channels_max) - 1)
1559 << card->num_playback_channels;
1560 card->num_playback_channels += dai->driver->playback.channels_max;
1561
1562 dai->capture_channel_map =
1563 ((1 << dai->driver->capture.channels_max) - 1)
1564 << card->num_capture_channels;
1565 card->num_capture_channels += dai->driver->capture.channels_max;
1566
1567 if (dai->driver->playback.channels_max)
1568 soc_init_dai_aif_channel_map(card, dai, SNDRV_PCM_STREAM_PLAYBACK);
1569 if (dai->driver->capture.channels_max)
1570 soc_init_dai_aif_channel_map(card, dai, SNDRV_PCM_STREAM_CAPTURE);
1571 }
1572}
1573
1574
Mark Brown87ba7da2012-03-14 21:18:39 +00001575static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001576{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001577 struct snd_soc_codec *codec;
1578 struct snd_soc_codec_conf *codec_conf;
1579 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001580 struct snd_soc_dai_link *dai_link;
Liam Girdwood0168bf02011-06-07 16:08:05 +01001581 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001582
1583 mutex_lock(&card->mutex);
1584
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001585
1586 /* bind DAIs */
Mark Brown87ba7da2012-03-14 21:18:39 +00001587 for (i = 0; i < card->num_links; i++) {
1588 ret = soc_bind_dai_link(card, i);
1589 if (ret != 0)
1590 goto base_error;
1591 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001592
Mark Brown87ba7da2012-03-14 21:18:39 +00001593 /* check aux_devs too */
1594 for (i = 0; i < card->num_aux_devs; i++) {
1595 ret = soc_check_aux_dev(card, i);
1596 if (ret != 0)
1597 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001598 }
1599
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001600 /* initialize the register cache for each available codec */
1601 list_for_each_entry(codec, &codec_list, list) {
1602 if (codec->cache_init)
1603 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001604 /* by default we don't override the compress_type */
1605 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001606 /* check to see if we need to override the compress_type */
1607 for (i = 0; i < card->num_configs; ++i) {
1608 codec_conf = &card->codec_conf[i];
1609 if (!strcmp(codec->name, codec_conf->dev_name)) {
1610 compress_type = codec_conf->compress_type;
1611 if (compress_type && compress_type
1612 != codec->compress_type)
1613 break;
1614 }
1615 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001616 ret = snd_soc_init_codec_cache(codec, compress_type);
Mark Brown87ba7da2012-03-14 21:18:39 +00001617 if (ret < 0)
1618 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001619 }
1620
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001621 /* card bind complete so register a sound card */
1622 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1623 card->owner, 0, &card->snd_card);
1624 if (ret < 0) {
1625 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1626 card->name);
Mark Brown87ba7da2012-03-14 21:18:39 +00001627 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001628 }
1629 card->snd_card->dev = card->dev;
1630
Mark Browne37a4972011-03-02 18:21:57 +00001631 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1632 card->dapm.dev = card->dev;
1633 card->dapm.card = card;
1634 list_add(&card->dapm.list, &card->dapm_list);
1635
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001636#ifdef CONFIG_DEBUG_FS
1637 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1638#endif
1639
Mark Brown88ee1c62011-02-02 10:43:26 +00001640#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001641 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001642 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001643#endif
Andy Green6ed25972008-06-13 16:24:05 +01001644
Mark Brown9a841eb2011-04-12 17:51:37 -07001645 if (card->dapm_widgets)
1646 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1647 card->num_dapm_widgets);
1648
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001649 /* initialise the sound card only once */
1650 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001651 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001652 if (ret < 0)
1653 goto card_probe_error;
1654 }
1655
Liam Girdwood0168bf02011-06-07 16:08:05 +01001656 /* early DAI link probe */
1657 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1658 order++) {
1659 for (i = 0; i < card->num_links; i++) {
1660 ret = soc_probe_dai_link(card, i, order);
1661 if (ret < 0) {
1662 pr_err("asoc: failed to instantiate card %s: %d\n",
Mark Brown321de0d2010-09-21 15:09:37 +01001663 card->name, ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001664 goto probe_dai_err;
1665 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001666 }
1667 }
1668
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001669 for (i = 0; i < card->num_aux_devs; i++) {
1670 ret = soc_probe_aux_dev(card, i);
1671 if (ret < 0) {
1672 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1673 card->name, ret);
1674 goto probe_aux_dev_err;
1675 }
1676 }
1677
Mark Brownb7af1da2011-04-07 19:18:44 +09001678 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001679 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001680
Mark Brownb8ad29d2011-03-02 18:35:51 +00001681 if (card->dapm_routes)
1682 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1683 card->num_dapm_routes);
1684
Mark Brownb90d2f92011-10-10 13:38:06 +01001685 snd_soc_dapm_new_widgets(&card->dapm);
1686
Mark Brown75d9ac42011-09-27 16:41:01 +01001687 for (i = 0; i < card->num_links; i++) {
1688 dai_link = &card->dai_link[i];
1689
1690 if (dai_link->dai_fmt) {
1691 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1692 dai_link->dai_fmt);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001693 if (ret != 0)
Mark Brown75d9ac42011-09-27 16:41:01 +01001694 dev_warn(card->rtd[i].codec_dai->dev,
1695 "Failed to set DAI format: %d\n",
1696 ret);
1697
1698 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1699 dai_link->dai_fmt);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001700 if (ret != 0)
Mark Brown75d9ac42011-09-27 16:41:01 +01001701 dev_warn(card->rtd[i].cpu_dai->dev,
1702 "Failed to set DAI format: %d\n",
1703 ret);
1704 }
1705 }
1706
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001707 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001708 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001709 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1710 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001711 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1712 "%s", card->driver_name ? card->driver_name : card->name);
1713 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1714 switch (card->snd_card->driver[i]) {
1715 case '_':
1716 case '-':
1717 case '\0':
1718 break;
1719 default:
1720 if (!isalnum(card->snd_card->driver[i]))
1721 card->snd_card->driver[i] = '_';
1722 break;
1723 }
1724 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001725
Mark Brown28e9ad92011-03-02 18:36:34 +00001726 if (card->late_probe) {
1727 ret = card->late_probe(card);
1728 if (ret < 0) {
1729 dev_err(card->dev, "%s late_probe() failed: %d\n",
1730 card->name, ret);
1731 goto probe_aux_dev_err;
1732 }
1733 }
1734
Mark Brown2dc00212011-10-08 13:59:44 +01001735 snd_soc_dapm_new_widgets(&card->dapm);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001736 soc_init_card_aif_channel_map(card);
Mark Brown2dc00212011-10-08 13:59:44 +01001737
Stephen Warren16332812011-11-23 12:42:04 -07001738 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001739 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001740 snd_soc_dapm_auto_nc_codec_pins(codec);
1741
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001742 ret = snd_card_register(card->snd_card);
1743 if (ret < 0) {
1744 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
Axel Lin6b3ed782010-12-07 16:12:29 +08001745 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001746 }
1747
1748#ifdef CONFIG_SND_SOC_AC97_BUS
1749 /* register any AC97 codecs */
1750 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001751 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1752 if (ret < 0) {
1753 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1754 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001755 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001756 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001757 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001758 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001759#endif
1760
Mark Brown435c5e22008-12-04 15:32:53 +00001761 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001762 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001763 mutex_unlock(&card->mutex);
Mark Brown87ba7da2012-03-14 21:18:39 +00001764 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001765
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001766probe_aux_dev_err:
1767 for (i = 0; i < card->num_aux_devs; i++)
1768 soc_remove_aux_dev(card, i);
1769
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001770probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001771 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001772
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001773card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001774 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001775 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001776
1777 snd_card_free(card->snd_card);
Mark Brown87ba7da2012-03-14 21:18:39 +00001778base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001779 mutex_unlock(&card->mutex);
Mark Brown87ba7da2012-03-14 21:18:39 +00001780 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001781}
1782
1783/* probes a new socdev */
1784static int soc_probe(struct platform_device *pdev)
1785{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001786 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001787 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001788
Vinod Koul70a7ca32011-01-14 19:22:48 +05301789 /*
1790 * no card, so machine driver should be registering card
1791 * we should not be here in that case so ret error
1792 */
1793 if (!card)
1794 return -EINVAL;
1795
Mark Brown435c5e22008-12-04 15:32:53 +00001796 /* Bodge while we unpick instantiation */
1797 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001798
Mark Brown435c5e22008-12-04 15:32:53 +00001799 ret = snd_soc_register_card(card);
1800 if (ret != 0) {
1801 dev_err(&pdev->dev, "Failed to register card\n");
1802 return ret;
1803 }
1804
1805 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001806}
1807
Vinod Koulb0e26482011-01-13 22:48:02 +05301808static int soc_cleanup_card_resources(struct snd_soc_card *card)
1809{
Vinod Koulb0e26482011-01-13 22:48:02 +05301810 int i;
1811
1812 /* make sure any delayed work runs */
1813 for (i = 0; i < card->num_rtd; i++) {
1814 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1815 flush_delayed_work_sync(&rtd->delayed_work);
1816 }
1817
1818 /* remove auxiliary devices */
1819 for (i = 0; i < card->num_aux_devs; i++)
1820 soc_remove_aux_dev(card, i);
1821
1822 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001823 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301824
1825 soc_cleanup_card_debugfs(card);
1826
1827 /* remove the card */
1828 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001829 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301830
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001831 snd_soc_dapm_free(&card->dapm);
1832
Vinod Koulb0e26482011-01-13 22:48:02 +05301833 kfree(card->rtd);
1834 snd_card_free(card->snd_card);
1835 return 0;
1836
1837}
1838
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001839/* removes a socdev */
1840static int soc_remove(struct platform_device *pdev)
1841{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001842 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001843
Mark Brownc5af3a22008-11-28 13:29:45 +00001844 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001845 return 0;
1846}
1847
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001848int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001849{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001850 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001851 int i;
Mark Brown51737472009-06-22 13:16:51 +01001852
1853 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001854 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001855
1856 /* Flush out pmdown_time work - we actually do want to run it
1857 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001858 for (i = 0; i < card->num_rtd; i++) {
1859 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001860 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001861 }
Mark Brown51737472009-06-22 13:16:51 +01001862
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001863 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001864
1865 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001866}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001867EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001868
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001869const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301870 .suspend = snd_soc_suspend,
1871 .resume = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001872 .poweroff = snd_soc_poweroff,
Mark Brown416356f2009-06-30 19:05:15 +01001873};
Stephen Warrendeb26072011-04-05 19:35:30 -06001874EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001875
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001876/* ASoC platform driver */
1877static struct platform_driver soc_driver = {
1878 .driver = {
1879 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001880 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001881 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001882 },
1883 .probe = soc_probe,
1884 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001885};
1886
Mark Brown096e49d2009-07-05 15:12:22 +01001887/**
1888 * snd_soc_codec_volatile_register: Report if a register is volatile.
1889 *
1890 * @codec: CODEC to query.
1891 * @reg: Register to query.
1892 *
1893 * Boolean function indiciating if a CODEC register is volatile.
1894 */
Mark Brown181e0552011-01-24 14:05:25 +00001895int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1896 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001897{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001898 if (codec->volatile_register)
1899 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001900 else
1901 return 0;
1902}
1903EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1904
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001905/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001906 * snd_soc_codec_readable_register: Report if a register is readable.
1907 *
1908 * @codec: CODEC to query.
1909 * @reg: Register to query.
1910 *
1911 * Boolean function indicating if a CODEC register is readable.
1912 */
1913int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1914 unsigned int reg)
1915{
1916 if (codec->readable_register)
1917 return codec->readable_register(codec, reg);
1918 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001919 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001920}
1921EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1922
1923/**
1924 * snd_soc_codec_writable_register: Report if a register is writable.
1925 *
1926 * @codec: CODEC to query.
1927 * @reg: Register to query.
1928 *
1929 * Boolean function indicating if a CODEC register is writable.
1930 */
1931int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1932 unsigned int reg)
1933{
1934 if (codec->writable_register)
1935 return codec->writable_register(codec, reg);
1936 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001937 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001938}
1939EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1940
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001941int snd_soc_platform_read(struct snd_soc_platform *platform,
1942 unsigned int reg)
1943{
1944 unsigned int ret;
1945
1946 if (!platform->driver->read) {
1947 dev_err(platform->dev, "platform has no read back\n");
1948 return -1;
1949 }
1950
1951 ret = platform->driver->read(platform, reg);
1952 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001953 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001954
1955 return ret;
1956}
1957EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1958
1959int snd_soc_platform_write(struct snd_soc_platform *platform,
1960 unsigned int reg, unsigned int val)
1961{
1962 if (!platform->driver->write) {
1963 dev_err(platform->dev, "platform has no write back\n");
1964 return -1;
1965 }
1966
1967 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001968 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001969 return platform->driver->write(platform, reg, val);
1970}
1971EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1972
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001973/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001974 * snd_soc_new_ac97_codec - initailise AC97 device
1975 * @codec: audio codec
1976 * @ops: AC97 bus operations
1977 * @num: AC97 codec number
1978 *
1979 * Initialises AC97 codec resources for use by ad-hoc devices only.
1980 */
1981int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1982 struct snd_ac97_bus_ops *ops, int num)
1983{
1984 mutex_lock(&codec->mutex);
1985
1986 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1987 if (codec->ac97 == NULL) {
1988 mutex_unlock(&codec->mutex);
1989 return -ENOMEM;
1990 }
1991
1992 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1993 if (codec->ac97->bus == NULL) {
1994 kfree(codec->ac97);
1995 codec->ac97 = NULL;
1996 mutex_unlock(&codec->mutex);
1997 return -ENOMEM;
1998 }
1999
2000 codec->ac97->bus->ops = ops;
2001 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002002
2003 /*
2004 * Mark the AC97 device to be created by us. This way we ensure that the
2005 * device will be registered with the device subsystem later on.
2006 */
2007 codec->ac97_created = 1;
2008
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002009 mutex_unlock(&codec->mutex);
2010 return 0;
2011}
2012EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2013
2014/**
2015 * snd_soc_free_ac97_codec - free AC97 codec device
2016 * @codec: audio codec
2017 *
2018 * Frees AC97 codec device resources.
2019 */
2020void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2021{
2022 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002023#ifdef CONFIG_SND_SOC_AC97_BUS
2024 soc_unregister_ac97_dai_link(codec);
2025#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002026 kfree(codec->ac97->bus);
2027 kfree(codec->ac97);
2028 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002029 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002030 mutex_unlock(&codec->mutex);
2031}
2032EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2033
Mark Brownc3753702010-11-01 15:41:57 -04002034unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2035{
2036 unsigned int ret;
2037
Joonwoo Park6fb64392013-08-07 18:59:09 -07002038 if (unlikely(!snd_card_is_online_state(codec->card->snd_card))) {
2039 dev_err(codec->dev, "read 0x%02x while offline\n", reg);
2040 return -ENODEV;
2041 }
Teow Wan Yee9c6ec1a2016-09-23 17:17:39 +08002042
2043 if (codec->read) {
2044 ret = codec->read(codec, reg);
2045 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
2046 trace_snd_soc_reg_read(codec, reg, ret);
2047 }
2048 else
Teow Wan Yeeed1afa62016-11-22 17:13:00 +08002049 ret = -1;
Mark Brownc3753702010-11-01 15:41:57 -04002050 return ret;
2051}
2052EXPORT_SYMBOL_GPL(snd_soc_read);
2053
2054unsigned int snd_soc_write(struct snd_soc_codec *codec,
2055 unsigned int reg, unsigned int val)
2056{
Joonwoo Park6fb64392013-08-07 18:59:09 -07002057 if (unlikely(!snd_card_is_online_state(codec->card->snd_card))) {
2058 dev_err(codec->dev, "write 0x%02x while offline\n", reg);
2059 return -ENODEV;
2060 }
Teow Wan Yee9c6ec1a2016-09-23 17:17:39 +08002061 if (codec->write) {
2062 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
2063 trace_snd_soc_reg_write(codec, reg, val);
2064 return codec->write(codec, reg, val);
2065 }
2066 else
Teow Wan Yeeed1afa62016-11-22 17:13:00 +08002067 return -1;
Mark Brownc3753702010-11-01 15:41:57 -04002068}
2069EXPORT_SYMBOL_GPL(snd_soc_write);
2070
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00002071unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2072 unsigned int reg, const void *data, size_t len)
2073{
2074 return codec->bulk_write_raw(codec, reg, data, len);
2075}
2076EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2077
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002078/**
2079 * snd_soc_update_bits - update codec register bits
2080 * @codec: audio codec
2081 * @reg: codec register
2082 * @mask: register mask
2083 * @value: new value
2084 *
2085 * Writes new register value.
2086 *
Timur Tabi180c3292011-01-10 15:58:13 -06002087 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002088 */
2089int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002090 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002091{
Mark Brown8a713da2011-12-03 12:33:55 +00002092 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002093 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002094 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002095
Mark Brown8a713da2011-12-03 12:33:55 +00002096 if (codec->using_regmap) {
2097 ret = regmap_update_bits_check(codec->control_data, reg,
2098 mask, value, &change);
2099 } else {
2100 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06002101 if (ret < 0)
2102 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00002103
2104 old = ret;
2105 new = (old & ~mask) | (value & mask);
2106 change = old != new;
2107 if (change)
2108 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06002109 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002110
Mark Brown8a713da2011-12-03 12:33:55 +00002111 if (ret < 0)
2112 return ret;
2113
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002114 return change;
2115}
2116EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2117
2118/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002119 * snd_soc_update_bits_locked - update codec register bits
2120 * @codec: audio codec
2121 * @reg: codec register
2122 * @mask: register mask
2123 * @value: new value
2124 *
2125 * Writes new register value, and takes the codec mutex.
2126 *
2127 * Returns 1 for change else 0.
2128 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002129int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2130 unsigned short reg, unsigned int mask,
2131 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002132{
2133 int change;
2134
2135 mutex_lock(&codec->mutex);
2136 change = snd_soc_update_bits(codec, reg, mask, value);
2137 mutex_unlock(&codec->mutex);
2138
2139 return change;
2140}
Mark Browndd1b3d52009-12-04 14:22:03 +00002141EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002142
2143/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002144 * snd_soc_test_bits - test register for change
2145 * @codec: audio codec
2146 * @reg: codec register
2147 * @mask: register mask
2148 * @value: new value
2149 *
2150 * Tests a register with a new value and checks if the new value is
2151 * different from the old value.
2152 *
2153 * Returns 1 for change else 0.
2154 */
2155int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002156 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002157{
2158 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002159 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002160
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002161 old = snd_soc_read(codec, reg);
2162 new = (old & ~mask) | value;
2163 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002164
2165 return change;
2166}
2167EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2168
2169/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002170 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2171 * @substream: the pcm substream
2172 * @hw: the hardware parameters
2173 *
2174 * Sets the substream runtime hardware parameters.
2175 */
2176int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2177 const struct snd_pcm_hardware *hw)
2178{
2179 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwood13e13a32011-01-31 21:30:52 +00002180 if (!runtime)
2181 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002182 runtime->hw.info = hw->info;
2183 runtime->hw.formats = hw->formats;
2184 runtime->hw.period_bytes_min = hw->period_bytes_min;
2185 runtime->hw.period_bytes_max = hw->period_bytes_max;
2186 runtime->hw.periods_min = hw->periods_min;
2187 runtime->hw.periods_max = hw->periods_max;
2188 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2189 runtime->hw.fifo_size = hw->fifo_size;
2190 return 0;
2191}
2192EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2193
2194/**
2195 * snd_soc_cnew - create new control
2196 * @_template: control template
2197 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002198 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002199 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002200 *
2201 * Create a new mixer control from a template control.
2202 *
2203 * Returns 0 for success, else error.
2204 */
2205struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brownefb7ac32011-03-08 17:23:24 +00002206 void *data, char *long_name,
2207 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002208{
2209 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002210 struct snd_kcontrol *kcontrol;
2211 char *name = NULL;
2212 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002213
2214 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002215 template.index = 0;
2216
Mark Brownefb7ac32011-03-08 17:23:24 +00002217 if (!long_name)
2218 long_name = template.name;
2219
2220 if (prefix) {
2221 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08002222 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00002223 if (!name)
2224 return NULL;
2225
2226 snprintf(name, name_len, "%s %s", prefix, long_name);
2227
2228 template.name = name;
2229 } else {
2230 template.name = long_name;
2231 }
2232
2233 kcontrol = snd_ctl_new1(&template, data);
2234
2235 kfree(name);
2236
2237 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002238}
2239EXPORT_SYMBOL_GPL(snd_soc_cnew);
2240
Liam Girdwood022658b2012-02-03 17:43:09 +00002241static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2242 const struct snd_kcontrol_new *controls, int num_controls,
2243 const char *prefix, void *data)
2244{
2245 int err, i;
2246
2247 for (i = 0; i < num_controls; i++) {
2248 const struct snd_kcontrol_new *control = &controls[i];
2249 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2250 control->name, prefix));
2251 if (err < 0) {
2252 dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2253 return err;
2254 }
2255 }
2256
2257 return 0;
2258}
2259
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002260/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002261 * snd_soc_add_codec_controls - add an array of controls to a codec.
2262 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002263 * duplicating this code.
2264 *
2265 * @codec: codec to add controls to
2266 * @controls: array of controls to add
2267 * @num_controls: number of elements in the array
2268 *
2269 * Return 0 for success, else error.
2270 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002271int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002272 const struct snd_kcontrol_new *controls, int num_controls)
2273{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002274 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002275
Liam Girdwood022658b2012-02-03 17:43:09 +00002276 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2277 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002278}
Liam Girdwood022658b2012-02-03 17:43:09 +00002279EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002280
2281/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002282 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002283 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002284 *
2285 * @platform: platform to add controls to
2286 * @controls: array of controls to add
2287 * @num_controls: number of elements in the array
2288 *
2289 * Return 0 for success, else error.
2290 */
2291int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2292 const struct snd_kcontrol_new *controls, int num_controls)
2293{
2294 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002295
Liam Girdwood022658b2012-02-03 17:43:09 +00002296 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2297 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002298}
2299EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2300
2301/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002302 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2303 * Convenience function to add a list of controls.
2304 *
2305 * @soc_card: SoC card to add controls to
2306 * @controls: array of controls to add
2307 * @num_controls: number of elements in the array
2308 *
2309 * Return 0 for success, else error.
2310 */
2311int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2312 const struct snd_kcontrol_new *controls, int num_controls)
2313{
2314 struct snd_card *card = soc_card->snd_card;
2315
2316 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2317 NULL, soc_card);
2318}
2319EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2320
2321/**
2322 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2323 * Convienience function to add a list of controls.
2324 *
2325 * @dai: DAI to add controls to
2326 * @controls: array of controls to add
2327 * @num_controls: number of elements in the array
2328 *
2329 * Return 0 for success, else error.
2330 */
2331int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2332 const struct snd_kcontrol_new *controls, int num_controls)
2333{
2334 struct snd_card *card = dai->card->snd_card;
2335
2336 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2337 NULL, dai);
2338}
2339EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2340
2341/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002342 * snd_soc_info_enum_double - enumerated double mixer info callback
2343 * @kcontrol: mixer control
2344 * @uinfo: control element information
2345 *
2346 * Callback to provide information about a double enumerated
2347 * mixer control.
2348 *
2349 * Returns 0 for success.
2350 */
2351int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2352 struct snd_ctl_elem_info *uinfo)
2353{
2354 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2355
2356 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2357 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002358 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002359
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002360 if (uinfo->value.enumerated.item > e->max - 1)
2361 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002362 strcpy(uinfo->value.enumerated.name,
Liam Girdwooda8f13d82011-05-18 18:39:21 +01002363 snd_soc_get_enum_text(e, uinfo->value.enumerated.item));
2364
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002365 return 0;
2366}
2367EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2368
2369/**
2370 * snd_soc_get_enum_double - enumerated double mixer get callback
2371 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002372 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002373 *
2374 * Callback to get the value of a double enumerated mixer.
2375 *
2376 * Returns 0 for success.
2377 */
2378int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2379 struct snd_ctl_elem_value *ucontrol)
2380{
2381 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2382 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002383 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002384
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002385 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002386 ;
2387 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002388 ucontrol->value.enumerated.item[0]
2389 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002390 if (e->shift_l != e->shift_r)
2391 ucontrol->value.enumerated.item[1] =
2392 (val >> e->shift_r) & (bitmask - 1);
2393
2394 return 0;
2395}
2396EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2397
2398/**
2399 * snd_soc_put_enum_double - enumerated double mixer put callback
2400 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002401 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002402 *
2403 * Callback to set the value of a double enumerated mixer.
2404 *
2405 * Returns 0 for success.
2406 */
2407int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2408 struct snd_ctl_elem_value *ucontrol)
2409{
2410 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2411 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002412 unsigned int val;
2413 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002414
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002415 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002416 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002417 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002418 return -EINVAL;
2419 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2420 mask = (bitmask - 1) << e->shift_l;
2421 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002422 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002423 return -EINVAL;
2424 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2425 mask |= (bitmask - 1) << e->shift_r;
2426 }
2427
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002428 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002429}
2430EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2431
2432/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002433 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2434 * @kcontrol: mixer control
2435 * @ucontrol: control element information
2436 *
2437 * Callback to get the value of a double semi enumerated mixer.
2438 *
2439 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2440 * used for handling bitfield coded enumeration for example.
2441 *
2442 * Returns 0 for success.
2443 */
2444int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2445 struct snd_ctl_elem_value *ucontrol)
2446{
2447 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002448 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002449 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002450
2451 reg_val = snd_soc_read(codec, e->reg);
2452 val = (reg_val >> e->shift_l) & e->mask;
2453 for (mux = 0; mux < e->max; mux++) {
2454 if (val == e->values[mux])
2455 break;
2456 }
2457 ucontrol->value.enumerated.item[0] = mux;
2458 if (e->shift_l != e->shift_r) {
2459 val = (reg_val >> e->shift_r) & e->mask;
2460 for (mux = 0; mux < e->max; mux++) {
2461 if (val == e->values[mux])
2462 break;
2463 }
2464 ucontrol->value.enumerated.item[1] = mux;
2465 }
2466
2467 return 0;
2468}
2469EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2470
2471/**
2472 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2473 * @kcontrol: mixer control
2474 * @ucontrol: control element information
2475 *
2476 * Callback to set the value of a double semi enumerated mixer.
2477 *
2478 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2479 * used for handling bitfield coded enumeration for example.
2480 *
2481 * Returns 0 for success.
2482 */
2483int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2484 struct snd_ctl_elem_value *ucontrol)
2485{
2486 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002487 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002488 unsigned int val;
2489 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002490
2491 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2492 return -EINVAL;
2493 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2494 mask = e->mask << e->shift_l;
2495 if (e->shift_l != e->shift_r) {
2496 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2497 return -EINVAL;
2498 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2499 mask |= e->mask << e->shift_r;
2500 }
2501
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002502 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002503}
2504EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2505
2506/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002507 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2508 * @kcontrol: mixer control
2509 * @uinfo: control element information
2510 *
2511 * Callback to provide information about an external enumerated
2512 * single mixer.
2513 *
2514 * Returns 0 for success.
2515 */
2516int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2517 struct snd_ctl_elem_info *uinfo)
2518{
2519 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2520
2521 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2522 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002523 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002524
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002525 if (uinfo->value.enumerated.item > e->max - 1)
2526 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002527 strcpy(uinfo->value.enumerated.name,
Liam Girdwooda8f13d82011-05-18 18:39:21 +01002528 snd_soc_get_enum_text(e, uinfo->value.enumerated.item));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002529 return 0;
2530}
2531EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2532
2533/**
2534 * snd_soc_info_volsw_ext - external single mixer info callback
2535 * @kcontrol: mixer control
2536 * @uinfo: control element information
2537 *
2538 * Callback to provide information about a single external mixer control.
2539 *
2540 * Returns 0 for success.
2541 */
2542int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2543 struct snd_ctl_elem_info *uinfo)
2544{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002545 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002546
Mark Brownfd5dfad2009-04-15 21:37:46 +01002547 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002548 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2549 else
2550 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2551
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002552 uinfo->count = 1;
2553 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002554 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002555 return 0;
2556}
2557EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2558
2559/**
Ben Romberger8f1dc4c2011-09-22 13:51:35 -07002560 * snd_soc_info_multi_ext - external single mixer info callback
2561 * @kcontrol: mixer control
2562 * @uinfo: control element information
2563 *
2564 * Callback to provide information about a single external mixer control.
2565 * that accepts multiple input.
2566 *
2567 * Returns 0 for success.
2568 */
2569int snd_soc_info_multi_ext(struct snd_kcontrol *kcontrol,
2570 struct snd_ctl_elem_info *uinfo)
2571{
2572 struct soc_multi_mixer_control *mc =
2573 (struct soc_multi_mixer_control *)kcontrol->private_value;
2574 int platform_max;
2575
2576 if (!mc->platform_max)
2577 mc->platform_max = mc->max;
2578 platform_max = mc->platform_max;
2579
2580 if (platform_max == 1 && !strnstr(kcontrol->id.name, " Volume", 30))
2581 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2582 else
2583 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2584
2585 uinfo->count = mc->count;
2586 uinfo->value.integer.min = 0;
2587 uinfo->value.integer.max = platform_max;
2588 return 0;
2589}
2590EXPORT_SYMBOL_GPL(snd_soc_info_multi_ext);
2591
2592/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002593 * snd_soc_info_volsw - single mixer info callback
2594 * @kcontrol: mixer control
2595 * @uinfo: control element information
2596 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002597 * Callback to provide information about a single mixer control, or a double
2598 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002599 *
2600 * Returns 0 for success.
2601 */
2602int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2603 struct snd_ctl_elem_info *uinfo)
2604{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002605 struct soc_mixer_control *mc =
2606 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002607 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002608
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002609 if (!mc->platform_max)
2610 mc->platform_max = mc->max;
2611 platform_max = mc->platform_max;
2612
2613 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002614 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2615 else
2616 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2617
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002618 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002619 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002620 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002621 return 0;
2622}
2623EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2624
2625/**
2626 * snd_soc_get_volsw - single mixer get callback
2627 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002628 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002629 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002630 * Callback to get the value of a single mixer control, or a double mixer
2631 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002632 *
2633 * Returns 0 for success.
2634 */
2635int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2636 struct snd_ctl_elem_value *ucontrol)
2637{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002638 struct soc_mixer_control *mc =
2639 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002640 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002641 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002642 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf82008-07-29 10:22:24 -04002643 unsigned int shift = mc->shift;
2644 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002645 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002646 unsigned int mask = (1 << fls(max)) - 1;
2647 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002648
2649 ucontrol->value.integer.value[0] =
2650 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002651 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002652 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002653 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002654
2655 if (snd_soc_volsw_is_stereo(mc)) {
2656 if (reg == reg2)
2657 ucontrol->value.integer.value[1] =
2658 (snd_soc_read(codec, reg) >> rshift) & mask;
2659 else
2660 ucontrol->value.integer.value[1] =
2661 (snd_soc_read(codec, reg2) >> shift) & mask;
2662 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002663 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002664 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002665 }
2666
2667 return 0;
2668}
2669EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2670
2671/**
2672 * snd_soc_put_volsw - single mixer put callback
2673 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002674 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002675 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002676 * Callback to set the value of a single mixer control, or a double mixer
2677 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002678 *
2679 * Returns 0 for success.
2680 */
2681int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2682 struct snd_ctl_elem_value *ucontrol)
2683{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002684 struct soc_mixer_control *mc =
2685 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002686 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002687 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002688 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf82008-07-29 10:22:24 -04002689 unsigned int shift = mc->shift;
2690 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002691 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002692 unsigned int mask = (1 << fls(max)) - 1;
2693 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002694 int err;
2695 bool type_2r = 0;
2696 unsigned int val2 = 0;
2697 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002698
2699 val = (ucontrol->value.integer.value[0] & mask);
2700 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002701 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002702 val_mask = mask << shift;
2703 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002704 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002705 val2 = (ucontrol->value.integer.value[1] & mask);
2706 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002707 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002708 if (reg == reg2) {
2709 val_mask |= mask << rshift;
2710 val |= val2 << rshift;
2711 } else {
2712 val2 = val2 << shift;
2713 type_2r = 1;
2714 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002715 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002716 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002717 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002718 return err;
2719
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002720 if (type_2r)
2721 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2722
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002723 return err;
2724}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002725EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002726
Mark Browne13ac2e2008-05-28 17:58:05 +01002727/**
2728 * snd_soc_info_volsw_s8 - signed mixer info callback
2729 * @kcontrol: mixer control
2730 * @uinfo: control element information
2731 *
2732 * Callback to provide information about a signed mixer control.
2733 *
2734 * Returns 0 for success.
2735 */
2736int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2737 struct snd_ctl_elem_info *uinfo)
2738{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002739 struct soc_mixer_control *mc =
2740 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002741 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002742 int min = mc->min;
Bradley Rubin572c3b42011-07-07 18:23:44 -07002743 unsigned int shift = mc->shift;
2744 unsigned int rshift = mc->rshift;
Mark Browne13ac2e2008-05-28 17:58:05 +01002745
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002746 if (!mc->platform_max)
2747 mc->platform_max = mc->max;
2748 platform_max = mc->platform_max;
2749
Mark Browne13ac2e2008-05-28 17:58:05 +01002750 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Bradley Rubin572c3b42011-07-07 18:23:44 -07002751 uinfo->count = shift == rshift ? 1 : 2;
Mark Browne13ac2e2008-05-28 17:58:05 +01002752 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002753 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002754 return 0;
2755}
2756EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2757
2758/**
2759 * snd_soc_get_volsw_s8 - signed mixer get callback
2760 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002761 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002762 *
2763 * Callback to get the value of a signed mixer control.
2764 *
2765 * Returns 0 for success.
2766 */
2767int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2768 struct snd_ctl_elem_value *ucontrol)
2769{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002770 struct soc_mixer_control *mc =
2771 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002772 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002773 unsigned int reg = mc->reg;
Bradley Rubin572c3b42011-07-07 18:23:44 -07002774 unsigned int shift = mc->shift;
2775 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002776 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002777 int val = snd_soc_read(codec, reg);
2778
2779 ucontrol->value.integer.value[0] =
Bradley Rubin572c3b42011-07-07 18:23:44 -07002780 ((signed char)((val >> shift) & 0xff))-min;
2781 if (shift != rshift)
2782 ucontrol->value.integer.value[1] =
2783 ((signed char)((val >> rshift) & 0xff))-min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002784 return 0;
2785}
2786EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2787
2788/**
2789 * snd_soc_put_volsw_sgn - signed mixer put callback
2790 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002791 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002792 *
2793 * Callback to set the value of a signed mixer control.
2794 *
2795 * Returns 0 for success.
2796 */
2797int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2798 struct snd_ctl_elem_value *ucontrol)
2799{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002800 struct soc_mixer_control *mc =
2801 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002802 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002803 unsigned int reg = mc->reg;
Bradley Rubin572c3b42011-07-07 18:23:44 -07002804 unsigned int shift = mc->shift;
2805 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002806 int min = mc->min;
Bradley Rubin572c3b42011-07-07 18:23:44 -07002807 unsigned int val, val2, val_mask;
Mark Browne13ac2e2008-05-28 17:58:05 +01002808
Bradley Rubin572c3b42011-07-07 18:23:44 -07002809 val = ((ucontrol->value.integer.value[0]+min) & 0xff) << shift;
2810 val_mask = 0xff << shift;
2811 if (shift != rshift) {
2812 val2 = (ucontrol->value.integer.value[1]+min) & 0xff;
2813 val |= val2 << rshift;
2814 val_mask |= 0xff << rshift;
2815 }
Mark Browne13ac2e2008-05-28 17:58:05 +01002816
Bradley Rubin572c3b42011-07-07 18:23:44 -07002817 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002818}
2819EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2820
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002821/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002822 * snd_soc_limit_volume - Set new limit to an existing volume control.
2823 *
2824 * @codec: where to look for the control
2825 * @name: Name of the control
2826 * @max: new maximum limit
2827 *
2828 * Return 0 for success, else error.
2829 */
2830int snd_soc_limit_volume(struct snd_soc_codec *codec,
2831 const char *name, int max)
2832{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002833 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002834 struct snd_kcontrol *kctl;
2835 struct soc_mixer_control *mc;
2836 int found = 0;
2837 int ret = -EINVAL;
2838
2839 /* Sanity check for name and max */
2840 if (unlikely(!name || max <= 0))
2841 return -EINVAL;
2842
2843 list_for_each_entry(kctl, &card->controls, list) {
2844 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2845 found = 1;
2846 break;
2847 }
2848 }
2849 if (found) {
2850 mc = (struct soc_mixer_control *)kctl->private_value;
2851 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002852 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002853 ret = 0;
2854 }
2855 }
2856 return ret;
2857}
2858EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2859
2860/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002861 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2862 * mixer info callback
2863 * @kcontrol: mixer control
2864 * @uinfo: control element information
2865 *
2866 * Returns 0 for success.
2867 */
2868int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2869 struct snd_ctl_elem_info *uinfo)
2870{
2871 struct soc_mixer_control *mc =
2872 (struct soc_mixer_control *)kcontrol->private_value;
2873 int max = mc->max;
2874 int min = mc->min;
2875
2876 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2877 uinfo->count = 2;
2878 uinfo->value.integer.min = 0;
2879 uinfo->value.integer.max = max-min;
2880
2881 return 0;
2882}
2883EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2884
2885/**
2886 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2887 * mixer get callback
2888 * @kcontrol: mixer control
2889 * @uinfo: control element information
2890 *
2891 * Returns 0 for success.
2892 */
2893int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2894 struct snd_ctl_elem_value *ucontrol)
2895{
2896 struct soc_mixer_control *mc =
2897 (struct soc_mixer_control *)kcontrol->private_value;
2898 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2899 unsigned int mask = (1<<mc->shift)-1;
2900 int min = mc->min;
2901 int val = snd_soc_read(codec, mc->reg) & mask;
2902 int valr = snd_soc_read(codec, mc->rreg) & mask;
2903
Stuart Longland20630c72010-06-18 12:56:10 +10002904 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2905 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002906 return 0;
2907}
2908EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2909
2910/**
2911 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2912 * mixer put callback
2913 * @kcontrol: mixer control
2914 * @uinfo: control element information
2915 *
2916 * Returns 0 for success.
2917 */
2918int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2919 struct snd_ctl_elem_value *ucontrol)
2920{
2921 struct soc_mixer_control *mc =
2922 (struct soc_mixer_control *)kcontrol->private_value;
2923 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2924 unsigned int mask = (1<<mc->shift)-1;
2925 int min = mc->min;
2926 int ret;
2927 unsigned int val, valr, oval, ovalr;
2928
2929 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2930 val &= mask;
2931 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2932 valr &= mask;
2933
2934 oval = snd_soc_read(codec, mc->reg) & mask;
2935 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2936
2937 ret = 0;
2938 if (oval != val) {
2939 ret = snd_soc_write(codec, mc->reg, val);
2940 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002941 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002942 }
2943 if (ovalr != valr) {
2944 ret = snd_soc_write(codec, mc->rreg, valr);
2945 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002946 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002947 }
2948
2949 return 0;
2950}
2951EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2952
2953/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002954 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2955 * @dai: DAI
2956 * @clk_id: DAI specific clock ID
2957 * @freq: new clock frequency in Hz
2958 * @dir: new clock direction - input/output.
2959 *
2960 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2961 */
2962int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2963 unsigned int freq, int dir)
2964{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002965 if (dai->driver && dai->driver->ops->set_sysclk)
2966 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002967 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002968 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00002969 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002970 else
2971 return -EINVAL;
2972}
2973EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2974
2975/**
Mark Brownec4ee522011-03-07 20:58:11 +00002976 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2977 * @codec: CODEC
2978 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01002979 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00002980 * @freq: new clock frequency in Hz
2981 * @dir: new clock direction - input/output.
2982 *
2983 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2984 */
2985int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01002986 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00002987{
2988 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002989 return codec->driver->set_sysclk(codec, clk_id, source,
2990 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002991 else
2992 return -EINVAL;
2993}
2994EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2995
2996/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002997 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2998 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002999 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003000 * @div: new clock divisor.
3001 *
3002 * Configures the clock dividers. This is used to derive the best DAI bit and
3003 * frame clocks from the system or master clock. It's best to set the DAI bit
3004 * and frame clocks as low as possible to save system power.
3005 */
3006int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3007 int div_id, int div)
3008{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003009 if (dai->driver && dai->driver->ops->set_clkdiv)
3010 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003011 else
3012 return -EINVAL;
3013}
3014EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3015
3016/**
3017 * snd_soc_dai_set_pll - configure DAI PLL.
3018 * @dai: DAI
3019 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003020 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003021 * @freq_in: PLL input clock frequency in Hz
3022 * @freq_out: requested PLL output clock frequency in Hz
3023 *
3024 * Configures and enables PLL to generate output clock based on input clock.
3025 */
Mark Brown85488032009-09-05 18:52:16 +01003026int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3027 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003028{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003029 if (dai->driver && dai->driver->ops->set_pll)
3030 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003031 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003032 else if (dai->codec && dai->codec->driver->set_pll)
3033 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3034 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003035 else
3036 return -EINVAL;
3037}
3038EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3039
Mark Brownec4ee522011-03-07 20:58:11 +00003040/*
3041 * snd_soc_codec_set_pll - configure codec PLL.
3042 * @codec: CODEC
3043 * @pll_id: DAI specific PLL ID
3044 * @source: DAI specific source for the PLL
3045 * @freq_in: PLL input clock frequency in Hz
3046 * @freq_out: requested PLL output clock frequency in Hz
3047 *
3048 * Configures and enables PLL to generate output clock based on input clock.
3049 */
3050int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3051 unsigned int freq_in, unsigned int freq_out)
3052{
3053 if (codec->driver->set_pll)
3054 return codec->driver->set_pll(codec, pll_id, source,
3055 freq_in, freq_out);
3056 else
3057 return -EINVAL;
3058}
3059EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3060
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003061/**
3062 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3063 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003064 * @fmt: SND_SOC_DAIFMT_ format value.
3065 *
3066 * Configures the DAI hardware format and clocking.
3067 */
3068int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3069{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003070 if (dai->driver && dai->driver->ops->set_fmt)
3071 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003072 else
3073 return -EINVAL;
3074}
3075EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3076
3077/**
3078 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3079 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003080 * @tx_mask: bitmask representing active TX slots.
3081 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003082 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003083 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003084 *
3085 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3086 * specific.
3087 */
3088int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003089 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003090{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003091 if (dai->driver && dai->driver->ops->set_tdm_slot)
3092 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003093 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003094 else
3095 return -EINVAL;
3096}
3097EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3098
3099/**
Barry Song472df3c2009-09-12 01:16:29 +08003100 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3101 * @dai: DAI
3102 * @tx_num: how many TX channels
3103 * @tx_slot: pointer to an array which imply the TX slot number channel
3104 * 0~num-1 uses
3105 * @rx_num: how many RX channels
3106 * @rx_slot: pointer to an array which imply the RX slot number channel
3107 * 0~num-1 uses
3108 *
3109 * configure the relationship between channel number and TDM slot number.
3110 */
3111int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3112 unsigned int tx_num, unsigned int *tx_slot,
3113 unsigned int rx_num, unsigned int *rx_slot)
3114{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003115 if (dai->driver && dai->driver->ops->set_channel_map)
3116 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003117 rx_num, rx_slot);
3118 else
3119 return -EINVAL;
3120}
3121EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3122
3123/**
Bharath Ramachandramurthy9c79f132011-11-28 11:18:57 -08003124 * snd_soc_dai_get_channel_map - configure DAI audio channel map
3125 * @dai: DAI
3126 * @tx_num: how many TX channels
3127 * @tx_slot: pointer to an array which imply the TX slot number channel
3128 * 0~num-1 uses
3129 * @rx_num: how many RX channels
3130 * @rx_slot: pointer to an array which imply the RX slot number channel
3131 * 0~num-1 uses
3132 *
3133 * configure the relationship between channel number and TDM slot number.
3134 */
3135int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
3136 unsigned int *tx_num, unsigned int *tx_slot,
3137 unsigned int *rx_num, unsigned int *rx_slot)
3138{
3139 if (dai->driver && dai->driver->ops->get_channel_map)
3140 return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
3141 rx_num, rx_slot);
3142 else
3143 return -EINVAL;
3144}
3145EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
3146/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003147 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3148 * @dai: DAI
3149 * @tristate: tristate enable
3150 *
3151 * Tristates the DAI so that others can use it.
3152 */
3153int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3154{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003155 if (dai->driver && dai->driver->ops->set_tristate)
3156 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003157 else
3158 return -EINVAL;
3159}
3160EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3161
3162/**
3163 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3164 * @dai: DAI
3165 * @mute: mute enable
3166 *
3167 * Mutes the DAI DAC.
3168 */
3169int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3170{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003171 if (dai->driver && dai->driver->ops->digital_mute)
3172 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003173 else
3174 return -EINVAL;
3175}
3176EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3177
Mark Brownc5af3a22008-11-28 13:29:45 +00003178/**
3179 * snd_soc_register_card - Register a card with the ASoC core
3180 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003181 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003182 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003183 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303184int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003185{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003186 int i;
Liam Girdwood13e13a32011-01-31 21:30:52 +00003187 int ret = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003188
Mark Brownc5af3a22008-11-28 13:29:45 +00003189 if (!card->name || !card->dev)
3190 return -EINVAL;
3191
Stephen Warren5a504962011-12-21 10:40:59 -07003192 for (i = 0; i < card->num_links; i++) {
3193 struct snd_soc_dai_link *link = &card->dai_link[i];
3194
3195 /*
3196 * Codec must be specified by 1 of name or OF node,
3197 * not both or neither.
3198 */
3199 if (!!link->codec_name == !!link->codec_of_node) {
3200 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003201 "Neither/both codec name/of_node are set for %s\n",
3202 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003203 return -EINVAL;
3204 }
3205
3206 /*
3207 * Platform may be specified by either name or OF node, but
3208 * can be left unspecified, and a dummy platform will be used.
3209 */
3210 if (link->platform_name && link->platform_of_node) {
3211 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003212 "Both platform name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003213 return -EINVAL;
3214 }
3215
3216 /*
3217 * CPU DAI must be specified by 1 of name or OF node,
3218 * not both or neither.
3219 */
3220 if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
3221 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003222 "Neither/both cpu_dai name/of_node are set for %s\n",
3223 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003224 return -EINVAL;
3225 }
3226 }
3227
Mark Browned77cc12011-05-03 18:25:34 +01003228 dev_set_drvdata(card->dev, card);
3229
Stephen Warren111c6412011-01-28 14:26:35 -07003230 snd_soc_initialize_card_lists(card);
3231
Vinod Koul150dd2f2011-01-13 22:48:32 +05303232 soc_init_card_debugfs(card);
3233
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003234 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
3235 (card->num_links + card->num_aux_devs),
3236 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003237 if (card->rtd == NULL)
3238 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003239 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003240 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003241
3242 for (i = 0; i < card->num_links; i++)
3243 card->rtd[i].dai_link = &card->dai_link[i];
3244
Mark Brownc5af3a22008-11-28 13:29:45 +00003245 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003246 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003247 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003248 mutex_init(&card->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003249 mutex_init(&card->dpcm_mutex);
Sriranjan Srikantamfd7f1ad2012-08-14 17:42:06 -07003250 mutex_init(&card->dapm_power_mutex);
Liam Girdwood6acab8c2012-03-07 10:38:26 +00003251 mutex_init(&card->dapm_mutex);
Mark Brown87ba7da2012-03-14 21:18:39 +00003252 ret = snd_soc_instantiate_card(card);
Phani Kumar Uppalapati6ebe2332013-09-26 14:41:27 -07003253 if (ret != 0) {
Mark Brown87ba7da2012-03-14 21:18:39 +00003254 soc_cleanup_card_debugfs(card);
Phani Kumar Uppalapati6ebe2332013-09-26 14:41:27 -07003255 if (card->rtd)
3256 kfree(card->rtd);
3257 }
Mark Brownc5af3a22008-11-28 13:29:45 +00003258
Mark Brownc5af3a22008-11-28 13:29:45 +00003259
Liam Girdwood13e13a32011-01-31 21:30:52 +00003260 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003261}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303262EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003263
3264/**
3265 * snd_soc_unregister_card - Unregister a card with the ASoC core
3266 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003267 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003268 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003269 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303270int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003271{
Vinod Koulb0e26482011-01-13 22:48:02 +05303272 if (card->instantiated)
3273 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003274 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3275
3276 return 0;
3277}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303278EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003279
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003280/*
3281 * Simplify DAI link configuration by removing ".-1" from device names
3282 * and sanitizing names.
3283 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003284static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003285{
3286 char *found, name[NAME_SIZE];
3287 int id1, id2;
3288
3289 if (dev_name(dev) == NULL)
3290 return NULL;
3291
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003292 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003293
3294 /* are we a "%s.%d" name (platform and SPI components) */
3295 found = strstr(name, dev->driver->name);
3296 if (found) {
3297 /* get ID */
3298 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3299
3300 /* discard ID from name if ID == -1 */
3301 if (*id == -1)
3302 found[strlen(dev->driver->name)] = '\0';
3303 }
3304
3305 } else {
3306 /* I2C component devices are named "bus-addr" */
3307 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3308 char tmp[NAME_SIZE];
3309
3310 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003311 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003312
3313 /* sanitize component name for DAI link creation */
3314 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003315 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003316 } else
3317 *id = 0;
3318 }
3319
3320 return kstrdup(name, GFP_KERNEL);
3321}
3322
3323/*
3324 * Simplify DAI link naming for single devices with multiple DAIs by removing
3325 * any ".-1" and using the DAI name (instead of device name).
3326 */
3327static inline char *fmt_multiple_name(struct device *dev,
3328 struct snd_soc_dai_driver *dai_drv)
3329{
3330 if (dai_drv->name == NULL) {
3331 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3332 dev_name(dev));
3333 return NULL;
3334 }
3335
3336 return kstrdup(dai_drv->name, GFP_KERNEL);
3337}
3338
Mark Brown91151712008-11-30 23:31:24 +00003339/**
3340 * snd_soc_register_dai - Register a DAI with the ASoC core
3341 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003342 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003343 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003344int snd_soc_register_dai(struct device *dev,
3345 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003346{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003347 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003348
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003349 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003350
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003351 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3352 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003353 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003354
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003355 /* create DAI component name */
3356 dai->name = fmt_single_name(dev, &dai->id);
3357 if (dai->name == NULL) {
3358 kfree(dai);
3359 return -ENOMEM;
3360 }
3361
3362 dai->dev = dev;
3363 dai->driver = dai_drv;
3364 if (!dai->driver->ops)
3365 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003366
3367 mutex_lock(&client_mutex);
3368 list_add(&dai->list, &dai_list);
3369 mutex_unlock(&client_mutex);
3370
3371 pr_debug("Registered DAI '%s'\n", dai->name);
3372
3373 return 0;
3374}
3375EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3376
3377/**
3378 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3379 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003380 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003381 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003382void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003383{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003384 struct snd_soc_dai *dai;
3385
3386 list_for_each_entry(dai, &dai_list, list) {
3387 if (dev == dai->dev)
3388 goto found;
3389 }
3390 return;
3391
3392found:
Mark Brown91151712008-11-30 23:31:24 +00003393 mutex_lock(&client_mutex);
3394 list_del(&dai->list);
3395 mutex_unlock(&client_mutex);
3396
3397 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003398 kfree(dai->name);
3399 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003400}
3401EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3402
3403/**
3404 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3405 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003406 * @dai: Array of DAIs to register
3407 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003408 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003409int snd_soc_register_dais(struct device *dev,
3410 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003411{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003412 struct snd_soc_dai *dai;
3413 int i, ret = 0;
3414
Liam Girdwood720ffa42010-08-18 00:30:30 +01003415 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003416
3417 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003418
3419 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003420 if (dai == NULL) {
3421 ret = -ENOMEM;
3422 goto err;
3423 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003424
3425 /* create DAI component name */
3426 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3427 if (dai->name == NULL) {
3428 kfree(dai);
3429 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003430 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003431 }
3432
3433 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003434 dai->driver = &dai_drv[i];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003435
Mark Brown0f9141c2010-10-12 15:43:21 +01003436 if (dai->driver->id)
3437 dai->id = dai->driver->id;
3438 else
3439 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003440 if (!dai->driver->ops)
3441 dai->driver->ops = &null_dai_ops;
3442
3443 mutex_lock(&client_mutex);
3444 list_add(&dai->list, &dai_list);
3445 mutex_unlock(&client_mutex);
3446
3447 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003448 }
3449
3450 return 0;
3451
3452err:
3453 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003454 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003455
3456 return ret;
3457}
3458EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3459
3460/**
3461 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3462 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003463 * @dai: Array of DAIs to unregister
3464 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003465 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003466void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003467{
3468 int i;
3469
3470 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003471 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003472}
3473EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3474
Mark Brown12a48a8c2008-12-03 19:40:30 +00003475/**
3476 * snd_soc_register_platform - Register a platform with the ASoC core
3477 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003478 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003479 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003480int snd_soc_register_platform(struct device *dev,
3481 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003482{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003483 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003484
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003485 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3486
3487 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3488 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003489 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003490
3491 /* create platform component name */
3492 platform->name = fmt_single_name(dev, &platform->id);
3493 if (platform->name == NULL) {
3494 kfree(platform);
3495 return -ENOMEM;
3496 }
3497
3498 platform->dev = dev;
3499 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003500 platform->dapm.dev = dev;
3501 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003502 platform->dapm.stream_event = platform_drv->stream_event;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003503
3504 mutex_lock(&client_mutex);
3505 list_add(&platform->list, &platform_list);
3506 mutex_unlock(&client_mutex);
3507
3508 pr_debug("Registered platform '%s'\n", platform->name);
3509
3510 return 0;
3511}
3512EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3513
3514/**
3515 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3516 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003517 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003518 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003519void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003520{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003521 struct snd_soc_platform *platform;
3522
3523 list_for_each_entry(platform, &platform_list, list) {
3524 if (dev == platform->dev)
3525 goto found;
3526 }
3527 return;
3528
3529found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003530 mutex_lock(&client_mutex);
3531 list_del(&platform->list);
3532 mutex_unlock(&client_mutex);
3533
3534 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003535 kfree(platform->name);
3536 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003537}
3538EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3539
Mark Brown151ab222009-05-09 16:22:58 +01003540static u64 codec_format_map[] = {
3541 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3542 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3543 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3544 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3545 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3546 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3547 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3548 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3549 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3550 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3551 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3552 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3553 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3554 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3555 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3556 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3557};
3558
3559/* Fix up the DAI formats for endianness: codecs don't actually see
3560 * the endianness of the data but we're using the CPU format
3561 * definitions which do need to include endianness so we ensure that
3562 * codec DAIs always have both big and little endian variants set.
3563 */
3564static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3565{
3566 int i;
3567
3568 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3569 if (stream->formats & codec_format_map[i])
3570 stream->formats |= codec_format_map[i];
3571}
3572
Mark Brown0d0cf002008-12-10 14:32:45 +00003573/**
Joonwoo Park6fb64392013-08-07 18:59:09 -07003574 * snd_soc_card_change_online_state - Mark if soc card is online/offline
3575 *
3576 * @soc_card : soc_card to mark
3577 */
3578void snd_soc_card_change_online_state(struct snd_soc_card *soc_card, int online)
3579{
3580 snd_card_change_online_state(soc_card->snd_card, online);
3581}
3582EXPORT_SYMBOL(snd_soc_card_change_online_state);
3583
3584/**
Mark Brown0d0cf002008-12-10 14:32:45 +00003585 * snd_soc_register_codec - Register a codec with the ASoC core
3586 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003587 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003588 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003589int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003590 const struct snd_soc_codec_driver *codec_drv,
3591 struct snd_soc_dai_driver *dai_drv,
3592 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003593{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003594 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003595 struct snd_soc_codec *codec;
3596 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003597
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003598 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003599
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003600 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3601 if (codec == NULL)
3602 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003603
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003604 /* create CODEC component name */
3605 codec->name = fmt_single_name(dev, &codec->id);
3606 if (codec->name == NULL) {
3607 kfree(codec);
3608 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003609 }
3610
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003611 if (codec_drv->compress_type)
3612 codec->compress_type = codec_drv->compress_type;
3613 else
3614 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3615
Mark Brownc3acec22010-12-02 16:15:29 +00003616 codec->write = codec_drv->write;
3617 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003618 codec->volatile_register = codec_drv->volatile_register;
3619 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003620 codec->writable_register = codec_drv->writable_register;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003621 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3622 codec->dapm.dev = dev;
3623 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003624 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003625 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003626 codec->dev = dev;
3627 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003628 codec->num_dai = num_dai;
3629 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003630
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003631 /* allocate CODEC register cache */
3632 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003633 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003634 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003635 /* it is necessary to make a copy of the default register cache
3636 * because in the case of using a compression type that requires
3637 * the default register cache to be marked as __devinitconst the
3638 * kernel might have freed the array by the time we initialize
3639 * the cache.
3640 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003641 if (codec_drv->reg_cache_default) {
3642 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3643 reg_size, GFP_KERNEL);
3644 if (!codec->reg_def_copy) {
3645 ret = -ENOMEM;
3646 goto fail;
3647 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003648 }
3649 }
3650
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003651 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3652 if (!codec->volatile_register)
3653 codec->volatile_register = snd_soc_default_volatile_register;
3654 if (!codec->readable_register)
3655 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003656 if (!codec->writable_register)
3657 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003658 }
3659
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003660 for (i = 0; i < num_dai; i++) {
3661 fixup_codec_formats(&dai_drv[i].playback);
3662 fixup_codec_formats(&dai_drv[i].capture);
3663 }
3664
Mark Brown26b01cc2010-08-18 20:20:55 +01003665 /* register any DAIs */
3666 if (num_dai) {
3667 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3668 if (ret < 0)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003669 goto fail;
Mark Brown26b01cc2010-08-18 20:20:55 +01003670 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003671
Mark Brown0d0cf002008-12-10 14:32:45 +00003672 mutex_lock(&client_mutex);
3673 list_add(&codec->list, &codec_list);
Mark Brown0d0cf002008-12-10 14:32:45 +00003674 mutex_unlock(&client_mutex);
3675
3676 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003677 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003678
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003679fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003680 kfree(codec->reg_def_copy);
3681 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003682 kfree(codec->name);
3683 kfree(codec);
3684 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003685}
3686EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3687
3688/**
3689 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3690 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003691 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003692 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003693void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003694{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003695 struct snd_soc_codec *codec;
3696 int i;
3697
3698 list_for_each_entry(codec, &codec_list, list) {
3699 if (dev == codec->dev)
3700 goto found;
3701 }
3702 return;
3703
3704found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003705 if (codec->num_dai)
3706 for (i = 0; i < codec->num_dai; i++)
3707 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003708
Mark Brown0d0cf002008-12-10 14:32:45 +00003709 mutex_lock(&client_mutex);
3710 list_del(&codec->list);
3711 mutex_unlock(&client_mutex);
3712
3713 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003714
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003715 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003716 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003717 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003718 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003719}
3720EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3721
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003722/* Retrieve a card's name from device tree */
3723int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3724 const char *propname)
3725{
3726 struct device_node *np = card->dev->of_node;
3727 int ret;
3728
3729 ret = of_property_read_string_index(np, propname, 0, &card->name);
3730 /*
3731 * EINVAL means the property does not exist. This is fine providing
3732 * card->name was previously set, which is checked later in
3733 * snd_soc_register_card.
3734 */
3735 if (ret < 0 && ret != -EINVAL) {
3736 dev_err(card->dev,
3737 "Property '%s' could not be read: %d\n",
3738 propname, ret);
3739 return ret;
3740 }
3741
3742 return 0;
3743}
3744EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3745
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003746int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3747 const char *propname)
3748{
3749 struct device_node *np = card->dev->of_node;
3750 int num_routes;
3751 struct snd_soc_dapm_route *routes;
3752 int i, ret;
3753
3754 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003755 if (num_routes < 0 || num_routes & 1) {
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003756 dev_err(card->dev,
Richard Zhaoc34ce322012-04-24 15:24:43 +08003757 "Property '%s' does not exist or its length is not even\n",
3758 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003759 return -EINVAL;
3760 }
3761 num_routes /= 2;
3762 if (!num_routes) {
3763 dev_err(card->dev,
3764 "Property '%s's length is zero\n",
3765 propname);
3766 return -EINVAL;
3767 }
3768
3769 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3770 GFP_KERNEL);
3771 if (!routes) {
3772 dev_err(card->dev,
3773 "Could not allocate DAPM route table\n");
3774 return -EINVAL;
3775 }
3776
3777 for (i = 0; i < num_routes; i++) {
3778 ret = of_property_read_string_index(np, propname,
3779 2 * i, &routes[i].sink);
3780 if (ret) {
3781 dev_err(card->dev,
3782 "Property '%s' index %d could not be read: %d\n",
3783 propname, 2 * i, ret);
3784 return -EINVAL;
3785 }
3786 ret = of_property_read_string_index(np, propname,
3787 (2 * i) + 1, &routes[i].source);
3788 if (ret) {
3789 dev_err(card->dev,
3790 "Property '%s' index %d could not be read: %d\n",
3791 propname, (2 * i) + 1, ret);
3792 return -EINVAL;
3793 }
3794 }
3795
3796 card->num_dapm_routes = num_routes;
3797 card->dapm_routes = routes;
3798
3799 return 0;
3800}
3801EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3802
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003803static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003804{
Mark Brown384c89e2008-12-03 17:34:03 +00003805#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003806 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3807 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Mark Brown384c89e2008-12-03 17:34:03 +00003808 printk(KERN_WARNING
3809 "ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00003810 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00003811 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003812
Mark Brown8a9dab12011-01-10 22:25:21 +00003813 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01003814 &codec_list_fops))
3815 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3816
Mark Brown8a9dab12011-01-10 22:25:21 +00003817 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01003818 &dai_list_fops))
3819 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003820
Mark Brown8a9dab12011-01-10 22:25:21 +00003821 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01003822 &platform_list_fops))
3823 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003824#endif
3825
Mark Brownfb257892011-04-28 10:57:54 +01003826 snd_soc_util_init();
3827
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003828 return platform_driver_register(&soc_driver);
3829}
Mark Brown4abe8e12010-10-12 17:41:03 +01003830module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003831
Mark Brown7d8c16a2008-11-30 22:11:24 +00003832static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003833{
Mark Brownfb257892011-04-28 10:57:54 +01003834 snd_soc_util_exit();
3835
Mark Brown384c89e2008-12-03 17:34:03 +00003836#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003837 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00003838#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003839 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003840}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003841module_exit(snd_soc_exit);
3842
3843/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003844MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003845MODULE_DESCRIPTION("ALSA SoC Core");
3846MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003847MODULE_ALIAS("platform:soc-audio");