blob: 88daa649fc0648c70048fb1595b120b4f61184e0 [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
Jingoo Hanb785a492013-07-19 16:24:59 +0900195 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700196 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;
Jingoo Hanb785a492013-07-19 16:24:59 +0900240 int ret;
Mark Brown2624d5f2009-11-03 21:56:13 +0000241
242 buf_size = min(count, (sizeof(buf)-1));
243 if (copy_from_user(buf, user_buf, buf_size))
244 return -EFAULT;
245 buf[buf_size] = 0;
246
Mark Brown2624d5f2009-11-03 21:56:13 +0000247 while (*start == ' ')
248 start++;
249 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000250 while (*start == ' ')
251 start++;
Jingoo Hanb785a492013-07-19 16:24:59 +0900252 ret = kstrtoul(start, 16, &value);
253 if (ret)
254 return ret;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000255
256 /* Userspace has been fiddling around behind the kernel's back */
Rusty Russell373d4d02013-01-21 17:17:39 +1030257 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
Mark Brown0d51a9c2011-01-06 16:04:57 +0000258
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000259 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000260 return buf_size;
261}
262
263static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700264 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000265 .read = codec_reg_read_file,
266 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200267 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000268};
269
270static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
271{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200272 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
273
274 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
275 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000276 if (!codec->debugfs_codec_root) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200277 dev_warn(codec->dev,
278 "ASoC: Failed to create codec debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000279 return;
280 }
281
Mark Brownaaee8ef2011-01-26 20:53:50 +0000282 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
283 &codec->cache_sync);
284 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
285 &codec->cache_only);
286
Mark Brown2624d5f2009-11-03 21:56:13 +0000287 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
288 codec->debugfs_codec_root,
289 codec, &codec_reg_fops);
290 if (!codec->debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200291 dev_warn(codec->dev,
292 "ASoC: Failed to create codec register debugfs file\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000293
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200294 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000295}
296
297static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
298{
299 debugfs_remove_recursive(codec->debugfs_codec_root);
300}
301
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000302static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
303{
304 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
305
306 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
307 debugfs_card_root);
308 if (!platform->debugfs_platform_root) {
309 dev_warn(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000310 "ASoC: Failed to create platform debugfs directory\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000311 return;
312 }
313
314 snd_soc_dapm_debugfs_init(&platform->dapm,
315 platform->debugfs_platform_root);
316}
317
318static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
319{
320 debugfs_remove_recursive(platform->debugfs_platform_root);
321}
322
Mark Brownc3c5a192010-09-15 18:15:14 +0100323static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
324 size_t count, loff_t *ppos)
325{
326 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100327 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100328 struct snd_soc_codec *codec;
329
330 if (!buf)
331 return -ENOMEM;
332
Mark Brown2b194f9d2010-10-13 10:52:16 +0100333 list_for_each_entry(codec, &codec_list, list) {
334 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
335 codec->name);
336 if (len >= 0)
337 ret += len;
338 if (ret > PAGE_SIZE) {
339 ret = PAGE_SIZE;
340 break;
341 }
342 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100343
344 if (ret >= 0)
345 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
346
347 kfree(buf);
348
349 return ret;
350}
351
352static const struct file_operations codec_list_fops = {
353 .read = codec_list_read_file,
354 .llseek = default_llseek,/* read accesses f_pos */
355};
356
Mark Brownf3208782010-09-15 18:19:07 +0100357static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
358 size_t count, loff_t *ppos)
359{
360 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100361 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100362 struct snd_soc_dai *dai;
363
364 if (!buf)
365 return -ENOMEM;
366
Mark Brown2b194f9d2010-10-13 10:52:16 +0100367 list_for_each_entry(dai, &dai_list, list) {
368 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
369 if (len >= 0)
370 ret += len;
371 if (ret > PAGE_SIZE) {
372 ret = PAGE_SIZE;
373 break;
374 }
375 }
Mark Brownf3208782010-09-15 18:19:07 +0100376
Mark Brown2b194f9d2010-10-13 10:52:16 +0100377 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100378
379 kfree(buf);
380
381 return ret;
382}
383
384static const struct file_operations dai_list_fops = {
385 .read = dai_list_read_file,
386 .llseek = default_llseek,/* read accesses f_pos */
387};
388
Mark Brown19c7ac22010-09-15 18:22:40 +0100389static ssize_t platform_list_read_file(struct file *file,
390 char __user *user_buf,
391 size_t count, loff_t *ppos)
392{
393 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100394 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100395 struct snd_soc_platform *platform;
396
397 if (!buf)
398 return -ENOMEM;
399
Mark Brown2b194f9d2010-10-13 10:52:16 +0100400 list_for_each_entry(platform, &platform_list, list) {
401 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
402 platform->name);
403 if (len >= 0)
404 ret += len;
405 if (ret > PAGE_SIZE) {
406 ret = PAGE_SIZE;
407 break;
408 }
409 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100410
Mark Brown2b194f9d2010-10-13 10:52:16 +0100411 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100412
413 kfree(buf);
414
415 return ret;
416}
417
418static const struct file_operations platform_list_fops = {
419 .read = platform_list_read_file,
420 .llseek = default_llseek,/* read accesses f_pos */
421};
422
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200423static void soc_init_card_debugfs(struct snd_soc_card *card)
424{
425 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000426 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200427 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200428 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100429 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200430 return;
431 }
432
433 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
434 card->debugfs_card_root,
435 &card->pop_time);
436 if (!card->debugfs_pop_time)
437 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000438 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200439}
440
441static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
442{
443 debugfs_remove_recursive(card->debugfs_card_root);
444}
445
Mark Brown2624d5f2009-11-03 21:56:13 +0000446#else
447
448static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
449{
450}
451
452static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
453{
454}
Axel Linb95fccb2010-11-09 17:06:44 +0800455
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000456static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
457{
458}
459
460static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
461{
462}
463
Axel Linb95fccb2010-11-09 17:06:44 +0800464static inline void soc_init_card_debugfs(struct snd_soc_card *card)
465{
466}
467
468static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
469{
470}
Mark Brown2624d5f2009-11-03 21:56:13 +0000471#endif
472
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100473struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
474 const char *dai_link, int stream)
475{
476 int i;
477
478 for (i = 0; i < card->num_links; i++) {
479 if (card->rtd[i].dai_link->no_pcm &&
480 !strcmp(card->rtd[i].dai_link->name, dai_link))
481 return card->rtd[i].pcm->streams[stream].substream;
482 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000483 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100484 return NULL;
485}
486EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
487
488struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
489 const char *dai_link)
490{
491 int i;
492
493 for (i = 0; i < card->num_links; i++) {
494 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
495 return &card->rtd[i];
496 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000497 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100498 return NULL;
499}
500EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
501
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200502#ifdef CONFIG_SND_SOC_AC97_BUS
503/* unregister ac97 codec */
504static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
505{
506 if (codec->ac97->dev.bus)
507 device_unregister(&codec->ac97->dev);
508 return 0;
509}
510
511/* stop no dev release warning */
512static void soc_ac97_device_release(struct device *dev){}
513
514/* register ac97 codec to bus */
515static int soc_ac97_dev_register(struct snd_soc_codec *codec)
516{
517 int err;
518
519 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100520 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200521 codec->ac97->dev.release = soc_ac97_device_release;
522
Kay Sieversbb072bf2008-11-02 03:50:35 +0100523 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000524 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200525 err = device_register(&codec->ac97->dev);
526 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000527 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200528 codec->ac97->dev.bus = NULL;
529 return err;
530 }
531 return 0;
532}
533#endif
534
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000535#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200536/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000537int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200538{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000539 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200540 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200541 int i;
542
Daniel Macke3509ff2009-06-03 17:44:49 +0200543 /* If the initialization of this soc device failed, there is no codec
544 * associated with it. Just bail out in this case.
545 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000546 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200547 return 0;
548
Andy Green6ed25972008-06-13 16:24:05 +0100549 /* Due to the resume being scheduled into a workqueue we could
550 * suspend before that's finished - wait for it to complete.
551 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552 snd_power_lock(card->snd_card);
553 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
554 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100555
556 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000557 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100558
Mark Browna00f90f2010-12-02 16:24:24 +0000559 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000560 for (i = 0; i < card->num_rtd; i++) {
561 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
562 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100563
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000564 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100565 continue;
566
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000567 if (drv->ops->digital_mute && dai->playback_active)
568 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200569 }
570
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100571 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 for (i = 0; i < card->num_rtd; i++) {
573 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100574 continue;
575
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000576 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100577 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100578
Mark Brown87506542008-11-18 20:50:34 +0000579 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000580 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200581
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000582 for (i = 0; i < card->num_rtd; i++) {
583 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
584 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100585
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000586 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100587 continue;
588
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000589 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
590 cpu_dai->driver->suspend(cpu_dai);
591 if (platform->driver->suspend && !platform->suspended) {
592 platform->driver->suspend(cpu_dai);
593 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100594 }
595 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200596
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000597 /* close any waiting streams and save state */
598 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo43829732012-08-20 14:51:24 -0700599 flush_delayed_work(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200600 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000601 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100602
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000603 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000604
605 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100606 continue;
607
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800608 snd_soc_dapm_stream_event(&card->rtd[i],
609 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800610 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000611
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800612 snd_soc_dapm_stream_event(&card->rtd[i],
613 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800614 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000615 }
616
Mark Browne2d32ff2012-08-31 17:38:32 -0700617 /* Recheck all analogue paths too */
618 dapm_mark_io_dirty(&card->dapm);
619 snd_soc_dapm_sync(&card->dapm);
620
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200622 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000623 /* If there are paths active then the CODEC will be held with
624 * bias _ON and should not be suspended. */
625 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200626 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000627 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000628 /*
629 * If the CODEC is capable of idle
630 * bias off then being in STANDBY
631 * means it's doing something,
632 * otherwise fall through.
633 */
634 if (codec->dapm.idle_bias_off) {
635 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200636 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000637 break;
638 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100640 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000641 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900642 codec->cache_sync = 1;
Mark Brownda8b8e02012-09-12 12:21:52 +0800643 if (codec->using_regmap)
644 regcache_mark_dirty(codec->control_data);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000645 break;
646 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200647 dev_dbg(codec->dev,
648 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000649 break;
650 }
651 }
652 }
653
654 for (i = 0; i < card->num_rtd; i++) {
655 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
656
657 if (card->rtd[i].dai_link->ignore_suspend)
658 continue;
659
660 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
661 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200662 }
663
Mark Brown87506542008-11-18 20:50:34 +0000664 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000665 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200666
667 return 0;
668}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000669EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200670
Andy Green6ed25972008-06-13 16:24:05 +0100671/* deferred resume work, so resume can complete before we finished
672 * setting our codec back up, which can be very slow on I2C
673 */
674static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200675{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000676 struct snd_soc_card *card =
677 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200678 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200679 int i;
680
Andy Green6ed25972008-06-13 16:24:05 +0100681 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
682 * so userspace apps are blocked from touching us
683 */
684
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000685 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100686
Mark Brown99497882010-05-07 20:24:05 +0100687 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000688 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100689
Mark Brown87506542008-11-18 20:50:34 +0000690 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000691 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200692
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000693 /* resume AC97 DAIs */
694 for (i = 0; i < card->num_rtd; i++) {
695 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100696
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000697 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100698 continue;
699
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000700 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
701 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200702 }
703
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200704 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000705 /* If the CODEC was idle over suspend then it will have been
706 * left with bias OFF or STANDBY and suspended so we must now
707 * resume. Otherwise the suspend was suppressed.
708 */
709 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200710 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000711 case SND_SOC_BIAS_STANDBY:
712 case SND_SOC_BIAS_OFF:
713 codec->driver->resume(codec);
714 codec->suspended = 0;
715 break;
716 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200717 dev_dbg(codec->dev,
718 "ASoC: CODEC was on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000719 break;
720 }
Mark Brown1547aba2010-05-07 21:11:40 +0100721 }
722 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200723
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000724 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100725
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000726 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100727 continue;
728
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800729 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000730 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800731 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000732
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800733 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000734 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800735 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200736 }
737
Mark Brown3ff3f642008-05-19 12:32:25 +0200738 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000739 for (i = 0; i < card->num_rtd; i++) {
740 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
741 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100742
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000743 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100744 continue;
745
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000746 if (drv->ops->digital_mute && dai->playback_active)
747 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200748 }
749
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000750 for (i = 0; i < card->num_rtd; i++) {
751 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
752 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100753
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000754 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100755 continue;
756
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000757 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
758 cpu_dai->driver->resume(cpu_dai);
759 if (platform->driver->resume && platform->suspended) {
760 platform->driver->resume(cpu_dai);
761 platform->suspended = 0;
762 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200763 }
764
Mark Brown87506542008-11-18 20:50:34 +0000765 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000766 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200767
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000768 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100769
770 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000771 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700772
773 /* Recheck all analogue paths too */
774 dapm_mark_io_dirty(&card->dapm);
775 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100776}
777
778/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000779int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100780{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000781 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600782 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200783
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800784 /* If the initialization of this soc device failed, there is no codec
785 * associated with it. Just bail out in this case.
786 */
787 if (list_empty(&card->codec_dev_list))
788 return 0;
789
Mark Brown64ab9ba2009-03-31 11:27:03 +0100790 /* AC97 devices might have other drivers hanging off them so
791 * need to resume immediately. Other drivers don't have that
792 * problem and may take a substantial amount of time to resume
793 * due to I/O costs and anti-pop so handle them out of line.
794 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000795 for (i = 0; i < card->num_rtd; i++) {
796 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600797 ac97_control |= cpu_dai->driver->ac97_control;
798 }
799 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000800 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600801 soc_resume_deferred(&card->deferred_resume_work);
802 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000803 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600804 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000805 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100806 }
Andy Green6ed25972008-06-13 16:24:05 +0100807
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200808 return 0;
809}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000810EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200811#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000812#define snd_soc_suspend NULL
813#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200814#endif
815
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100816static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800817};
818
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000819static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200820{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000821 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
822 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000823 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000824 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000825 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100826 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200827
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000828 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000829
Mark Brownb19e6e72012-03-14 21:18:39 +0000830 /* Find CPU DAI from registered DAIs*/
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000831 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600832 if (dai_link->cpu_of_node &&
833 (cpu_dai->dev->of_node != dai_link->cpu_of_node))
834 continue;
835 if (dai_link->cpu_name &&
836 strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
837 continue;
838 if (dai_link->cpu_dai_name &&
839 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
840 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700841
842 rtd->cpu_dai = cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000843 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000844
845 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000846 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000847 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000848 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000849 }
850
Mark Brownb19e6e72012-03-14 21:18:39 +0000851 /* Find CODEC from registered CODECs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000852 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700853 if (dai_link->codec_of_node) {
854 if (codec->dev->of_node != dai_link->codec_of_node)
855 continue;
856 } else {
857 if (strcmp(codec->name, dai_link->codec_name))
858 continue;
859 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000860
Stephen Warren2610ab72011-12-07 13:58:27 -0700861 rtd->codec = codec;
862
863 /*
864 * CODEC found, so find CODEC DAI from registered DAIs from
865 * this CODEC
866 */
867 list_for_each_entry(codec_dai, &dai_list, list) {
868 if (codec->dev == codec_dai->dev &&
869 !strcmp(codec_dai->name,
870 dai_link->codec_dai_name)) {
871
872 rtd->codec_dai = codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000873 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000874 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000875
876 if (!rtd->codec_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000877 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
Stephen Warren2610ab72011-12-07 13:58:27 -0700878 dai_link->codec_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000879 return -EPROBE_DEFER;
880 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000881 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000882
Mark Brownb19e6e72012-03-14 21:18:39 +0000883 if (!rtd->codec) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000884 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
Mark Brownb19e6e72012-03-14 21:18:39 +0000885 dai_link->codec_name);
886 return -EPROBE_DEFER;
887 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100888
889 /* if there's no platform we match on the empty platform */
890 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700891 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100892 platform_name = "snd-soc-dummy";
893
Mark Brownb19e6e72012-03-14 21:18:39 +0000894 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000895 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700896 if (dai_link->platform_of_node) {
897 if (platform->dev->of_node !=
898 dai_link->platform_of_node)
899 continue;
900 } else {
901 if (strcmp(platform->name, platform_name))
902 continue;
903 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700904
905 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000906 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000907 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000908 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000909 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000910 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000911 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000912
913 card->num_rtd++;
914
915 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000916}
917
Stephen Warrend12cd192012-06-08 12:34:22 -0600918static int soc_remove_platform(struct snd_soc_platform *platform)
919{
920 int ret;
921
922 if (platform->driver->remove) {
923 ret = platform->driver->remove(platform);
924 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000925 dev_err(platform->dev, "ASoC: failed to remove %d\n",
926 ret);
Stephen Warrend12cd192012-06-08 12:34:22 -0600927 }
928
929 /* Make sure all DAPM widgets are freed */
930 snd_soc_dapm_free(&platform->dapm);
931
932 soc_cleanup_platform_debugfs(platform);
933 platform->probed = 0;
934 list_del(&platform->card_list);
935 module_put(platform->dev->driver->owner);
936
937 return 0;
938}
939
Jarkko Nikula589c3562010-12-06 16:27:07 +0200940static void soc_remove_codec(struct snd_soc_codec *codec)
941{
942 int err;
943
944 if (codec->driver->remove) {
945 err = codec->driver->remove(codec);
946 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000947 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200948 }
949
950 /* Make sure all DAPM widgets are freed */
951 snd_soc_dapm_free(&codec->dapm);
952
953 soc_cleanup_codec_debugfs(codec);
954 codec->probed = 0;
955 list_del(&codec->card_list);
956 module_put(codec->dev->driver->owner);
957}
958
Stephen Warren62ae68f2012-06-08 12:34:23 -0600959static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000960{
961 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000962 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
963 int err;
964
965 /* unregister the rtd device */
966 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800967 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
968 device_remove_file(rtd->dev, &dev_attr_codec_reg);
969 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000970 rtd->dev_registered = 0;
971 }
972
973 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100974 if (codec_dai && codec_dai->probed &&
975 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000976 if (codec_dai->driver->remove) {
977 err = codec_dai->driver->remove(codec_dai);
978 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000979 dev_err(codec_dai->dev,
980 "ASoC: failed to remove %s: %d\n",
981 codec_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000982 }
983 codec_dai->probed = 0;
984 list_del(&codec_dai->card_list);
985 }
986
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000987 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100988 if (cpu_dai && cpu_dai->probed &&
989 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000990 if (cpu_dai->driver->remove) {
991 err = cpu_dai->driver->remove(cpu_dai);
992 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000993 dev_err(cpu_dai->dev,
994 "ASoC: failed to remove %s: %d\n",
995 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000996 }
997 cpu_dai->probed = 0;
998 list_del(&cpu_dai->card_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -0600999
Stephen Warren18d75642012-06-08 12:34:21 -06001000 if (!cpu_dai->codec) {
1001 snd_soc_dapm_free(&cpu_dai->dapm);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001002 module_put(cpu_dai->dev->driver->owner);
Stephen Warren18d75642012-06-08 12:34:21 -06001003 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001004 }
1005}
1006
Stephen Warren62ae68f2012-06-08 12:34:23 -06001007static void soc_remove_link_components(struct snd_soc_card *card, int num,
1008 int order)
1009{
1010 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1011 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1012 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1013 struct snd_soc_platform *platform = rtd->platform;
1014 struct snd_soc_codec *codec;
1015
1016 /* remove the platform */
1017 if (platform && platform->probed &&
1018 platform->driver->remove_order == order) {
1019 soc_remove_platform(platform);
1020 }
1021
1022 /* remove the CODEC-side CODEC */
1023 if (codec_dai) {
1024 codec = codec_dai->codec;
1025 if (codec && codec->probed &&
1026 codec->driver->remove_order == order)
1027 soc_remove_codec(codec);
1028 }
1029
1030 /* remove any CPU-side CODEC */
1031 if (cpu_dai) {
1032 codec = cpu_dai->codec;
1033 if (codec && codec->probed &&
1034 codec->driver->remove_order == order)
1035 soc_remove_codec(codec);
1036 }
1037}
1038
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001039static void soc_remove_dai_links(struct snd_soc_card *card)
1040{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001041 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001042
Liam Girdwood0168bf02011-06-07 16:08:05 +01001043 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1044 order++) {
1045 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001046 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001047 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001048
1049 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1050 order++) {
1051 for (dai = 0; dai < card->num_rtd; dai++)
1052 soc_remove_link_components(card, dai, order);
1053 }
1054
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001055 card->num_rtd = 0;
1056}
1057
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001058static void soc_set_name_prefix(struct snd_soc_card *card,
1059 struct snd_soc_codec *codec)
1060{
1061 int i;
1062
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001063 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001064 return;
1065
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001066 for (i = 0; i < card->num_configs; i++) {
1067 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001068 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1069 codec->name_prefix = map->name_prefix;
1070 break;
1071 }
1072 }
1073}
1074
Jarkko Nikula589c3562010-12-06 16:27:07 +02001075static int soc_probe_codec(struct snd_soc_card *card,
1076 struct snd_soc_codec *codec)
1077{
1078 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001079 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001080 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001081
1082 codec->card = card;
1083 codec->dapm.card = card;
1084 soc_set_name_prefix(card, codec);
1085
Jarkko Nikula70d29332011-01-27 16:24:22 +02001086 if (!try_module_get(codec->dev->driver->owner))
1087 return -ENODEV;
1088
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001089 soc_init_codec_debugfs(codec);
1090
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001091 if (driver->dapm_widgets)
1092 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1093 driver->num_dapm_widgets);
1094
Mark Brown888df392012-02-16 19:37:51 -08001095 /* Create DAPM widgets for each DAI stream */
1096 list_for_each_entry(dai, &dai_list, list) {
1097 if (dai->dev != codec->dev)
1098 continue;
1099
1100 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1101 }
1102
Mark Brown33c5f962011-08-22 18:40:30 +01001103 codec->dapm.idle_bias_off = driver->idle_bias_off;
1104
Mark Brown89b95ac02011-03-07 16:38:44 +00001105 if (driver->probe) {
1106 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001107 if (ret < 0) {
1108 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001109 "ASoC: failed to probe CODEC %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001110 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001111 }
Chuansheng Liuff541f42012-12-21 18:17:12 +08001112 WARN(codec->dapm.idle_bias_off &&
1113 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001114 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1115 codec->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001116 }
1117
Mark Brown38cbf952012-06-22 13:04:02 +01001118 /* If the driver didn't set I/O up try regmap */
Mark Brown98d30882012-08-01 20:05:47 +01001119 if (!codec->write && dev_get_regmap(codec->dev, NULL))
Mark Brown38cbf952012-06-22 13:04:02 +01001120 snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
1121
Mark Brownb7af1da2011-04-07 19:18:44 +09001122 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001123 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001124 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001125 if (driver->dapm_routes)
1126 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1127 driver->num_dapm_routes);
1128
Jarkko Nikula589c3562010-12-06 16:27:07 +02001129 /* mark codec as probed and add to card codec list */
1130 codec->probed = 1;
1131 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001132 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001133
Jarkko Nikula70d29332011-01-27 16:24:22 +02001134 return 0;
1135
1136err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001137 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001138 module_put(codec->dev->driver->owner);
1139
Jarkko Nikula589c3562010-12-06 16:27:07 +02001140 return ret;
1141}
1142
Liam Girdwood956245e2011-07-01 16:54:08 +01001143static int soc_probe_platform(struct snd_soc_card *card,
1144 struct snd_soc_platform *platform)
1145{
1146 int ret = 0;
1147 const struct snd_soc_platform_driver *driver = platform->driver;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001148 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001149
1150 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001151 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001152
1153 if (!try_module_get(platform->dev->driver->owner))
1154 return -ENODEV;
1155
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001156 soc_init_platform_debugfs(platform);
1157
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001158 if (driver->dapm_widgets)
1159 snd_soc_dapm_new_controls(&platform->dapm,
1160 driver->dapm_widgets, driver->num_dapm_widgets);
1161
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001162 /* Create DAPM widgets for each DAI stream */
1163 list_for_each_entry(dai, &dai_list, list) {
1164 if (dai->dev != platform->dev)
1165 continue;
1166
1167 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1168 }
1169
Stephen Warren3fec6b62012-04-05 12:28:01 -06001170 platform->dapm.idle_bias_off = 1;
1171
Liam Girdwood956245e2011-07-01 16:54:08 +01001172 if (driver->probe) {
1173 ret = driver->probe(platform);
1174 if (ret < 0) {
1175 dev_err(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001176 "ASoC: failed to probe platform %d\n", ret);
Liam Girdwood956245e2011-07-01 16:54:08 +01001177 goto err_probe;
1178 }
1179 }
1180
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001181 if (driver->controls)
1182 snd_soc_add_platform_controls(platform, driver->controls,
1183 driver->num_controls);
1184 if (driver->dapm_routes)
1185 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1186 driver->num_dapm_routes);
1187
Liam Girdwood956245e2011-07-01 16:54:08 +01001188 /* mark platform as probed and add to card platform list */
1189 platform->probed = 1;
1190 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001191 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001192
1193 return 0;
1194
1195err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001196 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001197 module_put(platform->dev->driver->owner);
1198
1199 return ret;
1200}
1201
Mark Brown36ae1a92012-01-06 17:12:45 -08001202static void rtd_release(struct device *dev)
1203{
1204 kfree(dev);
1205}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001206
Jarkko Nikula589c3562010-12-06 16:27:07 +02001207static int soc_post_component_init(struct snd_soc_card *card,
1208 struct snd_soc_codec *codec,
1209 int num, int dailess)
1210{
1211 struct snd_soc_dai_link *dai_link = NULL;
1212 struct snd_soc_aux_dev *aux_dev = NULL;
1213 struct snd_soc_pcm_runtime *rtd;
1214 const char *temp, *name;
1215 int ret = 0;
1216
1217 if (!dailess) {
1218 dai_link = &card->dai_link[num];
1219 rtd = &card->rtd[num];
1220 name = dai_link->name;
1221 } else {
1222 aux_dev = &card->aux_dev[num];
1223 rtd = &card->rtd_aux[num];
1224 name = aux_dev->name;
1225 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001226 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001227
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001228 /* Make sure all DAPM widgets are instantiated */
1229 snd_soc_dapm_new_widgets(&codec->dapm);
1230
Jarkko Nikula589c3562010-12-06 16:27:07 +02001231 /* machine controls, routes and widgets are not prefixed */
1232 temp = codec->name_prefix;
1233 codec->name_prefix = NULL;
1234
1235 /* do machine specific initialization */
1236 if (!dailess && dai_link->init)
1237 ret = dai_link->init(rtd);
1238 else if (dailess && aux_dev->init)
1239 ret = aux_dev->init(&codec->dapm);
1240 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001241 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001242 return ret;
1243 }
1244 codec->name_prefix = temp;
1245
Jarkko Nikula589c3562010-12-06 16:27:07 +02001246 /* register the rtd device */
1247 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001248
1249 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1250 if (!rtd->dev)
1251 return -ENOMEM;
1252 device_initialize(rtd->dev);
1253 rtd->dev->parent = card->dev;
1254 rtd->dev->release = rtd_release;
1255 rtd->dev->init_name = name;
1256 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001257 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001258 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1259 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1260 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1261 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001262 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001263 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001264 /* calling put_device() here to free the rtd->dev */
1265 put_device(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001266 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001267 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001268 return ret;
1269 }
1270 rtd->dev_registered = 1;
1271
1272 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001273 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001274 if (ret < 0)
1275 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001276 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001277
1278 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001279 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001280 if (ret < 0)
1281 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001282 "ASoC: failed to add codec sysfs files: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001283
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001284#ifdef CONFIG_DEBUG_FS
1285 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001286 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001287 goto out;
1288
1289 ret = soc_dpcm_debugfs_add(rtd);
1290 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001291 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001292
1293out:
1294#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001295 return 0;
1296}
1297
Stephen Warren62ae68f2012-06-08 12:34:23 -06001298static int soc_probe_link_components(struct snd_soc_card *card, int num,
1299 int order)
1300{
1301 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1302 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1303 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1304 struct snd_soc_platform *platform = rtd->platform;
1305 int ret;
1306
1307 /* probe the CPU-side component, if it is a CODEC */
1308 if (cpu_dai->codec &&
1309 !cpu_dai->codec->probed &&
1310 cpu_dai->codec->driver->probe_order == order) {
1311 ret = soc_probe_codec(card, cpu_dai->codec);
1312 if (ret < 0)
1313 return ret;
1314 }
1315
1316 /* probe the CODEC-side component */
1317 if (!codec_dai->codec->probed &&
1318 codec_dai->codec->driver->probe_order == order) {
1319 ret = soc_probe_codec(card, codec_dai->codec);
1320 if (ret < 0)
1321 return ret;
1322 }
1323
1324 /* probe the platform */
1325 if (!platform->probed &&
1326 platform->driver->probe_order == order) {
1327 ret = soc_probe_platform(card, platform);
1328 if (ret < 0)
1329 return ret;
1330 }
1331
1332 return 0;
1333}
1334
1335static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001336{
1337 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1338 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1339 struct snd_soc_codec *codec = rtd->codec;
1340 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001341 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1342 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1343 struct snd_soc_dapm_widget *play_w, *capture_w;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001344 int ret;
1345
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001346 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001347 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001348
1349 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001350 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001351 codec_dai->card = card;
1352 cpu_dai->card = card;
1353
1354 /* set default power off timeout */
1355 rtd->pmdown_time = pmdown_time;
1356
1357 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001358 if (!cpu_dai->probed &&
1359 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001360 if (!cpu_dai->codec) {
1361 cpu_dai->dapm.card = card;
1362 if (!try_module_get(cpu_dai->dev->driver->owner))
1363 return -ENODEV;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001364
Stephen Warren18d75642012-06-08 12:34:21 -06001365 list_add(&cpu_dai->dapm.list, &card->dapm_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001366 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1367 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001368
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001369 if (cpu_dai->driver->probe) {
1370 ret = cpu_dai->driver->probe(cpu_dai);
1371 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001372 dev_err(cpu_dai->dev,
1373 "ASoC: failed to probe CPU DAI %s: %d\n",
1374 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001375 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001376 return ret;
1377 }
1378 }
1379 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001380 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001381 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1382 }
1383
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001384 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001385 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001386 if (codec_dai->driver->probe) {
1387 ret = codec_dai->driver->probe(codec_dai);
1388 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001389 dev_err(codec_dai->dev,
1390 "ASoC: failed to probe CODEC DAI %s: %d\n",
1391 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001392 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001393 }
1394 }
1395
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001396 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001397 codec_dai->probed = 1;
1398 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001399 }
1400
Liam Girdwood0168bf02011-06-07 16:08:05 +01001401 /* complete DAI probe during last probe */
1402 if (order != SND_SOC_COMP_ORDER_LAST)
1403 return 0;
1404
Jarkko Nikula589c3562010-12-06 16:27:07 +02001405 ret = soc_post_component_init(card, codec, num, 0);
1406 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001407 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001408
Mark Brown36ae1a92012-01-06 17:12:45 -08001409 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001410 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001411 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1412 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001413
Namarta Kohli1245b702012-08-16 17:10:41 +05301414 if (cpu_dai->driver->compress_dai) {
1415 /*create compress_device"*/
1416 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001417 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001418 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301419 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001420 return ret;
1421 }
1422 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301423
1424 if (!dai_link->params) {
1425 /* create the pcm */
1426 ret = soc_new_pcm(rtd, num);
1427 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001428 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301429 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001430 return ret;
1431 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301432 } else {
1433 /* link the DAI widgets */
1434 play_w = codec_dai->playback_widget;
1435 capture_w = cpu_dai->capture_widget;
1436 if (play_w && capture_w) {
1437 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Mark Brownc74184e2012-04-04 22:12:09 +01001438 capture_w, play_w);
Namarta Kohli1245b702012-08-16 17:10:41 +05301439 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001440 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301441 play_w->name, capture_w->name, ret);
1442 return ret;
1443 }
1444 }
1445
1446 play_w = cpu_dai->playback_widget;
1447 capture_w = codec_dai->capture_widget;
1448 if (play_w && capture_w) {
1449 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1450 capture_w, play_w);
1451 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001452 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301453 play_w->name, capture_w->name, ret);
1454 return ret;
1455 }
Mark Brownc74184e2012-04-04 22:12:09 +01001456 }
1457 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001458 }
1459
1460 /* add platform data for AC97 devices */
1461 if (rtd->codec_dai->driver->ac97_control)
1462 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1463
1464 return 0;
1465}
1466
1467#ifdef CONFIG_SND_SOC_AC97_BUS
1468static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1469{
1470 int ret;
1471
1472 /* Only instantiate AC97 if not already done by the adaptor
1473 * for the generic AC97 subsystem.
1474 */
1475 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001476 /*
1477 * It is possible that the AC97 device is already registered to
1478 * the device subsystem. This happens when the device is created
1479 * via snd_ac97_mixer(). Currently only SoC codec that does so
1480 * is the generic AC97 glue but others migh emerge.
1481 *
1482 * In those cases we don't try to register the device again.
1483 */
1484 if (!rtd->codec->ac97_created)
1485 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001486
1487 ret = soc_ac97_dev_register(rtd->codec);
1488 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001489 dev_err(rtd->codec->dev,
1490 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001491 return ret;
1492 }
1493
1494 rtd->codec->ac97_registered = 1;
1495 }
1496 return 0;
1497}
1498
1499static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1500{
1501 if (codec->ac97_registered) {
1502 soc_ac97_dev_unregister(codec);
1503 codec->ac97_registered = 0;
1504 }
1505}
1506#endif
1507
Mark Brownb19e6e72012-03-14 21:18:39 +00001508static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1509{
1510 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1511 struct snd_soc_codec *codec;
1512
1513 /* find CODEC from registered CODECs*/
1514 list_for_each_entry(codec, &codec_list, list) {
1515 if (!strcmp(codec->name, aux_dev->codec_name))
1516 return 0;
1517 }
1518
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001519 dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
Mark Brownfb099cb2012-08-09 18:44:37 +01001520
Mark Brownb19e6e72012-03-14 21:18:39 +00001521 return -EPROBE_DEFER;
1522}
1523
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001524static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1525{
1526 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001527 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001528 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001529
1530 /* find CODEC from registered CODECs*/
1531 list_for_each_entry(codec, &codec_list, list) {
1532 if (!strcmp(codec->name, aux_dev->codec_name)) {
1533 if (codec->probed) {
1534 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001535 "ASoC: codec already probed");
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001536 ret = -EBUSY;
1537 goto out;
1538 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001539 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001540 }
1541 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001542 /* codec not found */
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001543 dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001544 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001545
Jarkko Nikula676ad982010-12-03 09:18:22 +02001546found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001547 ret = soc_probe_codec(card, codec);
1548 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001549 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001550
Jarkko Nikula589c3562010-12-06 16:27:07 +02001551 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001552
1553out:
1554 return ret;
1555}
1556
1557static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1558{
1559 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1560 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001561
1562 /* unregister the rtd device */
1563 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001564 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001565 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001566 rtd->dev_registered = 0;
1567 }
1568
Jarkko Nikula589c3562010-12-06 16:27:07 +02001569 if (codec && codec->probed)
1570 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001571}
1572
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001573static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1574 enum snd_soc_compress_type compress_type)
1575{
1576 int ret;
1577
1578 if (codec->cache_init)
1579 return 0;
1580
1581 /* override the compress_type if necessary */
1582 if (compress_type && codec->compress_type != compress_type)
1583 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001584 ret = snd_soc_cache_init(codec);
1585 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001586 dev_err(codec->dev,
1587 "ASoC: Failed to set cache compression type: %d\n",
1588 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001589 return ret;
1590 }
1591 codec->cache_init = 1;
1592 return 0;
1593}
1594
Mark Brownb19e6e72012-03-14 21:18:39 +00001595static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001596{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001597 struct snd_soc_codec *codec;
1598 struct snd_soc_codec_conf *codec_conf;
1599 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001600 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001601 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001602
Liam Girdwood01b9d992012-03-07 10:38:25 +00001603 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001604
Mark Brownb19e6e72012-03-14 21:18:39 +00001605 /* bind DAIs */
1606 for (i = 0; i < card->num_links; i++) {
1607 ret = soc_bind_dai_link(card, i);
1608 if (ret != 0)
1609 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001610 }
1611
Mark Brownb19e6e72012-03-14 21:18:39 +00001612 /* check aux_devs too */
1613 for (i = 0; i < card->num_aux_devs; i++) {
1614 ret = soc_check_aux_dev(card, i);
1615 if (ret != 0)
1616 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001617 }
1618
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001619 /* initialize the register cache for each available codec */
1620 list_for_each_entry(codec, &codec_list, list) {
1621 if (codec->cache_init)
1622 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001623 /* by default we don't override the compress_type */
1624 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001625 /* check to see if we need to override the compress_type */
1626 for (i = 0; i < card->num_configs; ++i) {
1627 codec_conf = &card->codec_conf[i];
1628 if (!strcmp(codec->name, codec_conf->dev_name)) {
1629 compress_type = codec_conf->compress_type;
1630 if (compress_type && compress_type
1631 != codec->compress_type)
1632 break;
1633 }
1634 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001635 ret = snd_soc_init_codec_cache(codec, compress_type);
Mark Brownb19e6e72012-03-14 21:18:39 +00001636 if (ret < 0)
1637 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001638 }
1639
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001640 /* card bind complete so register a sound card */
1641 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1642 card->owner, 0, &card->snd_card);
1643 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001644 dev_err(card->dev,
1645 "ASoC: can't create sound card for card %s: %d\n",
1646 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001647 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001648 }
1649 card->snd_card->dev = card->dev;
1650
Mark Browne37a4972011-03-02 18:21:57 +00001651 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1652 card->dapm.dev = card->dev;
1653 card->dapm.card = card;
1654 list_add(&card->dapm.list, &card->dapm_list);
1655
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001656#ifdef CONFIG_DEBUG_FS
1657 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1658#endif
1659
Mark Brown88ee1c62011-02-02 10:43:26 +00001660#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001661 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001662 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001663#endif
Andy Green6ed25972008-06-13 16:24:05 +01001664
Mark Brown9a841eb2011-04-12 17:51:37 -07001665 if (card->dapm_widgets)
1666 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1667 card->num_dapm_widgets);
1668
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001669 /* initialise the sound card only once */
1670 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001671 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001672 if (ret < 0)
1673 goto card_probe_error;
1674 }
1675
Stephen Warren62ae68f2012-06-08 12:34:23 -06001676 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001677 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1678 order++) {
1679 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001680 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001681 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001682 dev_err(card->dev,
1683 "ASoC: failed to instantiate card %d\n",
1684 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001685 goto probe_dai_err;
1686 }
1687 }
1688 }
1689
1690 /* probe all DAI links on this card */
1691 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1692 order++) {
1693 for (i = 0; i < card->num_links; i++) {
1694 ret = soc_probe_link_dais(card, i, order);
1695 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001696 dev_err(card->dev,
1697 "ASoC: failed to instantiate card %d\n",
1698 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001699 goto probe_dai_err;
1700 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001701 }
1702 }
1703
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001704 for (i = 0; i < card->num_aux_devs; i++) {
1705 ret = soc_probe_aux_dev(card, i);
1706 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001707 dev_err(card->dev,
1708 "ASoC: failed to add auxiliary devices %d\n",
1709 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001710 goto probe_aux_dev_err;
1711 }
1712 }
1713
Mark Brown888df392012-02-16 19:37:51 -08001714 snd_soc_dapm_link_dai_widgets(card);
1715
Mark Brownb7af1da2011-04-07 19:18:44 +09001716 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001717 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001718
Mark Brownb8ad29d2011-03-02 18:35:51 +00001719 if (card->dapm_routes)
1720 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1721 card->num_dapm_routes);
1722
Mark Brownb90d2f92011-10-10 13:38:06 +01001723 snd_soc_dapm_new_widgets(&card->dapm);
1724
Mark Brown75d9ac42011-09-27 16:41:01 +01001725 for (i = 0; i < card->num_links; i++) {
1726 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001727 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001728
Mark Brownf04209a2012-04-09 16:29:21 +01001729 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001730 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001731 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001732 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001733 dev_warn(card->rtd[i].codec_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001734 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001735 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001736 }
1737
1738 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001739 if (dai_fmt &&
1740 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001741 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1742 dai_fmt);
1743 if (ret != 0 && ret != -ENOTSUPP)
1744 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001745 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001746 ret);
1747 } else if (dai_fmt) {
1748 /* Flip the polarity for the "CPU" end */
1749 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1750 switch (dai_link->dai_fmt &
1751 SND_SOC_DAIFMT_MASTER_MASK) {
1752 case SND_SOC_DAIFMT_CBM_CFM:
1753 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1754 break;
1755 case SND_SOC_DAIFMT_CBM_CFS:
1756 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1757 break;
1758 case SND_SOC_DAIFMT_CBS_CFM:
1759 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1760 break;
1761 case SND_SOC_DAIFMT_CBS_CFS:
1762 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1763 break;
1764 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001765
1766 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001767 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001768 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001769 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001770 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001771 ret);
1772 }
1773 }
1774
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001775 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001776 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001777 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1778 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001779 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1780 "%s", card->driver_name ? card->driver_name : card->name);
1781 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1782 switch (card->snd_card->driver[i]) {
1783 case '_':
1784 case '-':
1785 case '\0':
1786 break;
1787 default:
1788 if (!isalnum(card->snd_card->driver[i]))
1789 card->snd_card->driver[i] = '_';
1790 break;
1791 }
1792 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001793
Mark Brown28e9ad92011-03-02 18:36:34 +00001794 if (card->late_probe) {
1795 ret = card->late_probe(card);
1796 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001797 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001798 card->name, ret);
1799 goto probe_aux_dev_err;
1800 }
1801 }
1802
Mark Brown2dc00212011-10-08 13:59:44 +01001803 snd_soc_dapm_new_widgets(&card->dapm);
1804
Stephen Warren16332812011-11-23 12:42:04 -07001805 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001806 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001807 snd_soc_dapm_auto_nc_codec_pins(codec);
1808
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001809 ret = snd_card_register(card->snd_card);
1810 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001811 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1812 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001813 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001814 }
1815
1816#ifdef CONFIG_SND_SOC_AC97_BUS
1817 /* register any AC97 codecs */
1818 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001819 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1820 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001821 dev_err(card->dev,
1822 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001823 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001824 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001825 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001826 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001827 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001828#endif
1829
Mark Brown435c5e22008-12-04 15:32:53 +00001830 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001831 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001832 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001833
1834 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001835
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001836probe_aux_dev_err:
1837 for (i = 0; i < card->num_aux_devs; i++)
1838 soc_remove_aux_dev(card, i);
1839
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001840probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001841 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001842
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001843card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001844 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001845 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001846
1847 snd_card_free(card->snd_card);
1848
Mark Brownb19e6e72012-03-14 21:18:39 +00001849base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001850 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001851
Mark Brownb19e6e72012-03-14 21:18:39 +00001852 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001853}
1854
1855/* probes a new socdev */
1856static int soc_probe(struct platform_device *pdev)
1857{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001858 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001859
Vinod Koul70a7ca32011-01-14 19:22:48 +05301860 /*
1861 * no card, so machine driver should be registering card
1862 * we should not be here in that case so ret error
1863 */
1864 if (!card)
1865 return -EINVAL;
1866
Mark Brownfe4085e2012-03-02 13:07:41 +00001867 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001868 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001869 card->name);
1870
Mark Brown435c5e22008-12-04 15:32:53 +00001871 /* Bodge while we unpick instantiation */
1872 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001873
Mark Brown28d528c2012-08-09 18:45:23 +01001874 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001875}
1876
Vinod Koulb0e26482011-01-13 22:48:02 +05301877static int soc_cleanup_card_resources(struct snd_soc_card *card)
1878{
Vinod Koulb0e26482011-01-13 22:48:02 +05301879 int i;
1880
1881 /* make sure any delayed work runs */
1882 for (i = 0; i < card->num_rtd; i++) {
1883 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001884 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301885 }
1886
1887 /* remove auxiliary devices */
1888 for (i = 0; i < card->num_aux_devs; i++)
1889 soc_remove_aux_dev(card, i);
1890
1891 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001892 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301893
1894 soc_cleanup_card_debugfs(card);
1895
1896 /* remove the card */
1897 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001898 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301899
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001900 snd_soc_dapm_free(&card->dapm);
1901
Vinod Koulb0e26482011-01-13 22:48:02 +05301902 snd_card_free(card->snd_card);
1903 return 0;
1904
1905}
1906
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001907/* removes a socdev */
1908static int soc_remove(struct platform_device *pdev)
1909{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001910 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001911
Mark Brownc5af3a22008-11-28 13:29:45 +00001912 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001913 return 0;
1914}
1915
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001916int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001917{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001918 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001919 int i;
Mark Brown51737472009-06-22 13:16:51 +01001920
1921 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001922 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001923
1924 /* Flush out pmdown_time work - we actually do want to run it
1925 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001926 for (i = 0; i < card->num_rtd; i++) {
1927 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001928 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001929 }
Mark Brown51737472009-06-22 13:16:51 +01001930
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001931 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001932
1933 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001934}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001935EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001936
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001937const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301938 .suspend = snd_soc_suspend,
1939 .resume = snd_soc_resume,
1940 .freeze = snd_soc_suspend,
1941 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001942 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301943 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001944};
Stephen Warrendeb26072011-04-05 19:35:30 -06001945EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001946
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001947/* ASoC platform driver */
1948static struct platform_driver soc_driver = {
1949 .driver = {
1950 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001951 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001952 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001953 },
1954 .probe = soc_probe,
1955 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001956};
1957
Mark Brown096e49d2009-07-05 15:12:22 +01001958/**
1959 * snd_soc_codec_volatile_register: Report if a register is volatile.
1960 *
1961 * @codec: CODEC to query.
1962 * @reg: Register to query.
1963 *
1964 * Boolean function indiciating if a CODEC register is volatile.
1965 */
Mark Brown181e0552011-01-24 14:05:25 +00001966int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1967 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001968{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001969 if (codec->volatile_register)
1970 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001971 else
1972 return 0;
1973}
1974EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1975
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001976/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001977 * snd_soc_codec_readable_register: Report if a register is readable.
1978 *
1979 * @codec: CODEC to query.
1980 * @reg: Register to query.
1981 *
1982 * Boolean function indicating if a CODEC register is readable.
1983 */
1984int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1985 unsigned int reg)
1986{
1987 if (codec->readable_register)
1988 return codec->readable_register(codec, reg);
1989 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001990 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001991}
1992EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1993
1994/**
1995 * snd_soc_codec_writable_register: Report if a register is writable.
1996 *
1997 * @codec: CODEC to query.
1998 * @reg: Register to query.
1999 *
2000 * Boolean function indicating if a CODEC register is writable.
2001 */
2002int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
2003 unsigned int reg)
2004{
2005 if (codec->writable_register)
2006 return codec->writable_register(codec, reg);
2007 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02002008 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002009}
2010EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2011
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002012int snd_soc_platform_read(struct snd_soc_platform *platform,
2013 unsigned int reg)
2014{
2015 unsigned int ret;
2016
2017 if (!platform->driver->read) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002018 dev_err(platform->dev, "ASoC: platform has no read back\n");
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002019 return -1;
2020 }
2021
2022 ret = platform->driver->read(platform, reg);
2023 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002024 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002025
2026 return ret;
2027}
2028EXPORT_SYMBOL_GPL(snd_soc_platform_read);
2029
2030int snd_soc_platform_write(struct snd_soc_platform *platform,
2031 unsigned int reg, unsigned int val)
2032{
2033 if (!platform->driver->write) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002034 dev_err(platform->dev, "ASoC: platform has no write back\n");
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002035 return -1;
2036 }
2037
2038 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002039 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002040 return platform->driver->write(platform, reg, val);
2041}
2042EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2043
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002044/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002045 * snd_soc_new_ac97_codec - initailise AC97 device
2046 * @codec: audio codec
2047 * @ops: AC97 bus operations
2048 * @num: AC97 codec number
2049 *
2050 * Initialises AC97 codec resources for use by ad-hoc devices only.
2051 */
2052int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2053 struct snd_ac97_bus_ops *ops, int num)
2054{
2055 mutex_lock(&codec->mutex);
2056
2057 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2058 if (codec->ac97 == NULL) {
2059 mutex_unlock(&codec->mutex);
2060 return -ENOMEM;
2061 }
2062
2063 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2064 if (codec->ac97->bus == NULL) {
2065 kfree(codec->ac97);
2066 codec->ac97 = NULL;
2067 mutex_unlock(&codec->mutex);
2068 return -ENOMEM;
2069 }
2070
2071 codec->ac97->bus->ops = ops;
2072 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002073
2074 /*
2075 * Mark the AC97 device to be created by us. This way we ensure that the
2076 * device will be registered with the device subsystem later on.
2077 */
2078 codec->ac97_created = 1;
2079
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002080 mutex_unlock(&codec->mutex);
2081 return 0;
2082}
2083EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2084
Mark Brownb047e1c2013-06-26 12:45:59 +01002085struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002086EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002087
2088int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2089{
2090 if (ops == soc_ac97_ops)
2091 return 0;
2092
2093 if (soc_ac97_ops && ops)
2094 return -EBUSY;
2095
2096 soc_ac97_ops = ops;
2097
2098 return 0;
2099}
2100EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2101
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002102/**
2103 * snd_soc_free_ac97_codec - free AC97 codec device
2104 * @codec: audio codec
2105 *
2106 * Frees AC97 codec device resources.
2107 */
2108void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2109{
2110 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002111#ifdef CONFIG_SND_SOC_AC97_BUS
2112 soc_unregister_ac97_dai_link(codec);
2113#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002114 kfree(codec->ac97->bus);
2115 kfree(codec->ac97);
2116 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002117 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002118 mutex_unlock(&codec->mutex);
2119}
2120EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2121
Mark Brownc3753702010-11-01 15:41:57 -04002122unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2123{
2124 unsigned int ret;
2125
Mark Brownc3acec22010-12-02 16:15:29 +00002126 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002127 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002128 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002129
2130 return ret;
2131}
2132EXPORT_SYMBOL_GPL(snd_soc_read);
2133
2134unsigned int snd_soc_write(struct snd_soc_codec *codec,
2135 unsigned int reg, unsigned int val)
2136{
2137 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002138 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002139 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002140}
2141EXPORT_SYMBOL_GPL(snd_soc_write);
2142
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00002143unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2144 unsigned int reg, const void *data, size_t len)
2145{
2146 return codec->bulk_write_raw(codec, reg, data, len);
2147}
2148EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2149
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002150/**
2151 * snd_soc_update_bits - update codec register bits
2152 * @codec: audio codec
2153 * @reg: codec register
2154 * @mask: register mask
2155 * @value: new value
2156 *
2157 * Writes new register value.
2158 *
Timur Tabi180c3292011-01-10 15:58:13 -06002159 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002160 */
2161int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002162 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002163{
Mark Brown8a713da2011-12-03 12:33:55 +00002164 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002165 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002166 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002167
Mark Brown8a713da2011-12-03 12:33:55 +00002168 if (codec->using_regmap) {
2169 ret = regmap_update_bits_check(codec->control_data, reg,
2170 mask, value, &change);
2171 } else {
2172 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06002173 if (ret < 0)
2174 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00002175
2176 old = ret;
2177 new = (old & ~mask) | (value & mask);
2178 change = old != new;
2179 if (change)
2180 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06002181 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002182
Mark Brown8a713da2011-12-03 12:33:55 +00002183 if (ret < 0)
2184 return ret;
2185
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002186 return change;
2187}
2188EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2189
2190/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002191 * snd_soc_update_bits_locked - update codec register bits
2192 * @codec: audio codec
2193 * @reg: codec register
2194 * @mask: register mask
2195 * @value: new value
2196 *
2197 * Writes new register value, and takes the codec mutex.
2198 *
2199 * Returns 1 for change else 0.
2200 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002201int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2202 unsigned short reg, unsigned int mask,
2203 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002204{
2205 int change;
2206
2207 mutex_lock(&codec->mutex);
2208 change = snd_soc_update_bits(codec, reg, mask, value);
2209 mutex_unlock(&codec->mutex);
2210
2211 return change;
2212}
Mark Browndd1b3d52009-12-04 14:22:03 +00002213EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002214
2215/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002216 * snd_soc_test_bits - test register for change
2217 * @codec: audio codec
2218 * @reg: codec register
2219 * @mask: register mask
2220 * @value: new value
2221 *
2222 * Tests a register with a new value and checks if the new value is
2223 * different from the old value.
2224 *
2225 * Returns 1 for change else 0.
2226 */
2227int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002228 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002229{
2230 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002231 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002232
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002233 old = snd_soc_read(codec, reg);
2234 new = (old & ~mask) | value;
2235 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002236
2237 return change;
2238}
2239EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2240
2241/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002242 * snd_soc_cnew - create new control
2243 * @_template: control template
2244 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002245 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002246 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002247 *
2248 * Create a new mixer control from a template control.
2249 *
2250 * Returns 0 for success, else error.
2251 */
2252struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002253 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002254 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002255{
2256 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002257 struct snd_kcontrol *kcontrol;
2258 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002259
2260 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002261 template.index = 0;
2262
Mark Brownefb7ac32011-03-08 17:23:24 +00002263 if (!long_name)
2264 long_name = template.name;
2265
2266 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002267 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002268 if (!name)
2269 return NULL;
2270
Mark Brownefb7ac32011-03-08 17:23:24 +00002271 template.name = name;
2272 } else {
2273 template.name = long_name;
2274 }
2275
2276 kcontrol = snd_ctl_new1(&template, data);
2277
2278 kfree(name);
2279
2280 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002281}
2282EXPORT_SYMBOL_GPL(snd_soc_cnew);
2283
Liam Girdwood022658b2012-02-03 17:43:09 +00002284static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2285 const struct snd_kcontrol_new *controls, int num_controls,
2286 const char *prefix, void *data)
2287{
2288 int err, i;
2289
2290 for (i = 0; i < num_controls; i++) {
2291 const struct snd_kcontrol_new *control = &controls[i];
2292 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2293 control->name, prefix));
2294 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002295 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2296 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002297 return err;
2298 }
2299 }
2300
2301 return 0;
2302}
2303
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002304/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002305 * snd_soc_add_codec_controls - add an array of controls to a codec.
2306 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002307 * duplicating this code.
2308 *
2309 * @codec: codec to add controls to
2310 * @controls: array of controls to add
2311 * @num_controls: number of elements in the array
2312 *
2313 * Return 0 for success, else error.
2314 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002315int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002316 const struct snd_kcontrol_new *controls, int num_controls)
2317{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002318 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002319
Liam Girdwood022658b2012-02-03 17:43:09 +00002320 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2321 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002322}
Liam Girdwood022658b2012-02-03 17:43:09 +00002323EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002324
2325/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002326 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002327 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002328 *
2329 * @platform: platform to add controls to
2330 * @controls: array of controls to add
2331 * @num_controls: number of elements in the array
2332 *
2333 * Return 0 for success, else error.
2334 */
2335int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2336 const struct snd_kcontrol_new *controls, int num_controls)
2337{
2338 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002339
Liam Girdwood022658b2012-02-03 17:43:09 +00002340 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2341 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002342}
2343EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2344
2345/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002346 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2347 * Convenience function to add a list of controls.
2348 *
2349 * @soc_card: SoC card to add controls to
2350 * @controls: array of controls to add
2351 * @num_controls: number of elements in the array
2352 *
2353 * Return 0 for success, else error.
2354 */
2355int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2356 const struct snd_kcontrol_new *controls, int num_controls)
2357{
2358 struct snd_card *card = soc_card->snd_card;
2359
2360 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2361 NULL, soc_card);
2362}
2363EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2364
2365/**
2366 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2367 * Convienience function to add a list of controls.
2368 *
2369 * @dai: DAI to add controls to
2370 * @controls: array of controls to add
2371 * @num_controls: number of elements in the array
2372 *
2373 * Return 0 for success, else error.
2374 */
2375int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2376 const struct snd_kcontrol_new *controls, int num_controls)
2377{
2378 struct snd_card *card = dai->card->snd_card;
2379
2380 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2381 NULL, dai);
2382}
2383EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2384
2385/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002386 * snd_soc_info_enum_double - enumerated double mixer info callback
2387 * @kcontrol: mixer control
2388 * @uinfo: control element information
2389 *
2390 * Callback to provide information about a double enumerated
2391 * mixer control.
2392 *
2393 * Returns 0 for success.
2394 */
2395int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2396 struct snd_ctl_elem_info *uinfo)
2397{
2398 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2399
2400 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2401 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002402 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002403
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002404 if (uinfo->value.enumerated.item > e->max - 1)
2405 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002406 strcpy(uinfo->value.enumerated.name,
2407 e->texts[uinfo->value.enumerated.item]);
2408 return 0;
2409}
2410EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2411
2412/**
2413 * snd_soc_get_enum_double - enumerated double mixer get callback
2414 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002415 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002416 *
2417 * Callback to get the value of a double enumerated mixer.
2418 *
2419 * Returns 0 for success.
2420 */
2421int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2422 struct snd_ctl_elem_value *ucontrol)
2423{
2424 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2425 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002426 unsigned int val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002427
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002428 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002429 ucontrol->value.enumerated.item[0]
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002430 = (val >> e->shift_l) & e->mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002431 if (e->shift_l != e->shift_r)
2432 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002433 (val >> e->shift_r) & e->mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002434
2435 return 0;
2436}
2437EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2438
2439/**
2440 * snd_soc_put_enum_double - enumerated double mixer put callback
2441 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002442 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002443 *
2444 * Callback to set the value of a double enumerated mixer.
2445 *
2446 * Returns 0 for success.
2447 */
2448int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2449 struct snd_ctl_elem_value *ucontrol)
2450{
2451 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2452 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002453 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002454 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002455
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002456 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002457 return -EINVAL;
2458 val = ucontrol->value.enumerated.item[0] << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002459 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002460 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002461 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002462 return -EINVAL;
2463 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002464 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002465 }
2466
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002467 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002468}
2469EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2470
2471/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002472 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2473 * @kcontrol: mixer control
2474 * @ucontrol: control element information
2475 *
2476 * Callback to get the value of a double semi enumerated mixer.
2477 *
2478 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2479 * used for handling bitfield coded enumeration for example.
2480 *
2481 * Returns 0 for success.
2482 */
2483int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2484 struct snd_ctl_elem_value *ucontrol)
2485{
2486 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002487 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002488 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002489
2490 reg_val = snd_soc_read(codec, e->reg);
2491 val = (reg_val >> e->shift_l) & e->mask;
2492 for (mux = 0; mux < e->max; mux++) {
2493 if (val == e->values[mux])
2494 break;
2495 }
2496 ucontrol->value.enumerated.item[0] = mux;
2497 if (e->shift_l != e->shift_r) {
2498 val = (reg_val >> e->shift_r) & e->mask;
2499 for (mux = 0; mux < e->max; mux++) {
2500 if (val == e->values[mux])
2501 break;
2502 }
2503 ucontrol->value.enumerated.item[1] = mux;
2504 }
2505
2506 return 0;
2507}
2508EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2509
2510/**
2511 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2512 * @kcontrol: mixer control
2513 * @ucontrol: control element information
2514 *
2515 * Callback to set the value of a double semi enumerated mixer.
2516 *
2517 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2518 * used for handling bitfield coded enumeration for example.
2519 *
2520 * Returns 0 for success.
2521 */
2522int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2523 struct snd_ctl_elem_value *ucontrol)
2524{
2525 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002526 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002527 unsigned int val;
2528 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002529
2530 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2531 return -EINVAL;
2532 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2533 mask = e->mask << e->shift_l;
2534 if (e->shift_l != e->shift_r) {
2535 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2536 return -EINVAL;
2537 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2538 mask |= e->mask << e->shift_r;
2539 }
2540
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002541 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002542}
2543EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2544
2545/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002546 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2547 * @kcontrol: mixer control
2548 * @uinfo: control element information
2549 *
2550 * Callback to provide information about an external enumerated
2551 * single mixer.
2552 *
2553 * Returns 0 for success.
2554 */
2555int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2556 struct snd_ctl_elem_info *uinfo)
2557{
2558 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2559
2560 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2561 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002562 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002563
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002564 if (uinfo->value.enumerated.item > e->max - 1)
2565 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002566 strcpy(uinfo->value.enumerated.name,
2567 e->texts[uinfo->value.enumerated.item]);
2568 return 0;
2569}
2570EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2571
2572/**
2573 * snd_soc_info_volsw_ext - external single mixer info callback
2574 * @kcontrol: mixer control
2575 * @uinfo: control element information
2576 *
2577 * Callback to provide information about a single external mixer control.
2578 *
2579 * Returns 0 for success.
2580 */
2581int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2582 struct snd_ctl_elem_info *uinfo)
2583{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002584 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002585
Mark Brownfd5dfad2009-04-15 21:37:46 +01002586 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002587 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2588 else
2589 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2590
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002591 uinfo->count = 1;
2592 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002593 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002594 return 0;
2595}
2596EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2597
2598/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002599 * snd_soc_info_volsw - single mixer info callback
2600 * @kcontrol: mixer control
2601 * @uinfo: control element information
2602 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002603 * Callback to provide information about a single mixer control, or a double
2604 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002605 *
2606 * Returns 0 for success.
2607 */
2608int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2609 struct snd_ctl_elem_info *uinfo)
2610{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002611 struct soc_mixer_control *mc =
2612 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002613 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002614
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002615 if (!mc->platform_max)
2616 mc->platform_max = mc->max;
2617 platform_max = mc->platform_max;
2618
2619 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002620 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2621 else
2622 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2623
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002624 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002625 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002626 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002627 return 0;
2628}
2629EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2630
2631/**
2632 * snd_soc_get_volsw - single mixer get callback
2633 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002634 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002635 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002636 * Callback to get the value of a single mixer control, or a double mixer
2637 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002638 *
2639 * Returns 0 for success.
2640 */
2641int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2642 struct snd_ctl_elem_value *ucontrol)
2643{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002644 struct soc_mixer_control *mc =
2645 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002646 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002647 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002648 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002649 unsigned int shift = mc->shift;
2650 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002651 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002652 unsigned int mask = (1 << fls(max)) - 1;
2653 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002654
2655 ucontrol->value.integer.value[0] =
2656 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002657 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002658 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002659 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002660
2661 if (snd_soc_volsw_is_stereo(mc)) {
2662 if (reg == reg2)
2663 ucontrol->value.integer.value[1] =
2664 (snd_soc_read(codec, reg) >> rshift) & mask;
2665 else
2666 ucontrol->value.integer.value[1] =
2667 (snd_soc_read(codec, reg2) >> shift) & mask;
2668 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002669 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002670 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002671 }
2672
2673 return 0;
2674}
2675EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2676
2677/**
2678 * snd_soc_put_volsw - single mixer put callback
2679 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002680 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002681 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002682 * Callback to set the value of a single mixer control, or a double mixer
2683 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002684 *
2685 * Returns 0 for success.
2686 */
2687int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2688 struct snd_ctl_elem_value *ucontrol)
2689{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002690 struct soc_mixer_control *mc =
2691 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002692 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002693 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002694 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002695 unsigned int shift = mc->shift;
2696 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002697 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002698 unsigned int mask = (1 << fls(max)) - 1;
2699 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002700 int err;
2701 bool type_2r = 0;
2702 unsigned int val2 = 0;
2703 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002704
2705 val = (ucontrol->value.integer.value[0] & mask);
2706 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002707 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002708 val_mask = mask << shift;
2709 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002710 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002711 val2 = (ucontrol->value.integer.value[1] & mask);
2712 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002713 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002714 if (reg == reg2) {
2715 val_mask |= mask << rshift;
2716 val |= val2 << rshift;
2717 } else {
2718 val2 = val2 << shift;
2719 type_2r = 1;
2720 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002721 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002722 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002723 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002724 return err;
2725
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002726 if (type_2r)
2727 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2728
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002729 return err;
2730}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002731EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002732
Mark Browne13ac2e2008-05-28 17:58:05 +01002733/**
Brian Austin1d99f242012-03-30 10:43:55 -05002734 * snd_soc_get_volsw_sx - single mixer get callback
2735 * @kcontrol: mixer control
2736 * @ucontrol: control element information
2737 *
2738 * Callback to get the value of a single mixer control, or a double mixer
2739 * control that spans 2 registers.
2740 *
2741 * Returns 0 for success.
2742 */
2743int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2744 struct snd_ctl_elem_value *ucontrol)
2745{
2746 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2747 struct soc_mixer_control *mc =
2748 (struct soc_mixer_control *)kcontrol->private_value;
2749
2750 unsigned int reg = mc->reg;
2751 unsigned int reg2 = mc->rreg;
2752 unsigned int shift = mc->shift;
2753 unsigned int rshift = mc->rshift;
2754 int max = mc->max;
2755 int min = mc->min;
2756 int mask = (1 << (fls(min + max) - 1)) - 1;
2757
2758 ucontrol->value.integer.value[0] =
2759 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2760
2761 if (snd_soc_volsw_is_stereo(mc))
2762 ucontrol->value.integer.value[1] =
2763 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2764
2765 return 0;
2766}
2767EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2768
2769/**
2770 * snd_soc_put_volsw_sx - double mixer set callback
2771 * @kcontrol: mixer control
2772 * @uinfo: control element information
2773 *
2774 * Callback to set the value of a double mixer control that spans 2 registers.
2775 *
2776 * Returns 0 for success.
2777 */
2778int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2779 struct snd_ctl_elem_value *ucontrol)
2780{
2781 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2782 struct soc_mixer_control *mc =
2783 (struct soc_mixer_control *)kcontrol->private_value;
2784
2785 unsigned int reg = mc->reg;
2786 unsigned int reg2 = mc->rreg;
2787 unsigned int shift = mc->shift;
2788 unsigned int rshift = mc->rshift;
2789 int max = mc->max;
2790 int min = mc->min;
2791 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002792 int err = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002793 unsigned short val, val_mask, val2 = 0;
2794
2795 val_mask = mask << shift;
2796 val = (ucontrol->value.integer.value[0] + min) & mask;
2797 val = val << shift;
2798
Mukund Navadad0558522012-11-09 11:53:40 +05302799 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2800 if (err < 0)
2801 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002802
2803 if (snd_soc_volsw_is_stereo(mc)) {
2804 val_mask = mask << rshift;
2805 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2806 val2 = val2 << rshift;
2807
2808 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2809 return err;
2810 }
2811 return 0;
2812}
2813EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2814
2815/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002816 * snd_soc_info_volsw_s8 - signed mixer info callback
2817 * @kcontrol: mixer control
2818 * @uinfo: control element information
2819 *
2820 * Callback to provide information about a signed mixer control.
2821 *
2822 * Returns 0 for success.
2823 */
2824int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2825 struct snd_ctl_elem_info *uinfo)
2826{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002827 struct soc_mixer_control *mc =
2828 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002829 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002830 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002831
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002832 if (!mc->platform_max)
2833 mc->platform_max = mc->max;
2834 platform_max = mc->platform_max;
2835
Mark Browne13ac2e2008-05-28 17:58:05 +01002836 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2837 uinfo->count = 2;
2838 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002839 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002840 return 0;
2841}
2842EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2843
2844/**
2845 * snd_soc_get_volsw_s8 - signed mixer get callback
2846 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002847 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002848 *
2849 * Callback to get the value of a signed mixer control.
2850 *
2851 * Returns 0 for success.
2852 */
2853int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2854 struct snd_ctl_elem_value *ucontrol)
2855{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002856 struct soc_mixer_control *mc =
2857 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002858 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002859 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002860 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002861 int val = snd_soc_read(codec, reg);
2862
2863 ucontrol->value.integer.value[0] =
2864 ((signed char)(val & 0xff))-min;
2865 ucontrol->value.integer.value[1] =
2866 ((signed char)((val >> 8) & 0xff))-min;
2867 return 0;
2868}
2869EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2870
2871/**
2872 * snd_soc_put_volsw_sgn - signed mixer put callback
2873 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002874 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002875 *
2876 * Callback to set the value of a signed mixer control.
2877 *
2878 * Returns 0 for success.
2879 */
2880int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2881 struct snd_ctl_elem_value *ucontrol)
2882{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002883 struct soc_mixer_control *mc =
2884 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002885 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002886 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002887 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002888 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002889
2890 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2891 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2892
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002893 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002894}
2895EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2896
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002897/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002898 * snd_soc_info_volsw_range - single mixer info callback with range.
2899 * @kcontrol: mixer control
2900 * @uinfo: control element information
2901 *
2902 * Callback to provide information, within a range, about a single
2903 * mixer control.
2904 *
2905 * returns 0 for success.
2906 */
2907int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2908 struct snd_ctl_elem_info *uinfo)
2909{
2910 struct soc_mixer_control *mc =
2911 (struct soc_mixer_control *)kcontrol->private_value;
2912 int platform_max;
2913 int min = mc->min;
2914
2915 if (!mc->platform_max)
2916 mc->platform_max = mc->max;
2917 platform_max = mc->platform_max;
2918
2919 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002920 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002921 uinfo->value.integer.min = 0;
2922 uinfo->value.integer.max = platform_max - min;
2923
2924 return 0;
2925}
2926EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2927
2928/**
2929 * snd_soc_put_volsw_range - single mixer put value callback with range.
2930 * @kcontrol: mixer control
2931 * @ucontrol: control element information
2932 *
2933 * Callback to set the value, within a range, for a single mixer control.
2934 *
2935 * Returns 0 for success.
2936 */
2937int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2938 struct snd_ctl_elem_value *ucontrol)
2939{
2940 struct soc_mixer_control *mc =
2941 (struct soc_mixer_control *)kcontrol->private_value;
2942 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2943 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002944 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002945 unsigned int shift = mc->shift;
2946 int min = mc->min;
2947 int max = mc->max;
2948 unsigned int mask = (1 << fls(max)) - 1;
2949 unsigned int invert = mc->invert;
2950 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002951 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002952
2953 val = ((ucontrol->value.integer.value[0] + min) & mask);
2954 if (invert)
2955 val = max - val;
2956 val_mask = mask << shift;
2957 val = val << shift;
2958
Mark Brown9bde4f02012-12-19 16:05:00 +00002959 ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09002960 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00002961 return ret;
2962
2963 if (snd_soc_volsw_is_stereo(mc)) {
2964 val = ((ucontrol->value.integer.value[1] + min) & mask);
2965 if (invert)
2966 val = max - val;
2967 val_mask = mask << shift;
2968 val = val << shift;
2969
2970 ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
2971 }
2972
2973 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002974}
2975EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2976
2977/**
2978 * snd_soc_get_volsw_range - single mixer get callback with range
2979 * @kcontrol: mixer control
2980 * @ucontrol: control element information
2981 *
2982 * Callback to get the value, within a range, of a single mixer control.
2983 *
2984 * Returns 0 for success.
2985 */
2986int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2987 struct snd_ctl_elem_value *ucontrol)
2988{
2989 struct soc_mixer_control *mc =
2990 (struct soc_mixer_control *)kcontrol->private_value;
2991 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2992 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002993 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002994 unsigned int shift = mc->shift;
2995 int min = mc->min;
2996 int max = mc->max;
2997 unsigned int mask = (1 << fls(max)) - 1;
2998 unsigned int invert = mc->invert;
2999
3000 ucontrol->value.integer.value[0] =
3001 (snd_soc_read(codec, reg) >> shift) & mask;
3002 if (invert)
3003 ucontrol->value.integer.value[0] =
3004 max - ucontrol->value.integer.value[0];
3005 ucontrol->value.integer.value[0] =
3006 ucontrol->value.integer.value[0] - min;
3007
Mark Brown9bde4f02012-12-19 16:05:00 +00003008 if (snd_soc_volsw_is_stereo(mc)) {
3009 ucontrol->value.integer.value[1] =
3010 (snd_soc_read(codec, rreg) >> shift) & mask;
3011 if (invert)
3012 ucontrol->value.integer.value[1] =
3013 max - ucontrol->value.integer.value[1];
3014 ucontrol->value.integer.value[1] =
3015 ucontrol->value.integer.value[1] - min;
3016 }
3017
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003018 return 0;
3019}
3020EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3021
3022/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003023 * snd_soc_limit_volume - Set new limit to an existing volume control.
3024 *
3025 * @codec: where to look for the control
3026 * @name: Name of the control
3027 * @max: new maximum limit
3028 *
3029 * Return 0 for success, else error.
3030 */
3031int snd_soc_limit_volume(struct snd_soc_codec *codec,
3032 const char *name, int max)
3033{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003034 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003035 struct snd_kcontrol *kctl;
3036 struct soc_mixer_control *mc;
3037 int found = 0;
3038 int ret = -EINVAL;
3039
3040 /* Sanity check for name and max */
3041 if (unlikely(!name || max <= 0))
3042 return -EINVAL;
3043
3044 list_for_each_entry(kctl, &card->controls, list) {
3045 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3046 found = 1;
3047 break;
3048 }
3049 }
3050 if (found) {
3051 mc = (struct soc_mixer_control *)kctl->private_value;
3052 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003053 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003054 ret = 0;
3055 }
3056 }
3057 return ret;
3058}
3059EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3060
Mark Brown71d08512011-10-10 18:31:26 +01003061int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3062 struct snd_ctl_elem_info *uinfo)
3063{
3064 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3065 struct soc_bytes *params = (void *)kcontrol->private_value;
3066
3067 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3068 uinfo->count = params->num_regs * codec->val_bytes;
3069
3070 return 0;
3071}
3072EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3073
3074int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3075 struct snd_ctl_elem_value *ucontrol)
3076{
3077 struct soc_bytes *params = (void *)kcontrol->private_value;
3078 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3079 int ret;
3080
3081 if (codec->using_regmap)
3082 ret = regmap_raw_read(codec->control_data, params->base,
3083 ucontrol->value.bytes.data,
3084 params->num_regs * codec->val_bytes);
3085 else
3086 ret = -EINVAL;
3087
Mark Brownf831b052012-02-17 16:20:33 -08003088 /* Hide any masked bytes to ensure consistent data reporting */
3089 if (ret == 0 && params->mask) {
3090 switch (codec->val_bytes) {
3091 case 1:
3092 ucontrol->value.bytes.data[0] &= ~params->mask;
3093 break;
3094 case 2:
3095 ((u16 *)(&ucontrol->value.bytes.data))[0]
3096 &= ~params->mask;
3097 break;
3098 case 4:
3099 ((u32 *)(&ucontrol->value.bytes.data))[0]
3100 &= ~params->mask;
3101 break;
3102 default:
3103 return -EINVAL;
3104 }
3105 }
3106
Mark Brown71d08512011-10-10 18:31:26 +01003107 return ret;
3108}
3109EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3110
3111int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3112 struct snd_ctl_elem_value *ucontrol)
3113{
3114 struct soc_bytes *params = (void *)kcontrol->private_value;
3115 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08003116 int ret, len;
3117 unsigned int val;
3118 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003119
Mark Brownf831b052012-02-17 16:20:33 -08003120 if (!codec->using_regmap)
3121 return -EINVAL;
3122
Mark Brownf831b052012-02-17 16:20:33 -08003123 len = params->num_regs * codec->val_bytes;
3124
Mark Brownb5a8fe42013-01-20 21:42:22 +09003125 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3126 if (!data)
3127 return -ENOMEM;
3128
Mark Brownf831b052012-02-17 16:20:33 -08003129 /*
3130 * If we've got a mask then we need to preserve the register
3131 * bits. We shouldn't modify the incoming data so take a
3132 * copy.
3133 */
3134 if (params->mask) {
3135 ret = regmap_read(codec->control_data, params->base, &val);
3136 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003137 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003138
3139 val &= params->mask;
3140
Mark Brownf831b052012-02-17 16:20:33 -08003141 switch (codec->val_bytes) {
3142 case 1:
3143 ((u8 *)data)[0] &= ~params->mask;
3144 ((u8 *)data)[0] |= val;
3145 break;
3146 case 2:
3147 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3148 ((u16 *)data)[0] |= cpu_to_be16(val);
3149 break;
3150 case 4:
3151 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3152 ((u32 *)data)[0] |= cpu_to_be32(val);
3153 break;
3154 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003155 ret = -EINVAL;
3156 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003157 }
3158 }
3159
3160 ret = regmap_raw_write(codec->control_data, params->base,
3161 data, len);
3162
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003163out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003164 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003165
3166 return ret;
3167}
3168EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3169
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003170/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003171 * snd_soc_info_xr_sx - signed multi register info callback
3172 * @kcontrol: mreg control
3173 * @uinfo: control element information
3174 *
3175 * Callback to provide information of a control that can
3176 * span multiple codec registers which together
3177 * forms a single signed value in a MSB/LSB manner.
3178 *
3179 * Returns 0 for success.
3180 */
3181int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3182 struct snd_ctl_elem_info *uinfo)
3183{
3184 struct soc_mreg_control *mc =
3185 (struct soc_mreg_control *)kcontrol->private_value;
3186 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3187 uinfo->count = 1;
3188 uinfo->value.integer.min = mc->min;
3189 uinfo->value.integer.max = mc->max;
3190
3191 return 0;
3192}
3193EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3194
3195/**
3196 * snd_soc_get_xr_sx - signed multi register get callback
3197 * @kcontrol: mreg control
3198 * @ucontrol: control element information
3199 *
3200 * Callback to get the value of a control that can span
3201 * multiple codec registers which together forms a single
3202 * signed value in a MSB/LSB manner. The control supports
3203 * specifying total no of bits used to allow for bitfields
3204 * across the multiple codec registers.
3205 *
3206 * Returns 0 for success.
3207 */
3208int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3209 struct snd_ctl_elem_value *ucontrol)
3210{
3211 struct soc_mreg_control *mc =
3212 (struct soc_mreg_control *)kcontrol->private_value;
3213 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3214 unsigned int regbase = mc->regbase;
3215 unsigned int regcount = mc->regcount;
3216 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3217 unsigned int regwmask = (1<<regwshift)-1;
3218 unsigned int invert = mc->invert;
3219 unsigned long mask = (1UL<<mc->nbits)-1;
3220 long min = mc->min;
3221 long max = mc->max;
3222 long val = 0;
3223 unsigned long regval;
3224 unsigned int i;
3225
3226 for (i = 0; i < regcount; i++) {
3227 regval = snd_soc_read(codec, regbase+i) & regwmask;
3228 val |= regval << (regwshift*(regcount-i-1));
3229 }
3230 val &= mask;
3231 if (min < 0 && val > max)
3232 val |= ~mask;
3233 if (invert)
3234 val = max - val;
3235 ucontrol->value.integer.value[0] = val;
3236
3237 return 0;
3238}
3239EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3240
3241/**
3242 * snd_soc_put_xr_sx - signed multi register get callback
3243 * @kcontrol: mreg control
3244 * @ucontrol: control element information
3245 *
3246 * Callback to set the value of a control that can span
3247 * multiple codec registers which together forms a single
3248 * signed value in a MSB/LSB manner. The control supports
3249 * specifying total no of bits used to allow for bitfields
3250 * across the multiple codec registers.
3251 *
3252 * Returns 0 for success.
3253 */
3254int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3255 struct snd_ctl_elem_value *ucontrol)
3256{
3257 struct soc_mreg_control *mc =
3258 (struct soc_mreg_control *)kcontrol->private_value;
3259 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3260 unsigned int regbase = mc->regbase;
3261 unsigned int regcount = mc->regcount;
3262 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3263 unsigned int regwmask = (1<<regwshift)-1;
3264 unsigned int invert = mc->invert;
3265 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003266 long max = mc->max;
3267 long val = ucontrol->value.integer.value[0];
3268 unsigned int i, regval, regmask;
3269 int err;
3270
3271 if (invert)
3272 val = max - val;
3273 val &= mask;
3274 for (i = 0; i < regcount; i++) {
3275 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3276 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3277 err = snd_soc_update_bits_locked(codec, regbase+i,
3278 regmask, regval);
3279 if (err < 0)
3280 return err;
3281 }
3282
3283 return 0;
3284}
3285EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3286
3287/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003288 * snd_soc_get_strobe - strobe get callback
3289 * @kcontrol: mixer control
3290 * @ucontrol: control element information
3291 *
3292 * Callback get the value of a strobe mixer control.
3293 *
3294 * Returns 0 for success.
3295 */
3296int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3297 struct snd_ctl_elem_value *ucontrol)
3298{
3299 struct soc_mixer_control *mc =
3300 (struct soc_mixer_control *)kcontrol->private_value;
3301 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3302 unsigned int reg = mc->reg;
3303 unsigned int shift = mc->shift;
3304 unsigned int mask = 1 << shift;
3305 unsigned int invert = mc->invert != 0;
3306 unsigned int val = snd_soc_read(codec, reg) & mask;
3307
3308 if (shift != 0 && val != 0)
3309 val = val >> shift;
3310 ucontrol->value.enumerated.item[0] = val ^ invert;
3311
3312 return 0;
3313}
3314EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3315
3316/**
3317 * snd_soc_put_strobe - strobe put callback
3318 * @kcontrol: mixer control
3319 * @ucontrol: control element information
3320 *
3321 * Callback strobe a register bit to high then low (or the inverse)
3322 * in one pass of a single mixer enum control.
3323 *
3324 * Returns 1 for success.
3325 */
3326int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3327 struct snd_ctl_elem_value *ucontrol)
3328{
3329 struct soc_mixer_control *mc =
3330 (struct soc_mixer_control *)kcontrol->private_value;
3331 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3332 unsigned int reg = mc->reg;
3333 unsigned int shift = mc->shift;
3334 unsigned int mask = 1 << shift;
3335 unsigned int invert = mc->invert != 0;
3336 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3337 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3338 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3339 int err;
3340
3341 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3342 if (err < 0)
3343 return err;
3344
3345 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3346 return err;
3347}
3348EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3349
3350/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003351 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3352 * @dai: DAI
3353 * @clk_id: DAI specific clock ID
3354 * @freq: new clock frequency in Hz
3355 * @dir: new clock direction - input/output.
3356 *
3357 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3358 */
3359int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3360 unsigned int freq, int dir)
3361{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003362 if (dai->driver && dai->driver->ops->set_sysclk)
3363 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003364 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003365 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003366 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003367 else
3368 return -EINVAL;
3369}
3370EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3371
3372/**
Mark Brownec4ee522011-03-07 20:58:11 +00003373 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3374 * @codec: CODEC
3375 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003376 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003377 * @freq: new clock frequency in Hz
3378 * @dir: new clock direction - input/output.
3379 *
3380 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3381 */
3382int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003383 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003384{
3385 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003386 return codec->driver->set_sysclk(codec, clk_id, source,
3387 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003388 else
3389 return -EINVAL;
3390}
3391EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3392
3393/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003394 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3395 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003396 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003397 * @div: new clock divisor.
3398 *
3399 * Configures the clock dividers. This is used to derive the best DAI bit and
3400 * frame clocks from the system or master clock. It's best to set the DAI bit
3401 * and frame clocks as low as possible to save system power.
3402 */
3403int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3404 int div_id, int div)
3405{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003406 if (dai->driver && dai->driver->ops->set_clkdiv)
3407 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003408 else
3409 return -EINVAL;
3410}
3411EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3412
3413/**
3414 * snd_soc_dai_set_pll - configure DAI PLL.
3415 * @dai: DAI
3416 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003417 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003418 * @freq_in: PLL input clock frequency in Hz
3419 * @freq_out: requested PLL output clock frequency in Hz
3420 *
3421 * Configures and enables PLL to generate output clock based on input clock.
3422 */
Mark Brown85488032009-09-05 18:52:16 +01003423int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3424 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003425{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003426 if (dai->driver && dai->driver->ops->set_pll)
3427 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003428 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003429 else if (dai->codec && dai->codec->driver->set_pll)
3430 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3431 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003432 else
3433 return -EINVAL;
3434}
3435EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3436
Mark Brownec4ee522011-03-07 20:58:11 +00003437/*
3438 * snd_soc_codec_set_pll - configure codec PLL.
3439 * @codec: CODEC
3440 * @pll_id: DAI specific PLL ID
3441 * @source: DAI specific source for the PLL
3442 * @freq_in: PLL input clock frequency in Hz
3443 * @freq_out: requested PLL output clock frequency in Hz
3444 *
3445 * Configures and enables PLL to generate output clock based on input clock.
3446 */
3447int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3448 unsigned int freq_in, unsigned int freq_out)
3449{
3450 if (codec->driver->set_pll)
3451 return codec->driver->set_pll(codec, pll_id, source,
3452 freq_in, freq_out);
3453 else
3454 return -EINVAL;
3455}
3456EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3457
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003458/**
3459 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3460 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003461 * @fmt: SND_SOC_DAIFMT_ format value.
3462 *
3463 * Configures the DAI hardware format and clocking.
3464 */
3465int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3466{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003467 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003468 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003469 if (dai->driver->ops->set_fmt == NULL)
3470 return -ENOTSUPP;
3471 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003472}
3473EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3474
3475/**
3476 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3477 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003478 * @tx_mask: bitmask representing active TX slots.
3479 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003480 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003481 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003482 *
3483 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3484 * specific.
3485 */
3486int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003487 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003488{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003489 if (dai->driver && dai->driver->ops->set_tdm_slot)
3490 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003491 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003492 else
3493 return -EINVAL;
3494}
3495EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3496
3497/**
Barry Song472df3c2009-09-12 01:16:29 +08003498 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3499 * @dai: DAI
3500 * @tx_num: how many TX channels
3501 * @tx_slot: pointer to an array which imply the TX slot number channel
3502 * 0~num-1 uses
3503 * @rx_num: how many RX channels
3504 * @rx_slot: pointer to an array which imply the RX slot number channel
3505 * 0~num-1 uses
3506 *
3507 * configure the relationship between channel number and TDM slot number.
3508 */
3509int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3510 unsigned int tx_num, unsigned int *tx_slot,
3511 unsigned int rx_num, unsigned int *rx_slot)
3512{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003513 if (dai->driver && dai->driver->ops->set_channel_map)
3514 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003515 rx_num, rx_slot);
3516 else
3517 return -EINVAL;
3518}
3519EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3520
3521/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003522 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3523 * @dai: DAI
3524 * @tristate: tristate enable
3525 *
3526 * Tristates the DAI so that others can use it.
3527 */
3528int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3529{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003530 if (dai->driver && dai->driver->ops->set_tristate)
3531 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003532 else
3533 return -EINVAL;
3534}
3535EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3536
3537/**
3538 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3539 * @dai: DAI
3540 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003541 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003542 *
3543 * Mutes the DAI DAC.
3544 */
Mark Brownda183962013-02-06 15:44:07 +00003545int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3546 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003547{
Mark Brownda183962013-02-06 15:44:07 +00003548 if (!dai->driver)
3549 return -ENOTSUPP;
3550
3551 if (dai->driver->ops->mute_stream)
3552 return dai->driver->ops->mute_stream(dai, mute, direction);
3553 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3554 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003555 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003556 else
Mark Brown04570c62012-04-13 19:16:03 +01003557 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003558}
3559EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3560
Mark Brownc5af3a22008-11-28 13:29:45 +00003561/**
3562 * snd_soc_register_card - Register a card with the ASoC core
3563 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003564 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003565 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003566 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303567int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003568{
Mark Brownb19e6e72012-03-14 21:18:39 +00003569 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003570
Mark Brownc5af3a22008-11-28 13:29:45 +00003571 if (!card->name || !card->dev)
3572 return -EINVAL;
3573
Stephen Warren5a504962011-12-21 10:40:59 -07003574 for (i = 0; i < card->num_links; i++) {
3575 struct snd_soc_dai_link *link = &card->dai_link[i];
3576
3577 /*
3578 * Codec must be specified by 1 of name or OF node,
3579 * not both or neither.
3580 */
3581 if (!!link->codec_name == !!link->codec_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003582 dev_err(card->dev,
3583 "ASoC: Neither/both codec name/of_node are set for %s\n",
3584 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003585 return -EINVAL;
3586 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003587 /* Codec DAI name must be specified */
3588 if (!link->codec_dai_name) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003589 dev_err(card->dev,
3590 "ASoC: codec_dai_name not set for %s\n",
3591 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003592 return -EINVAL;
3593 }
Stephen Warren5a504962011-12-21 10:40:59 -07003594
3595 /*
3596 * Platform may be specified by either name or OF node, but
3597 * can be left unspecified, and a dummy platform will be used.
3598 */
3599 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003600 dev_err(card->dev,
3601 "ASoC: Both platform name/of_node are set for %s\n",
3602 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003603 return -EINVAL;
3604 }
3605
3606 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003607 * CPU device may be specified by either name or OF node, but
3608 * can be left unspecified, and will be matched based on DAI
3609 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003610 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003611 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003612 dev_err(card->dev,
3613 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3614 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003615 return -EINVAL;
3616 }
3617 /*
3618 * At least one of CPU DAI name or CPU device name/node must be
3619 * specified
3620 */
3621 if (!link->cpu_dai_name &&
3622 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003623 dev_err(card->dev,
3624 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3625 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003626 return -EINVAL;
3627 }
3628 }
3629
Mark Browned77cc12011-05-03 18:25:34 +01003630 dev_set_drvdata(card->dev, card);
3631
Stephen Warren111c6412011-01-28 14:26:35 -07003632 snd_soc_initialize_card_lists(card);
3633
Vinod Koul150dd2f2011-01-13 22:48:32 +05303634 soc_init_card_debugfs(card);
3635
Mark Brown181a6892012-03-14 20:18:49 +00003636 card->rtd = devm_kzalloc(card->dev,
3637 sizeof(struct snd_soc_pcm_runtime) *
3638 (card->num_links + card->num_aux_devs),
3639 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003640 if (card->rtd == NULL)
3641 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003642 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003643 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003644
3645 for (i = 0; i < card->num_links; i++)
3646 card->rtd[i].dai_link = &card->dai_link[i];
3647
Mark Brownc5af3a22008-11-28 13:29:45 +00003648 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003649 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003650 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003651 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003652 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003653
Mark Brownb19e6e72012-03-14 21:18:39 +00003654 ret = snd_soc_instantiate_card(card);
3655 if (ret != 0)
3656 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003657
Mark Brownb19e6e72012-03-14 21:18:39 +00003658 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003659}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303660EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003661
3662/**
3663 * snd_soc_unregister_card - Unregister a card with the ASoC core
3664 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003665 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003666 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003667 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303668int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003669{
Vinod Koulb0e26482011-01-13 22:48:02 +05303670 if (card->instantiated)
3671 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003672 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003673
3674 return 0;
3675}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303676EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003677
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003678/*
3679 * Simplify DAI link configuration by removing ".-1" from device names
3680 * and sanitizing names.
3681 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003682static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003683{
3684 char *found, name[NAME_SIZE];
3685 int id1, id2;
3686
3687 if (dev_name(dev) == NULL)
3688 return NULL;
3689
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003690 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003691
3692 /* are we a "%s.%d" name (platform and SPI components) */
3693 found = strstr(name, dev->driver->name);
3694 if (found) {
3695 /* get ID */
3696 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3697
3698 /* discard ID from name if ID == -1 */
3699 if (*id == -1)
3700 found[strlen(dev->driver->name)] = '\0';
3701 }
3702
3703 } else {
3704 /* I2C component devices are named "bus-addr" */
3705 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3706 char tmp[NAME_SIZE];
3707
3708 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003709 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003710
3711 /* sanitize component name for DAI link creation */
3712 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003713 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003714 } else
3715 *id = 0;
3716 }
3717
3718 return kstrdup(name, GFP_KERNEL);
3719}
3720
3721/*
3722 * Simplify DAI link naming for single devices with multiple DAIs by removing
3723 * any ".-1" and using the DAI name (instead of device name).
3724 */
3725static inline char *fmt_multiple_name(struct device *dev,
3726 struct snd_soc_dai_driver *dai_drv)
3727{
3728 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003729 dev_err(dev,
3730 "ASoC: error - multiple DAI %s registered with no name\n",
3731 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003732 return NULL;
3733 }
3734
3735 return kstrdup(dai_drv->name, GFP_KERNEL);
3736}
3737
Mark Brown91151712008-11-30 23:31:24 +00003738/**
3739 * snd_soc_register_dai - Register a DAI with the ASoC core
3740 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003741 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003742 */
Kuninori Morimotof53179c02013-03-21 03:38:30 -07003743static int snd_soc_register_dai(struct device *dev,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003744 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003745{
Mark Brown054880f2012-04-09 17:29:19 +01003746 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003747 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003748
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003749 dev_dbg(dev, "ASoC: dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003750
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003751 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3752 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003753 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003754
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003755 /* create DAI component name */
3756 dai->name = fmt_single_name(dev, &dai->id);
3757 if (dai->name == NULL) {
3758 kfree(dai);
3759 return -ENOMEM;
3760 }
3761
3762 dai->dev = dev;
3763 dai->driver = dai_drv;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003764 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003765 if (!dai->driver->ops)
3766 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003767
3768 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003769
3770 list_for_each_entry(codec, &codec_list, list) {
3771 if (codec->dev == dev) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003772 dev_dbg(dev, "ASoC: Mapped DAI %s to CODEC %s\n",
Mark Brown054880f2012-04-09 17:29:19 +01003773 dai->name, codec->name);
3774 dai->codec = codec;
3775 break;
3776 }
3777 }
3778
Peter Ujfalusi5f800082012-08-07 10:24:13 +03003779 if (!dai->codec)
3780 dai->dapm.idle_bias_off = 1;
3781
Mark Brown91151712008-11-30 23:31:24 +00003782 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003783
Mark Brown91151712008-11-30 23:31:24 +00003784 mutex_unlock(&client_mutex);
3785
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003786 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003787
3788 return 0;
3789}
Mark Brown91151712008-11-30 23:31:24 +00003790
3791/**
3792 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3793 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003794 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003795 */
Kuninori Morimotof53179c02013-03-21 03:38:30 -07003796static void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003797{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003798 struct snd_soc_dai *dai;
3799
3800 list_for_each_entry(dai, &dai_list, list) {
3801 if (dev == dai->dev)
3802 goto found;
3803 }
3804 return;
3805
3806found:
Mark Brown91151712008-11-30 23:31:24 +00003807 mutex_lock(&client_mutex);
3808 list_del(&dai->list);
3809 mutex_unlock(&client_mutex);
3810
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003811 dev_dbg(dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003812 kfree(dai->name);
3813 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003814}
Mark Brown91151712008-11-30 23:31:24 +00003815
3816/**
3817 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3818 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003819 * @dai: Array of DAIs to register
3820 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003821 */
Kuninori Morimotof53179c02013-03-21 03:38:30 -07003822static int snd_soc_register_dais(struct device *dev,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003823 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003824{
Mark Brown054880f2012-04-09 17:29:19 +01003825 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003826 struct snd_soc_dai *dai;
3827 int i, ret = 0;
3828
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003829 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003830
3831 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003832
3833 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003834 if (dai == NULL) {
3835 ret = -ENOMEM;
3836 goto err;
3837 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003838
3839 /* create DAI component name */
3840 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3841 if (dai->name == NULL) {
3842 kfree(dai);
3843 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003844 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003845 }
3846
3847 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003848 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003849 if (dai->driver->id)
3850 dai->id = dai->driver->id;
3851 else
3852 dai->id = i;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003853 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003854 if (!dai->driver->ops)
3855 dai->driver->ops = &null_dai_ops;
3856
3857 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003858
3859 list_for_each_entry(codec, &codec_list, list) {
3860 if (codec->dev == dev) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003861 dev_dbg(dev,
3862 "ASoC: Mapped DAI %s to CODEC %s\n",
3863 dai->name, codec->name);
Mark Brown054880f2012-04-09 17:29:19 +01003864 dai->codec = codec;
3865 break;
3866 }
3867 }
3868
Peter Ujfalusi5f800082012-08-07 10:24:13 +03003869 if (!dai->codec)
3870 dai->dapm.idle_bias_off = 1;
3871
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003872 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003873
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003874 mutex_unlock(&client_mutex);
3875
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003876 dev_dbg(dai->dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003877 }
3878
3879 return 0;
3880
3881err:
3882 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003883 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003884
3885 return ret;
3886}
Mark Brown91151712008-11-30 23:31:24 +00003887
3888/**
3889 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3890 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003891 * @dai: Array of DAIs to unregister
3892 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003893 */
Kuninori Morimotof53179c02013-03-21 03:38:30 -07003894static void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003895{
3896 int i;
3897
3898 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003899 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003900}
Mark Brown91151712008-11-30 23:31:24 +00003901
Mark Brown12a48a8c2008-12-03 19:40:30 +00003902/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003903 * snd_soc_add_platform - Add a platform to the ASoC core
3904 * @dev: The parent device for the platform
3905 * @platform: The platform to add
3906 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00003907 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003908int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01003909 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003910{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003911 /* create platform component name */
3912 platform->name = fmt_single_name(dev, &platform->id);
3913 if (platform->name == NULL) {
3914 kfree(platform);
3915 return -ENOMEM;
3916 }
3917
3918 platform->dev = dev;
3919 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003920 platform->dapm.dev = dev;
3921 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003922 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00003923 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003924
3925 mutex_lock(&client_mutex);
3926 list_add(&platform->list, &platform_list);
3927 mutex_unlock(&client_mutex);
3928
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003929 dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003930
3931 return 0;
3932}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003933EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3934
3935/**
3936 * snd_soc_register_platform - Register a platform with the ASoC core
3937 *
3938 * @platform: platform to register
3939 */
3940int snd_soc_register_platform(struct device *dev,
3941 const struct snd_soc_platform_driver *platform_drv)
3942{
3943 struct snd_soc_platform *platform;
3944 int ret;
3945
3946 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3947
3948 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3949 if (platform == NULL)
3950 return -ENOMEM;
3951
3952 ret = snd_soc_add_platform(dev, platform, platform_drv);
3953 if (ret)
3954 kfree(platform);
3955
3956 return ret;
3957}
Mark Brown12a48a8c2008-12-03 19:40:30 +00003958EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3959
3960/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003961 * snd_soc_remove_platform - Remove a platform from the ASoC core
3962 * @platform: the platform to remove
3963 */
3964void snd_soc_remove_platform(struct snd_soc_platform *platform)
3965{
3966 mutex_lock(&client_mutex);
3967 list_del(&platform->list);
3968 mutex_unlock(&client_mutex);
3969
3970 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3971 platform->name);
3972 kfree(platform->name);
3973}
3974EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3975
3976struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3977{
3978 struct snd_soc_platform *platform;
3979
3980 list_for_each_entry(platform, &platform_list, list) {
3981 if (dev == platform->dev)
3982 return platform;
3983 }
3984
3985 return NULL;
3986}
3987EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3988
3989/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00003990 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3991 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003992 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003993 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003994void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003995{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003996 struct snd_soc_platform *platform;
3997
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003998 platform = snd_soc_lookup_platform(dev);
3999 if (!platform)
4000 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004001
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004002 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004003 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004004}
4005EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4006
Mark Brown151ab222009-05-09 16:22:58 +01004007static u64 codec_format_map[] = {
4008 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4009 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4010 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4011 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4012 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4013 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4014 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4015 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4016 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4017 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4018 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4019 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4020 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4021 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4022 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4023 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4024};
4025
4026/* Fix up the DAI formats for endianness: codecs don't actually see
4027 * the endianness of the data but we're using the CPU format
4028 * definitions which do need to include endianness so we ensure that
4029 * codec DAIs always have both big and little endian variants set.
4030 */
4031static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4032{
4033 int i;
4034
4035 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4036 if (stream->formats & codec_format_map[i])
4037 stream->formats |= codec_format_map[i];
4038}
4039
Mark Brown0d0cf002008-12-10 14:32:45 +00004040/**
4041 * snd_soc_register_codec - Register a codec with the ASoC core
4042 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004043 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004044 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004045int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004046 const struct snd_soc_codec_driver *codec_drv,
4047 struct snd_soc_dai_driver *dai_drv,
4048 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004049{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004050 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004051 struct snd_soc_codec *codec;
4052 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004053
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004054 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004055
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004056 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4057 if (codec == NULL)
4058 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004059
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004060 /* create CODEC component name */
4061 codec->name = fmt_single_name(dev, &codec->id);
4062 if (codec->name == NULL) {
Kuninori Morimotof790b942013-02-25 00:40:09 -08004063 ret = -ENOMEM;
4064 goto fail_codec;
Mark Brown151ab222009-05-09 16:22:58 +01004065 }
4066
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00004067 if (codec_drv->compress_type)
4068 codec->compress_type = codec_drv->compress_type;
4069 else
4070 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
4071
Mark Brownc3acec22010-12-02 16:15:29 +00004072 codec->write = codec_drv->write;
4073 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004074 codec->volatile_register = codec_drv->volatile_register;
4075 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004076 codec->writable_register = codec_drv->writable_register;
Mark Brown5124e692012-02-08 13:20:50 +00004077 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004078 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4079 codec->dapm.dev = dev;
4080 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00004081 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01004082 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004083 codec->dev = dev;
4084 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004085 codec->num_dai = num_dai;
4086 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004087
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004088 /* allocate CODEC register cache */
4089 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004090 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00004091 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004092 /* it is necessary to make a copy of the default register cache
4093 * because in the case of using a compression type that requires
Bill Pembertonf6e65742012-11-19 13:25:33 -05004094 * the default register cache to be marked as the
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004095 * kernel might have freed the array by the time we initialize
4096 * the cache.
4097 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00004098 if (codec_drv->reg_cache_default) {
4099 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
4100 reg_size, GFP_KERNEL);
4101 if (!codec->reg_def_copy) {
4102 ret = -ENOMEM;
Kuninori Morimotof790b942013-02-25 00:40:09 -08004103 goto fail_codec_name;
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00004104 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004105 }
4106 }
4107
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004108 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
4109 if (!codec->volatile_register)
4110 codec->volatile_register = snd_soc_default_volatile_register;
4111 if (!codec->readable_register)
4112 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004113 if (!codec->writable_register)
4114 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004115 }
4116
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004117 for (i = 0; i < num_dai; i++) {
4118 fixup_codec_formats(&dai_drv[i].playback);
4119 fixup_codec_formats(&dai_drv[i].capture);
4120 }
4121
Mark Brown054880f2012-04-09 17:29:19 +01004122 mutex_lock(&client_mutex);
4123 list_add(&codec->list, &codec_list);
4124 mutex_unlock(&client_mutex);
4125
Mark Brown26b01cc2010-08-18 20:20:55 +01004126 /* register any DAIs */
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004127 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4128 if (ret < 0) {
4129 dev_err(codec->dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4130 goto fail_codec_name;
Mark Brown26b01cc2010-08-18 20:20:55 +01004131 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004132
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004133 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004134 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004135
Kuninori Morimotof790b942013-02-25 00:40:09 -08004136fail_codec_name:
Kuninori Morimotoe1328a82013-03-07 17:42:33 -08004137 mutex_lock(&client_mutex);
4138 list_del(&codec->list);
4139 mutex_unlock(&client_mutex);
4140
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004141 kfree(codec->name);
Kuninori Morimotof790b942013-02-25 00:40:09 -08004142fail_codec:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004143 kfree(codec);
4144 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004145}
4146EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4147
4148/**
4149 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4150 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004151 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004152 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004153void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004154{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004155 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004156
4157 list_for_each_entry(codec, &codec_list, list) {
4158 if (dev == codec->dev)
4159 goto found;
4160 }
4161 return;
4162
4163found:
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004164 snd_soc_unregister_dais(dev, codec->num_dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004165
Mark Brown0d0cf002008-12-10 14:32:45 +00004166 mutex_lock(&client_mutex);
4167 list_del(&codec->list);
4168 mutex_unlock(&client_mutex);
4169
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004170 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004171
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004172 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004173 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01004174 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004175 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004176}
4177EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4178
Kuninori Morimoto030e79f2013-03-11 18:27:21 -07004179
4180/**
4181 * snd_soc_register_component - Register a component with the ASoC core
4182 *
4183 */
4184int snd_soc_register_component(struct device *dev,
4185 const struct snd_soc_component_driver *cmpnt_drv,
4186 struct snd_soc_dai_driver *dai_drv,
4187 int num_dai)
4188{
4189 struct snd_soc_component *cmpnt;
4190 int ret;
4191
4192 dev_dbg(dev, "component register %s\n", dev_name(dev));
4193
4194 cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
4195 if (!cmpnt) {
4196 dev_err(dev, "ASoC: Failed to allocate memory\n");
4197 return -ENOMEM;
4198 }
4199
4200 cmpnt->name = fmt_single_name(dev, &cmpnt->id);
4201 if (!cmpnt->name) {
4202 dev_err(dev, "ASoC: Failed to simplifying name\n");
4203 return -ENOMEM;
4204 }
4205
4206 cmpnt->dev = dev;
4207 cmpnt->driver = cmpnt_drv;
4208 cmpnt->num_dai = num_dai;
4209
Kuninori Morimotoa1422b82013-03-21 03:27:13 -07004210 /*
4211 * snd_soc_register_dai() uses fmt_single_name(), and
4212 * snd_soc_register_dais() uses fmt_multiple_name()
4213 * for dai->name which is used for name based matching
4214 */
4215 if (1 == num_dai)
4216 ret = snd_soc_register_dai(dev, dai_drv);
4217 else
4218 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -07004219 if (ret < 0) {
4220 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4221 goto error_component_name;
4222 }
4223
4224 mutex_lock(&client_mutex);
4225 list_add(&cmpnt->list, &component_list);
4226 mutex_unlock(&client_mutex);
4227
4228 dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
4229
4230 return ret;
4231
4232error_component_name:
4233 kfree(cmpnt->name);
4234
4235 return ret;
4236}
Stephen Warren2e1cc192013-03-26 16:38:19 -06004237EXPORT_SYMBOL_GPL(snd_soc_register_component);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -07004238
4239/**
4240 * snd_soc_unregister_component - Unregister a component from the ASoC core
4241 *
4242 */
4243void snd_soc_unregister_component(struct device *dev)
4244{
4245 struct snd_soc_component *cmpnt;
4246
4247 list_for_each_entry(cmpnt, &component_list, list) {
4248 if (dev == cmpnt->dev)
4249 goto found;
4250 }
4251 return;
4252
4253found:
4254 snd_soc_unregister_dais(dev, cmpnt->num_dai);
4255
4256 mutex_lock(&client_mutex);
4257 list_del(&cmpnt->list);
4258 mutex_unlock(&client_mutex);
4259
4260 dev_dbg(dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
4261 kfree(cmpnt->name);
4262}
Stephen Warren2e1cc192013-03-26 16:38:19 -06004263EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
Kuninori Morimoto030e79f2013-03-11 18:27:21 -07004264
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004265/* Retrieve a card's name from device tree */
4266int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4267 const char *propname)
4268{
4269 struct device_node *np = card->dev->of_node;
4270 int ret;
4271
4272 ret = of_property_read_string_index(np, propname, 0, &card->name);
4273 /*
4274 * EINVAL means the property does not exist. This is fine providing
4275 * card->name was previously set, which is checked later in
4276 * snd_soc_register_card.
4277 */
4278 if (ret < 0 && ret != -EINVAL) {
4279 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004280 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004281 propname, ret);
4282 return ret;
4283 }
4284
4285 return 0;
4286}
4287EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4288
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004289int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4290 const char *propname)
4291{
4292 struct device_node *np = card->dev->of_node;
4293 int num_routes;
4294 struct snd_soc_dapm_route *routes;
4295 int i, ret;
4296
4297 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004298 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004299 dev_err(card->dev,
4300 "ASoC: Property '%s' does not exist or its length is not even\n",
4301 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004302 return -EINVAL;
4303 }
4304 num_routes /= 2;
4305 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004306 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004307 propname);
4308 return -EINVAL;
4309 }
4310
4311 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4312 GFP_KERNEL);
4313 if (!routes) {
4314 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004315 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004316 return -EINVAL;
4317 }
4318
4319 for (i = 0; i < num_routes; i++) {
4320 ret = of_property_read_string_index(np, propname,
4321 2 * i, &routes[i].sink);
4322 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004323 dev_err(card->dev,
4324 "ASoC: Property '%s' index %d could not be read: %d\n",
4325 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004326 return -EINVAL;
4327 }
4328 ret = of_property_read_string_index(np, propname,
4329 (2 * i) + 1, &routes[i].source);
4330 if (ret) {
4331 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004332 "ASoC: Property '%s' index %d could not be read: %d\n",
4333 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004334 return -EINVAL;
4335 }
4336 }
4337
4338 card->num_dapm_routes = num_routes;
4339 card->dapm_routes = routes;
4340
4341 return 0;
4342}
4343EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4344
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004345unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4346 const char *prefix)
4347{
4348 int ret, i;
4349 char prop[128];
4350 unsigned int format = 0;
4351 int bit, frame;
4352 const char *str;
4353 struct {
4354 char *name;
4355 unsigned int val;
4356 } of_fmt_table[] = {
4357 { "i2s", SND_SOC_DAIFMT_I2S },
4358 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4359 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4360 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4361 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4362 { "ac97", SND_SOC_DAIFMT_AC97 },
4363 { "pdm", SND_SOC_DAIFMT_PDM},
4364 { "msb", SND_SOC_DAIFMT_MSB },
4365 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004366 };
4367
4368 if (!prefix)
4369 prefix = "";
4370
4371 /*
4372 * check "[prefix]format = xxx"
4373 * SND_SOC_DAIFMT_FORMAT_MASK area
4374 */
4375 snprintf(prop, sizeof(prop), "%sformat", prefix);
4376 ret = of_property_read_string(np, prop, &str);
4377 if (ret == 0) {
4378 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4379 if (strcmp(str, of_fmt_table[i].name) == 0) {
4380 format |= of_fmt_table[i].val;
4381 break;
4382 }
4383 }
4384 }
4385
4386 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004387 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004388 * SND_SOC_DAIFMT_CLOCK_MASK area
4389 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004390 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4391 if (of_get_property(np, prop, NULL))
4392 format |= SND_SOC_DAIFMT_CONT;
4393 else
4394 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004395
4396 /*
4397 * check "[prefix]bitclock-inversion"
4398 * check "[prefix]frame-inversion"
4399 * SND_SOC_DAIFMT_INV_MASK area
4400 */
4401 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4402 bit = !!of_get_property(np, prop, NULL);
4403
4404 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4405 frame = !!of_get_property(np, prop, NULL);
4406
4407 switch ((bit << 4) + frame) {
4408 case 0x11:
4409 format |= SND_SOC_DAIFMT_IB_IF;
4410 break;
4411 case 0x10:
4412 format |= SND_SOC_DAIFMT_IB_NF;
4413 break;
4414 case 0x01:
4415 format |= SND_SOC_DAIFMT_NB_IF;
4416 break;
4417 default:
4418 /* SND_SOC_DAIFMT_NB_NF is default */
4419 break;
4420 }
4421
4422 /*
4423 * check "[prefix]bitclock-master"
4424 * check "[prefix]frame-master"
4425 * SND_SOC_DAIFMT_MASTER_MASK area
4426 */
4427 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4428 bit = !!of_get_property(np, prop, NULL);
4429
4430 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4431 frame = !!of_get_property(np, prop, NULL);
4432
4433 switch ((bit << 4) + frame) {
4434 case 0x11:
4435 format |= SND_SOC_DAIFMT_CBM_CFM;
4436 break;
4437 case 0x10:
4438 format |= SND_SOC_DAIFMT_CBM_CFS;
4439 break;
4440 case 0x01:
4441 format |= SND_SOC_DAIFMT_CBS_CFM;
4442 break;
4443 default:
4444 format |= SND_SOC_DAIFMT_CBS_CFS;
4445 break;
4446 }
4447
4448 return format;
4449}
4450EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4451
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004452static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004453{
Mark Brown384c89e2008-12-03 17:34:03 +00004454#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004455 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4456 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004457 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004458 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004459 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004460
Mark Brown8a9dab12011-01-10 22:25:21 +00004461 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004462 &codec_list_fops))
4463 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4464
Mark Brown8a9dab12011-01-10 22:25:21 +00004465 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004466 &dai_list_fops))
4467 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004468
Mark Brown8a9dab12011-01-10 22:25:21 +00004469 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004470 &platform_list_fops))
4471 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004472#endif
4473
Mark Brownfb257892011-04-28 10:57:54 +01004474 snd_soc_util_init();
4475
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004476 return platform_driver_register(&soc_driver);
4477}
Mark Brown4abe8e12010-10-12 17:41:03 +01004478module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004479
Mark Brown7d8c16a2008-11-30 22:11:24 +00004480static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004481{
Mark Brownfb257892011-04-28 10:57:54 +01004482 snd_soc_util_exit();
4483
Mark Brown384c89e2008-12-03 17:34:03 +00004484#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004485 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004486#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004487 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004488}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004489module_exit(snd_soc_exit);
4490
4491/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004492MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004493MODULE_DESCRIPTION("ALSA SoC Core");
4494MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004495MODULE_ALIAS("platform:soc-audio");