blob: e0d4630bfb4f85b9e44a8b001d3f00e2ec67d8d8 [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 Brown99497882010-05-07 20:24:05 +0100685 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000686 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100687
Mark Brown87506542008-11-18 20:50:34 +0000688 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000689 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000691 /* resume AC97 DAIs */
692 for (i = 0; i < card->num_rtd; i++) {
693 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100694
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000695 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100696 continue;
697
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000698 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
699 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200700 }
701
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200702 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000703 /* If the CODEC was idle over suspend then it will have been
704 * left with bias OFF or STANDBY and suspended so we must now
705 * resume. Otherwise the suspend was suppressed.
706 */
707 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200708 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000709 case SND_SOC_BIAS_STANDBY:
710 case SND_SOC_BIAS_OFF:
711 codec->driver->resume(codec);
712 codec->suspended = 0;
713 break;
714 default:
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 Nikula70d29332011-01-27 16:24:22 +02001084 if (!try_module_get(codec->dev->driver->owner))
1085 return -ENODEV;
1086
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001087 soc_init_codec_debugfs(codec);
1088
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001089 if (driver->dapm_widgets)
1090 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1091 driver->num_dapm_widgets);
1092
Mark Brown888df392012-02-16 19:37:51 -08001093 /* Create DAPM widgets for each DAI stream */
1094 list_for_each_entry(dai, &dai_list, list) {
1095 if (dai->dev != codec->dev)
1096 continue;
1097
1098 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1099 }
1100
Mark Brown33c5f962011-08-22 18:40:30 +01001101 codec->dapm.idle_bias_off = driver->idle_bias_off;
1102
Mark Brown89b95ac02011-03-07 16:38:44 +00001103 if (driver->probe) {
1104 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001105 if (ret < 0) {
1106 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001107 "ASoC: failed to probe CODEC %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001108 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001109 }
1110 }
1111
Mark Brown38cbf952012-06-22 13:04:02 +01001112 /* If the driver didn't set I/O up try regmap */
Mark Brown98d30882012-08-01 20:05:47 +01001113 if (!codec->write && dev_get_regmap(codec->dev, NULL))
Mark Brown38cbf952012-06-22 13:04:02 +01001114 snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
1115
Mark Brownb7af1da2011-04-07 19:18:44 +09001116 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001117 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001118 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001119 if (driver->dapm_routes)
1120 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1121 driver->num_dapm_routes);
1122
Jarkko Nikula589c3562010-12-06 16:27:07 +02001123 /* mark codec as probed and add to card codec list */
1124 codec->probed = 1;
1125 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001126 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001127
Jarkko Nikula70d29332011-01-27 16:24:22 +02001128 return 0;
1129
1130err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001131 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001132 module_put(codec->dev->driver->owner);
1133
Jarkko Nikula589c3562010-12-06 16:27:07 +02001134 return ret;
1135}
1136
Liam Girdwood956245e2011-07-01 16:54:08 +01001137static int soc_probe_platform(struct snd_soc_card *card,
1138 struct snd_soc_platform *platform)
1139{
1140 int ret = 0;
1141 const struct snd_soc_platform_driver *driver = platform->driver;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001142 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001143
1144 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001145 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001146
1147 if (!try_module_get(platform->dev->driver->owner))
1148 return -ENODEV;
1149
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001150 soc_init_platform_debugfs(platform);
1151
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001152 if (driver->dapm_widgets)
1153 snd_soc_dapm_new_controls(&platform->dapm,
1154 driver->dapm_widgets, driver->num_dapm_widgets);
1155
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001156 /* Create DAPM widgets for each DAI stream */
1157 list_for_each_entry(dai, &dai_list, list) {
1158 if (dai->dev != platform->dev)
1159 continue;
1160
1161 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1162 }
1163
Stephen Warren3fec6b62012-04-05 12:28:01 -06001164 platform->dapm.idle_bias_off = 1;
1165
Liam Girdwood956245e2011-07-01 16:54:08 +01001166 if (driver->probe) {
1167 ret = driver->probe(platform);
1168 if (ret < 0) {
1169 dev_err(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001170 "ASoC: failed to probe platform %d\n", ret);
Liam Girdwood956245e2011-07-01 16:54:08 +01001171 goto err_probe;
1172 }
1173 }
1174
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001175 if (driver->controls)
1176 snd_soc_add_platform_controls(platform, driver->controls,
1177 driver->num_controls);
1178 if (driver->dapm_routes)
1179 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1180 driver->num_dapm_routes);
1181
Liam Girdwood956245e2011-07-01 16:54:08 +01001182 /* mark platform as probed and add to card platform list */
1183 platform->probed = 1;
1184 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001185 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001186
1187 return 0;
1188
1189err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001190 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001191 module_put(platform->dev->driver->owner);
1192
1193 return ret;
1194}
1195
Mark Brown36ae1a92012-01-06 17:12:45 -08001196static void rtd_release(struct device *dev)
1197{
1198 kfree(dev);
1199}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001200
Jarkko Nikula589c3562010-12-06 16:27:07 +02001201static int soc_post_component_init(struct snd_soc_card *card,
1202 struct snd_soc_codec *codec,
1203 int num, int dailess)
1204{
1205 struct snd_soc_dai_link *dai_link = NULL;
1206 struct snd_soc_aux_dev *aux_dev = NULL;
1207 struct snd_soc_pcm_runtime *rtd;
1208 const char *temp, *name;
1209 int ret = 0;
1210
1211 if (!dailess) {
1212 dai_link = &card->dai_link[num];
1213 rtd = &card->rtd[num];
1214 name = dai_link->name;
1215 } else {
1216 aux_dev = &card->aux_dev[num];
1217 rtd = &card->rtd_aux[num];
1218 name = aux_dev->name;
1219 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001220 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001221
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001222 /* Make sure all DAPM widgets are instantiated */
1223 snd_soc_dapm_new_widgets(&codec->dapm);
1224
Jarkko Nikula589c3562010-12-06 16:27:07 +02001225 /* machine controls, routes and widgets are not prefixed */
1226 temp = codec->name_prefix;
1227 codec->name_prefix = NULL;
1228
1229 /* do machine specific initialization */
1230 if (!dailess && dai_link->init)
1231 ret = dai_link->init(rtd);
1232 else if (dailess && aux_dev->init)
1233 ret = aux_dev->init(&codec->dapm);
1234 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001235 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001236 return ret;
1237 }
1238 codec->name_prefix = temp;
1239
Jarkko Nikula589c3562010-12-06 16:27:07 +02001240 /* register the rtd device */
1241 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001242
1243 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1244 if (!rtd->dev)
1245 return -ENOMEM;
1246 device_initialize(rtd->dev);
1247 rtd->dev->parent = card->dev;
1248 rtd->dev->release = rtd_release;
1249 rtd->dev->init_name = name;
1250 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001251 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001252 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1253 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1254 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1255 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001256 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001257 if (ret < 0) {
1258 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001259 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001260 return ret;
1261 }
1262 rtd->dev_registered = 1;
1263
1264 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001265 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001266 if (ret < 0)
1267 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001268 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001269
1270 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001271 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001272 if (ret < 0)
1273 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001274 "ASoC: failed to add codec sysfs files: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001275
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001276#ifdef CONFIG_DEBUG_FS
1277 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001278 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001279 goto out;
1280
1281 ret = soc_dpcm_debugfs_add(rtd);
1282 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001283 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001284
1285out:
1286#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001287 return 0;
1288}
1289
Stephen Warren62ae68f2012-06-08 12:34:23 -06001290static int soc_probe_link_components(struct snd_soc_card *card, int num,
1291 int order)
1292{
1293 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1294 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1295 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1296 struct snd_soc_platform *platform = rtd->platform;
1297 int ret;
1298
1299 /* probe the CPU-side component, if it is a CODEC */
1300 if (cpu_dai->codec &&
1301 !cpu_dai->codec->probed &&
1302 cpu_dai->codec->driver->probe_order == order) {
1303 ret = soc_probe_codec(card, cpu_dai->codec);
1304 if (ret < 0)
1305 return ret;
1306 }
1307
1308 /* probe the CODEC-side component */
1309 if (!codec_dai->codec->probed &&
1310 codec_dai->codec->driver->probe_order == order) {
1311 ret = soc_probe_codec(card, codec_dai->codec);
1312 if (ret < 0)
1313 return ret;
1314 }
1315
1316 /* probe the platform */
1317 if (!platform->probed &&
1318 platform->driver->probe_order == order) {
1319 ret = soc_probe_platform(card, platform);
1320 if (ret < 0)
1321 return ret;
1322 }
1323
1324 return 0;
1325}
1326
1327static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001328{
1329 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1330 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1331 struct snd_soc_codec *codec = rtd->codec;
1332 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001333 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1334 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1335 struct snd_soc_dapm_widget *play_w, *capture_w;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001336 int ret;
1337
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001338 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001339 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001340
1341 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001342 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001343 codec_dai->card = card;
1344 cpu_dai->card = card;
1345
1346 /* set default power off timeout */
1347 rtd->pmdown_time = pmdown_time;
1348
1349 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001350 if (!cpu_dai->probed &&
1351 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001352 if (!cpu_dai->codec) {
1353 cpu_dai->dapm.card = card;
1354 if (!try_module_get(cpu_dai->dev->driver->owner))
1355 return -ENODEV;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001356
Stephen Warren18d75642012-06-08 12:34:21 -06001357 list_add(&cpu_dai->dapm.list, &card->dapm_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001358 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1359 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001360
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001361 if (cpu_dai->driver->probe) {
1362 ret = cpu_dai->driver->probe(cpu_dai);
1363 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001364 dev_err(cpu_dai->dev,
1365 "ASoC: failed to probe CPU DAI %s: %d\n",
1366 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001367 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001368 return ret;
1369 }
1370 }
1371 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001372 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001373 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1374 }
1375
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001376 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001377 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001378 if (codec_dai->driver->probe) {
1379 ret = codec_dai->driver->probe(codec_dai);
1380 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001381 dev_err(codec_dai->dev,
1382 "ASoC: failed to probe CODEC DAI %s: %d\n",
1383 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001384 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001385 }
1386 }
1387
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001388 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001389 codec_dai->probed = 1;
1390 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001391 }
1392
Liam Girdwood0168bf02011-06-07 16:08:05 +01001393 /* complete DAI probe during last probe */
1394 if (order != SND_SOC_COMP_ORDER_LAST)
1395 return 0;
1396
Jarkko Nikula589c3562010-12-06 16:27:07 +02001397 ret = soc_post_component_init(card, codec, num, 0);
1398 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001399 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001400
Mark Brown36ae1a92012-01-06 17:12:45 -08001401 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001402 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001403 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1404 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001405
Namarta Kohli1245b702012-08-16 17:10:41 +05301406 if (cpu_dai->driver->compress_dai) {
1407 /*create compress_device"*/
1408 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001409 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001410 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301411 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001412 return ret;
1413 }
1414 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301415
1416 if (!dai_link->params) {
1417 /* create the pcm */
1418 ret = soc_new_pcm(rtd, num);
1419 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001420 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301421 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001422 return ret;
1423 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301424 } else {
1425 /* link the DAI widgets */
1426 play_w = codec_dai->playback_widget;
1427 capture_w = cpu_dai->capture_widget;
1428 if (play_w && capture_w) {
1429 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Mark Brownc74184e2012-04-04 22:12:09 +01001430 capture_w, play_w);
Namarta Kohli1245b702012-08-16 17:10:41 +05301431 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001432 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301433 play_w->name, capture_w->name, ret);
1434 return ret;
1435 }
1436 }
1437
1438 play_w = cpu_dai->playback_widget;
1439 capture_w = codec_dai->capture_widget;
1440 if (play_w && capture_w) {
1441 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1442 capture_w, play_w);
1443 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001444 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301445 play_w->name, capture_w->name, ret);
1446 return ret;
1447 }
Mark Brownc74184e2012-04-04 22:12:09 +01001448 }
1449 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001450 }
1451
1452 /* add platform data for AC97 devices */
1453 if (rtd->codec_dai->driver->ac97_control)
1454 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1455
1456 return 0;
1457}
1458
1459#ifdef CONFIG_SND_SOC_AC97_BUS
1460static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1461{
1462 int ret;
1463
1464 /* Only instantiate AC97 if not already done by the adaptor
1465 * for the generic AC97 subsystem.
1466 */
1467 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001468 /*
1469 * It is possible that the AC97 device is already registered to
1470 * the device subsystem. This happens when the device is created
1471 * via snd_ac97_mixer(). Currently only SoC codec that does so
1472 * is the generic AC97 glue but others migh emerge.
1473 *
1474 * In those cases we don't try to register the device again.
1475 */
1476 if (!rtd->codec->ac97_created)
1477 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001478
1479 ret = soc_ac97_dev_register(rtd->codec);
1480 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001481 dev_err(rtd->codec->dev,
1482 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001483 return ret;
1484 }
1485
1486 rtd->codec->ac97_registered = 1;
1487 }
1488 return 0;
1489}
1490
1491static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1492{
1493 if (codec->ac97_registered) {
1494 soc_ac97_dev_unregister(codec);
1495 codec->ac97_registered = 0;
1496 }
1497}
1498#endif
1499
Mark Brownb19e6e72012-03-14 21:18:39 +00001500static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1501{
1502 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1503 struct snd_soc_codec *codec;
1504
1505 /* find CODEC from registered CODECs*/
1506 list_for_each_entry(codec, &codec_list, list) {
1507 if (!strcmp(codec->name, aux_dev->codec_name))
1508 return 0;
1509 }
1510
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001511 dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
Mark Brownfb099cb2012-08-09 18:44:37 +01001512
Mark Brownb19e6e72012-03-14 21:18:39 +00001513 return -EPROBE_DEFER;
1514}
1515
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001516static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1517{
1518 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001519 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001520 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001521
1522 /* find CODEC from registered CODECs*/
1523 list_for_each_entry(codec, &codec_list, list) {
1524 if (!strcmp(codec->name, aux_dev->codec_name)) {
1525 if (codec->probed) {
1526 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001527 "ASoC: codec already probed");
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001528 ret = -EBUSY;
1529 goto out;
1530 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001531 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001532 }
1533 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001534 /* codec not found */
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001535 dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001536 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001537
Jarkko Nikula676ad982010-12-03 09:18:22 +02001538found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001539 ret = soc_probe_codec(card, codec);
1540 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001541 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001542
Jarkko Nikula589c3562010-12-06 16:27:07 +02001543 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001544
1545out:
1546 return ret;
1547}
1548
1549static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1550{
1551 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1552 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001553
1554 /* unregister the rtd device */
1555 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001556 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1557 device_del(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001558 rtd->dev_registered = 0;
1559 }
1560
Jarkko Nikula589c3562010-12-06 16:27:07 +02001561 if (codec && codec->probed)
1562 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001563}
1564
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001565static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1566 enum snd_soc_compress_type compress_type)
1567{
1568 int ret;
1569
1570 if (codec->cache_init)
1571 return 0;
1572
1573 /* override the compress_type if necessary */
1574 if (compress_type && codec->compress_type != compress_type)
1575 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001576 ret = snd_soc_cache_init(codec);
1577 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001578 dev_err(codec->dev, "ASoC: Failed to set cache compression"
1579 " type: %d\n", ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001580 return ret;
1581 }
1582 codec->cache_init = 1;
1583 return 0;
1584}
1585
Mark Brownb19e6e72012-03-14 21:18:39 +00001586static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001587{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001588 struct snd_soc_codec *codec;
1589 struct snd_soc_codec_conf *codec_conf;
1590 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001591 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001592 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001593
Liam Girdwood01b9d992012-03-07 10:38:25 +00001594 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001595
Mark Brownb19e6e72012-03-14 21:18:39 +00001596 /* bind DAIs */
1597 for (i = 0; i < card->num_links; i++) {
1598 ret = soc_bind_dai_link(card, i);
1599 if (ret != 0)
1600 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001601 }
1602
Mark Brownb19e6e72012-03-14 21:18:39 +00001603 /* check aux_devs too */
1604 for (i = 0; i < card->num_aux_devs; i++) {
1605 ret = soc_check_aux_dev(card, i);
1606 if (ret != 0)
1607 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001608 }
1609
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001610 /* initialize the register cache for each available codec */
1611 list_for_each_entry(codec, &codec_list, list) {
1612 if (codec->cache_init)
1613 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001614 /* by default we don't override the compress_type */
1615 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001616 /* check to see if we need to override the compress_type */
1617 for (i = 0; i < card->num_configs; ++i) {
1618 codec_conf = &card->codec_conf[i];
1619 if (!strcmp(codec->name, codec_conf->dev_name)) {
1620 compress_type = codec_conf->compress_type;
1621 if (compress_type && compress_type
1622 != codec->compress_type)
1623 break;
1624 }
1625 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001626 ret = snd_soc_init_codec_cache(codec, compress_type);
Mark Brownb19e6e72012-03-14 21:18:39 +00001627 if (ret < 0)
1628 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001629 }
1630
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001631 /* card bind complete so register a sound card */
1632 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1633 card->owner, 0, &card->snd_card);
1634 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001635 dev_err(card->dev, "ASoC: can't create sound card for"
1636 " card %s: %d\n", card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001637 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001638 }
1639 card->snd_card->dev = card->dev;
1640
Mark Browne37a4972011-03-02 18:21:57 +00001641 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1642 card->dapm.dev = card->dev;
1643 card->dapm.card = card;
1644 list_add(&card->dapm.list, &card->dapm_list);
1645
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001646#ifdef CONFIG_DEBUG_FS
1647 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1648#endif
1649
Mark Brown88ee1c62011-02-02 10:43:26 +00001650#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001651 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001652 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001653#endif
Andy Green6ed25972008-06-13 16:24:05 +01001654
Mark Brown9a841eb2011-04-12 17:51:37 -07001655 if (card->dapm_widgets)
1656 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1657 card->num_dapm_widgets);
1658
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001659 /* initialise the sound card only once */
1660 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001661 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001662 if (ret < 0)
1663 goto card_probe_error;
1664 }
1665
Stephen Warren62ae68f2012-06-08 12:34:23 -06001666 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001667 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1668 order++) {
1669 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001670 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001671 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001672 dev_err(card->dev,
1673 "ASoC: failed to instantiate card %d\n",
1674 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001675 goto probe_dai_err;
1676 }
1677 }
1678 }
1679
1680 /* probe all DAI links on this card */
1681 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1682 order++) {
1683 for (i = 0; i < card->num_links; i++) {
1684 ret = soc_probe_link_dais(card, i, order);
1685 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001686 dev_err(card->dev,
1687 "ASoC: failed to instantiate card %d\n",
1688 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001689 goto probe_dai_err;
1690 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001691 }
1692 }
1693
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001694 for (i = 0; i < card->num_aux_devs; i++) {
1695 ret = soc_probe_aux_dev(card, i);
1696 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001697 dev_err(card->dev,
1698 "ASoC: failed to add auxiliary devices %d\n",
1699 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001700 goto probe_aux_dev_err;
1701 }
1702 }
1703
Mark Brown888df392012-02-16 19:37:51 -08001704 snd_soc_dapm_link_dai_widgets(card);
1705
Mark Brownb7af1da2011-04-07 19:18:44 +09001706 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001707 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001708
Mark Brownb8ad29d2011-03-02 18:35:51 +00001709 if (card->dapm_routes)
1710 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1711 card->num_dapm_routes);
1712
Mark Brownb90d2f92011-10-10 13:38:06 +01001713 snd_soc_dapm_new_widgets(&card->dapm);
1714
Mark Brown75d9ac42011-09-27 16:41:01 +01001715 for (i = 0; i < card->num_links; i++) {
1716 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001717 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001718
Mark Brownf04209a2012-04-09 16:29:21 +01001719 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001720 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001721 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001722 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001723 dev_warn(card->rtd[i].codec_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001724 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001725 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001726 }
1727
1728 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001729 if (dai_fmt &&
1730 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001731 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1732 dai_fmt);
1733 if (ret != 0 && ret != -ENOTSUPP)
1734 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001735 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001736 ret);
1737 } else if (dai_fmt) {
1738 /* Flip the polarity for the "CPU" end */
1739 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1740 switch (dai_link->dai_fmt &
1741 SND_SOC_DAIFMT_MASTER_MASK) {
1742 case SND_SOC_DAIFMT_CBM_CFM:
1743 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1744 break;
1745 case SND_SOC_DAIFMT_CBM_CFS:
1746 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1747 break;
1748 case SND_SOC_DAIFMT_CBS_CFM:
1749 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1750 break;
1751 case SND_SOC_DAIFMT_CBS_CFS:
1752 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1753 break;
1754 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001755
1756 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001757 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001758 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001759 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001760 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001761 ret);
1762 }
1763 }
1764
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001765 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001766 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001767 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1768 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001769 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1770 "%s", card->driver_name ? card->driver_name : card->name);
1771 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1772 switch (card->snd_card->driver[i]) {
1773 case '_':
1774 case '-':
1775 case '\0':
1776 break;
1777 default:
1778 if (!isalnum(card->snd_card->driver[i]))
1779 card->snd_card->driver[i] = '_';
1780 break;
1781 }
1782 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001783
Mark Brown28e9ad92011-03-02 18:36:34 +00001784 if (card->late_probe) {
1785 ret = card->late_probe(card);
1786 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001787 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001788 card->name, ret);
1789 goto probe_aux_dev_err;
1790 }
1791 }
1792
Mark Brown2dc00212011-10-08 13:59:44 +01001793 snd_soc_dapm_new_widgets(&card->dapm);
1794
Stephen Warren16332812011-11-23 12:42:04 -07001795 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001796 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001797 snd_soc_dapm_auto_nc_codec_pins(codec);
1798
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001799 ret = snd_card_register(card->snd_card);
1800 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001801 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1802 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001803 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001804 }
1805
1806#ifdef CONFIG_SND_SOC_AC97_BUS
1807 /* register any AC97 codecs */
1808 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001809 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1810 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001811 dev_err(card->dev, "ASoC: failed to register AC97:"
1812 " %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001813 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001814 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001815 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001816 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001817 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001818#endif
1819
Mark Brown435c5e22008-12-04 15:32:53 +00001820 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001821 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001822 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001823
1824 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001825
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001826probe_aux_dev_err:
1827 for (i = 0; i < card->num_aux_devs; i++)
1828 soc_remove_aux_dev(card, i);
1829
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001830probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001831 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001832
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001833card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001834 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001835 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001836
1837 snd_card_free(card->snd_card);
1838
Mark Brownb19e6e72012-03-14 21:18:39 +00001839base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001840 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001841
Mark Brownb19e6e72012-03-14 21:18:39 +00001842 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001843}
1844
1845/* probes a new socdev */
1846static int soc_probe(struct platform_device *pdev)
1847{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001848 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001849
Vinod Koul70a7ca32011-01-14 19:22:48 +05301850 /*
1851 * no card, so machine driver should be registering card
1852 * we should not be here in that case so ret error
1853 */
1854 if (!card)
1855 return -EINVAL;
1856
Mark Brownfe4085e2012-03-02 13:07:41 +00001857 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001858 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001859 card->name);
1860
Mark Brown435c5e22008-12-04 15:32:53 +00001861 /* Bodge while we unpick instantiation */
1862 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001863
Mark Brown28d528c2012-08-09 18:45:23 +01001864 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001865}
1866
Vinod Koulb0e26482011-01-13 22:48:02 +05301867static int soc_cleanup_card_resources(struct snd_soc_card *card)
1868{
Vinod Koulb0e26482011-01-13 22:48:02 +05301869 int i;
1870
1871 /* make sure any delayed work runs */
1872 for (i = 0; i < card->num_rtd; i++) {
1873 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001874 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301875 }
1876
1877 /* remove auxiliary devices */
1878 for (i = 0; i < card->num_aux_devs; i++)
1879 soc_remove_aux_dev(card, i);
1880
1881 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001882 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301883
1884 soc_cleanup_card_debugfs(card);
1885
1886 /* remove the card */
1887 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001888 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301889
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001890 snd_soc_dapm_free(&card->dapm);
1891
Vinod Koulb0e26482011-01-13 22:48:02 +05301892 snd_card_free(card->snd_card);
1893 return 0;
1894
1895}
1896
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001897/* removes a socdev */
1898static int soc_remove(struct platform_device *pdev)
1899{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001900 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001901
Mark Brownc5af3a22008-11-28 13:29:45 +00001902 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001903 return 0;
1904}
1905
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001906int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001907{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001908 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001909 int i;
Mark Brown51737472009-06-22 13:16:51 +01001910
1911 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001912 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001913
1914 /* Flush out pmdown_time work - we actually do want to run it
1915 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001916 for (i = 0; i < card->num_rtd; i++) {
1917 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001918 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001919 }
Mark Brown51737472009-06-22 13:16:51 +01001920
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001921 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001922
1923 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001924}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001925EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001926
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001927const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301928 .suspend = snd_soc_suspend,
1929 .resume = snd_soc_resume,
1930 .freeze = snd_soc_suspend,
1931 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001932 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301933 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001934};
Stephen Warrendeb26072011-04-05 19:35:30 -06001935EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001936
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001937/* ASoC platform driver */
1938static struct platform_driver soc_driver = {
1939 .driver = {
1940 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001941 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001942 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001943 },
1944 .probe = soc_probe,
1945 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001946};
1947
Mark Brown096e49d2009-07-05 15:12:22 +01001948/**
1949 * snd_soc_codec_volatile_register: Report if a register is volatile.
1950 *
1951 * @codec: CODEC to query.
1952 * @reg: Register to query.
1953 *
1954 * Boolean function indiciating if a CODEC register is volatile.
1955 */
Mark Brown181e0552011-01-24 14:05:25 +00001956int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1957 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001958{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001959 if (codec->volatile_register)
1960 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001961 else
1962 return 0;
1963}
1964EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1965
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001966/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001967 * snd_soc_codec_readable_register: Report if a register is readable.
1968 *
1969 * @codec: CODEC to query.
1970 * @reg: Register to query.
1971 *
1972 * Boolean function indicating if a CODEC register is readable.
1973 */
1974int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1975 unsigned int reg)
1976{
1977 if (codec->readable_register)
1978 return codec->readable_register(codec, reg);
1979 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001980 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001981}
1982EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1983
1984/**
1985 * snd_soc_codec_writable_register: Report if a register is writable.
1986 *
1987 * @codec: CODEC to query.
1988 * @reg: Register to query.
1989 *
1990 * Boolean function indicating if a CODEC register is writable.
1991 */
1992int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1993 unsigned int reg)
1994{
1995 if (codec->writable_register)
1996 return codec->writable_register(codec, reg);
1997 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001998 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001999}
2000EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2001
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002002int snd_soc_platform_read(struct snd_soc_platform *platform,
2003 unsigned int reg)
2004{
2005 unsigned int ret;
2006
2007 if (!platform->driver->read) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002008 dev_err(platform->dev, "ASoC: platform has no read back\n");
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002009 return -1;
2010 }
2011
2012 ret = platform->driver->read(platform, reg);
2013 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002014 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002015
2016 return ret;
2017}
2018EXPORT_SYMBOL_GPL(snd_soc_platform_read);
2019
2020int snd_soc_platform_write(struct snd_soc_platform *platform,
2021 unsigned int reg, unsigned int val)
2022{
2023 if (!platform->driver->write) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002024 dev_err(platform->dev, "ASoC: platform has no write back\n");
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002025 return -1;
2026 }
2027
2028 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002029 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002030 return platform->driver->write(platform, reg, val);
2031}
2032EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2033
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002034/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002035 * snd_soc_new_ac97_codec - initailise AC97 device
2036 * @codec: audio codec
2037 * @ops: AC97 bus operations
2038 * @num: AC97 codec number
2039 *
2040 * Initialises AC97 codec resources for use by ad-hoc devices only.
2041 */
2042int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2043 struct snd_ac97_bus_ops *ops, int num)
2044{
2045 mutex_lock(&codec->mutex);
2046
2047 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2048 if (codec->ac97 == NULL) {
2049 mutex_unlock(&codec->mutex);
2050 return -ENOMEM;
2051 }
2052
2053 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2054 if (codec->ac97->bus == NULL) {
2055 kfree(codec->ac97);
2056 codec->ac97 = NULL;
2057 mutex_unlock(&codec->mutex);
2058 return -ENOMEM;
2059 }
2060
2061 codec->ac97->bus->ops = ops;
2062 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002063
2064 /*
2065 * Mark the AC97 device to be created by us. This way we ensure that the
2066 * device will be registered with the device subsystem later on.
2067 */
2068 codec->ac97_created = 1;
2069
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002070 mutex_unlock(&codec->mutex);
2071 return 0;
2072}
2073EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2074
2075/**
2076 * snd_soc_free_ac97_codec - free AC97 codec device
2077 * @codec: audio codec
2078 *
2079 * Frees AC97 codec device resources.
2080 */
2081void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2082{
2083 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002084#ifdef CONFIG_SND_SOC_AC97_BUS
2085 soc_unregister_ac97_dai_link(codec);
2086#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002087 kfree(codec->ac97->bus);
2088 kfree(codec->ac97);
2089 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002090 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002091 mutex_unlock(&codec->mutex);
2092}
2093EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2094
Mark Brownc3753702010-11-01 15:41:57 -04002095unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2096{
2097 unsigned int ret;
2098
Mark Brownc3acec22010-12-02 16:15:29 +00002099 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002100 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002101 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002102
2103 return ret;
2104}
2105EXPORT_SYMBOL_GPL(snd_soc_read);
2106
2107unsigned int snd_soc_write(struct snd_soc_codec *codec,
2108 unsigned int reg, unsigned int val)
2109{
2110 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002111 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002112 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002113}
2114EXPORT_SYMBOL_GPL(snd_soc_write);
2115
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00002116unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2117 unsigned int reg, const void *data, size_t len)
2118{
2119 return codec->bulk_write_raw(codec, reg, data, len);
2120}
2121EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2122
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002123/**
2124 * snd_soc_update_bits - update codec register bits
2125 * @codec: audio codec
2126 * @reg: codec register
2127 * @mask: register mask
2128 * @value: new value
2129 *
2130 * Writes new register value.
2131 *
Timur Tabi180c3292011-01-10 15:58:13 -06002132 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002133 */
2134int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002135 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002136{
Mark Brown8a713da2011-12-03 12:33:55 +00002137 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002138 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002139 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002140
Mark Brown8a713da2011-12-03 12:33:55 +00002141 if (codec->using_regmap) {
2142 ret = regmap_update_bits_check(codec->control_data, reg,
2143 mask, value, &change);
2144 } else {
2145 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06002146 if (ret < 0)
2147 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00002148
2149 old = ret;
2150 new = (old & ~mask) | (value & mask);
2151 change = old != new;
2152 if (change)
2153 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06002154 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002155
Mark Brown8a713da2011-12-03 12:33:55 +00002156 if (ret < 0)
2157 return ret;
2158
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002159 return change;
2160}
2161EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2162
2163/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002164 * snd_soc_update_bits_locked - update codec register bits
2165 * @codec: audio codec
2166 * @reg: codec register
2167 * @mask: register mask
2168 * @value: new value
2169 *
2170 * Writes new register value, and takes the codec mutex.
2171 *
2172 * Returns 1 for change else 0.
2173 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002174int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2175 unsigned short reg, unsigned int mask,
2176 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002177{
2178 int change;
2179
2180 mutex_lock(&codec->mutex);
2181 change = snd_soc_update_bits(codec, reg, mask, value);
2182 mutex_unlock(&codec->mutex);
2183
2184 return change;
2185}
Mark Browndd1b3d52009-12-04 14:22:03 +00002186EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002187
2188/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002189 * snd_soc_test_bits - test register for change
2190 * @codec: audio codec
2191 * @reg: codec register
2192 * @mask: register mask
2193 * @value: new value
2194 *
2195 * Tests a register with a new value and checks if the new value is
2196 * different from the old value.
2197 *
2198 * Returns 1 for change else 0.
2199 */
2200int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002201 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002202{
2203 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002204 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002205
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002206 old = snd_soc_read(codec, reg);
2207 new = (old & ~mask) | value;
2208 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002209
2210 return change;
2211}
2212EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2213
2214/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002215 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2216 * @substream: the pcm substream
2217 * @hw: the hardware parameters
2218 *
2219 * Sets the substream runtime hardware parameters.
2220 */
2221int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2222 const struct snd_pcm_hardware *hw)
2223{
2224 struct snd_pcm_runtime *runtime = substream->runtime;
2225 runtime->hw.info = hw->info;
2226 runtime->hw.formats = hw->formats;
2227 runtime->hw.period_bytes_min = hw->period_bytes_min;
2228 runtime->hw.period_bytes_max = hw->period_bytes_max;
2229 runtime->hw.periods_min = hw->periods_min;
2230 runtime->hw.periods_max = hw->periods_max;
2231 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2232 runtime->hw.fifo_size = hw->fifo_size;
2233 return 0;
2234}
2235EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2236
2237/**
2238 * snd_soc_cnew - create new control
2239 * @_template: control template
2240 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002241 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002242 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002243 *
2244 * Create a new mixer control from a template control.
2245 *
2246 * Returns 0 for success, else error.
2247 */
2248struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002249 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002250 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002251{
2252 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002253 struct snd_kcontrol *kcontrol;
2254 char *name = NULL;
2255 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002256
2257 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002258 template.index = 0;
2259
Mark Brownefb7ac32011-03-08 17:23:24 +00002260 if (!long_name)
2261 long_name = template.name;
2262
2263 if (prefix) {
2264 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08002265 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00002266 if (!name)
2267 return NULL;
2268
2269 snprintf(name, name_len, "%s %s", prefix, long_name);
2270
2271 template.name = name;
2272 } else {
2273 template.name = long_name;
2274 }
2275
2276 kcontrol = snd_ctl_new1(&template, data);
2277
2278 kfree(name);
2279
2280 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002281}
2282EXPORT_SYMBOL_GPL(snd_soc_cnew);
2283
Liam Girdwood022658b2012-02-03 17:43:09 +00002284static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2285 const struct snd_kcontrol_new *controls, int num_controls,
2286 const char *prefix, void *data)
2287{
2288 int err, i;
2289
2290 for (i = 0; i < num_controls; i++) {
2291 const struct snd_kcontrol_new *control = &controls[i];
2292 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2293 control->name, prefix));
2294 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002295 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2296 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002297 return err;
2298 }
2299 }
2300
2301 return 0;
2302}
2303
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002304/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002305 * snd_soc_add_codec_controls - add an array of controls to a codec.
2306 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002307 * duplicating this code.
2308 *
2309 * @codec: codec to add controls to
2310 * @controls: array of controls to add
2311 * @num_controls: number of elements in the array
2312 *
2313 * Return 0 for success, else error.
2314 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002315int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002316 const struct snd_kcontrol_new *controls, int num_controls)
2317{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002318 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002319
Liam Girdwood022658b2012-02-03 17:43:09 +00002320 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2321 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002322}
Liam Girdwood022658b2012-02-03 17:43:09 +00002323EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002324
2325/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002326 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002327 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002328 *
2329 * @platform: platform to add controls to
2330 * @controls: array of controls to add
2331 * @num_controls: number of elements in the array
2332 *
2333 * Return 0 for success, else error.
2334 */
2335int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2336 const struct snd_kcontrol_new *controls, int num_controls)
2337{
2338 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002339
Liam Girdwood022658b2012-02-03 17:43:09 +00002340 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2341 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002342}
2343EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2344
2345/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002346 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2347 * Convenience function to add a list of controls.
2348 *
2349 * @soc_card: SoC card to add controls to
2350 * @controls: array of controls to add
2351 * @num_controls: number of elements in the array
2352 *
2353 * Return 0 for success, else error.
2354 */
2355int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2356 const struct snd_kcontrol_new *controls, int num_controls)
2357{
2358 struct snd_card *card = soc_card->snd_card;
2359
2360 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2361 NULL, soc_card);
2362}
2363EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2364
2365/**
2366 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2367 * Convienience function to add a list of controls.
2368 *
2369 * @dai: DAI to add controls to
2370 * @controls: array of controls to add
2371 * @num_controls: number of elements in the array
2372 *
2373 * Return 0 for success, else error.
2374 */
2375int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2376 const struct snd_kcontrol_new *controls, int num_controls)
2377{
2378 struct snd_card *card = dai->card->snd_card;
2379
2380 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2381 NULL, dai);
2382}
2383EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2384
2385/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002386 * snd_soc_info_enum_double - enumerated double mixer info callback
2387 * @kcontrol: mixer control
2388 * @uinfo: control element information
2389 *
2390 * Callback to provide information about a double enumerated
2391 * mixer control.
2392 *
2393 * Returns 0 for success.
2394 */
2395int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2396 struct snd_ctl_elem_info *uinfo)
2397{
2398 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2399
2400 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2401 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002402 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002403
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002404 if (uinfo->value.enumerated.item > e->max - 1)
2405 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002406 strcpy(uinfo->value.enumerated.name,
2407 e->texts[uinfo->value.enumerated.item]);
2408 return 0;
2409}
2410EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2411
2412/**
2413 * snd_soc_get_enum_double - enumerated double mixer get callback
2414 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002415 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002416 *
2417 * Callback to get the value of a double enumerated mixer.
2418 *
2419 * Returns 0 for success.
2420 */
2421int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2422 struct snd_ctl_elem_value *ucontrol)
2423{
2424 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2425 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002426 unsigned int val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002427
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002428 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002429 ucontrol->value.enumerated.item[0]
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002430 = (val >> e->shift_l) & e->mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002431 if (e->shift_l != e->shift_r)
2432 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002433 (val >> e->shift_r) & e->mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002434
2435 return 0;
2436}
2437EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2438
2439/**
2440 * snd_soc_put_enum_double - enumerated double mixer put callback
2441 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002442 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002443 *
2444 * Callback to set the value of a double enumerated mixer.
2445 *
2446 * Returns 0 for success.
2447 */
2448int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2449 struct snd_ctl_elem_value *ucontrol)
2450{
2451 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2452 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002453 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002454 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002455
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002456 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002457 return -EINVAL;
2458 val = ucontrol->value.enumerated.item[0] << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002459 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002460 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002461 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002462 return -EINVAL;
2463 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002464 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002465 }
2466
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002467 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002468}
2469EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2470
2471/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002472 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2473 * @kcontrol: mixer control
2474 * @ucontrol: control element information
2475 *
2476 * Callback to get the value of a double semi enumerated mixer.
2477 *
2478 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2479 * used for handling bitfield coded enumeration for example.
2480 *
2481 * Returns 0 for success.
2482 */
2483int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2484 struct snd_ctl_elem_value *ucontrol)
2485{
2486 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002487 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002488 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002489
2490 reg_val = snd_soc_read(codec, e->reg);
2491 val = (reg_val >> e->shift_l) & e->mask;
2492 for (mux = 0; mux < e->max; mux++) {
2493 if (val == e->values[mux])
2494 break;
2495 }
2496 ucontrol->value.enumerated.item[0] = mux;
2497 if (e->shift_l != e->shift_r) {
2498 val = (reg_val >> e->shift_r) & e->mask;
2499 for (mux = 0; mux < e->max; mux++) {
2500 if (val == e->values[mux])
2501 break;
2502 }
2503 ucontrol->value.enumerated.item[1] = mux;
2504 }
2505
2506 return 0;
2507}
2508EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2509
2510/**
2511 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2512 * @kcontrol: mixer control
2513 * @ucontrol: control element information
2514 *
2515 * Callback to set the value of a double semi enumerated mixer.
2516 *
2517 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2518 * used for handling bitfield coded enumeration for example.
2519 *
2520 * Returns 0 for success.
2521 */
2522int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2523 struct snd_ctl_elem_value *ucontrol)
2524{
2525 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002526 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002527 unsigned int val;
2528 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002529
2530 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2531 return -EINVAL;
2532 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2533 mask = e->mask << e->shift_l;
2534 if (e->shift_l != e->shift_r) {
2535 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2536 return -EINVAL;
2537 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2538 mask |= e->mask << e->shift_r;
2539 }
2540
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002541 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002542}
2543EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2544
2545/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002546 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2547 * @kcontrol: mixer control
2548 * @uinfo: control element information
2549 *
2550 * Callback to provide information about an external enumerated
2551 * single mixer.
2552 *
2553 * Returns 0 for success.
2554 */
2555int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2556 struct snd_ctl_elem_info *uinfo)
2557{
2558 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2559
2560 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2561 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002562 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002563
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002564 if (uinfo->value.enumerated.item > e->max - 1)
2565 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002566 strcpy(uinfo->value.enumerated.name,
2567 e->texts[uinfo->value.enumerated.item]);
2568 return 0;
2569}
2570EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2571
2572/**
2573 * snd_soc_info_volsw_ext - external single mixer info callback
2574 * @kcontrol: mixer control
2575 * @uinfo: control element information
2576 *
2577 * Callback to provide information about a single external mixer control.
2578 *
2579 * Returns 0 for success.
2580 */
2581int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2582 struct snd_ctl_elem_info *uinfo)
2583{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002584 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002585
Mark Brownfd5dfad2009-04-15 21:37:46 +01002586 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002587 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2588 else
2589 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2590
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002591 uinfo->count = 1;
2592 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002593 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002594 return 0;
2595}
2596EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2597
2598/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002599 * snd_soc_info_volsw - single mixer info callback
2600 * @kcontrol: mixer control
2601 * @uinfo: control element information
2602 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002603 * Callback to provide information about a single mixer control, or a double
2604 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002605 *
2606 * Returns 0 for success.
2607 */
2608int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2609 struct snd_ctl_elem_info *uinfo)
2610{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002611 struct soc_mixer_control *mc =
2612 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002613 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002614
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002615 if (!mc->platform_max)
2616 mc->platform_max = mc->max;
2617 platform_max = mc->platform_max;
2618
2619 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002620 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2621 else
2622 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2623
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002624 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002625 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002626 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002627 return 0;
2628}
2629EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2630
2631/**
2632 * snd_soc_get_volsw - single mixer get callback
2633 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002634 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002635 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002636 * Callback to get the value of a single mixer control, or a double mixer
2637 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002638 *
2639 * Returns 0 for success.
2640 */
2641int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2642 struct snd_ctl_elem_value *ucontrol)
2643{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002644 struct soc_mixer_control *mc =
2645 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002646 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002647 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002648 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002649 unsigned int shift = mc->shift;
2650 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002651 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002652 unsigned int mask = (1 << fls(max)) - 1;
2653 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002654
2655 ucontrol->value.integer.value[0] =
2656 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002657 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002658 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002659 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002660
2661 if (snd_soc_volsw_is_stereo(mc)) {
2662 if (reg == reg2)
2663 ucontrol->value.integer.value[1] =
2664 (snd_soc_read(codec, reg) >> rshift) & mask;
2665 else
2666 ucontrol->value.integer.value[1] =
2667 (snd_soc_read(codec, reg2) >> shift) & mask;
2668 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002669 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002670 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002671 }
2672
2673 return 0;
2674}
2675EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2676
2677/**
2678 * snd_soc_put_volsw - single mixer put callback
2679 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002680 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002681 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002682 * Callback to set the value of a single mixer control, or a double mixer
2683 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002684 *
2685 * Returns 0 for success.
2686 */
2687int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2688 struct snd_ctl_elem_value *ucontrol)
2689{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002690 struct soc_mixer_control *mc =
2691 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002692 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002693 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002694 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002695 unsigned int shift = mc->shift;
2696 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002697 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002698 unsigned int mask = (1 << fls(max)) - 1;
2699 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002700 int err;
2701 bool type_2r = 0;
2702 unsigned int val2 = 0;
2703 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002704
2705 val = (ucontrol->value.integer.value[0] & mask);
2706 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002707 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002708 val_mask = mask << shift;
2709 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002710 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002711 val2 = (ucontrol->value.integer.value[1] & mask);
2712 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002713 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002714 if (reg == reg2) {
2715 val_mask |= mask << rshift;
2716 val |= val2 << rshift;
2717 } else {
2718 val2 = val2 << shift;
2719 type_2r = 1;
2720 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002721 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002722 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002723 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002724 return err;
2725
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002726 if (type_2r)
2727 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2728
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002729 return err;
2730}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002731EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002732
Mark Browne13ac2e2008-05-28 17:58:05 +01002733/**
Brian Austin1d99f242012-03-30 10:43:55 -05002734 * snd_soc_get_volsw_sx - single mixer get callback
2735 * @kcontrol: mixer control
2736 * @ucontrol: control element information
2737 *
2738 * Callback to get the value of a single mixer control, or a double mixer
2739 * control that spans 2 registers.
2740 *
2741 * Returns 0 for success.
2742 */
2743int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2744 struct snd_ctl_elem_value *ucontrol)
2745{
2746 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2747 struct soc_mixer_control *mc =
2748 (struct soc_mixer_control *)kcontrol->private_value;
2749
2750 unsigned int reg = mc->reg;
2751 unsigned int reg2 = mc->rreg;
2752 unsigned int shift = mc->shift;
2753 unsigned int rshift = mc->rshift;
2754 int max = mc->max;
2755 int min = mc->min;
2756 int mask = (1 << (fls(min + max) - 1)) - 1;
2757
2758 ucontrol->value.integer.value[0] =
2759 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2760
2761 if (snd_soc_volsw_is_stereo(mc))
2762 ucontrol->value.integer.value[1] =
2763 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2764
2765 return 0;
2766}
2767EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2768
2769/**
2770 * snd_soc_put_volsw_sx - double mixer set callback
2771 * @kcontrol: mixer control
2772 * @uinfo: control element information
2773 *
2774 * Callback to set the value of a double mixer control that spans 2 registers.
2775 *
2776 * Returns 0 for success.
2777 */
2778int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2779 struct snd_ctl_elem_value *ucontrol)
2780{
2781 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2782 struct soc_mixer_control *mc =
2783 (struct soc_mixer_control *)kcontrol->private_value;
2784
2785 unsigned int reg = mc->reg;
2786 unsigned int reg2 = mc->rreg;
2787 unsigned int shift = mc->shift;
2788 unsigned int rshift = mc->rshift;
2789 int max = mc->max;
2790 int min = mc->min;
2791 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002792 int err = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002793 unsigned short val, val_mask, val2 = 0;
2794
2795 val_mask = mask << shift;
2796 val = (ucontrol->value.integer.value[0] + min) & mask;
2797 val = val << shift;
2798
Mukund Navadad0558522012-11-09 11:53:40 +05302799 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2800 if (err < 0)
2801 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002802
2803 if (snd_soc_volsw_is_stereo(mc)) {
2804 val_mask = mask << rshift;
2805 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2806 val2 = val2 << rshift;
2807
2808 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2809 return err;
2810 }
2811 return 0;
2812}
2813EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2814
2815/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002816 * snd_soc_info_volsw_s8 - signed mixer info callback
2817 * @kcontrol: mixer control
2818 * @uinfo: control element information
2819 *
2820 * Callback to provide information about a signed mixer control.
2821 *
2822 * Returns 0 for success.
2823 */
2824int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2825 struct snd_ctl_elem_info *uinfo)
2826{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002827 struct soc_mixer_control *mc =
2828 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002829 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002830 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002831
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002832 if (!mc->platform_max)
2833 mc->platform_max = mc->max;
2834 platform_max = mc->platform_max;
2835
Mark Browne13ac2e2008-05-28 17:58:05 +01002836 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2837 uinfo->count = 2;
2838 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002839 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002840 return 0;
2841}
2842EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2843
2844/**
2845 * snd_soc_get_volsw_s8 - signed mixer get callback
2846 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002847 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002848 *
2849 * Callback to get the value of a signed mixer control.
2850 *
2851 * Returns 0 for success.
2852 */
2853int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2854 struct snd_ctl_elem_value *ucontrol)
2855{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002856 struct soc_mixer_control *mc =
2857 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002858 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002859 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002860 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002861 int val = snd_soc_read(codec, reg);
2862
2863 ucontrol->value.integer.value[0] =
2864 ((signed char)(val & 0xff))-min;
2865 ucontrol->value.integer.value[1] =
2866 ((signed char)((val >> 8) & 0xff))-min;
2867 return 0;
2868}
2869EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2870
2871/**
2872 * snd_soc_put_volsw_sgn - signed mixer put callback
2873 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002874 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002875 *
2876 * Callback to set the value of a signed mixer control.
2877 *
2878 * Returns 0 for success.
2879 */
2880int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2881 struct snd_ctl_elem_value *ucontrol)
2882{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002883 struct soc_mixer_control *mc =
2884 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002885 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002886 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002887 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002888 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002889
2890 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2891 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2892
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002893 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002894}
2895EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2896
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002897/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002898 * snd_soc_info_volsw_range - single mixer info callback with range.
2899 * @kcontrol: mixer control
2900 * @uinfo: control element information
2901 *
2902 * Callback to provide information, within a range, about a single
2903 * mixer control.
2904 *
2905 * returns 0 for success.
2906 */
2907int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2908 struct snd_ctl_elem_info *uinfo)
2909{
2910 struct soc_mixer_control *mc =
2911 (struct soc_mixer_control *)kcontrol->private_value;
2912 int platform_max;
2913 int min = mc->min;
2914
2915 if (!mc->platform_max)
2916 mc->platform_max = mc->max;
2917 platform_max = mc->platform_max;
2918
2919 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002920 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002921 uinfo->value.integer.min = 0;
2922 uinfo->value.integer.max = platform_max - min;
2923
2924 return 0;
2925}
2926EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2927
2928/**
2929 * snd_soc_put_volsw_range - single mixer put value callback with range.
2930 * @kcontrol: mixer control
2931 * @ucontrol: control element information
2932 *
2933 * Callback to set the value, within a range, for a single mixer control.
2934 *
2935 * Returns 0 for success.
2936 */
2937int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2938 struct snd_ctl_elem_value *ucontrol)
2939{
2940 struct soc_mixer_control *mc =
2941 (struct soc_mixer_control *)kcontrol->private_value;
2942 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2943 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002944 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002945 unsigned int shift = mc->shift;
2946 int min = mc->min;
2947 int max = mc->max;
2948 unsigned int mask = (1 << fls(max)) - 1;
2949 unsigned int invert = mc->invert;
2950 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002951 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002952
2953 val = ((ucontrol->value.integer.value[0] + min) & mask);
2954 if (invert)
2955 val = max - val;
2956 val_mask = mask << shift;
2957 val = val << shift;
2958
Mark Brown9bde4f02012-12-19 16:05:00 +00002959 ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2960 if (ret != 0)
2961 return ret;
2962
2963 if (snd_soc_volsw_is_stereo(mc)) {
2964 val = ((ucontrol->value.integer.value[1] + min) & mask);
2965 if (invert)
2966 val = max - val;
2967 val_mask = mask << shift;
2968 val = val << shift;
2969
2970 ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
2971 }
2972
2973 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002974}
2975EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2976
2977/**
2978 * snd_soc_get_volsw_range - single mixer get callback with range
2979 * @kcontrol: mixer control
2980 * @ucontrol: control element information
2981 *
2982 * Callback to get the value, within a range, of a single mixer control.
2983 *
2984 * Returns 0 for success.
2985 */
2986int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2987 struct snd_ctl_elem_value *ucontrol)
2988{
2989 struct soc_mixer_control *mc =
2990 (struct soc_mixer_control *)kcontrol->private_value;
2991 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2992 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002993 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002994 unsigned int shift = mc->shift;
2995 int min = mc->min;
2996 int max = mc->max;
2997 unsigned int mask = (1 << fls(max)) - 1;
2998 unsigned int invert = mc->invert;
Mark Brown9bde4f02012-12-19 16:05:00 +00002999 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003000
3001 ucontrol->value.integer.value[0] =
3002 (snd_soc_read(codec, reg) >> shift) & mask;
3003 if (invert)
3004 ucontrol->value.integer.value[0] =
3005 max - ucontrol->value.integer.value[0];
3006 ucontrol->value.integer.value[0] =
3007 ucontrol->value.integer.value[0] - min;
3008
Mark Brown9bde4f02012-12-19 16:05:00 +00003009 if (snd_soc_volsw_is_stereo(mc)) {
3010 ucontrol->value.integer.value[1] =
3011 (snd_soc_read(codec, rreg) >> shift) & mask;
3012 if (invert)
3013 ucontrol->value.integer.value[1] =
3014 max - ucontrol->value.integer.value[1];
3015 ucontrol->value.integer.value[1] =
3016 ucontrol->value.integer.value[1] - min;
3017 }
3018
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003019 return 0;
3020}
3021EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3022
3023/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003024 * snd_soc_limit_volume - Set new limit to an existing volume control.
3025 *
3026 * @codec: where to look for the control
3027 * @name: Name of the control
3028 * @max: new maximum limit
3029 *
3030 * Return 0 for success, else error.
3031 */
3032int snd_soc_limit_volume(struct snd_soc_codec *codec,
3033 const char *name, int max)
3034{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003035 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003036 struct snd_kcontrol *kctl;
3037 struct soc_mixer_control *mc;
3038 int found = 0;
3039 int ret = -EINVAL;
3040
3041 /* Sanity check for name and max */
3042 if (unlikely(!name || max <= 0))
3043 return -EINVAL;
3044
3045 list_for_each_entry(kctl, &card->controls, list) {
3046 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3047 found = 1;
3048 break;
3049 }
3050 }
3051 if (found) {
3052 mc = (struct soc_mixer_control *)kctl->private_value;
3053 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003054 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003055 ret = 0;
3056 }
3057 }
3058 return ret;
3059}
3060EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3061
Mark Brown71d08512011-10-10 18:31:26 +01003062int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3063 struct snd_ctl_elem_info *uinfo)
3064{
3065 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3066 struct soc_bytes *params = (void *)kcontrol->private_value;
3067
3068 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3069 uinfo->count = params->num_regs * codec->val_bytes;
3070
3071 return 0;
3072}
3073EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3074
3075int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3076 struct snd_ctl_elem_value *ucontrol)
3077{
3078 struct soc_bytes *params = (void *)kcontrol->private_value;
3079 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3080 int ret;
3081
3082 if (codec->using_regmap)
3083 ret = regmap_raw_read(codec->control_data, params->base,
3084 ucontrol->value.bytes.data,
3085 params->num_regs * codec->val_bytes);
3086 else
3087 ret = -EINVAL;
3088
Mark Brownf831b052012-02-17 16:20:33 -08003089 /* Hide any masked bytes to ensure consistent data reporting */
3090 if (ret == 0 && params->mask) {
3091 switch (codec->val_bytes) {
3092 case 1:
3093 ucontrol->value.bytes.data[0] &= ~params->mask;
3094 break;
3095 case 2:
3096 ((u16 *)(&ucontrol->value.bytes.data))[0]
3097 &= ~params->mask;
3098 break;
3099 case 4:
3100 ((u32 *)(&ucontrol->value.bytes.data))[0]
3101 &= ~params->mask;
3102 break;
3103 default:
3104 return -EINVAL;
3105 }
3106 }
3107
Mark Brown71d08512011-10-10 18:31:26 +01003108 return ret;
3109}
3110EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3111
3112int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3113 struct snd_ctl_elem_value *ucontrol)
3114{
3115 struct soc_bytes *params = (void *)kcontrol->private_value;
3116 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08003117 int ret, len;
3118 unsigned int val;
3119 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003120
Mark Brownf831b052012-02-17 16:20:33 -08003121 if (!codec->using_regmap)
3122 return -EINVAL;
3123
3124 data = ucontrol->value.bytes.data;
3125 len = params->num_regs * codec->val_bytes;
3126
3127 /*
3128 * If we've got a mask then we need to preserve the register
3129 * bits. We shouldn't modify the incoming data so take a
3130 * copy.
3131 */
3132 if (params->mask) {
3133 ret = regmap_read(codec->control_data, params->base, &val);
3134 if (ret != 0)
3135 return ret;
3136
3137 val &= params->mask;
3138
3139 data = kmemdup(data, len, GFP_KERNEL);
3140 if (!data)
3141 return -ENOMEM;
3142
3143 switch (codec->val_bytes) {
3144 case 1:
3145 ((u8 *)data)[0] &= ~params->mask;
3146 ((u8 *)data)[0] |= val;
3147 break;
3148 case 2:
3149 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3150 ((u16 *)data)[0] |= cpu_to_be16(val);
3151 break;
3152 case 4:
3153 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3154 ((u32 *)data)[0] |= cpu_to_be32(val);
3155 break;
3156 default:
3157 return -EINVAL;
3158 }
3159 }
3160
3161 ret = regmap_raw_write(codec->control_data, params->base,
3162 data, len);
3163
3164 if (params->mask)
3165 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003166
3167 return ret;
3168}
3169EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3170
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003171/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003172 * snd_soc_info_xr_sx - signed multi register info callback
3173 * @kcontrol: mreg control
3174 * @uinfo: control element information
3175 *
3176 * Callback to provide information of a control that can
3177 * span multiple codec registers which together
3178 * forms a single signed value in a MSB/LSB manner.
3179 *
3180 * Returns 0 for success.
3181 */
3182int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3183 struct snd_ctl_elem_info *uinfo)
3184{
3185 struct soc_mreg_control *mc =
3186 (struct soc_mreg_control *)kcontrol->private_value;
3187 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3188 uinfo->count = 1;
3189 uinfo->value.integer.min = mc->min;
3190 uinfo->value.integer.max = mc->max;
3191
3192 return 0;
3193}
3194EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3195
3196/**
3197 * snd_soc_get_xr_sx - signed multi register get callback
3198 * @kcontrol: mreg control
3199 * @ucontrol: control element information
3200 *
3201 * Callback to get the value of a control that can span
3202 * multiple codec registers which together forms a single
3203 * signed value in a MSB/LSB manner. The control supports
3204 * specifying total no of bits used to allow for bitfields
3205 * across the multiple codec registers.
3206 *
3207 * Returns 0 for success.
3208 */
3209int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3210 struct snd_ctl_elem_value *ucontrol)
3211{
3212 struct soc_mreg_control *mc =
3213 (struct soc_mreg_control *)kcontrol->private_value;
3214 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3215 unsigned int regbase = mc->regbase;
3216 unsigned int regcount = mc->regcount;
3217 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3218 unsigned int regwmask = (1<<regwshift)-1;
3219 unsigned int invert = mc->invert;
3220 unsigned long mask = (1UL<<mc->nbits)-1;
3221 long min = mc->min;
3222 long max = mc->max;
3223 long val = 0;
3224 unsigned long regval;
3225 unsigned int i;
3226
3227 for (i = 0; i < regcount; i++) {
3228 regval = snd_soc_read(codec, regbase+i) & regwmask;
3229 val |= regval << (regwshift*(regcount-i-1));
3230 }
3231 val &= mask;
3232 if (min < 0 && val > max)
3233 val |= ~mask;
3234 if (invert)
3235 val = max - val;
3236 ucontrol->value.integer.value[0] = val;
3237
3238 return 0;
3239}
3240EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3241
3242/**
3243 * snd_soc_put_xr_sx - signed multi register get callback
3244 * @kcontrol: mreg control
3245 * @ucontrol: control element information
3246 *
3247 * Callback to set the value of a control that can span
3248 * multiple codec registers which together forms a single
3249 * signed value in a MSB/LSB manner. The control supports
3250 * specifying total no of bits used to allow for bitfields
3251 * across the multiple codec registers.
3252 *
3253 * Returns 0 for success.
3254 */
3255int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3256 struct snd_ctl_elem_value *ucontrol)
3257{
3258 struct soc_mreg_control *mc =
3259 (struct soc_mreg_control *)kcontrol->private_value;
3260 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3261 unsigned int regbase = mc->regbase;
3262 unsigned int regcount = mc->regcount;
3263 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3264 unsigned int regwmask = (1<<regwshift)-1;
3265 unsigned int invert = mc->invert;
3266 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003267 long max = mc->max;
3268 long val = ucontrol->value.integer.value[0];
3269 unsigned int i, regval, regmask;
3270 int err;
3271
3272 if (invert)
3273 val = max - val;
3274 val &= mask;
3275 for (i = 0; i < regcount; i++) {
3276 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3277 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3278 err = snd_soc_update_bits_locked(codec, regbase+i,
3279 regmask, regval);
3280 if (err < 0)
3281 return err;
3282 }
3283
3284 return 0;
3285}
3286EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3287
3288/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003289 * snd_soc_get_strobe - strobe get callback
3290 * @kcontrol: mixer control
3291 * @ucontrol: control element information
3292 *
3293 * Callback get the value of a strobe mixer control.
3294 *
3295 * Returns 0 for success.
3296 */
3297int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3298 struct snd_ctl_elem_value *ucontrol)
3299{
3300 struct soc_mixer_control *mc =
3301 (struct soc_mixer_control *)kcontrol->private_value;
3302 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3303 unsigned int reg = mc->reg;
3304 unsigned int shift = mc->shift;
3305 unsigned int mask = 1 << shift;
3306 unsigned int invert = mc->invert != 0;
3307 unsigned int val = snd_soc_read(codec, reg) & mask;
3308
3309 if (shift != 0 && val != 0)
3310 val = val >> shift;
3311 ucontrol->value.enumerated.item[0] = val ^ invert;
3312
3313 return 0;
3314}
3315EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3316
3317/**
3318 * snd_soc_put_strobe - strobe put callback
3319 * @kcontrol: mixer control
3320 * @ucontrol: control element information
3321 *
3322 * Callback strobe a register bit to high then low (or the inverse)
3323 * in one pass of a single mixer enum control.
3324 *
3325 * Returns 1 for success.
3326 */
3327int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3328 struct snd_ctl_elem_value *ucontrol)
3329{
3330 struct soc_mixer_control *mc =
3331 (struct soc_mixer_control *)kcontrol->private_value;
3332 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3333 unsigned int reg = mc->reg;
3334 unsigned int shift = mc->shift;
3335 unsigned int mask = 1 << shift;
3336 unsigned int invert = mc->invert != 0;
3337 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3338 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3339 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3340 int err;
3341
3342 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3343 if (err < 0)
3344 return err;
3345
3346 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3347 return err;
3348}
3349EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3350
3351/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003352 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3353 * @dai: DAI
3354 * @clk_id: DAI specific clock ID
3355 * @freq: new clock frequency in Hz
3356 * @dir: new clock direction - input/output.
3357 *
3358 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3359 */
3360int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3361 unsigned int freq, int dir)
3362{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003363 if (dai->driver && dai->driver->ops->set_sysclk)
3364 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003365 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003366 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003367 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003368 else
3369 return -EINVAL;
3370}
3371EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3372
3373/**
Mark Brownec4ee522011-03-07 20:58:11 +00003374 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3375 * @codec: CODEC
3376 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003377 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003378 * @freq: new clock frequency in Hz
3379 * @dir: new clock direction - input/output.
3380 *
3381 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3382 */
3383int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003384 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003385{
3386 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003387 return codec->driver->set_sysclk(codec, clk_id, source,
3388 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003389 else
3390 return -EINVAL;
3391}
3392EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3393
3394/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003395 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3396 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003397 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003398 * @div: new clock divisor.
3399 *
3400 * Configures the clock dividers. This is used to derive the best DAI bit and
3401 * frame clocks from the system or master clock. It's best to set the DAI bit
3402 * and frame clocks as low as possible to save system power.
3403 */
3404int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3405 int div_id, int div)
3406{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003407 if (dai->driver && dai->driver->ops->set_clkdiv)
3408 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003409 else
3410 return -EINVAL;
3411}
3412EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3413
3414/**
3415 * snd_soc_dai_set_pll - configure DAI PLL.
3416 * @dai: DAI
3417 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003418 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003419 * @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 */
Mark Brown85488032009-09-05 18:52:16 +01003424int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3425 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003426{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003427 if (dai->driver && dai->driver->ops->set_pll)
3428 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003429 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003430 else if (dai->codec && dai->codec->driver->set_pll)
3431 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3432 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003433 else
3434 return -EINVAL;
3435}
3436EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3437
Mark Brownec4ee522011-03-07 20:58:11 +00003438/*
3439 * snd_soc_codec_set_pll - configure codec PLL.
3440 * @codec: CODEC
3441 * @pll_id: DAI specific PLL ID
3442 * @source: DAI specific source for the PLL
3443 * @freq_in: PLL input clock frequency in Hz
3444 * @freq_out: requested PLL output clock frequency in Hz
3445 *
3446 * Configures and enables PLL to generate output clock based on input clock.
3447 */
3448int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3449 unsigned int freq_in, unsigned int freq_out)
3450{
3451 if (codec->driver->set_pll)
3452 return codec->driver->set_pll(codec, pll_id, source,
3453 freq_in, freq_out);
3454 else
3455 return -EINVAL;
3456}
3457EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3458
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003459/**
3460 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3461 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003462 * @fmt: SND_SOC_DAIFMT_ format value.
3463 *
3464 * Configures the DAI hardware format and clocking.
3465 */
3466int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3467{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003468 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003469 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003470 if (dai->driver->ops->set_fmt == NULL)
3471 return -ENOTSUPP;
3472 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003473}
3474EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3475
3476/**
3477 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3478 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003479 * @tx_mask: bitmask representing active TX slots.
3480 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003481 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003482 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003483 *
3484 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3485 * specific.
3486 */
3487int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003488 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003489{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003490 if (dai->driver && dai->driver->ops->set_tdm_slot)
3491 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003492 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003493 else
3494 return -EINVAL;
3495}
3496EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3497
3498/**
Barry Song472df3c2009-09-12 01:16:29 +08003499 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3500 * @dai: DAI
3501 * @tx_num: how many TX channels
3502 * @tx_slot: pointer to an array which imply the TX slot number channel
3503 * 0~num-1 uses
3504 * @rx_num: how many RX channels
3505 * @rx_slot: pointer to an array which imply the RX slot number channel
3506 * 0~num-1 uses
3507 *
3508 * configure the relationship between channel number and TDM slot number.
3509 */
3510int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3511 unsigned int tx_num, unsigned int *tx_slot,
3512 unsigned int rx_num, unsigned int *rx_slot)
3513{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003514 if (dai->driver && dai->driver->ops->set_channel_map)
3515 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003516 rx_num, rx_slot);
3517 else
3518 return -EINVAL;
3519}
3520EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3521
3522/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003523 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3524 * @dai: DAI
3525 * @tristate: tristate enable
3526 *
3527 * Tristates the DAI so that others can use it.
3528 */
3529int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3530{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003531 if (dai->driver && dai->driver->ops->set_tristate)
3532 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003533 else
3534 return -EINVAL;
3535}
3536EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3537
3538/**
3539 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3540 * @dai: DAI
3541 * @mute: mute enable
3542 *
3543 * Mutes the DAI DAC.
3544 */
3545int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3546{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003547 if (dai->driver && dai->driver->ops->digital_mute)
3548 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003549 else
Mark Brown04570c62012-04-13 19:16:03 +01003550 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003551}
3552EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3553
Mark Brownc5af3a22008-11-28 13:29:45 +00003554/**
3555 * snd_soc_register_card - Register a card with the ASoC core
3556 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003557 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003558 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003559 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303560int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003561{
Mark Brownb19e6e72012-03-14 21:18:39 +00003562 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003563
Mark Brownc5af3a22008-11-28 13:29:45 +00003564 if (!card->name || !card->dev)
3565 return -EINVAL;
3566
Stephen Warren5a504962011-12-21 10:40:59 -07003567 for (i = 0; i < card->num_links; i++) {
3568 struct snd_soc_dai_link *link = &card->dai_link[i];
3569
3570 /*
3571 * Codec must be specified by 1 of name or OF node,
3572 * not both or neither.
3573 */
3574 if (!!link->codec_name == !!link->codec_of_node) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003575 dev_err(card->dev, "ASoC: Neither/both codec"
3576 " name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003577 return -EINVAL;
3578 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003579 /* Codec DAI name must be specified */
3580 if (!link->codec_dai_name) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003581 dev_err(card->dev, "ASoC: codec_dai_name not"
3582 " set for %s\n", link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003583 return -EINVAL;
3584 }
Stephen Warren5a504962011-12-21 10:40:59 -07003585
3586 /*
3587 * Platform may be specified by either name or OF node, but
3588 * can be left unspecified, and a dummy platform will be used.
3589 */
3590 if (link->platform_name && link->platform_of_node) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003591 dev_err(card->dev, "ASoC: Both platform name/of_node"
3592 " are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003593 return -EINVAL;
3594 }
3595
3596 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003597 * CPU device may be specified by either name or OF node, but
3598 * can be left unspecified, and will be matched based on DAI
3599 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003600 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003601 if (link->cpu_name && link->cpu_of_node) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003602 dev_err(card->dev, "ASoC: Neither/both "
3603 "cpu name/of_node are set for %s\n",link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003604 return -EINVAL;
3605 }
3606 /*
3607 * At least one of CPU DAI name or CPU device name/node must be
3608 * specified
3609 */
3610 if (!link->cpu_dai_name &&
3611 !(link->cpu_name || link->cpu_of_node)) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003612 dev_err(card->dev, "ASoC: Neither cpu_dai_name nor "
3613 "cpu_name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003614 return -EINVAL;
3615 }
3616 }
3617
Mark Browned77cc12011-05-03 18:25:34 +01003618 dev_set_drvdata(card->dev, card);
3619
Stephen Warren111c6412011-01-28 14:26:35 -07003620 snd_soc_initialize_card_lists(card);
3621
Vinod Koul150dd2f2011-01-13 22:48:32 +05303622 soc_init_card_debugfs(card);
3623
Mark Brown181a6892012-03-14 20:18:49 +00003624 card->rtd = devm_kzalloc(card->dev,
3625 sizeof(struct snd_soc_pcm_runtime) *
3626 (card->num_links + card->num_aux_devs),
3627 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003628 if (card->rtd == NULL)
3629 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003630 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003631 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003632
3633 for (i = 0; i < card->num_links; i++)
3634 card->rtd[i].dai_link = &card->dai_link[i];
3635
Mark Brownc5af3a22008-11-28 13:29:45 +00003636 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003637 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003638 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003639 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003640 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003641
Mark Brownb19e6e72012-03-14 21:18:39 +00003642 ret = snd_soc_instantiate_card(card);
3643 if (ret != 0)
3644 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003645
Mark Brownb19e6e72012-03-14 21:18:39 +00003646 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003647}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303648EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003649
3650/**
3651 * snd_soc_unregister_card - Unregister a card with the ASoC core
3652 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003653 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003654 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003655 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303656int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003657{
Vinod Koulb0e26482011-01-13 22:48:02 +05303658 if (card->instantiated)
3659 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003660 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003661
3662 return 0;
3663}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303664EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003665
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003666/*
3667 * Simplify DAI link configuration by removing ".-1" from device names
3668 * and sanitizing names.
3669 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003670static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003671{
3672 char *found, name[NAME_SIZE];
3673 int id1, id2;
3674
3675 if (dev_name(dev) == NULL)
3676 return NULL;
3677
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003678 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003679
3680 /* are we a "%s.%d" name (platform and SPI components) */
3681 found = strstr(name, dev->driver->name);
3682 if (found) {
3683 /* get ID */
3684 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3685
3686 /* discard ID from name if ID == -1 */
3687 if (*id == -1)
3688 found[strlen(dev->driver->name)] = '\0';
3689 }
3690
3691 } else {
3692 /* I2C component devices are named "bus-addr" */
3693 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3694 char tmp[NAME_SIZE];
3695
3696 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003697 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003698
3699 /* sanitize component name for DAI link creation */
3700 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003701 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003702 } else
3703 *id = 0;
3704 }
3705
3706 return kstrdup(name, GFP_KERNEL);
3707}
3708
3709/*
3710 * Simplify DAI link naming for single devices with multiple DAIs by removing
3711 * any ".-1" and using the DAI name (instead of device name).
3712 */
3713static inline char *fmt_multiple_name(struct device *dev,
3714 struct snd_soc_dai_driver *dai_drv)
3715{
3716 if (dai_drv->name == NULL) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003717 dev_err(dev, "ASoC: error - multiple DAI %s registered with"
3718 " no name\n", dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003719 return NULL;
3720 }
3721
3722 return kstrdup(dai_drv->name, GFP_KERNEL);
3723}
3724
Mark Brown91151712008-11-30 23:31:24 +00003725/**
3726 * snd_soc_register_dai - Register a DAI with the ASoC core
3727 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003728 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003729 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003730int snd_soc_register_dai(struct device *dev,
3731 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003732{
Mark Brown054880f2012-04-09 17:29:19 +01003733 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003734 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003735
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003736 dev_dbg(dev, "ASoC: dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003737
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003738 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3739 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003740 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003741
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003742 /* create DAI component name */
3743 dai->name = fmt_single_name(dev, &dai->id);
3744 if (dai->name == NULL) {
3745 kfree(dai);
3746 return -ENOMEM;
3747 }
3748
3749 dai->dev = dev;
3750 dai->driver = dai_drv;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003751 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003752 if (!dai->driver->ops)
3753 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003754
3755 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003756
3757 list_for_each_entry(codec, &codec_list, list) {
3758 if (codec->dev == dev) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003759 dev_dbg(dev, "ASoC: Mapped DAI %s to CODEC %s\n",
Mark Brown054880f2012-04-09 17:29:19 +01003760 dai->name, codec->name);
3761 dai->codec = codec;
3762 break;
3763 }
3764 }
3765
Peter Ujfalusi5f800082012-08-07 10:24:13 +03003766 if (!dai->codec)
3767 dai->dapm.idle_bias_off = 1;
3768
Mark Brown91151712008-11-30 23:31:24 +00003769 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003770
Mark Brown91151712008-11-30 23:31:24 +00003771 mutex_unlock(&client_mutex);
3772
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003773 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003774
3775 return 0;
3776}
3777EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3778
3779/**
3780 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3781 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003782 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003783 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003784void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003785{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003786 struct snd_soc_dai *dai;
3787
3788 list_for_each_entry(dai, &dai_list, list) {
3789 if (dev == dai->dev)
3790 goto found;
3791 }
3792 return;
3793
3794found:
Mark Brown91151712008-11-30 23:31:24 +00003795 mutex_lock(&client_mutex);
3796 list_del(&dai->list);
3797 mutex_unlock(&client_mutex);
3798
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003799 dev_dbg(dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003800 kfree(dai->name);
3801 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003802}
3803EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3804
3805/**
3806 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3807 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003808 * @dai: Array of DAIs to register
3809 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003810 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003811int snd_soc_register_dais(struct device *dev,
3812 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003813{
Mark Brown054880f2012-04-09 17:29:19 +01003814 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003815 struct snd_soc_dai *dai;
3816 int i, ret = 0;
3817
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003818 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003819
3820 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003821
3822 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003823 if (dai == NULL) {
3824 ret = -ENOMEM;
3825 goto err;
3826 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003827
3828 /* create DAI component name */
3829 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3830 if (dai->name == NULL) {
3831 kfree(dai);
3832 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003833 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003834 }
3835
3836 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003837 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003838 if (dai->driver->id)
3839 dai->id = dai->driver->id;
3840 else
3841 dai->id = i;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003842 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003843 if (!dai->driver->ops)
3844 dai->driver->ops = &null_dai_ops;
3845
3846 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003847
3848 list_for_each_entry(codec, &codec_list, list) {
3849 if (codec->dev == dev) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003850 dev_dbg(dev, "ASoC: Mapped DAI %s to "
3851 "CODEC %s\n", dai->name, codec->name);
Mark Brown054880f2012-04-09 17:29:19 +01003852 dai->codec = codec;
3853 break;
3854 }
3855 }
3856
Peter Ujfalusi5f800082012-08-07 10:24:13 +03003857 if (!dai->codec)
3858 dai->dapm.idle_bias_off = 1;
3859
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003860 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003861
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003862 mutex_unlock(&client_mutex);
3863
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003864 dev_dbg(dai->dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003865 }
3866
3867 return 0;
3868
3869err:
3870 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003871 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003872
3873 return ret;
3874}
3875EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3876
3877/**
3878 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3879 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003880 * @dai: Array of DAIs to unregister
3881 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003882 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003883void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003884{
3885 int i;
3886
3887 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003888 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003889}
3890EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3891
Mark Brown12a48a8c2008-12-03 19:40:30 +00003892/**
3893 * snd_soc_register_platform - Register a platform with the ASoC core
3894 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003895 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003896 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003897int snd_soc_register_platform(struct device *dev,
3898 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003899{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003900 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003901
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003902 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003903
3904 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3905 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003906 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003907
3908 /* create platform component name */
3909 platform->name = fmt_single_name(dev, &platform->id);
3910 if (platform->name == NULL) {
3911 kfree(platform);
3912 return -ENOMEM;
3913 }
3914
3915 platform->dev = dev;
3916 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003917 platform->dapm.dev = dev;
3918 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003919 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00003920 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003921
3922 mutex_lock(&client_mutex);
3923 list_add(&platform->list, &platform_list);
3924 mutex_unlock(&client_mutex);
3925
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003926 dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003927
3928 return 0;
3929}
3930EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3931
3932/**
3933 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3934 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003935 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003936 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003937void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003938{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003939 struct snd_soc_platform *platform;
3940
3941 list_for_each_entry(platform, &platform_list, list) {
3942 if (dev == platform->dev)
3943 goto found;
3944 }
3945 return;
3946
3947found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003948 mutex_lock(&client_mutex);
3949 list_del(&platform->list);
3950 mutex_unlock(&client_mutex);
3951
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003952 dev_dbg(dev, "ASoC: Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003953 kfree(platform->name);
3954 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003955}
3956EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3957
Mark Brown151ab222009-05-09 16:22:58 +01003958static u64 codec_format_map[] = {
3959 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3960 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3961 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3962 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3963 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3964 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3965 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3966 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3967 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3968 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3969 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3970 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3971 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3972 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3973 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3974 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3975};
3976
3977/* Fix up the DAI formats for endianness: codecs don't actually see
3978 * the endianness of the data but we're using the CPU format
3979 * definitions which do need to include endianness so we ensure that
3980 * codec DAIs always have both big and little endian variants set.
3981 */
3982static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3983{
3984 int i;
3985
3986 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3987 if (stream->formats & codec_format_map[i])
3988 stream->formats |= codec_format_map[i];
3989}
3990
Mark Brown0d0cf002008-12-10 14:32:45 +00003991/**
3992 * snd_soc_register_codec - Register a codec with the ASoC core
3993 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003994 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003995 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003996int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003997 const struct snd_soc_codec_driver *codec_drv,
3998 struct snd_soc_dai_driver *dai_drv,
3999 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004000{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004001 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004002 struct snd_soc_codec *codec;
4003 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004004
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004005 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004006
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004007 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4008 if (codec == NULL)
4009 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004010
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004011 /* create CODEC component name */
4012 codec->name = fmt_single_name(dev, &codec->id);
4013 if (codec->name == NULL) {
4014 kfree(codec);
4015 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01004016 }
4017
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00004018 if (codec_drv->compress_type)
4019 codec->compress_type = codec_drv->compress_type;
4020 else
4021 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
4022
Mark Brownc3acec22010-12-02 16:15:29 +00004023 codec->write = codec_drv->write;
4024 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004025 codec->volatile_register = codec_drv->volatile_register;
4026 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004027 codec->writable_register = codec_drv->writable_register;
Mark Brown5124e692012-02-08 13:20:50 +00004028 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004029 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4030 codec->dapm.dev = dev;
4031 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00004032 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01004033 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004034 codec->dev = dev;
4035 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004036 codec->num_dai = num_dai;
4037 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004038
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004039 /* allocate CODEC register cache */
4040 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004041 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00004042 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004043 /* it is necessary to make a copy of the default register cache
4044 * because in the case of using a compression type that requires
Bill Pembertonf6e65742012-11-19 13:25:33 -05004045 * the default register cache to be marked as the
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004046 * kernel might have freed the array by the time we initialize
4047 * the cache.
4048 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00004049 if (codec_drv->reg_cache_default) {
4050 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
4051 reg_size, GFP_KERNEL);
4052 if (!codec->reg_def_copy) {
4053 ret = -ENOMEM;
4054 goto fail;
4055 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004056 }
4057 }
4058
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004059 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
4060 if (!codec->volatile_register)
4061 codec->volatile_register = snd_soc_default_volatile_register;
4062 if (!codec->readable_register)
4063 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004064 if (!codec->writable_register)
4065 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004066 }
4067
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004068 for (i = 0; i < num_dai; i++) {
4069 fixup_codec_formats(&dai_drv[i].playback);
4070 fixup_codec_formats(&dai_drv[i].capture);
4071 }
4072
Mark Brown054880f2012-04-09 17:29:19 +01004073 mutex_lock(&client_mutex);
4074 list_add(&codec->list, &codec_list);
4075 mutex_unlock(&client_mutex);
4076
Mark Brown26b01cc2010-08-18 20:20:55 +01004077 /* register any DAIs */
4078 if (num_dai) {
4079 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4080 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004081 dev_err(codec->dev, "ASoC: Failed to regster"
4082 " DAIs: %d\n", ret);
Mark Brown26b01cc2010-08-18 20:20:55 +01004083 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004084
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004085 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004086 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004087
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00004088fail:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004089 kfree(codec->name);
4090 kfree(codec);
4091 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004092}
4093EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4094
4095/**
4096 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4097 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004098 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004099 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004100void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004101{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004102 struct snd_soc_codec *codec;
4103 int i;
4104
4105 list_for_each_entry(codec, &codec_list, list) {
4106 if (dev == codec->dev)
4107 goto found;
4108 }
4109 return;
4110
4111found:
Mark Brown26b01cc2010-08-18 20:20:55 +01004112 if (codec->num_dai)
4113 for (i = 0; i < codec->num_dai; i++)
4114 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004115
Mark Brown0d0cf002008-12-10 14:32:45 +00004116 mutex_lock(&client_mutex);
4117 list_del(&codec->list);
4118 mutex_unlock(&client_mutex);
4119
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004120 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004121
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004122 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004123 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01004124 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004125 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004126}
4127EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4128
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004129/* Retrieve a card's name from device tree */
4130int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4131 const char *propname)
4132{
4133 struct device_node *np = card->dev->of_node;
4134 int ret;
4135
4136 ret = of_property_read_string_index(np, propname, 0, &card->name);
4137 /*
4138 * EINVAL means the property does not exist. This is fine providing
4139 * card->name was previously set, which is checked later in
4140 * snd_soc_register_card.
4141 */
4142 if (ret < 0 && ret != -EINVAL) {
4143 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004144 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004145 propname, ret);
4146 return ret;
4147 }
4148
4149 return 0;
4150}
4151EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4152
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004153int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4154 const char *propname)
4155{
4156 struct device_node *np = card->dev->of_node;
4157 int num_routes;
4158 struct snd_soc_dapm_route *routes;
4159 int i, ret;
4160
4161 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004162 if (num_routes < 0 || num_routes & 1) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004163 dev_err(card->dev, "ASoC: Property '%s' does not exist or its"
4164 " length is not even\n", propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004165 return -EINVAL;
4166 }
4167 num_routes /= 2;
4168 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004169 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004170 propname);
4171 return -EINVAL;
4172 }
4173
4174 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4175 GFP_KERNEL);
4176 if (!routes) {
4177 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004178 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004179 return -EINVAL;
4180 }
4181
4182 for (i = 0; i < num_routes; i++) {
4183 ret = of_property_read_string_index(np, propname,
4184 2 * i, &routes[i].sink);
4185 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004186 dev_err(card->dev,
4187 "ASoC: Property '%s' index %d could not be read: %d\n",
4188 propname, 2 * i, ret);
Matthias Kaehlckeb761c0c2012-07-11 17:36:34 +02004189 kfree(routes);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004190 return -EINVAL;
4191 }
4192 ret = of_property_read_string_index(np, propname,
4193 (2 * i) + 1, &routes[i].source);
4194 if (ret) {
4195 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004196 "ASoC: Property '%s' index %d could not be read: %d\n",
4197 propname, (2 * i) + 1, ret);
Matthias Kaehlckeb761c0c2012-07-11 17:36:34 +02004198 kfree(routes);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004199 return -EINVAL;
4200 }
4201 }
4202
4203 card->num_dapm_routes = num_routes;
4204 card->dapm_routes = routes;
4205
4206 return 0;
4207}
4208EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4209
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004210static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004211{
Mark Brown384c89e2008-12-03 17:34:03 +00004212#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004213 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4214 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004215 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004216 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004217 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004218
Mark Brown8a9dab12011-01-10 22:25:21 +00004219 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004220 &codec_list_fops))
4221 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4222
Mark Brown8a9dab12011-01-10 22:25:21 +00004223 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004224 &dai_list_fops))
4225 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004226
Mark Brown8a9dab12011-01-10 22:25:21 +00004227 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004228 &platform_list_fops))
4229 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004230#endif
4231
Mark Brownfb257892011-04-28 10:57:54 +01004232 snd_soc_util_init();
4233
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004234 return platform_driver_register(&soc_driver);
4235}
Mark Brown4abe8e12010-10-12 17:41:03 +01004236module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004237
Mark Brown7d8c16a2008-11-30 22:11:24 +00004238static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004239{
Mark Brownfb257892011-04-28 10:57:54 +01004240 snd_soc_util_exit();
4241
Mark Brown384c89e2008-12-03 17:34:03 +00004242#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004243 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004244#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004245 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004246}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004247module_exit(snd_soc_exit);
4248
4249/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004250MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004251MODULE_DESCRIPTION("ALSA SoC Core");
4252MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004253MODULE_ALIAS("platform:soc-audio");