blob: 0a8a5f50b4665e23eed1cf617c72a040fba37246 [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>
Liam Girdwood01d75842012-04-25 12:12:49 +010042#include <sound/soc-dpcm.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020043#include <sound/initval.h>
44
Mark Browna8b1d342010-11-03 18:05:58 -040045#define CREATE_TRACE_POINTS
46#include <trace/events/asoc.h>
47
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000048#define NAME_SIZE 32
49
Frank Mandarinodb2a4162006-10-06 18:31:09 +020050static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
51
Mark Brown384c89e2008-12-03 17:34:03 +000052#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000053struct dentry *snd_soc_debugfs_root;
54EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000055#endif
56
Mark Brownc5af3a22008-11-28 13:29:45 +000057static DEFINE_MUTEX(client_mutex);
Mark Brown91151712008-11-30 23:31:24 +000058static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000059static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000060static LIST_HEAD(codec_list);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -070061static LIST_HEAD(component_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000062
Frank Mandarinodb2a4162006-10-06 18:31:09 +020063/*
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
67 */
68static int pmdown_time = 5000;
69module_param(pmdown_time, int, 0);
70MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000072/* returns the minimum number of bytes needed to represent
73 * a particular given value */
74static int min_bytes_needed(unsigned long val)
75{
76 int c = 0;
77 int i;
78
79 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
80 if (val & (1UL << i))
81 break;
82 c = (sizeof val * 8) - c;
83 if (!c || (c % 8))
84 c = (c + 8) / 8;
85 else
86 c /= 8;
87 return c;
88}
89
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000090/* fill buf which is 'len' bytes with a formatted
91 * string of the form 'reg: value\n' */
92static int format_register_str(struct snd_soc_codec *codec,
93 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +000094{
Stephen Warren00b317a2011-04-01 14:50:44 -060095 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
96 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000097 int ret;
98 char tmpbuf[len + 1];
99 char regbuf[regsize + 1];
100
101 /* since tmpbuf is allocated on the stack, warn the callers if they
102 * try to abuse this function */
103 WARN_ON(len > 63);
104
105 /* +2 for ': ' and + 1 for '\n' */
106 if (wordsize + regsize + 2 + 1 != len)
107 return -EINVAL;
108
Mark Brown25032c12011-08-01 13:52:48 +0900109 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000110 if (ret < 0) {
111 memset(regbuf, 'X', regsize);
112 regbuf[regsize] = '\0';
113 } else {
114 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
115 }
116
117 /* prepare the buffer */
118 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
119 /* copy it back to the caller without the '\0' */
120 memcpy(buf, tmpbuf, len);
121
122 return 0;
123}
124
125/* codec register dump */
126static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
127 size_t count, loff_t pos)
128{
129 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000130 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000131 int len;
132 size_t total = 0;
133 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000134
Stephen Warren00b317a2011-04-01 14:50:44 -0600135 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
136 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000137
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000138 len = wordsize + regsize + 2 + 1;
139
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000140 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000141 return 0;
142
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000143 if (codec->driver->reg_cache_step)
144 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000145
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000146 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausenb92d1502011-08-27 18:24:14 +0200147 if (!snd_soc_codec_readable_register(codec, i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000148 continue;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000149 if (codec->driver->display_register) {
150 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000151 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100152 } else {
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000153 /* only support larger than PAGE_SIZE bytes debugfs
154 * entries for the default case */
155 if (p >= pos) {
156 if (total + len >= count - 1)
157 break;
158 format_register_str(codec, i, buf + total, len);
159 total += len;
160 }
161 p += len;
Mark Brown5164d742010-07-14 16:14:33 +0100162 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000163 }
164
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000165 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000166
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000167 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000168}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000169
Mark Brown2624d5f2009-11-03 21:56:13 +0000170static ssize_t codec_reg_show(struct device *dev,
171 struct device_attribute *attr, char *buf)
172{
Mark Brown36ae1a92012-01-06 17:12:45 -0800173 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000174
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000175 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000176}
177
178static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
179
Mark Browndbe21402010-02-12 11:37:24 +0000180static ssize_t pmdown_time_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);
Mark Browndbe21402010-02-12 11:37:24 +0000184
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000185 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000186}
187
188static ssize_t pmdown_time_set(struct device *dev,
189 struct device_attribute *attr,
190 const char *buf, size_t count)
191{
Mark Brown36ae1a92012-01-06 17:12:45 -0800192 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700193 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000194
Mark Brownc593b522010-10-27 20:11:17 -0700195 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
196 if (ret)
197 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000198
199 return count;
200}
201
202static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
203
Mark Brown2624d5f2009-11-03 21:56:13 +0000204#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000205static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000206 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000207{
208 ssize_t ret;
209 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000210 char *buf;
211
212 if (*ppos < 0 || !count)
213 return -EINVAL;
214
215 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000216 if (!buf)
217 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000218
219 ret = soc_codec_reg_show(codec, buf, count, *ppos);
220 if (ret >= 0) {
221 if (copy_to_user(user_buf, buf, ret)) {
222 kfree(buf);
223 return -EFAULT;
224 }
225 *ppos += ret;
226 }
227
Mark Brown2624d5f2009-11-03 21:56:13 +0000228 kfree(buf);
229 return ret;
230}
231
232static ssize_t codec_reg_write_file(struct file *file,
233 const char __user *user_buf, size_t count, loff_t *ppos)
234{
235 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700236 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000237 char *start = buf;
238 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000239 struct snd_soc_codec *codec = file->private_data;
240
241 buf_size = min(count, (sizeof(buf)-1));
242 if (copy_from_user(buf, user_buf, buf_size))
243 return -EFAULT;
244 buf[buf_size] = 0;
245
Mark Brown2624d5f2009-11-03 21:56:13 +0000246 while (*start == ' ')
247 start++;
248 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000249 while (*start == ' ')
250 start++;
251 if (strict_strtoul(start, 16, &value))
252 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000253
254 /* Userspace has been fiddling around behind the kernel's back */
Rusty Russell373d4d02013-01-21 17:17:39 +1030255 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
Mark Brown0d51a9c2011-01-06 16:04:57 +0000256
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000257 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000258 return buf_size;
259}
260
261static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700262 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000263 .read = codec_reg_read_file,
264 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200265 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000266};
267
268static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
269{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200270 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
271
272 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
273 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000274 if (!codec->debugfs_codec_root) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000275 dev_warn(codec->dev, "ASoC: Failed to create codec debugfs"
276 " directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000277 return;
278 }
279
Mark Brownaaee8ef2011-01-26 20:53:50 +0000280 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
281 &codec->cache_sync);
282 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
283 &codec->cache_only);
284
Mark Brown2624d5f2009-11-03 21:56:13 +0000285 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
286 codec->debugfs_codec_root,
287 codec, &codec_reg_fops);
288 if (!codec->debugfs_reg)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000289 dev_warn(codec->dev, "ASoC: Failed to create codec register"
290 " debugfs file\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000291
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200292 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000293}
294
295static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
296{
297 debugfs_remove_recursive(codec->debugfs_codec_root);
298}
299
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000300static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
301{
302 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
303
304 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
305 debugfs_card_root);
306 if (!platform->debugfs_platform_root) {
307 dev_warn(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000308 "ASoC: Failed to create platform debugfs directory\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000309 return;
310 }
311
312 snd_soc_dapm_debugfs_init(&platform->dapm,
313 platform->debugfs_platform_root);
314}
315
316static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
317{
318 debugfs_remove_recursive(platform->debugfs_platform_root);
319}
320
Mark Brownc3c5a192010-09-15 18:15:14 +0100321static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
322 size_t count, loff_t *ppos)
323{
324 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100325 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100326 struct snd_soc_codec *codec;
327
328 if (!buf)
329 return -ENOMEM;
330
Mark Brown2b194f9d2010-10-13 10:52:16 +0100331 list_for_each_entry(codec, &codec_list, list) {
332 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
333 codec->name);
334 if (len >= 0)
335 ret += len;
336 if (ret > PAGE_SIZE) {
337 ret = PAGE_SIZE;
338 break;
339 }
340 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100341
342 if (ret >= 0)
343 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
344
345 kfree(buf);
346
347 return ret;
348}
349
350static const struct file_operations codec_list_fops = {
351 .read = codec_list_read_file,
352 .llseek = default_llseek,/* read accesses f_pos */
353};
354
Mark Brownf3208782010-09-15 18:19:07 +0100355static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
356 size_t count, loff_t *ppos)
357{
358 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100359 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100360 struct snd_soc_dai *dai;
361
362 if (!buf)
363 return -ENOMEM;
364
Mark Brown2b194f9d2010-10-13 10:52:16 +0100365 list_for_each_entry(dai, &dai_list, list) {
366 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
367 if (len >= 0)
368 ret += len;
369 if (ret > PAGE_SIZE) {
370 ret = PAGE_SIZE;
371 break;
372 }
373 }
Mark Brownf3208782010-09-15 18:19:07 +0100374
Mark Brown2b194f9d2010-10-13 10:52:16 +0100375 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100376
377 kfree(buf);
378
379 return ret;
380}
381
382static const struct file_operations dai_list_fops = {
383 .read = dai_list_read_file,
384 .llseek = default_llseek,/* read accesses f_pos */
385};
386
Mark Brown19c7ac22010-09-15 18:22:40 +0100387static ssize_t platform_list_read_file(struct file *file,
388 char __user *user_buf,
389 size_t count, loff_t *ppos)
390{
391 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100392 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100393 struct snd_soc_platform *platform;
394
395 if (!buf)
396 return -ENOMEM;
397
Mark Brown2b194f9d2010-10-13 10:52:16 +0100398 list_for_each_entry(platform, &platform_list, list) {
399 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
400 platform->name);
401 if (len >= 0)
402 ret += len;
403 if (ret > PAGE_SIZE) {
404 ret = PAGE_SIZE;
405 break;
406 }
407 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100408
Mark Brown2b194f9d2010-10-13 10:52:16 +0100409 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100410
411 kfree(buf);
412
413 return ret;
414}
415
416static const struct file_operations platform_list_fops = {
417 .read = platform_list_read_file,
418 .llseek = default_llseek,/* read accesses f_pos */
419};
420
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200421static void soc_init_card_debugfs(struct snd_soc_card *card)
422{
423 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000424 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200425 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200426 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100427 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200428 return;
429 }
430
431 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
432 card->debugfs_card_root,
433 &card->pop_time);
434 if (!card->debugfs_pop_time)
435 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000436 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200437}
438
439static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
440{
441 debugfs_remove_recursive(card->debugfs_card_root);
442}
443
Mark Brown2624d5f2009-11-03 21:56:13 +0000444#else
445
446static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
447{
448}
449
450static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
451{
452}
Axel Linb95fccb2010-11-09 17:06:44 +0800453
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000454static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
455{
456}
457
458static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
459{
460}
461
Axel Linb95fccb2010-11-09 17:06:44 +0800462static inline void soc_init_card_debugfs(struct snd_soc_card *card)
463{
464}
465
466static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
467{
468}
Mark Brown2624d5f2009-11-03 21:56:13 +0000469#endif
470
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100471struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
472 const char *dai_link, int stream)
473{
474 int i;
475
476 for (i = 0; i < card->num_links; i++) {
477 if (card->rtd[i].dai_link->no_pcm &&
478 !strcmp(card->rtd[i].dai_link->name, dai_link))
479 return card->rtd[i].pcm->streams[stream].substream;
480 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000481 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100482 return NULL;
483}
484EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
485
486struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
487 const char *dai_link)
488{
489 int i;
490
491 for (i = 0; i < card->num_links; i++) {
492 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
493 return &card->rtd[i];
494 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000495 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100496 return NULL;
497}
498EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
499
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200500#ifdef CONFIG_SND_SOC_AC97_BUS
501/* unregister ac97 codec */
502static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
503{
504 if (codec->ac97->dev.bus)
505 device_unregister(&codec->ac97->dev);
506 return 0;
507}
508
509/* stop no dev release warning */
510static void soc_ac97_device_release(struct device *dev){}
511
512/* register ac97 codec to bus */
513static int soc_ac97_dev_register(struct snd_soc_codec *codec)
514{
515 int err;
516
517 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100518 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200519 codec->ac97->dev.release = soc_ac97_device_release;
520
Kay Sieversbb072bf2008-11-02 03:50:35 +0100521 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000522 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200523 err = device_register(&codec->ac97->dev);
524 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000525 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200526 codec->ac97->dev.bus = NULL;
527 return err;
528 }
529 return 0;
530}
531#endif
532
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000533#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200534/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000535int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200536{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000537 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200538 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200539 int i;
540
Daniel Macke3509ff2009-06-03 17:44:49 +0200541 /* If the initialization of this soc device failed, there is no codec
542 * associated with it. Just bail out in this case.
543 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000544 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200545 return 0;
546
Andy Green6ed25972008-06-13 16:24:05 +0100547 /* Due to the resume being scheduled into a workqueue we could
548 * suspend before that's finished - wait for it to complete.
549 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000550 snd_power_lock(card->snd_card);
551 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
552 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100553
554 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000555 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100556
Mark Browna00f90f2010-12-02 16:24:24 +0000557 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 for (i = 0; i < card->num_rtd; i++) {
559 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
560 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100561
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100563 continue;
564
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000565 if (drv->ops->digital_mute && dai->playback_active)
566 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200567 }
568
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100569 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000570 for (i = 0; i < card->num_rtd; i++) {
571 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100572 continue;
573
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000574 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100575 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100576
Mark Brown87506542008-11-18 20:50:34 +0000577 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000578 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200579
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000580 for (i = 0; i < card->num_rtd; i++) {
581 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
582 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100583
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000584 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100585 continue;
586
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000587 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
588 cpu_dai->driver->suspend(cpu_dai);
589 if (platform->driver->suspend && !platform->suspended) {
590 platform->driver->suspend(cpu_dai);
591 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100592 }
593 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200594
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000595 /* close any waiting streams and save state */
596 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo43829732012-08-20 14:51:24 -0700597 flush_delayed_work(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200598 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100600
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000601 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000602
603 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100604 continue;
605
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800606 snd_soc_dapm_stream_event(&card->rtd[i],
607 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800608 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000609
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800610 snd_soc_dapm_stream_event(&card->rtd[i],
611 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800612 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000613 }
614
Mark Browne2d32ff2012-08-31 17:38:32 -0700615 /* Recheck all analogue paths too */
616 dapm_mark_io_dirty(&card->dapm);
617 snd_soc_dapm_sync(&card->dapm);
618
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000619 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200620 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 /* If there are paths active then the CODEC will be held with
622 * bias _ON and should not be suspended. */
623 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200624 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000625 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000626 /*
627 * If the CODEC is capable of idle
628 * bias off then being in STANDBY
629 * means it's doing something,
630 * otherwise fall through.
631 */
632 if (codec->dapm.idle_bias_off) {
633 dev_dbg(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000634 "ASoC: idle_bias_off CODEC on"
635 " over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000636 break;
637 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000638 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100639 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000640 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900641 codec->cache_sync = 1;
Mark Brownda8b8e02012-09-12 12:21:52 +0800642 if (codec->using_regmap)
643 regcache_mark_dirty(codec->control_data);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000644 break;
645 default:
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000646 dev_dbg(codec->dev, "ASoC: CODEC is on"
647 " over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000648 break;
649 }
650 }
651 }
652
653 for (i = 0; i < card->num_rtd; i++) {
654 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
655
656 if (card->rtd[i].dai_link->ignore_suspend)
657 continue;
658
659 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
660 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200661 }
662
Mark Brown87506542008-11-18 20:50:34 +0000663 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000664 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200665
666 return 0;
667}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000668EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200669
Andy Green6ed25972008-06-13 16:24:05 +0100670/* deferred resume work, so resume can complete before we finished
671 * setting our codec back up, which can be very slow on I2C
672 */
673static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200674{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000675 struct snd_soc_card *card =
676 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200677 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200678 int i;
679
Andy Green6ed25972008-06-13 16:24:05 +0100680 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
681 * so userspace apps are blocked from touching us
682 */
683
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000684 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100685
Mark Brown99497882010-05-07 20:24:05 +0100686 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000687 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100688
Mark Brown87506542008-11-18 20:50:34 +0000689 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000690 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200691
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000692 /* resume AC97 DAIs */
693 for (i = 0; i < card->num_rtd; i++) {
694 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100695
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000696 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100697 continue;
698
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000699 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
700 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200701 }
702
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200703 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000704 /* If the CODEC was idle over suspend then it will have been
705 * left with bias OFF or STANDBY and suspended so we must now
706 * resume. Otherwise the suspend was suppressed.
707 */
708 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200709 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000710 case SND_SOC_BIAS_STANDBY:
711 case SND_SOC_BIAS_OFF:
712 codec->driver->resume(codec);
713 codec->suspended = 0;
714 break;
715 default:
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000716 dev_dbg(codec->dev, "ASoC: CODEC was on over"
717 " suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000718 break;
719 }
Mark Brown1547aba2010-05-07 21:11:40 +0100720 }
721 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200722
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000723 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100724
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000725 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100726 continue;
727
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800728 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000729 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800730 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000731
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800732 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000733 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800734 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200735 }
736
Mark Brown3ff3f642008-05-19 12:32:25 +0200737 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000738 for (i = 0; i < card->num_rtd; i++) {
739 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
740 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100741
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000742 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100743 continue;
744
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000745 if (drv->ops->digital_mute && dai->playback_active)
746 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200747 }
748
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000749 for (i = 0; i < card->num_rtd; i++) {
750 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
751 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100752
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000753 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100754 continue;
755
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000756 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
757 cpu_dai->driver->resume(cpu_dai);
758 if (platform->driver->resume && platform->suspended) {
759 platform->driver->resume(cpu_dai);
760 platform->suspended = 0;
761 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200762 }
763
Mark Brown87506542008-11-18 20:50:34 +0000764 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000765 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200766
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000767 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100768
769 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000770 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700771
772 /* Recheck all analogue paths too */
773 dapm_mark_io_dirty(&card->dapm);
774 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100775}
776
777/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000778int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100779{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000780 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600781 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200782
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800783 /* If the initialization of this soc device failed, there is no codec
784 * associated with it. Just bail out in this case.
785 */
786 if (list_empty(&card->codec_dev_list))
787 return 0;
788
Mark Brown64ab9ba2009-03-31 11:27:03 +0100789 /* AC97 devices might have other drivers hanging off them so
790 * need to resume immediately. Other drivers don't have that
791 * problem and may take a substantial amount of time to resume
792 * due to I/O costs and anti-pop so handle them out of line.
793 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000794 for (i = 0; i < card->num_rtd; i++) {
795 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600796 ac97_control |= cpu_dai->driver->ac97_control;
797 }
798 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000799 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600800 soc_resume_deferred(&card->deferred_resume_work);
801 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000802 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600803 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000804 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100805 }
Andy Green6ed25972008-06-13 16:24:05 +0100806
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200807 return 0;
808}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000809EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200810#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000811#define snd_soc_suspend NULL
812#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200813#endif
814
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100815static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800816};
817
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000818static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200819{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000820 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
821 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000822 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000823 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000824 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100825 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200826
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000827 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000828
Mark Brownb19e6e72012-03-14 21:18:39 +0000829 /* Find CPU DAI from registered DAIs*/
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000830 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600831 if (dai_link->cpu_of_node &&
832 (cpu_dai->dev->of_node != dai_link->cpu_of_node))
833 continue;
834 if (dai_link->cpu_name &&
835 strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
836 continue;
837 if (dai_link->cpu_dai_name &&
838 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
839 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700840
841 rtd->cpu_dai = cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000842 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000843
844 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000845 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000846 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000847 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000848 }
849
Mark Brownb19e6e72012-03-14 21:18:39 +0000850 /* Find CODEC from registered CODECs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000851 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700852 if (dai_link->codec_of_node) {
853 if (codec->dev->of_node != dai_link->codec_of_node)
854 continue;
855 } else {
856 if (strcmp(codec->name, dai_link->codec_name))
857 continue;
858 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000859
Stephen Warren2610ab72011-12-07 13:58:27 -0700860 rtd->codec = codec;
861
862 /*
863 * CODEC found, so find CODEC DAI from registered DAIs from
864 * this CODEC
865 */
866 list_for_each_entry(codec_dai, &dai_list, list) {
867 if (codec->dev == codec_dai->dev &&
868 !strcmp(codec_dai->name,
869 dai_link->codec_dai_name)) {
870
871 rtd->codec_dai = codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000872 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000873 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000874
875 if (!rtd->codec_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000876 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
Stephen Warren2610ab72011-12-07 13:58:27 -0700877 dai_link->codec_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000878 return -EPROBE_DEFER;
879 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000880 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000881
Mark Brownb19e6e72012-03-14 21:18:39 +0000882 if (!rtd->codec) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000883 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
Mark Brownb19e6e72012-03-14 21:18:39 +0000884 dai_link->codec_name);
885 return -EPROBE_DEFER;
886 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100887
888 /* if there's no platform we match on the empty platform */
889 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700890 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100891 platform_name = "snd-soc-dummy";
892
Mark Brownb19e6e72012-03-14 21:18:39 +0000893 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000894 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700895 if (dai_link->platform_of_node) {
896 if (platform->dev->of_node !=
897 dai_link->platform_of_node)
898 continue;
899 } else {
900 if (strcmp(platform->name, platform_name))
901 continue;
902 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700903
904 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000905 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000906 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000907 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000908 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000909 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000910 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000911
912 card->num_rtd++;
913
914 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000915}
916
Stephen Warrend12cd192012-06-08 12:34:22 -0600917static int soc_remove_platform(struct snd_soc_platform *platform)
918{
919 int ret;
920
921 if (platform->driver->remove) {
922 ret = platform->driver->remove(platform);
923 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000924 dev_err(platform->dev, "ASoC: failed to remove %d\n",
925 ret);
Stephen Warrend12cd192012-06-08 12:34:22 -0600926 }
927
928 /* Make sure all DAPM widgets are freed */
929 snd_soc_dapm_free(&platform->dapm);
930
931 soc_cleanup_platform_debugfs(platform);
932 platform->probed = 0;
933 list_del(&platform->card_list);
934 module_put(platform->dev->driver->owner);
935
936 return 0;
937}
938
Jarkko Nikula589c3562010-12-06 16:27:07 +0200939static void soc_remove_codec(struct snd_soc_codec *codec)
940{
941 int err;
942
943 if (codec->driver->remove) {
944 err = codec->driver->remove(codec);
945 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000946 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200947 }
948
949 /* Make sure all DAPM widgets are freed */
950 snd_soc_dapm_free(&codec->dapm);
951
952 soc_cleanup_codec_debugfs(codec);
953 codec->probed = 0;
954 list_del(&codec->card_list);
955 module_put(codec->dev->driver->owner);
956}
957
Stephen Warren62ae68f2012-06-08 12:34:23 -0600958static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000959{
960 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000961 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
962 int err;
963
964 /* unregister the rtd device */
965 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800966 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
967 device_remove_file(rtd->dev, &dev_attr_codec_reg);
968 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000969 rtd->dev_registered = 0;
970 }
971
972 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100973 if (codec_dai && codec_dai->probed &&
974 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000975 if (codec_dai->driver->remove) {
976 err = codec_dai->driver->remove(codec_dai);
977 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000978 dev_err(codec_dai->dev,
979 "ASoC: failed to remove %s: %d\n",
980 codec_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000981 }
982 codec_dai->probed = 0;
983 list_del(&codec_dai->card_list);
984 }
985
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000986 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100987 if (cpu_dai && cpu_dai->probed &&
988 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000989 if (cpu_dai->driver->remove) {
990 err = cpu_dai->driver->remove(cpu_dai);
991 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000992 dev_err(cpu_dai->dev,
993 "ASoC: failed to remove %s: %d\n",
994 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000995 }
996 cpu_dai->probed = 0;
997 list_del(&cpu_dai->card_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -0600998
Stephen Warren18d75642012-06-08 12:34:21 -0600999 if (!cpu_dai->codec) {
1000 snd_soc_dapm_free(&cpu_dai->dapm);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001001 module_put(cpu_dai->dev->driver->owner);
Stephen Warren18d75642012-06-08 12:34:21 -06001002 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001003 }
1004}
1005
Stephen Warren62ae68f2012-06-08 12:34:23 -06001006static void soc_remove_link_components(struct snd_soc_card *card, int num,
1007 int order)
1008{
1009 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1010 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1011 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1012 struct snd_soc_platform *platform = rtd->platform;
1013 struct snd_soc_codec *codec;
1014
1015 /* remove the platform */
1016 if (platform && platform->probed &&
1017 platform->driver->remove_order == order) {
1018 soc_remove_platform(platform);
1019 }
1020
1021 /* remove the CODEC-side CODEC */
1022 if (codec_dai) {
1023 codec = codec_dai->codec;
1024 if (codec && codec->probed &&
1025 codec->driver->remove_order == order)
1026 soc_remove_codec(codec);
1027 }
1028
1029 /* remove any CPU-side CODEC */
1030 if (cpu_dai) {
1031 codec = cpu_dai->codec;
1032 if (codec && codec->probed &&
1033 codec->driver->remove_order == order)
1034 soc_remove_codec(codec);
1035 }
1036}
1037
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001038static void soc_remove_dai_links(struct snd_soc_card *card)
1039{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001040 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001041
Liam Girdwood0168bf02011-06-07 16:08:05 +01001042 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1043 order++) {
1044 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001045 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001046 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001047
1048 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1049 order++) {
1050 for (dai = 0; dai < card->num_rtd; dai++)
1051 soc_remove_link_components(card, dai, order);
1052 }
1053
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001054 card->num_rtd = 0;
1055}
1056
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001057static void soc_set_name_prefix(struct snd_soc_card *card,
1058 struct snd_soc_codec *codec)
1059{
1060 int i;
1061
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001062 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001063 return;
1064
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001065 for (i = 0; i < card->num_configs; i++) {
1066 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001067 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1068 codec->name_prefix = map->name_prefix;
1069 break;
1070 }
1071 }
1072}
1073
Jarkko Nikula589c3562010-12-06 16:27:07 +02001074static int soc_probe_codec(struct snd_soc_card *card,
1075 struct snd_soc_codec *codec)
1076{
1077 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001078 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001079 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001080
1081 codec->card = card;
1082 codec->dapm.card = card;
1083 soc_set_name_prefix(card, codec);
1084
Jarkko Nikula70d29332011-01-27 16:24:22 +02001085 if (!try_module_get(codec->dev->driver->owner))
1086 return -ENODEV;
1087
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001088 soc_init_codec_debugfs(codec);
1089
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001090 if (driver->dapm_widgets)
1091 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1092 driver->num_dapm_widgets);
1093
Mark Brown888df392012-02-16 19:37:51 -08001094 /* Create DAPM widgets for each DAI stream */
1095 list_for_each_entry(dai, &dai_list, list) {
1096 if (dai->dev != codec->dev)
1097 continue;
1098
1099 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1100 }
1101
Mark Brown33c5f962011-08-22 18:40:30 +01001102 codec->dapm.idle_bias_off = driver->idle_bias_off;
1103
Mark Brown89b95ac02011-03-07 16:38:44 +00001104 if (driver->probe) {
1105 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001106 if (ret < 0) {
1107 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001108 "ASoC: failed to probe CODEC %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001109 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001110 }
Chuansheng Liuff541f42012-12-21 18:17:12 +08001111 WARN(codec->dapm.idle_bias_off &&
1112 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
1113 "codec %s can not start from non-off bias"
1114 " with idle_bias_off==1\n", codec->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001115 }
1116
Mark Brown38cbf952012-06-22 13:04:02 +01001117 /* If the driver didn't set I/O up try regmap */
Mark Brown98d30882012-08-01 20:05:47 +01001118 if (!codec->write && dev_get_regmap(codec->dev, NULL))
Mark Brown38cbf952012-06-22 13:04:02 +01001119 snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
1120
Mark Brownb7af1da2011-04-07 19:18:44 +09001121 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001122 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001123 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001124 if (driver->dapm_routes)
1125 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1126 driver->num_dapm_routes);
1127
Jarkko Nikula589c3562010-12-06 16:27:07 +02001128 /* mark codec as probed and add to card codec list */
1129 codec->probed = 1;
1130 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001131 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001132
Jarkko Nikula70d29332011-01-27 16:24:22 +02001133 return 0;
1134
1135err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001136 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001137 module_put(codec->dev->driver->owner);
1138
Jarkko Nikula589c3562010-12-06 16:27:07 +02001139 return ret;
1140}
1141
Liam Girdwood956245e2011-07-01 16:54:08 +01001142static int soc_probe_platform(struct snd_soc_card *card,
1143 struct snd_soc_platform *platform)
1144{
1145 int ret = 0;
1146 const struct snd_soc_platform_driver *driver = platform->driver;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001147 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001148
1149 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001150 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001151
1152 if (!try_module_get(platform->dev->driver->owner))
1153 return -ENODEV;
1154
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001155 soc_init_platform_debugfs(platform);
1156
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001157 if (driver->dapm_widgets)
1158 snd_soc_dapm_new_controls(&platform->dapm,
1159 driver->dapm_widgets, driver->num_dapm_widgets);
1160
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001161 /* Create DAPM widgets for each DAI stream */
1162 list_for_each_entry(dai, &dai_list, list) {
1163 if (dai->dev != platform->dev)
1164 continue;
1165
1166 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1167 }
1168
Stephen Warren3fec6b62012-04-05 12:28:01 -06001169 platform->dapm.idle_bias_off = 1;
1170
Liam Girdwood956245e2011-07-01 16:54:08 +01001171 if (driver->probe) {
1172 ret = driver->probe(platform);
1173 if (ret < 0) {
1174 dev_err(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001175 "ASoC: failed to probe platform %d\n", ret);
Liam Girdwood956245e2011-07-01 16:54:08 +01001176 goto err_probe;
1177 }
1178 }
1179
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001180 if (driver->controls)
1181 snd_soc_add_platform_controls(platform, driver->controls,
1182 driver->num_controls);
1183 if (driver->dapm_routes)
1184 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1185 driver->num_dapm_routes);
1186
Liam Girdwood956245e2011-07-01 16:54:08 +01001187 /* mark platform as probed and add to card platform list */
1188 platform->probed = 1;
1189 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001190 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001191
1192 return 0;
1193
1194err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001195 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001196 module_put(platform->dev->driver->owner);
1197
1198 return ret;
1199}
1200
Mark Brown36ae1a92012-01-06 17:12:45 -08001201static void rtd_release(struct device *dev)
1202{
1203 kfree(dev);
1204}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001205
Jarkko Nikula589c3562010-12-06 16:27:07 +02001206static int soc_post_component_init(struct snd_soc_card *card,
1207 struct snd_soc_codec *codec,
1208 int num, int dailess)
1209{
1210 struct snd_soc_dai_link *dai_link = NULL;
1211 struct snd_soc_aux_dev *aux_dev = NULL;
1212 struct snd_soc_pcm_runtime *rtd;
1213 const char *temp, *name;
1214 int ret = 0;
1215
1216 if (!dailess) {
1217 dai_link = &card->dai_link[num];
1218 rtd = &card->rtd[num];
1219 name = dai_link->name;
1220 } else {
1221 aux_dev = &card->aux_dev[num];
1222 rtd = &card->rtd_aux[num];
1223 name = aux_dev->name;
1224 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001225 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001226
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001227 /* Make sure all DAPM widgets are instantiated */
1228 snd_soc_dapm_new_widgets(&codec->dapm);
1229
Jarkko Nikula589c3562010-12-06 16:27:07 +02001230 /* machine controls, routes and widgets are not prefixed */
1231 temp = codec->name_prefix;
1232 codec->name_prefix = NULL;
1233
1234 /* do machine specific initialization */
1235 if (!dailess && dai_link->init)
1236 ret = dai_link->init(rtd);
1237 else if (dailess && aux_dev->init)
1238 ret = aux_dev->init(&codec->dapm);
1239 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001240 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001241 return ret;
1242 }
1243 codec->name_prefix = temp;
1244
Jarkko Nikula589c3562010-12-06 16:27:07 +02001245 /* register the rtd device */
1246 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001247
1248 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1249 if (!rtd->dev)
1250 return -ENOMEM;
1251 device_initialize(rtd->dev);
1252 rtd->dev->parent = card->dev;
1253 rtd->dev->release = rtd_release;
1254 rtd->dev->init_name = name;
1255 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001256 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001257 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1258 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1259 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1260 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001261 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001262 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001263 /* calling put_device() here to free the rtd->dev */
1264 put_device(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001265 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001266 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001267 return ret;
1268 }
1269 rtd->dev_registered = 1;
1270
1271 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001272 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001273 if (ret < 0)
1274 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001275 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001276
1277 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001278 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001279 if (ret < 0)
1280 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001281 "ASoC: failed to add codec sysfs files: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001282
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001283#ifdef CONFIG_DEBUG_FS
1284 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001285 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001286 goto out;
1287
1288 ret = soc_dpcm_debugfs_add(rtd);
1289 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001290 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001291
1292out:
1293#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001294 return 0;
1295}
1296
Stephen Warren62ae68f2012-06-08 12:34:23 -06001297static int soc_probe_link_components(struct snd_soc_card *card, int num,
1298 int order)
1299{
1300 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1301 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1302 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1303 struct snd_soc_platform *platform = rtd->platform;
1304 int ret;
1305
1306 /* probe the CPU-side component, if it is a CODEC */
1307 if (cpu_dai->codec &&
1308 !cpu_dai->codec->probed &&
1309 cpu_dai->codec->driver->probe_order == order) {
1310 ret = soc_probe_codec(card, cpu_dai->codec);
1311 if (ret < 0)
1312 return ret;
1313 }
1314
1315 /* probe the CODEC-side component */
1316 if (!codec_dai->codec->probed &&
1317 codec_dai->codec->driver->probe_order == order) {
1318 ret = soc_probe_codec(card, codec_dai->codec);
1319 if (ret < 0)
1320 return ret;
1321 }
1322
1323 /* probe the platform */
1324 if (!platform->probed &&
1325 platform->driver->probe_order == order) {
1326 ret = soc_probe_platform(card, platform);
1327 if (ret < 0)
1328 return ret;
1329 }
1330
1331 return 0;
1332}
1333
1334static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001335{
1336 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1337 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1338 struct snd_soc_codec *codec = rtd->codec;
1339 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001340 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1341 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1342 struct snd_soc_dapm_widget *play_w, *capture_w;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001343 int ret;
1344
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001345 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001346 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001347
1348 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001349 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001350 codec_dai->card = card;
1351 cpu_dai->card = card;
1352
1353 /* set default power off timeout */
1354 rtd->pmdown_time = pmdown_time;
1355
1356 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001357 if (!cpu_dai->probed &&
1358 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001359 if (!cpu_dai->codec) {
1360 cpu_dai->dapm.card = card;
1361 if (!try_module_get(cpu_dai->dev->driver->owner))
1362 return -ENODEV;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001363
Stephen Warren18d75642012-06-08 12:34:21 -06001364 list_add(&cpu_dai->dapm.list, &card->dapm_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001365 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1366 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001367
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001368 if (cpu_dai->driver->probe) {
1369 ret = cpu_dai->driver->probe(cpu_dai);
1370 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001371 dev_err(cpu_dai->dev,
1372 "ASoC: failed to probe CPU DAI %s: %d\n",
1373 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001374 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001375 return ret;
1376 }
1377 }
1378 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001379 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001380 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1381 }
1382
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001383 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001384 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001385 if (codec_dai->driver->probe) {
1386 ret = codec_dai->driver->probe(codec_dai);
1387 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001388 dev_err(codec_dai->dev,
1389 "ASoC: failed to probe CODEC DAI %s: %d\n",
1390 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001391 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001392 }
1393 }
1394
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001395 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001396 codec_dai->probed = 1;
1397 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001398 }
1399
Liam Girdwood0168bf02011-06-07 16:08:05 +01001400 /* complete DAI probe during last probe */
1401 if (order != SND_SOC_COMP_ORDER_LAST)
1402 return 0;
1403
Jarkko Nikula589c3562010-12-06 16:27:07 +02001404 ret = soc_post_component_init(card, codec, num, 0);
1405 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001406 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001407
Mark Brown36ae1a92012-01-06 17:12:45 -08001408 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001409 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001410 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1411 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001412
Namarta Kohli1245b702012-08-16 17:10:41 +05301413 if (cpu_dai->driver->compress_dai) {
1414 /*create compress_device"*/
1415 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001416 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001417 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301418 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001419 return ret;
1420 }
1421 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301422
1423 if (!dai_link->params) {
1424 /* create the pcm */
1425 ret = soc_new_pcm(rtd, num);
1426 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001427 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301428 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001429 return ret;
1430 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301431 } else {
1432 /* link the DAI widgets */
1433 play_w = codec_dai->playback_widget;
1434 capture_w = cpu_dai->capture_widget;
1435 if (play_w && capture_w) {
1436 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Mark Brownc74184e2012-04-04 22:12:09 +01001437 capture_w, play_w);
Namarta Kohli1245b702012-08-16 17:10:41 +05301438 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001439 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301440 play_w->name, capture_w->name, ret);
1441 return ret;
1442 }
1443 }
1444
1445 play_w = cpu_dai->playback_widget;
1446 capture_w = codec_dai->capture_widget;
1447 if (play_w && capture_w) {
1448 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1449 capture_w, play_w);
1450 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001451 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301452 play_w->name, capture_w->name, ret);
1453 return ret;
1454 }
Mark Brownc74184e2012-04-04 22:12:09 +01001455 }
1456 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001457 }
1458
1459 /* add platform data for AC97 devices */
1460 if (rtd->codec_dai->driver->ac97_control)
1461 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1462
1463 return 0;
1464}
1465
1466#ifdef CONFIG_SND_SOC_AC97_BUS
1467static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1468{
1469 int ret;
1470
1471 /* Only instantiate AC97 if not already done by the adaptor
1472 * for the generic AC97 subsystem.
1473 */
1474 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001475 /*
1476 * It is possible that the AC97 device is already registered to
1477 * the device subsystem. This happens when the device is created
1478 * via snd_ac97_mixer(). Currently only SoC codec that does so
1479 * is the generic AC97 glue but others migh emerge.
1480 *
1481 * In those cases we don't try to register the device again.
1482 */
1483 if (!rtd->codec->ac97_created)
1484 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001485
1486 ret = soc_ac97_dev_register(rtd->codec);
1487 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001488 dev_err(rtd->codec->dev,
1489 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001490 return ret;
1491 }
1492
1493 rtd->codec->ac97_registered = 1;
1494 }
1495 return 0;
1496}
1497
1498static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1499{
1500 if (codec->ac97_registered) {
1501 soc_ac97_dev_unregister(codec);
1502 codec->ac97_registered = 0;
1503 }
1504}
1505#endif
1506
Mark Brownb19e6e72012-03-14 21:18:39 +00001507static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1508{
1509 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1510 struct snd_soc_codec *codec;
1511
1512 /* find CODEC from registered CODECs*/
1513 list_for_each_entry(codec, &codec_list, list) {
1514 if (!strcmp(codec->name, aux_dev->codec_name))
1515 return 0;
1516 }
1517
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001518 dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
Mark Brownfb099cb2012-08-09 18:44:37 +01001519
Mark Brownb19e6e72012-03-14 21:18:39 +00001520 return -EPROBE_DEFER;
1521}
1522
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001523static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1524{
1525 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001526 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001527 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001528
1529 /* find CODEC from registered CODECs*/
1530 list_for_each_entry(codec, &codec_list, list) {
1531 if (!strcmp(codec->name, aux_dev->codec_name)) {
1532 if (codec->probed) {
1533 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001534 "ASoC: codec already probed");
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001535 ret = -EBUSY;
1536 goto out;
1537 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001538 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001539 }
1540 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001541 /* codec not found */
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001542 dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001543 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001544
Jarkko Nikula676ad982010-12-03 09:18:22 +02001545found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001546 ret = soc_probe_codec(card, codec);
1547 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001548 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001549
Jarkko Nikula589c3562010-12-06 16:27:07 +02001550 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001551
1552out:
1553 return ret;
1554}
1555
1556static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1557{
1558 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1559 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001560
1561 /* unregister the rtd device */
1562 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001563 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001564 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001565 rtd->dev_registered = 0;
1566 }
1567
Jarkko Nikula589c3562010-12-06 16:27:07 +02001568 if (codec && codec->probed)
1569 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001570}
1571
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001572static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1573 enum snd_soc_compress_type compress_type)
1574{
1575 int ret;
1576
1577 if (codec->cache_init)
1578 return 0;
1579
1580 /* override the compress_type if necessary */
1581 if (compress_type && codec->compress_type != compress_type)
1582 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001583 ret = snd_soc_cache_init(codec);
1584 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001585 dev_err(codec->dev, "ASoC: Failed to set cache compression"
1586 " type: %d\n", ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001587 return ret;
1588 }
1589 codec->cache_init = 1;
1590 return 0;
1591}
1592
Mark Brownb19e6e72012-03-14 21:18:39 +00001593static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001594{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001595 struct snd_soc_codec *codec;
1596 struct snd_soc_codec_conf *codec_conf;
1597 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001598 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001599 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001600
Liam Girdwood01b9d992012-03-07 10:38:25 +00001601 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001602
Mark Brownb19e6e72012-03-14 21:18:39 +00001603 /* bind DAIs */
1604 for (i = 0; i < card->num_links; i++) {
1605 ret = soc_bind_dai_link(card, i);
1606 if (ret != 0)
1607 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001608 }
1609
Mark Brownb19e6e72012-03-14 21:18:39 +00001610 /* check aux_devs too */
1611 for (i = 0; i < card->num_aux_devs; i++) {
1612 ret = soc_check_aux_dev(card, i);
1613 if (ret != 0)
1614 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001615 }
1616
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001617 /* initialize the register cache for each available codec */
1618 list_for_each_entry(codec, &codec_list, list) {
1619 if (codec->cache_init)
1620 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001621 /* by default we don't override the compress_type */
1622 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001623 /* check to see if we need to override the compress_type */
1624 for (i = 0; i < card->num_configs; ++i) {
1625 codec_conf = &card->codec_conf[i];
1626 if (!strcmp(codec->name, codec_conf->dev_name)) {
1627 compress_type = codec_conf->compress_type;
1628 if (compress_type && compress_type
1629 != codec->compress_type)
1630 break;
1631 }
1632 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001633 ret = snd_soc_init_codec_cache(codec, compress_type);
Mark Brownb19e6e72012-03-14 21:18:39 +00001634 if (ret < 0)
1635 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001636 }
1637
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001638 /* card bind complete so register a sound card */
1639 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1640 card->owner, 0, &card->snd_card);
1641 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001642 dev_err(card->dev, "ASoC: can't create sound card for"
1643 " card %s: %d\n", card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001644 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001645 }
1646 card->snd_card->dev = card->dev;
1647
Mark Browne37a4972011-03-02 18:21:57 +00001648 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1649 card->dapm.dev = card->dev;
1650 card->dapm.card = card;
1651 list_add(&card->dapm.list, &card->dapm_list);
1652
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001653#ifdef CONFIG_DEBUG_FS
1654 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1655#endif
1656
Mark Brown88ee1c62011-02-02 10:43:26 +00001657#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001658 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001659 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001660#endif
Andy Green6ed25972008-06-13 16:24:05 +01001661
Mark Brown9a841eb2011-04-12 17:51:37 -07001662 if (card->dapm_widgets)
1663 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1664 card->num_dapm_widgets);
1665
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001666 /* initialise the sound card only once */
1667 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001668 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001669 if (ret < 0)
1670 goto card_probe_error;
1671 }
1672
Stephen Warren62ae68f2012-06-08 12:34:23 -06001673 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001674 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1675 order++) {
1676 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001677 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001678 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001679 dev_err(card->dev,
1680 "ASoC: failed to instantiate card %d\n",
1681 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001682 goto probe_dai_err;
1683 }
1684 }
1685 }
1686
1687 /* probe all DAI links on this card */
1688 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1689 order++) {
1690 for (i = 0; i < card->num_links; i++) {
1691 ret = soc_probe_link_dais(card, i, order);
1692 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001693 dev_err(card->dev,
1694 "ASoC: failed to instantiate card %d\n",
1695 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001696 goto probe_dai_err;
1697 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001698 }
1699 }
1700
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001701 for (i = 0; i < card->num_aux_devs; i++) {
1702 ret = soc_probe_aux_dev(card, i);
1703 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001704 dev_err(card->dev,
1705 "ASoC: failed to add auxiliary devices %d\n",
1706 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001707 goto probe_aux_dev_err;
1708 }
1709 }
1710
Mark Brown888df392012-02-16 19:37:51 -08001711 snd_soc_dapm_link_dai_widgets(card);
1712
Mark Brownb7af1da2011-04-07 19:18:44 +09001713 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001714 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001715
Mark Brownb8ad29d2011-03-02 18:35:51 +00001716 if (card->dapm_routes)
1717 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1718 card->num_dapm_routes);
1719
Mark Brownb90d2f92011-10-10 13:38:06 +01001720 snd_soc_dapm_new_widgets(&card->dapm);
1721
Mark Brown75d9ac42011-09-27 16:41:01 +01001722 for (i = 0; i < card->num_links; i++) {
1723 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001724 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001725
Mark Brownf04209a2012-04-09 16:29:21 +01001726 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001727 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001728 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001729 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001730 dev_warn(card->rtd[i].codec_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001731 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001732 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001733 }
1734
1735 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001736 if (dai_fmt &&
1737 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001738 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1739 dai_fmt);
1740 if (ret != 0 && ret != -ENOTSUPP)
1741 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001742 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001743 ret);
1744 } else if (dai_fmt) {
1745 /* Flip the polarity for the "CPU" end */
1746 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1747 switch (dai_link->dai_fmt &
1748 SND_SOC_DAIFMT_MASTER_MASK) {
1749 case SND_SOC_DAIFMT_CBM_CFM:
1750 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1751 break;
1752 case SND_SOC_DAIFMT_CBM_CFS:
1753 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1754 break;
1755 case SND_SOC_DAIFMT_CBS_CFM:
1756 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1757 break;
1758 case SND_SOC_DAIFMT_CBS_CFS:
1759 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1760 break;
1761 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001762
1763 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001764 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001765 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001766 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001767 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001768 ret);
1769 }
1770 }
1771
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001772 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001773 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001774 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1775 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001776 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1777 "%s", card->driver_name ? card->driver_name : card->name);
1778 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1779 switch (card->snd_card->driver[i]) {
1780 case '_':
1781 case '-':
1782 case '\0':
1783 break;
1784 default:
1785 if (!isalnum(card->snd_card->driver[i]))
1786 card->snd_card->driver[i] = '_';
1787 break;
1788 }
1789 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001790
Mark Brown28e9ad92011-03-02 18:36:34 +00001791 if (card->late_probe) {
1792 ret = card->late_probe(card);
1793 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001794 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001795 card->name, ret);
1796 goto probe_aux_dev_err;
1797 }
1798 }
1799
Mark Brown2dc00212011-10-08 13:59:44 +01001800 snd_soc_dapm_new_widgets(&card->dapm);
1801
Stephen Warren16332812011-11-23 12:42:04 -07001802 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001803 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001804 snd_soc_dapm_auto_nc_codec_pins(codec);
1805
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001806 ret = snd_card_register(card->snd_card);
1807 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001808 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1809 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001810 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001811 }
1812
1813#ifdef CONFIG_SND_SOC_AC97_BUS
1814 /* register any AC97 codecs */
1815 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001816 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1817 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001818 dev_err(card->dev, "ASoC: failed to register AC97:"
1819 " %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001820 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001821 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001822 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001823 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001824 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001825#endif
1826
Mark Brown435c5e22008-12-04 15:32:53 +00001827 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001828 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001829 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001830
1831 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001832
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001833probe_aux_dev_err:
1834 for (i = 0; i < card->num_aux_devs; i++)
1835 soc_remove_aux_dev(card, i);
1836
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001837probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001838 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001839
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001840card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001841 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001842 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001843
1844 snd_card_free(card->snd_card);
1845
Mark Brownb19e6e72012-03-14 21:18:39 +00001846base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001847 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001848
Mark Brownb19e6e72012-03-14 21:18:39 +00001849 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001850}
1851
1852/* probes a new socdev */
1853static int soc_probe(struct platform_device *pdev)
1854{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001855 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001856
Vinod Koul70a7ca32011-01-14 19:22:48 +05301857 /*
1858 * no card, so machine driver should be registering card
1859 * we should not be here in that case so ret error
1860 */
1861 if (!card)
1862 return -EINVAL;
1863
Mark Brownfe4085e2012-03-02 13:07:41 +00001864 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001865 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001866 card->name);
1867
Mark Brown435c5e22008-12-04 15:32:53 +00001868 /* Bodge while we unpick instantiation */
1869 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001870
Mark Brown28d528c2012-08-09 18:45:23 +01001871 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001872}
1873
Vinod Koulb0e26482011-01-13 22:48:02 +05301874static int soc_cleanup_card_resources(struct snd_soc_card *card)
1875{
Vinod Koulb0e26482011-01-13 22:48:02 +05301876 int i;
1877
1878 /* make sure any delayed work runs */
1879 for (i = 0; i < card->num_rtd; i++) {
1880 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001881 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301882 }
1883
1884 /* remove auxiliary devices */
1885 for (i = 0; i < card->num_aux_devs; i++)
1886 soc_remove_aux_dev(card, i);
1887
1888 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001889 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301890
1891 soc_cleanup_card_debugfs(card);
1892
1893 /* remove the card */
1894 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001895 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301896
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001897 snd_soc_dapm_free(&card->dapm);
1898
Vinod Koulb0e26482011-01-13 22:48:02 +05301899 snd_card_free(card->snd_card);
1900 return 0;
1901
1902}
1903
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001904/* removes a socdev */
1905static int soc_remove(struct platform_device *pdev)
1906{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001907 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001908
Mark Brownc5af3a22008-11-28 13:29:45 +00001909 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001910 return 0;
1911}
1912
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001913int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001914{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001915 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001916 int i;
Mark Brown51737472009-06-22 13:16:51 +01001917
1918 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001919 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001920
1921 /* Flush out pmdown_time work - we actually do want to run it
1922 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001923 for (i = 0; i < card->num_rtd; i++) {
1924 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001925 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001926 }
Mark Brown51737472009-06-22 13:16:51 +01001927
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001928 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001929
1930 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001931}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001932EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001933
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001934const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301935 .suspend = snd_soc_suspend,
1936 .resume = snd_soc_resume,
1937 .freeze = snd_soc_suspend,
1938 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001939 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301940 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001941};
Stephen Warrendeb26072011-04-05 19:35:30 -06001942EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001943
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001944/* ASoC platform driver */
1945static struct platform_driver soc_driver = {
1946 .driver = {
1947 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001948 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001949 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001950 },
1951 .probe = soc_probe,
1952 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001953};
1954
Mark Brown096e49d2009-07-05 15:12:22 +01001955/**
1956 * snd_soc_codec_volatile_register: Report if a register is volatile.
1957 *
1958 * @codec: CODEC to query.
1959 * @reg: Register to query.
1960 *
1961 * Boolean function indiciating if a CODEC register is volatile.
1962 */
Mark Brown181e0552011-01-24 14:05:25 +00001963int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1964 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001965{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001966 if (codec->volatile_register)
1967 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001968 else
1969 return 0;
1970}
1971EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1972
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001973/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001974 * snd_soc_codec_readable_register: Report if a register is readable.
1975 *
1976 * @codec: CODEC to query.
1977 * @reg: Register to query.
1978 *
1979 * Boolean function indicating if a CODEC register is readable.
1980 */
1981int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1982 unsigned int reg)
1983{
1984 if (codec->readable_register)
1985 return codec->readable_register(codec, reg);
1986 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001987 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001988}
1989EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1990
1991/**
1992 * snd_soc_codec_writable_register: Report if a register is writable.
1993 *
1994 * @codec: CODEC to query.
1995 * @reg: Register to query.
1996 *
1997 * Boolean function indicating if a CODEC register is writable.
1998 */
1999int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
2000 unsigned int reg)
2001{
2002 if (codec->writable_register)
2003 return codec->writable_register(codec, reg);
2004 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02002005 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002006}
2007EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2008
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002009int snd_soc_platform_read(struct snd_soc_platform *platform,
2010 unsigned int reg)
2011{
2012 unsigned int ret;
2013
2014 if (!platform->driver->read) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002015 dev_err(platform->dev, "ASoC: platform has no read back\n");
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002016 return -1;
2017 }
2018
2019 ret = platform->driver->read(platform, reg);
2020 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002021 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002022
2023 return ret;
2024}
2025EXPORT_SYMBOL_GPL(snd_soc_platform_read);
2026
2027int snd_soc_platform_write(struct snd_soc_platform *platform,
2028 unsigned int reg, unsigned int val)
2029{
2030 if (!platform->driver->write) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002031 dev_err(platform->dev, "ASoC: platform has no write back\n");
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002032 return -1;
2033 }
2034
2035 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002036 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002037 return platform->driver->write(platform, reg, val);
2038}
2039EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2040
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002041/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002042 * snd_soc_new_ac97_codec - initailise AC97 device
2043 * @codec: audio codec
2044 * @ops: AC97 bus operations
2045 * @num: AC97 codec number
2046 *
2047 * Initialises AC97 codec resources for use by ad-hoc devices only.
2048 */
2049int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2050 struct snd_ac97_bus_ops *ops, int num)
2051{
2052 mutex_lock(&codec->mutex);
2053
2054 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2055 if (codec->ac97 == NULL) {
2056 mutex_unlock(&codec->mutex);
2057 return -ENOMEM;
2058 }
2059
2060 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2061 if (codec->ac97->bus == NULL) {
2062 kfree(codec->ac97);
2063 codec->ac97 = NULL;
2064 mutex_unlock(&codec->mutex);
2065 return -ENOMEM;
2066 }
2067
2068 codec->ac97->bus->ops = ops;
2069 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002070
2071 /*
2072 * Mark the AC97 device to be created by us. This way we ensure that the
2073 * device will be registered with the device subsystem later on.
2074 */
2075 codec->ac97_created = 1;
2076
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002077 mutex_unlock(&codec->mutex);
2078 return 0;
2079}
2080EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2081
Mark Brownb047e1c2013-06-26 12:45:59 +01002082struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002083EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002084
2085int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2086{
2087 if (ops == soc_ac97_ops)
2088 return 0;
2089
2090 if (soc_ac97_ops && ops)
2091 return -EBUSY;
2092
2093 soc_ac97_ops = ops;
2094
2095 return 0;
2096}
2097EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2098
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002099/**
2100 * snd_soc_free_ac97_codec - free AC97 codec device
2101 * @codec: audio codec
2102 *
2103 * Frees AC97 codec device resources.
2104 */
2105void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2106{
2107 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002108#ifdef CONFIG_SND_SOC_AC97_BUS
2109 soc_unregister_ac97_dai_link(codec);
2110#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002111 kfree(codec->ac97->bus);
2112 kfree(codec->ac97);
2113 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002114 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002115 mutex_unlock(&codec->mutex);
2116}
2117EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2118
Mark Brownc3753702010-11-01 15:41:57 -04002119unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2120{
2121 unsigned int ret;
2122
Mark Brownc3acec22010-12-02 16:15:29 +00002123 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002124 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002125 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002126
2127 return ret;
2128}
2129EXPORT_SYMBOL_GPL(snd_soc_read);
2130
2131unsigned int snd_soc_write(struct snd_soc_codec *codec,
2132 unsigned int reg, unsigned int val)
2133{
2134 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002135 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002136 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002137}
2138EXPORT_SYMBOL_GPL(snd_soc_write);
2139
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00002140unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2141 unsigned int reg, const void *data, size_t len)
2142{
2143 return codec->bulk_write_raw(codec, reg, data, len);
2144}
2145EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2146
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002147/**
2148 * snd_soc_update_bits - update codec register bits
2149 * @codec: audio codec
2150 * @reg: codec register
2151 * @mask: register mask
2152 * @value: new value
2153 *
2154 * Writes new register value.
2155 *
Timur Tabi180c3292011-01-10 15:58:13 -06002156 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002157 */
2158int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002159 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002160{
Mark Brown8a713da2011-12-03 12:33:55 +00002161 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002162 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002163 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002164
Mark Brown8a713da2011-12-03 12:33:55 +00002165 if (codec->using_regmap) {
2166 ret = regmap_update_bits_check(codec->control_data, reg,
2167 mask, value, &change);
2168 } else {
2169 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06002170 if (ret < 0)
2171 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00002172
2173 old = ret;
2174 new = (old & ~mask) | (value & mask);
2175 change = old != new;
2176 if (change)
2177 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06002178 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002179
Mark Brown8a713da2011-12-03 12:33:55 +00002180 if (ret < 0)
2181 return ret;
2182
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002183 return change;
2184}
2185EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2186
2187/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002188 * snd_soc_update_bits_locked - update codec register bits
2189 * @codec: audio codec
2190 * @reg: codec register
2191 * @mask: register mask
2192 * @value: new value
2193 *
2194 * Writes new register value, and takes the codec mutex.
2195 *
2196 * Returns 1 for change else 0.
2197 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002198int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2199 unsigned short reg, unsigned int mask,
2200 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002201{
2202 int change;
2203
2204 mutex_lock(&codec->mutex);
2205 change = snd_soc_update_bits(codec, reg, mask, value);
2206 mutex_unlock(&codec->mutex);
2207
2208 return change;
2209}
Mark Browndd1b3d52009-12-04 14:22:03 +00002210EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002211
2212/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002213 * snd_soc_test_bits - test register for change
2214 * @codec: audio codec
2215 * @reg: codec register
2216 * @mask: register mask
2217 * @value: new value
2218 *
2219 * Tests a register with a new value and checks if the new value is
2220 * different from the old value.
2221 *
2222 * Returns 1 for change else 0.
2223 */
2224int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002225 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002226{
2227 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002228 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002229
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002230 old = snd_soc_read(codec, reg);
2231 new = (old & ~mask) | value;
2232 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002233
2234 return change;
2235}
2236EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2237
2238/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002239 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2240 * @substream: the pcm substream
2241 * @hw: the hardware parameters
2242 *
2243 * Sets the substream runtime hardware parameters.
2244 */
2245int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2246 const struct snd_pcm_hardware *hw)
2247{
2248 struct snd_pcm_runtime *runtime = substream->runtime;
2249 runtime->hw.info = hw->info;
2250 runtime->hw.formats = hw->formats;
2251 runtime->hw.period_bytes_min = hw->period_bytes_min;
2252 runtime->hw.period_bytes_max = hw->period_bytes_max;
2253 runtime->hw.periods_min = hw->periods_min;
2254 runtime->hw.periods_max = hw->periods_max;
2255 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2256 runtime->hw.fifo_size = hw->fifo_size;
2257 return 0;
2258}
2259EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2260
2261/**
2262 * snd_soc_cnew - create new control
2263 * @_template: control template
2264 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002265 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002266 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002267 *
2268 * Create a new mixer control from a template control.
2269 *
2270 * Returns 0 for success, else error.
2271 */
2272struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002273 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002274 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002275{
2276 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002277 struct snd_kcontrol *kcontrol;
2278 char *name = NULL;
2279 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002280
2281 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002282 template.index = 0;
2283
Mark Brownefb7ac32011-03-08 17:23:24 +00002284 if (!long_name)
2285 long_name = template.name;
2286
2287 if (prefix) {
2288 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08002289 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00002290 if (!name)
2291 return NULL;
2292
2293 snprintf(name, name_len, "%s %s", prefix, long_name);
2294
2295 template.name = name;
2296 } else {
2297 template.name = long_name;
2298 }
2299
2300 kcontrol = snd_ctl_new1(&template, data);
2301
2302 kfree(name);
2303
2304 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002305}
2306EXPORT_SYMBOL_GPL(snd_soc_cnew);
2307
Liam Girdwood022658b2012-02-03 17:43:09 +00002308static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2309 const struct snd_kcontrol_new *controls, int num_controls,
2310 const char *prefix, void *data)
2311{
2312 int err, i;
2313
2314 for (i = 0; i < num_controls; i++) {
2315 const struct snd_kcontrol_new *control = &controls[i];
2316 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2317 control->name, prefix));
2318 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002319 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2320 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002321 return err;
2322 }
2323 }
2324
2325 return 0;
2326}
2327
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002328/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002329 * snd_soc_add_codec_controls - add an array of controls to a codec.
2330 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002331 * duplicating this code.
2332 *
2333 * @codec: codec to add controls to
2334 * @controls: array of controls to add
2335 * @num_controls: number of elements in the array
2336 *
2337 * Return 0 for success, else error.
2338 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002339int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002340 const struct snd_kcontrol_new *controls, int num_controls)
2341{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002342 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002343
Liam Girdwood022658b2012-02-03 17:43:09 +00002344 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2345 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002346}
Liam Girdwood022658b2012-02-03 17:43:09 +00002347EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002348
2349/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002350 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002351 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002352 *
2353 * @platform: platform to add controls to
2354 * @controls: array of controls to add
2355 * @num_controls: number of elements in the array
2356 *
2357 * Return 0 for success, else error.
2358 */
2359int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2360 const struct snd_kcontrol_new *controls, int num_controls)
2361{
2362 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002363
Liam Girdwood022658b2012-02-03 17:43:09 +00002364 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2365 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002366}
2367EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2368
2369/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002370 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2371 * Convenience function to add a list of controls.
2372 *
2373 * @soc_card: SoC card to add controls to
2374 * @controls: array of controls to add
2375 * @num_controls: number of elements in the array
2376 *
2377 * Return 0 for success, else error.
2378 */
2379int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2380 const struct snd_kcontrol_new *controls, int num_controls)
2381{
2382 struct snd_card *card = soc_card->snd_card;
2383
2384 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2385 NULL, soc_card);
2386}
2387EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2388
2389/**
2390 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2391 * Convienience function to add a list of controls.
2392 *
2393 * @dai: DAI to add controls to
2394 * @controls: array of controls to add
2395 * @num_controls: number of elements in the array
2396 *
2397 * Return 0 for success, else error.
2398 */
2399int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2400 const struct snd_kcontrol_new *controls, int num_controls)
2401{
2402 struct snd_card *card = dai->card->snd_card;
2403
2404 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2405 NULL, dai);
2406}
2407EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2408
2409/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002410 * snd_soc_info_enum_double - enumerated double mixer info callback
2411 * @kcontrol: mixer control
2412 * @uinfo: control element information
2413 *
2414 * Callback to provide information about a double enumerated
2415 * mixer control.
2416 *
2417 * Returns 0 for success.
2418 */
2419int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2420 struct snd_ctl_elem_info *uinfo)
2421{
2422 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2423
2424 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2425 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002426 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002427
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002428 if (uinfo->value.enumerated.item > e->max - 1)
2429 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002430 strcpy(uinfo->value.enumerated.name,
2431 e->texts[uinfo->value.enumerated.item]);
2432 return 0;
2433}
2434EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2435
2436/**
2437 * snd_soc_get_enum_double - enumerated double mixer get callback
2438 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002439 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002440 *
2441 * Callback to get the value of a double enumerated mixer.
2442 *
2443 * Returns 0 for success.
2444 */
2445int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2446 struct snd_ctl_elem_value *ucontrol)
2447{
2448 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2449 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002450 unsigned int val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002451
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002452 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002453 ucontrol->value.enumerated.item[0]
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002454 = (val >> e->shift_l) & e->mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002455 if (e->shift_l != e->shift_r)
2456 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002457 (val >> e->shift_r) & e->mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002458
2459 return 0;
2460}
2461EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2462
2463/**
2464 * snd_soc_put_enum_double - enumerated double mixer put callback
2465 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002466 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002467 *
2468 * Callback to set the value of a double enumerated mixer.
2469 *
2470 * Returns 0 for success.
2471 */
2472int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2473 struct snd_ctl_elem_value *ucontrol)
2474{
2475 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2476 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002477 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002478 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002479
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002480 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002481 return -EINVAL;
2482 val = ucontrol->value.enumerated.item[0] << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002483 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002484 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002485 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002486 return -EINVAL;
2487 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002488 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002489 }
2490
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002491 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002492}
2493EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2494
2495/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002496 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2497 * @kcontrol: mixer control
2498 * @ucontrol: control element information
2499 *
2500 * Callback to get the value of a double semi enumerated mixer.
2501 *
2502 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2503 * used for handling bitfield coded enumeration for example.
2504 *
2505 * Returns 0 for success.
2506 */
2507int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2508 struct snd_ctl_elem_value *ucontrol)
2509{
2510 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002511 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002512 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002513
2514 reg_val = snd_soc_read(codec, e->reg);
2515 val = (reg_val >> e->shift_l) & e->mask;
2516 for (mux = 0; mux < e->max; mux++) {
2517 if (val == e->values[mux])
2518 break;
2519 }
2520 ucontrol->value.enumerated.item[0] = mux;
2521 if (e->shift_l != e->shift_r) {
2522 val = (reg_val >> e->shift_r) & e->mask;
2523 for (mux = 0; mux < e->max; mux++) {
2524 if (val == e->values[mux])
2525 break;
2526 }
2527 ucontrol->value.enumerated.item[1] = mux;
2528 }
2529
2530 return 0;
2531}
2532EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2533
2534/**
2535 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2536 * @kcontrol: mixer control
2537 * @ucontrol: control element information
2538 *
2539 * Callback to set the value of a double semi enumerated mixer.
2540 *
2541 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2542 * used for handling bitfield coded enumeration for example.
2543 *
2544 * Returns 0 for success.
2545 */
2546int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2547 struct snd_ctl_elem_value *ucontrol)
2548{
2549 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002550 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002551 unsigned int val;
2552 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002553
2554 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2555 return -EINVAL;
2556 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2557 mask = e->mask << e->shift_l;
2558 if (e->shift_l != e->shift_r) {
2559 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2560 return -EINVAL;
2561 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2562 mask |= e->mask << e->shift_r;
2563 }
2564
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002565 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002566}
2567EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2568
2569/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002570 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2571 * @kcontrol: mixer control
2572 * @uinfo: control element information
2573 *
2574 * Callback to provide information about an external enumerated
2575 * single mixer.
2576 *
2577 * Returns 0 for success.
2578 */
2579int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2580 struct snd_ctl_elem_info *uinfo)
2581{
2582 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2583
2584 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2585 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002586 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002587
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002588 if (uinfo->value.enumerated.item > e->max - 1)
2589 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002590 strcpy(uinfo->value.enumerated.name,
2591 e->texts[uinfo->value.enumerated.item]);
2592 return 0;
2593}
2594EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2595
2596/**
2597 * snd_soc_info_volsw_ext - external single mixer info callback
2598 * @kcontrol: mixer control
2599 * @uinfo: control element information
2600 *
2601 * Callback to provide information about a single external mixer control.
2602 *
2603 * Returns 0 for success.
2604 */
2605int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2606 struct snd_ctl_elem_info *uinfo)
2607{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002608 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002609
Mark Brownfd5dfad2009-04-15 21:37:46 +01002610 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002611 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2612 else
2613 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2614
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002615 uinfo->count = 1;
2616 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002617 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002618 return 0;
2619}
2620EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2621
2622/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002623 * snd_soc_info_volsw - single mixer info callback
2624 * @kcontrol: mixer control
2625 * @uinfo: control element information
2626 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002627 * Callback to provide information about a single mixer control, or a double
2628 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002629 *
2630 * Returns 0 for success.
2631 */
2632int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2633 struct snd_ctl_elem_info *uinfo)
2634{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002635 struct soc_mixer_control *mc =
2636 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002637 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002638
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002639 if (!mc->platform_max)
2640 mc->platform_max = mc->max;
2641 platform_max = mc->platform_max;
2642
2643 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002644 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2645 else
2646 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2647
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002648 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002649 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002650 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002651 return 0;
2652}
2653EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2654
2655/**
2656 * snd_soc_get_volsw - single mixer get 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 Ujfalusif7915d92011-10-05 10:29:24 +03002660 * Callback to get 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_get_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 Smirl815ecf8d2008-07-29 10:22:24 -04002671 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002672 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-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 Smirl815ecf8d2008-07-29 10:22:24 -04002676 unsigned int mask = (1 << fls(max)) - 1;
2677 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002678
2679 ucontrol->value.integer.value[0] =
2680 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002681 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002682 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002683 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002684
2685 if (snd_soc_volsw_is_stereo(mc)) {
2686 if (reg == reg2)
2687 ucontrol->value.integer.value[1] =
2688 (snd_soc_read(codec, reg) >> rshift) & mask;
2689 else
2690 ucontrol->value.integer.value[1] =
2691 (snd_soc_read(codec, reg2) >> shift) & mask;
2692 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002693 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002694 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002695 }
2696
2697 return 0;
2698}
2699EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2700
2701/**
2702 * snd_soc_put_volsw - single mixer put callback
2703 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002704 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002705 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002706 * Callback to set the value of a single mixer control, or a double mixer
2707 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002708 *
2709 * Returns 0 for success.
2710 */
2711int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2712 struct snd_ctl_elem_value *ucontrol)
2713{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002714 struct soc_mixer_control *mc =
2715 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002716 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002717 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002718 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002719 unsigned int shift = mc->shift;
2720 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002721 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002722 unsigned int mask = (1 << fls(max)) - 1;
2723 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002724 int err;
2725 bool type_2r = 0;
2726 unsigned int val2 = 0;
2727 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002728
2729 val = (ucontrol->value.integer.value[0] & mask);
2730 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002731 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002732 val_mask = mask << shift;
2733 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002734 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002735 val2 = (ucontrol->value.integer.value[1] & mask);
2736 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002737 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002738 if (reg == reg2) {
2739 val_mask |= mask << rshift;
2740 val |= val2 << rshift;
2741 } else {
2742 val2 = val2 << shift;
2743 type_2r = 1;
2744 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002745 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002746 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002747 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002748 return err;
2749
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002750 if (type_2r)
2751 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2752
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002753 return err;
2754}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002755EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002756
Mark Browne13ac2e2008-05-28 17:58:05 +01002757/**
Brian Austin1d99f242012-03-30 10:43:55 -05002758 * snd_soc_get_volsw_sx - single mixer get callback
2759 * @kcontrol: mixer control
2760 * @ucontrol: control element information
2761 *
2762 * Callback to get the value of a single mixer control, or a double mixer
2763 * control that spans 2 registers.
2764 *
2765 * Returns 0 for success.
2766 */
2767int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2768 struct snd_ctl_elem_value *ucontrol)
2769{
2770 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2771 struct soc_mixer_control *mc =
2772 (struct soc_mixer_control *)kcontrol->private_value;
2773
2774 unsigned int reg = mc->reg;
2775 unsigned int reg2 = mc->rreg;
2776 unsigned int shift = mc->shift;
2777 unsigned int rshift = mc->rshift;
2778 int max = mc->max;
2779 int min = mc->min;
2780 int mask = (1 << (fls(min + max) - 1)) - 1;
2781
2782 ucontrol->value.integer.value[0] =
2783 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2784
2785 if (snd_soc_volsw_is_stereo(mc))
2786 ucontrol->value.integer.value[1] =
2787 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2788
2789 return 0;
2790}
2791EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2792
2793/**
2794 * snd_soc_put_volsw_sx - double mixer set callback
2795 * @kcontrol: mixer control
2796 * @uinfo: control element information
2797 *
2798 * Callback to set the value of a double mixer control that spans 2 registers.
2799 *
2800 * Returns 0 for success.
2801 */
2802int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2803 struct snd_ctl_elem_value *ucontrol)
2804{
2805 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2806 struct soc_mixer_control *mc =
2807 (struct soc_mixer_control *)kcontrol->private_value;
2808
2809 unsigned int reg = mc->reg;
2810 unsigned int reg2 = mc->rreg;
2811 unsigned int shift = mc->shift;
2812 unsigned int rshift = mc->rshift;
2813 int max = mc->max;
2814 int min = mc->min;
2815 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002816 int err = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002817 unsigned short val, val_mask, val2 = 0;
2818
2819 val_mask = mask << shift;
2820 val = (ucontrol->value.integer.value[0] + min) & mask;
2821 val = val << shift;
2822
Mukund Navadad0558522012-11-09 11:53:40 +05302823 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2824 if (err < 0)
2825 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002826
2827 if (snd_soc_volsw_is_stereo(mc)) {
2828 val_mask = mask << rshift;
2829 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2830 val2 = val2 << rshift;
2831
2832 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2833 return err;
2834 }
2835 return 0;
2836}
2837EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2838
2839/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002840 * snd_soc_info_volsw_s8 - signed mixer info callback
2841 * @kcontrol: mixer control
2842 * @uinfo: control element information
2843 *
2844 * Callback to provide information about a signed mixer control.
2845 *
2846 * Returns 0 for success.
2847 */
2848int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2849 struct snd_ctl_elem_info *uinfo)
2850{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002851 struct soc_mixer_control *mc =
2852 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002853 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002854 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002855
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002856 if (!mc->platform_max)
2857 mc->platform_max = mc->max;
2858 platform_max = mc->platform_max;
2859
Mark Browne13ac2e2008-05-28 17:58:05 +01002860 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2861 uinfo->count = 2;
2862 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002863 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002864 return 0;
2865}
2866EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2867
2868/**
2869 * snd_soc_get_volsw_s8 - signed mixer get callback
2870 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002871 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002872 *
2873 * Callback to get the value of a signed mixer control.
2874 *
2875 * Returns 0 for success.
2876 */
2877int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2878 struct snd_ctl_elem_value *ucontrol)
2879{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002880 struct soc_mixer_control *mc =
2881 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002882 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002883 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002884 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002885 int val = snd_soc_read(codec, reg);
2886
2887 ucontrol->value.integer.value[0] =
2888 ((signed char)(val & 0xff))-min;
2889 ucontrol->value.integer.value[1] =
2890 ((signed char)((val >> 8) & 0xff))-min;
2891 return 0;
2892}
2893EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2894
2895/**
2896 * snd_soc_put_volsw_sgn - signed mixer put callback
2897 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002898 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002899 *
2900 * Callback to set the value of a signed mixer control.
2901 *
2902 * Returns 0 for success.
2903 */
2904int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2905 struct snd_ctl_elem_value *ucontrol)
2906{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002907 struct soc_mixer_control *mc =
2908 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002909 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002910 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002911 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002912 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002913
2914 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2915 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2916
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002917 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002918}
2919EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2920
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002921/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002922 * snd_soc_info_volsw_range - single mixer info callback with range.
2923 * @kcontrol: mixer control
2924 * @uinfo: control element information
2925 *
2926 * Callback to provide information, within a range, about a single
2927 * mixer control.
2928 *
2929 * returns 0 for success.
2930 */
2931int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2932 struct snd_ctl_elem_info *uinfo)
2933{
2934 struct soc_mixer_control *mc =
2935 (struct soc_mixer_control *)kcontrol->private_value;
2936 int platform_max;
2937 int min = mc->min;
2938
2939 if (!mc->platform_max)
2940 mc->platform_max = mc->max;
2941 platform_max = mc->platform_max;
2942
2943 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002944 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002945 uinfo->value.integer.min = 0;
2946 uinfo->value.integer.max = platform_max - min;
2947
2948 return 0;
2949}
2950EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2951
2952/**
2953 * snd_soc_put_volsw_range - single mixer put value callback with range.
2954 * @kcontrol: mixer control
2955 * @ucontrol: control element information
2956 *
2957 * Callback to set the value, within a range, for a single mixer control.
2958 *
2959 * Returns 0 for success.
2960 */
2961int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2962 struct snd_ctl_elem_value *ucontrol)
2963{
2964 struct soc_mixer_control *mc =
2965 (struct soc_mixer_control *)kcontrol->private_value;
2966 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2967 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002968 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002969 unsigned int shift = mc->shift;
2970 int min = mc->min;
2971 int max = mc->max;
2972 unsigned int mask = (1 << fls(max)) - 1;
2973 unsigned int invert = mc->invert;
2974 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002975 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002976
2977 val = ((ucontrol->value.integer.value[0] + min) & mask);
2978 if (invert)
2979 val = max - val;
2980 val_mask = mask << shift;
2981 val = val << shift;
2982
Mark Brown9bde4f02012-12-19 16:05:00 +00002983 ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09002984 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00002985 return ret;
2986
2987 if (snd_soc_volsw_is_stereo(mc)) {
2988 val = ((ucontrol->value.integer.value[1] + min) & mask);
2989 if (invert)
2990 val = max - val;
2991 val_mask = mask << shift;
2992 val = val << shift;
2993
2994 ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
2995 }
2996
2997 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002998}
2999EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
3000
3001/**
3002 * snd_soc_get_volsw_range - single mixer get callback with range
3003 * @kcontrol: mixer control
3004 * @ucontrol: control element information
3005 *
3006 * Callback to get the value, within a range, of a single mixer control.
3007 *
3008 * Returns 0 for success.
3009 */
3010int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
3011 struct snd_ctl_elem_value *ucontrol)
3012{
3013 struct soc_mixer_control *mc =
3014 (struct soc_mixer_control *)kcontrol->private_value;
3015 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3016 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00003017 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003018 unsigned int shift = mc->shift;
3019 int min = mc->min;
3020 int max = mc->max;
3021 unsigned int mask = (1 << fls(max)) - 1;
3022 unsigned int invert = mc->invert;
3023
3024 ucontrol->value.integer.value[0] =
3025 (snd_soc_read(codec, reg) >> shift) & mask;
3026 if (invert)
3027 ucontrol->value.integer.value[0] =
3028 max - ucontrol->value.integer.value[0];
3029 ucontrol->value.integer.value[0] =
3030 ucontrol->value.integer.value[0] - min;
3031
Mark Brown9bde4f02012-12-19 16:05:00 +00003032 if (snd_soc_volsw_is_stereo(mc)) {
3033 ucontrol->value.integer.value[1] =
3034 (snd_soc_read(codec, rreg) >> shift) & mask;
3035 if (invert)
3036 ucontrol->value.integer.value[1] =
3037 max - ucontrol->value.integer.value[1];
3038 ucontrol->value.integer.value[1] =
3039 ucontrol->value.integer.value[1] - min;
3040 }
3041
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003042 return 0;
3043}
3044EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3045
3046/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003047 * snd_soc_limit_volume - Set new limit to an existing volume control.
3048 *
3049 * @codec: where to look for the control
3050 * @name: Name of the control
3051 * @max: new maximum limit
3052 *
3053 * Return 0 for success, else error.
3054 */
3055int snd_soc_limit_volume(struct snd_soc_codec *codec,
3056 const char *name, int max)
3057{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003058 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003059 struct snd_kcontrol *kctl;
3060 struct soc_mixer_control *mc;
3061 int found = 0;
3062 int ret = -EINVAL;
3063
3064 /* Sanity check for name and max */
3065 if (unlikely(!name || max <= 0))
3066 return -EINVAL;
3067
3068 list_for_each_entry(kctl, &card->controls, list) {
3069 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3070 found = 1;
3071 break;
3072 }
3073 }
3074 if (found) {
3075 mc = (struct soc_mixer_control *)kctl->private_value;
3076 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003077 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003078 ret = 0;
3079 }
3080 }
3081 return ret;
3082}
3083EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3084
Mark Brown71d08512011-10-10 18:31:26 +01003085int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3086 struct snd_ctl_elem_info *uinfo)
3087{
3088 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3089 struct soc_bytes *params = (void *)kcontrol->private_value;
3090
3091 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3092 uinfo->count = params->num_regs * codec->val_bytes;
3093
3094 return 0;
3095}
3096EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3097
3098int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3099 struct snd_ctl_elem_value *ucontrol)
3100{
3101 struct soc_bytes *params = (void *)kcontrol->private_value;
3102 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3103 int ret;
3104
3105 if (codec->using_regmap)
3106 ret = regmap_raw_read(codec->control_data, params->base,
3107 ucontrol->value.bytes.data,
3108 params->num_regs * codec->val_bytes);
3109 else
3110 ret = -EINVAL;
3111
Mark Brownf831b052012-02-17 16:20:33 -08003112 /* Hide any masked bytes to ensure consistent data reporting */
3113 if (ret == 0 && params->mask) {
3114 switch (codec->val_bytes) {
3115 case 1:
3116 ucontrol->value.bytes.data[0] &= ~params->mask;
3117 break;
3118 case 2:
3119 ((u16 *)(&ucontrol->value.bytes.data))[0]
3120 &= ~params->mask;
3121 break;
3122 case 4:
3123 ((u32 *)(&ucontrol->value.bytes.data))[0]
3124 &= ~params->mask;
3125 break;
3126 default:
3127 return -EINVAL;
3128 }
3129 }
3130
Mark Brown71d08512011-10-10 18:31:26 +01003131 return ret;
3132}
3133EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3134
3135int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3136 struct snd_ctl_elem_value *ucontrol)
3137{
3138 struct soc_bytes *params = (void *)kcontrol->private_value;
3139 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08003140 int ret, len;
3141 unsigned int val;
3142 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003143
Mark Brownf831b052012-02-17 16:20:33 -08003144 if (!codec->using_regmap)
3145 return -EINVAL;
3146
Mark Brownf831b052012-02-17 16:20:33 -08003147 len = params->num_regs * codec->val_bytes;
3148
Mark Brownb5a8fe42013-01-20 21:42:22 +09003149 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3150 if (!data)
3151 return -ENOMEM;
3152
Mark Brownf831b052012-02-17 16:20:33 -08003153 /*
3154 * If we've got a mask then we need to preserve the register
3155 * bits. We shouldn't modify the incoming data so take a
3156 * copy.
3157 */
3158 if (params->mask) {
3159 ret = regmap_read(codec->control_data, params->base, &val);
3160 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003161 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003162
3163 val &= params->mask;
3164
Mark Brownf831b052012-02-17 16:20:33 -08003165 switch (codec->val_bytes) {
3166 case 1:
3167 ((u8 *)data)[0] &= ~params->mask;
3168 ((u8 *)data)[0] |= val;
3169 break;
3170 case 2:
3171 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3172 ((u16 *)data)[0] |= cpu_to_be16(val);
3173 break;
3174 case 4:
3175 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3176 ((u32 *)data)[0] |= cpu_to_be32(val);
3177 break;
3178 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003179 ret = -EINVAL;
3180 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003181 }
3182 }
3183
3184 ret = regmap_raw_write(codec->control_data, params->base,
3185 data, len);
3186
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003187out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003188 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003189
3190 return ret;
3191}
3192EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3193
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003194/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003195 * snd_soc_info_xr_sx - signed multi register info callback
3196 * @kcontrol: mreg control
3197 * @uinfo: control element information
3198 *
3199 * Callback to provide information of a control that can
3200 * span multiple codec registers which together
3201 * forms a single signed value in a MSB/LSB manner.
3202 *
3203 * Returns 0 for success.
3204 */
3205int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3206 struct snd_ctl_elem_info *uinfo)
3207{
3208 struct soc_mreg_control *mc =
3209 (struct soc_mreg_control *)kcontrol->private_value;
3210 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3211 uinfo->count = 1;
3212 uinfo->value.integer.min = mc->min;
3213 uinfo->value.integer.max = mc->max;
3214
3215 return 0;
3216}
3217EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3218
3219/**
3220 * snd_soc_get_xr_sx - signed multi register get callback
3221 * @kcontrol: mreg control
3222 * @ucontrol: control element information
3223 *
3224 * Callback to get the value of a control that can span
3225 * multiple codec registers which together forms a single
3226 * signed value in a MSB/LSB manner. The control supports
3227 * specifying total no of bits used to allow for bitfields
3228 * across the multiple codec registers.
3229 *
3230 * Returns 0 for success.
3231 */
3232int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3233 struct snd_ctl_elem_value *ucontrol)
3234{
3235 struct soc_mreg_control *mc =
3236 (struct soc_mreg_control *)kcontrol->private_value;
3237 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3238 unsigned int regbase = mc->regbase;
3239 unsigned int regcount = mc->regcount;
3240 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3241 unsigned int regwmask = (1<<regwshift)-1;
3242 unsigned int invert = mc->invert;
3243 unsigned long mask = (1UL<<mc->nbits)-1;
3244 long min = mc->min;
3245 long max = mc->max;
3246 long val = 0;
3247 unsigned long regval;
3248 unsigned int i;
3249
3250 for (i = 0; i < regcount; i++) {
3251 regval = snd_soc_read(codec, regbase+i) & regwmask;
3252 val |= regval << (regwshift*(regcount-i-1));
3253 }
3254 val &= mask;
3255 if (min < 0 && val > max)
3256 val |= ~mask;
3257 if (invert)
3258 val = max - val;
3259 ucontrol->value.integer.value[0] = val;
3260
3261 return 0;
3262}
3263EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3264
3265/**
3266 * snd_soc_put_xr_sx - signed multi register get callback
3267 * @kcontrol: mreg control
3268 * @ucontrol: control element information
3269 *
3270 * Callback to set the value of a control that can span
3271 * multiple codec registers which together forms a single
3272 * signed value in a MSB/LSB manner. The control supports
3273 * specifying total no of bits used to allow for bitfields
3274 * across the multiple codec registers.
3275 *
3276 * Returns 0 for success.
3277 */
3278int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3279 struct snd_ctl_elem_value *ucontrol)
3280{
3281 struct soc_mreg_control *mc =
3282 (struct soc_mreg_control *)kcontrol->private_value;
3283 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3284 unsigned int regbase = mc->regbase;
3285 unsigned int regcount = mc->regcount;
3286 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3287 unsigned int regwmask = (1<<regwshift)-1;
3288 unsigned int invert = mc->invert;
3289 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003290 long max = mc->max;
3291 long val = ucontrol->value.integer.value[0];
3292 unsigned int i, regval, regmask;
3293 int err;
3294
3295 if (invert)
3296 val = max - val;
3297 val &= mask;
3298 for (i = 0; i < regcount; i++) {
3299 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3300 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3301 err = snd_soc_update_bits_locked(codec, regbase+i,
3302 regmask, regval);
3303 if (err < 0)
3304 return err;
3305 }
3306
3307 return 0;
3308}
3309EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3310
3311/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003312 * snd_soc_get_strobe - strobe get callback
3313 * @kcontrol: mixer control
3314 * @ucontrol: control element information
3315 *
3316 * Callback get the value of a strobe mixer control.
3317 *
3318 * Returns 0 for success.
3319 */
3320int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3321 struct snd_ctl_elem_value *ucontrol)
3322{
3323 struct soc_mixer_control *mc =
3324 (struct soc_mixer_control *)kcontrol->private_value;
3325 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3326 unsigned int reg = mc->reg;
3327 unsigned int shift = mc->shift;
3328 unsigned int mask = 1 << shift;
3329 unsigned int invert = mc->invert != 0;
3330 unsigned int val = snd_soc_read(codec, reg) & mask;
3331
3332 if (shift != 0 && val != 0)
3333 val = val >> shift;
3334 ucontrol->value.enumerated.item[0] = val ^ invert;
3335
3336 return 0;
3337}
3338EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3339
3340/**
3341 * snd_soc_put_strobe - strobe put callback
3342 * @kcontrol: mixer control
3343 * @ucontrol: control element information
3344 *
3345 * Callback strobe a register bit to high then low (or the inverse)
3346 * in one pass of a single mixer enum control.
3347 *
3348 * Returns 1 for success.
3349 */
3350int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3351 struct snd_ctl_elem_value *ucontrol)
3352{
3353 struct soc_mixer_control *mc =
3354 (struct soc_mixer_control *)kcontrol->private_value;
3355 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3356 unsigned int reg = mc->reg;
3357 unsigned int shift = mc->shift;
3358 unsigned int mask = 1 << shift;
3359 unsigned int invert = mc->invert != 0;
3360 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3361 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3362 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3363 int err;
3364
3365 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3366 if (err < 0)
3367 return err;
3368
3369 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3370 return err;
3371}
3372EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3373
3374/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003375 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3376 * @dai: DAI
3377 * @clk_id: DAI specific clock ID
3378 * @freq: new clock frequency in Hz
3379 * @dir: new clock direction - input/output.
3380 *
3381 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3382 */
3383int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3384 unsigned int freq, int dir)
3385{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003386 if (dai->driver && dai->driver->ops->set_sysclk)
3387 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003388 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003389 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003390 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003391 else
3392 return -EINVAL;
3393}
3394EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3395
3396/**
Mark Brownec4ee522011-03-07 20:58:11 +00003397 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3398 * @codec: CODEC
3399 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003400 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003401 * @freq: new clock frequency in Hz
3402 * @dir: new clock direction - input/output.
3403 *
3404 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3405 */
3406int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003407 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003408{
3409 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003410 return codec->driver->set_sysclk(codec, clk_id, source,
3411 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003412 else
3413 return -EINVAL;
3414}
3415EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3416
3417/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003418 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3419 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003420 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003421 * @div: new clock divisor.
3422 *
3423 * Configures the clock dividers. This is used to derive the best DAI bit and
3424 * frame clocks from the system or master clock. It's best to set the DAI bit
3425 * and frame clocks as low as possible to save system power.
3426 */
3427int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3428 int div_id, int div)
3429{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003430 if (dai->driver && dai->driver->ops->set_clkdiv)
3431 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003432 else
3433 return -EINVAL;
3434}
3435EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3436
3437/**
3438 * snd_soc_dai_set_pll - configure DAI PLL.
3439 * @dai: DAI
3440 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003441 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003442 * @freq_in: PLL input clock frequency in Hz
3443 * @freq_out: requested PLL output clock frequency in Hz
3444 *
3445 * Configures and enables PLL to generate output clock based on input clock.
3446 */
Mark Brown85488032009-09-05 18:52:16 +01003447int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3448 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003449{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003450 if (dai->driver && dai->driver->ops->set_pll)
3451 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003452 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003453 else if (dai->codec && dai->codec->driver->set_pll)
3454 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3455 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003456 else
3457 return -EINVAL;
3458}
3459EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3460
Mark Brownec4ee522011-03-07 20:58:11 +00003461/*
3462 * snd_soc_codec_set_pll - configure codec PLL.
3463 * @codec: CODEC
3464 * @pll_id: DAI specific PLL ID
3465 * @source: DAI specific source for the PLL
3466 * @freq_in: PLL input clock frequency in Hz
3467 * @freq_out: requested PLL output clock frequency in Hz
3468 *
3469 * Configures and enables PLL to generate output clock based on input clock.
3470 */
3471int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3472 unsigned int freq_in, unsigned int freq_out)
3473{
3474 if (codec->driver->set_pll)
3475 return codec->driver->set_pll(codec, pll_id, source,
3476 freq_in, freq_out);
3477 else
3478 return -EINVAL;
3479}
3480EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3481
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003482/**
3483 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3484 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003485 * @fmt: SND_SOC_DAIFMT_ format value.
3486 *
3487 * Configures the DAI hardware format and clocking.
3488 */
3489int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3490{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003491 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003492 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003493 if (dai->driver->ops->set_fmt == NULL)
3494 return -ENOTSUPP;
3495 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003496}
3497EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3498
3499/**
3500 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3501 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003502 * @tx_mask: bitmask representing active TX slots.
3503 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003504 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003505 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003506 *
3507 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3508 * specific.
3509 */
3510int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003511 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003512{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003513 if (dai->driver && dai->driver->ops->set_tdm_slot)
3514 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003515 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003516 else
3517 return -EINVAL;
3518}
3519EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3520
3521/**
Barry Song472df3c2009-09-12 01:16:29 +08003522 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3523 * @dai: DAI
3524 * @tx_num: how many TX channels
3525 * @tx_slot: pointer to an array which imply the TX slot number channel
3526 * 0~num-1 uses
3527 * @rx_num: how many RX channels
3528 * @rx_slot: pointer to an array which imply the RX slot number channel
3529 * 0~num-1 uses
3530 *
3531 * configure the relationship between channel number and TDM slot number.
3532 */
3533int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3534 unsigned int tx_num, unsigned int *tx_slot,
3535 unsigned int rx_num, unsigned int *rx_slot)
3536{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003537 if (dai->driver && dai->driver->ops->set_channel_map)
3538 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003539 rx_num, rx_slot);
3540 else
3541 return -EINVAL;
3542}
3543EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3544
3545/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003546 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3547 * @dai: DAI
3548 * @tristate: tristate enable
3549 *
3550 * Tristates the DAI so that others can use it.
3551 */
3552int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3553{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003554 if (dai->driver && dai->driver->ops->set_tristate)
3555 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003556 else
3557 return -EINVAL;
3558}
3559EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3560
3561/**
3562 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3563 * @dai: DAI
3564 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003565 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003566 *
3567 * Mutes the DAI DAC.
3568 */
Mark Brownda183962013-02-06 15:44:07 +00003569int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3570 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003571{
Mark Brownda183962013-02-06 15:44:07 +00003572 if (!dai->driver)
3573 return -ENOTSUPP;
3574
3575 if (dai->driver->ops->mute_stream)
3576 return dai->driver->ops->mute_stream(dai, mute, direction);
3577 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3578 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003579 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003580 else
Mark Brown04570c62012-04-13 19:16:03 +01003581 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003582}
3583EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3584
Mark Brownc5af3a22008-11-28 13:29:45 +00003585/**
3586 * snd_soc_register_card - Register a card with the ASoC core
3587 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003588 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003589 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003590 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303591int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003592{
Mark Brownb19e6e72012-03-14 21:18:39 +00003593 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003594
Mark Brownc5af3a22008-11-28 13:29:45 +00003595 if (!card->name || !card->dev)
3596 return -EINVAL;
3597
Stephen Warren5a504962011-12-21 10:40:59 -07003598 for (i = 0; i < card->num_links; i++) {
3599 struct snd_soc_dai_link *link = &card->dai_link[i];
3600
3601 /*
3602 * Codec must be specified by 1 of name or OF node,
3603 * not both or neither.
3604 */
3605 if (!!link->codec_name == !!link->codec_of_node) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003606 dev_err(card->dev, "ASoC: Neither/both codec"
3607 " name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003608 return -EINVAL;
3609 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003610 /* Codec DAI name must be specified */
3611 if (!link->codec_dai_name) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003612 dev_err(card->dev, "ASoC: codec_dai_name not"
3613 " set for %s\n", link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003614 return -EINVAL;
3615 }
Stephen Warren5a504962011-12-21 10:40:59 -07003616
3617 /*
3618 * Platform may be specified by either name or OF node, but
3619 * can be left unspecified, and a dummy platform will be used.
3620 */
3621 if (link->platform_name && link->platform_of_node) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003622 dev_err(card->dev, "ASoC: Both platform name/of_node"
3623 " are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003624 return -EINVAL;
3625 }
3626
3627 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003628 * CPU device may be specified by either name or OF node, but
3629 * can be left unspecified, and will be matched based on DAI
3630 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003631 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003632 if (link->cpu_name && link->cpu_of_node) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003633 dev_err(card->dev, "ASoC: Neither/both "
3634 "cpu name/of_node are set for %s\n",link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003635 return -EINVAL;
3636 }
3637 /*
3638 * At least one of CPU DAI name or CPU device name/node must be
3639 * specified
3640 */
3641 if (!link->cpu_dai_name &&
3642 !(link->cpu_name || link->cpu_of_node)) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003643 dev_err(card->dev, "ASoC: Neither cpu_dai_name nor "
3644 "cpu_name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003645 return -EINVAL;
3646 }
3647 }
3648
Mark Browned77cc12011-05-03 18:25:34 +01003649 dev_set_drvdata(card->dev, card);
3650
Stephen Warren111c6412011-01-28 14:26:35 -07003651 snd_soc_initialize_card_lists(card);
3652
Vinod Koul150dd2f2011-01-13 22:48:32 +05303653 soc_init_card_debugfs(card);
3654
Mark Brown181a6892012-03-14 20:18:49 +00003655 card->rtd = devm_kzalloc(card->dev,
3656 sizeof(struct snd_soc_pcm_runtime) *
3657 (card->num_links + card->num_aux_devs),
3658 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003659 if (card->rtd == NULL)
3660 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003661 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003662 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003663
3664 for (i = 0; i < card->num_links; i++)
3665 card->rtd[i].dai_link = &card->dai_link[i];
3666
Mark Brownc5af3a22008-11-28 13:29:45 +00003667 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003668 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003669 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003670 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003671 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003672
Mark Brownb19e6e72012-03-14 21:18:39 +00003673 ret = snd_soc_instantiate_card(card);
3674 if (ret != 0)
3675 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003676
Mark Brownb19e6e72012-03-14 21:18:39 +00003677 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003678}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303679EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003680
3681/**
3682 * snd_soc_unregister_card - Unregister a card with the ASoC core
3683 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003684 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003685 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003686 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303687int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003688{
Vinod Koulb0e26482011-01-13 22:48:02 +05303689 if (card->instantiated)
3690 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003691 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003692
3693 return 0;
3694}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303695EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003696
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003697/*
3698 * Simplify DAI link configuration by removing ".-1" from device names
3699 * and sanitizing names.
3700 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003701static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003702{
3703 char *found, name[NAME_SIZE];
3704 int id1, id2;
3705
3706 if (dev_name(dev) == NULL)
3707 return NULL;
3708
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003709 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003710
3711 /* are we a "%s.%d" name (platform and SPI components) */
3712 found = strstr(name, dev->driver->name);
3713 if (found) {
3714 /* get ID */
3715 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3716
3717 /* discard ID from name if ID == -1 */
3718 if (*id == -1)
3719 found[strlen(dev->driver->name)] = '\0';
3720 }
3721
3722 } else {
3723 /* I2C component devices are named "bus-addr" */
3724 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3725 char tmp[NAME_SIZE];
3726
3727 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003728 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003729
3730 /* sanitize component name for DAI link creation */
3731 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003732 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003733 } else
3734 *id = 0;
3735 }
3736
3737 return kstrdup(name, GFP_KERNEL);
3738}
3739
3740/*
3741 * Simplify DAI link naming for single devices with multiple DAIs by removing
3742 * any ".-1" and using the DAI name (instead of device name).
3743 */
3744static inline char *fmt_multiple_name(struct device *dev,
3745 struct snd_soc_dai_driver *dai_drv)
3746{
3747 if (dai_drv->name == NULL) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003748 dev_err(dev, "ASoC: error - multiple DAI %s registered with"
3749 " no name\n", dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003750 return NULL;
3751 }
3752
3753 return kstrdup(dai_drv->name, GFP_KERNEL);
3754}
3755
Mark Brown91151712008-11-30 23:31:24 +00003756/**
3757 * snd_soc_register_dai - Register a DAI with the ASoC core
3758 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003759 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003760 */
Kuninori Morimotof53179c02013-03-21 03:38:30 -07003761static int snd_soc_register_dai(struct device *dev,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003762 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003763{
Mark Brown054880f2012-04-09 17:29:19 +01003764 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003765 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003766
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003767 dev_dbg(dev, "ASoC: dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003768
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003769 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3770 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003771 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003772
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003773 /* create DAI component name */
3774 dai->name = fmt_single_name(dev, &dai->id);
3775 if (dai->name == NULL) {
3776 kfree(dai);
3777 return -ENOMEM;
3778 }
3779
3780 dai->dev = dev;
3781 dai->driver = dai_drv;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003782 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003783 if (!dai->driver->ops)
3784 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003785
3786 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003787
3788 list_for_each_entry(codec, &codec_list, list) {
3789 if (codec->dev == dev) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003790 dev_dbg(dev, "ASoC: Mapped DAI %s to CODEC %s\n",
Mark Brown054880f2012-04-09 17:29:19 +01003791 dai->name, codec->name);
3792 dai->codec = codec;
3793 break;
3794 }
3795 }
3796
Peter Ujfalusi5f800082012-08-07 10:24:13 +03003797 if (!dai->codec)
3798 dai->dapm.idle_bias_off = 1;
3799
Mark Brown91151712008-11-30 23:31:24 +00003800 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003801
Mark Brown91151712008-11-30 23:31:24 +00003802 mutex_unlock(&client_mutex);
3803
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003804 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003805
3806 return 0;
3807}
Mark Brown91151712008-11-30 23:31:24 +00003808
3809/**
3810 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3811 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003812 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003813 */
Kuninori Morimotof53179c02013-03-21 03:38:30 -07003814static void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003815{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003816 struct snd_soc_dai *dai;
3817
3818 list_for_each_entry(dai, &dai_list, list) {
3819 if (dev == dai->dev)
3820 goto found;
3821 }
3822 return;
3823
3824found:
Mark Brown91151712008-11-30 23:31:24 +00003825 mutex_lock(&client_mutex);
3826 list_del(&dai->list);
3827 mutex_unlock(&client_mutex);
3828
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003829 dev_dbg(dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003830 kfree(dai->name);
3831 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003832}
Mark Brown91151712008-11-30 23:31:24 +00003833
3834/**
3835 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3836 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003837 * @dai: Array of DAIs to register
3838 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003839 */
Kuninori Morimotof53179c02013-03-21 03:38:30 -07003840static int snd_soc_register_dais(struct device *dev,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003841 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003842{
Mark Brown054880f2012-04-09 17:29:19 +01003843 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003844 struct snd_soc_dai *dai;
3845 int i, ret = 0;
3846
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003847 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003848
3849 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003850
3851 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003852 if (dai == NULL) {
3853 ret = -ENOMEM;
3854 goto err;
3855 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003856
3857 /* create DAI component name */
3858 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3859 if (dai->name == NULL) {
3860 kfree(dai);
3861 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003862 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003863 }
3864
3865 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003866 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003867 if (dai->driver->id)
3868 dai->id = dai->driver->id;
3869 else
3870 dai->id = i;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003871 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003872 if (!dai->driver->ops)
3873 dai->driver->ops = &null_dai_ops;
3874
3875 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003876
3877 list_for_each_entry(codec, &codec_list, list) {
3878 if (codec->dev == dev) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003879 dev_dbg(dev, "ASoC: Mapped DAI %s to "
3880 "CODEC %s\n", dai->name, codec->name);
Mark Brown054880f2012-04-09 17:29:19 +01003881 dai->codec = codec;
3882 break;
3883 }
3884 }
3885
Peter Ujfalusi5f800082012-08-07 10:24:13 +03003886 if (!dai->codec)
3887 dai->dapm.idle_bias_off = 1;
3888
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003889 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003890
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003891 mutex_unlock(&client_mutex);
3892
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003893 dev_dbg(dai->dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003894 }
3895
3896 return 0;
3897
3898err:
3899 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003900 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003901
3902 return ret;
3903}
Mark Brown91151712008-11-30 23:31:24 +00003904
3905/**
3906 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3907 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003908 * @dai: Array of DAIs to unregister
3909 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003910 */
Kuninori Morimotof53179c02013-03-21 03:38:30 -07003911static void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003912{
3913 int i;
3914
3915 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003916 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003917}
Mark Brown91151712008-11-30 23:31:24 +00003918
Mark Brown12a48a8c2008-12-03 19:40:30 +00003919/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003920 * snd_soc_add_platform - Add a platform to the ASoC core
3921 * @dev: The parent device for the platform
3922 * @platform: The platform to add
3923 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00003924 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003925int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01003926 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003927{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003928 /* create platform component name */
3929 platform->name = fmt_single_name(dev, &platform->id);
3930 if (platform->name == NULL) {
3931 kfree(platform);
3932 return -ENOMEM;
3933 }
3934
3935 platform->dev = dev;
3936 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003937 platform->dapm.dev = dev;
3938 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003939 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00003940 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003941
3942 mutex_lock(&client_mutex);
3943 list_add(&platform->list, &platform_list);
3944 mutex_unlock(&client_mutex);
3945
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003946 dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003947
3948 return 0;
3949}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003950EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3951
3952/**
3953 * snd_soc_register_platform - Register a platform with the ASoC core
3954 *
3955 * @platform: platform to register
3956 */
3957int snd_soc_register_platform(struct device *dev,
3958 const struct snd_soc_platform_driver *platform_drv)
3959{
3960 struct snd_soc_platform *platform;
3961 int ret;
3962
3963 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3964
3965 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3966 if (platform == NULL)
3967 return -ENOMEM;
3968
3969 ret = snd_soc_add_platform(dev, platform, platform_drv);
3970 if (ret)
3971 kfree(platform);
3972
3973 return ret;
3974}
Mark Brown12a48a8c2008-12-03 19:40:30 +00003975EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3976
3977/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003978 * snd_soc_remove_platform - Remove a platform from the ASoC core
3979 * @platform: the platform to remove
3980 */
3981void snd_soc_remove_platform(struct snd_soc_platform *platform)
3982{
3983 mutex_lock(&client_mutex);
3984 list_del(&platform->list);
3985 mutex_unlock(&client_mutex);
3986
3987 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3988 platform->name);
3989 kfree(platform->name);
3990}
3991EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3992
3993struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3994{
3995 struct snd_soc_platform *platform;
3996
3997 list_for_each_entry(platform, &platform_list, list) {
3998 if (dev == platform->dev)
3999 return platform;
4000 }
4001
4002 return NULL;
4003}
4004EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4005
4006/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004007 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4008 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004009 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004010 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004011void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004012{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004013 struct snd_soc_platform *platform;
4014
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004015 platform = snd_soc_lookup_platform(dev);
4016 if (!platform)
4017 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004018
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004019 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004020 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004021}
4022EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4023
Mark Brown151ab222009-05-09 16:22:58 +01004024static u64 codec_format_map[] = {
4025 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4026 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4027 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4028 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4029 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4030 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4031 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4032 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4033 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4034 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4035 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4036 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4037 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4038 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4039 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4040 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4041};
4042
4043/* Fix up the DAI formats for endianness: codecs don't actually see
4044 * the endianness of the data but we're using the CPU format
4045 * definitions which do need to include endianness so we ensure that
4046 * codec DAIs always have both big and little endian variants set.
4047 */
4048static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4049{
4050 int i;
4051
4052 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4053 if (stream->formats & codec_format_map[i])
4054 stream->formats |= codec_format_map[i];
4055}
4056
Mark Brown0d0cf002008-12-10 14:32:45 +00004057/**
4058 * snd_soc_register_codec - Register a codec with the ASoC core
4059 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004060 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004061 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004062int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004063 const struct snd_soc_codec_driver *codec_drv,
4064 struct snd_soc_dai_driver *dai_drv,
4065 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004066{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004067 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004068 struct snd_soc_codec *codec;
4069 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004070
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004071 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004072
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004073 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4074 if (codec == NULL)
4075 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004076
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004077 /* create CODEC component name */
4078 codec->name = fmt_single_name(dev, &codec->id);
4079 if (codec->name == NULL) {
Kuninori Morimotof790b942013-02-25 00:40:09 -08004080 ret = -ENOMEM;
4081 goto fail_codec;
Mark Brown151ab222009-05-09 16:22:58 +01004082 }
4083
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00004084 if (codec_drv->compress_type)
4085 codec->compress_type = codec_drv->compress_type;
4086 else
4087 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
4088
Mark Brownc3acec22010-12-02 16:15:29 +00004089 codec->write = codec_drv->write;
4090 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004091 codec->volatile_register = codec_drv->volatile_register;
4092 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004093 codec->writable_register = codec_drv->writable_register;
Mark Brown5124e692012-02-08 13:20:50 +00004094 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004095 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4096 codec->dapm.dev = dev;
4097 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00004098 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01004099 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004100 codec->dev = dev;
4101 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004102 codec->num_dai = num_dai;
4103 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004104
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004105 /* allocate CODEC register cache */
4106 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004107 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00004108 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004109 /* it is necessary to make a copy of the default register cache
4110 * because in the case of using a compression type that requires
Bill Pembertonf6e65742012-11-19 13:25:33 -05004111 * the default register cache to be marked as the
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004112 * kernel might have freed the array by the time we initialize
4113 * the cache.
4114 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00004115 if (codec_drv->reg_cache_default) {
4116 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
4117 reg_size, GFP_KERNEL);
4118 if (!codec->reg_def_copy) {
4119 ret = -ENOMEM;
Kuninori Morimotof790b942013-02-25 00:40:09 -08004120 goto fail_codec_name;
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00004121 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004122 }
4123 }
4124
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004125 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
4126 if (!codec->volatile_register)
4127 codec->volatile_register = snd_soc_default_volatile_register;
4128 if (!codec->readable_register)
4129 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004130 if (!codec->writable_register)
4131 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004132 }
4133
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004134 for (i = 0; i < num_dai; i++) {
4135 fixup_codec_formats(&dai_drv[i].playback);
4136 fixup_codec_formats(&dai_drv[i].capture);
4137 }
4138
Mark Brown054880f2012-04-09 17:29:19 +01004139 mutex_lock(&client_mutex);
4140 list_add(&codec->list, &codec_list);
4141 mutex_unlock(&client_mutex);
4142
Mark Brown26b01cc2010-08-18 20:20:55 +01004143 /* register any DAIs */
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004144 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4145 if (ret < 0) {
4146 dev_err(codec->dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4147 goto fail_codec_name;
Mark Brown26b01cc2010-08-18 20:20:55 +01004148 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004149
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004150 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004151 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004152
Kuninori Morimotof790b942013-02-25 00:40:09 -08004153fail_codec_name:
Kuninori Morimotoe1328a82013-03-07 17:42:33 -08004154 mutex_lock(&client_mutex);
4155 list_del(&codec->list);
4156 mutex_unlock(&client_mutex);
4157
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004158 kfree(codec->name);
Kuninori Morimotof790b942013-02-25 00:40:09 -08004159fail_codec:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004160 kfree(codec);
4161 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004162}
4163EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4164
4165/**
4166 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4167 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004168 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004169 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004170void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004171{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004172 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004173
4174 list_for_each_entry(codec, &codec_list, list) {
4175 if (dev == codec->dev)
4176 goto found;
4177 }
4178 return;
4179
4180found:
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004181 snd_soc_unregister_dais(dev, codec->num_dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004182
Mark Brown0d0cf002008-12-10 14:32:45 +00004183 mutex_lock(&client_mutex);
4184 list_del(&codec->list);
4185 mutex_unlock(&client_mutex);
4186
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004187 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004188
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004189 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004190 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01004191 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004192 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004193}
4194EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4195
Kuninori Morimoto030e79f2013-03-11 18:27:21 -07004196
4197/**
4198 * snd_soc_register_component - Register a component with the ASoC core
4199 *
4200 */
4201int snd_soc_register_component(struct device *dev,
4202 const struct snd_soc_component_driver *cmpnt_drv,
4203 struct snd_soc_dai_driver *dai_drv,
4204 int num_dai)
4205{
4206 struct snd_soc_component *cmpnt;
4207 int ret;
4208
4209 dev_dbg(dev, "component register %s\n", dev_name(dev));
4210
4211 cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
4212 if (!cmpnt) {
4213 dev_err(dev, "ASoC: Failed to allocate memory\n");
4214 return -ENOMEM;
4215 }
4216
4217 cmpnt->name = fmt_single_name(dev, &cmpnt->id);
4218 if (!cmpnt->name) {
4219 dev_err(dev, "ASoC: Failed to simplifying name\n");
4220 return -ENOMEM;
4221 }
4222
4223 cmpnt->dev = dev;
4224 cmpnt->driver = cmpnt_drv;
4225 cmpnt->num_dai = num_dai;
4226
Kuninori Morimotoa1422b82013-03-21 03:27:13 -07004227 /*
4228 * snd_soc_register_dai() uses fmt_single_name(), and
4229 * snd_soc_register_dais() uses fmt_multiple_name()
4230 * for dai->name which is used for name based matching
4231 */
4232 if (1 == num_dai)
4233 ret = snd_soc_register_dai(dev, dai_drv);
4234 else
4235 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -07004236 if (ret < 0) {
4237 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4238 goto error_component_name;
4239 }
4240
4241 mutex_lock(&client_mutex);
4242 list_add(&cmpnt->list, &component_list);
4243 mutex_unlock(&client_mutex);
4244
4245 dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
4246
4247 return ret;
4248
4249error_component_name:
4250 kfree(cmpnt->name);
4251
4252 return ret;
4253}
Stephen Warren2e1cc192013-03-26 16:38:19 -06004254EXPORT_SYMBOL_GPL(snd_soc_register_component);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -07004255
4256/**
4257 * snd_soc_unregister_component - Unregister a component from the ASoC core
4258 *
4259 */
4260void snd_soc_unregister_component(struct device *dev)
4261{
4262 struct snd_soc_component *cmpnt;
4263
4264 list_for_each_entry(cmpnt, &component_list, list) {
4265 if (dev == cmpnt->dev)
4266 goto found;
4267 }
4268 return;
4269
4270found:
4271 snd_soc_unregister_dais(dev, cmpnt->num_dai);
4272
4273 mutex_lock(&client_mutex);
4274 list_del(&cmpnt->list);
4275 mutex_unlock(&client_mutex);
4276
4277 dev_dbg(dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
4278 kfree(cmpnt->name);
4279}
Stephen Warren2e1cc192013-03-26 16:38:19 -06004280EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -07004281
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004282/* Retrieve a card's name from device tree */
4283int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4284 const char *propname)
4285{
4286 struct device_node *np = card->dev->of_node;
4287 int ret;
4288
4289 ret = of_property_read_string_index(np, propname, 0, &card->name);
4290 /*
4291 * EINVAL means the property does not exist. This is fine providing
4292 * card->name was previously set, which is checked later in
4293 * snd_soc_register_card.
4294 */
4295 if (ret < 0 && ret != -EINVAL) {
4296 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004297 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004298 propname, ret);
4299 return ret;
4300 }
4301
4302 return 0;
4303}
4304EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4305
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004306int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4307 const char *propname)
4308{
4309 struct device_node *np = card->dev->of_node;
4310 int num_routes;
4311 struct snd_soc_dapm_route *routes;
4312 int i, ret;
4313
4314 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004315 if (num_routes < 0 || num_routes & 1) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004316 dev_err(card->dev, "ASoC: Property '%s' does not exist or its"
4317 " length is not even\n", propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004318 return -EINVAL;
4319 }
4320 num_routes /= 2;
4321 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004322 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004323 propname);
4324 return -EINVAL;
4325 }
4326
4327 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4328 GFP_KERNEL);
4329 if (!routes) {
4330 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004331 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004332 return -EINVAL;
4333 }
4334
4335 for (i = 0; i < num_routes; i++) {
4336 ret = of_property_read_string_index(np, propname,
4337 2 * i, &routes[i].sink);
4338 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004339 dev_err(card->dev,
4340 "ASoC: Property '%s' index %d could not be read: %d\n",
4341 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004342 return -EINVAL;
4343 }
4344 ret = of_property_read_string_index(np, propname,
4345 (2 * i) + 1, &routes[i].source);
4346 if (ret) {
4347 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004348 "ASoC: Property '%s' index %d could not be read: %d\n",
4349 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004350 return -EINVAL;
4351 }
4352 }
4353
4354 card->num_dapm_routes = num_routes;
4355 card->dapm_routes = routes;
4356
4357 return 0;
4358}
4359EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4360
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004361unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4362 const char *prefix)
4363{
4364 int ret, i;
4365 char prop[128];
4366 unsigned int format = 0;
4367 int bit, frame;
4368 const char *str;
4369 struct {
4370 char *name;
4371 unsigned int val;
4372 } of_fmt_table[] = {
4373 { "i2s", SND_SOC_DAIFMT_I2S },
4374 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4375 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4376 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4377 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4378 { "ac97", SND_SOC_DAIFMT_AC97 },
4379 { "pdm", SND_SOC_DAIFMT_PDM},
4380 { "msb", SND_SOC_DAIFMT_MSB },
4381 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004382 };
4383
4384 if (!prefix)
4385 prefix = "";
4386
4387 /*
4388 * check "[prefix]format = xxx"
4389 * SND_SOC_DAIFMT_FORMAT_MASK area
4390 */
4391 snprintf(prop, sizeof(prop), "%sformat", prefix);
4392 ret = of_property_read_string(np, prop, &str);
4393 if (ret == 0) {
4394 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4395 if (strcmp(str, of_fmt_table[i].name) == 0) {
4396 format |= of_fmt_table[i].val;
4397 break;
4398 }
4399 }
4400 }
4401
4402 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004403 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004404 * SND_SOC_DAIFMT_CLOCK_MASK area
4405 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004406 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4407 if (of_get_property(np, prop, NULL))
4408 format |= SND_SOC_DAIFMT_CONT;
4409 else
4410 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004411
4412 /*
4413 * check "[prefix]bitclock-inversion"
4414 * check "[prefix]frame-inversion"
4415 * SND_SOC_DAIFMT_INV_MASK area
4416 */
4417 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4418 bit = !!of_get_property(np, prop, NULL);
4419
4420 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4421 frame = !!of_get_property(np, prop, NULL);
4422
4423 switch ((bit << 4) + frame) {
4424 case 0x11:
4425 format |= SND_SOC_DAIFMT_IB_IF;
4426 break;
4427 case 0x10:
4428 format |= SND_SOC_DAIFMT_IB_NF;
4429 break;
4430 case 0x01:
4431 format |= SND_SOC_DAIFMT_NB_IF;
4432 break;
4433 default:
4434 /* SND_SOC_DAIFMT_NB_NF is default */
4435 break;
4436 }
4437
4438 /*
4439 * check "[prefix]bitclock-master"
4440 * check "[prefix]frame-master"
4441 * SND_SOC_DAIFMT_MASTER_MASK area
4442 */
4443 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4444 bit = !!of_get_property(np, prop, NULL);
4445
4446 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4447 frame = !!of_get_property(np, prop, NULL);
4448
4449 switch ((bit << 4) + frame) {
4450 case 0x11:
4451 format |= SND_SOC_DAIFMT_CBM_CFM;
4452 break;
4453 case 0x10:
4454 format |= SND_SOC_DAIFMT_CBM_CFS;
4455 break;
4456 case 0x01:
4457 format |= SND_SOC_DAIFMT_CBS_CFM;
4458 break;
4459 default:
4460 format |= SND_SOC_DAIFMT_CBS_CFS;
4461 break;
4462 }
4463
4464 return format;
4465}
4466EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4467
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004468static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004469{
Mark Brown384c89e2008-12-03 17:34:03 +00004470#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004471 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4472 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004473 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004474 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004475 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004476
Mark Brown8a9dab12011-01-10 22:25:21 +00004477 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004478 &codec_list_fops))
4479 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4480
Mark Brown8a9dab12011-01-10 22:25:21 +00004481 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004482 &dai_list_fops))
4483 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004484
Mark Brown8a9dab12011-01-10 22:25:21 +00004485 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004486 &platform_list_fops))
4487 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004488#endif
4489
Mark Brownfb257892011-04-28 10:57:54 +01004490 snd_soc_util_init();
4491
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004492 return platform_driver_register(&soc_driver);
4493}
Mark Brown4abe8e12010-10-12 17:41:03 +01004494module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004495
Mark Brown7d8c16a2008-11-30 22:11:24 +00004496static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004497{
Mark Brownfb257892011-04-28 10:57:54 +01004498 snd_soc_util_exit();
4499
Mark Brown384c89e2008-12-03 17:34:03 +00004500#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004501 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004502#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004503 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004504}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004505module_exit(snd_soc_exit);
4506
4507/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004508MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004509MODULE_DESCRIPTION("ALSA SoC Core");
4510MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004511MODULE_ALIAS("platform:soc-audio");