blob: 79016b5753989801a908a983f203b3ab8b61d364 [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);
58static LIST_HEAD(card_list);
Mark Brown91151712008-11-30 23:31:24 +000059static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000060static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000061static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000062
Steve Mucklef132c6c2012-06-06 18:30:57 -070063int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
64int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd);
65int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute);
66int soc_dpcm_be_ac97_cpu_dai_suspend(struct snd_soc_pcm_runtime *fe);
67int soc_dpcm_be_ac97_cpu_dai_resume(struct snd_soc_pcm_runtime *fe);
68int soc_dpcm_be_cpu_dai_resume(struct snd_soc_pcm_runtime *fe);
69int soc_dpcm_be_cpu_dai_suspend(struct snd_soc_pcm_runtime *fe);
70int soc_dpcm_be_platform_suspend(struct snd_soc_pcm_runtime *fe);
71int soc_dpcm_be_platform_resume(struct snd_soc_pcm_runtime *fe);
Mark Brownc5af3a22008-11-28 13:29:45 +000072
Frank Mandarinodb2a4162006-10-06 18:31:09 +020073/*
74 * This is a timeout to do a DAPM powerdown after a stream is closed().
75 * It can be used to eliminate pops between different playback streams, e.g.
76 * between two audio tracks.
77 */
Patrick Lai95c1e672012-04-13 21:52:20 -070078static int pmdown_time;
Frank Mandarinodb2a4162006-10-06 18:31:09 +020079module_param(pmdown_time, int, 0);
80MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
81
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000082/* returns the minimum number of bytes needed to represent
83 * a particular given value */
84static int min_bytes_needed(unsigned long val)
85{
86 int c = 0;
87 int i;
88
89 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
90 if (val & (1UL << i))
91 break;
92 c = (sizeof val * 8) - c;
93 if (!c || (c % 8))
94 c = (c + 8) / 8;
95 else
96 c /= 8;
97 return c;
98}
99
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000100/* fill buf which is 'len' bytes with a formatted
101 * string of the form 'reg: value\n' */
102static int format_register_str(struct snd_soc_codec *codec,
103 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +0000104{
Stephen Warren00b317a2011-04-01 14:50:44 -0600105 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
106 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000107 int ret;
108 char tmpbuf[len + 1];
109 char regbuf[regsize + 1];
110
111 /* since tmpbuf is allocated on the stack, warn the callers if they
112 * try to abuse this function */
113 WARN_ON(len > 63);
114
115 /* +2 for ': ' and + 1 for '\n' */
116 if (wordsize + regsize + 2 + 1 != len)
117 return -EINVAL;
118
Mark Brown25032c12011-08-01 13:52:48 +0900119 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000120 if (ret < 0) {
121 memset(regbuf, 'X', regsize);
122 regbuf[regsize] = '\0';
123 } else {
124 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
125 }
126
127 /* prepare the buffer */
128 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
129 /* copy it back to the caller without the '\0' */
130 memcpy(buf, tmpbuf, len);
131
132 return 0;
133}
134
135/* codec register dump */
136static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
137 size_t count, loff_t pos)
138{
139 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000140 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000141 int len;
142 size_t total = 0;
143 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000144
Stephen Warren00b317a2011-04-01 14:50:44 -0600145 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
146 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000147
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000148 len = wordsize + regsize + 2 + 1;
149
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000150 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000151 return 0;
152
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000153 if (codec->driver->reg_cache_step)
154 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000155
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000156 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausenb92d1502011-08-27 18:24:14 +0200157 if (!snd_soc_codec_readable_register(codec, i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000158 continue;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000159 if (codec->driver->display_register) {
160 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000161 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100162 } else {
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000163 /* only support larger than PAGE_SIZE bytes debugfs
164 * entries for the default case */
165 if (p >= pos) {
166 if (total + len >= count - 1)
167 break;
168 format_register_str(codec, i, buf + total, len);
169 total += len;
170 }
171 p += len;
Mark Brown5164d742010-07-14 16:14:33 +0100172 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000173 }
174
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000175 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000176
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000177 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000178}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000179
Mark Brown2624d5f2009-11-03 21:56:13 +0000180static ssize_t codec_reg_show(struct device *dev,
181 struct device_attribute *attr, char *buf)
182{
Mark Brown36ae1a92012-01-06 17:12:45 -0800183 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000184
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000185 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000186}
187
188static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
189
Mark Browndbe21402010-02-12 11:37:24 +0000190static ssize_t pmdown_time_show(struct device *dev,
191 struct device_attribute *attr, char *buf)
192{
Mark Brown36ae1a92012-01-06 17:12:45 -0800193 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000194
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000195 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000196}
197
198static ssize_t pmdown_time_set(struct device *dev,
199 struct device_attribute *attr,
200 const char *buf, size_t count)
201{
Mark Brown36ae1a92012-01-06 17:12:45 -0800202 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700203 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000204
Mark Brownc593b522010-10-27 20:11:17 -0700205 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
206 if (ret)
207 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000208
209 return count;
210}
211
212static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
213
Mark Brown2624d5f2009-11-03 21:56:13 +0000214#ifdef CONFIG_DEBUG_FS
215static int codec_reg_open_file(struct inode *inode, struct file *file)
216{
217 file->private_data = inode->i_private;
218 return 0;
219}
220
221static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000222 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000223{
224 ssize_t ret;
225 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000226 char *buf;
227
228 if (*ppos < 0 || !count)
229 return -EINVAL;
230
231 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000232 if (!buf)
233 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000234
235 ret = soc_codec_reg_show(codec, buf, count, *ppos);
236 if (ret >= 0) {
237 if (copy_to_user(user_buf, buf, ret)) {
238 kfree(buf);
239 return -EFAULT;
240 }
241 *ppos += ret;
242 }
243
Mark Brown2624d5f2009-11-03 21:56:13 +0000244 kfree(buf);
245 return ret;
246}
247
248static ssize_t codec_reg_write_file(struct file *file,
249 const char __user *user_buf, size_t count, loff_t *ppos)
250{
251 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700252 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000253 char *start = buf;
254 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000255 struct snd_soc_codec *codec = file->private_data;
256
257 buf_size = min(count, (sizeof(buf)-1));
258 if (copy_from_user(buf, user_buf, buf_size))
259 return -EFAULT;
260 buf[buf_size] = 0;
261
Mark Brown2624d5f2009-11-03 21:56:13 +0000262 while (*start == ' ')
263 start++;
264 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000265 while (*start == ' ')
266 start++;
267 if (strict_strtoul(start, 16, &value))
268 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000269
270 /* Userspace has been fiddling around behind the kernel's back */
271 add_taint(TAINT_USER);
272
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000273 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000274 return buf_size;
275}
276
277static const struct file_operations codec_reg_fops = {
278 .open = codec_reg_open_file,
279 .read = codec_reg_read_file,
280 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200281 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000282};
283
284static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
285{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200286 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
287
288 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
289 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000290 if (!codec->debugfs_codec_root) {
291 printk(KERN_WARNING
292 "ASoC: Failed to create codec debugfs directory\n");
293 return;
294 }
295
Mark Brownaaee8ef2011-01-26 20:53:50 +0000296 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
297 &codec->cache_sync);
298 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
299 &codec->cache_only);
300
Mark Brown2624d5f2009-11-03 21:56:13 +0000301 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
302 codec->debugfs_codec_root,
303 codec, &codec_reg_fops);
304 if (!codec->debugfs_reg)
305 printk(KERN_WARNING
306 "ASoC: Failed to create codec register debugfs file\n");
307
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200308 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000309}
310
311static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
312{
313 debugfs_remove_recursive(codec->debugfs_codec_root);
314}
315
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000316static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
317{
318 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
319
320 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
321 debugfs_card_root);
322 if (!platform->debugfs_platform_root) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700323 printk(KERN_WARNING
324 "ASoC: Failed to create platform debugfs directory\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000325 return;
326 }
327
Steve Mucklef132c6c2012-06-06 18:30:57 -0700328 snd_soc_dapm_debugfs_init(&platform->dapm, platform->debugfs_platform_root);
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000329}
330
331static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
332{
333 debugfs_remove_recursive(platform->debugfs_platform_root);
334}
335
Mark Brownc3c5a192010-09-15 18:15:14 +0100336static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
337 size_t count, loff_t *ppos)
338{
339 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100340 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100341 struct snd_soc_codec *codec;
342
343 if (!buf)
344 return -ENOMEM;
345
Mark Brown2b194f9d2010-10-13 10:52:16 +0100346 list_for_each_entry(codec, &codec_list, list) {
347 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
348 codec->name);
349 if (len >= 0)
350 ret += len;
351 if (ret > PAGE_SIZE) {
352 ret = PAGE_SIZE;
353 break;
354 }
355 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100356
357 if (ret >= 0)
358 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
359
360 kfree(buf);
361
362 return ret;
363}
364
365static const struct file_operations codec_list_fops = {
366 .read = codec_list_read_file,
367 .llseek = default_llseek,/* read accesses f_pos */
368};
369
Mark Brownf3208782010-09-15 18:19:07 +0100370static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
371 size_t count, loff_t *ppos)
372{
373 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100374 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100375 struct snd_soc_dai *dai;
376
377 if (!buf)
378 return -ENOMEM;
379
Mark Brown2b194f9d2010-10-13 10:52:16 +0100380 list_for_each_entry(dai, &dai_list, list) {
381 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
382 if (len >= 0)
383 ret += len;
384 if (ret > PAGE_SIZE) {
385 ret = PAGE_SIZE;
386 break;
387 }
388 }
Mark Brownf3208782010-09-15 18:19:07 +0100389
Mark Brown2b194f9d2010-10-13 10:52:16 +0100390 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100391
392 kfree(buf);
393
394 return ret;
395}
396
397static const struct file_operations dai_list_fops = {
398 .read = dai_list_read_file,
399 .llseek = default_llseek,/* read accesses f_pos */
400};
401
Mark Brown19c7ac22010-09-15 18:22:40 +0100402static ssize_t platform_list_read_file(struct file *file,
403 char __user *user_buf,
404 size_t count, loff_t *ppos)
405{
406 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100407 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100408 struct snd_soc_platform *platform;
409
410 if (!buf)
411 return -ENOMEM;
412
Mark Brown2b194f9d2010-10-13 10:52:16 +0100413 list_for_each_entry(platform, &platform_list, list) {
414 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
415 platform->name);
416 if (len >= 0)
417 ret += len;
418 if (ret > PAGE_SIZE) {
419 ret = PAGE_SIZE;
420 break;
421 }
422 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100423
Mark Brown2b194f9d2010-10-13 10:52:16 +0100424 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100425
426 kfree(buf);
427
428 return ret;
429}
430
431static const struct file_operations platform_list_fops = {
432 .read = platform_list_read_file,
433 .llseek = default_llseek,/* read accesses f_pos */
434};
435
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200436static void soc_init_card_debugfs(struct snd_soc_card *card)
437{
438 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000439 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200440 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200441 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100442 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200443 return;
444 }
445
446 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
447 card->debugfs_card_root,
448 &card->pop_time);
449 if (!card->debugfs_pop_time)
450 dev_warn(card->dev,
451 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200452}
453
454static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
455{
456 debugfs_remove_recursive(card->debugfs_card_root);
457}
458
Mark Brown2624d5f2009-11-03 21:56:13 +0000459#else
460
461static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
462{
463}
464
465static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
466{
467}
Axel Linb95fccb2010-11-09 17:06:44 +0800468
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000469static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
470{
471}
472
473static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
474{
475}
476
Axel Linb95fccb2010-11-09 17:06:44 +0800477static inline void soc_init_card_debugfs(struct snd_soc_card *card)
478{
479}
480
481static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
482{
483}
Mark Brown2624d5f2009-11-03 21:56:13 +0000484#endif
485
Steve Mucklef132c6c2012-06-06 18:30:57 -0700486struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
487 const char *dai_link, int stream)
488{
489 int i;
490
491 for (i = 0; i < card->num_links; i++) {
492 if (card->rtd[i].dai_link->no_pcm &&
493 !strcmp(card->rtd[i].dai_link->name, dai_link))
494 return card->rtd[i].pcm->streams[stream].substream;
495 }
496 dev_dbg(card->dev, "failed to find dai link %s\n", dai_link);
497 return NULL;
498}
499EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
500
501struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
502 const char *dai_link)
503{
504 int i;
505
506 for (i = 0; i < card->num_links; i++) {
507 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
508 return &card->rtd[i];
509 }
510 dev_dbg(card->dev, "failed to find rtd %s\n", dai_link);
511 return NULL;
512}
513EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
514
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200515#ifdef CONFIG_SND_SOC_AC97_BUS
516/* unregister ac97 codec */
517static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
518{
519 if (codec->ac97->dev.bus)
520 device_unregister(&codec->ac97->dev);
521 return 0;
522}
523
524/* stop no dev release warning */
525static void soc_ac97_device_release(struct device *dev){}
526
527/* register ac97 codec to bus */
528static int soc_ac97_dev_register(struct snd_soc_codec *codec)
529{
530 int err;
531
532 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100533 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200534 codec->ac97->dev.release = soc_ac97_device_release;
535
Kay Sieversbb072bf2008-11-02 03:50:35 +0100536 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000537 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200538 err = device_register(&codec->ac97->dev);
539 if (err < 0) {
540 snd_printk(KERN_ERR "Can't register ac97 bus\n");
541 codec->ac97->dev.bus = NULL;
542 return err;
543 }
544 return 0;
545}
546#endif
547
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000548#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200549/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000550int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200551{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000552 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200553 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200554 int i;
555
Daniel Macke3509ff2009-06-03 17:44:49 +0200556 /* If the initialization of this soc device failed, there is no codec
557 * associated with it. Just bail out in this case.
558 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000559 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200560 return 0;
561
Andy Green6ed25972008-06-13 16:24:05 +0100562 /* Due to the resume being scheduled into a workqueue we could
563 * suspend before that's finished - wait for it to complete.
564 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000565 snd_power_lock(card->snd_card);
566 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
567 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100568
569 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000570 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100571
Mark Browna00f90f2010-12-02 16:24:24 +0000572 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000573 for (i = 0; i < card->num_rtd; i++) {
574 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
575 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100576
Liam Girdwood13e13a32011-01-31 21:30:52 +0000577 if (card->rtd[i].dai_link->ignore_suspend ||
578 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +0100579 continue;
580
Liam Girdwood13e13a32011-01-31 21:30:52 +0000581 if (card->rtd[i].dai_link->dynamic)
Steve Mucklef132c6c2012-06-06 18:30:57 -0700582 soc_dpcm_be_digital_mute(&card->rtd[i], 1);
Liam Girdwood13e13a32011-01-31 21:30:52 +0000583 else {
584 if (drv->ops->digital_mute && dai->playback_active)
585 drv->ops->digital_mute(dai, 1);
586 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200587 }
588
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100589 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000590 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwood13e13a32011-01-31 21:30:52 +0000591 if (card->rtd[i].dai_link->ignore_suspend ||
592 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +0100593 continue;
594
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000595 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100596 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100597
Mark Brown87506542008-11-18 20:50:34 +0000598 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000599 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200600
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000601 for (i = 0; i < card->num_rtd; i++) {
602 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
603 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100604
Liam Girdwood13e13a32011-01-31 21:30:52 +0000605 if (card->rtd[i].dai_link->ignore_suspend ||
606 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +0100607 continue;
608
Liam Girdwood13e13a32011-01-31 21:30:52 +0000609 if (card->rtd[i].dai_link->dynamic) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700610 soc_dpcm_be_cpu_dai_suspend(&card->rtd[i]);
611 soc_dpcm_be_platform_suspend(&card->rtd[i]);
Liam Girdwood13e13a32011-01-31 21:30:52 +0000612 } else {
613 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
614 cpu_dai->driver->suspend(cpu_dai);
615 if (platform->driver->suspend && !platform->suspended) {
616 platform->driver->suspend(cpu_dai);
617 platform->suspended = 1;
618 }
Mark Brown1547aba2010-05-07 21:11:40 +0100619 }
620 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200621
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000622 /* close any waiting streams and save state */
623 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +0100624 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200625 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000626 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100627
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000628 for (i = 0; i < card->num_rtd; i++) {
629 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
630
Liam Girdwood13e13a32011-01-31 21:30:52 +0000631 if (card->rtd[i].dai_link->ignore_suspend ||
632 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +0100633 continue;
634
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000635 if (driver->playback.stream_name != NULL)
636 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
637 SND_SOC_DAPM_STREAM_SUSPEND);
638
639 if (driver->capture.stream_name != NULL)
640 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
641 SND_SOC_DAPM_STREAM_SUSPEND);
642 }
643
644 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200645 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000646 /* If there are paths active then the CODEC will be held with
647 * bias _ON and should not be suspended. */
648 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200649 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000650 case SND_SOC_BIAS_STANDBY:
651 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100652 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000653 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900654 codec->cache_sync = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000655 break;
656 default:
657 dev_dbg(codec->dev, "CODEC is on over suspend\n");
658 break;
659 }
660 }
661 }
662
663 for (i = 0; i < card->num_rtd; i++) {
664 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
665
Liam Girdwood13e13a32011-01-31 21:30:52 +0000666 if (card->rtd[i].dai_link->ignore_suspend ||
667 card->rtd[i].dai_link->no_pcm)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000668 continue;
669
Liam Girdwood13e13a32011-01-31 21:30:52 +0000670 if (card->rtd[i].dai_link->dynamic)
Steve Mucklef132c6c2012-06-06 18:30:57 -0700671 soc_dpcm_be_ac97_cpu_dai_suspend(&card->rtd[i]);
Liam Girdwood13e13a32011-01-31 21:30:52 +0000672 else
673 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
674 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200675 }
676
Mark Brown87506542008-11-18 20:50:34 +0000677 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000678 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200679
680 return 0;
681}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000682EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200683
Andy Green6ed25972008-06-13 16:24:05 +0100684/* deferred resume work, so resume can complete before we finished
685 * setting our codec back up, which can be very slow on I2C
686 */
687static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200688{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000689 struct snd_soc_card *card =
690 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200691 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200692 int i;
693
Andy Green6ed25972008-06-13 16:24:05 +0100694 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
695 * so userspace apps are blocked from touching us
696 */
697
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000698 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100699
Mark Brown99497882010-05-07 20:24:05 +0100700 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000701 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100702
Mark Brown87506542008-11-18 20:50:34 +0000703 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000704 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200705
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000706 /* resume AC97 DAIs */
707 for (i = 0; i < card->num_rtd; i++) {
708 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100709
Liam Girdwood13e13a32011-01-31 21:30:52 +0000710 if (card->rtd[i].dai_link->ignore_suspend ||
711 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +0100712 continue;
713
Liam Girdwood13e13a32011-01-31 21:30:52 +0000714 if (card->rtd[i].dai_link->dynamic)
Steve Mucklef132c6c2012-06-06 18:30:57 -0700715 soc_dpcm_be_ac97_cpu_dai_resume(&card->rtd[i]);
Liam Girdwood13e13a32011-01-31 21:30:52 +0000716 else
717 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
718 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200719 }
720
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200721 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000722 /* If the CODEC was idle over suspend then it will have been
723 * left with bias OFF or STANDBY and suspended so we must now
724 * resume. Otherwise the suspend was suppressed.
725 */
726 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200727 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000728 case SND_SOC_BIAS_STANDBY:
729 case SND_SOC_BIAS_OFF:
730 codec->driver->resume(codec);
731 codec->suspended = 0;
732 break;
733 default:
734 dev_dbg(codec->dev, "CODEC was on over suspend\n");
735 break;
736 }
Mark Brown1547aba2010-05-07 21:11:40 +0100737 }
738 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200739
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000740 for (i = 0; i < card->num_rtd; i++) {
741 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100742
Liam Girdwood13e13a32011-01-31 21:30:52 +0000743 if (card->rtd[i].dai_link->ignore_suspend ||
744 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +0100745 continue;
746
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000747 if (driver->playback.stream_name != NULL)
748 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200749 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000750
751 if (driver->capture.stream_name != NULL)
752 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200753 SND_SOC_DAPM_STREAM_RESUME);
754 }
755
Mark Brown3ff3f642008-05-19 12:32:25 +0200756 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000757 for (i = 0; i < card->num_rtd; i++) {
758 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
759 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100760
Liam Girdwood13e13a32011-01-31 21:30:52 +0000761 if (card->rtd[i].dai_link->ignore_suspend ||
762 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +0100763 continue;
764
Liam Girdwood13e13a32011-01-31 21:30:52 +0000765 if (card->rtd[i].dai_link->dynamic)
Steve Mucklef132c6c2012-06-06 18:30:57 -0700766 soc_dpcm_be_digital_mute(&card->rtd[i], 0);
Liam Girdwood13e13a32011-01-31 21:30:52 +0000767 else {
768 if (drv->ops->digital_mute && dai->playback_active)
769 drv->ops->digital_mute(dai, 0);
770 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200771 }
772
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000773 for (i = 0; i < card->num_rtd; i++) {
774 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
775 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100776
Liam Girdwood13e13a32011-01-31 21:30:52 +0000777 if (card->rtd[i].dai_link->ignore_suspend ||
778 card->rtd[i].dai_link->no_pcm)
Mark Brown3efab7d2010-05-09 13:25:43 +0100779 continue;
780
Liam Girdwood13e13a32011-01-31 21:30:52 +0000781 if (card->rtd[i].dai_link->dynamic) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700782 soc_dpcm_be_cpu_dai_resume(&card->rtd[i]);
783 soc_dpcm_be_platform_resume(&card->rtd[i]);
Liam Girdwood13e13a32011-01-31 21:30:52 +0000784 } else {
785 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
786 cpu_dai->driver->resume(cpu_dai);
787 if (platform->driver->resume && platform->suspended) {
788 platform->driver->resume(cpu_dai);
789 platform->suspended = 0;
790 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000791 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200792 }
793
Mark Brown87506542008-11-18 20:50:34 +0000794 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000795 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200796
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000797 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100798
799 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000800 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100801}
802
803/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000804int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100805{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000806 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600807 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200808
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800809 /* If the initialization of this soc device failed, there is no codec
810 * associated with it. Just bail out in this case.
811 */
812 if (list_empty(&card->codec_dev_list))
813 return 0;
814
Mark Brown64ab9ba2009-03-31 11:27:03 +0100815 /* AC97 devices might have other drivers hanging off them so
816 * need to resume immediately. Other drivers don't have that
817 * problem and may take a substantial amount of time to resume
818 * due to I/O costs and anti-pop so handle them out of line.
819 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000820 for (i = 0; i < card->num_rtd; i++) {
821 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600822 ac97_control |= cpu_dai->driver->ac97_control;
823 }
824 if (ac97_control) {
825 dev_dbg(dev, "Resuming AC97 immediately\n");
826 soc_resume_deferred(&card->deferred_resume_work);
827 } else {
828 dev_dbg(dev, "Scheduling resume work\n");
829 if (!schedule_work(&card->deferred_resume_work))
830 dev_err(dev, "resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100831 }
Andy Green6ed25972008-06-13 16:24:05 +0100832
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200833 return 0;
834}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000835EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200836#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000837#define snd_soc_suspend NULL
838#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200839#endif
840
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100841static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800842};
843
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000844static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200845{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000846 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
847 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000848 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000849 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000850 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100851 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200852
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000853 if (rtd->complete)
854 return 1;
855 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000856
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000857 /* do we already have the CPU DAI for this link ? */
858 if (rtd->cpu_dai) {
859 goto find_codec;
860 }
861 /* no, then find CPU DAI from registered DAIs*/
862 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700863 if (dai_link->cpu_dai_of_node) {
864 if (cpu_dai->dev->of_node != dai_link->cpu_dai_of_node)
865 continue;
866 } else {
867 if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
868 continue;
869 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700870
871 rtd->cpu_dai = cpu_dai;
872 goto find_codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000873 }
874 dev_dbg(card->dev, "CPU DAI %s not registered\n",
875 dai_link->cpu_dai_name);
876
877find_codec:
878 /* do we already have the CODEC for this link ? */
879 if (rtd->codec) {
880 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +0000881 }
882
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000883 /* no, then find CODEC from registered CODECs*/
884 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700885 if (dai_link->codec_of_node) {
886 if (codec->dev->of_node != dai_link->codec_of_node)
887 continue;
888 } else {
889 if (strcmp(codec->name, dai_link->codec_name))
890 continue;
891 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000892
Stephen Warren2610ab72011-12-07 13:58:27 -0700893 rtd->codec = codec;
894
895 /*
896 * CODEC found, so find CODEC DAI from registered DAIs from
897 * this CODEC
898 */
899 list_for_each_entry(codec_dai, &dai_list, list) {
900 if (codec->dev == codec_dai->dev &&
901 !strcmp(codec_dai->name,
902 dai_link->codec_dai_name)) {
903
904 rtd->codec_dai = codec_dai;
905 goto find_platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000906 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000907 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700908 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
909 dai_link->codec_dai_name);
910
911 goto find_platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000912 }
913 dev_dbg(card->dev, "CODEC %s not registered\n",
914 dai_link->codec_name);
915
916find_platform:
Mark Brown848dd8b2011-04-27 18:16:32 +0100917 /* do we need a platform? */
918 if (rtd->platform)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000919 goto out;
Mark Brown848dd8b2011-04-27 18:16:32 +0100920
921 /* if there's no platform we match on the empty platform */
922 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700923 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100924 platform_name = "snd-soc-dummy";
925
926 /* no, then find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000927 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700928 if (dai_link->platform_of_node) {
929 if (platform->dev->of_node !=
930 dai_link->platform_of_node)
931 continue;
932 } else {
933 if (strcmp(platform->name, platform_name))
934 continue;
935 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700936
937 rtd->platform = platform;
938 goto out;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000939 }
940
941 dev_dbg(card->dev, "platform %s not registered\n",
942 dai_link->platform_name);
943 return 0;
944
945out:
946 /* mark rtd as complete if we found all 4 of our client devices */
947 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
948 rtd->complete = 1;
949 card->num_rtd++;
950 }
951 return 1;
952}
953
Jarkko Nikula589c3562010-12-06 16:27:07 +0200954static void soc_remove_codec(struct snd_soc_codec *codec)
955{
956 int err;
957
958 if (codec->driver->remove) {
959 err = codec->driver->remove(codec);
960 if (err < 0)
961 dev_err(codec->dev,
962 "asoc: failed to remove %s: %d\n",
963 codec->name, err);
964 }
965
966 /* Make sure all DAPM widgets are freed */
967 snd_soc_dapm_free(&codec->dapm);
968
969 soc_cleanup_codec_debugfs(codec);
970 codec->probed = 0;
971 list_del(&codec->card_list);
972 module_put(codec->dev->driver->owner);
973}
974
Liam Girdwood0168bf02011-06-07 16:08:05 +0100975static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000976{
977 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
978 struct snd_soc_codec *codec = rtd->codec;
979 struct snd_soc_platform *platform = rtd->platform;
980 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
981 int err;
982
983 /* unregister the rtd device */
984 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800985 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
986 device_remove_file(rtd->dev, &dev_attr_codec_reg);
987 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000988 rtd->dev_registered = 0;
989 }
990
991 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100992 if (codec_dai && codec_dai->probed &&
993 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000994 if (codec_dai->driver->remove) {
995 err = codec_dai->driver->remove(codec_dai);
996 if (err < 0)
997 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
998 }
999 codec_dai->probed = 0;
1000 list_del(&codec_dai->card_list);
1001 }
1002
1003 /* remove the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001004 if (platform && platform->probed &&
1005 platform->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001006 if (platform->driver->remove) {
1007 err = platform->driver->remove(platform);
1008 if (err < 0)
1009 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1010 }
Liam Girdwood675c4962012-01-16 15:25:37 +00001011 /* Make sure all DAPM widgets are freed */
1012 snd_soc_dapm_free(&platform->dapm);
1013
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001014 soc_cleanup_platform_debugfs(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001015 platform->probed = 0;
1016 list_del(&platform->card_list);
1017 module_put(platform->dev->driver->owner);
1018 }
1019
1020 /* remove the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001021 if (codec && codec->probed &&
1022 codec->driver->remove_order == order)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001023 soc_remove_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001024
1025 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001026 if (cpu_dai && cpu_dai->probed &&
1027 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001028 if (cpu_dai->driver->remove) {
1029 err = cpu_dai->driver->remove(cpu_dai);
1030 if (err < 0)
1031 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1032 }
1033 cpu_dai->probed = 0;
1034 list_del(&cpu_dai->card_list);
1035 module_put(cpu_dai->dev->driver->owner);
1036 }
1037}
1038
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001039static void soc_remove_dai_links(struct snd_soc_card *card)
1040{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001041 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001042
Liam Girdwood0168bf02011-06-07 16:08:05 +01001043 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1044 order++) {
1045 for (dai = 0; dai < card->num_rtd; dai++)
1046 soc_remove_dai_link(card, dai, order);
1047 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001048 card->num_rtd = 0;
1049}
1050
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001051static void soc_set_name_prefix(struct snd_soc_card *card,
1052 struct snd_soc_codec *codec)
1053{
1054 int i;
1055
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001056 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001057 return;
1058
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001059 for (i = 0; i < card->num_configs; i++) {
1060 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001061 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1062 codec->name_prefix = map->name_prefix;
1063 break;
1064 }
1065 }
1066}
1067
Jarkko Nikula589c3562010-12-06 16:27:07 +02001068static int soc_probe_codec(struct snd_soc_card *card,
1069 struct snd_soc_codec *codec)
1070{
1071 int ret = 0;
Mark Brown89b95ac2011-03-07 16:38:44 +00001072 const struct snd_soc_codec_driver *driver = codec->driver;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001073
1074 codec->card = card;
1075 codec->dapm.card = card;
1076 soc_set_name_prefix(card, codec);
1077
Jarkko Nikula70d293312011-01-27 16:24:22 +02001078 if (!try_module_get(codec->dev->driver->owner))
1079 return -ENODEV;
1080
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001081 soc_init_codec_debugfs(codec);
1082
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001083 if (driver->dapm_widgets)
1084 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1085 driver->num_dapm_widgets);
1086
Mark Brown33c5f962011-08-22 18:40:30 +01001087 codec->dapm.idle_bias_off = driver->idle_bias_off;
1088
Mark Brown89b95ac2011-03-07 16:38:44 +00001089 if (driver->probe) {
1090 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001091 if (ret < 0) {
1092 dev_err(codec->dev,
1093 "asoc: failed to probe CODEC %s: %d\n",
1094 codec->name, ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001095 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001096 }
1097 }
1098
Mark Brownb7af1da2011-04-07 19:18:44 +09001099 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001100 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001101 driver->num_controls);
Mark Brown89b95ac2011-03-07 16:38:44 +00001102 if (driver->dapm_routes)
1103 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1104 driver->num_dapm_routes);
1105
Jarkko Nikula589c3562010-12-06 16:27:07 +02001106 /* mark codec as probed and add to card codec list */
1107 codec->probed = 1;
1108 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001109 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001110
Jarkko Nikula70d293312011-01-27 16:24:22 +02001111 return 0;
1112
1113err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001114 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001115 module_put(codec->dev->driver->owner);
1116
Jarkko Nikula589c3562010-12-06 16:27:07 +02001117 return ret;
1118}
1119
Liam Girdwood956245e2011-07-01 16:54:08 +01001120static int soc_probe_platform(struct snd_soc_card *card,
1121 struct snd_soc_platform *platform)
1122{
1123 int ret = 0;
1124 const struct snd_soc_platform_driver *driver = platform->driver;
1125
1126 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001127 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001128
1129 if (!try_module_get(platform->dev->driver->owner))
1130 return -ENODEV;
1131
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001132 soc_init_platform_debugfs(platform);
1133
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001134 if (driver->dapm_widgets)
1135 snd_soc_dapm_new_controls(&platform->dapm,
1136 driver->dapm_widgets, driver->num_dapm_widgets);
1137
Liam Girdwood956245e2011-07-01 16:54:08 +01001138 if (driver->probe) {
1139 ret = driver->probe(platform);
1140 if (ret < 0) {
1141 dev_err(platform->dev,
1142 "asoc: failed to probe platform %s: %d\n",
1143 platform->name, ret);
1144 goto err_probe;
1145 }
1146 }
1147
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001148 if (driver->controls)
1149 snd_soc_add_platform_controls(platform, driver->controls,
1150 driver->num_controls);
1151 if (driver->dapm_routes)
1152 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1153 driver->num_dapm_routes);
1154
Liam Girdwood956245e2011-07-01 16:54:08 +01001155 /* mark platform as probed and add to card platform list */
1156 platform->probed = 1;
1157 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001158 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001159
1160 return 0;
1161
1162err_probe:
1163 module_put(platform->dev->driver->owner);
1164
1165 return ret;
1166}
1167
Mark Brown36ae1a92012-01-06 17:12:45 -08001168static void rtd_release(struct device *dev)
1169{
1170 kfree(dev);
1171}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001172
Jarkko Nikula589c3562010-12-06 16:27:07 +02001173static int soc_post_component_init(struct snd_soc_card *card,
1174 struct snd_soc_codec *codec,
1175 int num, int dailess)
1176{
1177 struct snd_soc_dai_link *dai_link = NULL;
1178 struct snd_soc_aux_dev *aux_dev = NULL;
1179 struct snd_soc_pcm_runtime *rtd;
1180 const char *temp, *name;
1181 int ret = 0;
1182
1183 if (!dailess) {
1184 dai_link = &card->dai_link[num];
1185 rtd = &card->rtd[num];
1186 name = dai_link->name;
1187 } else {
1188 aux_dev = &card->aux_dev[num];
1189 rtd = &card->rtd_aux[num];
1190 name = aux_dev->name;
1191 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001192 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001193
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001194 /* Make sure all DAPM widgets are instantiated */
1195 snd_soc_dapm_new_widgets(&codec->dapm);
1196
Jarkko Nikula589c3562010-12-06 16:27:07 +02001197 /* machine controls, routes and widgets are not prefixed */
1198 temp = codec->name_prefix;
1199 codec->name_prefix = NULL;
1200
1201 /* do machine specific initialization */
1202 if (!dailess && dai_link->init)
1203 ret = dai_link->init(rtd);
1204 else if (dailess && aux_dev->init)
1205 ret = aux_dev->init(&codec->dapm);
1206 if (ret < 0) {
1207 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1208 return ret;
1209 }
1210 codec->name_prefix = temp;
1211
Jarkko Nikula589c3562010-12-06 16:27:07 +02001212 /* register the rtd device */
1213 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001214
1215 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1216 if (!rtd->dev)
1217 return -ENOMEM;
1218 device_initialize(rtd->dev);
1219 rtd->dev->parent = card->dev;
1220 rtd->dev->release = rtd_release;
1221 rtd->dev->init_name = name;
1222 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001223 mutex_init(&rtd->pcm_mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001224 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1225 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1226 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1227 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001228 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001229 if (ret < 0) {
1230 dev_err(card->dev,
1231 "asoc: failed to register runtime device: %d\n", ret);
1232 return ret;
1233 }
1234 rtd->dev_registered = 1;
1235
1236 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001237 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001238 if (ret < 0)
1239 dev_err(codec->dev,
1240 "asoc: failed to add codec dapm sysfs entries: %d\n",
1241 ret);
1242
1243 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001244 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001245 if (ret < 0)
1246 dev_err(codec->dev,
1247 "asoc: failed to add codec sysfs files: %d\n", ret);
1248
Liam Girdwood13e13a32011-01-31 21:30:52 +00001249#ifdef CONFIG_DEBUG_FS
1250 /* add DSP sysfs entries */
1251 if (!dai_link->dynamic)
1252 goto out;
1253
Steve Mucklef132c6c2012-06-06 18:30:57 -07001254 ret = soc_dpcm_debugfs_add(rtd);
Liam Girdwood13e13a32011-01-31 21:30:52 +00001255 if (ret < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001256 dev_err(rtd->dev, "asoc: failed to add dpcm sysfs entries: %d\n", ret);
Liam Girdwood13e13a32011-01-31 21:30:52 +00001257
1258out:
1259#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001260 return 0;
1261}
1262
Liam Girdwood0168bf02011-06-07 16:08:05 +01001263static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001264{
1265 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1266 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1267 struct snd_soc_codec *codec = rtd->codec;
1268 struct snd_soc_platform *platform = rtd->platform;
1269 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1270 int ret;
1271
Liam Girdwood0168bf02011-06-07 16:08:05 +01001272 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1273 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001274
1275 /* config components */
1276 codec_dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001277 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001278 codec_dai->card = card;
1279 cpu_dai->card = card;
1280
1281 /* set default power off timeout */
1282 rtd->pmdown_time = pmdown_time;
1283
1284 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001285 if (!cpu_dai->probed &&
1286 cpu_dai->driver->probe_order == order) {
Liam Girdwood61b61e32011-05-24 13:57:43 +01001287 if (!try_module_get(cpu_dai->dev->driver->owner))
1288 return -ENODEV;
1289
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001290 if (cpu_dai->driver->probe) {
1291 ret = cpu_dai->driver->probe(cpu_dai);
1292 if (ret < 0) {
1293 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1294 cpu_dai->name);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001295 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001296 return ret;
1297 }
1298 }
1299 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001300 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001301 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1302 }
1303
1304 /* probe the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001305 if (!codec->probed &&
1306 codec->driver->probe_order == order) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001307 ret = soc_probe_codec(card, codec);
1308 if (ret < 0)
1309 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001310 }
1311
1312 /* probe the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001313 if (!platform->probed &&
1314 platform->driver->probe_order == order) {
Liam Girdwood956245e2011-07-01 16:54:08 +01001315 ret = soc_probe_platform(card, platform);
1316 if (ret < 0)
1317 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001318 }
1319
1320 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001321 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001322 if (codec_dai->driver->probe) {
1323 ret = codec_dai->driver->probe(codec_dai);
1324 if (ret < 0) {
1325 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1326 codec_dai->name);
1327 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001328 }
1329 }
1330
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001331 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001332 codec_dai->probed = 1;
1333 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001334 }
1335
Liam Girdwood0168bf02011-06-07 16:08:05 +01001336 /* complete DAI probe during last probe */
1337 if (order != SND_SOC_COMP_ORDER_LAST)
1338 return 0;
1339
Jarkko Nikula589c3562010-12-06 16:27:07 +02001340 ret = soc_post_component_init(card, codec, num, 0);
1341 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001342 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001343
Mark Brown36ae1a92012-01-06 17:12:45 -08001344 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001345 if (ret < 0)
1346 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1347
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001348 /* create the pcm */
1349 ret = soc_new_pcm(rtd, num);
1350 if (ret < 0) {
1351 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1352 return ret;
1353 }
1354
1355 /* add platform data for AC97 devices */
1356 if (rtd->codec_dai->driver->ac97_control)
1357 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1358
1359 return 0;
1360}
1361
1362#ifdef CONFIG_SND_SOC_AC97_BUS
1363static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1364{
1365 int ret;
1366
1367 /* Only instantiate AC97 if not already done by the adaptor
1368 * for the generic AC97 subsystem.
1369 */
1370 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001371 /*
1372 * It is possible that the AC97 device is already registered to
1373 * the device subsystem. This happens when the device is created
1374 * via snd_ac97_mixer(). Currently only SoC codec that does so
1375 * is the generic AC97 glue but others migh emerge.
1376 *
1377 * In those cases we don't try to register the device again.
1378 */
1379 if (!rtd->codec->ac97_created)
1380 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001381
1382 ret = soc_ac97_dev_register(rtd->codec);
1383 if (ret < 0) {
1384 printk(KERN_ERR "asoc: AC97 device register failed\n");
1385 return ret;
1386 }
1387
1388 rtd->codec->ac97_registered = 1;
1389 }
1390 return 0;
1391}
1392
1393static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1394{
1395 if (codec->ac97_registered) {
1396 soc_ac97_dev_unregister(codec);
1397 codec->ac97_registered = 0;
1398 }
1399}
1400#endif
1401
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001402static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1403{
1404 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001405 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001406 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001407
1408 /* find CODEC from registered CODECs*/
1409 list_for_each_entry(codec, &codec_list, list) {
1410 if (!strcmp(codec->name, aux_dev->codec_name)) {
1411 if (codec->probed) {
1412 dev_err(codec->dev,
1413 "asoc: codec already probed");
1414 ret = -EBUSY;
1415 goto out;
1416 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001417 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001418 }
1419 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001420 /* codec not found */
1421 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1422 goto out;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001423
Jarkko Nikula676ad982010-12-03 09:18:22 +02001424found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001425 ret = soc_probe_codec(card, codec);
1426 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001427 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001428
Jarkko Nikula589c3562010-12-06 16:27:07 +02001429 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001430
1431out:
1432 return ret;
1433}
1434
1435static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1436{
1437 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1438 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001439
1440 /* unregister the rtd device */
1441 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001442 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1443 device_del(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001444 rtd->dev_registered = 0;
1445 }
1446
Jarkko Nikula589c3562010-12-06 16:27:07 +02001447 if (codec && codec->probed)
1448 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001449}
1450
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001451static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1452 enum snd_soc_compress_type compress_type)
1453{
1454 int ret;
1455
1456 if (codec->cache_init)
1457 return 0;
1458
1459 /* override the compress_type if necessary */
1460 if (compress_type && codec->compress_type != compress_type)
1461 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001462 ret = snd_soc_cache_init(codec);
1463 if (ret < 0) {
1464 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1465 ret);
1466 return ret;
1467 }
1468 codec->cache_init = 1;
1469 return 0;
1470}
1471
Steve Mucklef132c6c2012-06-06 18:30:57 -07001472static void soc_init_dai_aif_channel_map(struct snd_soc_card *card,
1473 struct snd_soc_dai *dai, int stream)
1474{
1475 struct snd_soc_dapm_widget *w = NULL;
1476 const char *aif_name;
1477
1478 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1479 aif_name = dai->driver->playback.aif_name;
1480 else
1481 aif_name = dai->driver->capture.aif_name;
1482
1483 if (dai->codec) {
1484 struct snd_soc_codec *codec;
1485
1486 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
1487 w = snd_soc_get_codec_widget(card, codec, aif_name);
1488 if (w)
1489 break;
1490 }
1491 } else if (dai->platform) {
1492 struct snd_soc_platform *platform;
1493
1494 list_for_each_entry(platform, &card->platform_dev_list, card_list) {
1495 w = snd_soc_get_platform_widget(card, platform, aif_name);
1496 if (w)
1497 break;
1498 }
1499 }
1500
1501 if (w) {
1502 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1503 dai->playback_aif = w;
1504 else
1505 dai->capture_aif = w;
1506 } else
1507 dev_err(dai->dev, "unable to find %s DAI AIF %s\n",
1508 stream ? "capture" : "playback", aif_name);
1509
1510 dai->channel_map_instanciated = 1;
1511}
1512
1513static int soc_is_dai_pcm(struct snd_soc_card *card, struct snd_soc_dai *dai)
1514{
1515 int i;
1516
1517 for (i = 0; i < card->num_rtd; i++) {
1518 if (card->rtd[i].cpu_dai == dai && !card->rtd[i].dai_link->no_pcm)
1519 return 1;
1520 }
1521 return 0;
1522}
1523
1524static void soc_init_card_aif_channel_map(struct snd_soc_card *card)
1525{
1526 struct snd_soc_dai *dai;
1527
1528 list_for_each_entry(dai, &card->dai_dev_list, card_list) {
1529
1530 /* only process DAIs that use the new API until
1531 * the old "stream name" API is fully deprecated */
1532 if (!dai->driver->playback.aif_name && !dai->driver->capture.aif_name)
1533 continue;
1534
1535 /* channels are only mapped from PCM DAIs */
1536 if (!soc_is_dai_pcm(card, dai))
1537 continue;
1538
1539 /* skip if already instanciated */
1540 if (dai->channel_map_instanciated)
1541 continue;
1542
1543 /* create unique channels masks for each DAI in the sound card */
1544 dai->playback_channel_map =
1545 ((1 << dai->driver->playback.channels_max) - 1)
1546 << card->num_playback_channels;
1547 card->num_playback_channels += dai->driver->playback.channels_max;
1548
1549 dai->capture_channel_map =
1550 ((1 << dai->driver->capture.channels_max) - 1)
1551 << card->num_capture_channels;
1552 card->num_capture_channels += dai->driver->capture.channels_max;
1553
1554 if (dai->driver->playback.channels_max)
1555 soc_init_dai_aif_channel_map(card, dai, SNDRV_PCM_STREAM_PLAYBACK);
1556 if (dai->driver->capture.channels_max)
1557 soc_init_dai_aif_channel_map(card, dai, SNDRV_PCM_STREAM_CAPTURE);
1558 }
1559}
1560
1561
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001562static void snd_soc_instantiate_card(struct snd_soc_card *card)
1563{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001564 struct snd_soc_codec *codec;
1565 struct snd_soc_codec_conf *codec_conf;
1566 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001567 struct snd_soc_dai_link *dai_link;
Liam Girdwood0168bf02011-06-07 16:08:05 +01001568 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001569
1570 mutex_lock(&card->mutex);
1571
1572 if (card->instantiated) {
1573 mutex_unlock(&card->mutex);
1574 return;
1575 }
1576
1577 /* bind DAIs */
1578 for (i = 0; i < card->num_links; i++)
1579 soc_bind_dai_link(card, i);
1580
1581 /* bind completed ? */
1582 if (card->num_rtd != card->num_links) {
1583 mutex_unlock(&card->mutex);
1584 return;
1585 }
1586
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001587 /* initialize the register cache for each available codec */
1588 list_for_each_entry(codec, &codec_list, list) {
1589 if (codec->cache_init)
1590 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001591 /* by default we don't override the compress_type */
1592 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001593 /* check to see if we need to override the compress_type */
1594 for (i = 0; i < card->num_configs; ++i) {
1595 codec_conf = &card->codec_conf[i];
1596 if (!strcmp(codec->name, codec_conf->dev_name)) {
1597 compress_type = codec_conf->compress_type;
1598 if (compress_type && compress_type
1599 != codec->compress_type)
1600 break;
1601 }
1602 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001603 ret = snd_soc_init_codec_cache(codec, compress_type);
1604 if (ret < 0) {
1605 mutex_unlock(&card->mutex);
1606 return;
1607 }
1608 }
1609
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001610 /* card bind complete so register a sound card */
1611 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1612 card->owner, 0, &card->snd_card);
1613 if (ret < 0) {
1614 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1615 card->name);
1616 mutex_unlock(&card->mutex);
1617 return;
1618 }
1619 card->snd_card->dev = card->dev;
1620
Mark Browne37a4972011-03-02 18:21:57 +00001621 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1622 card->dapm.dev = card->dev;
1623 card->dapm.card = card;
1624 list_add(&card->dapm.list, &card->dapm_list);
1625
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001626#ifdef CONFIG_DEBUG_FS
1627 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1628#endif
1629
Mark Brown88ee1c62011-02-02 10:43:26 +00001630#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001631 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001632 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001633#endif
Andy Green6ed25972008-06-13 16:24:05 +01001634
Mark Brown9a841eb2011-04-12 17:51:37 -07001635 if (card->dapm_widgets)
1636 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1637 card->num_dapm_widgets);
1638
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001639 /* initialise the sound card only once */
1640 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001641 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001642 if (ret < 0)
1643 goto card_probe_error;
1644 }
1645
Liam Girdwood0168bf02011-06-07 16:08:05 +01001646 /* early DAI link probe */
1647 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1648 order++) {
1649 for (i = 0; i < card->num_links; i++) {
1650 ret = soc_probe_dai_link(card, i, order);
1651 if (ret < 0) {
1652 pr_err("asoc: failed to instantiate card %s: %d\n",
Mark Brown321de0d2010-09-21 15:09:37 +01001653 card->name, ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001654 goto probe_dai_err;
1655 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001656 }
1657 }
1658
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001659 for (i = 0; i < card->num_aux_devs; i++) {
1660 ret = soc_probe_aux_dev(card, i);
1661 if (ret < 0) {
1662 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1663 card->name, ret);
1664 goto probe_aux_dev_err;
1665 }
1666 }
1667
Mark Brownb7af1da2011-04-07 19:18:44 +09001668 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001669 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001670
Mark Brownb8ad29d2011-03-02 18:35:51 +00001671 if (card->dapm_routes)
1672 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1673 card->num_dapm_routes);
1674
Mark Brownb90d2f92011-10-10 13:38:06 +01001675 snd_soc_dapm_new_widgets(&card->dapm);
1676
Mark Brown75d9ac42011-09-27 16:41:01 +01001677 for (i = 0; i < card->num_links; i++) {
1678 dai_link = &card->dai_link[i];
1679
1680 if (dai_link->dai_fmt) {
1681 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1682 dai_link->dai_fmt);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001683 if (ret != 0)
Mark Brown75d9ac42011-09-27 16:41:01 +01001684 dev_warn(card->rtd[i].codec_dai->dev,
1685 "Failed to set DAI format: %d\n",
1686 ret);
1687
1688 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1689 dai_link->dai_fmt);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001690 if (ret != 0)
Mark Brown75d9ac42011-09-27 16:41:01 +01001691 dev_warn(card->rtd[i].cpu_dai->dev,
1692 "Failed to set DAI format: %d\n",
1693 ret);
1694 }
1695 }
1696
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001697 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001698 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001699 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1700 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001701 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1702 "%s", card->driver_name ? card->driver_name : card->name);
1703 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1704 switch (card->snd_card->driver[i]) {
1705 case '_':
1706 case '-':
1707 case '\0':
1708 break;
1709 default:
1710 if (!isalnum(card->snd_card->driver[i]))
1711 card->snd_card->driver[i] = '_';
1712 break;
1713 }
1714 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001715
Mark Brown28e9ad92011-03-02 18:36:34 +00001716 if (card->late_probe) {
1717 ret = card->late_probe(card);
1718 if (ret < 0) {
1719 dev_err(card->dev, "%s late_probe() failed: %d\n",
1720 card->name, ret);
1721 goto probe_aux_dev_err;
1722 }
1723 }
1724
Mark Brown2dc00212011-10-08 13:59:44 +01001725 snd_soc_dapm_new_widgets(&card->dapm);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001726 soc_init_card_aif_channel_map(card);
Mark Brown2dc00212011-10-08 13:59:44 +01001727
Stephen Warren16332812011-11-23 12:42:04 -07001728 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001729 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001730 snd_soc_dapm_auto_nc_codec_pins(codec);
1731
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001732 ret = snd_card_register(card->snd_card);
1733 if (ret < 0) {
1734 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
Axel Lin6b3ed782010-12-07 16:12:29 +08001735 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001736 }
1737
1738#ifdef CONFIG_SND_SOC_AC97_BUS
1739 /* register any AC97 codecs */
1740 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001741 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1742 if (ret < 0) {
1743 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1744 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001745 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001746 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001747 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001748 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001749#endif
1750
Mark Brown435c5e22008-12-04 15:32:53 +00001751 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001752 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001753 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001754 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001755
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001756probe_aux_dev_err:
1757 for (i = 0; i < card->num_aux_devs; i++)
1758 soc_remove_aux_dev(card, i);
1759
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001760probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001761 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001762
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001763card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001764 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001765 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001766
1767 snd_card_free(card->snd_card);
1768
1769 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001770}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001771
Mark Brown435c5e22008-12-04 15:32:53 +00001772/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001773 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001774 * client_mutex.
1775 */
1776static void snd_soc_instantiate_cards(void)
1777{
1778 struct snd_soc_card *card;
1779 list_for_each_entry(card, &card_list, list)
1780 snd_soc_instantiate_card(card);
1781}
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
Mark Brownc3acec22010-12-02 16:15:29 +00002038 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002039 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002040 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002041
2042 return ret;
2043}
2044EXPORT_SYMBOL_GPL(snd_soc_read);
2045
2046unsigned int snd_soc_write(struct snd_soc_codec *codec,
2047 unsigned int reg, unsigned int val)
2048{
2049 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002050 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002051 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002052}
2053EXPORT_SYMBOL_GPL(snd_soc_write);
2054
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00002055unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2056 unsigned int reg, const void *data, size_t len)
2057{
2058 return codec->bulk_write_raw(codec, reg, data, len);
2059}
2060EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2061
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002062/**
2063 * snd_soc_update_bits - update codec register bits
2064 * @codec: audio codec
2065 * @reg: codec register
2066 * @mask: register mask
2067 * @value: new value
2068 *
2069 * Writes new register value.
2070 *
Timur Tabi180c3292011-01-10 15:58:13 -06002071 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002072 */
2073int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002074 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002075{
Mark Brown8a713da2011-12-03 12:33:55 +00002076 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002077 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002078 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002079
Mark Brown8a713da2011-12-03 12:33:55 +00002080 if (codec->using_regmap) {
2081 ret = regmap_update_bits_check(codec->control_data, reg,
2082 mask, value, &change);
2083 } else {
2084 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06002085 if (ret < 0)
2086 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00002087
2088 old = ret;
2089 new = (old & ~mask) | (value & mask);
2090 change = old != new;
2091 if (change)
2092 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06002093 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002094
Mark Brown8a713da2011-12-03 12:33:55 +00002095 if (ret < 0)
2096 return ret;
2097
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002098 return change;
2099}
2100EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2101
2102/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002103 * snd_soc_update_bits_locked - update codec register bits
2104 * @codec: audio codec
2105 * @reg: codec register
2106 * @mask: register mask
2107 * @value: new value
2108 *
2109 * Writes new register value, and takes the codec mutex.
2110 *
2111 * Returns 1 for change else 0.
2112 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002113int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2114 unsigned short reg, unsigned int mask,
2115 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002116{
2117 int change;
2118
2119 mutex_lock(&codec->mutex);
2120 change = snd_soc_update_bits(codec, reg, mask, value);
2121 mutex_unlock(&codec->mutex);
2122
2123 return change;
2124}
Mark Browndd1b3d52009-12-04 14:22:03 +00002125EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002126
2127/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002128 * snd_soc_test_bits - test register for change
2129 * @codec: audio codec
2130 * @reg: codec register
2131 * @mask: register mask
2132 * @value: new value
2133 *
2134 * Tests a register with a new value and checks if the new value is
2135 * different from the old value.
2136 *
2137 * Returns 1 for change else 0.
2138 */
2139int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002140 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002141{
2142 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002143 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002144
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002145 old = snd_soc_read(codec, reg);
2146 new = (old & ~mask) | value;
2147 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002148
2149 return change;
2150}
2151EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2152
2153/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002154 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2155 * @substream: the pcm substream
2156 * @hw: the hardware parameters
2157 *
2158 * Sets the substream runtime hardware parameters.
2159 */
2160int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2161 const struct snd_pcm_hardware *hw)
2162{
2163 struct snd_pcm_runtime *runtime = substream->runtime;
Liam Girdwood13e13a32011-01-31 21:30:52 +00002164 if (!runtime)
2165 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002166 runtime->hw.info = hw->info;
2167 runtime->hw.formats = hw->formats;
2168 runtime->hw.period_bytes_min = hw->period_bytes_min;
2169 runtime->hw.period_bytes_max = hw->period_bytes_max;
2170 runtime->hw.periods_min = hw->periods_min;
2171 runtime->hw.periods_max = hw->periods_max;
2172 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2173 runtime->hw.fifo_size = hw->fifo_size;
2174 return 0;
2175}
2176EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2177
2178/**
2179 * snd_soc_cnew - create new control
2180 * @_template: control template
2181 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002182 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002183 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002184 *
2185 * Create a new mixer control from a template control.
2186 *
2187 * Returns 0 for success, else error.
2188 */
2189struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brownefb7ac32011-03-08 17:23:24 +00002190 void *data, char *long_name,
2191 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002192{
2193 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002194 struct snd_kcontrol *kcontrol;
2195 char *name = NULL;
2196 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002197
2198 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002199 template.index = 0;
2200
Mark Brownefb7ac32011-03-08 17:23:24 +00002201 if (!long_name)
2202 long_name = template.name;
2203
2204 if (prefix) {
2205 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08002206 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00002207 if (!name)
2208 return NULL;
2209
2210 snprintf(name, name_len, "%s %s", prefix, long_name);
2211
2212 template.name = name;
2213 } else {
2214 template.name = long_name;
2215 }
2216
2217 kcontrol = snd_ctl_new1(&template, data);
2218
2219 kfree(name);
2220
2221 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002222}
2223EXPORT_SYMBOL_GPL(snd_soc_cnew);
2224
Liam Girdwood022658b2012-02-03 17:43:09 +00002225static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2226 const struct snd_kcontrol_new *controls, int num_controls,
2227 const char *prefix, void *data)
2228{
2229 int err, i;
2230
2231 for (i = 0; i < num_controls; i++) {
2232 const struct snd_kcontrol_new *control = &controls[i];
2233 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2234 control->name, prefix));
2235 if (err < 0) {
2236 dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2237 return err;
2238 }
2239 }
2240
2241 return 0;
2242}
2243
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002244/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002245 * snd_soc_add_codec_controls - add an array of controls to a codec.
2246 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002247 * duplicating this code.
2248 *
2249 * @codec: codec to add controls to
2250 * @controls: array of controls to add
2251 * @num_controls: number of elements in the array
2252 *
2253 * Return 0 for success, else error.
2254 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002255int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002256 const struct snd_kcontrol_new *controls, int num_controls)
2257{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002258 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002259
Liam Girdwood022658b2012-02-03 17:43:09 +00002260 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2261 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002262}
Liam Girdwood022658b2012-02-03 17:43:09 +00002263EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002264
2265/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002266 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002267 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002268 *
2269 * @platform: platform to add controls to
2270 * @controls: array of controls to add
2271 * @num_controls: number of elements in the array
2272 *
2273 * Return 0 for success, else error.
2274 */
2275int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2276 const struct snd_kcontrol_new *controls, int num_controls)
2277{
2278 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002279
Liam Girdwood022658b2012-02-03 17:43:09 +00002280 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2281 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002282}
2283EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2284
2285/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002286 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2287 * Convenience function to add a list of controls.
2288 *
2289 * @soc_card: SoC card to add controls to
2290 * @controls: array of controls to add
2291 * @num_controls: number of elements in the array
2292 *
2293 * Return 0 for success, else error.
2294 */
2295int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2296 const struct snd_kcontrol_new *controls, int num_controls)
2297{
2298 struct snd_card *card = soc_card->snd_card;
2299
2300 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2301 NULL, soc_card);
2302}
2303EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2304
2305/**
2306 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2307 * Convienience function to add a list of controls.
2308 *
2309 * @dai: DAI to add controls to
2310 * @controls: array of controls to add
2311 * @num_controls: number of elements in the array
2312 *
2313 * Return 0 for success, else error.
2314 */
2315int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2316 const struct snd_kcontrol_new *controls, int num_controls)
2317{
2318 struct snd_card *card = dai->card->snd_card;
2319
2320 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2321 NULL, dai);
2322}
2323EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2324
2325/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002326 * snd_soc_info_enum_double - enumerated double mixer info callback
2327 * @kcontrol: mixer control
2328 * @uinfo: control element information
2329 *
2330 * Callback to provide information about a double enumerated
2331 * mixer control.
2332 *
2333 * Returns 0 for success.
2334 */
2335int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2336 struct snd_ctl_elem_info *uinfo)
2337{
2338 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2339
2340 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2341 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002342 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002343
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002344 if (uinfo->value.enumerated.item > e->max - 1)
2345 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002346 strcpy(uinfo->value.enumerated.name,
Liam Girdwooda8f13d82011-05-18 18:39:21 +01002347 snd_soc_get_enum_text(e, uinfo->value.enumerated.item));
2348
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002349 return 0;
2350}
2351EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2352
2353/**
2354 * snd_soc_get_enum_double - enumerated double mixer get callback
2355 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002356 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002357 *
2358 * Callback to get the value of a double enumerated mixer.
2359 *
2360 * Returns 0 for success.
2361 */
2362int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2363 struct snd_ctl_elem_value *ucontrol)
2364{
2365 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2366 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002367 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002368
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002369 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002370 ;
2371 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002372 ucontrol->value.enumerated.item[0]
2373 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002374 if (e->shift_l != e->shift_r)
2375 ucontrol->value.enumerated.item[1] =
2376 (val >> e->shift_r) & (bitmask - 1);
2377
2378 return 0;
2379}
2380EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2381
2382/**
2383 * snd_soc_put_enum_double - enumerated double mixer put callback
2384 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002385 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002386 *
2387 * Callback to set the value of a double enumerated mixer.
2388 *
2389 * Returns 0 for success.
2390 */
2391int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2392 struct snd_ctl_elem_value *ucontrol)
2393{
2394 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2395 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002396 unsigned int val;
2397 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002398
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002399 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002400 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002401 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002402 return -EINVAL;
2403 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2404 mask = (bitmask - 1) << e->shift_l;
2405 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002406 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002407 return -EINVAL;
2408 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2409 mask |= (bitmask - 1) << e->shift_r;
2410 }
2411
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002412 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002413}
2414EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2415
2416/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002417 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2418 * @kcontrol: mixer control
2419 * @ucontrol: control element information
2420 *
2421 * Callback to get the value of a double semi enumerated mixer.
2422 *
2423 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2424 * used for handling bitfield coded enumeration for example.
2425 *
2426 * Returns 0 for success.
2427 */
2428int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2429 struct snd_ctl_elem_value *ucontrol)
2430{
2431 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002432 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002433 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002434
2435 reg_val = snd_soc_read(codec, e->reg);
2436 val = (reg_val >> e->shift_l) & e->mask;
2437 for (mux = 0; mux < e->max; mux++) {
2438 if (val == e->values[mux])
2439 break;
2440 }
2441 ucontrol->value.enumerated.item[0] = mux;
2442 if (e->shift_l != e->shift_r) {
2443 val = (reg_val >> e->shift_r) & e->mask;
2444 for (mux = 0; mux < e->max; mux++) {
2445 if (val == e->values[mux])
2446 break;
2447 }
2448 ucontrol->value.enumerated.item[1] = mux;
2449 }
2450
2451 return 0;
2452}
2453EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2454
2455/**
2456 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2457 * @kcontrol: mixer control
2458 * @ucontrol: control element information
2459 *
2460 * Callback to set the value of a double semi enumerated mixer.
2461 *
2462 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2463 * used for handling bitfield coded enumeration for example.
2464 *
2465 * Returns 0 for success.
2466 */
2467int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2468 struct snd_ctl_elem_value *ucontrol)
2469{
2470 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002471 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002472 unsigned int val;
2473 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002474
2475 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2476 return -EINVAL;
2477 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2478 mask = e->mask << e->shift_l;
2479 if (e->shift_l != e->shift_r) {
2480 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2481 return -EINVAL;
2482 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2483 mask |= e->mask << e->shift_r;
2484 }
2485
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002486 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002487}
2488EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2489
2490/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002491 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2492 * @kcontrol: mixer control
2493 * @uinfo: control element information
2494 *
2495 * Callback to provide information about an external enumerated
2496 * single mixer.
2497 *
2498 * Returns 0 for success.
2499 */
2500int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2501 struct snd_ctl_elem_info *uinfo)
2502{
2503 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2504
2505 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2506 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002507 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002508
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002509 if (uinfo->value.enumerated.item > e->max - 1)
2510 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002511 strcpy(uinfo->value.enumerated.name,
Liam Girdwooda8f13d82011-05-18 18:39:21 +01002512 snd_soc_get_enum_text(e, uinfo->value.enumerated.item));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002513 return 0;
2514}
2515EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2516
2517/**
2518 * snd_soc_info_volsw_ext - external single mixer info callback
2519 * @kcontrol: mixer control
2520 * @uinfo: control element information
2521 *
2522 * Callback to provide information about a single external mixer control.
2523 *
2524 * Returns 0 for success.
2525 */
2526int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2527 struct snd_ctl_elem_info *uinfo)
2528{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002529 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002530
Mark Brownfd5dfad2009-04-15 21:37:46 +01002531 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002532 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2533 else
2534 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2535
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002536 uinfo->count = 1;
2537 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002538 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002539 return 0;
2540}
2541EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2542
2543/**
Ben Romberger8f1dc4c2011-09-22 13:51:35 -07002544 * snd_soc_info_multi_ext - external single mixer info callback
2545 * @kcontrol: mixer control
2546 * @uinfo: control element information
2547 *
2548 * Callback to provide information about a single external mixer control.
2549 * that accepts multiple input.
2550 *
2551 * Returns 0 for success.
2552 */
2553int snd_soc_info_multi_ext(struct snd_kcontrol *kcontrol,
2554 struct snd_ctl_elem_info *uinfo)
2555{
2556 struct soc_multi_mixer_control *mc =
2557 (struct soc_multi_mixer_control *)kcontrol->private_value;
2558 int platform_max;
2559
2560 if (!mc->platform_max)
2561 mc->platform_max = mc->max;
2562 platform_max = mc->platform_max;
2563
2564 if (platform_max == 1 && !strnstr(kcontrol->id.name, " Volume", 30))
2565 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2566 else
2567 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2568
2569 uinfo->count = mc->count;
2570 uinfo->value.integer.min = 0;
2571 uinfo->value.integer.max = platform_max;
2572 return 0;
2573}
2574EXPORT_SYMBOL_GPL(snd_soc_info_multi_ext);
2575
2576/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002577 * snd_soc_info_volsw - single mixer info callback
2578 * @kcontrol: mixer control
2579 * @uinfo: control element information
2580 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002581 * Callback to provide information about a single mixer control, or a double
2582 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002583 *
2584 * Returns 0 for success.
2585 */
2586int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2587 struct snd_ctl_elem_info *uinfo)
2588{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002589 struct soc_mixer_control *mc =
2590 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002591 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002592
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002593 if (!mc->platform_max)
2594 mc->platform_max = mc->max;
2595 platform_max = mc->platform_max;
2596
2597 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002598 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2599 else
2600 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2601
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002602 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002603 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002604 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002605 return 0;
2606}
2607EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2608
2609/**
2610 * snd_soc_get_volsw - single mixer get callback
2611 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002612 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002613 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002614 * Callback to get the value of a single mixer control, or a double mixer
2615 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002616 *
2617 * Returns 0 for success.
2618 */
2619int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2620 struct snd_ctl_elem_value *ucontrol)
2621{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002622 struct soc_mixer_control *mc =
2623 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002624 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002625 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002626 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf82008-07-29 10:22:24 -04002627 unsigned int shift = mc->shift;
2628 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002629 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002630 unsigned int mask = (1 << fls(max)) - 1;
2631 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002632
2633 ucontrol->value.integer.value[0] =
2634 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002635 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002636 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002637 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002638
2639 if (snd_soc_volsw_is_stereo(mc)) {
2640 if (reg == reg2)
2641 ucontrol->value.integer.value[1] =
2642 (snd_soc_read(codec, reg) >> rshift) & mask;
2643 else
2644 ucontrol->value.integer.value[1] =
2645 (snd_soc_read(codec, reg2) >> shift) & mask;
2646 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002647 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002648 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002649 }
2650
2651 return 0;
2652}
2653EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2654
2655/**
2656 * snd_soc_put_volsw - single mixer put callback
2657 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002658 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002659 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002660 * Callback to set the value of a single mixer control, or a double mixer
2661 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002662 *
2663 * Returns 0 for success.
2664 */
2665int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2666 struct snd_ctl_elem_value *ucontrol)
2667{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002668 struct soc_mixer_control *mc =
2669 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002670 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002671 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002672 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf82008-07-29 10:22:24 -04002673 unsigned int shift = mc->shift;
2674 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002675 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002676 unsigned int mask = (1 << fls(max)) - 1;
2677 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002678 int err;
2679 bool type_2r = 0;
2680 unsigned int val2 = 0;
2681 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002682
2683 val = (ucontrol->value.integer.value[0] & mask);
2684 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002685 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002686 val_mask = mask << shift;
2687 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002688 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002689 val2 = (ucontrol->value.integer.value[1] & mask);
2690 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002691 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002692 if (reg == reg2) {
2693 val_mask |= mask << rshift;
2694 val |= val2 << rshift;
2695 } else {
2696 val2 = val2 << shift;
2697 type_2r = 1;
2698 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002699 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002700 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002701 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002702 return err;
2703
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002704 if (type_2r)
2705 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2706
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002707 return err;
2708}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002709EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002710
Mark Browne13ac2e2008-05-28 17:58:05 +01002711/**
2712 * snd_soc_info_volsw_s8 - signed mixer info callback
2713 * @kcontrol: mixer control
2714 * @uinfo: control element information
2715 *
2716 * Callback to provide information about a signed mixer control.
2717 *
2718 * Returns 0 for success.
2719 */
2720int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2721 struct snd_ctl_elem_info *uinfo)
2722{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002723 struct soc_mixer_control *mc =
2724 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002725 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002726 int min = mc->min;
Bradley Rubin572c3b42011-07-07 18:23:44 -07002727 unsigned int shift = mc->shift;
2728 unsigned int rshift = mc->rshift;
Mark Browne13ac2e2008-05-28 17:58:05 +01002729
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002730 if (!mc->platform_max)
2731 mc->platform_max = mc->max;
2732 platform_max = mc->platform_max;
2733
Mark Browne13ac2e2008-05-28 17:58:05 +01002734 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Bradley Rubin572c3b42011-07-07 18:23:44 -07002735 uinfo->count = shift == rshift ? 1 : 2;
Mark Browne13ac2e2008-05-28 17:58:05 +01002736 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002737 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002738 return 0;
2739}
2740EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2741
2742/**
2743 * snd_soc_get_volsw_s8 - signed mixer get callback
2744 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002745 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002746 *
2747 * Callback to get the value of a signed mixer control.
2748 *
2749 * Returns 0 for success.
2750 */
2751int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2752 struct snd_ctl_elem_value *ucontrol)
2753{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002754 struct soc_mixer_control *mc =
2755 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002756 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002757 unsigned int reg = mc->reg;
Bradley Rubin572c3b42011-07-07 18:23:44 -07002758 unsigned int shift = mc->shift;
2759 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002760 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002761 int val = snd_soc_read(codec, reg);
2762
2763 ucontrol->value.integer.value[0] =
Bradley Rubin572c3b42011-07-07 18:23:44 -07002764 ((signed char)((val >> shift) & 0xff))-min;
2765 if (shift != rshift)
2766 ucontrol->value.integer.value[1] =
2767 ((signed char)((val >> rshift) & 0xff))-min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002768 return 0;
2769}
2770EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2771
2772/**
2773 * snd_soc_put_volsw_sgn - signed mixer put callback
2774 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002775 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002776 *
2777 * Callback to set the value of a signed mixer control.
2778 *
2779 * Returns 0 for success.
2780 */
2781int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2782 struct snd_ctl_elem_value *ucontrol)
2783{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002784 struct soc_mixer_control *mc =
2785 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002786 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002787 unsigned int reg = mc->reg;
Bradley Rubin572c3b42011-07-07 18:23:44 -07002788 unsigned int shift = mc->shift;
2789 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002790 int min = mc->min;
Bradley Rubin572c3b42011-07-07 18:23:44 -07002791 unsigned int val, val2, val_mask;
Mark Browne13ac2e2008-05-28 17:58:05 +01002792
Bradley Rubin572c3b42011-07-07 18:23:44 -07002793 val = ((ucontrol->value.integer.value[0]+min) & 0xff) << shift;
2794 val_mask = 0xff << shift;
2795 if (shift != rshift) {
2796 val2 = (ucontrol->value.integer.value[1]+min) & 0xff;
2797 val |= val2 << rshift;
2798 val_mask |= 0xff << rshift;
2799 }
Mark Browne13ac2e2008-05-28 17:58:05 +01002800
Bradley Rubin572c3b42011-07-07 18:23:44 -07002801 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002802}
2803EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2804
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002805/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002806 * snd_soc_limit_volume - Set new limit to an existing volume control.
2807 *
2808 * @codec: where to look for the control
2809 * @name: Name of the control
2810 * @max: new maximum limit
2811 *
2812 * Return 0 for success, else error.
2813 */
2814int snd_soc_limit_volume(struct snd_soc_codec *codec,
2815 const char *name, int max)
2816{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002817 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002818 struct snd_kcontrol *kctl;
2819 struct soc_mixer_control *mc;
2820 int found = 0;
2821 int ret = -EINVAL;
2822
2823 /* Sanity check for name and max */
2824 if (unlikely(!name || max <= 0))
2825 return -EINVAL;
2826
2827 list_for_each_entry(kctl, &card->controls, list) {
2828 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2829 found = 1;
2830 break;
2831 }
2832 }
2833 if (found) {
2834 mc = (struct soc_mixer_control *)kctl->private_value;
2835 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002836 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002837 ret = 0;
2838 }
2839 }
2840 return ret;
2841}
2842EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2843
2844/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002845 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2846 * mixer info callback
2847 * @kcontrol: mixer control
2848 * @uinfo: control element information
2849 *
2850 * Returns 0 for success.
2851 */
2852int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2853 struct snd_ctl_elem_info *uinfo)
2854{
2855 struct soc_mixer_control *mc =
2856 (struct soc_mixer_control *)kcontrol->private_value;
2857 int max = mc->max;
2858 int min = mc->min;
2859
2860 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2861 uinfo->count = 2;
2862 uinfo->value.integer.min = 0;
2863 uinfo->value.integer.max = max-min;
2864
2865 return 0;
2866}
2867EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2868
2869/**
2870 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2871 * mixer get callback
2872 * @kcontrol: mixer control
2873 * @uinfo: control element information
2874 *
2875 * Returns 0 for success.
2876 */
2877int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2878 struct snd_ctl_elem_value *ucontrol)
2879{
2880 struct soc_mixer_control *mc =
2881 (struct soc_mixer_control *)kcontrol->private_value;
2882 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2883 unsigned int mask = (1<<mc->shift)-1;
2884 int min = mc->min;
2885 int val = snd_soc_read(codec, mc->reg) & mask;
2886 int valr = snd_soc_read(codec, mc->rreg) & mask;
2887
Stuart Longland20630c72010-06-18 12:56:10 +10002888 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2889 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002890 return 0;
2891}
2892EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2893
2894/**
2895 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2896 * mixer put callback
2897 * @kcontrol: mixer control
2898 * @uinfo: control element information
2899 *
2900 * Returns 0 for success.
2901 */
2902int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2903 struct snd_ctl_elem_value *ucontrol)
2904{
2905 struct soc_mixer_control *mc =
2906 (struct soc_mixer_control *)kcontrol->private_value;
2907 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2908 unsigned int mask = (1<<mc->shift)-1;
2909 int min = mc->min;
2910 int ret;
2911 unsigned int val, valr, oval, ovalr;
2912
2913 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2914 val &= mask;
2915 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2916 valr &= mask;
2917
2918 oval = snd_soc_read(codec, mc->reg) & mask;
2919 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2920
2921 ret = 0;
2922 if (oval != val) {
2923 ret = snd_soc_write(codec, mc->reg, val);
2924 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002925 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002926 }
2927 if (ovalr != valr) {
2928 ret = snd_soc_write(codec, mc->rreg, valr);
2929 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002930 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002931 }
2932
2933 return 0;
2934}
2935EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2936
2937/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002938 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2939 * @dai: DAI
2940 * @clk_id: DAI specific clock ID
2941 * @freq: new clock frequency in Hz
2942 * @dir: new clock direction - input/output.
2943 *
2944 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2945 */
2946int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2947 unsigned int freq, int dir)
2948{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002949 if (dai->driver && dai->driver->ops->set_sysclk)
2950 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002951 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002952 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00002953 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002954 else
2955 return -EINVAL;
2956}
2957EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2958
2959/**
Mark Brownec4ee522011-03-07 20:58:11 +00002960 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2961 * @codec: CODEC
2962 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01002963 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00002964 * @freq: new clock frequency in Hz
2965 * @dir: new clock direction - input/output.
2966 *
2967 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2968 */
2969int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01002970 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00002971{
2972 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002973 return codec->driver->set_sysclk(codec, clk_id, source,
2974 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002975 else
2976 return -EINVAL;
2977}
2978EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2979
2980/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002981 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2982 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002983 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002984 * @div: new clock divisor.
2985 *
2986 * Configures the clock dividers. This is used to derive the best DAI bit and
2987 * frame clocks from the system or master clock. It's best to set the DAI bit
2988 * and frame clocks as low as possible to save system power.
2989 */
2990int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2991 int div_id, int div)
2992{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002993 if (dai->driver && dai->driver->ops->set_clkdiv)
2994 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002995 else
2996 return -EINVAL;
2997}
2998EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2999
3000/**
3001 * snd_soc_dai_set_pll - configure DAI PLL.
3002 * @dai: DAI
3003 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003004 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003005 * @freq_in: PLL input clock frequency in Hz
3006 * @freq_out: requested PLL output clock frequency in Hz
3007 *
3008 * Configures and enables PLL to generate output clock based on input clock.
3009 */
Mark Brown85488032009-09-05 18:52:16 +01003010int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3011 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003012{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003013 if (dai->driver && dai->driver->ops->set_pll)
3014 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003015 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003016 else if (dai->codec && dai->codec->driver->set_pll)
3017 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3018 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003019 else
3020 return -EINVAL;
3021}
3022EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3023
Mark Brownec4ee522011-03-07 20:58:11 +00003024/*
3025 * snd_soc_codec_set_pll - configure codec PLL.
3026 * @codec: CODEC
3027 * @pll_id: DAI specific PLL ID
3028 * @source: DAI specific source for the PLL
3029 * @freq_in: PLL input clock frequency in Hz
3030 * @freq_out: requested PLL output clock frequency in Hz
3031 *
3032 * Configures and enables PLL to generate output clock based on input clock.
3033 */
3034int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3035 unsigned int freq_in, unsigned int freq_out)
3036{
3037 if (codec->driver->set_pll)
3038 return codec->driver->set_pll(codec, pll_id, source,
3039 freq_in, freq_out);
3040 else
3041 return -EINVAL;
3042}
3043EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3044
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003045/**
3046 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3047 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003048 * @fmt: SND_SOC_DAIFMT_ format value.
3049 *
3050 * Configures the DAI hardware format and clocking.
3051 */
3052int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3053{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003054 if (dai->driver && dai->driver->ops->set_fmt)
3055 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003056 else
3057 return -EINVAL;
3058}
3059EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3060
3061/**
3062 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3063 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003064 * @tx_mask: bitmask representing active TX slots.
3065 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003066 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003067 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003068 *
3069 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3070 * specific.
3071 */
3072int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003073 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003074{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003075 if (dai->driver && dai->driver->ops->set_tdm_slot)
3076 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003077 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003078 else
3079 return -EINVAL;
3080}
3081EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3082
3083/**
Barry Song472df3c2009-09-12 01:16:29 +08003084 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3085 * @dai: DAI
3086 * @tx_num: how many TX channels
3087 * @tx_slot: pointer to an array which imply the TX slot number channel
3088 * 0~num-1 uses
3089 * @rx_num: how many RX channels
3090 * @rx_slot: pointer to an array which imply the RX slot number channel
3091 * 0~num-1 uses
3092 *
3093 * configure the relationship between channel number and TDM slot number.
3094 */
3095int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3096 unsigned int tx_num, unsigned int *tx_slot,
3097 unsigned int rx_num, unsigned int *rx_slot)
3098{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003099 if (dai->driver && dai->driver->ops->set_channel_map)
3100 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003101 rx_num, rx_slot);
3102 else
3103 return -EINVAL;
3104}
3105EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3106
3107/**
Bharath Ramachandramurthy9c79f132011-11-28 11:18:57 -08003108 * snd_soc_dai_get_channel_map - configure DAI audio channel map
3109 * @dai: DAI
3110 * @tx_num: how many TX channels
3111 * @tx_slot: pointer to an array which imply the TX slot number channel
3112 * 0~num-1 uses
3113 * @rx_num: how many RX channels
3114 * @rx_slot: pointer to an array which imply the RX slot number channel
3115 * 0~num-1 uses
3116 *
3117 * configure the relationship between channel number and TDM slot number.
3118 */
3119int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
3120 unsigned int *tx_num, unsigned int *tx_slot,
3121 unsigned int *rx_num, unsigned int *rx_slot)
3122{
3123 if (dai->driver && dai->driver->ops->get_channel_map)
3124 return dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
3125 rx_num, rx_slot);
3126 else
3127 return -EINVAL;
3128}
3129EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
3130/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003131 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3132 * @dai: DAI
3133 * @tristate: tristate enable
3134 *
3135 * Tristates the DAI so that others can use it.
3136 */
3137int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3138{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003139 if (dai->driver && dai->driver->ops->set_tristate)
3140 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003141 else
3142 return -EINVAL;
3143}
3144EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3145
3146/**
3147 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3148 * @dai: DAI
3149 * @mute: mute enable
3150 *
3151 * Mutes the DAI DAC.
3152 */
3153int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3154{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003155 if (dai->driver && dai->driver->ops->digital_mute)
3156 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003157 else
3158 return -EINVAL;
3159}
3160EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3161
Mark Brownc5af3a22008-11-28 13:29:45 +00003162/**
3163 * snd_soc_register_card - Register a card with the ASoC core
3164 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003165 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003166 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003167 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303168int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003169{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003170 int i;
Liam Girdwood13e13a32011-01-31 21:30:52 +00003171 int ret = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003172
Mark Brownc5af3a22008-11-28 13:29:45 +00003173 if (!card->name || !card->dev)
3174 return -EINVAL;
3175
Stephen Warren5a504962011-12-21 10:40:59 -07003176 for (i = 0; i < card->num_links; i++) {
3177 struct snd_soc_dai_link *link = &card->dai_link[i];
3178
3179 /*
3180 * Codec must be specified by 1 of name or OF node,
3181 * not both or neither.
3182 */
3183 if (!!link->codec_name == !!link->codec_of_node) {
3184 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003185 "Neither/both codec name/of_node are set for %s\n",
3186 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003187 return -EINVAL;
3188 }
3189
3190 /*
3191 * Platform may be specified by either name or OF node, but
3192 * can be left unspecified, and a dummy platform will be used.
3193 */
3194 if (link->platform_name && link->platform_of_node) {
3195 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003196 "Both platform name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003197 return -EINVAL;
3198 }
3199
3200 /*
3201 * CPU DAI must be specified by 1 of name or OF node,
3202 * not both or neither.
3203 */
3204 if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
3205 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003206 "Neither/both cpu_dai name/of_node are set for %s\n",
3207 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003208 return -EINVAL;
3209 }
3210 }
3211
Mark Browned77cc12011-05-03 18:25:34 +01003212 dev_set_drvdata(card->dev, card);
3213
Stephen Warren111c6412011-01-28 14:26:35 -07003214 snd_soc_initialize_card_lists(card);
3215
Vinod Koul150dd2f2011-01-13 22:48:32 +05303216 soc_init_card_debugfs(card);
3217
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003218 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
3219 (card->num_links + card->num_aux_devs),
3220 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003221 if (card->rtd == NULL)
3222 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003223 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003224 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003225
3226 for (i = 0; i < card->num_links; i++)
3227 card->rtd[i].dai_link = &card->dai_link[i];
3228
Mark Brownc5af3a22008-11-28 13:29:45 +00003229 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003230 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003231 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003232 mutex_init(&card->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003233 mutex_init(&card->dpcm_mutex);
Sriranjan Srikantamfd7f1ad2012-08-14 17:42:06 -07003234 mutex_init(&card->dapm_power_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003235
3236 mutex_lock(&client_mutex);
3237 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003238 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00003239 mutex_unlock(&client_mutex);
3240
3241 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
3242
Liam Girdwood13e13a32011-01-31 21:30:52 +00003243 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003244}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303245EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003246
3247/**
3248 * snd_soc_unregister_card - Unregister a card with the ASoC core
3249 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003250 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003251 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003252 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303253int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003254{
Vinod Koulb0e26482011-01-13 22:48:02 +05303255 if (card->instantiated)
3256 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003257 mutex_lock(&client_mutex);
3258 list_del(&card->list);
3259 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003260 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3261
3262 return 0;
3263}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303264EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003265
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003266/*
3267 * Simplify DAI link configuration by removing ".-1" from device names
3268 * and sanitizing names.
3269 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003270static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003271{
3272 char *found, name[NAME_SIZE];
3273 int id1, id2;
3274
3275 if (dev_name(dev) == NULL)
3276 return NULL;
3277
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003278 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003279
3280 /* are we a "%s.%d" name (platform and SPI components) */
3281 found = strstr(name, dev->driver->name);
3282 if (found) {
3283 /* get ID */
3284 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3285
3286 /* discard ID from name if ID == -1 */
3287 if (*id == -1)
3288 found[strlen(dev->driver->name)] = '\0';
3289 }
3290
3291 } else {
3292 /* I2C component devices are named "bus-addr" */
3293 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3294 char tmp[NAME_SIZE];
3295
3296 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003297 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003298
3299 /* sanitize component name for DAI link creation */
3300 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003301 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003302 } else
3303 *id = 0;
3304 }
3305
3306 return kstrdup(name, GFP_KERNEL);
3307}
3308
3309/*
3310 * Simplify DAI link naming for single devices with multiple DAIs by removing
3311 * any ".-1" and using the DAI name (instead of device name).
3312 */
3313static inline char *fmt_multiple_name(struct device *dev,
3314 struct snd_soc_dai_driver *dai_drv)
3315{
3316 if (dai_drv->name == NULL) {
3317 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3318 dev_name(dev));
3319 return NULL;
3320 }
3321
3322 return kstrdup(dai_drv->name, GFP_KERNEL);
3323}
3324
Mark Brown91151712008-11-30 23:31:24 +00003325/**
3326 * snd_soc_register_dai - Register a DAI with the ASoC core
3327 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003328 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003329 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003330int snd_soc_register_dai(struct device *dev,
3331 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003332{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003333 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003334
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003335 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003336
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003337 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3338 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003339 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003340
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003341 /* create DAI component name */
3342 dai->name = fmt_single_name(dev, &dai->id);
3343 if (dai->name == NULL) {
3344 kfree(dai);
3345 return -ENOMEM;
3346 }
3347
3348 dai->dev = dev;
3349 dai->driver = dai_drv;
3350 if (!dai->driver->ops)
3351 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003352
3353 mutex_lock(&client_mutex);
3354 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003355 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003356 mutex_unlock(&client_mutex);
3357
3358 pr_debug("Registered DAI '%s'\n", dai->name);
3359
3360 return 0;
3361}
3362EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3363
3364/**
3365 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3366 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003367 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003368 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003369void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003370{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003371 struct snd_soc_dai *dai;
3372
3373 list_for_each_entry(dai, &dai_list, list) {
3374 if (dev == dai->dev)
3375 goto found;
3376 }
3377 return;
3378
3379found:
Mark Brown91151712008-11-30 23:31:24 +00003380 mutex_lock(&client_mutex);
3381 list_del(&dai->list);
3382 mutex_unlock(&client_mutex);
3383
3384 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003385 kfree(dai->name);
3386 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003387}
3388EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3389
3390/**
3391 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3392 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003393 * @dai: Array of DAIs to register
3394 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003395 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003396int snd_soc_register_dais(struct device *dev,
3397 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003398{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003399 struct snd_soc_dai *dai;
3400 int i, ret = 0;
3401
Liam Girdwood720ffa42010-08-18 00:30:30 +01003402 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003403
3404 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003405
3406 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003407 if (dai == NULL) {
3408 ret = -ENOMEM;
3409 goto err;
3410 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003411
3412 /* create DAI component name */
3413 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3414 if (dai->name == NULL) {
3415 kfree(dai);
3416 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003417 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003418 }
3419
3420 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003421 dai->driver = &dai_drv[i];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003422
Mark Brown0f9141c2010-10-12 15:43:21 +01003423 if (dai->driver->id)
3424 dai->id = dai->driver->id;
3425 else
3426 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003427 if (!dai->driver->ops)
3428 dai->driver->ops = &null_dai_ops;
3429
3430 mutex_lock(&client_mutex);
3431 list_add(&dai->list, &dai_list);
3432 mutex_unlock(&client_mutex);
3433
3434 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003435 }
3436
Axel Lin1dcb4f32010-12-06 16:48:03 +08003437 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003438 snd_soc_instantiate_cards();
Axel Lin1dcb4f32010-12-06 16:48:03 +08003439 mutex_unlock(&client_mutex);
Mark Brown91151712008-11-30 23:31:24 +00003440 return 0;
3441
3442err:
3443 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003444 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003445
3446 return ret;
3447}
3448EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3449
3450/**
3451 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3452 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003453 * @dai: Array of DAIs to unregister
3454 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003455 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003456void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003457{
3458 int i;
3459
3460 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003461 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003462}
3463EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3464
Mark Brown12a48a8c2008-12-03 19:40:30 +00003465/**
3466 * snd_soc_register_platform - Register a platform with the ASoC core
3467 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003468 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003469 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003470int snd_soc_register_platform(struct device *dev,
3471 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003472{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003473 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003474
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003475 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3476
3477 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3478 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003479 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003480
3481 /* create platform component name */
3482 platform->name = fmt_single_name(dev, &platform->id);
3483 if (platform->name == NULL) {
3484 kfree(platform);
3485 return -ENOMEM;
3486 }
3487
3488 platform->dev = dev;
3489 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003490 platform->dapm.dev = dev;
3491 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003492 platform->dapm.stream_event = platform_drv->stream_event;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003493
3494 mutex_lock(&client_mutex);
3495 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003496 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003497 mutex_unlock(&client_mutex);
3498
3499 pr_debug("Registered platform '%s'\n", platform->name);
3500
3501 return 0;
3502}
3503EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3504
3505/**
3506 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3507 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003508 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003509 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003510void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003511{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003512 struct snd_soc_platform *platform;
3513
3514 list_for_each_entry(platform, &platform_list, list) {
3515 if (dev == platform->dev)
3516 goto found;
3517 }
3518 return;
3519
3520found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003521 mutex_lock(&client_mutex);
3522 list_del(&platform->list);
3523 mutex_unlock(&client_mutex);
3524
3525 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003526 kfree(platform->name);
3527 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003528}
3529EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3530
Mark Brown151ab222009-05-09 16:22:58 +01003531static u64 codec_format_map[] = {
3532 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3533 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3534 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3535 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3536 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3537 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3538 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3539 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3540 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3541 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3542 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3543 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3544 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3545 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3546 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3547 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3548};
3549
3550/* Fix up the DAI formats for endianness: codecs don't actually see
3551 * the endianness of the data but we're using the CPU format
3552 * definitions which do need to include endianness so we ensure that
3553 * codec DAIs always have both big and little endian variants set.
3554 */
3555static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3556{
3557 int i;
3558
3559 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3560 if (stream->formats & codec_format_map[i])
3561 stream->formats |= codec_format_map[i];
3562}
3563
Mark Brown0d0cf002008-12-10 14:32:45 +00003564/**
3565 * snd_soc_register_codec - Register a codec with the ASoC core
3566 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003567 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003568 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003569int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003570 const struct snd_soc_codec_driver *codec_drv,
3571 struct snd_soc_dai_driver *dai_drv,
3572 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003573{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003574 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003575 struct snd_soc_codec *codec;
3576 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003577
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003578 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003579
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003580 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3581 if (codec == NULL)
3582 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003583
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003584 /* create CODEC component name */
3585 codec->name = fmt_single_name(dev, &codec->id);
3586 if (codec->name == NULL) {
3587 kfree(codec);
3588 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003589 }
3590
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003591 if (codec_drv->compress_type)
3592 codec->compress_type = codec_drv->compress_type;
3593 else
3594 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3595
Mark Brownc3acec22010-12-02 16:15:29 +00003596 codec->write = codec_drv->write;
3597 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003598 codec->volatile_register = codec_drv->volatile_register;
3599 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003600 codec->writable_register = codec_drv->writable_register;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003601 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3602 codec->dapm.dev = dev;
3603 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003604 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003605 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003606 codec->dev = dev;
3607 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003608 codec->num_dai = num_dai;
3609 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003610
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003611 /* allocate CODEC register cache */
3612 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003613 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003614 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003615 /* it is necessary to make a copy of the default register cache
3616 * because in the case of using a compression type that requires
3617 * the default register cache to be marked as __devinitconst the
3618 * kernel might have freed the array by the time we initialize
3619 * the cache.
3620 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003621 if (codec_drv->reg_cache_default) {
3622 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3623 reg_size, GFP_KERNEL);
3624 if (!codec->reg_def_copy) {
3625 ret = -ENOMEM;
3626 goto fail;
3627 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003628 }
3629 }
3630
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003631 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3632 if (!codec->volatile_register)
3633 codec->volatile_register = snd_soc_default_volatile_register;
3634 if (!codec->readable_register)
3635 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003636 if (!codec->writable_register)
3637 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003638 }
3639
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003640 for (i = 0; i < num_dai; i++) {
3641 fixup_codec_formats(&dai_drv[i].playback);
3642 fixup_codec_formats(&dai_drv[i].capture);
3643 }
3644
Mark Brown26b01cc2010-08-18 20:20:55 +01003645 /* register any DAIs */
3646 if (num_dai) {
3647 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3648 if (ret < 0)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003649 goto fail;
Mark Brown26b01cc2010-08-18 20:20:55 +01003650 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003651
Mark Brown0d0cf002008-12-10 14:32:45 +00003652 mutex_lock(&client_mutex);
3653 list_add(&codec->list, &codec_list);
3654 snd_soc_instantiate_cards();
3655 mutex_unlock(&client_mutex);
3656
3657 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003658 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003659
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003660fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003661 kfree(codec->reg_def_copy);
3662 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003663 kfree(codec->name);
3664 kfree(codec);
3665 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003666}
3667EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3668
3669/**
3670 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3671 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003672 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003673 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003674void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003675{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003676 struct snd_soc_codec *codec;
3677 int i;
3678
3679 list_for_each_entry(codec, &codec_list, list) {
3680 if (dev == codec->dev)
3681 goto found;
3682 }
3683 return;
3684
3685found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003686 if (codec->num_dai)
3687 for (i = 0; i < codec->num_dai; i++)
3688 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003689
Mark Brown0d0cf002008-12-10 14:32:45 +00003690 mutex_lock(&client_mutex);
3691 list_del(&codec->list);
3692 mutex_unlock(&client_mutex);
3693
3694 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003695
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003696 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003697 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003698 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003699 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003700}
3701EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3702
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003703/* Retrieve a card's name from device tree */
3704int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3705 const char *propname)
3706{
3707 struct device_node *np = card->dev->of_node;
3708 int ret;
3709
3710 ret = of_property_read_string_index(np, propname, 0, &card->name);
3711 /*
3712 * EINVAL means the property does not exist. This is fine providing
3713 * card->name was previously set, which is checked later in
3714 * snd_soc_register_card.
3715 */
3716 if (ret < 0 && ret != -EINVAL) {
3717 dev_err(card->dev,
3718 "Property '%s' could not be read: %d\n",
3719 propname, ret);
3720 return ret;
3721 }
3722
3723 return 0;
3724}
3725EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3726
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003727int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3728 const char *propname)
3729{
3730 struct device_node *np = card->dev->of_node;
3731 int num_routes;
3732 struct snd_soc_dapm_route *routes;
3733 int i, ret;
3734
3735 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08003736 if (num_routes < 0 || num_routes & 1) {
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003737 dev_err(card->dev,
Richard Zhaoc34ce322012-04-24 15:24:43 +08003738 "Property '%s' does not exist or its length is not even\n",
3739 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003740 return -EINVAL;
3741 }
3742 num_routes /= 2;
3743 if (!num_routes) {
3744 dev_err(card->dev,
3745 "Property '%s's length is zero\n",
3746 propname);
3747 return -EINVAL;
3748 }
3749
3750 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3751 GFP_KERNEL);
3752 if (!routes) {
3753 dev_err(card->dev,
3754 "Could not allocate DAPM route table\n");
3755 return -EINVAL;
3756 }
3757
3758 for (i = 0; i < num_routes; i++) {
3759 ret = of_property_read_string_index(np, propname,
3760 2 * i, &routes[i].sink);
3761 if (ret) {
3762 dev_err(card->dev,
3763 "Property '%s' index %d could not be read: %d\n",
3764 propname, 2 * i, ret);
3765 return -EINVAL;
3766 }
3767 ret = of_property_read_string_index(np, propname,
3768 (2 * i) + 1, &routes[i].source);
3769 if (ret) {
3770 dev_err(card->dev,
3771 "Property '%s' index %d could not be read: %d\n",
3772 propname, (2 * i) + 1, ret);
3773 return -EINVAL;
3774 }
3775 }
3776
3777 card->num_dapm_routes = num_routes;
3778 card->dapm_routes = routes;
3779
3780 return 0;
3781}
3782EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3783
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003784static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003785{
Mark Brown384c89e2008-12-03 17:34:03 +00003786#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003787 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3788 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Mark Brown384c89e2008-12-03 17:34:03 +00003789 printk(KERN_WARNING
3790 "ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00003791 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00003792 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003793
Mark Brown8a9dab12011-01-10 22:25:21 +00003794 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01003795 &codec_list_fops))
3796 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3797
Mark Brown8a9dab12011-01-10 22:25:21 +00003798 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01003799 &dai_list_fops))
3800 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003801
Mark Brown8a9dab12011-01-10 22:25:21 +00003802 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01003803 &platform_list_fops))
3804 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003805#endif
3806
Mark Brownfb257892011-04-28 10:57:54 +01003807 snd_soc_util_init();
3808
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003809 return platform_driver_register(&soc_driver);
3810}
Mark Brown4abe8e12010-10-12 17:41:03 +01003811module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003812
Mark Brown7d8c16a2008-11-30 22:11:24 +00003813static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003814{
Mark Brownfb257892011-04-28 10:57:54 +01003815 snd_soc_util_exit();
3816
Mark Brown384c89e2008-12-03 17:34:03 +00003817#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003818 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00003819#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003820 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003821}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003822module_exit(snd_soc_exit);
3823
3824/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003825MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003826MODULE_DESCRIPTION("ALSA SoC Core");
3827MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003828MODULE_ALIAS("platform:soc-audio");