blob: cef714effc1e31edcc6db312560071b7fe7ff5c8 [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) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200275 dev_warn(codec->dev,
276 "ASoC: Failed to create codec debugfs 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)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200289 dev_warn(codec->dev,
290 "ASoC: Failed to create codec register 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,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200634 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000635 break;
636 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000637 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100638 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900640 codec->cache_sync = 1;
Mark Brownda8b8e02012-09-12 12:21:52 +0800641 if (codec->using_regmap)
642 regcache_mark_dirty(codec->control_data);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000643 break;
644 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200645 dev_dbg(codec->dev,
646 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000647 break;
648 }
649 }
650 }
651
652 for (i = 0; i < card->num_rtd; i++) {
653 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
654
655 if (card->rtd[i].dai_link->ignore_suspend)
656 continue;
657
658 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
659 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200660 }
661
Mark Brown87506542008-11-18 20:50:34 +0000662 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000663 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200664
665 return 0;
666}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000667EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200668
Andy Green6ed25972008-06-13 16:24:05 +0100669/* deferred resume work, so resume can complete before we finished
670 * setting our codec back up, which can be very slow on I2C
671 */
672static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200673{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000674 struct snd_soc_card *card =
675 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200676 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200677 int i;
678
Andy Green6ed25972008-06-13 16:24:05 +0100679 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
680 * so userspace apps are blocked from touching us
681 */
682
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000683 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100684
Mark Brown99497882010-05-07 20:24:05 +0100685 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000686 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100687
Mark Brown87506542008-11-18 20:50:34 +0000688 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000689 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000691 /* resume AC97 DAIs */
692 for (i = 0; i < card->num_rtd; i++) {
693 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100694
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000695 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100696 continue;
697
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000698 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
699 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200700 }
701
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200702 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000703 /* If the CODEC was idle over suspend then it will have been
704 * left with bias OFF or STANDBY and suspended so we must now
705 * resume. Otherwise the suspend was suppressed.
706 */
707 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200708 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000709 case SND_SOC_BIAS_STANDBY:
710 case SND_SOC_BIAS_OFF:
711 codec->driver->resume(codec);
712 codec->suspended = 0;
713 break;
714 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200715 dev_dbg(codec->dev,
716 "ASoC: CODEC was on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000717 break;
718 }
Mark Brown1547aba2010-05-07 21:11:40 +0100719 }
720 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200721
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000722 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100723
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000724 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100725 continue;
726
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800727 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000728 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800729 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000730
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800731 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000732 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800733 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200734 }
735
Mark Brown3ff3f642008-05-19 12:32:25 +0200736 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000737 for (i = 0; i < card->num_rtd; i++) {
738 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
739 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100740
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000741 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100742 continue;
743
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000744 if (drv->ops->digital_mute && dai->playback_active)
745 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200746 }
747
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000748 for (i = 0; i < card->num_rtd; i++) {
749 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
750 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100751
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000752 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100753 continue;
754
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000755 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
756 cpu_dai->driver->resume(cpu_dai);
757 if (platform->driver->resume && platform->suspended) {
758 platform->driver->resume(cpu_dai);
759 platform->suspended = 0;
760 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200761 }
762
Mark Brown87506542008-11-18 20:50:34 +0000763 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000764 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200765
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000766 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100767
768 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000769 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700770
771 /* Recheck all analogue paths too */
772 dapm_mark_io_dirty(&card->dapm);
773 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100774}
775
776/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000777int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100778{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000779 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600780 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200781
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800782 /* If the initialization of this soc device failed, there is no codec
783 * associated with it. Just bail out in this case.
784 */
785 if (list_empty(&card->codec_dev_list))
786 return 0;
787
Mark Brown64ab9ba2009-03-31 11:27:03 +0100788 /* AC97 devices might have other drivers hanging off them so
789 * need to resume immediately. Other drivers don't have that
790 * problem and may take a substantial amount of time to resume
791 * due to I/O costs and anti-pop so handle them out of line.
792 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000793 for (i = 0; i < card->num_rtd; i++) {
794 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600795 ac97_control |= cpu_dai->driver->ac97_control;
796 }
797 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000798 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600799 soc_resume_deferred(&card->deferred_resume_work);
800 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000801 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600802 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000803 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100804 }
Andy Green6ed25972008-06-13 16:24:05 +0100805
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200806 return 0;
807}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000808EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200809#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000810#define snd_soc_suspend NULL
811#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200812#endif
813
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100814static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800815};
816
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000817static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200818{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000819 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
820 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000821 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000822 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000823 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100824 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200825
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000826 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000827
Mark Brownb19e6e72012-03-14 21:18:39 +0000828 /* Find CPU DAI from registered DAIs*/
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000829 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600830 if (dai_link->cpu_of_node &&
831 (cpu_dai->dev->of_node != dai_link->cpu_of_node))
832 continue;
833 if (dai_link->cpu_name &&
834 strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
835 continue;
836 if (dai_link->cpu_dai_name &&
837 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
838 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700839
840 rtd->cpu_dai = cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000841 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000842
843 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000844 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000845 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000846 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000847 }
848
Mark Brownb19e6e72012-03-14 21:18:39 +0000849 /* Find CODEC from registered CODECs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000850 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700851 if (dai_link->codec_of_node) {
852 if (codec->dev->of_node != dai_link->codec_of_node)
853 continue;
854 } else {
855 if (strcmp(codec->name, dai_link->codec_name))
856 continue;
857 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000858
Stephen Warren2610ab72011-12-07 13:58:27 -0700859 rtd->codec = codec;
860
861 /*
862 * CODEC found, so find CODEC DAI from registered DAIs from
863 * this CODEC
864 */
865 list_for_each_entry(codec_dai, &dai_list, list) {
866 if (codec->dev == codec_dai->dev &&
867 !strcmp(codec_dai->name,
868 dai_link->codec_dai_name)) {
869
870 rtd->codec_dai = codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000871 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000872 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000873
874 if (!rtd->codec_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000875 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
Stephen Warren2610ab72011-12-07 13:58:27 -0700876 dai_link->codec_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000877 return -EPROBE_DEFER;
878 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000879 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000880
Mark Brownb19e6e72012-03-14 21:18:39 +0000881 if (!rtd->codec) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000882 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
Mark Brownb19e6e72012-03-14 21:18:39 +0000883 dai_link->codec_name);
884 return -EPROBE_DEFER;
885 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100886
887 /* if there's no platform we match on the empty platform */
888 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700889 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100890 platform_name = "snd-soc-dummy";
891
Mark Brownb19e6e72012-03-14 21:18:39 +0000892 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000893 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700894 if (dai_link->platform_of_node) {
895 if (platform->dev->of_node !=
896 dai_link->platform_of_node)
897 continue;
898 } else {
899 if (strcmp(platform->name, platform_name))
900 continue;
901 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700902
903 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000904 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000905 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000906 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000907 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000908 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000909 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000910
911 card->num_rtd++;
912
913 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000914}
915
Stephen Warrend12cd192012-06-08 12:34:22 -0600916static int soc_remove_platform(struct snd_soc_platform *platform)
917{
918 int ret;
919
920 if (platform->driver->remove) {
921 ret = platform->driver->remove(platform);
922 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000923 dev_err(platform->dev, "ASoC: failed to remove %d\n",
924 ret);
Stephen Warrend12cd192012-06-08 12:34:22 -0600925 }
926
927 /* Make sure all DAPM widgets are freed */
928 snd_soc_dapm_free(&platform->dapm);
929
930 soc_cleanup_platform_debugfs(platform);
931 platform->probed = 0;
932 list_del(&platform->card_list);
933 module_put(platform->dev->driver->owner);
934
935 return 0;
936}
937
Jarkko Nikula589c3562010-12-06 16:27:07 +0200938static void soc_remove_codec(struct snd_soc_codec *codec)
939{
940 int err;
941
942 if (codec->driver->remove) {
943 err = codec->driver->remove(codec);
944 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000945 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200946 }
947
948 /* Make sure all DAPM widgets are freed */
949 snd_soc_dapm_free(&codec->dapm);
950
951 soc_cleanup_codec_debugfs(codec);
952 codec->probed = 0;
953 list_del(&codec->card_list);
954 module_put(codec->dev->driver->owner);
955}
956
Stephen Warren62ae68f2012-06-08 12:34:23 -0600957static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000958{
959 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000960 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
961 int err;
962
963 /* unregister the rtd device */
964 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800965 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
966 device_remove_file(rtd->dev, &dev_attr_codec_reg);
967 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000968 rtd->dev_registered = 0;
969 }
970
971 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100972 if (codec_dai && codec_dai->probed &&
973 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000974 if (codec_dai->driver->remove) {
975 err = codec_dai->driver->remove(codec_dai);
976 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000977 dev_err(codec_dai->dev,
978 "ASoC: failed to remove %s: %d\n",
979 codec_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000980 }
981 codec_dai->probed = 0;
982 list_del(&codec_dai->card_list);
983 }
984
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000985 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100986 if (cpu_dai && cpu_dai->probed &&
987 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000988 if (cpu_dai->driver->remove) {
989 err = cpu_dai->driver->remove(cpu_dai);
990 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000991 dev_err(cpu_dai->dev,
992 "ASoC: failed to remove %s: %d\n",
993 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000994 }
995 cpu_dai->probed = 0;
996 list_del(&cpu_dai->card_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -0600997
Stephen Warren18d75642012-06-08 12:34:21 -0600998 if (!cpu_dai->codec) {
999 snd_soc_dapm_free(&cpu_dai->dapm);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001000 module_put(cpu_dai->dev->driver->owner);
Stephen Warren18d75642012-06-08 12:34:21 -06001001 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001002 }
1003}
1004
Stephen Warren62ae68f2012-06-08 12:34:23 -06001005static void soc_remove_link_components(struct snd_soc_card *card, int num,
1006 int order)
1007{
1008 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1009 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1010 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1011 struct snd_soc_platform *platform = rtd->platform;
1012 struct snd_soc_codec *codec;
1013
1014 /* remove the platform */
1015 if (platform && platform->probed &&
1016 platform->driver->remove_order == order) {
1017 soc_remove_platform(platform);
1018 }
1019
1020 /* remove the CODEC-side CODEC */
1021 if (codec_dai) {
1022 codec = codec_dai->codec;
1023 if (codec && codec->probed &&
1024 codec->driver->remove_order == order)
1025 soc_remove_codec(codec);
1026 }
1027
1028 /* remove any CPU-side CODEC */
1029 if (cpu_dai) {
1030 codec = cpu_dai->codec;
1031 if (codec && codec->probed &&
1032 codec->driver->remove_order == order)
1033 soc_remove_codec(codec);
1034 }
1035}
1036
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001037static void soc_remove_dai_links(struct snd_soc_card *card)
1038{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001039 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001040
Liam Girdwood0168bf02011-06-07 16:08:05 +01001041 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1042 order++) {
1043 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001044 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001045 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001046
1047 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1048 order++) {
1049 for (dai = 0; dai < card->num_rtd; dai++)
1050 soc_remove_link_components(card, dai, order);
1051 }
1052
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001053 card->num_rtd = 0;
1054}
1055
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001056static void soc_set_name_prefix(struct snd_soc_card *card,
1057 struct snd_soc_codec *codec)
1058{
1059 int i;
1060
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001061 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001062 return;
1063
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001064 for (i = 0; i < card->num_configs; i++) {
1065 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001066 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1067 codec->name_prefix = map->name_prefix;
1068 break;
1069 }
1070 }
1071}
1072
Jarkko Nikula589c3562010-12-06 16:27:07 +02001073static int soc_probe_codec(struct snd_soc_card *card,
1074 struct snd_soc_codec *codec)
1075{
1076 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001077 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001078 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001079
1080 codec->card = card;
1081 codec->dapm.card = card;
1082 soc_set_name_prefix(card, codec);
1083
Jarkko Nikula70d29332011-01-27 16:24:22 +02001084 if (!try_module_get(codec->dev->driver->owner))
1085 return -ENODEV;
1086
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001087 soc_init_codec_debugfs(codec);
1088
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001089 if (driver->dapm_widgets)
1090 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1091 driver->num_dapm_widgets);
1092
Mark Brown888df392012-02-16 19:37:51 -08001093 /* Create DAPM widgets for each DAI stream */
1094 list_for_each_entry(dai, &dai_list, list) {
1095 if (dai->dev != codec->dev)
1096 continue;
1097
1098 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1099 }
1100
Mark Brown33c5f962011-08-22 18:40:30 +01001101 codec->dapm.idle_bias_off = driver->idle_bias_off;
1102
Mark Brown89b95ac02011-03-07 16:38:44 +00001103 if (driver->probe) {
1104 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001105 if (ret < 0) {
1106 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001107 "ASoC: failed to probe CODEC %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001108 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001109 }
Chuansheng Liuff541f42012-12-21 18:17:12 +08001110 WARN(codec->dapm.idle_bias_off &&
1111 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001112 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1113 codec->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001114 }
1115
Mark Brown38cbf952012-06-22 13:04:02 +01001116 /* If the driver didn't set I/O up try regmap */
Mark Brown98d30882012-08-01 20:05:47 +01001117 if (!codec->write && dev_get_regmap(codec->dev, NULL))
Mark Brown38cbf952012-06-22 13:04:02 +01001118 snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
1119
Mark Brownb7af1da2011-04-07 19:18:44 +09001120 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001121 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001122 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001123 if (driver->dapm_routes)
1124 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1125 driver->num_dapm_routes);
1126
Jarkko Nikula589c3562010-12-06 16:27:07 +02001127 /* mark codec as probed and add to card codec list */
1128 codec->probed = 1;
1129 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001130 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001131
Jarkko Nikula70d29332011-01-27 16:24:22 +02001132 return 0;
1133
1134err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001135 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001136 module_put(codec->dev->driver->owner);
1137
Jarkko Nikula589c3562010-12-06 16:27:07 +02001138 return ret;
1139}
1140
Liam Girdwood956245e2011-07-01 16:54:08 +01001141static int soc_probe_platform(struct snd_soc_card *card,
1142 struct snd_soc_platform *platform)
1143{
1144 int ret = 0;
1145 const struct snd_soc_platform_driver *driver = platform->driver;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001146 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001147
1148 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001149 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001150
1151 if (!try_module_get(platform->dev->driver->owner))
1152 return -ENODEV;
1153
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001154 soc_init_platform_debugfs(platform);
1155
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001156 if (driver->dapm_widgets)
1157 snd_soc_dapm_new_controls(&platform->dapm,
1158 driver->dapm_widgets, driver->num_dapm_widgets);
1159
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001160 /* Create DAPM widgets for each DAI stream */
1161 list_for_each_entry(dai, &dai_list, list) {
1162 if (dai->dev != platform->dev)
1163 continue;
1164
1165 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1166 }
1167
Stephen Warren3fec6b62012-04-05 12:28:01 -06001168 platform->dapm.idle_bias_off = 1;
1169
Liam Girdwood956245e2011-07-01 16:54:08 +01001170 if (driver->probe) {
1171 ret = driver->probe(platform);
1172 if (ret < 0) {
1173 dev_err(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001174 "ASoC: failed to probe platform %d\n", ret);
Liam Girdwood956245e2011-07-01 16:54:08 +01001175 goto err_probe;
1176 }
1177 }
1178
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001179 if (driver->controls)
1180 snd_soc_add_platform_controls(platform, driver->controls,
1181 driver->num_controls);
1182 if (driver->dapm_routes)
1183 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1184 driver->num_dapm_routes);
1185
Liam Girdwood956245e2011-07-01 16:54:08 +01001186 /* mark platform as probed and add to card platform list */
1187 platform->probed = 1;
1188 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001189 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001190
1191 return 0;
1192
1193err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001194 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001195 module_put(platform->dev->driver->owner);
1196
1197 return ret;
1198}
1199
Mark Brown36ae1a92012-01-06 17:12:45 -08001200static void rtd_release(struct device *dev)
1201{
1202 kfree(dev);
1203}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001204
Jarkko Nikula589c3562010-12-06 16:27:07 +02001205static int soc_post_component_init(struct snd_soc_card *card,
1206 struct snd_soc_codec *codec,
1207 int num, int dailess)
1208{
1209 struct snd_soc_dai_link *dai_link = NULL;
1210 struct snd_soc_aux_dev *aux_dev = NULL;
1211 struct snd_soc_pcm_runtime *rtd;
1212 const char *temp, *name;
1213 int ret = 0;
1214
1215 if (!dailess) {
1216 dai_link = &card->dai_link[num];
1217 rtd = &card->rtd[num];
1218 name = dai_link->name;
1219 } else {
1220 aux_dev = &card->aux_dev[num];
1221 rtd = &card->rtd_aux[num];
1222 name = aux_dev->name;
1223 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001224 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001225
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001226 /* Make sure all DAPM widgets are instantiated */
1227 snd_soc_dapm_new_widgets(&codec->dapm);
1228
Jarkko Nikula589c3562010-12-06 16:27:07 +02001229 /* machine controls, routes and widgets are not prefixed */
1230 temp = codec->name_prefix;
1231 codec->name_prefix = NULL;
1232
1233 /* do machine specific initialization */
1234 if (!dailess && dai_link->init)
1235 ret = dai_link->init(rtd);
1236 else if (dailess && aux_dev->init)
1237 ret = aux_dev->init(&codec->dapm);
1238 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001239 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001240 return ret;
1241 }
1242 codec->name_prefix = temp;
1243
Jarkko Nikula589c3562010-12-06 16:27:07 +02001244 /* register the rtd device */
1245 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001246
1247 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1248 if (!rtd->dev)
1249 return -ENOMEM;
1250 device_initialize(rtd->dev);
1251 rtd->dev->parent = card->dev;
1252 rtd->dev->release = rtd_release;
1253 rtd->dev->init_name = name;
1254 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001255 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001256 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1257 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1258 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1259 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001260 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001261 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001262 /* calling put_device() here to free the rtd->dev */
1263 put_device(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001264 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001265 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001266 return ret;
1267 }
1268 rtd->dev_registered = 1;
1269
1270 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001271 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001272 if (ret < 0)
1273 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001274 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001275
1276 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001277 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001278 if (ret < 0)
1279 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001280 "ASoC: failed to add codec sysfs files: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001281
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001282#ifdef CONFIG_DEBUG_FS
1283 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001284 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001285 goto out;
1286
1287 ret = soc_dpcm_debugfs_add(rtd);
1288 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001289 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001290
1291out:
1292#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001293 return 0;
1294}
1295
Stephen Warren62ae68f2012-06-08 12:34:23 -06001296static int soc_probe_link_components(struct snd_soc_card *card, int num,
1297 int order)
1298{
1299 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1300 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1301 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1302 struct snd_soc_platform *platform = rtd->platform;
1303 int ret;
1304
1305 /* probe the CPU-side component, if it is a CODEC */
1306 if (cpu_dai->codec &&
1307 !cpu_dai->codec->probed &&
1308 cpu_dai->codec->driver->probe_order == order) {
1309 ret = soc_probe_codec(card, cpu_dai->codec);
1310 if (ret < 0)
1311 return ret;
1312 }
1313
1314 /* probe the CODEC-side component */
1315 if (!codec_dai->codec->probed &&
1316 codec_dai->codec->driver->probe_order == order) {
1317 ret = soc_probe_codec(card, codec_dai->codec);
1318 if (ret < 0)
1319 return ret;
1320 }
1321
1322 /* probe the platform */
1323 if (!platform->probed &&
1324 platform->driver->probe_order == order) {
1325 ret = soc_probe_platform(card, platform);
1326 if (ret < 0)
1327 return ret;
1328 }
1329
1330 return 0;
1331}
1332
1333static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001334{
1335 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1336 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1337 struct snd_soc_codec *codec = rtd->codec;
1338 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001339 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1340 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1341 struct snd_soc_dapm_widget *play_w, *capture_w;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001342 int ret;
1343
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001344 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001345 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001346
1347 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001348 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001349 codec_dai->card = card;
1350 cpu_dai->card = card;
1351
1352 /* set default power off timeout */
1353 rtd->pmdown_time = pmdown_time;
1354
1355 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001356 if (!cpu_dai->probed &&
1357 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001358 if (!cpu_dai->codec) {
1359 cpu_dai->dapm.card = card;
1360 if (!try_module_get(cpu_dai->dev->driver->owner))
1361 return -ENODEV;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001362
Stephen Warren18d75642012-06-08 12:34:21 -06001363 list_add(&cpu_dai->dapm.list, &card->dapm_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001364 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1365 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001366
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001367 if (cpu_dai->driver->probe) {
1368 ret = cpu_dai->driver->probe(cpu_dai);
1369 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001370 dev_err(cpu_dai->dev,
1371 "ASoC: failed to probe CPU DAI %s: %d\n",
1372 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001373 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001374 return ret;
1375 }
1376 }
1377 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001378 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001379 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1380 }
1381
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001382 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001383 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001384 if (codec_dai->driver->probe) {
1385 ret = codec_dai->driver->probe(codec_dai);
1386 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001387 dev_err(codec_dai->dev,
1388 "ASoC: failed to probe CODEC DAI %s: %d\n",
1389 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001390 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001391 }
1392 }
1393
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001394 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001395 codec_dai->probed = 1;
1396 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001397 }
1398
Liam Girdwood0168bf02011-06-07 16:08:05 +01001399 /* complete DAI probe during last probe */
1400 if (order != SND_SOC_COMP_ORDER_LAST)
1401 return 0;
1402
Jarkko Nikula589c3562010-12-06 16:27:07 +02001403 ret = soc_post_component_init(card, codec, num, 0);
1404 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001405 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001406
Mark Brown36ae1a92012-01-06 17:12:45 -08001407 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001408 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001409 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1410 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001411
Namarta Kohli1245b702012-08-16 17:10:41 +05301412 if (cpu_dai->driver->compress_dai) {
1413 /*create compress_device"*/
1414 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001415 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001416 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301417 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001418 return ret;
1419 }
1420 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301421
1422 if (!dai_link->params) {
1423 /* create the pcm */
1424 ret = soc_new_pcm(rtd, num);
1425 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001426 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301427 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001428 return ret;
1429 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301430 } else {
1431 /* link the DAI widgets */
1432 play_w = codec_dai->playback_widget;
1433 capture_w = cpu_dai->capture_widget;
1434 if (play_w && capture_w) {
1435 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Mark Brownc74184e2012-04-04 22:12:09 +01001436 capture_w, play_w);
Namarta Kohli1245b702012-08-16 17:10:41 +05301437 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001438 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301439 play_w->name, capture_w->name, ret);
1440 return ret;
1441 }
1442 }
1443
1444 play_w = cpu_dai->playback_widget;
1445 capture_w = codec_dai->capture_widget;
1446 if (play_w && capture_w) {
1447 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1448 capture_w, play_w);
1449 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001450 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301451 play_w->name, capture_w->name, ret);
1452 return ret;
1453 }
Mark Brownc74184e2012-04-04 22:12:09 +01001454 }
1455 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001456 }
1457
1458 /* add platform data for AC97 devices */
1459 if (rtd->codec_dai->driver->ac97_control)
1460 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1461
1462 return 0;
1463}
1464
1465#ifdef CONFIG_SND_SOC_AC97_BUS
1466static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1467{
1468 int ret;
1469
1470 /* Only instantiate AC97 if not already done by the adaptor
1471 * for the generic AC97 subsystem.
1472 */
1473 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001474 /*
1475 * It is possible that the AC97 device is already registered to
1476 * the device subsystem. This happens when the device is created
1477 * via snd_ac97_mixer(). Currently only SoC codec that does so
1478 * is the generic AC97 glue but others migh emerge.
1479 *
1480 * In those cases we don't try to register the device again.
1481 */
1482 if (!rtd->codec->ac97_created)
1483 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001484
1485 ret = soc_ac97_dev_register(rtd->codec);
1486 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001487 dev_err(rtd->codec->dev,
1488 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001489 return ret;
1490 }
1491
1492 rtd->codec->ac97_registered = 1;
1493 }
1494 return 0;
1495}
1496
1497static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1498{
1499 if (codec->ac97_registered) {
1500 soc_ac97_dev_unregister(codec);
1501 codec->ac97_registered = 0;
1502 }
1503}
1504#endif
1505
Mark Brownb19e6e72012-03-14 21:18:39 +00001506static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1507{
1508 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1509 struct snd_soc_codec *codec;
1510
1511 /* find CODEC from registered CODECs*/
1512 list_for_each_entry(codec, &codec_list, list) {
1513 if (!strcmp(codec->name, aux_dev->codec_name))
1514 return 0;
1515 }
1516
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001517 dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
Mark Brownfb099cb2012-08-09 18:44:37 +01001518
Mark Brownb19e6e72012-03-14 21:18:39 +00001519 return -EPROBE_DEFER;
1520}
1521
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001522static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1523{
1524 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001525 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001526 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001527
1528 /* find CODEC from registered CODECs*/
1529 list_for_each_entry(codec, &codec_list, list) {
1530 if (!strcmp(codec->name, aux_dev->codec_name)) {
1531 if (codec->probed) {
1532 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001533 "ASoC: codec already probed");
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001534 ret = -EBUSY;
1535 goto out;
1536 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001537 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001538 }
1539 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001540 /* codec not found */
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001541 dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001542 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001543
Jarkko Nikula676ad982010-12-03 09:18:22 +02001544found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001545 ret = soc_probe_codec(card, codec);
1546 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001547 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001548
Jarkko Nikula589c3562010-12-06 16:27:07 +02001549 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001550
1551out:
1552 return ret;
1553}
1554
1555static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1556{
1557 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1558 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001559
1560 /* unregister the rtd device */
1561 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001562 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001563 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001564 rtd->dev_registered = 0;
1565 }
1566
Jarkko Nikula589c3562010-12-06 16:27:07 +02001567 if (codec && codec->probed)
1568 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001569}
1570
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001571static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1572 enum snd_soc_compress_type compress_type)
1573{
1574 int ret;
1575
1576 if (codec->cache_init)
1577 return 0;
1578
1579 /* override the compress_type if necessary */
1580 if (compress_type && codec->compress_type != compress_type)
1581 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001582 ret = snd_soc_cache_init(codec);
1583 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001584 dev_err(codec->dev,
1585 "ASoC: Failed to set cache compression type: %d\n",
1586 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) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001642 dev_err(card->dev,
1643 "ASoC: can't create sound card for card %s: %d\n",
1644 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001645 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001646 }
1647 card->snd_card->dev = card->dev;
1648
Mark Browne37a4972011-03-02 18:21:57 +00001649 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1650 card->dapm.dev = card->dev;
1651 card->dapm.card = card;
1652 list_add(&card->dapm.list, &card->dapm_list);
1653
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001654#ifdef CONFIG_DEBUG_FS
1655 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1656#endif
1657
Mark Brown88ee1c62011-02-02 10:43:26 +00001658#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001659 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001660 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001661#endif
Andy Green6ed25972008-06-13 16:24:05 +01001662
Mark Brown9a841eb2011-04-12 17:51:37 -07001663 if (card->dapm_widgets)
1664 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1665 card->num_dapm_widgets);
1666
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001667 /* initialise the sound card only once */
1668 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001669 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001670 if (ret < 0)
1671 goto card_probe_error;
1672 }
1673
Stephen Warren62ae68f2012-06-08 12:34:23 -06001674 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001675 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1676 order++) {
1677 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001678 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001679 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001680 dev_err(card->dev,
1681 "ASoC: failed to instantiate card %d\n",
1682 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001683 goto probe_dai_err;
1684 }
1685 }
1686 }
1687
1688 /* probe all DAI links on this card */
1689 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1690 order++) {
1691 for (i = 0; i < card->num_links; i++) {
1692 ret = soc_probe_link_dais(card, i, order);
1693 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001694 dev_err(card->dev,
1695 "ASoC: failed to instantiate card %d\n",
1696 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001697 goto probe_dai_err;
1698 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001699 }
1700 }
1701
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001702 for (i = 0; i < card->num_aux_devs; i++) {
1703 ret = soc_probe_aux_dev(card, i);
1704 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001705 dev_err(card->dev,
1706 "ASoC: failed to add auxiliary devices %d\n",
1707 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001708 goto probe_aux_dev_err;
1709 }
1710 }
1711
Mark Brown888df392012-02-16 19:37:51 -08001712 snd_soc_dapm_link_dai_widgets(card);
1713
Mark Brownb7af1da2011-04-07 19:18:44 +09001714 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001715 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001716
Mark Brownb8ad29d2011-03-02 18:35:51 +00001717 if (card->dapm_routes)
1718 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1719 card->num_dapm_routes);
1720
Mark Brownb90d2f92011-10-10 13:38:06 +01001721 snd_soc_dapm_new_widgets(&card->dapm);
1722
Mark Brown75d9ac42011-09-27 16:41:01 +01001723 for (i = 0; i < card->num_links; i++) {
1724 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001725 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001726
Mark Brownf04209a2012-04-09 16:29:21 +01001727 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001728 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001729 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001730 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001731 dev_warn(card->rtd[i].codec_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001732 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001733 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001734 }
1735
1736 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001737 if (dai_fmt &&
1738 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001739 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1740 dai_fmt);
1741 if (ret != 0 && ret != -ENOTSUPP)
1742 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001743 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001744 ret);
1745 } else if (dai_fmt) {
1746 /* Flip the polarity for the "CPU" end */
1747 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1748 switch (dai_link->dai_fmt &
1749 SND_SOC_DAIFMT_MASTER_MASK) {
1750 case SND_SOC_DAIFMT_CBM_CFM:
1751 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1752 break;
1753 case SND_SOC_DAIFMT_CBM_CFS:
1754 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1755 break;
1756 case SND_SOC_DAIFMT_CBS_CFM:
1757 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1758 break;
1759 case SND_SOC_DAIFMT_CBS_CFS:
1760 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1761 break;
1762 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001763
1764 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001765 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001766 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001767 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001768 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001769 ret);
1770 }
1771 }
1772
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001773 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001774 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001775 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1776 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001777 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1778 "%s", card->driver_name ? card->driver_name : card->name);
1779 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1780 switch (card->snd_card->driver[i]) {
1781 case '_':
1782 case '-':
1783 case '\0':
1784 break;
1785 default:
1786 if (!isalnum(card->snd_card->driver[i]))
1787 card->snd_card->driver[i] = '_';
1788 break;
1789 }
1790 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001791
Mark Brown28e9ad92011-03-02 18:36:34 +00001792 if (card->late_probe) {
1793 ret = card->late_probe(card);
1794 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001795 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001796 card->name, ret);
1797 goto probe_aux_dev_err;
1798 }
1799 }
1800
Mark Brown2dc00212011-10-08 13:59:44 +01001801 snd_soc_dapm_new_widgets(&card->dapm);
1802
Stephen Warren16332812011-11-23 12:42:04 -07001803 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001804 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001805 snd_soc_dapm_auto_nc_codec_pins(codec);
1806
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001807 ret = snd_card_register(card->snd_card);
1808 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001809 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1810 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001811 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001812 }
1813
1814#ifdef CONFIG_SND_SOC_AC97_BUS
1815 /* register any AC97 codecs */
1816 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001817 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1818 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001819 dev_err(card->dev,
1820 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001821 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001822 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001823 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001824 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001825 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001826#endif
1827
Mark Brown435c5e22008-12-04 15:32:53 +00001828 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001829 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001830 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001831
1832 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001833
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001834probe_aux_dev_err:
1835 for (i = 0; i < card->num_aux_devs; i++)
1836 soc_remove_aux_dev(card, i);
1837
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001838probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001839 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001840
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001841card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001842 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001843 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001844
1845 snd_card_free(card->snd_card);
1846
Mark Brownb19e6e72012-03-14 21:18:39 +00001847base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001848 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001849
Mark Brownb19e6e72012-03-14 21:18:39 +00001850 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001851}
1852
1853/* probes a new socdev */
1854static int soc_probe(struct platform_device *pdev)
1855{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001856 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001857
Vinod Koul70a7ca32011-01-14 19:22:48 +05301858 /*
1859 * no card, so machine driver should be registering card
1860 * we should not be here in that case so ret error
1861 */
1862 if (!card)
1863 return -EINVAL;
1864
Mark Brownfe4085e2012-03-02 13:07:41 +00001865 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001866 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001867 card->name);
1868
Mark Brown435c5e22008-12-04 15:32:53 +00001869 /* Bodge while we unpick instantiation */
1870 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001871
Mark Brown28d528c2012-08-09 18:45:23 +01001872 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001873}
1874
Vinod Koulb0e26482011-01-13 22:48:02 +05301875static int soc_cleanup_card_resources(struct snd_soc_card *card)
1876{
Vinod Koulb0e26482011-01-13 22:48:02 +05301877 int i;
1878
1879 /* make sure any delayed work runs */
1880 for (i = 0; i < card->num_rtd; i++) {
1881 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001882 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301883 }
1884
1885 /* remove auxiliary devices */
1886 for (i = 0; i < card->num_aux_devs; i++)
1887 soc_remove_aux_dev(card, i);
1888
1889 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001890 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301891
1892 soc_cleanup_card_debugfs(card);
1893
1894 /* remove the card */
1895 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001896 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301897
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001898 snd_soc_dapm_free(&card->dapm);
1899
Vinod Koulb0e26482011-01-13 22:48:02 +05301900 snd_card_free(card->snd_card);
1901 return 0;
1902
1903}
1904
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001905/* removes a socdev */
1906static int soc_remove(struct platform_device *pdev)
1907{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001908 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001909
Mark Brownc5af3a22008-11-28 13:29:45 +00001910 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001911 return 0;
1912}
1913
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001914int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001915{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001916 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001917 int i;
Mark Brown51737472009-06-22 13:16:51 +01001918
1919 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001920 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001921
1922 /* Flush out pmdown_time work - we actually do want to run it
1923 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001924 for (i = 0; i < card->num_rtd; i++) {
1925 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001926 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001927 }
Mark Brown51737472009-06-22 13:16:51 +01001928
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001929 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001930
1931 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001932}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001933EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001934
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001935const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301936 .suspend = snd_soc_suspend,
1937 .resume = snd_soc_resume,
1938 .freeze = snd_soc_suspend,
1939 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001940 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301941 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001942};
Stephen Warrendeb26072011-04-05 19:35:30 -06001943EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001944
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001945/* ASoC platform driver */
1946static struct platform_driver soc_driver = {
1947 .driver = {
1948 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001949 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001950 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001951 },
1952 .probe = soc_probe,
1953 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001954};
1955
Mark Brown096e49d2009-07-05 15:12:22 +01001956/**
1957 * snd_soc_codec_volatile_register: Report if a register is volatile.
1958 *
1959 * @codec: CODEC to query.
1960 * @reg: Register to query.
1961 *
1962 * Boolean function indiciating if a CODEC register is volatile.
1963 */
Mark Brown181e0552011-01-24 14:05:25 +00001964int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1965 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001966{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001967 if (codec->volatile_register)
1968 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001969 else
1970 return 0;
1971}
1972EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1973
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001974/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001975 * snd_soc_codec_readable_register: Report if a register is readable.
1976 *
1977 * @codec: CODEC to query.
1978 * @reg: Register to query.
1979 *
1980 * Boolean function indicating if a CODEC register is readable.
1981 */
1982int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1983 unsigned int reg)
1984{
1985 if (codec->readable_register)
1986 return codec->readable_register(codec, reg);
1987 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001988 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001989}
1990EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1991
1992/**
1993 * snd_soc_codec_writable_register: Report if a register is writable.
1994 *
1995 * @codec: CODEC to query.
1996 * @reg: Register to query.
1997 *
1998 * Boolean function indicating if a CODEC register is writable.
1999 */
2000int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
2001 unsigned int reg)
2002{
2003 if (codec->writable_register)
2004 return codec->writable_register(codec, reg);
2005 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02002006 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002007}
2008EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2009
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002010int snd_soc_platform_read(struct snd_soc_platform *platform,
2011 unsigned int reg)
2012{
2013 unsigned int ret;
2014
2015 if (!platform->driver->read) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002016 dev_err(platform->dev, "ASoC: platform has no read back\n");
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002017 return -1;
2018 }
2019
2020 ret = platform->driver->read(platform, reg);
2021 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002022 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002023
2024 return ret;
2025}
2026EXPORT_SYMBOL_GPL(snd_soc_platform_read);
2027
2028int snd_soc_platform_write(struct snd_soc_platform *platform,
2029 unsigned int reg, unsigned int val)
2030{
2031 if (!platform->driver->write) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002032 dev_err(platform->dev, "ASoC: platform has no write back\n");
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002033 return -1;
2034 }
2035
2036 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002037 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002038 return platform->driver->write(platform, reg, val);
2039}
2040EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2041
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002042/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002043 * snd_soc_new_ac97_codec - initailise AC97 device
2044 * @codec: audio codec
2045 * @ops: AC97 bus operations
2046 * @num: AC97 codec number
2047 *
2048 * Initialises AC97 codec resources for use by ad-hoc devices only.
2049 */
2050int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2051 struct snd_ac97_bus_ops *ops, int num)
2052{
2053 mutex_lock(&codec->mutex);
2054
2055 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2056 if (codec->ac97 == NULL) {
2057 mutex_unlock(&codec->mutex);
2058 return -ENOMEM;
2059 }
2060
2061 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2062 if (codec->ac97->bus == NULL) {
2063 kfree(codec->ac97);
2064 codec->ac97 = NULL;
2065 mutex_unlock(&codec->mutex);
2066 return -ENOMEM;
2067 }
2068
2069 codec->ac97->bus->ops = ops;
2070 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002071
2072 /*
2073 * Mark the AC97 device to be created by us. This way we ensure that the
2074 * device will be registered with the device subsystem later on.
2075 */
2076 codec->ac97_created = 1;
2077
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002078 mutex_unlock(&codec->mutex);
2079 return 0;
2080}
2081EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2082
Mark Brownb047e1c2013-06-26 12:45:59 +01002083struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002084EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002085
2086int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2087{
2088 if (ops == soc_ac97_ops)
2089 return 0;
2090
2091 if (soc_ac97_ops && ops)
2092 return -EBUSY;
2093
2094 soc_ac97_ops = ops;
2095
2096 return 0;
2097}
2098EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2099
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002100/**
2101 * snd_soc_free_ac97_codec - free AC97 codec device
2102 * @codec: audio codec
2103 *
2104 * Frees AC97 codec device resources.
2105 */
2106void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2107{
2108 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002109#ifdef CONFIG_SND_SOC_AC97_BUS
2110 soc_unregister_ac97_dai_link(codec);
2111#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002112 kfree(codec->ac97->bus);
2113 kfree(codec->ac97);
2114 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002115 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002116 mutex_unlock(&codec->mutex);
2117}
2118EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2119
Mark Brownc3753702010-11-01 15:41:57 -04002120unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2121{
2122 unsigned int ret;
2123
Mark Brownc3acec22010-12-02 16:15:29 +00002124 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002125 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002126 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002127
2128 return ret;
2129}
2130EXPORT_SYMBOL_GPL(snd_soc_read);
2131
2132unsigned int snd_soc_write(struct snd_soc_codec *codec,
2133 unsigned int reg, unsigned int val)
2134{
2135 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002136 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002137 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002138}
2139EXPORT_SYMBOL_GPL(snd_soc_write);
2140
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00002141unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2142 unsigned int reg, const void *data, size_t len)
2143{
2144 return codec->bulk_write_raw(codec, reg, data, len);
2145}
2146EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2147
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002148/**
2149 * snd_soc_update_bits - update codec register bits
2150 * @codec: audio codec
2151 * @reg: codec register
2152 * @mask: register mask
2153 * @value: new value
2154 *
2155 * Writes new register value.
2156 *
Timur Tabi180c3292011-01-10 15:58:13 -06002157 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002158 */
2159int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002160 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002161{
Mark Brown8a713da2011-12-03 12:33:55 +00002162 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002163 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002164 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002165
Mark Brown8a713da2011-12-03 12:33:55 +00002166 if (codec->using_regmap) {
2167 ret = regmap_update_bits_check(codec->control_data, reg,
2168 mask, value, &change);
2169 } else {
2170 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06002171 if (ret < 0)
2172 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00002173
2174 old = ret;
2175 new = (old & ~mask) | (value & mask);
2176 change = old != new;
2177 if (change)
2178 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06002179 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002180
Mark Brown8a713da2011-12-03 12:33:55 +00002181 if (ret < 0)
2182 return ret;
2183
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002184 return change;
2185}
2186EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2187
2188/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002189 * snd_soc_update_bits_locked - update codec register bits
2190 * @codec: audio codec
2191 * @reg: codec register
2192 * @mask: register mask
2193 * @value: new value
2194 *
2195 * Writes new register value, and takes the codec mutex.
2196 *
2197 * Returns 1 for change else 0.
2198 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002199int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2200 unsigned short reg, unsigned int mask,
2201 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002202{
2203 int change;
2204
2205 mutex_lock(&codec->mutex);
2206 change = snd_soc_update_bits(codec, reg, mask, value);
2207 mutex_unlock(&codec->mutex);
2208
2209 return change;
2210}
Mark Browndd1b3d52009-12-04 14:22:03 +00002211EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002212
2213/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002214 * snd_soc_test_bits - test register for change
2215 * @codec: audio codec
2216 * @reg: codec register
2217 * @mask: register mask
2218 * @value: new value
2219 *
2220 * Tests a register with a new value and checks if the new value is
2221 * different from the old value.
2222 *
2223 * Returns 1 for change else 0.
2224 */
2225int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002226 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002227{
2228 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002229 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002230
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002231 old = snd_soc_read(codec, reg);
2232 new = (old & ~mask) | value;
2233 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002234
2235 return change;
2236}
2237EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2238
2239/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002240 * snd_soc_cnew - create new control
2241 * @_template: control template
2242 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002243 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002244 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002245 *
2246 * Create a new mixer control from a template control.
2247 *
2248 * Returns 0 for success, else error.
2249 */
2250struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002251 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002252 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002253{
2254 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002255 struct snd_kcontrol *kcontrol;
2256 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002257
2258 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002259 template.index = 0;
2260
Mark Brownefb7ac32011-03-08 17:23:24 +00002261 if (!long_name)
2262 long_name = template.name;
2263
2264 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002265 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002266 if (!name)
2267 return NULL;
2268
Mark Brownefb7ac32011-03-08 17:23:24 +00002269 template.name = name;
2270 } else {
2271 template.name = long_name;
2272 }
2273
2274 kcontrol = snd_ctl_new1(&template, data);
2275
2276 kfree(name);
2277
2278 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002279}
2280EXPORT_SYMBOL_GPL(snd_soc_cnew);
2281
Liam Girdwood022658b2012-02-03 17:43:09 +00002282static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2283 const struct snd_kcontrol_new *controls, int num_controls,
2284 const char *prefix, void *data)
2285{
2286 int err, i;
2287
2288 for (i = 0; i < num_controls; i++) {
2289 const struct snd_kcontrol_new *control = &controls[i];
2290 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2291 control->name, prefix));
2292 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002293 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2294 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002295 return err;
2296 }
2297 }
2298
2299 return 0;
2300}
2301
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002302struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2303 const char *name)
2304{
2305 struct snd_card *card = soc_card->snd_card;
2306 struct snd_kcontrol *kctl;
2307
2308 if (unlikely(!name))
2309 return NULL;
2310
2311 list_for_each_entry(kctl, &card->controls, list)
2312 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2313 return kctl;
2314 return NULL;
2315}
2316EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2317
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002318/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002319 * snd_soc_add_codec_controls - add an array of controls to a codec.
2320 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002321 * duplicating this code.
2322 *
2323 * @codec: codec to add controls to
2324 * @controls: array of controls to add
2325 * @num_controls: number of elements in the array
2326 *
2327 * Return 0 for success, else error.
2328 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002329int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002330 const struct snd_kcontrol_new *controls, int num_controls)
2331{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002332 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002333
Liam Girdwood022658b2012-02-03 17:43:09 +00002334 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2335 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002336}
Liam Girdwood022658b2012-02-03 17:43:09 +00002337EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002338
2339/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002340 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002341 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002342 *
2343 * @platform: platform to add controls to
2344 * @controls: array of controls to add
2345 * @num_controls: number of elements in the array
2346 *
2347 * Return 0 for success, else error.
2348 */
2349int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2350 const struct snd_kcontrol_new *controls, int num_controls)
2351{
2352 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002353
Liam Girdwood022658b2012-02-03 17:43:09 +00002354 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2355 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002356}
2357EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2358
2359/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002360 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2361 * Convenience function to add a list of controls.
2362 *
2363 * @soc_card: SoC card to add controls to
2364 * @controls: array of controls to add
2365 * @num_controls: number of elements in the array
2366 *
2367 * Return 0 for success, else error.
2368 */
2369int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2370 const struct snd_kcontrol_new *controls, int num_controls)
2371{
2372 struct snd_card *card = soc_card->snd_card;
2373
2374 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2375 NULL, soc_card);
2376}
2377EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2378
2379/**
2380 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2381 * Convienience function to add a list of controls.
2382 *
2383 * @dai: DAI to add controls to
2384 * @controls: array of controls to add
2385 * @num_controls: number of elements in the array
2386 *
2387 * Return 0 for success, else error.
2388 */
2389int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2390 const struct snd_kcontrol_new *controls, int num_controls)
2391{
2392 struct snd_card *card = dai->card->snd_card;
2393
2394 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2395 NULL, dai);
2396}
2397EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2398
2399/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002400 * snd_soc_info_enum_double - enumerated double mixer info callback
2401 * @kcontrol: mixer control
2402 * @uinfo: control element information
2403 *
2404 * Callback to provide information about a double enumerated
2405 * mixer control.
2406 *
2407 * Returns 0 for success.
2408 */
2409int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2410 struct snd_ctl_elem_info *uinfo)
2411{
2412 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2413
2414 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2415 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002416 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002417
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002418 if (uinfo->value.enumerated.item > e->max - 1)
2419 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002420 strcpy(uinfo->value.enumerated.name,
2421 e->texts[uinfo->value.enumerated.item]);
2422 return 0;
2423}
2424EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2425
2426/**
2427 * snd_soc_get_enum_double - enumerated double mixer get callback
2428 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002429 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002430 *
2431 * Callback to get the value of a double enumerated mixer.
2432 *
2433 * Returns 0 for success.
2434 */
2435int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2436 struct snd_ctl_elem_value *ucontrol)
2437{
2438 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2439 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002440 unsigned int val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002441
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002442 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002443 ucontrol->value.enumerated.item[0]
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002444 = (val >> e->shift_l) & e->mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002445 if (e->shift_l != e->shift_r)
2446 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002447 (val >> e->shift_r) & e->mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002448
2449 return 0;
2450}
2451EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2452
2453/**
2454 * snd_soc_put_enum_double - enumerated double mixer put callback
2455 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002456 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002457 *
2458 * Callback to set the value of a double enumerated mixer.
2459 *
2460 * Returns 0 for success.
2461 */
2462int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2463 struct snd_ctl_elem_value *ucontrol)
2464{
2465 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2466 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002467 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002468 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002469
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002470 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002471 return -EINVAL;
2472 val = ucontrol->value.enumerated.item[0] << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002473 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002474 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002475 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002476 return -EINVAL;
2477 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002478 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002479 }
2480
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002481 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002482}
2483EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2484
2485/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002486 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2487 * @kcontrol: mixer control
2488 * @ucontrol: control element information
2489 *
2490 * Callback to get the value of a double semi enumerated mixer.
2491 *
2492 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2493 * used for handling bitfield coded enumeration for example.
2494 *
2495 * Returns 0 for success.
2496 */
2497int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2498 struct snd_ctl_elem_value *ucontrol)
2499{
2500 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002501 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002502 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002503
2504 reg_val = snd_soc_read(codec, e->reg);
2505 val = (reg_val >> e->shift_l) & e->mask;
2506 for (mux = 0; mux < e->max; mux++) {
2507 if (val == e->values[mux])
2508 break;
2509 }
2510 ucontrol->value.enumerated.item[0] = mux;
2511 if (e->shift_l != e->shift_r) {
2512 val = (reg_val >> e->shift_r) & e->mask;
2513 for (mux = 0; mux < e->max; mux++) {
2514 if (val == e->values[mux])
2515 break;
2516 }
2517 ucontrol->value.enumerated.item[1] = mux;
2518 }
2519
2520 return 0;
2521}
2522EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2523
2524/**
2525 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2526 * @kcontrol: mixer control
2527 * @ucontrol: control element information
2528 *
2529 * Callback to set the value of a double semi enumerated mixer.
2530 *
2531 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2532 * used for handling bitfield coded enumeration for example.
2533 *
2534 * Returns 0 for success.
2535 */
2536int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2537 struct snd_ctl_elem_value *ucontrol)
2538{
2539 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002540 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002541 unsigned int val;
2542 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002543
2544 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2545 return -EINVAL;
2546 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2547 mask = e->mask << e->shift_l;
2548 if (e->shift_l != e->shift_r) {
2549 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2550 return -EINVAL;
2551 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2552 mask |= e->mask << e->shift_r;
2553 }
2554
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002555 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002556}
2557EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2558
2559/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002560 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2561 * @kcontrol: mixer control
2562 * @uinfo: control element information
2563 *
2564 * Callback to provide information about an external enumerated
2565 * single mixer.
2566 *
2567 * Returns 0 for success.
2568 */
2569int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2570 struct snd_ctl_elem_info *uinfo)
2571{
2572 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2573
2574 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2575 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002576 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002577
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002578 if (uinfo->value.enumerated.item > e->max - 1)
2579 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002580 strcpy(uinfo->value.enumerated.name,
2581 e->texts[uinfo->value.enumerated.item]);
2582 return 0;
2583}
2584EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2585
2586/**
2587 * snd_soc_info_volsw_ext - external single mixer info callback
2588 * @kcontrol: mixer control
2589 * @uinfo: control element information
2590 *
2591 * Callback to provide information about a single external mixer control.
2592 *
2593 * Returns 0 for success.
2594 */
2595int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2596 struct snd_ctl_elem_info *uinfo)
2597{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002598 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002599
Mark Brownfd5dfad2009-04-15 21:37:46 +01002600 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002601 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2602 else
2603 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2604
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002605 uinfo->count = 1;
2606 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002607 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002608 return 0;
2609}
2610EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2611
2612/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002613 * snd_soc_info_volsw - single mixer info callback
2614 * @kcontrol: mixer control
2615 * @uinfo: control element information
2616 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002617 * Callback to provide information about a single mixer control, or a double
2618 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002619 *
2620 * Returns 0 for success.
2621 */
2622int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2623 struct snd_ctl_elem_info *uinfo)
2624{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002625 struct soc_mixer_control *mc =
2626 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002627 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002628
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002629 if (!mc->platform_max)
2630 mc->platform_max = mc->max;
2631 platform_max = mc->platform_max;
2632
2633 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002634 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2635 else
2636 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2637
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002638 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002639 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002640 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002641 return 0;
2642}
2643EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2644
2645/**
2646 * snd_soc_get_volsw - single mixer get callback
2647 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002648 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002649 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002650 * Callback to get the value of a single mixer control, or a double mixer
2651 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002652 *
2653 * Returns 0 for success.
2654 */
2655int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2656 struct snd_ctl_elem_value *ucontrol)
2657{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002658 struct soc_mixer_control *mc =
2659 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002660 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002661 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002662 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002663 unsigned int shift = mc->shift;
2664 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002665 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002666 unsigned int mask = (1 << fls(max)) - 1;
2667 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002668
2669 ucontrol->value.integer.value[0] =
2670 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002671 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002672 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002673 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002674
2675 if (snd_soc_volsw_is_stereo(mc)) {
2676 if (reg == reg2)
2677 ucontrol->value.integer.value[1] =
2678 (snd_soc_read(codec, reg) >> rshift) & mask;
2679 else
2680 ucontrol->value.integer.value[1] =
2681 (snd_soc_read(codec, reg2) >> shift) & mask;
2682 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002683 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002684 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002685 }
2686
2687 return 0;
2688}
2689EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2690
2691/**
2692 * snd_soc_put_volsw - single mixer put callback
2693 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002694 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002695 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002696 * Callback to set the value of a single mixer control, or a double mixer
2697 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002698 *
2699 * Returns 0 for success.
2700 */
2701int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2702 struct snd_ctl_elem_value *ucontrol)
2703{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002704 struct soc_mixer_control *mc =
2705 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002706 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002707 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002708 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002709 unsigned int shift = mc->shift;
2710 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002711 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002712 unsigned int mask = (1 << fls(max)) - 1;
2713 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002714 int err;
2715 bool type_2r = 0;
2716 unsigned int val2 = 0;
2717 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002718
2719 val = (ucontrol->value.integer.value[0] & mask);
2720 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002721 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002722 val_mask = mask << shift;
2723 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002724 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002725 val2 = (ucontrol->value.integer.value[1] & mask);
2726 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002727 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002728 if (reg == reg2) {
2729 val_mask |= mask << rshift;
2730 val |= val2 << rshift;
2731 } else {
2732 val2 = val2 << shift;
2733 type_2r = 1;
2734 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002735 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002736 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002737 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002738 return err;
2739
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002740 if (type_2r)
2741 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2742
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002743 return err;
2744}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002745EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002746
Mark Browne13ac2e2008-05-28 17:58:05 +01002747/**
Brian Austin1d99f242012-03-30 10:43:55 -05002748 * snd_soc_get_volsw_sx - single mixer get callback
2749 * @kcontrol: mixer control
2750 * @ucontrol: control element information
2751 *
2752 * Callback to get the value of a single mixer control, or a double mixer
2753 * control that spans 2 registers.
2754 *
2755 * Returns 0 for success.
2756 */
2757int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2758 struct snd_ctl_elem_value *ucontrol)
2759{
2760 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2761 struct soc_mixer_control *mc =
2762 (struct soc_mixer_control *)kcontrol->private_value;
2763
2764 unsigned int reg = mc->reg;
2765 unsigned int reg2 = mc->rreg;
2766 unsigned int shift = mc->shift;
2767 unsigned int rshift = mc->rshift;
2768 int max = mc->max;
2769 int min = mc->min;
2770 int mask = (1 << (fls(min + max) - 1)) - 1;
2771
2772 ucontrol->value.integer.value[0] =
2773 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2774
2775 if (snd_soc_volsw_is_stereo(mc))
2776 ucontrol->value.integer.value[1] =
2777 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2778
2779 return 0;
2780}
2781EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2782
2783/**
2784 * snd_soc_put_volsw_sx - double mixer set callback
2785 * @kcontrol: mixer control
2786 * @uinfo: control element information
2787 *
2788 * Callback to set the value of a double mixer control that spans 2 registers.
2789 *
2790 * Returns 0 for success.
2791 */
2792int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2793 struct snd_ctl_elem_value *ucontrol)
2794{
2795 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2796 struct soc_mixer_control *mc =
2797 (struct soc_mixer_control *)kcontrol->private_value;
2798
2799 unsigned int reg = mc->reg;
2800 unsigned int reg2 = mc->rreg;
2801 unsigned int shift = mc->shift;
2802 unsigned int rshift = mc->rshift;
2803 int max = mc->max;
2804 int min = mc->min;
2805 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002806 int err = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002807 unsigned short val, val_mask, val2 = 0;
2808
2809 val_mask = mask << shift;
2810 val = (ucontrol->value.integer.value[0] + min) & mask;
2811 val = val << shift;
2812
Mukund Navadad0558522012-11-09 11:53:40 +05302813 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2814 if (err < 0)
2815 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002816
2817 if (snd_soc_volsw_is_stereo(mc)) {
2818 val_mask = mask << rshift;
2819 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2820 val2 = val2 << rshift;
2821
2822 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2823 return err;
2824 }
2825 return 0;
2826}
2827EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2828
2829/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002830 * snd_soc_info_volsw_s8 - signed mixer info callback
2831 * @kcontrol: mixer control
2832 * @uinfo: control element information
2833 *
2834 * Callback to provide information about a signed mixer control.
2835 *
2836 * Returns 0 for success.
2837 */
2838int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2839 struct snd_ctl_elem_info *uinfo)
2840{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002841 struct soc_mixer_control *mc =
2842 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002843 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002844 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002845
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002846 if (!mc->platform_max)
2847 mc->platform_max = mc->max;
2848 platform_max = mc->platform_max;
2849
Mark Browne13ac2e2008-05-28 17:58:05 +01002850 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2851 uinfo->count = 2;
2852 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002853 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002854 return 0;
2855}
2856EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2857
2858/**
2859 * snd_soc_get_volsw_s8 - signed mixer get callback
2860 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002861 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002862 *
2863 * Callback to get the value of a signed mixer control.
2864 *
2865 * Returns 0 for success.
2866 */
2867int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2868 struct snd_ctl_elem_value *ucontrol)
2869{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002870 struct soc_mixer_control *mc =
2871 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002872 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002873 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002874 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002875 int val = snd_soc_read(codec, reg);
2876
2877 ucontrol->value.integer.value[0] =
2878 ((signed char)(val & 0xff))-min;
2879 ucontrol->value.integer.value[1] =
2880 ((signed char)((val >> 8) & 0xff))-min;
2881 return 0;
2882}
2883EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2884
2885/**
2886 * snd_soc_put_volsw_sgn - signed mixer put callback
2887 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002888 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002889 *
2890 * Callback to set the value of a signed mixer control.
2891 *
2892 * Returns 0 for success.
2893 */
2894int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2895 struct snd_ctl_elem_value *ucontrol)
2896{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002897 struct soc_mixer_control *mc =
2898 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002899 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002900 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002901 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002902 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002903
2904 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2905 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2906
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002907 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002908}
2909EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2910
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002911/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002912 * snd_soc_info_volsw_range - single mixer info callback with range.
2913 * @kcontrol: mixer control
2914 * @uinfo: control element information
2915 *
2916 * Callback to provide information, within a range, about a single
2917 * mixer control.
2918 *
2919 * returns 0 for success.
2920 */
2921int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2922 struct snd_ctl_elem_info *uinfo)
2923{
2924 struct soc_mixer_control *mc =
2925 (struct soc_mixer_control *)kcontrol->private_value;
2926 int platform_max;
2927 int min = mc->min;
2928
2929 if (!mc->platform_max)
2930 mc->platform_max = mc->max;
2931 platform_max = mc->platform_max;
2932
2933 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002934 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002935 uinfo->value.integer.min = 0;
2936 uinfo->value.integer.max = platform_max - min;
2937
2938 return 0;
2939}
2940EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2941
2942/**
2943 * snd_soc_put_volsw_range - single mixer put value callback with range.
2944 * @kcontrol: mixer control
2945 * @ucontrol: control element information
2946 *
2947 * Callback to set the value, within a range, for a single mixer control.
2948 *
2949 * Returns 0 for success.
2950 */
2951int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2952 struct snd_ctl_elem_value *ucontrol)
2953{
2954 struct soc_mixer_control *mc =
2955 (struct soc_mixer_control *)kcontrol->private_value;
2956 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2957 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002958 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002959 unsigned int shift = mc->shift;
2960 int min = mc->min;
2961 int max = mc->max;
2962 unsigned int mask = (1 << fls(max)) - 1;
2963 unsigned int invert = mc->invert;
2964 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002965 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002966
2967 val = ((ucontrol->value.integer.value[0] + min) & mask);
2968 if (invert)
2969 val = max - val;
2970 val_mask = mask << shift;
2971 val = val << shift;
2972
Mark Brown9bde4f02012-12-19 16:05:00 +00002973 ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09002974 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00002975 return ret;
2976
2977 if (snd_soc_volsw_is_stereo(mc)) {
2978 val = ((ucontrol->value.integer.value[1] + min) & mask);
2979 if (invert)
2980 val = max - val;
2981 val_mask = mask << shift;
2982 val = val << shift;
2983
2984 ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
2985 }
2986
2987 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002988}
2989EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2990
2991/**
2992 * snd_soc_get_volsw_range - single mixer get callback with range
2993 * @kcontrol: mixer control
2994 * @ucontrol: control element information
2995 *
2996 * Callback to get the value, within a range, of a single mixer control.
2997 *
2998 * Returns 0 for success.
2999 */
3000int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
3001 struct snd_ctl_elem_value *ucontrol)
3002{
3003 struct soc_mixer_control *mc =
3004 (struct soc_mixer_control *)kcontrol->private_value;
3005 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3006 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00003007 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003008 unsigned int shift = mc->shift;
3009 int min = mc->min;
3010 int max = mc->max;
3011 unsigned int mask = (1 << fls(max)) - 1;
3012 unsigned int invert = mc->invert;
3013
3014 ucontrol->value.integer.value[0] =
3015 (snd_soc_read(codec, reg) >> shift) & mask;
3016 if (invert)
3017 ucontrol->value.integer.value[0] =
3018 max - ucontrol->value.integer.value[0];
3019 ucontrol->value.integer.value[0] =
3020 ucontrol->value.integer.value[0] - min;
3021
Mark Brown9bde4f02012-12-19 16:05:00 +00003022 if (snd_soc_volsw_is_stereo(mc)) {
3023 ucontrol->value.integer.value[1] =
3024 (snd_soc_read(codec, rreg) >> shift) & mask;
3025 if (invert)
3026 ucontrol->value.integer.value[1] =
3027 max - ucontrol->value.integer.value[1];
3028 ucontrol->value.integer.value[1] =
3029 ucontrol->value.integer.value[1] - min;
3030 }
3031
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003032 return 0;
3033}
3034EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3035
3036/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003037 * snd_soc_limit_volume - Set new limit to an existing volume control.
3038 *
3039 * @codec: where to look for the control
3040 * @name: Name of the control
3041 * @max: new maximum limit
3042 *
3043 * Return 0 for success, else error.
3044 */
3045int snd_soc_limit_volume(struct snd_soc_codec *codec,
3046 const char *name, int max)
3047{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003048 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003049 struct snd_kcontrol *kctl;
3050 struct soc_mixer_control *mc;
3051 int found = 0;
3052 int ret = -EINVAL;
3053
3054 /* Sanity check for name and max */
3055 if (unlikely(!name || max <= 0))
3056 return -EINVAL;
3057
3058 list_for_each_entry(kctl, &card->controls, list) {
3059 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3060 found = 1;
3061 break;
3062 }
3063 }
3064 if (found) {
3065 mc = (struct soc_mixer_control *)kctl->private_value;
3066 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003067 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003068 ret = 0;
3069 }
3070 }
3071 return ret;
3072}
3073EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3074
Mark Brown71d08512011-10-10 18:31:26 +01003075int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3076 struct snd_ctl_elem_info *uinfo)
3077{
3078 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3079 struct soc_bytes *params = (void *)kcontrol->private_value;
3080
3081 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3082 uinfo->count = params->num_regs * codec->val_bytes;
3083
3084 return 0;
3085}
3086EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3087
3088int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3089 struct snd_ctl_elem_value *ucontrol)
3090{
3091 struct soc_bytes *params = (void *)kcontrol->private_value;
3092 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3093 int ret;
3094
3095 if (codec->using_regmap)
3096 ret = regmap_raw_read(codec->control_data, params->base,
3097 ucontrol->value.bytes.data,
3098 params->num_regs * codec->val_bytes);
3099 else
3100 ret = -EINVAL;
3101
Mark Brownf831b052012-02-17 16:20:33 -08003102 /* Hide any masked bytes to ensure consistent data reporting */
3103 if (ret == 0 && params->mask) {
3104 switch (codec->val_bytes) {
3105 case 1:
3106 ucontrol->value.bytes.data[0] &= ~params->mask;
3107 break;
3108 case 2:
3109 ((u16 *)(&ucontrol->value.bytes.data))[0]
3110 &= ~params->mask;
3111 break;
3112 case 4:
3113 ((u32 *)(&ucontrol->value.bytes.data))[0]
3114 &= ~params->mask;
3115 break;
3116 default:
3117 return -EINVAL;
3118 }
3119 }
3120
Mark Brown71d08512011-10-10 18:31:26 +01003121 return ret;
3122}
3123EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3124
3125int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3126 struct snd_ctl_elem_value *ucontrol)
3127{
3128 struct soc_bytes *params = (void *)kcontrol->private_value;
3129 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08003130 int ret, len;
3131 unsigned int val;
3132 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003133
Mark Brownf831b052012-02-17 16:20:33 -08003134 if (!codec->using_regmap)
3135 return -EINVAL;
3136
Mark Brownf831b052012-02-17 16:20:33 -08003137 len = params->num_regs * codec->val_bytes;
3138
Mark Brownb5a8fe42013-01-20 21:42:22 +09003139 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3140 if (!data)
3141 return -ENOMEM;
3142
Mark Brownf831b052012-02-17 16:20:33 -08003143 /*
3144 * If we've got a mask then we need to preserve the register
3145 * bits. We shouldn't modify the incoming data so take a
3146 * copy.
3147 */
3148 if (params->mask) {
3149 ret = regmap_read(codec->control_data, params->base, &val);
3150 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003151 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003152
3153 val &= params->mask;
3154
Mark Brownf831b052012-02-17 16:20:33 -08003155 switch (codec->val_bytes) {
3156 case 1:
3157 ((u8 *)data)[0] &= ~params->mask;
3158 ((u8 *)data)[0] |= val;
3159 break;
3160 case 2:
3161 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3162 ((u16 *)data)[0] |= cpu_to_be16(val);
3163 break;
3164 case 4:
3165 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3166 ((u32 *)data)[0] |= cpu_to_be32(val);
3167 break;
3168 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003169 ret = -EINVAL;
3170 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003171 }
3172 }
3173
3174 ret = regmap_raw_write(codec->control_data, params->base,
3175 data, len);
3176
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003177out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003178 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003179
3180 return ret;
3181}
3182EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3183
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003184/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003185 * snd_soc_info_xr_sx - signed multi register info callback
3186 * @kcontrol: mreg control
3187 * @uinfo: control element information
3188 *
3189 * Callback to provide information of a control that can
3190 * span multiple codec registers which together
3191 * forms a single signed value in a MSB/LSB manner.
3192 *
3193 * Returns 0 for success.
3194 */
3195int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3196 struct snd_ctl_elem_info *uinfo)
3197{
3198 struct soc_mreg_control *mc =
3199 (struct soc_mreg_control *)kcontrol->private_value;
3200 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3201 uinfo->count = 1;
3202 uinfo->value.integer.min = mc->min;
3203 uinfo->value.integer.max = mc->max;
3204
3205 return 0;
3206}
3207EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3208
3209/**
3210 * snd_soc_get_xr_sx - signed multi register get callback
3211 * @kcontrol: mreg control
3212 * @ucontrol: control element information
3213 *
3214 * Callback to get the value of a control that can span
3215 * multiple codec registers which together forms a single
3216 * signed value in a MSB/LSB manner. The control supports
3217 * specifying total no of bits used to allow for bitfields
3218 * across the multiple codec registers.
3219 *
3220 * Returns 0 for success.
3221 */
3222int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3223 struct snd_ctl_elem_value *ucontrol)
3224{
3225 struct soc_mreg_control *mc =
3226 (struct soc_mreg_control *)kcontrol->private_value;
3227 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3228 unsigned int regbase = mc->regbase;
3229 unsigned int regcount = mc->regcount;
3230 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3231 unsigned int regwmask = (1<<regwshift)-1;
3232 unsigned int invert = mc->invert;
3233 unsigned long mask = (1UL<<mc->nbits)-1;
3234 long min = mc->min;
3235 long max = mc->max;
3236 long val = 0;
3237 unsigned long regval;
3238 unsigned int i;
3239
3240 for (i = 0; i < regcount; i++) {
3241 regval = snd_soc_read(codec, regbase+i) & regwmask;
3242 val |= regval << (regwshift*(regcount-i-1));
3243 }
3244 val &= mask;
3245 if (min < 0 && val > max)
3246 val |= ~mask;
3247 if (invert)
3248 val = max - val;
3249 ucontrol->value.integer.value[0] = val;
3250
3251 return 0;
3252}
3253EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3254
3255/**
3256 * snd_soc_put_xr_sx - signed multi register get callback
3257 * @kcontrol: mreg control
3258 * @ucontrol: control element information
3259 *
3260 * Callback to set the value of a control that can span
3261 * multiple codec registers which together forms a single
3262 * signed value in a MSB/LSB manner. The control supports
3263 * specifying total no of bits used to allow for bitfields
3264 * across the multiple codec registers.
3265 *
3266 * Returns 0 for success.
3267 */
3268int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3269 struct snd_ctl_elem_value *ucontrol)
3270{
3271 struct soc_mreg_control *mc =
3272 (struct soc_mreg_control *)kcontrol->private_value;
3273 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3274 unsigned int regbase = mc->regbase;
3275 unsigned int regcount = mc->regcount;
3276 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3277 unsigned int regwmask = (1<<regwshift)-1;
3278 unsigned int invert = mc->invert;
3279 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003280 long max = mc->max;
3281 long val = ucontrol->value.integer.value[0];
3282 unsigned int i, regval, regmask;
3283 int err;
3284
3285 if (invert)
3286 val = max - val;
3287 val &= mask;
3288 for (i = 0; i < regcount; i++) {
3289 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3290 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3291 err = snd_soc_update_bits_locked(codec, regbase+i,
3292 regmask, regval);
3293 if (err < 0)
3294 return err;
3295 }
3296
3297 return 0;
3298}
3299EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3300
3301/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003302 * snd_soc_get_strobe - strobe get callback
3303 * @kcontrol: mixer control
3304 * @ucontrol: control element information
3305 *
3306 * Callback get the value of a strobe mixer control.
3307 *
3308 * Returns 0 for success.
3309 */
3310int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3311 struct snd_ctl_elem_value *ucontrol)
3312{
3313 struct soc_mixer_control *mc =
3314 (struct soc_mixer_control *)kcontrol->private_value;
3315 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3316 unsigned int reg = mc->reg;
3317 unsigned int shift = mc->shift;
3318 unsigned int mask = 1 << shift;
3319 unsigned int invert = mc->invert != 0;
3320 unsigned int val = snd_soc_read(codec, reg) & mask;
3321
3322 if (shift != 0 && val != 0)
3323 val = val >> shift;
3324 ucontrol->value.enumerated.item[0] = val ^ invert;
3325
3326 return 0;
3327}
3328EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3329
3330/**
3331 * snd_soc_put_strobe - strobe put callback
3332 * @kcontrol: mixer control
3333 * @ucontrol: control element information
3334 *
3335 * Callback strobe a register bit to high then low (or the inverse)
3336 * in one pass of a single mixer enum control.
3337 *
3338 * Returns 1 for success.
3339 */
3340int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3341 struct snd_ctl_elem_value *ucontrol)
3342{
3343 struct soc_mixer_control *mc =
3344 (struct soc_mixer_control *)kcontrol->private_value;
3345 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3346 unsigned int reg = mc->reg;
3347 unsigned int shift = mc->shift;
3348 unsigned int mask = 1 << shift;
3349 unsigned int invert = mc->invert != 0;
3350 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3351 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3352 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3353 int err;
3354
3355 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3356 if (err < 0)
3357 return err;
3358
3359 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3360 return err;
3361}
3362EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3363
3364/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003365 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3366 * @dai: DAI
3367 * @clk_id: DAI specific clock ID
3368 * @freq: new clock frequency in Hz
3369 * @dir: new clock direction - input/output.
3370 *
3371 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3372 */
3373int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3374 unsigned int freq, int dir)
3375{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003376 if (dai->driver && dai->driver->ops->set_sysclk)
3377 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003378 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003379 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003380 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003381 else
3382 return -EINVAL;
3383}
3384EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3385
3386/**
Mark Brownec4ee522011-03-07 20:58:11 +00003387 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3388 * @codec: CODEC
3389 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003390 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003391 * @freq: new clock frequency in Hz
3392 * @dir: new clock direction - input/output.
3393 *
3394 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3395 */
3396int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003397 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003398{
3399 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003400 return codec->driver->set_sysclk(codec, clk_id, source,
3401 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003402 else
3403 return -EINVAL;
3404}
3405EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3406
3407/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003408 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3409 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003410 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003411 * @div: new clock divisor.
3412 *
3413 * Configures the clock dividers. This is used to derive the best DAI bit and
3414 * frame clocks from the system or master clock. It's best to set the DAI bit
3415 * and frame clocks as low as possible to save system power.
3416 */
3417int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3418 int div_id, int div)
3419{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003420 if (dai->driver && dai->driver->ops->set_clkdiv)
3421 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003422 else
3423 return -EINVAL;
3424}
3425EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3426
3427/**
3428 * snd_soc_dai_set_pll - configure DAI PLL.
3429 * @dai: DAI
3430 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003431 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003432 * @freq_in: PLL input clock frequency in Hz
3433 * @freq_out: requested PLL output clock frequency in Hz
3434 *
3435 * Configures and enables PLL to generate output clock based on input clock.
3436 */
Mark Brown85488032009-09-05 18:52:16 +01003437int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3438 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003439{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003440 if (dai->driver && dai->driver->ops->set_pll)
3441 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003442 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003443 else if (dai->codec && dai->codec->driver->set_pll)
3444 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3445 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003446 else
3447 return -EINVAL;
3448}
3449EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3450
Mark Brownec4ee522011-03-07 20:58:11 +00003451/*
3452 * snd_soc_codec_set_pll - configure codec PLL.
3453 * @codec: CODEC
3454 * @pll_id: DAI specific PLL ID
3455 * @source: DAI specific source for the PLL
3456 * @freq_in: PLL input clock frequency in Hz
3457 * @freq_out: requested PLL output clock frequency in Hz
3458 *
3459 * Configures and enables PLL to generate output clock based on input clock.
3460 */
3461int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3462 unsigned int freq_in, unsigned int freq_out)
3463{
3464 if (codec->driver->set_pll)
3465 return codec->driver->set_pll(codec, pll_id, source,
3466 freq_in, freq_out);
3467 else
3468 return -EINVAL;
3469}
3470EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3471
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003472/**
3473 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3474 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003475 * @fmt: SND_SOC_DAIFMT_ format value.
3476 *
3477 * Configures the DAI hardware format and clocking.
3478 */
3479int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3480{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003481 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003482 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003483 if (dai->driver->ops->set_fmt == NULL)
3484 return -ENOTSUPP;
3485 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003486}
3487EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3488
3489/**
3490 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3491 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003492 * @tx_mask: bitmask representing active TX slots.
3493 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003494 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003495 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003496 *
3497 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3498 * specific.
3499 */
3500int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003501 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003502{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003503 if (dai->driver && dai->driver->ops->set_tdm_slot)
3504 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003505 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003506 else
3507 return -EINVAL;
3508}
3509EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3510
3511/**
Barry Song472df3c2009-09-12 01:16:29 +08003512 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3513 * @dai: DAI
3514 * @tx_num: how many TX channels
3515 * @tx_slot: pointer to an array which imply the TX slot number channel
3516 * 0~num-1 uses
3517 * @rx_num: how many RX channels
3518 * @rx_slot: pointer to an array which imply the RX slot number channel
3519 * 0~num-1 uses
3520 *
3521 * configure the relationship between channel number and TDM slot number.
3522 */
3523int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3524 unsigned int tx_num, unsigned int *tx_slot,
3525 unsigned int rx_num, unsigned int *rx_slot)
3526{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003527 if (dai->driver && dai->driver->ops->set_channel_map)
3528 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003529 rx_num, rx_slot);
3530 else
3531 return -EINVAL;
3532}
3533EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3534
3535/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003536 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3537 * @dai: DAI
3538 * @tristate: tristate enable
3539 *
3540 * Tristates the DAI so that others can use it.
3541 */
3542int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3543{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003544 if (dai->driver && dai->driver->ops->set_tristate)
3545 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003546 else
3547 return -EINVAL;
3548}
3549EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3550
3551/**
3552 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3553 * @dai: DAI
3554 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003555 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003556 *
3557 * Mutes the DAI DAC.
3558 */
Mark Brownda183962013-02-06 15:44:07 +00003559int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3560 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003561{
Mark Brownda183962013-02-06 15:44:07 +00003562 if (!dai->driver)
3563 return -ENOTSUPP;
3564
3565 if (dai->driver->ops->mute_stream)
3566 return dai->driver->ops->mute_stream(dai, mute, direction);
3567 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3568 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003569 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003570 else
Mark Brown04570c62012-04-13 19:16:03 +01003571 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003572}
3573EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3574
Mark Brownc5af3a22008-11-28 13:29:45 +00003575/**
3576 * snd_soc_register_card - Register a card with the ASoC core
3577 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003578 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003579 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003580 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303581int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003582{
Mark Brownb19e6e72012-03-14 21:18:39 +00003583 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003584
Mark Brownc5af3a22008-11-28 13:29:45 +00003585 if (!card->name || !card->dev)
3586 return -EINVAL;
3587
Stephen Warren5a504962011-12-21 10:40:59 -07003588 for (i = 0; i < card->num_links; i++) {
3589 struct snd_soc_dai_link *link = &card->dai_link[i];
3590
3591 /*
3592 * Codec must be specified by 1 of name or OF node,
3593 * not both or neither.
3594 */
3595 if (!!link->codec_name == !!link->codec_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003596 dev_err(card->dev,
3597 "ASoC: Neither/both codec name/of_node are set for %s\n",
3598 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003599 return -EINVAL;
3600 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003601 /* Codec DAI name must be specified */
3602 if (!link->codec_dai_name) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003603 dev_err(card->dev,
3604 "ASoC: codec_dai_name not set for %s\n",
3605 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003606 return -EINVAL;
3607 }
Stephen Warren5a504962011-12-21 10:40:59 -07003608
3609 /*
3610 * Platform may be specified by either name or OF node, but
3611 * can be left unspecified, and a dummy platform will be used.
3612 */
3613 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003614 dev_err(card->dev,
3615 "ASoC: Both platform name/of_node are set for %s\n",
3616 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003617 return -EINVAL;
3618 }
3619
3620 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003621 * CPU device may be specified by either name or OF node, but
3622 * can be left unspecified, and will be matched based on DAI
3623 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003624 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003625 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003626 dev_err(card->dev,
3627 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3628 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003629 return -EINVAL;
3630 }
3631 /*
3632 * At least one of CPU DAI name or CPU device name/node must be
3633 * specified
3634 */
3635 if (!link->cpu_dai_name &&
3636 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003637 dev_err(card->dev,
3638 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3639 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003640 return -EINVAL;
3641 }
3642 }
3643
Mark Browned77cc12011-05-03 18:25:34 +01003644 dev_set_drvdata(card->dev, card);
3645
Stephen Warren111c6412011-01-28 14:26:35 -07003646 snd_soc_initialize_card_lists(card);
3647
Vinod Koul150dd2f2011-01-13 22:48:32 +05303648 soc_init_card_debugfs(card);
3649
Mark Brown181a6892012-03-14 20:18:49 +00003650 card->rtd = devm_kzalloc(card->dev,
3651 sizeof(struct snd_soc_pcm_runtime) *
3652 (card->num_links + card->num_aux_devs),
3653 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003654 if (card->rtd == NULL)
3655 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003656 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003657 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003658
3659 for (i = 0; i < card->num_links; i++)
3660 card->rtd[i].dai_link = &card->dai_link[i];
3661
Mark Brownc5af3a22008-11-28 13:29:45 +00003662 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003663 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003664 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003665 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003666 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003667
Mark Brownb19e6e72012-03-14 21:18:39 +00003668 ret = snd_soc_instantiate_card(card);
3669 if (ret != 0)
3670 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003671
Mark Brownb19e6e72012-03-14 21:18:39 +00003672 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003673}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303674EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003675
3676/**
3677 * snd_soc_unregister_card - Unregister a card with the ASoC core
3678 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003679 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003680 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003681 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303682int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003683{
Vinod Koulb0e26482011-01-13 22:48:02 +05303684 if (card->instantiated)
3685 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003686 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003687
3688 return 0;
3689}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303690EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003691
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003692/*
3693 * Simplify DAI link configuration by removing ".-1" from device names
3694 * and sanitizing names.
3695 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003696static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003697{
3698 char *found, name[NAME_SIZE];
3699 int id1, id2;
3700
3701 if (dev_name(dev) == NULL)
3702 return NULL;
3703
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003704 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003705
3706 /* are we a "%s.%d" name (platform and SPI components) */
3707 found = strstr(name, dev->driver->name);
3708 if (found) {
3709 /* get ID */
3710 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3711
3712 /* discard ID from name if ID == -1 */
3713 if (*id == -1)
3714 found[strlen(dev->driver->name)] = '\0';
3715 }
3716
3717 } else {
3718 /* I2C component devices are named "bus-addr" */
3719 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3720 char tmp[NAME_SIZE];
3721
3722 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003723 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003724
3725 /* sanitize component name for DAI link creation */
3726 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003727 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003728 } else
3729 *id = 0;
3730 }
3731
3732 return kstrdup(name, GFP_KERNEL);
3733}
3734
3735/*
3736 * Simplify DAI link naming for single devices with multiple DAIs by removing
3737 * any ".-1" and using the DAI name (instead of device name).
3738 */
3739static inline char *fmt_multiple_name(struct device *dev,
3740 struct snd_soc_dai_driver *dai_drv)
3741{
3742 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003743 dev_err(dev,
3744 "ASoC: error - multiple DAI %s registered with no name\n",
3745 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003746 return NULL;
3747 }
3748
3749 return kstrdup(dai_drv->name, GFP_KERNEL);
3750}
3751
Mark Brown91151712008-11-30 23:31:24 +00003752/**
3753 * snd_soc_register_dai - Register a DAI with the ASoC core
3754 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003755 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003756 */
Kuninori Morimotof53179c02013-03-21 03:38:30 -07003757static int snd_soc_register_dai(struct device *dev,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003758 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003759{
Mark Brown054880f2012-04-09 17:29:19 +01003760 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003761 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003762
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003763 dev_dbg(dev, "ASoC: dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003764
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003765 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3766 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003767 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003768
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003769 /* create DAI component name */
3770 dai->name = fmt_single_name(dev, &dai->id);
3771 if (dai->name == NULL) {
3772 kfree(dai);
3773 return -ENOMEM;
3774 }
3775
3776 dai->dev = dev;
3777 dai->driver = dai_drv;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003778 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003779 if (!dai->driver->ops)
3780 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003781
3782 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003783
3784 list_for_each_entry(codec, &codec_list, list) {
3785 if (codec->dev == dev) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003786 dev_dbg(dev, "ASoC: Mapped DAI %s to CODEC %s\n",
Mark Brown054880f2012-04-09 17:29:19 +01003787 dai->name, codec->name);
3788 dai->codec = codec;
3789 break;
3790 }
3791 }
3792
Peter Ujfalusi5f800082012-08-07 10:24:13 +03003793 if (!dai->codec)
3794 dai->dapm.idle_bias_off = 1;
3795
Mark Brown91151712008-11-30 23:31:24 +00003796 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003797
Mark Brown91151712008-11-30 23:31:24 +00003798 mutex_unlock(&client_mutex);
3799
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003800 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003801
3802 return 0;
3803}
Mark Brown91151712008-11-30 23:31:24 +00003804
3805/**
3806 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3807 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003808 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003809 */
Kuninori Morimotof53179c02013-03-21 03:38:30 -07003810static void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003811{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003812 struct snd_soc_dai *dai;
3813
3814 list_for_each_entry(dai, &dai_list, list) {
3815 if (dev == dai->dev)
3816 goto found;
3817 }
3818 return;
3819
3820found:
Mark Brown91151712008-11-30 23:31:24 +00003821 mutex_lock(&client_mutex);
3822 list_del(&dai->list);
3823 mutex_unlock(&client_mutex);
3824
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003825 dev_dbg(dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003826 kfree(dai->name);
3827 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003828}
Mark Brown91151712008-11-30 23:31:24 +00003829
3830/**
3831 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3832 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003833 * @dai: Array of DAIs to register
3834 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003835 */
Kuninori Morimotof53179c02013-03-21 03:38:30 -07003836static int snd_soc_register_dais(struct device *dev,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003837 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003838{
Mark Brown054880f2012-04-09 17:29:19 +01003839 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003840 struct snd_soc_dai *dai;
3841 int i, ret = 0;
3842
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003843 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003844
3845 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003846
3847 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003848 if (dai == NULL) {
3849 ret = -ENOMEM;
3850 goto err;
3851 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003852
3853 /* create DAI component name */
3854 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3855 if (dai->name == NULL) {
3856 kfree(dai);
3857 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003858 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003859 }
3860
3861 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003862 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003863 if (dai->driver->id)
3864 dai->id = dai->driver->id;
3865 else
3866 dai->id = i;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003867 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003868 if (!dai->driver->ops)
3869 dai->driver->ops = &null_dai_ops;
3870
3871 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003872
3873 list_for_each_entry(codec, &codec_list, list) {
3874 if (codec->dev == dev) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003875 dev_dbg(dev,
3876 "ASoC: Mapped DAI %s to CODEC %s\n",
3877 dai->name, codec->name);
Mark Brown054880f2012-04-09 17:29:19 +01003878 dai->codec = codec;
3879 break;
3880 }
3881 }
3882
Peter Ujfalusi5f800082012-08-07 10:24:13 +03003883 if (!dai->codec)
3884 dai->dapm.idle_bias_off = 1;
3885
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003886 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003887
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003888 mutex_unlock(&client_mutex);
3889
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003890 dev_dbg(dai->dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003891 }
3892
3893 return 0;
3894
3895err:
3896 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003897 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003898
3899 return ret;
3900}
Mark Brown91151712008-11-30 23:31:24 +00003901
3902/**
3903 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3904 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003905 * @dai: Array of DAIs to unregister
3906 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003907 */
Kuninori Morimotof53179c02013-03-21 03:38:30 -07003908static void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003909{
3910 int i;
3911
3912 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003913 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003914}
Mark Brown91151712008-11-30 23:31:24 +00003915
Mark Brown12a48a8c2008-12-03 19:40:30 +00003916/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003917 * snd_soc_add_platform - Add a platform to the ASoC core
3918 * @dev: The parent device for the platform
3919 * @platform: The platform to add
3920 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00003921 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003922int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01003923 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003924{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003925 /* create platform component name */
3926 platform->name = fmt_single_name(dev, &platform->id);
3927 if (platform->name == NULL) {
3928 kfree(platform);
3929 return -ENOMEM;
3930 }
3931
3932 platform->dev = dev;
3933 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003934 platform->dapm.dev = dev;
3935 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003936 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00003937 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003938
3939 mutex_lock(&client_mutex);
3940 list_add(&platform->list, &platform_list);
3941 mutex_unlock(&client_mutex);
3942
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003943 dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003944
3945 return 0;
3946}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003947EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3948
3949/**
3950 * snd_soc_register_platform - Register a platform with the ASoC core
3951 *
3952 * @platform: platform to register
3953 */
3954int snd_soc_register_platform(struct device *dev,
3955 const struct snd_soc_platform_driver *platform_drv)
3956{
3957 struct snd_soc_platform *platform;
3958 int ret;
3959
3960 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3961
3962 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3963 if (platform == NULL)
3964 return -ENOMEM;
3965
3966 ret = snd_soc_add_platform(dev, platform, platform_drv);
3967 if (ret)
3968 kfree(platform);
3969
3970 return ret;
3971}
Mark Brown12a48a8c2008-12-03 19:40:30 +00003972EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3973
3974/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003975 * snd_soc_remove_platform - Remove a platform from the ASoC core
3976 * @platform: the platform to remove
3977 */
3978void snd_soc_remove_platform(struct snd_soc_platform *platform)
3979{
3980 mutex_lock(&client_mutex);
3981 list_del(&platform->list);
3982 mutex_unlock(&client_mutex);
3983
3984 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3985 platform->name);
3986 kfree(platform->name);
3987}
3988EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3989
3990struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3991{
3992 struct snd_soc_platform *platform;
3993
3994 list_for_each_entry(platform, &platform_list, list) {
3995 if (dev == platform->dev)
3996 return platform;
3997 }
3998
3999 return NULL;
4000}
4001EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4002
4003/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004004 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4005 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004006 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004007 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004008void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004009{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004010 struct snd_soc_platform *platform;
4011
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004012 platform = snd_soc_lookup_platform(dev);
4013 if (!platform)
4014 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004015
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004016 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004017 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004018}
4019EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4020
Mark Brown151ab222009-05-09 16:22:58 +01004021static u64 codec_format_map[] = {
4022 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4023 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4024 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4025 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4026 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4027 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4028 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4029 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4030 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4031 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4032 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4033 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4034 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4035 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4036 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4037 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4038};
4039
4040/* Fix up the DAI formats for endianness: codecs don't actually see
4041 * the endianness of the data but we're using the CPU format
4042 * definitions which do need to include endianness so we ensure that
4043 * codec DAIs always have both big and little endian variants set.
4044 */
4045static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4046{
4047 int i;
4048
4049 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4050 if (stream->formats & codec_format_map[i])
4051 stream->formats |= codec_format_map[i];
4052}
4053
Mark Brown0d0cf002008-12-10 14:32:45 +00004054/**
4055 * snd_soc_register_codec - Register a codec with the ASoC core
4056 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004057 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004058 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004059int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004060 const struct snd_soc_codec_driver *codec_drv,
4061 struct snd_soc_dai_driver *dai_drv,
4062 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004063{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004064 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004065 struct snd_soc_codec *codec;
4066 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004067
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004068 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004069
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004070 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4071 if (codec == NULL)
4072 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004073
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004074 /* create CODEC component name */
4075 codec->name = fmt_single_name(dev, &codec->id);
4076 if (codec->name == NULL) {
Kuninori Morimotof790b942013-02-25 00:40:09 -08004077 ret = -ENOMEM;
4078 goto fail_codec;
Mark Brown151ab222009-05-09 16:22:58 +01004079 }
4080
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00004081 if (codec_drv->compress_type)
4082 codec->compress_type = codec_drv->compress_type;
4083 else
4084 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
4085
Mark Brownc3acec22010-12-02 16:15:29 +00004086 codec->write = codec_drv->write;
4087 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004088 codec->volatile_register = codec_drv->volatile_register;
4089 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004090 codec->writable_register = codec_drv->writable_register;
Mark Brown5124e692012-02-08 13:20:50 +00004091 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004092 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4093 codec->dapm.dev = dev;
4094 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00004095 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01004096 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004097 codec->dev = dev;
4098 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004099 codec->num_dai = num_dai;
4100 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004101
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004102 /* allocate CODEC register cache */
4103 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004104 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00004105 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004106 /* it is necessary to make a copy of the default register cache
4107 * because in the case of using a compression type that requires
Bill Pembertonf6e65742012-11-19 13:25:33 -05004108 * the default register cache to be marked as the
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004109 * kernel might have freed the array by the time we initialize
4110 * the cache.
4111 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00004112 if (codec_drv->reg_cache_default) {
4113 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
4114 reg_size, GFP_KERNEL);
4115 if (!codec->reg_def_copy) {
4116 ret = -ENOMEM;
Kuninori Morimotof790b942013-02-25 00:40:09 -08004117 goto fail_codec_name;
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00004118 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004119 }
4120 }
4121
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004122 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
4123 if (!codec->volatile_register)
4124 codec->volatile_register = snd_soc_default_volatile_register;
4125 if (!codec->readable_register)
4126 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004127 if (!codec->writable_register)
4128 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004129 }
4130
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004131 for (i = 0; i < num_dai; i++) {
4132 fixup_codec_formats(&dai_drv[i].playback);
4133 fixup_codec_formats(&dai_drv[i].capture);
4134 }
4135
Mark Brown054880f2012-04-09 17:29:19 +01004136 mutex_lock(&client_mutex);
4137 list_add(&codec->list, &codec_list);
4138 mutex_unlock(&client_mutex);
4139
Mark Brown26b01cc2010-08-18 20:20:55 +01004140 /* register any DAIs */
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004141 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4142 if (ret < 0) {
4143 dev_err(codec->dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4144 goto fail_codec_name;
Mark Brown26b01cc2010-08-18 20:20:55 +01004145 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004146
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004147 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004148 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004149
Kuninori Morimotof790b942013-02-25 00:40:09 -08004150fail_codec_name:
Kuninori Morimotoe1328a82013-03-07 17:42:33 -08004151 mutex_lock(&client_mutex);
4152 list_del(&codec->list);
4153 mutex_unlock(&client_mutex);
4154
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004155 kfree(codec->name);
Kuninori Morimotof790b942013-02-25 00:40:09 -08004156fail_codec:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004157 kfree(codec);
4158 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004159}
4160EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4161
4162/**
4163 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4164 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004165 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004166 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004167void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004168{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004169 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004170
4171 list_for_each_entry(codec, &codec_list, list) {
4172 if (dev == codec->dev)
4173 goto found;
4174 }
4175 return;
4176
4177found:
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004178 snd_soc_unregister_dais(dev, codec->num_dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004179
Mark Brown0d0cf002008-12-10 14:32:45 +00004180 mutex_lock(&client_mutex);
4181 list_del(&codec->list);
4182 mutex_unlock(&client_mutex);
4183
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004184 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004185
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004186 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004187 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01004188 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004189 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004190}
4191EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4192
Kuninori Morimoto030e79f2013-03-11 18:27:21 -07004193
4194/**
4195 * snd_soc_register_component - Register a component with the ASoC core
4196 *
4197 */
4198int snd_soc_register_component(struct device *dev,
4199 const struct snd_soc_component_driver *cmpnt_drv,
4200 struct snd_soc_dai_driver *dai_drv,
4201 int num_dai)
4202{
4203 struct snd_soc_component *cmpnt;
4204 int ret;
4205
4206 dev_dbg(dev, "component register %s\n", dev_name(dev));
4207
4208 cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
4209 if (!cmpnt) {
4210 dev_err(dev, "ASoC: Failed to allocate memory\n");
4211 return -ENOMEM;
4212 }
4213
4214 cmpnt->name = fmt_single_name(dev, &cmpnt->id);
4215 if (!cmpnt->name) {
4216 dev_err(dev, "ASoC: Failed to simplifying name\n");
4217 return -ENOMEM;
4218 }
4219
4220 cmpnt->dev = dev;
4221 cmpnt->driver = cmpnt_drv;
4222 cmpnt->num_dai = num_dai;
4223
Kuninori Morimotoa1422b82013-03-21 03:27:13 -07004224 /*
4225 * snd_soc_register_dai() uses fmt_single_name(), and
4226 * snd_soc_register_dais() uses fmt_multiple_name()
4227 * for dai->name which is used for name based matching
4228 */
4229 if (1 == num_dai)
4230 ret = snd_soc_register_dai(dev, dai_drv);
4231 else
4232 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -07004233 if (ret < 0) {
4234 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4235 goto error_component_name;
4236 }
4237
4238 mutex_lock(&client_mutex);
4239 list_add(&cmpnt->list, &component_list);
4240 mutex_unlock(&client_mutex);
4241
4242 dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
4243
4244 return ret;
4245
4246error_component_name:
4247 kfree(cmpnt->name);
4248
4249 return ret;
4250}
Stephen Warren2e1cc192013-03-26 16:38:19 -06004251EXPORT_SYMBOL_GPL(snd_soc_register_component);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -07004252
4253/**
4254 * snd_soc_unregister_component - Unregister a component from the ASoC core
4255 *
4256 */
4257void snd_soc_unregister_component(struct device *dev)
4258{
4259 struct snd_soc_component *cmpnt;
4260
4261 list_for_each_entry(cmpnt, &component_list, list) {
4262 if (dev == cmpnt->dev)
4263 goto found;
4264 }
4265 return;
4266
4267found:
4268 snd_soc_unregister_dais(dev, cmpnt->num_dai);
4269
4270 mutex_lock(&client_mutex);
4271 list_del(&cmpnt->list);
4272 mutex_unlock(&client_mutex);
4273
4274 dev_dbg(dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
4275 kfree(cmpnt->name);
4276}
Stephen Warren2e1cc192013-03-26 16:38:19 -06004277EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -07004278
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004279/* Retrieve a card's name from device tree */
4280int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4281 const char *propname)
4282{
4283 struct device_node *np = card->dev->of_node;
4284 int ret;
4285
4286 ret = of_property_read_string_index(np, propname, 0, &card->name);
4287 /*
4288 * EINVAL means the property does not exist. This is fine providing
4289 * card->name was previously set, which is checked later in
4290 * snd_soc_register_card.
4291 */
4292 if (ret < 0 && ret != -EINVAL) {
4293 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004294 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004295 propname, ret);
4296 return ret;
4297 }
4298
4299 return 0;
4300}
4301EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4302
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004303int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4304 const char *propname)
4305{
4306 struct device_node *np = card->dev->of_node;
4307 int num_routes;
4308 struct snd_soc_dapm_route *routes;
4309 int i, ret;
4310
4311 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004312 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004313 dev_err(card->dev,
4314 "ASoC: Property '%s' does not exist or its length is not even\n",
4315 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004316 return -EINVAL;
4317 }
4318 num_routes /= 2;
4319 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004320 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004321 propname);
4322 return -EINVAL;
4323 }
4324
4325 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4326 GFP_KERNEL);
4327 if (!routes) {
4328 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004329 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004330 return -EINVAL;
4331 }
4332
4333 for (i = 0; i < num_routes; i++) {
4334 ret = of_property_read_string_index(np, propname,
4335 2 * i, &routes[i].sink);
4336 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004337 dev_err(card->dev,
4338 "ASoC: Property '%s' index %d could not be read: %d\n",
4339 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004340 return -EINVAL;
4341 }
4342 ret = of_property_read_string_index(np, propname,
4343 (2 * i) + 1, &routes[i].source);
4344 if (ret) {
4345 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004346 "ASoC: Property '%s' index %d could not be read: %d\n",
4347 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004348 return -EINVAL;
4349 }
4350 }
4351
4352 card->num_dapm_routes = num_routes;
4353 card->dapm_routes = routes;
4354
4355 return 0;
4356}
4357EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4358
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004359unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4360 const char *prefix)
4361{
4362 int ret, i;
4363 char prop[128];
4364 unsigned int format = 0;
4365 int bit, frame;
4366 const char *str;
4367 struct {
4368 char *name;
4369 unsigned int val;
4370 } of_fmt_table[] = {
4371 { "i2s", SND_SOC_DAIFMT_I2S },
4372 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4373 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4374 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4375 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4376 { "ac97", SND_SOC_DAIFMT_AC97 },
4377 { "pdm", SND_SOC_DAIFMT_PDM},
4378 { "msb", SND_SOC_DAIFMT_MSB },
4379 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004380 };
4381
4382 if (!prefix)
4383 prefix = "";
4384
4385 /*
4386 * check "[prefix]format = xxx"
4387 * SND_SOC_DAIFMT_FORMAT_MASK area
4388 */
4389 snprintf(prop, sizeof(prop), "%sformat", prefix);
4390 ret = of_property_read_string(np, prop, &str);
4391 if (ret == 0) {
4392 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4393 if (strcmp(str, of_fmt_table[i].name) == 0) {
4394 format |= of_fmt_table[i].val;
4395 break;
4396 }
4397 }
4398 }
4399
4400 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004401 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004402 * SND_SOC_DAIFMT_CLOCK_MASK area
4403 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004404 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4405 if (of_get_property(np, prop, NULL))
4406 format |= SND_SOC_DAIFMT_CONT;
4407 else
4408 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004409
4410 /*
4411 * check "[prefix]bitclock-inversion"
4412 * check "[prefix]frame-inversion"
4413 * SND_SOC_DAIFMT_INV_MASK area
4414 */
4415 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4416 bit = !!of_get_property(np, prop, NULL);
4417
4418 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4419 frame = !!of_get_property(np, prop, NULL);
4420
4421 switch ((bit << 4) + frame) {
4422 case 0x11:
4423 format |= SND_SOC_DAIFMT_IB_IF;
4424 break;
4425 case 0x10:
4426 format |= SND_SOC_DAIFMT_IB_NF;
4427 break;
4428 case 0x01:
4429 format |= SND_SOC_DAIFMT_NB_IF;
4430 break;
4431 default:
4432 /* SND_SOC_DAIFMT_NB_NF is default */
4433 break;
4434 }
4435
4436 /*
4437 * check "[prefix]bitclock-master"
4438 * check "[prefix]frame-master"
4439 * SND_SOC_DAIFMT_MASTER_MASK area
4440 */
4441 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4442 bit = !!of_get_property(np, prop, NULL);
4443
4444 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4445 frame = !!of_get_property(np, prop, NULL);
4446
4447 switch ((bit << 4) + frame) {
4448 case 0x11:
4449 format |= SND_SOC_DAIFMT_CBM_CFM;
4450 break;
4451 case 0x10:
4452 format |= SND_SOC_DAIFMT_CBM_CFS;
4453 break;
4454 case 0x01:
4455 format |= SND_SOC_DAIFMT_CBS_CFM;
4456 break;
4457 default:
4458 format |= SND_SOC_DAIFMT_CBS_CFS;
4459 break;
4460 }
4461
4462 return format;
4463}
4464EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4465
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004466static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004467{
Mark Brown384c89e2008-12-03 17:34:03 +00004468#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004469 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4470 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004471 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004472 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004473 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004474
Mark Brown8a9dab12011-01-10 22:25:21 +00004475 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004476 &codec_list_fops))
4477 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4478
Mark Brown8a9dab12011-01-10 22:25:21 +00004479 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004480 &dai_list_fops))
4481 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004482
Mark Brown8a9dab12011-01-10 22:25:21 +00004483 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004484 &platform_list_fops))
4485 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004486#endif
4487
Mark Brownfb257892011-04-28 10:57:54 +01004488 snd_soc_util_init();
4489
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004490 return platform_driver_register(&soc_driver);
4491}
Mark Brown4abe8e12010-10-12 17:41:03 +01004492module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004493
Mark Brown7d8c16a2008-11-30 22:11:24 +00004494static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004495{
Mark Brownfb257892011-04-28 10:57:54 +01004496 snd_soc_util_exit();
4497
Mark Brown384c89e2008-12-03 17:34:03 +00004498#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004499 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004500#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004501 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004502}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004503module_exit(snd_soc_exit);
4504
4505/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004506MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004507MODULE_DESCRIPTION("ALSA SoC Core");
4508MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004509MODULE_ALIAS("platform:soc-audio");