blob: e0e8ce0c031acf2aa905787b223953def13403b7 [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);
Mark Brownc5af3a22008-11-28 13:29:45 +000061
Frank Mandarinodb2a4162006-10-06 18:31:09 +020062/*
63 * This is a timeout to do a DAPM powerdown after a stream is closed().
64 * It can be used to eliminate pops between different playback streams, e.g.
65 * between two audio tracks.
66 */
67static int pmdown_time = 5000;
68module_param(pmdown_time, int, 0);
69MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
70
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000071/* returns the minimum number of bytes needed to represent
72 * a particular given value */
73static int min_bytes_needed(unsigned long val)
74{
75 int c = 0;
76 int i;
77
78 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
79 if (val & (1UL << i))
80 break;
81 c = (sizeof val * 8) - c;
82 if (!c || (c % 8))
83 c = (c + 8) / 8;
84 else
85 c /= 8;
86 return c;
87}
88
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000089/* fill buf which is 'len' bytes with a formatted
90 * string of the form 'reg: value\n' */
91static int format_register_str(struct snd_soc_codec *codec,
92 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +000093{
Stephen Warren00b317a2011-04-01 14:50:44 -060094 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
95 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000096 int ret;
97 char tmpbuf[len + 1];
98 char regbuf[regsize + 1];
99
100 /* since tmpbuf is allocated on the stack, warn the callers if they
101 * try to abuse this function */
102 WARN_ON(len > 63);
103
104 /* +2 for ': ' and + 1 for '\n' */
105 if (wordsize + regsize + 2 + 1 != len)
106 return -EINVAL;
107
Mark Brown25032c12011-08-01 13:52:48 +0900108 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000109 if (ret < 0) {
110 memset(regbuf, 'X', regsize);
111 regbuf[regsize] = '\0';
112 } else {
113 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
114 }
115
116 /* prepare the buffer */
117 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
118 /* copy it back to the caller without the '\0' */
119 memcpy(buf, tmpbuf, len);
120
121 return 0;
122}
123
124/* codec register dump */
125static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
126 size_t count, loff_t pos)
127{
128 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000129 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000130 int len;
131 size_t total = 0;
132 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000133
Stephen Warren00b317a2011-04-01 14:50:44 -0600134 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
135 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000136
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000137 len = wordsize + regsize + 2 + 1;
138
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000139 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000140 return 0;
141
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000142 if (codec->driver->reg_cache_step)
143 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000144
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000145 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausenb92d1502011-08-27 18:24:14 +0200146 if (!snd_soc_codec_readable_register(codec, i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000147 continue;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000148 if (codec->driver->display_register) {
149 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000150 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100151 } else {
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000152 /* only support larger than PAGE_SIZE bytes debugfs
153 * entries for the default case */
154 if (p >= pos) {
155 if (total + len >= count - 1)
156 break;
157 format_register_str(codec, i, buf + total, len);
158 total += len;
159 }
160 p += len;
Mark Brown5164d742010-07-14 16:14:33 +0100161 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000162 }
163
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000164 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000165
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000166 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000167}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000168
Mark Brown2624d5f2009-11-03 21:56:13 +0000169static ssize_t codec_reg_show(struct device *dev,
170 struct device_attribute *attr, char *buf)
171{
Mark Brown36ae1a92012-01-06 17:12:45 -0800172 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000173
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000174 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000175}
176
177static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
178
Mark Browndbe21402010-02-12 11:37:24 +0000179static ssize_t pmdown_time_show(struct device *dev,
180 struct device_attribute *attr, char *buf)
181{
Mark Brown36ae1a92012-01-06 17:12:45 -0800182 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000183
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000184 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000185}
186
187static ssize_t pmdown_time_set(struct device *dev,
188 struct device_attribute *attr,
189 const char *buf, size_t count)
190{
Mark Brown36ae1a92012-01-06 17:12:45 -0800191 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700192 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000193
Mark Brownc593b522010-10-27 20:11:17 -0700194 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
195 if (ret)
196 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000197
198 return count;
199}
200
201static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
202
Mark Brown2624d5f2009-11-03 21:56:13 +0000203#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000204static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000205 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000206{
207 ssize_t ret;
208 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000209 char *buf;
210
211 if (*ppos < 0 || !count)
212 return -EINVAL;
213
214 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000215 if (!buf)
216 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000217
218 ret = soc_codec_reg_show(codec, buf, count, *ppos);
219 if (ret >= 0) {
220 if (copy_to_user(user_buf, buf, ret)) {
221 kfree(buf);
222 return -EFAULT;
223 }
224 *ppos += ret;
225 }
226
Mark Brown2624d5f2009-11-03 21:56:13 +0000227 kfree(buf);
228 return ret;
229}
230
231static ssize_t codec_reg_write_file(struct file *file,
232 const char __user *user_buf, size_t count, loff_t *ppos)
233{
234 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700235 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000236 char *start = buf;
237 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000238 struct snd_soc_codec *codec = file->private_data;
239
240 buf_size = min(count, (sizeof(buf)-1));
241 if (copy_from_user(buf, user_buf, buf_size))
242 return -EFAULT;
243 buf[buf_size] = 0;
244
Mark Brown2624d5f2009-11-03 21:56:13 +0000245 while (*start == ' ')
246 start++;
247 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000248 while (*start == ' ')
249 start++;
250 if (strict_strtoul(start, 16, &value))
251 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000252
253 /* Userspace has been fiddling around behind the kernel's back */
254 add_taint(TAINT_USER);
255
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000256 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000257 return buf_size;
258}
259
260static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700261 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000262 .read = codec_reg_read_file,
263 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200264 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000265};
266
267static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
268{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200269 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
270
271 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
272 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000273 if (!codec->debugfs_codec_root) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000274 dev_warn(codec->dev, "ASoC: Failed to create codec debugfs"
275 " directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000276 return;
277 }
278
Mark Brownaaee8ef2011-01-26 20:53:50 +0000279 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
280 &codec->cache_sync);
281 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
282 &codec->cache_only);
283
Mark Brown2624d5f2009-11-03 21:56:13 +0000284 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
285 codec->debugfs_codec_root,
286 codec, &codec_reg_fops);
287 if (!codec->debugfs_reg)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000288 dev_warn(codec->dev, "ASoC: Failed to create codec register"
289 " debugfs file\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000290
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200291 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000292}
293
294static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
295{
296 debugfs_remove_recursive(codec->debugfs_codec_root);
297}
298
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000299static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
300{
301 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
302
303 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
304 debugfs_card_root);
305 if (!platform->debugfs_platform_root) {
306 dev_warn(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000307 "ASoC: Failed to create platform debugfs directory\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000308 return;
309 }
310
311 snd_soc_dapm_debugfs_init(&platform->dapm,
312 platform->debugfs_platform_root);
313}
314
315static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
316{
317 debugfs_remove_recursive(platform->debugfs_platform_root);
318}
319
Mark Brownc3c5a192010-09-15 18:15:14 +0100320static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
321 size_t count, loff_t *ppos)
322{
323 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100324 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100325 struct snd_soc_codec *codec;
326
327 if (!buf)
328 return -ENOMEM;
329
Mark Brown2b194f9d2010-10-13 10:52:16 +0100330 list_for_each_entry(codec, &codec_list, list) {
331 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
332 codec->name);
333 if (len >= 0)
334 ret += len;
335 if (ret > PAGE_SIZE) {
336 ret = PAGE_SIZE;
337 break;
338 }
339 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100340
341 if (ret >= 0)
342 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
343
344 kfree(buf);
345
346 return ret;
347}
348
349static const struct file_operations codec_list_fops = {
350 .read = codec_list_read_file,
351 .llseek = default_llseek,/* read accesses f_pos */
352};
353
Mark Brownf3208782010-09-15 18:19:07 +0100354static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
355 size_t count, loff_t *ppos)
356{
357 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100358 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100359 struct snd_soc_dai *dai;
360
361 if (!buf)
362 return -ENOMEM;
363
Mark Brown2b194f9d2010-10-13 10:52:16 +0100364 list_for_each_entry(dai, &dai_list, list) {
365 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
366 if (len >= 0)
367 ret += len;
368 if (ret > PAGE_SIZE) {
369 ret = PAGE_SIZE;
370 break;
371 }
372 }
Mark Brownf3208782010-09-15 18:19:07 +0100373
Mark Brown2b194f9d2010-10-13 10:52:16 +0100374 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100375
376 kfree(buf);
377
378 return ret;
379}
380
381static const struct file_operations dai_list_fops = {
382 .read = dai_list_read_file,
383 .llseek = default_llseek,/* read accesses f_pos */
384};
385
Mark Brown19c7ac22010-09-15 18:22:40 +0100386static ssize_t platform_list_read_file(struct file *file,
387 char __user *user_buf,
388 size_t count, loff_t *ppos)
389{
390 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100391 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100392 struct snd_soc_platform *platform;
393
394 if (!buf)
395 return -ENOMEM;
396
Mark Brown2b194f9d2010-10-13 10:52:16 +0100397 list_for_each_entry(platform, &platform_list, list) {
398 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
399 platform->name);
400 if (len >= 0)
401 ret += len;
402 if (ret > PAGE_SIZE) {
403 ret = PAGE_SIZE;
404 break;
405 }
406 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100407
Mark Brown2b194f9d2010-10-13 10:52:16 +0100408 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100409
410 kfree(buf);
411
412 return ret;
413}
414
415static const struct file_operations platform_list_fops = {
416 .read = platform_list_read_file,
417 .llseek = default_llseek,/* read accesses f_pos */
418};
419
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200420static void soc_init_card_debugfs(struct snd_soc_card *card)
421{
422 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000423 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200424 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200425 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100426 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200427 return;
428 }
429
430 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
431 card->debugfs_card_root,
432 &card->pop_time);
433 if (!card->debugfs_pop_time)
434 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000435 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200436}
437
438static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
439{
440 debugfs_remove_recursive(card->debugfs_card_root);
441}
442
Mark Brown2624d5f2009-11-03 21:56:13 +0000443#else
444
445static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
446{
447}
448
449static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
450{
451}
Axel Linb95fccb2010-11-09 17:06:44 +0800452
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000453static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
454{
455}
456
457static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
458{
459}
460
Axel Linb95fccb2010-11-09 17:06:44 +0800461static inline void soc_init_card_debugfs(struct snd_soc_card *card)
462{
463}
464
465static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
466{
467}
Mark Brown2624d5f2009-11-03 21:56:13 +0000468#endif
469
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100470struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
471 const char *dai_link, int stream)
472{
473 int i;
474
475 for (i = 0; i < card->num_links; i++) {
476 if (card->rtd[i].dai_link->no_pcm &&
477 !strcmp(card->rtd[i].dai_link->name, dai_link))
478 return card->rtd[i].pcm->streams[stream].substream;
479 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000480 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100481 return NULL;
482}
483EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
484
485struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
486 const char *dai_link)
487{
488 int i;
489
490 for (i = 0; i < card->num_links; i++) {
491 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
492 return &card->rtd[i];
493 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000494 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100495 return NULL;
496}
497EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
498
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200499#ifdef CONFIG_SND_SOC_AC97_BUS
500/* unregister ac97 codec */
501static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
502{
503 if (codec->ac97->dev.bus)
504 device_unregister(&codec->ac97->dev);
505 return 0;
506}
507
508/* stop no dev release warning */
509static void soc_ac97_device_release(struct device *dev){}
510
511/* register ac97 codec to bus */
512static int soc_ac97_dev_register(struct snd_soc_codec *codec)
513{
514 int err;
515
516 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100517 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200518 codec->ac97->dev.release = soc_ac97_device_release;
519
Kay Sieversbb072bf2008-11-02 03:50:35 +0100520 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000521 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200522 err = device_register(&codec->ac97->dev);
523 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000524 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200525 codec->ac97->dev.bus = NULL;
526 return err;
527 }
528 return 0;
529}
530#endif
531
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000532#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200533/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000534int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200535{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000536 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200537 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200538 int i;
539
Daniel Macke3509ff2009-06-03 17:44:49 +0200540 /* If the initialization of this soc device failed, there is no codec
541 * associated with it. Just bail out in this case.
542 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000543 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200544 return 0;
545
Andy Green6ed25972008-06-13 16:24:05 +0100546 /* Due to the resume being scheduled into a workqueue we could
547 * suspend before that's finished - wait for it to complete.
548 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000549 snd_power_lock(card->snd_card);
550 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
551 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100552
553 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000554 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100555
Mark Browna00f90f2010-12-02 16:24:24 +0000556 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000557 for (i = 0; i < card->num_rtd; i++) {
558 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
559 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100560
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000561 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100562 continue;
563
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000564 if (drv->ops->digital_mute && dai->playback_active)
565 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200566 }
567
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100568 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569 for (i = 0; i < card->num_rtd; i++) {
570 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100571 continue;
572
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000573 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100574 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100575
Mark Brown87506542008-11-18 20:50:34 +0000576 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000577 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200578
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000579 for (i = 0; i < card->num_rtd; i++) {
580 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
581 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100582
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000583 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100584 continue;
585
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000586 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
587 cpu_dai->driver->suspend(cpu_dai);
588 if (platform->driver->suspend && !platform->suspended) {
589 platform->driver->suspend(cpu_dai);
590 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100591 }
592 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200593
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000594 /* close any waiting streams and save state */
595 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo43829732012-08-20 14:51:24 -0700596 flush_delayed_work(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200597 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100599
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000600 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000601
602 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100603 continue;
604
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800605 snd_soc_dapm_stream_event(&card->rtd[i],
606 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800607 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800609 snd_soc_dapm_stream_event(&card->rtd[i],
610 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800611 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000612 }
613
Mark Browne2d32ff2012-08-31 17:38:32 -0700614 /* Recheck all analogue paths too */
615 dapm_mark_io_dirty(&card->dapm);
616 snd_soc_dapm_sync(&card->dapm);
617
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000618 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200619 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000620 /* If there are paths active then the CODEC will be held with
621 * bias _ON and should not be suspended. */
622 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200623 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000624 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000625 /*
626 * If the CODEC is capable of idle
627 * bias off then being in STANDBY
628 * means it's doing something,
629 * otherwise fall through.
630 */
631 if (codec->dapm.idle_bias_off) {
632 dev_dbg(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000633 "ASoC: idle_bias_off CODEC on"
634 " over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000635 break;
636 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000637 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100638 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900640 codec->cache_sync = 1;
Mark Brownda8b8e02012-09-12 12:21:52 +0800641 if (codec->using_regmap)
642 regcache_mark_dirty(codec->control_data);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000643 break;
644 default:
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000645 dev_dbg(codec->dev, "ASoC: CODEC is on"
646 " over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000647 break;
648 }
649 }
650 }
651
652 for (i = 0; i < card->num_rtd; i++) {
653 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
654
655 if (card->rtd[i].dai_link->ignore_suspend)
656 continue;
657
658 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
659 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200660 }
661
Mark Brown87506542008-11-18 20:50:34 +0000662 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000663 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200664
665 return 0;
666}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000667EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200668
Andy Green6ed25972008-06-13 16:24:05 +0100669/* deferred resume work, so resume can complete before we finished
670 * setting our codec back up, which can be very slow on I2C
671 */
672static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200673{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000674 struct snd_soc_card *card =
675 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200676 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200677 int i;
678
Andy Green6ed25972008-06-13 16:24:05 +0100679 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
680 * so userspace apps are blocked from touching us
681 */
682
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000683 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100684
Mark Brown9949788b2010-05-07 20:24:05 +0100685 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000686 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100687
Mark Brown87506542008-11-18 20:50:34 +0000688 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000689 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000691 /* resume AC97 DAIs */
692 for (i = 0; i < card->num_rtd; i++) {
693 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100694
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000695 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100696 continue;
697
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000698 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
699 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200700 }
701
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200702 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000703 /* If the CODEC was idle over suspend then it will have been
704 * left with bias OFF or STANDBY and suspended so we must now
705 * resume. Otherwise the suspend was suppressed.
706 */
707 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200708 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000709 case SND_SOC_BIAS_STANDBY:
710 case SND_SOC_BIAS_OFF:
711 codec->driver->resume(codec);
712 codec->suspended = 0;
713 break;
714 default:
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000715 dev_dbg(codec->dev, "ASoC: CODEC was on over"
716 " suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000717 break;
718 }
Mark Brown1547aba2010-05-07 21:11:40 +0100719 }
720 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200721
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000722 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100723
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000724 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100725 continue;
726
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800727 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000728 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800729 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000730
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800731 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000732 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800733 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200734 }
735
Mark Brown3ff3f642008-05-19 12:32:25 +0200736 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000737 for (i = 0; i < card->num_rtd; i++) {
738 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
739 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100740
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000741 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100742 continue;
743
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000744 if (drv->ops->digital_mute && dai->playback_active)
745 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200746 }
747
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000748 for (i = 0; i < card->num_rtd; i++) {
749 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
750 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100751
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000752 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100753 continue;
754
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000755 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
756 cpu_dai->driver->resume(cpu_dai);
757 if (platform->driver->resume && platform->suspended) {
758 platform->driver->resume(cpu_dai);
759 platform->suspended = 0;
760 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200761 }
762
Mark Brown87506542008-11-18 20:50:34 +0000763 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000764 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200765
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000766 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100767
768 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000769 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700770
771 /* Recheck all analogue paths too */
772 dapm_mark_io_dirty(&card->dapm);
773 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100774}
775
776/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000777int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100778{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000779 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600780 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200781
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800782 /* If the initialization of this soc device failed, there is no codec
783 * associated with it. Just bail out in this case.
784 */
785 if (list_empty(&card->codec_dev_list))
786 return 0;
787
Mark Brown64ab9ba2009-03-31 11:27:03 +0100788 /* AC97 devices might have other drivers hanging off them so
789 * need to resume immediately. Other drivers don't have that
790 * problem and may take a substantial amount of time to resume
791 * due to I/O costs and anti-pop so handle them out of line.
792 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000793 for (i = 0; i < card->num_rtd; i++) {
794 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600795 ac97_control |= cpu_dai->driver->ac97_control;
796 }
797 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000798 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600799 soc_resume_deferred(&card->deferred_resume_work);
800 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000801 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600802 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000803 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100804 }
Andy Green6ed25972008-06-13 16:24:05 +0100805
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200806 return 0;
807}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000808EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200809#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000810#define snd_soc_suspend NULL
811#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200812#endif
813
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100814static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800815};
816
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000817static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200818{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000819 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
820 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000821 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000822 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000823 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100824 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200825
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000826 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000827
Mark Brownb19e6e72012-03-14 21:18:39 +0000828 /* Find CPU DAI from registered DAIs*/
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000829 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600830 if (dai_link->cpu_of_node &&
831 (cpu_dai->dev->of_node != dai_link->cpu_of_node))
832 continue;
833 if (dai_link->cpu_name &&
834 strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
835 continue;
836 if (dai_link->cpu_dai_name &&
837 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
838 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700839
840 rtd->cpu_dai = cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000841 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000842
843 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000844 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000845 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000846 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000847 }
848
Mark Brownb19e6e72012-03-14 21:18:39 +0000849 /* Find CODEC from registered CODECs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000850 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700851 if (dai_link->codec_of_node) {
852 if (codec->dev->of_node != dai_link->codec_of_node)
853 continue;
854 } else {
855 if (strcmp(codec->name, dai_link->codec_name))
856 continue;
857 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000858
Stephen Warren2610ab72011-12-07 13:58:27 -0700859 rtd->codec = codec;
860
861 /*
862 * CODEC found, so find CODEC DAI from registered DAIs from
863 * this CODEC
864 */
865 list_for_each_entry(codec_dai, &dai_list, list) {
866 if (codec->dev == codec_dai->dev &&
867 !strcmp(codec_dai->name,
868 dai_link->codec_dai_name)) {
869
870 rtd->codec_dai = codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000871 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000872 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000873
874 if (!rtd->codec_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000875 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
Stephen Warren2610ab72011-12-07 13:58:27 -0700876 dai_link->codec_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000877 return -EPROBE_DEFER;
878 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000879 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000880
Mark Brownb19e6e72012-03-14 21:18:39 +0000881 if (!rtd->codec) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000882 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
Mark Brownb19e6e72012-03-14 21:18:39 +0000883 dai_link->codec_name);
884 return -EPROBE_DEFER;
885 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100886
887 /* if there's no platform we match on the empty platform */
888 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700889 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100890 platform_name = "snd-soc-dummy";
891
Mark Brownb19e6e72012-03-14 21:18:39 +0000892 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000893 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700894 if (dai_link->platform_of_node) {
895 if (platform->dev->of_node !=
896 dai_link->platform_of_node)
897 continue;
898 } else {
899 if (strcmp(platform->name, platform_name))
900 continue;
901 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700902
903 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000904 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000905 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000906 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000907 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000908 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000909 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000910
911 card->num_rtd++;
912
913 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000914}
915
Stephen Warrend12cd192012-06-08 12:34:22 -0600916static int soc_remove_platform(struct snd_soc_platform *platform)
917{
918 int ret;
919
920 if (platform->driver->remove) {
921 ret = platform->driver->remove(platform);
922 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000923 dev_err(platform->dev, "ASoC: failed to remove %d\n",
924 ret);
Stephen Warrend12cd192012-06-08 12:34:22 -0600925 }
926
927 /* Make sure all DAPM widgets are freed */
928 snd_soc_dapm_free(&platform->dapm);
929
930 soc_cleanup_platform_debugfs(platform);
931 platform->probed = 0;
932 list_del(&platform->card_list);
933 module_put(platform->dev->driver->owner);
934
935 return 0;
936}
937
Jarkko Nikula589c3562010-12-06 16:27:07 +0200938static void soc_remove_codec(struct snd_soc_codec *codec)
939{
940 int err;
941
942 if (codec->driver->remove) {
943 err = codec->driver->remove(codec);
944 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000945 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200946 }
947
948 /* Make sure all DAPM widgets are freed */
949 snd_soc_dapm_free(&codec->dapm);
950
951 soc_cleanup_codec_debugfs(codec);
952 codec->probed = 0;
953 list_del(&codec->card_list);
954 module_put(codec->dev->driver->owner);
955}
956
Stephen Warren62ae68f2012-06-08 12:34:23 -0600957static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000958{
959 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000960 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
961 int err;
962
963 /* unregister the rtd device */
964 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800965 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
966 device_remove_file(rtd->dev, &dev_attr_codec_reg);
967 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000968 rtd->dev_registered = 0;
969 }
970
971 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100972 if (codec_dai && codec_dai->probed &&
973 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000974 if (codec_dai->driver->remove) {
975 err = codec_dai->driver->remove(codec_dai);
976 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000977 dev_err(codec_dai->dev,
978 "ASoC: failed to remove %s: %d\n",
979 codec_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000980 }
981 codec_dai->probed = 0;
982 list_del(&codec_dai->card_list);
983 }
984
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000985 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100986 if (cpu_dai && cpu_dai->probed &&
987 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000988 if (cpu_dai->driver->remove) {
989 err = cpu_dai->driver->remove(cpu_dai);
990 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000991 dev_err(cpu_dai->dev,
992 "ASoC: failed to remove %s: %d\n",
993 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000994 }
995 cpu_dai->probed = 0;
996 list_del(&cpu_dai->card_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -0600997
Stephen Warren18d75642012-06-08 12:34:21 -0600998 if (!cpu_dai->codec) {
999 snd_soc_dapm_free(&cpu_dai->dapm);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001000 module_put(cpu_dai->dev->driver->owner);
Stephen Warren18d75642012-06-08 12:34:21 -06001001 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001002 }
1003}
1004
Stephen Warren62ae68f2012-06-08 12:34:23 -06001005static void soc_remove_link_components(struct snd_soc_card *card, int num,
1006 int order)
1007{
1008 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1009 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1010 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1011 struct snd_soc_platform *platform = rtd->platform;
1012 struct snd_soc_codec *codec;
1013
1014 /* remove the platform */
1015 if (platform && platform->probed &&
1016 platform->driver->remove_order == order) {
1017 soc_remove_platform(platform);
1018 }
1019
1020 /* remove the CODEC-side CODEC */
1021 if (codec_dai) {
1022 codec = codec_dai->codec;
1023 if (codec && codec->probed &&
1024 codec->driver->remove_order == order)
1025 soc_remove_codec(codec);
1026 }
1027
1028 /* remove any CPU-side CODEC */
1029 if (cpu_dai) {
1030 codec = cpu_dai->codec;
1031 if (codec && codec->probed &&
1032 codec->driver->remove_order == order)
1033 soc_remove_codec(codec);
1034 }
1035}
1036
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001037static void soc_remove_dai_links(struct snd_soc_card *card)
1038{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001039 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001040
Liam Girdwood0168bf02011-06-07 16:08:05 +01001041 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1042 order++) {
1043 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001044 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001045 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001046
1047 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1048 order++) {
1049 for (dai = 0; dai < card->num_rtd; dai++)
1050 soc_remove_link_components(card, dai, order);
1051 }
1052
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001053 card->num_rtd = 0;
1054}
1055
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001056static void soc_set_name_prefix(struct snd_soc_card *card,
1057 struct snd_soc_codec *codec)
1058{
1059 int i;
1060
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001061 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001062 return;
1063
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001064 for (i = 0; i < card->num_configs; i++) {
1065 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001066 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1067 codec->name_prefix = map->name_prefix;
1068 break;
1069 }
1070 }
1071}
1072
Jarkko Nikula589c3562010-12-06 16:27:07 +02001073static int soc_probe_codec(struct snd_soc_card *card,
1074 struct snd_soc_codec *codec)
1075{
1076 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001077 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001078 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001079
1080 codec->card = card;
1081 codec->dapm.card = card;
1082 soc_set_name_prefix(card, codec);
1083
Jarkko Nikula70d293312011-01-27 16:24:22 +02001084 if (!try_module_get(codec->dev->driver->owner))
1085 return -ENODEV;
1086
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001087 soc_init_codec_debugfs(codec);
1088
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001089 if (driver->dapm_widgets)
1090 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1091 driver->num_dapm_widgets);
1092
Mark Brown888df392012-02-16 19:37:51 -08001093 /* Create DAPM widgets for each DAI stream */
1094 list_for_each_entry(dai, &dai_list, list) {
1095 if (dai->dev != codec->dev)
1096 continue;
1097
1098 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1099 }
1100
Mark Brown33c5f962011-08-22 18:40:30 +01001101 codec->dapm.idle_bias_off = driver->idle_bias_off;
1102
Mark Brown89b95ac02011-03-07 16:38:44 +00001103 if (driver->probe) {
1104 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001105 if (ret < 0) {
1106 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001107 "ASoC: failed to probe CODEC %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001108 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001109 }
Chuansheng Liuff541f42012-12-21 18:17:12 +08001110 WARN(codec->dapm.idle_bias_off &&
1111 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
1112 "codec %s can not start from non-off bias"
1113 " with idle_bias_off==1\n", codec->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001114 }
1115
Mark Brown38cbf952012-06-22 13:04:02 +01001116 /* If the driver didn't set I/O up try regmap */
Mark Brown98d30882012-08-01 20:05:47 +01001117 if (!codec->write && dev_get_regmap(codec->dev, NULL))
Mark Brown38cbf952012-06-22 13:04:02 +01001118 snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
1119
Mark Brownb7af1da2011-04-07 19:18:44 +09001120 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001121 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001122 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001123 if (driver->dapm_routes)
1124 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1125 driver->num_dapm_routes);
1126
Jarkko Nikula589c3562010-12-06 16:27:07 +02001127 /* mark codec as probed and add to card codec list */
1128 codec->probed = 1;
1129 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001130 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001131
Jarkko Nikula70d293312011-01-27 16:24:22 +02001132 return 0;
1133
1134err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001135 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001136 module_put(codec->dev->driver->owner);
1137
Jarkko Nikula589c3562010-12-06 16:27:07 +02001138 return ret;
1139}
1140
Liam Girdwood956245e2011-07-01 16:54:08 +01001141static int soc_probe_platform(struct snd_soc_card *card,
1142 struct snd_soc_platform *platform)
1143{
1144 int ret = 0;
1145 const struct snd_soc_platform_driver *driver = platform->driver;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001146 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001147
1148 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001149 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001150
1151 if (!try_module_get(platform->dev->driver->owner))
1152 return -ENODEV;
1153
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001154 soc_init_platform_debugfs(platform);
1155
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001156 if (driver->dapm_widgets)
1157 snd_soc_dapm_new_controls(&platform->dapm,
1158 driver->dapm_widgets, driver->num_dapm_widgets);
1159
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001160 /* Create DAPM widgets for each DAI stream */
1161 list_for_each_entry(dai, &dai_list, list) {
1162 if (dai->dev != platform->dev)
1163 continue;
1164
1165 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1166 }
1167
Stephen Warren3fec6b62012-04-05 12:28:01 -06001168 platform->dapm.idle_bias_off = 1;
1169
Liam Girdwood956245e2011-07-01 16:54:08 +01001170 if (driver->probe) {
1171 ret = driver->probe(platform);
1172 if (ret < 0) {
1173 dev_err(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001174 "ASoC: failed to probe platform %d\n", ret);
Liam Girdwood956245e2011-07-01 16:54:08 +01001175 goto err_probe;
1176 }
1177 }
1178
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001179 if (driver->controls)
1180 snd_soc_add_platform_controls(platform, driver->controls,
1181 driver->num_controls);
1182 if (driver->dapm_routes)
1183 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1184 driver->num_dapm_routes);
1185
Liam Girdwood956245e2011-07-01 16:54:08 +01001186 /* mark platform as probed and add to card platform list */
1187 platform->probed = 1;
1188 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001189 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001190
1191 return 0;
1192
1193err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001194 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001195 module_put(platform->dev->driver->owner);
1196
1197 return ret;
1198}
1199
Mark Brown36ae1a92012-01-06 17:12:45 -08001200static void rtd_release(struct device *dev)
1201{
1202 kfree(dev);
1203}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001204
Jarkko Nikula589c3562010-12-06 16:27:07 +02001205static int soc_post_component_init(struct snd_soc_card *card,
1206 struct snd_soc_codec *codec,
1207 int num, int dailess)
1208{
1209 struct snd_soc_dai_link *dai_link = NULL;
1210 struct snd_soc_aux_dev *aux_dev = NULL;
1211 struct snd_soc_pcm_runtime *rtd;
1212 const char *temp, *name;
1213 int ret = 0;
1214
1215 if (!dailess) {
1216 dai_link = &card->dai_link[num];
1217 rtd = &card->rtd[num];
1218 name = dai_link->name;
1219 } else {
1220 aux_dev = &card->aux_dev[num];
1221 rtd = &card->rtd_aux[num];
1222 name = aux_dev->name;
1223 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001224 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001225
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001226 /* Make sure all DAPM widgets are instantiated */
1227 snd_soc_dapm_new_widgets(&codec->dapm);
1228
Jarkko Nikula589c3562010-12-06 16:27:07 +02001229 /* machine controls, routes and widgets are not prefixed */
1230 temp = codec->name_prefix;
1231 codec->name_prefix = NULL;
1232
1233 /* do machine specific initialization */
1234 if (!dailess && dai_link->init)
1235 ret = dai_link->init(rtd);
1236 else if (dailess && aux_dev->init)
1237 ret = aux_dev->init(&codec->dapm);
1238 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001239 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001240 return ret;
1241 }
1242 codec->name_prefix = temp;
1243
Jarkko Nikula589c3562010-12-06 16:27:07 +02001244 /* register the rtd device */
1245 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001246
1247 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1248 if (!rtd->dev)
1249 return -ENOMEM;
1250 device_initialize(rtd->dev);
1251 rtd->dev->parent = card->dev;
1252 rtd->dev->release = rtd_release;
1253 rtd->dev->init_name = name;
1254 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001255 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001256 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1257 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1258 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1259 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001260 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001261 if (ret < 0) {
1262 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001263 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001264 return ret;
1265 }
1266 rtd->dev_registered = 1;
1267
1268 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001269 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001270 if (ret < 0)
1271 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001272 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001273
1274 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001275 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001276 if (ret < 0)
1277 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001278 "ASoC: failed to add codec sysfs files: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001279
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001280#ifdef CONFIG_DEBUG_FS
1281 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001282 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001283 goto out;
1284
1285 ret = soc_dpcm_debugfs_add(rtd);
1286 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001287 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001288
1289out:
1290#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001291 return 0;
1292}
1293
Stephen Warren62ae68f2012-06-08 12:34:23 -06001294static int soc_probe_link_components(struct snd_soc_card *card, int num,
1295 int order)
1296{
1297 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1298 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1299 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1300 struct snd_soc_platform *platform = rtd->platform;
1301 int ret;
1302
1303 /* probe the CPU-side component, if it is a CODEC */
1304 if (cpu_dai->codec &&
1305 !cpu_dai->codec->probed &&
1306 cpu_dai->codec->driver->probe_order == order) {
1307 ret = soc_probe_codec(card, cpu_dai->codec);
1308 if (ret < 0)
1309 return ret;
1310 }
1311
1312 /* probe the CODEC-side component */
1313 if (!codec_dai->codec->probed &&
1314 codec_dai->codec->driver->probe_order == order) {
1315 ret = soc_probe_codec(card, codec_dai->codec);
1316 if (ret < 0)
1317 return ret;
1318 }
1319
1320 /* probe the platform */
1321 if (!platform->probed &&
1322 platform->driver->probe_order == order) {
1323 ret = soc_probe_platform(card, platform);
1324 if (ret < 0)
1325 return ret;
1326 }
1327
1328 return 0;
1329}
1330
1331static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001332{
1333 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1334 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1335 struct snd_soc_codec *codec = rtd->codec;
1336 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001337 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1338 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1339 struct snd_soc_dapm_widget *play_w, *capture_w;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001340 int ret;
1341
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001342 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001343 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001344
1345 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001346 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001347 codec_dai->card = card;
1348 cpu_dai->card = card;
1349
1350 /* set default power off timeout */
1351 rtd->pmdown_time = pmdown_time;
1352
1353 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001354 if (!cpu_dai->probed &&
1355 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001356 if (!cpu_dai->codec) {
1357 cpu_dai->dapm.card = card;
1358 if (!try_module_get(cpu_dai->dev->driver->owner))
1359 return -ENODEV;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001360
Stephen Warren18d75642012-06-08 12:34:21 -06001361 list_add(&cpu_dai->dapm.list, &card->dapm_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001362 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1363 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001364
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001365 if (cpu_dai->driver->probe) {
1366 ret = cpu_dai->driver->probe(cpu_dai);
1367 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001368 dev_err(cpu_dai->dev,
1369 "ASoC: failed to probe CPU DAI %s: %d\n",
1370 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001371 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001372 return ret;
1373 }
1374 }
1375 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001376 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001377 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1378 }
1379
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001380 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001381 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001382 if (codec_dai->driver->probe) {
1383 ret = codec_dai->driver->probe(codec_dai);
1384 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001385 dev_err(codec_dai->dev,
1386 "ASoC: failed to probe CODEC DAI %s: %d\n",
1387 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001388 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001389 }
1390 }
1391
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001392 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001393 codec_dai->probed = 1;
1394 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001395 }
1396
Liam Girdwood0168bf02011-06-07 16:08:05 +01001397 /* complete DAI probe during last probe */
1398 if (order != SND_SOC_COMP_ORDER_LAST)
1399 return 0;
1400
Jarkko Nikula589c3562010-12-06 16:27:07 +02001401 ret = soc_post_component_init(card, codec, num, 0);
1402 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001403 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001404
Mark Brown36ae1a92012-01-06 17:12:45 -08001405 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001406 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001407 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1408 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001409
Namarta Kohli1245b702012-08-16 17:10:41 +05301410 if (cpu_dai->driver->compress_dai) {
1411 /*create compress_device"*/
1412 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001413 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001414 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301415 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001416 return ret;
1417 }
1418 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301419
1420 if (!dai_link->params) {
1421 /* create the pcm */
1422 ret = soc_new_pcm(rtd, num);
1423 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001424 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301425 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001426 return ret;
1427 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301428 } else {
1429 /* link the DAI widgets */
1430 play_w = codec_dai->playback_widget;
1431 capture_w = cpu_dai->capture_widget;
1432 if (play_w && capture_w) {
1433 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Mark Brownc74184e2012-04-04 22:12:09 +01001434 capture_w, play_w);
Namarta Kohli1245b702012-08-16 17:10:41 +05301435 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001436 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301437 play_w->name, capture_w->name, ret);
1438 return ret;
1439 }
1440 }
1441
1442 play_w = cpu_dai->playback_widget;
1443 capture_w = codec_dai->capture_widget;
1444 if (play_w && capture_w) {
1445 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1446 capture_w, play_w);
1447 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001448 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301449 play_w->name, capture_w->name, ret);
1450 return ret;
1451 }
Mark Brownc74184e2012-04-04 22:12:09 +01001452 }
1453 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001454 }
1455
1456 /* add platform data for AC97 devices */
1457 if (rtd->codec_dai->driver->ac97_control)
1458 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1459
1460 return 0;
1461}
1462
1463#ifdef CONFIG_SND_SOC_AC97_BUS
1464static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1465{
1466 int ret;
1467
1468 /* Only instantiate AC97 if not already done by the adaptor
1469 * for the generic AC97 subsystem.
1470 */
1471 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001472 /*
1473 * It is possible that the AC97 device is already registered to
1474 * the device subsystem. This happens when the device is created
1475 * via snd_ac97_mixer(). Currently only SoC codec that does so
1476 * is the generic AC97 glue but others migh emerge.
1477 *
1478 * In those cases we don't try to register the device again.
1479 */
1480 if (!rtd->codec->ac97_created)
1481 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001482
1483 ret = soc_ac97_dev_register(rtd->codec);
1484 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001485 dev_err(rtd->codec->dev,
1486 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001487 return ret;
1488 }
1489
1490 rtd->codec->ac97_registered = 1;
1491 }
1492 return 0;
1493}
1494
1495static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1496{
1497 if (codec->ac97_registered) {
1498 soc_ac97_dev_unregister(codec);
1499 codec->ac97_registered = 0;
1500 }
1501}
1502#endif
1503
Mark Brownb19e6e72012-03-14 21:18:39 +00001504static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1505{
1506 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1507 struct snd_soc_codec *codec;
1508
1509 /* find CODEC from registered CODECs*/
1510 list_for_each_entry(codec, &codec_list, list) {
1511 if (!strcmp(codec->name, aux_dev->codec_name))
1512 return 0;
1513 }
1514
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001515 dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
Mark Brownfb099cb2012-08-09 18:44:37 +01001516
Mark Brownb19e6e72012-03-14 21:18:39 +00001517 return -EPROBE_DEFER;
1518}
1519
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001520static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1521{
1522 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001523 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001524 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001525
1526 /* find CODEC from registered CODECs*/
1527 list_for_each_entry(codec, &codec_list, list) {
1528 if (!strcmp(codec->name, aux_dev->codec_name)) {
1529 if (codec->probed) {
1530 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001531 "ASoC: codec already probed");
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001532 ret = -EBUSY;
1533 goto out;
1534 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001535 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001536 }
1537 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001538 /* codec not found */
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001539 dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001540 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001541
Jarkko Nikula676ad982010-12-03 09:18:22 +02001542found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001543 ret = soc_probe_codec(card, codec);
1544 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001545 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001546
Jarkko Nikula589c3562010-12-06 16:27:07 +02001547 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001548
1549out:
1550 return ret;
1551}
1552
1553static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1554{
1555 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1556 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001557
1558 /* unregister the rtd device */
1559 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001560 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1561 device_del(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001562 rtd->dev_registered = 0;
1563 }
1564
Jarkko Nikula589c3562010-12-06 16:27:07 +02001565 if (codec && codec->probed)
1566 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001567}
1568
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001569static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1570 enum snd_soc_compress_type compress_type)
1571{
1572 int ret;
1573
1574 if (codec->cache_init)
1575 return 0;
1576
1577 /* override the compress_type if necessary */
1578 if (compress_type && codec->compress_type != compress_type)
1579 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001580 ret = snd_soc_cache_init(codec);
1581 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001582 dev_err(codec->dev, "ASoC: Failed to set cache compression"
1583 " type: %d\n", ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001584 return ret;
1585 }
1586 codec->cache_init = 1;
1587 return 0;
1588}
1589
Mark Brownb19e6e72012-03-14 21:18:39 +00001590static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001591{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001592 struct snd_soc_codec *codec;
1593 struct snd_soc_codec_conf *codec_conf;
1594 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001595 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001596 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001597
Liam Girdwood01b9d992012-03-07 10:38:25 +00001598 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001599
Mark Brownb19e6e72012-03-14 21:18:39 +00001600 /* bind DAIs */
1601 for (i = 0; i < card->num_links; i++) {
1602 ret = soc_bind_dai_link(card, i);
1603 if (ret != 0)
1604 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001605 }
1606
Mark Brownb19e6e72012-03-14 21:18:39 +00001607 /* check aux_devs too */
1608 for (i = 0; i < card->num_aux_devs; i++) {
1609 ret = soc_check_aux_dev(card, i);
1610 if (ret != 0)
1611 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001612 }
1613
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001614 /* initialize the register cache for each available codec */
1615 list_for_each_entry(codec, &codec_list, list) {
1616 if (codec->cache_init)
1617 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001618 /* by default we don't override the compress_type */
1619 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001620 /* check to see if we need to override the compress_type */
1621 for (i = 0; i < card->num_configs; ++i) {
1622 codec_conf = &card->codec_conf[i];
1623 if (!strcmp(codec->name, codec_conf->dev_name)) {
1624 compress_type = codec_conf->compress_type;
1625 if (compress_type && compress_type
1626 != codec->compress_type)
1627 break;
1628 }
1629 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001630 ret = snd_soc_init_codec_cache(codec, compress_type);
Mark Brownb19e6e72012-03-14 21:18:39 +00001631 if (ret < 0)
1632 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001633 }
1634
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001635 /* card bind complete so register a sound card */
1636 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1637 card->owner, 0, &card->snd_card);
1638 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001639 dev_err(card->dev, "ASoC: can't create sound card for"
1640 " card %s: %d\n", card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001641 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001642 }
1643 card->snd_card->dev = card->dev;
1644
Mark Browne37a4972011-03-02 18:21:57 +00001645 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1646 card->dapm.dev = card->dev;
1647 card->dapm.card = card;
1648 list_add(&card->dapm.list, &card->dapm_list);
1649
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001650#ifdef CONFIG_DEBUG_FS
1651 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1652#endif
1653
Mark Brown88ee1c62011-02-02 10:43:26 +00001654#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001655 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001656 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001657#endif
Andy Green6ed25972008-06-13 16:24:05 +01001658
Mark Brown9a841eb2011-04-12 17:51:37 -07001659 if (card->dapm_widgets)
1660 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1661 card->num_dapm_widgets);
1662
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001663 /* initialise the sound card only once */
1664 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001665 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001666 if (ret < 0)
1667 goto card_probe_error;
1668 }
1669
Stephen Warren62ae68f2012-06-08 12:34:23 -06001670 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001671 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1672 order++) {
1673 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001674 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001675 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001676 dev_err(card->dev,
1677 "ASoC: failed to instantiate card %d\n",
1678 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001679 goto probe_dai_err;
1680 }
1681 }
1682 }
1683
1684 /* probe all DAI links on this card */
1685 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1686 order++) {
1687 for (i = 0; i < card->num_links; i++) {
1688 ret = soc_probe_link_dais(card, i, order);
1689 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001690 dev_err(card->dev,
1691 "ASoC: failed to instantiate card %d\n",
1692 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001693 goto probe_dai_err;
1694 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001695 }
1696 }
1697
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001698 for (i = 0; i < card->num_aux_devs; i++) {
1699 ret = soc_probe_aux_dev(card, i);
1700 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001701 dev_err(card->dev,
1702 "ASoC: failed to add auxiliary devices %d\n",
1703 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001704 goto probe_aux_dev_err;
1705 }
1706 }
1707
Mark Brown888df392012-02-16 19:37:51 -08001708 snd_soc_dapm_link_dai_widgets(card);
1709
Mark Brownb7af1da2011-04-07 19:18:44 +09001710 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001711 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001712
Mark Brownb8ad29d2011-03-02 18:35:51 +00001713 if (card->dapm_routes)
1714 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1715 card->num_dapm_routes);
1716
Mark Brownb90d2f92011-10-10 13:38:06 +01001717 snd_soc_dapm_new_widgets(&card->dapm);
1718
Mark Brown75d9ac42011-09-27 16:41:01 +01001719 for (i = 0; i < card->num_links; i++) {
1720 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001721 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001722
Mark Brownf04209a2012-04-09 16:29:21 +01001723 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001724 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001725 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001726 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001727 dev_warn(card->rtd[i].codec_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001728 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001729 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001730 }
1731
1732 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001733 if (dai_fmt &&
1734 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001735 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1736 dai_fmt);
1737 if (ret != 0 && ret != -ENOTSUPP)
1738 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001739 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001740 ret);
1741 } else if (dai_fmt) {
1742 /* Flip the polarity for the "CPU" end */
1743 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1744 switch (dai_link->dai_fmt &
1745 SND_SOC_DAIFMT_MASTER_MASK) {
1746 case SND_SOC_DAIFMT_CBM_CFM:
1747 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1748 break;
1749 case SND_SOC_DAIFMT_CBM_CFS:
1750 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1751 break;
1752 case SND_SOC_DAIFMT_CBS_CFM:
1753 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1754 break;
1755 case SND_SOC_DAIFMT_CBS_CFS:
1756 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1757 break;
1758 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001759
1760 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001761 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001762 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001763 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001764 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001765 ret);
1766 }
1767 }
1768
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001769 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001770 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001771 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1772 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001773 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1774 "%s", card->driver_name ? card->driver_name : card->name);
1775 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1776 switch (card->snd_card->driver[i]) {
1777 case '_':
1778 case '-':
1779 case '\0':
1780 break;
1781 default:
1782 if (!isalnum(card->snd_card->driver[i]))
1783 card->snd_card->driver[i] = '_';
1784 break;
1785 }
1786 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001787
Mark Brown28e9ad92011-03-02 18:36:34 +00001788 if (card->late_probe) {
1789 ret = card->late_probe(card);
1790 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001791 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001792 card->name, ret);
1793 goto probe_aux_dev_err;
1794 }
1795 }
1796
Mark Brown2dc00212011-10-08 13:59:44 +01001797 snd_soc_dapm_new_widgets(&card->dapm);
1798
Stephen Warren16332812011-11-23 12:42:04 -07001799 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001800 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001801 snd_soc_dapm_auto_nc_codec_pins(codec);
1802
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001803 ret = snd_card_register(card->snd_card);
1804 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001805 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1806 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001807 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001808 }
1809
1810#ifdef CONFIG_SND_SOC_AC97_BUS
1811 /* register any AC97 codecs */
1812 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001813 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1814 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001815 dev_err(card->dev, "ASoC: failed to register AC97:"
1816 " %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001817 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001818 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001819 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001820 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001821 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001822#endif
1823
Mark Brown435c5e22008-12-04 15:32:53 +00001824 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001825 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001826 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001827
1828 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001829
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001830probe_aux_dev_err:
1831 for (i = 0; i < card->num_aux_devs; i++)
1832 soc_remove_aux_dev(card, i);
1833
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001834probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001835 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001836
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001837card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001838 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001839 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001840
1841 snd_card_free(card->snd_card);
1842
Mark Brownb19e6e72012-03-14 21:18:39 +00001843base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001844 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001845
Mark Brownb19e6e72012-03-14 21:18:39 +00001846 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001847}
1848
1849/* probes a new socdev */
1850static int soc_probe(struct platform_device *pdev)
1851{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001852 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001853
Vinod Koul70a7ca32011-01-14 19:22:48 +05301854 /*
1855 * no card, so machine driver should be registering card
1856 * we should not be here in that case so ret error
1857 */
1858 if (!card)
1859 return -EINVAL;
1860
Mark Brownfe4085e2012-03-02 13:07:41 +00001861 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001862 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001863 card->name);
1864
Mark Brown435c5e22008-12-04 15:32:53 +00001865 /* Bodge while we unpick instantiation */
1866 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001867
Mark Brown28d528c2012-08-09 18:45:23 +01001868 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001869}
1870
Vinod Koulb0e26482011-01-13 22:48:02 +05301871static int soc_cleanup_card_resources(struct snd_soc_card *card)
1872{
Vinod Koulb0e26482011-01-13 22:48:02 +05301873 int i;
1874
1875 /* make sure any delayed work runs */
1876 for (i = 0; i < card->num_rtd; i++) {
1877 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001878 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301879 }
1880
1881 /* remove auxiliary devices */
1882 for (i = 0; i < card->num_aux_devs; i++)
1883 soc_remove_aux_dev(card, i);
1884
1885 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001886 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301887
1888 soc_cleanup_card_debugfs(card);
1889
1890 /* remove the card */
1891 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001892 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301893
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001894 snd_soc_dapm_free(&card->dapm);
1895
Vinod Koulb0e26482011-01-13 22:48:02 +05301896 snd_card_free(card->snd_card);
1897 return 0;
1898
1899}
1900
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001901/* removes a socdev */
1902static int soc_remove(struct platform_device *pdev)
1903{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001904 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001905
Mark Brownc5af3a22008-11-28 13:29:45 +00001906 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001907 return 0;
1908}
1909
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001910int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001911{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001912 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001913 int i;
Mark Brown51737472009-06-22 13:16:51 +01001914
1915 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001916 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001917
1918 /* Flush out pmdown_time work - we actually do want to run it
1919 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001920 for (i = 0; i < card->num_rtd; i++) {
1921 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001922 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001923 }
Mark Brown51737472009-06-22 13:16:51 +01001924
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001925 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001926
1927 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001928}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001929EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001930
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001931const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301932 .suspend = snd_soc_suspend,
1933 .resume = snd_soc_resume,
1934 .freeze = snd_soc_suspend,
1935 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001936 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301937 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001938};
Stephen Warrendeb26072011-04-05 19:35:30 -06001939EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001940
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001941/* ASoC platform driver */
1942static struct platform_driver soc_driver = {
1943 .driver = {
1944 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001945 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001946 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001947 },
1948 .probe = soc_probe,
1949 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001950};
1951
Mark Brown096e49d2009-07-05 15:12:22 +01001952/**
1953 * snd_soc_codec_volatile_register: Report if a register is volatile.
1954 *
1955 * @codec: CODEC to query.
1956 * @reg: Register to query.
1957 *
1958 * Boolean function indiciating if a CODEC register is volatile.
1959 */
Mark Brown181e0552011-01-24 14:05:25 +00001960int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1961 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001962{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001963 if (codec->volatile_register)
1964 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001965 else
1966 return 0;
1967}
1968EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1969
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001970/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001971 * snd_soc_codec_readable_register: Report if a register is readable.
1972 *
1973 * @codec: CODEC to query.
1974 * @reg: Register to query.
1975 *
1976 * Boolean function indicating if a CODEC register is readable.
1977 */
1978int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1979 unsigned int reg)
1980{
1981 if (codec->readable_register)
1982 return codec->readable_register(codec, reg);
1983 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001984 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001985}
1986EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1987
1988/**
1989 * snd_soc_codec_writable_register: Report if a register is writable.
1990 *
1991 * @codec: CODEC to query.
1992 * @reg: Register to query.
1993 *
1994 * Boolean function indicating if a CODEC register is writable.
1995 */
1996int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1997 unsigned int reg)
1998{
1999 if (codec->writable_register)
2000 return codec->writable_register(codec, reg);
2001 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02002002 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002003}
2004EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2005
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002006int snd_soc_platform_read(struct snd_soc_platform *platform,
2007 unsigned int reg)
2008{
2009 unsigned int ret;
2010
2011 if (!platform->driver->read) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002012 dev_err(platform->dev, "ASoC: platform has no read back\n");
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002013 return -1;
2014 }
2015
2016 ret = platform->driver->read(platform, reg);
2017 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002018 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002019
2020 return ret;
2021}
2022EXPORT_SYMBOL_GPL(snd_soc_platform_read);
2023
2024int snd_soc_platform_write(struct snd_soc_platform *platform,
2025 unsigned int reg, unsigned int val)
2026{
2027 if (!platform->driver->write) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002028 dev_err(platform->dev, "ASoC: platform has no write back\n");
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002029 return -1;
2030 }
2031
2032 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002033 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002034 return platform->driver->write(platform, reg, val);
2035}
2036EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2037
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002038/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002039 * snd_soc_new_ac97_codec - initailise AC97 device
2040 * @codec: audio codec
2041 * @ops: AC97 bus operations
2042 * @num: AC97 codec number
2043 *
2044 * Initialises AC97 codec resources for use by ad-hoc devices only.
2045 */
2046int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2047 struct snd_ac97_bus_ops *ops, int num)
2048{
2049 mutex_lock(&codec->mutex);
2050
2051 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2052 if (codec->ac97 == NULL) {
2053 mutex_unlock(&codec->mutex);
2054 return -ENOMEM;
2055 }
2056
2057 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2058 if (codec->ac97->bus == NULL) {
2059 kfree(codec->ac97);
2060 codec->ac97 = NULL;
2061 mutex_unlock(&codec->mutex);
2062 return -ENOMEM;
2063 }
2064
2065 codec->ac97->bus->ops = ops;
2066 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002067
2068 /*
2069 * Mark the AC97 device to be created by us. This way we ensure that the
2070 * device will be registered with the device subsystem later on.
2071 */
2072 codec->ac97_created = 1;
2073
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002074 mutex_unlock(&codec->mutex);
2075 return 0;
2076}
2077EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2078
2079/**
2080 * snd_soc_free_ac97_codec - free AC97 codec device
2081 * @codec: audio codec
2082 *
2083 * Frees AC97 codec device resources.
2084 */
2085void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2086{
2087 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002088#ifdef CONFIG_SND_SOC_AC97_BUS
2089 soc_unregister_ac97_dai_link(codec);
2090#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002091 kfree(codec->ac97->bus);
2092 kfree(codec->ac97);
2093 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002094 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002095 mutex_unlock(&codec->mutex);
2096}
2097EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2098
Mark Brownc3753702010-11-01 15:41:57 -04002099unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2100{
2101 unsigned int ret;
2102
Mark Brownc3acec22010-12-02 16:15:29 +00002103 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002104 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002105 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002106
2107 return ret;
2108}
2109EXPORT_SYMBOL_GPL(snd_soc_read);
2110
2111unsigned int snd_soc_write(struct snd_soc_codec *codec,
2112 unsigned int reg, unsigned int val)
2113{
2114 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002115 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002116 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002117}
2118EXPORT_SYMBOL_GPL(snd_soc_write);
2119
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00002120unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2121 unsigned int reg, const void *data, size_t len)
2122{
2123 return codec->bulk_write_raw(codec, reg, data, len);
2124}
2125EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2126
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002127/**
2128 * snd_soc_update_bits - update codec register bits
2129 * @codec: audio codec
2130 * @reg: codec register
2131 * @mask: register mask
2132 * @value: new value
2133 *
2134 * Writes new register value.
2135 *
Timur Tabi180c3292011-01-10 15:58:13 -06002136 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002137 */
2138int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002139 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002140{
Mark Brown8a713da2011-12-03 12:33:55 +00002141 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002142 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002143 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002144
Mark Brown8a713da2011-12-03 12:33:55 +00002145 if (codec->using_regmap) {
2146 ret = regmap_update_bits_check(codec->control_data, reg,
2147 mask, value, &change);
2148 } else {
2149 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06002150 if (ret < 0)
2151 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00002152
2153 old = ret;
2154 new = (old & ~mask) | (value & mask);
2155 change = old != new;
2156 if (change)
2157 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06002158 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002159
Mark Brown8a713da2011-12-03 12:33:55 +00002160 if (ret < 0)
2161 return ret;
2162
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002163 return change;
2164}
2165EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2166
2167/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002168 * snd_soc_update_bits_locked - update codec register bits
2169 * @codec: audio codec
2170 * @reg: codec register
2171 * @mask: register mask
2172 * @value: new value
2173 *
2174 * Writes new register value, and takes the codec mutex.
2175 *
2176 * Returns 1 for change else 0.
2177 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002178int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2179 unsigned short reg, unsigned int mask,
2180 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002181{
2182 int change;
2183
2184 mutex_lock(&codec->mutex);
2185 change = snd_soc_update_bits(codec, reg, mask, value);
2186 mutex_unlock(&codec->mutex);
2187
2188 return change;
2189}
Mark Browndd1b3d52009-12-04 14:22:03 +00002190EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002191
2192/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002193 * snd_soc_test_bits - test register for change
2194 * @codec: audio codec
2195 * @reg: codec register
2196 * @mask: register mask
2197 * @value: new value
2198 *
2199 * Tests a register with a new value and checks if the new value is
2200 * different from the old value.
2201 *
2202 * Returns 1 for change else 0.
2203 */
2204int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002205 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002206{
2207 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002208 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002209
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002210 old = snd_soc_read(codec, reg);
2211 new = (old & ~mask) | value;
2212 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002213
2214 return change;
2215}
2216EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2217
2218/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002219 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2220 * @substream: the pcm substream
2221 * @hw: the hardware parameters
2222 *
2223 * Sets the substream runtime hardware parameters.
2224 */
2225int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2226 const struct snd_pcm_hardware *hw)
2227{
2228 struct snd_pcm_runtime *runtime = substream->runtime;
2229 runtime->hw.info = hw->info;
2230 runtime->hw.formats = hw->formats;
2231 runtime->hw.period_bytes_min = hw->period_bytes_min;
2232 runtime->hw.period_bytes_max = hw->period_bytes_max;
2233 runtime->hw.periods_min = hw->periods_min;
2234 runtime->hw.periods_max = hw->periods_max;
2235 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2236 runtime->hw.fifo_size = hw->fifo_size;
2237 return 0;
2238}
2239EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2240
2241/**
2242 * 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;
2259 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002260
2261 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002262 template.index = 0;
2263
Mark Brownefb7ac32011-03-08 17:23:24 +00002264 if (!long_name)
2265 long_name = template.name;
2266
2267 if (prefix) {
2268 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08002269 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00002270 if (!name)
2271 return NULL;
2272
2273 snprintf(name, name_len, "%s %s", prefix, long_name);
2274
2275 template.name = name;
2276 } else {
2277 template.name = long_name;
2278 }
2279
2280 kcontrol = snd_ctl_new1(&template, data);
2281
2282 kfree(name);
2283
2284 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002285}
2286EXPORT_SYMBOL_GPL(snd_soc_cnew);
2287
Liam Girdwood022658b2012-02-03 17:43:09 +00002288static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2289 const struct snd_kcontrol_new *controls, int num_controls,
2290 const char *prefix, void *data)
2291{
2292 int err, i;
2293
2294 for (i = 0; i < num_controls; i++) {
2295 const struct snd_kcontrol_new *control = &controls[i];
2296 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2297 control->name, prefix));
2298 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002299 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2300 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002301 return err;
2302 }
2303 }
2304
2305 return 0;
2306}
2307
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002308/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002309 * snd_soc_add_codec_controls - add an array of controls to a codec.
2310 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002311 * duplicating this code.
2312 *
2313 * @codec: codec to add controls to
2314 * @controls: array of controls to add
2315 * @num_controls: number of elements in the array
2316 *
2317 * Return 0 for success, else error.
2318 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002319int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002320 const struct snd_kcontrol_new *controls, int num_controls)
2321{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002322 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002323
Liam Girdwood022658b2012-02-03 17:43:09 +00002324 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2325 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002326}
Liam Girdwood022658b2012-02-03 17:43:09 +00002327EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002328
2329/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002330 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002331 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002332 *
2333 * @platform: platform to add controls to
2334 * @controls: array of controls to add
2335 * @num_controls: number of elements in the array
2336 *
2337 * Return 0 for success, else error.
2338 */
2339int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2340 const struct snd_kcontrol_new *controls, int num_controls)
2341{
2342 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002343
Liam Girdwood022658b2012-02-03 17:43:09 +00002344 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2345 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002346}
2347EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2348
2349/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002350 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2351 * Convenience function to add a list of controls.
2352 *
2353 * @soc_card: SoC card to add controls to
2354 * @controls: array of controls to add
2355 * @num_controls: number of elements in the array
2356 *
2357 * Return 0 for success, else error.
2358 */
2359int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2360 const struct snd_kcontrol_new *controls, int num_controls)
2361{
2362 struct snd_card *card = soc_card->snd_card;
2363
2364 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2365 NULL, soc_card);
2366}
2367EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2368
2369/**
2370 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2371 * Convienience function to add a list of controls.
2372 *
2373 * @dai: DAI to add controls to
2374 * @controls: array of controls to add
2375 * @num_controls: number of elements in the array
2376 *
2377 * Return 0 for success, else error.
2378 */
2379int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2380 const struct snd_kcontrol_new *controls, int num_controls)
2381{
2382 struct snd_card *card = dai->card->snd_card;
2383
2384 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2385 NULL, dai);
2386}
2387EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2388
2389/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002390 * snd_soc_info_enum_double - enumerated double mixer info callback
2391 * @kcontrol: mixer control
2392 * @uinfo: control element information
2393 *
2394 * Callback to provide information about a double enumerated
2395 * mixer control.
2396 *
2397 * Returns 0 for success.
2398 */
2399int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2400 struct snd_ctl_elem_info *uinfo)
2401{
2402 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2403
2404 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2405 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002406 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002407
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002408 if (uinfo->value.enumerated.item > e->max - 1)
2409 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002410 strcpy(uinfo->value.enumerated.name,
2411 e->texts[uinfo->value.enumerated.item]);
2412 return 0;
2413}
2414EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2415
2416/**
2417 * snd_soc_get_enum_double - enumerated double mixer get callback
2418 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002419 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002420 *
2421 * Callback to get the value of a double enumerated mixer.
2422 *
2423 * Returns 0 for success.
2424 */
2425int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2426 struct snd_ctl_elem_value *ucontrol)
2427{
2428 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2429 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002430 unsigned int val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002431
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002432 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002433 ucontrol->value.enumerated.item[0]
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002434 = (val >> e->shift_l) & e->mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002435 if (e->shift_l != e->shift_r)
2436 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002437 (val >> e->shift_r) & e->mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002438
2439 return 0;
2440}
2441EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2442
2443/**
2444 * snd_soc_put_enum_double - enumerated double mixer put callback
2445 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002446 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002447 *
2448 * Callback to set the value of a double enumerated mixer.
2449 *
2450 * Returns 0 for success.
2451 */
2452int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2453 struct snd_ctl_elem_value *ucontrol)
2454{
2455 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2456 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002457 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002458 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002459
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002460 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002461 return -EINVAL;
2462 val = ucontrol->value.enumerated.item[0] << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002463 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002464 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002465 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002466 return -EINVAL;
2467 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002468 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002469 }
2470
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002471 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002472}
2473EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2474
2475/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002476 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2477 * @kcontrol: mixer control
2478 * @ucontrol: control element information
2479 *
2480 * Callback to get the value of a double semi enumerated mixer.
2481 *
2482 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2483 * used for handling bitfield coded enumeration for example.
2484 *
2485 * Returns 0 for success.
2486 */
2487int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2488 struct snd_ctl_elem_value *ucontrol)
2489{
2490 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002491 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002492 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002493
2494 reg_val = snd_soc_read(codec, e->reg);
2495 val = (reg_val >> e->shift_l) & e->mask;
2496 for (mux = 0; mux < e->max; mux++) {
2497 if (val == e->values[mux])
2498 break;
2499 }
2500 ucontrol->value.enumerated.item[0] = mux;
2501 if (e->shift_l != e->shift_r) {
2502 val = (reg_val >> e->shift_r) & e->mask;
2503 for (mux = 0; mux < e->max; mux++) {
2504 if (val == e->values[mux])
2505 break;
2506 }
2507 ucontrol->value.enumerated.item[1] = mux;
2508 }
2509
2510 return 0;
2511}
2512EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2513
2514/**
2515 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2516 * @kcontrol: mixer control
2517 * @ucontrol: control element information
2518 *
2519 * Callback to set the value of a double semi enumerated mixer.
2520 *
2521 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2522 * used for handling bitfield coded enumeration for example.
2523 *
2524 * Returns 0 for success.
2525 */
2526int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2527 struct snd_ctl_elem_value *ucontrol)
2528{
2529 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002530 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002531 unsigned int val;
2532 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002533
2534 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2535 return -EINVAL;
2536 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2537 mask = e->mask << e->shift_l;
2538 if (e->shift_l != e->shift_r) {
2539 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2540 return -EINVAL;
2541 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2542 mask |= e->mask << e->shift_r;
2543 }
2544
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002545 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002546}
2547EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2548
2549/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002550 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2551 * @kcontrol: mixer control
2552 * @uinfo: control element information
2553 *
2554 * Callback to provide information about an external enumerated
2555 * single mixer.
2556 *
2557 * Returns 0 for success.
2558 */
2559int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2560 struct snd_ctl_elem_info *uinfo)
2561{
2562 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2563
2564 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2565 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002566 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002567
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002568 if (uinfo->value.enumerated.item > e->max - 1)
2569 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002570 strcpy(uinfo->value.enumerated.name,
2571 e->texts[uinfo->value.enumerated.item]);
2572 return 0;
2573}
2574EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2575
2576/**
2577 * snd_soc_info_volsw_ext - external single mixer info callback
2578 * @kcontrol: mixer control
2579 * @uinfo: control element information
2580 *
2581 * Callback to provide information about a single external mixer control.
2582 *
2583 * Returns 0 for success.
2584 */
2585int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2586 struct snd_ctl_elem_info *uinfo)
2587{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002588 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002589
Mark Brownfd5dfad2009-04-15 21:37:46 +01002590 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002591 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2592 else
2593 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2594
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002595 uinfo->count = 1;
2596 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002597 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002598 return 0;
2599}
2600EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2601
2602/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002603 * snd_soc_info_volsw - single mixer info callback
2604 * @kcontrol: mixer control
2605 * @uinfo: control element information
2606 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002607 * Callback to provide information about a single mixer control, or a double
2608 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002609 *
2610 * Returns 0 for success.
2611 */
2612int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2613 struct snd_ctl_elem_info *uinfo)
2614{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002615 struct soc_mixer_control *mc =
2616 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002617 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002618
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002619 if (!mc->platform_max)
2620 mc->platform_max = mc->max;
2621 platform_max = mc->platform_max;
2622
2623 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002624 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2625 else
2626 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2627
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002628 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002629 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002630 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002631 return 0;
2632}
2633EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2634
2635/**
2636 * snd_soc_get_volsw - single mixer get callback
2637 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002638 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002639 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002640 * Callback to get the value of a single mixer control, or a double mixer
2641 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002642 *
2643 * Returns 0 for success.
2644 */
2645int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2646 struct snd_ctl_elem_value *ucontrol)
2647{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002648 struct soc_mixer_control *mc =
2649 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002650 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002651 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002652 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002653 unsigned int shift = mc->shift;
2654 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002655 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002656 unsigned int mask = (1 << fls(max)) - 1;
2657 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002658
2659 ucontrol->value.integer.value[0] =
2660 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002661 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002662 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002663 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002664
2665 if (snd_soc_volsw_is_stereo(mc)) {
2666 if (reg == reg2)
2667 ucontrol->value.integer.value[1] =
2668 (snd_soc_read(codec, reg) >> rshift) & mask;
2669 else
2670 ucontrol->value.integer.value[1] =
2671 (snd_soc_read(codec, reg2) >> shift) & mask;
2672 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002673 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002674 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002675 }
2676
2677 return 0;
2678}
2679EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2680
2681/**
2682 * snd_soc_put_volsw - single mixer put callback
2683 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002684 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002685 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002686 * Callback to set the value of a single mixer control, or a double mixer
2687 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002688 *
2689 * Returns 0 for success.
2690 */
2691int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2692 struct snd_ctl_elem_value *ucontrol)
2693{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002694 struct soc_mixer_control *mc =
2695 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002696 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002697 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002698 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002699 unsigned int shift = mc->shift;
2700 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002701 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002702 unsigned int mask = (1 << fls(max)) - 1;
2703 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002704 int err;
2705 bool type_2r = 0;
2706 unsigned int val2 = 0;
2707 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002708
2709 val = (ucontrol->value.integer.value[0] & mask);
2710 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002711 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002712 val_mask = mask << shift;
2713 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002714 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002715 val2 = (ucontrol->value.integer.value[1] & mask);
2716 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002717 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002718 if (reg == reg2) {
2719 val_mask |= mask << rshift;
2720 val |= val2 << rshift;
2721 } else {
2722 val2 = val2 << shift;
2723 type_2r = 1;
2724 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002725 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002726 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002727 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002728 return err;
2729
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002730 if (type_2r)
2731 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2732
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002733 return err;
2734}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002735EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002736
Mark Browne13ac2e2008-05-28 17:58:05 +01002737/**
Brian Austin1d99f242012-03-30 10:43:55 -05002738 * snd_soc_get_volsw_sx - single mixer get callback
2739 * @kcontrol: mixer control
2740 * @ucontrol: control element information
2741 *
2742 * Callback to get the value of a single mixer control, or a double mixer
2743 * control that spans 2 registers.
2744 *
2745 * Returns 0 for success.
2746 */
2747int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2748 struct snd_ctl_elem_value *ucontrol)
2749{
2750 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2751 struct soc_mixer_control *mc =
2752 (struct soc_mixer_control *)kcontrol->private_value;
2753
2754 unsigned int reg = mc->reg;
2755 unsigned int reg2 = mc->rreg;
2756 unsigned int shift = mc->shift;
2757 unsigned int rshift = mc->rshift;
2758 int max = mc->max;
2759 int min = mc->min;
2760 int mask = (1 << (fls(min + max) - 1)) - 1;
2761
2762 ucontrol->value.integer.value[0] =
2763 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2764
2765 if (snd_soc_volsw_is_stereo(mc))
2766 ucontrol->value.integer.value[1] =
2767 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2768
2769 return 0;
2770}
2771EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2772
2773/**
2774 * snd_soc_put_volsw_sx - double mixer set callback
2775 * @kcontrol: mixer control
2776 * @uinfo: control element information
2777 *
2778 * Callback to set the value of a double mixer control that spans 2 registers.
2779 *
2780 * Returns 0 for success.
2781 */
2782int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2783 struct snd_ctl_elem_value *ucontrol)
2784{
2785 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2786 struct soc_mixer_control *mc =
2787 (struct soc_mixer_control *)kcontrol->private_value;
2788
2789 unsigned int reg = mc->reg;
2790 unsigned int reg2 = mc->rreg;
2791 unsigned int shift = mc->shift;
2792 unsigned int rshift = mc->rshift;
2793 int max = mc->max;
2794 int min = mc->min;
2795 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002796 int err = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002797 unsigned short val, val_mask, val2 = 0;
2798
2799 val_mask = mask << shift;
2800 val = (ucontrol->value.integer.value[0] + min) & mask;
2801 val = val << shift;
2802
Mukund Navadad0558522012-11-09 11:53:40 +05302803 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2804 if (err < 0)
2805 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002806
2807 if (snd_soc_volsw_is_stereo(mc)) {
2808 val_mask = mask << rshift;
2809 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2810 val2 = val2 << rshift;
2811
2812 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2813 return err;
2814 }
2815 return 0;
2816}
2817EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2818
2819/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002820 * snd_soc_info_volsw_s8 - signed mixer info callback
2821 * @kcontrol: mixer control
2822 * @uinfo: control element information
2823 *
2824 * Callback to provide information about a signed mixer control.
2825 *
2826 * Returns 0 for success.
2827 */
2828int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2829 struct snd_ctl_elem_info *uinfo)
2830{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002831 struct soc_mixer_control *mc =
2832 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002833 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002834 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002835
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002836 if (!mc->platform_max)
2837 mc->platform_max = mc->max;
2838 platform_max = mc->platform_max;
2839
Mark Browne13ac2e2008-05-28 17:58:05 +01002840 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2841 uinfo->count = 2;
2842 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002843 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002844 return 0;
2845}
2846EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2847
2848/**
2849 * snd_soc_get_volsw_s8 - signed mixer get callback
2850 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002851 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002852 *
2853 * Callback to get the value of a signed mixer control.
2854 *
2855 * Returns 0 for success.
2856 */
2857int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2858 struct snd_ctl_elem_value *ucontrol)
2859{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002860 struct soc_mixer_control *mc =
2861 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002862 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002863 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002864 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002865 int val = snd_soc_read(codec, reg);
2866
2867 ucontrol->value.integer.value[0] =
2868 ((signed char)(val & 0xff))-min;
2869 ucontrol->value.integer.value[1] =
2870 ((signed char)((val >> 8) & 0xff))-min;
2871 return 0;
2872}
2873EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2874
2875/**
2876 * snd_soc_put_volsw_sgn - signed mixer put callback
2877 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002878 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002879 *
2880 * Callback to set the value of a signed mixer control.
2881 *
2882 * Returns 0 for success.
2883 */
2884int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2885 struct snd_ctl_elem_value *ucontrol)
2886{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002887 struct soc_mixer_control *mc =
2888 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002889 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002890 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002891 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002892 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002893
2894 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2895 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2896
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002897 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002898}
2899EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2900
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002901/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002902 * snd_soc_info_volsw_range - single mixer info callback with range.
2903 * @kcontrol: mixer control
2904 * @uinfo: control element information
2905 *
2906 * Callback to provide information, within a range, about a single
2907 * mixer control.
2908 *
2909 * returns 0 for success.
2910 */
2911int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2912 struct snd_ctl_elem_info *uinfo)
2913{
2914 struct soc_mixer_control *mc =
2915 (struct soc_mixer_control *)kcontrol->private_value;
2916 int platform_max;
2917 int min = mc->min;
2918
2919 if (!mc->platform_max)
2920 mc->platform_max = mc->max;
2921 platform_max = mc->platform_max;
2922
2923 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2924 uinfo->count = 1;
2925 uinfo->value.integer.min = 0;
2926 uinfo->value.integer.max = platform_max - min;
2927
2928 return 0;
2929}
2930EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2931
2932/**
2933 * snd_soc_put_volsw_range - single mixer put value callback with range.
2934 * @kcontrol: mixer control
2935 * @ucontrol: control element information
2936 *
2937 * Callback to set the value, within a range, for a single mixer control.
2938 *
2939 * Returns 0 for success.
2940 */
2941int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2942 struct snd_ctl_elem_value *ucontrol)
2943{
2944 struct soc_mixer_control *mc =
2945 (struct soc_mixer_control *)kcontrol->private_value;
2946 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2947 unsigned int reg = mc->reg;
2948 unsigned int shift = mc->shift;
2949 int min = mc->min;
2950 int max = mc->max;
2951 unsigned int mask = (1 << fls(max)) - 1;
2952 unsigned int invert = mc->invert;
2953 unsigned int val, val_mask;
2954
2955 val = ((ucontrol->value.integer.value[0] + min) & mask);
2956 if (invert)
2957 val = max - val;
2958 val_mask = mask << shift;
2959 val = val << shift;
2960
2961 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2962}
2963EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2964
2965/**
2966 * snd_soc_get_volsw_range - single mixer get callback with range
2967 * @kcontrol: mixer control
2968 * @ucontrol: control element information
2969 *
2970 * Callback to get the value, within a range, of a single mixer control.
2971 *
2972 * Returns 0 for success.
2973 */
2974int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2975 struct snd_ctl_elem_value *ucontrol)
2976{
2977 struct soc_mixer_control *mc =
2978 (struct soc_mixer_control *)kcontrol->private_value;
2979 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2980 unsigned int reg = mc->reg;
2981 unsigned int shift = mc->shift;
2982 int min = mc->min;
2983 int max = mc->max;
2984 unsigned int mask = (1 << fls(max)) - 1;
2985 unsigned int invert = mc->invert;
2986
2987 ucontrol->value.integer.value[0] =
2988 (snd_soc_read(codec, reg) >> shift) & mask;
2989 if (invert)
2990 ucontrol->value.integer.value[0] =
2991 max - ucontrol->value.integer.value[0];
2992 ucontrol->value.integer.value[0] =
2993 ucontrol->value.integer.value[0] - min;
2994
2995 return 0;
2996}
2997EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2998
2999/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003000 * snd_soc_limit_volume - Set new limit to an existing volume control.
3001 *
3002 * @codec: where to look for the control
3003 * @name: Name of the control
3004 * @max: new maximum limit
3005 *
3006 * Return 0 for success, else error.
3007 */
3008int snd_soc_limit_volume(struct snd_soc_codec *codec,
3009 const char *name, int max)
3010{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003011 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003012 struct snd_kcontrol *kctl;
3013 struct soc_mixer_control *mc;
3014 int found = 0;
3015 int ret = -EINVAL;
3016
3017 /* Sanity check for name and max */
3018 if (unlikely(!name || max <= 0))
3019 return -EINVAL;
3020
3021 list_for_each_entry(kctl, &card->controls, list) {
3022 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3023 found = 1;
3024 break;
3025 }
3026 }
3027 if (found) {
3028 mc = (struct soc_mixer_control *)kctl->private_value;
3029 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003030 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003031 ret = 0;
3032 }
3033 }
3034 return ret;
3035}
3036EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3037
Mark Brown71d08512011-10-10 18:31:26 +01003038int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3039 struct snd_ctl_elem_info *uinfo)
3040{
3041 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3042 struct soc_bytes *params = (void *)kcontrol->private_value;
3043
3044 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3045 uinfo->count = params->num_regs * codec->val_bytes;
3046
3047 return 0;
3048}
3049EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3050
3051int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3052 struct snd_ctl_elem_value *ucontrol)
3053{
3054 struct soc_bytes *params = (void *)kcontrol->private_value;
3055 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3056 int ret;
3057
3058 if (codec->using_regmap)
3059 ret = regmap_raw_read(codec->control_data, params->base,
3060 ucontrol->value.bytes.data,
3061 params->num_regs * codec->val_bytes);
3062 else
3063 ret = -EINVAL;
3064
Mark Brownf831b052012-02-17 16:20:33 -08003065 /* Hide any masked bytes to ensure consistent data reporting */
3066 if (ret == 0 && params->mask) {
3067 switch (codec->val_bytes) {
3068 case 1:
3069 ucontrol->value.bytes.data[0] &= ~params->mask;
3070 break;
3071 case 2:
3072 ((u16 *)(&ucontrol->value.bytes.data))[0]
3073 &= ~params->mask;
3074 break;
3075 case 4:
3076 ((u32 *)(&ucontrol->value.bytes.data))[0]
3077 &= ~params->mask;
3078 break;
3079 default:
3080 return -EINVAL;
3081 }
3082 }
3083
Mark Brown71d08512011-10-10 18:31:26 +01003084 return ret;
3085}
3086EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3087
3088int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3089 struct snd_ctl_elem_value *ucontrol)
3090{
3091 struct soc_bytes *params = (void *)kcontrol->private_value;
3092 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08003093 int ret, len;
3094 unsigned int val;
3095 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003096
Mark Brownf831b052012-02-17 16:20:33 -08003097 if (!codec->using_regmap)
3098 return -EINVAL;
3099
3100 data = ucontrol->value.bytes.data;
3101 len = params->num_regs * codec->val_bytes;
3102
3103 /*
3104 * If we've got a mask then we need to preserve the register
3105 * bits. We shouldn't modify the incoming data so take a
3106 * copy.
3107 */
3108 if (params->mask) {
3109 ret = regmap_read(codec->control_data, params->base, &val);
3110 if (ret != 0)
3111 return ret;
3112
3113 val &= params->mask;
3114
3115 data = kmemdup(data, len, GFP_KERNEL);
3116 if (!data)
3117 return -ENOMEM;
3118
3119 switch (codec->val_bytes) {
3120 case 1:
3121 ((u8 *)data)[0] &= ~params->mask;
3122 ((u8 *)data)[0] |= val;
3123 break;
3124 case 2:
3125 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3126 ((u16 *)data)[0] |= cpu_to_be16(val);
3127 break;
3128 case 4:
3129 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3130 ((u32 *)data)[0] |= cpu_to_be32(val);
3131 break;
3132 default:
3133 return -EINVAL;
3134 }
3135 }
3136
3137 ret = regmap_raw_write(codec->control_data, params->base,
3138 data, len);
3139
3140 if (params->mask)
3141 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003142
3143 return ret;
3144}
3145EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3146
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003147/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003148 * snd_soc_info_xr_sx - signed multi register info callback
3149 * @kcontrol: mreg control
3150 * @uinfo: control element information
3151 *
3152 * Callback to provide information of a control that can
3153 * span multiple codec registers which together
3154 * forms a single signed value in a MSB/LSB manner.
3155 *
3156 * Returns 0 for success.
3157 */
3158int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3159 struct snd_ctl_elem_info *uinfo)
3160{
3161 struct soc_mreg_control *mc =
3162 (struct soc_mreg_control *)kcontrol->private_value;
3163 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3164 uinfo->count = 1;
3165 uinfo->value.integer.min = mc->min;
3166 uinfo->value.integer.max = mc->max;
3167
3168 return 0;
3169}
3170EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3171
3172/**
3173 * snd_soc_get_xr_sx - signed multi register get callback
3174 * @kcontrol: mreg control
3175 * @ucontrol: control element information
3176 *
3177 * Callback to get the value of a control that can span
3178 * multiple codec registers which together forms a single
3179 * signed value in a MSB/LSB manner. The control supports
3180 * specifying total no of bits used to allow for bitfields
3181 * across the multiple codec registers.
3182 *
3183 * Returns 0 for success.
3184 */
3185int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3186 struct snd_ctl_elem_value *ucontrol)
3187{
3188 struct soc_mreg_control *mc =
3189 (struct soc_mreg_control *)kcontrol->private_value;
3190 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3191 unsigned int regbase = mc->regbase;
3192 unsigned int regcount = mc->regcount;
3193 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3194 unsigned int regwmask = (1<<regwshift)-1;
3195 unsigned int invert = mc->invert;
3196 unsigned long mask = (1UL<<mc->nbits)-1;
3197 long min = mc->min;
3198 long max = mc->max;
3199 long val = 0;
3200 unsigned long regval;
3201 unsigned int i;
3202
3203 for (i = 0; i < regcount; i++) {
3204 regval = snd_soc_read(codec, regbase+i) & regwmask;
3205 val |= regval << (regwshift*(regcount-i-1));
3206 }
3207 val &= mask;
3208 if (min < 0 && val > max)
3209 val |= ~mask;
3210 if (invert)
3211 val = max - val;
3212 ucontrol->value.integer.value[0] = val;
3213
3214 return 0;
3215}
3216EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3217
3218/**
3219 * snd_soc_put_xr_sx - signed multi register get callback
3220 * @kcontrol: mreg control
3221 * @ucontrol: control element information
3222 *
3223 * Callback to set the value of a control that can span
3224 * multiple codec registers which together forms a single
3225 * signed value in a MSB/LSB manner. The control supports
3226 * specifying total no of bits used to allow for bitfields
3227 * across the multiple codec registers.
3228 *
3229 * Returns 0 for success.
3230 */
3231int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3232 struct snd_ctl_elem_value *ucontrol)
3233{
3234 struct soc_mreg_control *mc =
3235 (struct soc_mreg_control *)kcontrol->private_value;
3236 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3237 unsigned int regbase = mc->regbase;
3238 unsigned int regcount = mc->regcount;
3239 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3240 unsigned int regwmask = (1<<regwshift)-1;
3241 unsigned int invert = mc->invert;
3242 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003243 long max = mc->max;
3244 long val = ucontrol->value.integer.value[0];
3245 unsigned int i, regval, regmask;
3246 int err;
3247
3248 if (invert)
3249 val = max - val;
3250 val &= mask;
3251 for (i = 0; i < regcount; i++) {
3252 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3253 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3254 err = snd_soc_update_bits_locked(codec, regbase+i,
3255 regmask, regval);
3256 if (err < 0)
3257 return err;
3258 }
3259
3260 return 0;
3261}
3262EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3263
3264/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003265 * snd_soc_get_strobe - strobe get callback
3266 * @kcontrol: mixer control
3267 * @ucontrol: control element information
3268 *
3269 * Callback get the value of a strobe mixer control.
3270 *
3271 * Returns 0 for success.
3272 */
3273int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3274 struct snd_ctl_elem_value *ucontrol)
3275{
3276 struct soc_mixer_control *mc =
3277 (struct soc_mixer_control *)kcontrol->private_value;
3278 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3279 unsigned int reg = mc->reg;
3280 unsigned int shift = mc->shift;
3281 unsigned int mask = 1 << shift;
3282 unsigned int invert = mc->invert != 0;
3283 unsigned int val = snd_soc_read(codec, reg) & mask;
3284
3285 if (shift != 0 && val != 0)
3286 val = val >> shift;
3287 ucontrol->value.enumerated.item[0] = val ^ invert;
3288
3289 return 0;
3290}
3291EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3292
3293/**
3294 * snd_soc_put_strobe - strobe put callback
3295 * @kcontrol: mixer control
3296 * @ucontrol: control element information
3297 *
3298 * Callback strobe a register bit to high then low (or the inverse)
3299 * in one pass of a single mixer enum control.
3300 *
3301 * Returns 1 for success.
3302 */
3303int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3304 struct snd_ctl_elem_value *ucontrol)
3305{
3306 struct soc_mixer_control *mc =
3307 (struct soc_mixer_control *)kcontrol->private_value;
3308 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3309 unsigned int reg = mc->reg;
3310 unsigned int shift = mc->shift;
3311 unsigned int mask = 1 << shift;
3312 unsigned int invert = mc->invert != 0;
3313 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3314 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3315 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3316 int err;
3317
3318 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3319 if (err < 0)
3320 return err;
3321
3322 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3323 return err;
3324}
3325EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3326
3327/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003328 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3329 * @dai: DAI
3330 * @clk_id: DAI specific clock ID
3331 * @freq: new clock frequency in Hz
3332 * @dir: new clock direction - input/output.
3333 *
3334 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3335 */
3336int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3337 unsigned int freq, int dir)
3338{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003339 if (dai->driver && dai->driver->ops->set_sysclk)
3340 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003341 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003342 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003343 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003344 else
3345 return -EINVAL;
3346}
3347EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3348
3349/**
Mark Brownec4ee522011-03-07 20:58:11 +00003350 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3351 * @codec: CODEC
3352 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003353 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003354 * @freq: new clock frequency in Hz
3355 * @dir: new clock direction - input/output.
3356 *
3357 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3358 */
3359int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003360 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003361{
3362 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003363 return codec->driver->set_sysclk(codec, clk_id, source,
3364 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003365 else
3366 return -EINVAL;
3367}
3368EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3369
3370/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003371 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3372 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003373 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003374 * @div: new clock divisor.
3375 *
3376 * Configures the clock dividers. This is used to derive the best DAI bit and
3377 * frame clocks from the system or master clock. It's best to set the DAI bit
3378 * and frame clocks as low as possible to save system power.
3379 */
3380int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3381 int div_id, int div)
3382{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003383 if (dai->driver && dai->driver->ops->set_clkdiv)
3384 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003385 else
3386 return -EINVAL;
3387}
3388EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3389
3390/**
3391 * snd_soc_dai_set_pll - configure DAI PLL.
3392 * @dai: DAI
3393 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003394 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003395 * @freq_in: PLL input clock frequency in Hz
3396 * @freq_out: requested PLL output clock frequency in Hz
3397 *
3398 * Configures and enables PLL to generate output clock based on input clock.
3399 */
Mark Brown85488032009-09-05 18:52:16 +01003400int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3401 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003402{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003403 if (dai->driver && dai->driver->ops->set_pll)
3404 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003405 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003406 else if (dai->codec && dai->codec->driver->set_pll)
3407 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3408 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003409 else
3410 return -EINVAL;
3411}
3412EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3413
Mark Brownec4ee522011-03-07 20:58:11 +00003414/*
3415 * snd_soc_codec_set_pll - configure codec PLL.
3416 * @codec: CODEC
3417 * @pll_id: DAI specific PLL ID
3418 * @source: DAI specific source for the PLL
3419 * @freq_in: PLL input clock frequency in Hz
3420 * @freq_out: requested PLL output clock frequency in Hz
3421 *
3422 * Configures and enables PLL to generate output clock based on input clock.
3423 */
3424int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3425 unsigned int freq_in, unsigned int freq_out)
3426{
3427 if (codec->driver->set_pll)
3428 return codec->driver->set_pll(codec, pll_id, source,
3429 freq_in, freq_out);
3430 else
3431 return -EINVAL;
3432}
3433EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3434
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003435/**
3436 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3437 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003438 * @fmt: SND_SOC_DAIFMT_ format value.
3439 *
3440 * Configures the DAI hardware format and clocking.
3441 */
3442int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3443{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003444 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003445 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003446 if (dai->driver->ops->set_fmt == NULL)
3447 return -ENOTSUPP;
3448 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003449}
3450EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3451
3452/**
3453 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3454 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003455 * @tx_mask: bitmask representing active TX slots.
3456 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003457 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003458 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003459 *
3460 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3461 * specific.
3462 */
3463int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003464 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003465{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003466 if (dai->driver && dai->driver->ops->set_tdm_slot)
3467 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003468 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003469 else
3470 return -EINVAL;
3471}
3472EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3473
3474/**
Barry Song472df3c2009-09-12 01:16:29 +08003475 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3476 * @dai: DAI
3477 * @tx_num: how many TX channels
3478 * @tx_slot: pointer to an array which imply the TX slot number channel
3479 * 0~num-1 uses
3480 * @rx_num: how many RX channels
3481 * @rx_slot: pointer to an array which imply the RX slot number channel
3482 * 0~num-1 uses
3483 *
3484 * configure the relationship between channel number and TDM slot number.
3485 */
3486int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3487 unsigned int tx_num, unsigned int *tx_slot,
3488 unsigned int rx_num, unsigned int *rx_slot)
3489{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003490 if (dai->driver && dai->driver->ops->set_channel_map)
3491 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003492 rx_num, rx_slot);
3493 else
3494 return -EINVAL;
3495}
3496EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3497
3498/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003499 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3500 * @dai: DAI
3501 * @tristate: tristate enable
3502 *
3503 * Tristates the DAI so that others can use it.
3504 */
3505int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3506{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003507 if (dai->driver && dai->driver->ops->set_tristate)
3508 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003509 else
3510 return -EINVAL;
3511}
3512EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3513
3514/**
3515 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3516 * @dai: DAI
3517 * @mute: mute enable
3518 *
3519 * Mutes the DAI DAC.
3520 */
3521int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3522{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003523 if (dai->driver && dai->driver->ops->digital_mute)
3524 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003525 else
Mark Brown04570c62012-04-13 19:16:03 +01003526 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003527}
3528EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3529
Mark Brownc5af3a22008-11-28 13:29:45 +00003530/**
3531 * snd_soc_register_card - Register a card with the ASoC core
3532 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003533 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003534 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003535 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303536int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003537{
Mark Brownb19e6e72012-03-14 21:18:39 +00003538 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003539
Mark Brownc5af3a22008-11-28 13:29:45 +00003540 if (!card->name || !card->dev)
3541 return -EINVAL;
3542
Stephen Warren5a504962011-12-21 10:40:59 -07003543 for (i = 0; i < card->num_links; i++) {
3544 struct snd_soc_dai_link *link = &card->dai_link[i];
3545
3546 /*
3547 * Codec must be specified by 1 of name or OF node,
3548 * not both or neither.
3549 */
3550 if (!!link->codec_name == !!link->codec_of_node) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003551 dev_err(card->dev, "ASoC: Neither/both codec"
3552 " name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003553 return -EINVAL;
3554 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003555 /* Codec DAI name must be specified */
3556 if (!link->codec_dai_name) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003557 dev_err(card->dev, "ASoC: codec_dai_name not"
3558 " set for %s\n", link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003559 return -EINVAL;
3560 }
Stephen Warren5a504962011-12-21 10:40:59 -07003561
3562 /*
3563 * Platform may be specified by either name or OF node, but
3564 * can be left unspecified, and a dummy platform will be used.
3565 */
3566 if (link->platform_name && link->platform_of_node) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003567 dev_err(card->dev, "ASoC: Both platform name/of_node"
3568 " are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003569 return -EINVAL;
3570 }
3571
3572 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003573 * CPU device may be specified by either name or OF node, but
3574 * can be left unspecified, and will be matched based on DAI
3575 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003576 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003577 if (link->cpu_name && link->cpu_of_node) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003578 dev_err(card->dev, "ASoC: Neither/both "
3579 "cpu name/of_node are set for %s\n",link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003580 return -EINVAL;
3581 }
3582 /*
3583 * At least one of CPU DAI name or CPU device name/node must be
3584 * specified
3585 */
3586 if (!link->cpu_dai_name &&
3587 !(link->cpu_name || link->cpu_of_node)) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003588 dev_err(card->dev, "ASoC: Neither cpu_dai_name nor "
3589 "cpu_name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003590 return -EINVAL;
3591 }
3592 }
3593
Mark Browned77cc12011-05-03 18:25:34 +01003594 dev_set_drvdata(card->dev, card);
3595
Stephen Warren111c6412011-01-28 14:26:35 -07003596 snd_soc_initialize_card_lists(card);
3597
Vinod Koul150dd2f2011-01-13 22:48:32 +05303598 soc_init_card_debugfs(card);
3599
Mark Brown181a6892012-03-14 20:18:49 +00003600 card->rtd = devm_kzalloc(card->dev,
3601 sizeof(struct snd_soc_pcm_runtime) *
3602 (card->num_links + card->num_aux_devs),
3603 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003604 if (card->rtd == NULL)
3605 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003606 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003607 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003608
3609 for (i = 0; i < card->num_links; i++)
3610 card->rtd[i].dai_link = &card->dai_link[i];
3611
Mark Brownc5af3a22008-11-28 13:29:45 +00003612 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003613 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003614 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003615 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003616 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003617
Mark Brownb19e6e72012-03-14 21:18:39 +00003618 ret = snd_soc_instantiate_card(card);
3619 if (ret != 0)
3620 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003621
Mark Brownb19e6e72012-03-14 21:18:39 +00003622 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003623}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303624EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003625
3626/**
3627 * snd_soc_unregister_card - Unregister a card with the ASoC core
3628 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003629 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003630 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003631 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303632int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003633{
Vinod Koulb0e26482011-01-13 22:48:02 +05303634 if (card->instantiated)
3635 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003636 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003637
3638 return 0;
3639}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303640EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003641
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003642/*
3643 * Simplify DAI link configuration by removing ".-1" from device names
3644 * and sanitizing names.
3645 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003646static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003647{
3648 char *found, name[NAME_SIZE];
3649 int id1, id2;
3650
3651 if (dev_name(dev) == NULL)
3652 return NULL;
3653
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003654 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003655
3656 /* are we a "%s.%d" name (platform and SPI components) */
3657 found = strstr(name, dev->driver->name);
3658 if (found) {
3659 /* get ID */
3660 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3661
3662 /* discard ID from name if ID == -1 */
3663 if (*id == -1)
3664 found[strlen(dev->driver->name)] = '\0';
3665 }
3666
3667 } else {
3668 /* I2C component devices are named "bus-addr" */
3669 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3670 char tmp[NAME_SIZE];
3671
3672 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003673 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003674
3675 /* sanitize component name for DAI link creation */
3676 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003677 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003678 } else
3679 *id = 0;
3680 }
3681
3682 return kstrdup(name, GFP_KERNEL);
3683}
3684
3685/*
3686 * Simplify DAI link naming for single devices with multiple DAIs by removing
3687 * any ".-1" and using the DAI name (instead of device name).
3688 */
3689static inline char *fmt_multiple_name(struct device *dev,
3690 struct snd_soc_dai_driver *dai_drv)
3691{
3692 if (dai_drv->name == NULL) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003693 dev_err(dev, "ASoC: error - multiple DAI %s registered with"
3694 " no name\n", dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003695 return NULL;
3696 }
3697
3698 return kstrdup(dai_drv->name, GFP_KERNEL);
3699}
3700
Mark Brown91151712008-11-30 23:31:24 +00003701/**
3702 * snd_soc_register_dai - Register a DAI with the ASoC core
3703 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003704 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003705 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003706int snd_soc_register_dai(struct device *dev,
3707 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003708{
Mark Brown054880f2012-04-09 17:29:19 +01003709 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003710 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003711
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003712 dev_dbg(dev, "ASoC: dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003713
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003714 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3715 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003716 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003717
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003718 /* create DAI component name */
3719 dai->name = fmt_single_name(dev, &dai->id);
3720 if (dai->name == NULL) {
3721 kfree(dai);
3722 return -ENOMEM;
3723 }
3724
3725 dai->dev = dev;
3726 dai->driver = dai_drv;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003727 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003728 if (!dai->driver->ops)
3729 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003730
3731 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003732
3733 list_for_each_entry(codec, &codec_list, list) {
3734 if (codec->dev == dev) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003735 dev_dbg(dev, "ASoC: Mapped DAI %s to CODEC %s\n",
Mark Brown054880f2012-04-09 17:29:19 +01003736 dai->name, codec->name);
3737 dai->codec = codec;
3738 break;
3739 }
3740 }
3741
Peter Ujfalusi5f800082012-08-07 10:24:13 +03003742 if (!dai->codec)
3743 dai->dapm.idle_bias_off = 1;
3744
Mark Brown91151712008-11-30 23:31:24 +00003745 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003746
Mark Brown91151712008-11-30 23:31:24 +00003747 mutex_unlock(&client_mutex);
3748
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003749 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003750
3751 return 0;
3752}
3753EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3754
3755/**
3756 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3757 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003758 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003759 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003760void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003761{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003762 struct snd_soc_dai *dai;
3763
3764 list_for_each_entry(dai, &dai_list, list) {
3765 if (dev == dai->dev)
3766 goto found;
3767 }
3768 return;
3769
3770found:
Mark Brown91151712008-11-30 23:31:24 +00003771 mutex_lock(&client_mutex);
3772 list_del(&dai->list);
3773 mutex_unlock(&client_mutex);
3774
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003775 dev_dbg(dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003776 kfree(dai->name);
3777 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003778}
3779EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3780
3781/**
3782 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3783 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003784 * @dai: Array of DAIs to register
3785 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003786 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003787int snd_soc_register_dais(struct device *dev,
3788 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003789{
Mark Brown054880f2012-04-09 17:29:19 +01003790 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003791 struct snd_soc_dai *dai;
3792 int i, ret = 0;
3793
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003794 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003795
3796 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003797
3798 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003799 if (dai == NULL) {
3800 ret = -ENOMEM;
3801 goto err;
3802 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003803
3804 /* create DAI component name */
3805 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3806 if (dai->name == NULL) {
3807 kfree(dai);
3808 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003809 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003810 }
3811
3812 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003813 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003814 if (dai->driver->id)
3815 dai->id = dai->driver->id;
3816 else
3817 dai->id = i;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003818 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003819 if (!dai->driver->ops)
3820 dai->driver->ops = &null_dai_ops;
3821
3822 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003823
3824 list_for_each_entry(codec, &codec_list, list) {
3825 if (codec->dev == dev) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003826 dev_dbg(dev, "ASoC: Mapped DAI %s to "
3827 "CODEC %s\n", dai->name, codec->name);
Mark Brown054880f2012-04-09 17:29:19 +01003828 dai->codec = codec;
3829 break;
3830 }
3831 }
3832
Peter Ujfalusi5f800082012-08-07 10:24:13 +03003833 if (!dai->codec)
3834 dai->dapm.idle_bias_off = 1;
3835
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003836 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003837
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003838 mutex_unlock(&client_mutex);
3839
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003840 dev_dbg(dai->dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003841 }
3842
3843 return 0;
3844
3845err:
3846 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003847 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003848
3849 return ret;
3850}
3851EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3852
3853/**
3854 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3855 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003856 * @dai: Array of DAIs to unregister
3857 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003858 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003859void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003860{
3861 int i;
3862
3863 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003864 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003865}
3866EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3867
Mark Brown12a48a8c2008-12-03 19:40:30 +00003868/**
3869 * snd_soc_register_platform - Register a platform with the ASoC core
3870 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003871 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003872 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003873int snd_soc_register_platform(struct device *dev,
3874 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003875{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003876 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003877
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003878 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003879
3880 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3881 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003882 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003883
3884 /* create platform component name */
3885 platform->name = fmt_single_name(dev, &platform->id);
3886 if (platform->name == NULL) {
3887 kfree(platform);
3888 return -ENOMEM;
3889 }
3890
3891 platform->dev = dev;
3892 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003893 platform->dapm.dev = dev;
3894 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003895 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00003896 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003897
3898 mutex_lock(&client_mutex);
3899 list_add(&platform->list, &platform_list);
3900 mutex_unlock(&client_mutex);
3901
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003902 dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003903
3904 return 0;
3905}
3906EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3907
3908/**
3909 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3910 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003911 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003912 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003913void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003914{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003915 struct snd_soc_platform *platform;
3916
3917 list_for_each_entry(platform, &platform_list, list) {
3918 if (dev == platform->dev)
3919 goto found;
3920 }
3921 return;
3922
3923found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003924 mutex_lock(&client_mutex);
3925 list_del(&platform->list);
3926 mutex_unlock(&client_mutex);
3927
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003928 dev_dbg(dev, "ASoC: Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003929 kfree(platform->name);
3930 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003931}
3932EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3933
Mark Brown151ab222009-05-09 16:22:58 +01003934static u64 codec_format_map[] = {
3935 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3936 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3937 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3938 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3939 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3940 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3941 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3942 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3943 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3944 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3945 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3946 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3947 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3948 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3949 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3950 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3951};
3952
3953/* Fix up the DAI formats for endianness: codecs don't actually see
3954 * the endianness of the data but we're using the CPU format
3955 * definitions which do need to include endianness so we ensure that
3956 * codec DAIs always have both big and little endian variants set.
3957 */
3958static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3959{
3960 int i;
3961
3962 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3963 if (stream->formats & codec_format_map[i])
3964 stream->formats |= codec_format_map[i];
3965}
3966
Mark Brown0d0cf002008-12-10 14:32:45 +00003967/**
3968 * snd_soc_register_codec - Register a codec with the ASoC core
3969 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003970 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003971 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003972int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003973 const struct snd_soc_codec_driver *codec_drv,
3974 struct snd_soc_dai_driver *dai_drv,
3975 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003976{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003977 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003978 struct snd_soc_codec *codec;
3979 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003980
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003981 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003982
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003983 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3984 if (codec == NULL)
3985 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003986
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003987 /* create CODEC component name */
3988 codec->name = fmt_single_name(dev, &codec->id);
3989 if (codec->name == NULL) {
3990 kfree(codec);
3991 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003992 }
3993
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003994 if (codec_drv->compress_type)
3995 codec->compress_type = codec_drv->compress_type;
3996 else
3997 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3998
Mark Brownc3acec22010-12-02 16:15:29 +00003999 codec->write = codec_drv->write;
4000 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004001 codec->volatile_register = codec_drv->volatile_register;
4002 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004003 codec->writable_register = codec_drv->writable_register;
Mark Brown5124e692012-02-08 13:20:50 +00004004 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004005 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4006 codec->dapm.dev = dev;
4007 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00004008 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01004009 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004010 codec->dev = dev;
4011 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004012 codec->num_dai = num_dai;
4013 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004014
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004015 /* allocate CODEC register cache */
4016 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004017 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00004018 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004019 /* it is necessary to make a copy of the default register cache
4020 * because in the case of using a compression type that requires
Bill Pembertonf6e65742012-11-19 13:25:33 -05004021 * the default register cache to be marked as the
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004022 * kernel might have freed the array by the time we initialize
4023 * the cache.
4024 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00004025 if (codec_drv->reg_cache_default) {
4026 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
4027 reg_size, GFP_KERNEL);
4028 if (!codec->reg_def_copy) {
4029 ret = -ENOMEM;
4030 goto fail;
4031 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004032 }
4033 }
4034
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004035 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
4036 if (!codec->volatile_register)
4037 codec->volatile_register = snd_soc_default_volatile_register;
4038 if (!codec->readable_register)
4039 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004040 if (!codec->writable_register)
4041 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004042 }
4043
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004044 for (i = 0; i < num_dai; i++) {
4045 fixup_codec_formats(&dai_drv[i].playback);
4046 fixup_codec_formats(&dai_drv[i].capture);
4047 }
4048
Mark Brown054880f2012-04-09 17:29:19 +01004049 mutex_lock(&client_mutex);
4050 list_add(&codec->list, &codec_list);
4051 mutex_unlock(&client_mutex);
4052
Mark Brown26b01cc2010-08-18 20:20:55 +01004053 /* register any DAIs */
4054 if (num_dai) {
4055 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4056 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004057 dev_err(codec->dev, "ASoC: Failed to regster"
4058 " DAIs: %d\n", ret);
Mark Brown26b01cc2010-08-18 20:20:55 +01004059 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004060
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004061 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004062 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004063
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00004064fail:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004065 kfree(codec->name);
4066 kfree(codec);
4067 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004068}
4069EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4070
4071/**
4072 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4073 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004074 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004075 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004076void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004077{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004078 struct snd_soc_codec *codec;
4079 int i;
4080
4081 list_for_each_entry(codec, &codec_list, list) {
4082 if (dev == codec->dev)
4083 goto found;
4084 }
4085 return;
4086
4087found:
Mark Brown26b01cc2010-08-18 20:20:55 +01004088 if (codec->num_dai)
4089 for (i = 0; i < codec->num_dai; i++)
4090 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004091
Mark Brown0d0cf002008-12-10 14:32:45 +00004092 mutex_lock(&client_mutex);
4093 list_del(&codec->list);
4094 mutex_unlock(&client_mutex);
4095
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004096 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004097
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004098 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004099 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01004100 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004101 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004102}
4103EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4104
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004105/* Retrieve a card's name from device tree */
4106int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4107 const char *propname)
4108{
4109 struct device_node *np = card->dev->of_node;
4110 int ret;
4111
4112 ret = of_property_read_string_index(np, propname, 0, &card->name);
4113 /*
4114 * EINVAL means the property does not exist. This is fine providing
4115 * card->name was previously set, which is checked later in
4116 * snd_soc_register_card.
4117 */
4118 if (ret < 0 && ret != -EINVAL) {
4119 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004120 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004121 propname, ret);
4122 return ret;
4123 }
4124
4125 return 0;
4126}
4127EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4128
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004129int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4130 const char *propname)
4131{
4132 struct device_node *np = card->dev->of_node;
4133 int num_routes;
4134 struct snd_soc_dapm_route *routes;
4135 int i, ret;
4136
4137 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004138 if (num_routes < 0 || num_routes & 1) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004139 dev_err(card->dev, "ASoC: Property '%s' does not exist or its"
4140 " length is not even\n", propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004141 return -EINVAL;
4142 }
4143 num_routes /= 2;
4144 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004145 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004146 propname);
4147 return -EINVAL;
4148 }
4149
4150 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4151 GFP_KERNEL);
4152 if (!routes) {
4153 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004154 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004155 return -EINVAL;
4156 }
4157
4158 for (i = 0; i < num_routes; i++) {
4159 ret = of_property_read_string_index(np, propname,
4160 2 * i, &routes[i].sink);
4161 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004162 dev_err(card->dev,
4163 "ASoC: Property '%s' index %d could not be read: %d\n",
4164 propname, 2 * i, ret);
Matthias Kaehlckeb761c0c2012-07-11 17:36:34 +02004165 kfree(routes);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004166 return -EINVAL;
4167 }
4168 ret = of_property_read_string_index(np, propname,
4169 (2 * i) + 1, &routes[i].source);
4170 if (ret) {
4171 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004172 "ASoC: Property '%s' index %d could not be read: %d\n",
4173 propname, (2 * i) + 1, ret);
Matthias Kaehlckeb761c0c2012-07-11 17:36:34 +02004174 kfree(routes);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004175 return -EINVAL;
4176 }
4177 }
4178
4179 card->num_dapm_routes = num_routes;
4180 card->dapm_routes = routes;
4181
4182 return 0;
4183}
4184EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4185
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004186static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004187{
Mark Brown384c89e2008-12-03 17:34:03 +00004188#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004189 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4190 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004191 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004192 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004193 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004194
Mark Brown8a9dab12011-01-10 22:25:21 +00004195 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004196 &codec_list_fops))
4197 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4198
Mark Brown8a9dab12011-01-10 22:25:21 +00004199 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004200 &dai_list_fops))
4201 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004202
Mark Brown8a9dab12011-01-10 22:25:21 +00004203 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004204 &platform_list_fops))
4205 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004206#endif
4207
Mark Brownfb257892011-04-28 10:57:54 +01004208 snd_soc_util_init();
4209
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004210 return platform_driver_register(&soc_driver);
4211}
Mark Brown4abe8e12010-10-12 17:41:03 +01004212module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004213
Mark Brown7d8c16a2008-11-30 22:11:24 +00004214static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004215{
Mark Brownfb257892011-04-28 10:57:54 +01004216 snd_soc_util_exit();
4217
Mark Brown384c89e2008-12-03 17:34:03 +00004218#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004219 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004220#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004221 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004222}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004223module_exit(snd_soc_exit);
4224
4225/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004226MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004227MODULE_DESCRIPTION("ALSA SoC Core");
4228MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004229MODULE_ALIAS("platform:soc-audio");