blob: 0e64af1a7df922f8de3e4fcaf6ba35c139e6b97b [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 */
Rusty Russell373d4d02013-01-21 17:17:39 +1030254 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
Mark Brown0d51a9c2011-01-06 16:04:57 +0000255
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 }
Chuansheng Liuff541f42012-12-21 18:17:12 +08001110 WARN(codec->dapm.idle_bias_off &&
1111 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
1112 "codec %s can not start from non-off bias"
1113 " with idle_bias_off==1\n", codec->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001114 }
1115
Mark Brown38cbf952012-06-22 13:04:02 +01001116 /* If the driver didn't set I/O up try regmap */
Mark Brown98d30882012-08-01 20:05:47 +01001117 if (!codec->write && dev_get_regmap(codec->dev, NULL))
Mark Brown38cbf952012-06-22 13:04:02 +01001118 snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
1119
Mark Brownb7af1da2011-04-07 19:18:44 +09001120 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001121 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001122 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001123 if (driver->dapm_routes)
1124 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1125 driver->num_dapm_routes);
1126
Jarkko Nikula589c3562010-12-06 16:27:07 +02001127 /* mark codec as probed and add to card codec list */
1128 codec->probed = 1;
1129 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001130 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001131
Jarkko Nikula70d29332011-01-27 16:24:22 +02001132 return 0;
1133
1134err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001135 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001136 module_put(codec->dev->driver->owner);
1137
Jarkko Nikula589c3562010-12-06 16:27:07 +02001138 return ret;
1139}
1140
Liam Girdwood956245e2011-07-01 16:54:08 +01001141static int soc_probe_platform(struct snd_soc_card *card,
1142 struct snd_soc_platform *platform)
1143{
1144 int ret = 0;
1145 const struct snd_soc_platform_driver *driver = platform->driver;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001146 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001147
1148 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001149 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001150
1151 if (!try_module_get(platform->dev->driver->owner))
1152 return -ENODEV;
1153
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001154 soc_init_platform_debugfs(platform);
1155
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001156 if (driver->dapm_widgets)
1157 snd_soc_dapm_new_controls(&platform->dapm,
1158 driver->dapm_widgets, driver->num_dapm_widgets);
1159
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001160 /* Create DAPM widgets for each DAI stream */
1161 list_for_each_entry(dai, &dai_list, list) {
1162 if (dai->dev != platform->dev)
1163 continue;
1164
1165 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1166 }
1167
Stephen Warren3fec6b62012-04-05 12:28:01 -06001168 platform->dapm.idle_bias_off = 1;
1169
Liam Girdwood956245e2011-07-01 16:54:08 +01001170 if (driver->probe) {
1171 ret = driver->probe(platform);
1172 if (ret < 0) {
1173 dev_err(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001174 "ASoC: failed to probe platform %d\n", ret);
Liam Girdwood956245e2011-07-01 16:54:08 +01001175 goto err_probe;
1176 }
1177 }
1178
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001179 if (driver->controls)
1180 snd_soc_add_platform_controls(platform, driver->controls,
1181 driver->num_controls);
1182 if (driver->dapm_routes)
1183 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1184 driver->num_dapm_routes);
1185
Liam Girdwood956245e2011-07-01 16:54:08 +01001186 /* mark platform as probed and add to card platform list */
1187 platform->probed = 1;
1188 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001189 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001190
1191 return 0;
1192
1193err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001194 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001195 module_put(platform->dev->driver->owner);
1196
1197 return ret;
1198}
1199
Mark Brown36ae1a92012-01-06 17:12:45 -08001200static void rtd_release(struct device *dev)
1201{
1202 kfree(dev);
1203}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001204
Jarkko Nikula589c3562010-12-06 16:27:07 +02001205static int soc_post_component_init(struct snd_soc_card *card,
1206 struct snd_soc_codec *codec,
1207 int num, int dailess)
1208{
1209 struct snd_soc_dai_link *dai_link = NULL;
1210 struct snd_soc_aux_dev *aux_dev = NULL;
1211 struct snd_soc_pcm_runtime *rtd;
1212 const char *temp, *name;
1213 int ret = 0;
1214
1215 if (!dailess) {
1216 dai_link = &card->dai_link[num];
1217 rtd = &card->rtd[num];
1218 name = dai_link->name;
1219 } else {
1220 aux_dev = &card->aux_dev[num];
1221 rtd = &card->rtd_aux[num];
1222 name = aux_dev->name;
1223 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001224 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001225
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001226 /* Make sure all DAPM widgets are instantiated */
1227 snd_soc_dapm_new_widgets(&codec->dapm);
1228
Jarkko Nikula589c3562010-12-06 16:27:07 +02001229 /* machine controls, routes and widgets are not prefixed */
1230 temp = codec->name_prefix;
1231 codec->name_prefix = NULL;
1232
1233 /* do machine specific initialization */
1234 if (!dailess && dai_link->init)
1235 ret = dai_link->init(rtd);
1236 else if (dailess && aux_dev->init)
1237 ret = aux_dev->init(&codec->dapm);
1238 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001239 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001240 return ret;
1241 }
1242 codec->name_prefix = temp;
1243
Jarkko Nikula589c3562010-12-06 16:27:07 +02001244 /* register the rtd device */
1245 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001246
1247 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1248 if (!rtd->dev)
1249 return -ENOMEM;
1250 device_initialize(rtd->dev);
1251 rtd->dev->parent = card->dev;
1252 rtd->dev->release = rtd_release;
1253 rtd->dev->init_name = name;
1254 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001255 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001256 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1257 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1258 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1259 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001260 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001261 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001262 /* calling put_device() here to free the rtd->dev */
1263 put_device(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001264 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001265 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001266 return ret;
1267 }
1268 rtd->dev_registered = 1;
1269
1270 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001271 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001272 if (ret < 0)
1273 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001274 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001275
1276 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001277 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001278 if (ret < 0)
1279 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001280 "ASoC: failed to add codec sysfs files: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001281
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001282#ifdef CONFIG_DEBUG_FS
1283 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001284 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001285 goto out;
1286
1287 ret = soc_dpcm_debugfs_add(rtd);
1288 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001289 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001290
1291out:
1292#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001293 return 0;
1294}
1295
Stephen Warren62ae68f2012-06-08 12:34:23 -06001296static int soc_probe_link_components(struct snd_soc_card *card, int num,
1297 int order)
1298{
1299 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1300 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1301 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1302 struct snd_soc_platform *platform = rtd->platform;
1303 int ret;
1304
1305 /* probe the CPU-side component, if it is a CODEC */
1306 if (cpu_dai->codec &&
1307 !cpu_dai->codec->probed &&
1308 cpu_dai->codec->driver->probe_order == order) {
1309 ret = soc_probe_codec(card, cpu_dai->codec);
1310 if (ret < 0)
1311 return ret;
1312 }
1313
1314 /* probe the CODEC-side component */
1315 if (!codec_dai->codec->probed &&
1316 codec_dai->codec->driver->probe_order == order) {
1317 ret = soc_probe_codec(card, codec_dai->codec);
1318 if (ret < 0)
1319 return ret;
1320 }
1321
1322 /* probe the platform */
1323 if (!platform->probed &&
1324 platform->driver->probe_order == order) {
1325 ret = soc_probe_platform(card, platform);
1326 if (ret < 0)
1327 return ret;
1328 }
1329
1330 return 0;
1331}
1332
1333static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001334{
1335 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1336 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1337 struct snd_soc_codec *codec = rtd->codec;
1338 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001339 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1340 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1341 struct snd_soc_dapm_widget *play_w, *capture_w;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001342 int ret;
1343
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001344 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001345 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001346
1347 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001348 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001349 codec_dai->card = card;
1350 cpu_dai->card = card;
1351
1352 /* set default power off timeout */
1353 rtd->pmdown_time = pmdown_time;
1354
1355 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001356 if (!cpu_dai->probed &&
1357 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001358 if (!cpu_dai->codec) {
1359 cpu_dai->dapm.card = card;
1360 if (!try_module_get(cpu_dai->dev->driver->owner))
1361 return -ENODEV;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001362
Stephen Warren18d75642012-06-08 12:34:21 -06001363 list_add(&cpu_dai->dapm.list, &card->dapm_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001364 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1365 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001366
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001367 if (cpu_dai->driver->probe) {
1368 ret = cpu_dai->driver->probe(cpu_dai);
1369 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001370 dev_err(cpu_dai->dev,
1371 "ASoC: failed to probe CPU DAI %s: %d\n",
1372 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001373 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001374 return ret;
1375 }
1376 }
1377 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001378 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001379 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1380 }
1381
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001382 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001383 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001384 if (codec_dai->driver->probe) {
1385 ret = codec_dai->driver->probe(codec_dai);
1386 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001387 dev_err(codec_dai->dev,
1388 "ASoC: failed to probe CODEC DAI %s: %d\n",
1389 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001390 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001391 }
1392 }
1393
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001394 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001395 codec_dai->probed = 1;
1396 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001397 }
1398
Liam Girdwood0168bf02011-06-07 16:08:05 +01001399 /* complete DAI probe during last probe */
1400 if (order != SND_SOC_COMP_ORDER_LAST)
1401 return 0;
1402
Jarkko Nikula589c3562010-12-06 16:27:07 +02001403 ret = soc_post_component_init(card, codec, num, 0);
1404 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001405 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001406
Mark Brown36ae1a92012-01-06 17:12:45 -08001407 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001408 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001409 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1410 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001411
Namarta Kohli1245b702012-08-16 17:10:41 +05301412 if (cpu_dai->driver->compress_dai) {
1413 /*create compress_device"*/
1414 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001415 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001416 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301417 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001418 return ret;
1419 }
1420 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301421
1422 if (!dai_link->params) {
1423 /* create the pcm */
1424 ret = soc_new_pcm(rtd, num);
1425 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001426 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301427 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001428 return ret;
1429 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301430 } else {
1431 /* link the DAI widgets */
1432 play_w = codec_dai->playback_widget;
1433 capture_w = cpu_dai->capture_widget;
1434 if (play_w && capture_w) {
1435 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Mark Brownc74184e2012-04-04 22:12:09 +01001436 capture_w, play_w);
Namarta Kohli1245b702012-08-16 17:10:41 +05301437 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001438 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301439 play_w->name, capture_w->name, ret);
1440 return ret;
1441 }
1442 }
1443
1444 play_w = cpu_dai->playback_widget;
1445 capture_w = codec_dai->capture_widget;
1446 if (play_w && capture_w) {
1447 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1448 capture_w, play_w);
1449 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001450 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301451 play_w->name, capture_w->name, ret);
1452 return ret;
1453 }
Mark Brownc74184e2012-04-04 22:12:09 +01001454 }
1455 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001456 }
1457
1458 /* add platform data for AC97 devices */
1459 if (rtd->codec_dai->driver->ac97_control)
1460 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1461
1462 return 0;
1463}
1464
1465#ifdef CONFIG_SND_SOC_AC97_BUS
1466static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1467{
1468 int ret;
1469
1470 /* Only instantiate AC97 if not already done by the adaptor
1471 * for the generic AC97 subsystem.
1472 */
1473 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001474 /*
1475 * It is possible that the AC97 device is already registered to
1476 * the device subsystem. This happens when the device is created
1477 * via snd_ac97_mixer(). Currently only SoC codec that does so
1478 * is the generic AC97 glue but others migh emerge.
1479 *
1480 * In those cases we don't try to register the device again.
1481 */
1482 if (!rtd->codec->ac97_created)
1483 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001484
1485 ret = soc_ac97_dev_register(rtd->codec);
1486 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001487 dev_err(rtd->codec->dev,
1488 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001489 return ret;
1490 }
1491
1492 rtd->codec->ac97_registered = 1;
1493 }
1494 return 0;
1495}
1496
1497static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1498{
1499 if (codec->ac97_registered) {
1500 soc_ac97_dev_unregister(codec);
1501 codec->ac97_registered = 0;
1502 }
1503}
1504#endif
1505
Mark Brownb19e6e72012-03-14 21:18:39 +00001506static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1507{
1508 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1509 struct snd_soc_codec *codec;
1510
1511 /* find CODEC from registered CODECs*/
1512 list_for_each_entry(codec, &codec_list, list) {
1513 if (!strcmp(codec->name, aux_dev->codec_name))
1514 return 0;
1515 }
1516
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001517 dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
Mark Brownfb099cb2012-08-09 18:44:37 +01001518
Mark Brownb19e6e72012-03-14 21:18:39 +00001519 return -EPROBE_DEFER;
1520}
1521
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001522static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1523{
1524 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001525 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001526 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001527
1528 /* find CODEC from registered CODECs*/
1529 list_for_each_entry(codec, &codec_list, list) {
1530 if (!strcmp(codec->name, aux_dev->codec_name)) {
1531 if (codec->probed) {
1532 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001533 "ASoC: codec already probed");
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001534 ret = -EBUSY;
1535 goto out;
1536 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001537 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001538 }
1539 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001540 /* codec not found */
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001541 dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001542 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001543
Jarkko Nikula676ad982010-12-03 09:18:22 +02001544found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001545 ret = soc_probe_codec(card, codec);
1546 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001547 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001548
Jarkko Nikula589c3562010-12-06 16:27:07 +02001549 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001550
1551out:
1552 return ret;
1553}
1554
1555static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1556{
1557 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1558 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001559
1560 /* unregister the rtd device */
1561 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001562 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001563 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001564 rtd->dev_registered = 0;
1565 }
1566
Jarkko Nikula589c3562010-12-06 16:27:07 +02001567 if (codec && codec->probed)
1568 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001569}
1570
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001571static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1572 enum snd_soc_compress_type compress_type)
1573{
1574 int ret;
1575
1576 if (codec->cache_init)
1577 return 0;
1578
1579 /* override the compress_type if necessary */
1580 if (compress_type && codec->compress_type != compress_type)
1581 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001582 ret = snd_soc_cache_init(codec);
1583 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001584 dev_err(codec->dev, "ASoC: Failed to set cache compression"
1585 " type: %d\n", ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001586 return ret;
1587 }
1588 codec->cache_init = 1;
1589 return 0;
1590}
1591
Mark Brownb19e6e72012-03-14 21:18:39 +00001592static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001593{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001594 struct snd_soc_codec *codec;
1595 struct snd_soc_codec_conf *codec_conf;
1596 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001597 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001598 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001599
Liam Girdwood01b9d992012-03-07 10:38:25 +00001600 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001601
Mark Brownb19e6e72012-03-14 21:18:39 +00001602 /* bind DAIs */
1603 for (i = 0; i < card->num_links; i++) {
1604 ret = soc_bind_dai_link(card, i);
1605 if (ret != 0)
1606 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001607 }
1608
Mark Brownb19e6e72012-03-14 21:18:39 +00001609 /* check aux_devs too */
1610 for (i = 0; i < card->num_aux_devs; i++) {
1611 ret = soc_check_aux_dev(card, i);
1612 if (ret != 0)
1613 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001614 }
1615
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001616 /* initialize the register cache for each available codec */
1617 list_for_each_entry(codec, &codec_list, list) {
1618 if (codec->cache_init)
1619 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001620 /* by default we don't override the compress_type */
1621 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001622 /* check to see if we need to override the compress_type */
1623 for (i = 0; i < card->num_configs; ++i) {
1624 codec_conf = &card->codec_conf[i];
1625 if (!strcmp(codec->name, codec_conf->dev_name)) {
1626 compress_type = codec_conf->compress_type;
1627 if (compress_type && compress_type
1628 != codec->compress_type)
1629 break;
1630 }
1631 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001632 ret = snd_soc_init_codec_cache(codec, compress_type);
Mark Brownb19e6e72012-03-14 21:18:39 +00001633 if (ret < 0)
1634 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001635 }
1636
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001637 /* card bind complete so register a sound card */
1638 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1639 card->owner, 0, &card->snd_card);
1640 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001641 dev_err(card->dev, "ASoC: can't create sound card for"
1642 " card %s: %d\n", card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001643 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001644 }
1645 card->snd_card->dev = card->dev;
1646
Mark Browne37a4972011-03-02 18:21:57 +00001647 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1648 card->dapm.dev = card->dev;
1649 card->dapm.card = card;
1650 list_add(&card->dapm.list, &card->dapm_list);
1651
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001652#ifdef CONFIG_DEBUG_FS
1653 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1654#endif
1655
Mark Brown88ee1c62011-02-02 10:43:26 +00001656#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001657 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001658 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001659#endif
Andy Green6ed25972008-06-13 16:24:05 +01001660
Mark Brown9a841eb2011-04-12 17:51:37 -07001661 if (card->dapm_widgets)
1662 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1663 card->num_dapm_widgets);
1664
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001665 /* initialise the sound card only once */
1666 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001667 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001668 if (ret < 0)
1669 goto card_probe_error;
1670 }
1671
Stephen Warren62ae68f2012-06-08 12:34:23 -06001672 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001673 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1674 order++) {
1675 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001676 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001677 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001678 dev_err(card->dev,
1679 "ASoC: failed to instantiate card %d\n",
1680 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001681 goto probe_dai_err;
1682 }
1683 }
1684 }
1685
1686 /* probe all DAI links on this card */
1687 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1688 order++) {
1689 for (i = 0; i < card->num_links; i++) {
1690 ret = soc_probe_link_dais(card, i, order);
1691 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001692 dev_err(card->dev,
1693 "ASoC: failed to instantiate card %d\n",
1694 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001695 goto probe_dai_err;
1696 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001697 }
1698 }
1699
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001700 for (i = 0; i < card->num_aux_devs; i++) {
1701 ret = soc_probe_aux_dev(card, i);
1702 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001703 dev_err(card->dev,
1704 "ASoC: failed to add auxiliary devices %d\n",
1705 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001706 goto probe_aux_dev_err;
1707 }
1708 }
1709
Mark Brown888df392012-02-16 19:37:51 -08001710 snd_soc_dapm_link_dai_widgets(card);
1711
Mark Brownb7af1da2011-04-07 19:18:44 +09001712 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001713 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001714
Mark Brownb8ad29d2011-03-02 18:35:51 +00001715 if (card->dapm_routes)
1716 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1717 card->num_dapm_routes);
1718
Mark Brownb90d2f92011-10-10 13:38:06 +01001719 snd_soc_dapm_new_widgets(&card->dapm);
1720
Mark Brown75d9ac42011-09-27 16:41:01 +01001721 for (i = 0; i < card->num_links; i++) {
1722 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001723 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001724
Mark Brownf04209a2012-04-09 16:29:21 +01001725 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001726 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001727 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001728 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001729 dev_warn(card->rtd[i].codec_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001730 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001731 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001732 }
1733
1734 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001735 if (dai_fmt &&
1736 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001737 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1738 dai_fmt);
1739 if (ret != 0 && ret != -ENOTSUPP)
1740 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001741 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001742 ret);
1743 } else if (dai_fmt) {
1744 /* Flip the polarity for the "CPU" end */
1745 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1746 switch (dai_link->dai_fmt &
1747 SND_SOC_DAIFMT_MASTER_MASK) {
1748 case SND_SOC_DAIFMT_CBM_CFM:
1749 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1750 break;
1751 case SND_SOC_DAIFMT_CBM_CFS:
1752 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1753 break;
1754 case SND_SOC_DAIFMT_CBS_CFM:
1755 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1756 break;
1757 case SND_SOC_DAIFMT_CBS_CFS:
1758 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1759 break;
1760 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001761
1762 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001763 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001764 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001765 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001766 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001767 ret);
1768 }
1769 }
1770
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001771 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001772 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001773 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1774 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001775 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1776 "%s", card->driver_name ? card->driver_name : card->name);
1777 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1778 switch (card->snd_card->driver[i]) {
1779 case '_':
1780 case '-':
1781 case '\0':
1782 break;
1783 default:
1784 if (!isalnum(card->snd_card->driver[i]))
1785 card->snd_card->driver[i] = '_';
1786 break;
1787 }
1788 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001789
Mark Brown28e9ad92011-03-02 18:36:34 +00001790 if (card->late_probe) {
1791 ret = card->late_probe(card);
1792 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001793 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001794 card->name, ret);
1795 goto probe_aux_dev_err;
1796 }
1797 }
1798
Mark Brown2dc00212011-10-08 13:59:44 +01001799 snd_soc_dapm_new_widgets(&card->dapm);
1800
Stephen Warren16332812011-11-23 12:42:04 -07001801 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001802 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001803 snd_soc_dapm_auto_nc_codec_pins(codec);
1804
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001805 ret = snd_card_register(card->snd_card);
1806 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001807 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1808 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001809 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001810 }
1811
1812#ifdef CONFIG_SND_SOC_AC97_BUS
1813 /* register any AC97 codecs */
1814 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001815 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1816 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001817 dev_err(card->dev, "ASoC: failed to register AC97:"
1818 " %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001819 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001820 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001821 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001822 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001823 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001824#endif
1825
Mark Brown435c5e22008-12-04 15:32:53 +00001826 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001827 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001828 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001829
1830 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001831
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001832probe_aux_dev_err:
1833 for (i = 0; i < card->num_aux_devs; i++)
1834 soc_remove_aux_dev(card, i);
1835
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001836probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001837 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001838
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001839card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001840 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001841 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001842
1843 snd_card_free(card->snd_card);
1844
Mark Brownb19e6e72012-03-14 21:18:39 +00001845base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001846 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001847
Mark Brownb19e6e72012-03-14 21:18:39 +00001848 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001849}
1850
1851/* probes a new socdev */
1852static int soc_probe(struct platform_device *pdev)
1853{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001854 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001855
Vinod Koul70a7ca32011-01-14 19:22:48 +05301856 /*
1857 * no card, so machine driver should be registering card
1858 * we should not be here in that case so ret error
1859 */
1860 if (!card)
1861 return -EINVAL;
1862
Mark Brownfe4085e2012-03-02 13:07:41 +00001863 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001864 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001865 card->name);
1866
Mark Brown435c5e22008-12-04 15:32:53 +00001867 /* Bodge while we unpick instantiation */
1868 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001869
Mark Brown28d528c2012-08-09 18:45:23 +01001870 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001871}
1872
Vinod Koulb0e26482011-01-13 22:48:02 +05301873static int soc_cleanup_card_resources(struct snd_soc_card *card)
1874{
Vinod Koulb0e26482011-01-13 22:48:02 +05301875 int i;
1876
1877 /* make sure any delayed work runs */
1878 for (i = 0; i < card->num_rtd; i++) {
1879 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001880 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301881 }
1882
1883 /* remove auxiliary devices */
1884 for (i = 0; i < card->num_aux_devs; i++)
1885 soc_remove_aux_dev(card, i);
1886
1887 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001888 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301889
1890 soc_cleanup_card_debugfs(card);
1891
1892 /* remove the card */
1893 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001894 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301895
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001896 snd_soc_dapm_free(&card->dapm);
1897
Vinod Koulb0e26482011-01-13 22:48:02 +05301898 snd_card_free(card->snd_card);
1899 return 0;
1900
1901}
1902
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001903/* removes a socdev */
1904static int soc_remove(struct platform_device *pdev)
1905{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001906 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001907
Mark Brownc5af3a22008-11-28 13:29:45 +00001908 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001909 return 0;
1910}
1911
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001912int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001913{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001914 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001915 int i;
Mark Brown51737472009-06-22 13:16:51 +01001916
1917 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001918 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001919
1920 /* Flush out pmdown_time work - we actually do want to run it
1921 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001922 for (i = 0; i < card->num_rtd; i++) {
1923 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001924 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001925 }
Mark Brown51737472009-06-22 13:16:51 +01001926
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001927 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001928
1929 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001930}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001931EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001932
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001933const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301934 .suspend = snd_soc_suspend,
1935 .resume = snd_soc_resume,
1936 .freeze = snd_soc_suspend,
1937 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001938 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301939 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001940};
Stephen Warrendeb26072011-04-05 19:35:30 -06001941EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001942
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001943/* ASoC platform driver */
1944static struct platform_driver soc_driver = {
1945 .driver = {
1946 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001947 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001948 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001949 },
1950 .probe = soc_probe,
1951 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001952};
1953
Mark Brown096e49d2009-07-05 15:12:22 +01001954/**
1955 * snd_soc_codec_volatile_register: Report if a register is volatile.
1956 *
1957 * @codec: CODEC to query.
1958 * @reg: Register to query.
1959 *
1960 * Boolean function indiciating if a CODEC register is volatile.
1961 */
Mark Brown181e0552011-01-24 14:05:25 +00001962int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1963 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001964{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001965 if (codec->volatile_register)
1966 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001967 else
1968 return 0;
1969}
1970EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1971
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001972/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001973 * snd_soc_codec_readable_register: Report if a register is readable.
1974 *
1975 * @codec: CODEC to query.
1976 * @reg: Register to query.
1977 *
1978 * Boolean function indicating if a CODEC register is readable.
1979 */
1980int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1981 unsigned int reg)
1982{
1983 if (codec->readable_register)
1984 return codec->readable_register(codec, reg);
1985 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001986 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001987}
1988EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1989
1990/**
1991 * snd_soc_codec_writable_register: Report if a register is writable.
1992 *
1993 * @codec: CODEC to query.
1994 * @reg: Register to query.
1995 *
1996 * Boolean function indicating if a CODEC register is writable.
1997 */
1998int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1999 unsigned int reg)
2000{
2001 if (codec->writable_register)
2002 return codec->writable_register(codec, reg);
2003 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02002004 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002005}
2006EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2007
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002008int snd_soc_platform_read(struct snd_soc_platform *platform,
2009 unsigned int reg)
2010{
2011 unsigned int ret;
2012
2013 if (!platform->driver->read) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002014 dev_err(platform->dev, "ASoC: platform has no read back\n");
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002015 return -1;
2016 }
2017
2018 ret = platform->driver->read(platform, reg);
2019 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002020 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002021
2022 return ret;
2023}
2024EXPORT_SYMBOL_GPL(snd_soc_platform_read);
2025
2026int snd_soc_platform_write(struct snd_soc_platform *platform,
2027 unsigned int reg, unsigned int val)
2028{
2029 if (!platform->driver->write) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002030 dev_err(platform->dev, "ASoC: platform has no write back\n");
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002031 return -1;
2032 }
2033
2034 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002035 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002036 return platform->driver->write(platform, reg, val);
2037}
2038EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2039
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002040/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002041 * snd_soc_new_ac97_codec - initailise AC97 device
2042 * @codec: audio codec
2043 * @ops: AC97 bus operations
2044 * @num: AC97 codec number
2045 *
2046 * Initialises AC97 codec resources for use by ad-hoc devices only.
2047 */
2048int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2049 struct snd_ac97_bus_ops *ops, int num)
2050{
2051 mutex_lock(&codec->mutex);
2052
2053 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2054 if (codec->ac97 == NULL) {
2055 mutex_unlock(&codec->mutex);
2056 return -ENOMEM;
2057 }
2058
2059 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2060 if (codec->ac97->bus == NULL) {
2061 kfree(codec->ac97);
2062 codec->ac97 = NULL;
2063 mutex_unlock(&codec->mutex);
2064 return -ENOMEM;
2065 }
2066
2067 codec->ac97->bus->ops = ops;
2068 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002069
2070 /*
2071 * Mark the AC97 device to be created by us. This way we ensure that the
2072 * device will be registered with the device subsystem later on.
2073 */
2074 codec->ac97_created = 1;
2075
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002076 mutex_unlock(&codec->mutex);
2077 return 0;
2078}
2079EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2080
2081/**
2082 * snd_soc_free_ac97_codec - free AC97 codec device
2083 * @codec: audio codec
2084 *
2085 * Frees AC97 codec device resources.
2086 */
2087void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2088{
2089 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002090#ifdef CONFIG_SND_SOC_AC97_BUS
2091 soc_unregister_ac97_dai_link(codec);
2092#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002093 kfree(codec->ac97->bus);
2094 kfree(codec->ac97);
2095 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002096 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002097 mutex_unlock(&codec->mutex);
2098}
2099EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2100
Mark Brownc3753702010-11-01 15:41:57 -04002101unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2102{
2103 unsigned int ret;
2104
Mark Brownc3acec22010-12-02 16:15:29 +00002105 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002106 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002107 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002108
2109 return ret;
2110}
2111EXPORT_SYMBOL_GPL(snd_soc_read);
2112
2113unsigned int snd_soc_write(struct snd_soc_codec *codec,
2114 unsigned int reg, unsigned int val)
2115{
2116 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002117 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002118 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002119}
2120EXPORT_SYMBOL_GPL(snd_soc_write);
2121
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00002122unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2123 unsigned int reg, const void *data, size_t len)
2124{
2125 return codec->bulk_write_raw(codec, reg, data, len);
2126}
2127EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2128
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002129/**
2130 * snd_soc_update_bits - update codec register bits
2131 * @codec: audio codec
2132 * @reg: codec register
2133 * @mask: register mask
2134 * @value: new value
2135 *
2136 * Writes new register value.
2137 *
Timur Tabi180c3292011-01-10 15:58:13 -06002138 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002139 */
2140int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002141 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002142{
Mark Brown8a713da2011-12-03 12:33:55 +00002143 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002144 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002145 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002146
Mark Brown8a713da2011-12-03 12:33:55 +00002147 if (codec->using_regmap) {
2148 ret = regmap_update_bits_check(codec->control_data, reg,
2149 mask, value, &change);
2150 } else {
2151 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06002152 if (ret < 0)
2153 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00002154
2155 old = ret;
2156 new = (old & ~mask) | (value & mask);
2157 change = old != new;
2158 if (change)
2159 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06002160 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002161
Mark Brown8a713da2011-12-03 12:33:55 +00002162 if (ret < 0)
2163 return ret;
2164
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002165 return change;
2166}
2167EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2168
2169/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002170 * snd_soc_update_bits_locked - update codec register bits
2171 * @codec: audio codec
2172 * @reg: codec register
2173 * @mask: register mask
2174 * @value: new value
2175 *
2176 * Writes new register value, and takes the codec mutex.
2177 *
2178 * Returns 1 for change else 0.
2179 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002180int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2181 unsigned short reg, unsigned int mask,
2182 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002183{
2184 int change;
2185
2186 mutex_lock(&codec->mutex);
2187 change = snd_soc_update_bits(codec, reg, mask, value);
2188 mutex_unlock(&codec->mutex);
2189
2190 return change;
2191}
Mark Browndd1b3d52009-12-04 14:22:03 +00002192EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002193
2194/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002195 * snd_soc_test_bits - test register for change
2196 * @codec: audio codec
2197 * @reg: codec register
2198 * @mask: register mask
2199 * @value: new value
2200 *
2201 * Tests a register with a new value and checks if the new value is
2202 * different from the old value.
2203 *
2204 * Returns 1 for change else 0.
2205 */
2206int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002207 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002208{
2209 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002210 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002211
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002212 old = snd_soc_read(codec, reg);
2213 new = (old & ~mask) | value;
2214 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002215
2216 return change;
2217}
2218EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2219
2220/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002221 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2222 * @substream: the pcm substream
2223 * @hw: the hardware parameters
2224 *
2225 * Sets the substream runtime hardware parameters.
2226 */
2227int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2228 const struct snd_pcm_hardware *hw)
2229{
2230 struct snd_pcm_runtime *runtime = substream->runtime;
2231 runtime->hw.info = hw->info;
2232 runtime->hw.formats = hw->formats;
2233 runtime->hw.period_bytes_min = hw->period_bytes_min;
2234 runtime->hw.period_bytes_max = hw->period_bytes_max;
2235 runtime->hw.periods_min = hw->periods_min;
2236 runtime->hw.periods_max = hw->periods_max;
2237 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2238 runtime->hw.fifo_size = hw->fifo_size;
2239 return 0;
2240}
2241EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2242
2243/**
2244 * snd_soc_cnew - create new control
2245 * @_template: control template
2246 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002247 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002248 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002249 *
2250 * Create a new mixer control from a template control.
2251 *
2252 * Returns 0 for success, else error.
2253 */
2254struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002255 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002256 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002257{
2258 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002259 struct snd_kcontrol *kcontrol;
2260 char *name = NULL;
2261 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002262
2263 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002264 template.index = 0;
2265
Mark Brownefb7ac32011-03-08 17:23:24 +00002266 if (!long_name)
2267 long_name = template.name;
2268
2269 if (prefix) {
2270 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08002271 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00002272 if (!name)
2273 return NULL;
2274
2275 snprintf(name, name_len, "%s %s", prefix, long_name);
2276
2277 template.name = name;
2278 } else {
2279 template.name = long_name;
2280 }
2281
2282 kcontrol = snd_ctl_new1(&template, data);
2283
2284 kfree(name);
2285
2286 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002287}
2288EXPORT_SYMBOL_GPL(snd_soc_cnew);
2289
Liam Girdwood022658b2012-02-03 17:43:09 +00002290static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2291 const struct snd_kcontrol_new *controls, int num_controls,
2292 const char *prefix, void *data)
2293{
2294 int err, i;
2295
2296 for (i = 0; i < num_controls; i++) {
2297 const struct snd_kcontrol_new *control = &controls[i];
2298 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2299 control->name, prefix));
2300 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002301 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2302 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002303 return err;
2304 }
2305 }
2306
2307 return 0;
2308}
2309
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002310/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002311 * snd_soc_add_codec_controls - add an array of controls to a codec.
2312 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002313 * duplicating this code.
2314 *
2315 * @codec: codec to add controls to
2316 * @controls: array of controls to add
2317 * @num_controls: number of elements in the array
2318 *
2319 * Return 0 for success, else error.
2320 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002321int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002322 const struct snd_kcontrol_new *controls, int num_controls)
2323{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002324 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002325
Liam Girdwood022658b2012-02-03 17:43:09 +00002326 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2327 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002328}
Liam Girdwood022658b2012-02-03 17:43:09 +00002329EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002330
2331/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002332 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002333 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002334 *
2335 * @platform: platform to add controls to
2336 * @controls: array of controls to add
2337 * @num_controls: number of elements in the array
2338 *
2339 * Return 0 for success, else error.
2340 */
2341int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2342 const struct snd_kcontrol_new *controls, int num_controls)
2343{
2344 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002345
Liam Girdwood022658b2012-02-03 17:43:09 +00002346 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2347 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002348}
2349EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2350
2351/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002352 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2353 * Convenience function to add a list of controls.
2354 *
2355 * @soc_card: SoC card to add controls to
2356 * @controls: array of controls to add
2357 * @num_controls: number of elements in the array
2358 *
2359 * Return 0 for success, else error.
2360 */
2361int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2362 const struct snd_kcontrol_new *controls, int num_controls)
2363{
2364 struct snd_card *card = soc_card->snd_card;
2365
2366 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2367 NULL, soc_card);
2368}
2369EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2370
2371/**
2372 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2373 * Convienience function to add a list of controls.
2374 *
2375 * @dai: DAI to add controls to
2376 * @controls: array of controls to add
2377 * @num_controls: number of elements in the array
2378 *
2379 * Return 0 for success, else error.
2380 */
2381int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2382 const struct snd_kcontrol_new *controls, int num_controls)
2383{
2384 struct snd_card *card = dai->card->snd_card;
2385
2386 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2387 NULL, dai);
2388}
2389EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2390
2391/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002392 * snd_soc_info_enum_double - enumerated double mixer info callback
2393 * @kcontrol: mixer control
2394 * @uinfo: control element information
2395 *
2396 * Callback to provide information about a double enumerated
2397 * mixer control.
2398 *
2399 * Returns 0 for success.
2400 */
2401int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2402 struct snd_ctl_elem_info *uinfo)
2403{
2404 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2405
2406 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2407 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002408 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002409
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002410 if (uinfo->value.enumerated.item > e->max - 1)
2411 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002412 strcpy(uinfo->value.enumerated.name,
2413 e->texts[uinfo->value.enumerated.item]);
2414 return 0;
2415}
2416EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2417
2418/**
2419 * snd_soc_get_enum_double - enumerated double mixer get callback
2420 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002421 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002422 *
2423 * Callback to get the value of a double enumerated mixer.
2424 *
2425 * Returns 0 for success.
2426 */
2427int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2428 struct snd_ctl_elem_value *ucontrol)
2429{
2430 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2431 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002432 unsigned int val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002433
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002434 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002435 ucontrol->value.enumerated.item[0]
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002436 = (val >> e->shift_l) & e->mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002437 if (e->shift_l != e->shift_r)
2438 ucontrol->value.enumerated.item[1] =
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002439 (val >> e->shift_r) & e->mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002440
2441 return 0;
2442}
2443EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2444
2445/**
2446 * snd_soc_put_enum_double - enumerated double mixer put callback
2447 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002448 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002449 *
2450 * Callback to set the value of a double enumerated mixer.
2451 *
2452 * Returns 0 for success.
2453 */
2454int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2455 struct snd_ctl_elem_value *ucontrol)
2456{
2457 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2458 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002459 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002460 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002461
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002462 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002463 return -EINVAL;
2464 val = ucontrol->value.enumerated.item[0] << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002465 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002466 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002467 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002468 return -EINVAL;
2469 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002470 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002471 }
2472
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002473 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002474}
2475EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2476
2477/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002478 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2479 * @kcontrol: mixer control
2480 * @ucontrol: control element information
2481 *
2482 * Callback to get the value of a double semi enumerated mixer.
2483 *
2484 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2485 * used for handling bitfield coded enumeration for example.
2486 *
2487 * Returns 0 for success.
2488 */
2489int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2490 struct snd_ctl_elem_value *ucontrol)
2491{
2492 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002493 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002494 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002495
2496 reg_val = snd_soc_read(codec, e->reg);
2497 val = (reg_val >> e->shift_l) & e->mask;
2498 for (mux = 0; mux < e->max; mux++) {
2499 if (val == e->values[mux])
2500 break;
2501 }
2502 ucontrol->value.enumerated.item[0] = mux;
2503 if (e->shift_l != e->shift_r) {
2504 val = (reg_val >> e->shift_r) & e->mask;
2505 for (mux = 0; mux < e->max; mux++) {
2506 if (val == e->values[mux])
2507 break;
2508 }
2509 ucontrol->value.enumerated.item[1] = mux;
2510 }
2511
2512 return 0;
2513}
2514EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2515
2516/**
2517 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2518 * @kcontrol: mixer control
2519 * @ucontrol: control element information
2520 *
2521 * Callback to set the value of a double semi enumerated mixer.
2522 *
2523 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2524 * used for handling bitfield coded enumeration for example.
2525 *
2526 * Returns 0 for success.
2527 */
2528int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2529 struct snd_ctl_elem_value *ucontrol)
2530{
2531 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002532 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002533 unsigned int val;
2534 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002535
2536 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2537 return -EINVAL;
2538 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2539 mask = e->mask << e->shift_l;
2540 if (e->shift_l != e->shift_r) {
2541 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2542 return -EINVAL;
2543 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2544 mask |= e->mask << e->shift_r;
2545 }
2546
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002547 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002548}
2549EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2550
2551/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002552 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2553 * @kcontrol: mixer control
2554 * @uinfo: control element information
2555 *
2556 * Callback to provide information about an external enumerated
2557 * single mixer.
2558 *
2559 * Returns 0 for success.
2560 */
2561int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2562 struct snd_ctl_elem_info *uinfo)
2563{
2564 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2565
2566 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2567 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002568 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002569
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002570 if (uinfo->value.enumerated.item > e->max - 1)
2571 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002572 strcpy(uinfo->value.enumerated.name,
2573 e->texts[uinfo->value.enumerated.item]);
2574 return 0;
2575}
2576EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2577
2578/**
2579 * snd_soc_info_volsw_ext - external single mixer info callback
2580 * @kcontrol: mixer control
2581 * @uinfo: control element information
2582 *
2583 * Callback to provide information about a single external mixer control.
2584 *
2585 * Returns 0 for success.
2586 */
2587int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2588 struct snd_ctl_elem_info *uinfo)
2589{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002590 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002591
Mark Brownfd5dfad2009-04-15 21:37:46 +01002592 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002593 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2594 else
2595 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2596
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002597 uinfo->count = 1;
2598 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002599 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002600 return 0;
2601}
2602EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2603
2604/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002605 * snd_soc_info_volsw - single mixer info callback
2606 * @kcontrol: mixer control
2607 * @uinfo: control element information
2608 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002609 * Callback to provide information about a single mixer control, or a double
2610 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002611 *
2612 * Returns 0 for success.
2613 */
2614int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2615 struct snd_ctl_elem_info *uinfo)
2616{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002617 struct soc_mixer_control *mc =
2618 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002619 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002620
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002621 if (!mc->platform_max)
2622 mc->platform_max = mc->max;
2623 platform_max = mc->platform_max;
2624
2625 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002626 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2627 else
2628 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2629
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002630 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002631 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002632 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002633 return 0;
2634}
2635EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2636
2637/**
2638 * snd_soc_get_volsw - single mixer get callback
2639 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002640 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002641 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002642 * Callback to get the value of a single mixer control, or a double mixer
2643 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002644 *
2645 * Returns 0 for success.
2646 */
2647int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2648 struct snd_ctl_elem_value *ucontrol)
2649{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002650 struct soc_mixer_control *mc =
2651 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002652 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002653 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002654 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002655 unsigned int shift = mc->shift;
2656 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002657 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002658 unsigned int mask = (1 << fls(max)) - 1;
2659 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002660
2661 ucontrol->value.integer.value[0] =
2662 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002663 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002664 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002665 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002666
2667 if (snd_soc_volsw_is_stereo(mc)) {
2668 if (reg == reg2)
2669 ucontrol->value.integer.value[1] =
2670 (snd_soc_read(codec, reg) >> rshift) & mask;
2671 else
2672 ucontrol->value.integer.value[1] =
2673 (snd_soc_read(codec, reg2) >> shift) & mask;
2674 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002675 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002676 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002677 }
2678
2679 return 0;
2680}
2681EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2682
2683/**
2684 * snd_soc_put_volsw - single mixer put callback
2685 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002686 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002687 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002688 * Callback to set the value of a single mixer control, or a double mixer
2689 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002690 *
2691 * Returns 0 for success.
2692 */
2693int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2694 struct snd_ctl_elem_value *ucontrol)
2695{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002696 struct soc_mixer_control *mc =
2697 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002698 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002699 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002700 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002701 unsigned int shift = mc->shift;
2702 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002703 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002704 unsigned int mask = (1 << fls(max)) - 1;
2705 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002706 int err;
2707 bool type_2r = 0;
2708 unsigned int val2 = 0;
2709 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002710
2711 val = (ucontrol->value.integer.value[0] & mask);
2712 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002713 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002714 val_mask = mask << shift;
2715 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002716 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002717 val2 = (ucontrol->value.integer.value[1] & mask);
2718 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002719 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002720 if (reg == reg2) {
2721 val_mask |= mask << rshift;
2722 val |= val2 << rshift;
2723 } else {
2724 val2 = val2 << shift;
2725 type_2r = 1;
2726 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002727 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002728 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002729 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002730 return err;
2731
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002732 if (type_2r)
2733 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2734
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002735 return err;
2736}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002737EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002738
Mark Browne13ac2e2008-05-28 17:58:05 +01002739/**
Brian Austin1d99f242012-03-30 10:43:55 -05002740 * snd_soc_get_volsw_sx - single mixer get callback
2741 * @kcontrol: mixer control
2742 * @ucontrol: control element information
2743 *
2744 * Callback to get the value of a single mixer control, or a double mixer
2745 * control that spans 2 registers.
2746 *
2747 * Returns 0 for success.
2748 */
2749int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2750 struct snd_ctl_elem_value *ucontrol)
2751{
2752 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2753 struct soc_mixer_control *mc =
2754 (struct soc_mixer_control *)kcontrol->private_value;
2755
2756 unsigned int reg = mc->reg;
2757 unsigned int reg2 = mc->rreg;
2758 unsigned int shift = mc->shift;
2759 unsigned int rshift = mc->rshift;
2760 int max = mc->max;
2761 int min = mc->min;
2762 int mask = (1 << (fls(min + max) - 1)) - 1;
2763
2764 ucontrol->value.integer.value[0] =
2765 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2766
2767 if (snd_soc_volsw_is_stereo(mc))
2768 ucontrol->value.integer.value[1] =
2769 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2770
2771 return 0;
2772}
2773EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2774
2775/**
2776 * snd_soc_put_volsw_sx - double mixer set callback
2777 * @kcontrol: mixer control
2778 * @uinfo: control element information
2779 *
2780 * Callback to set the value of a double mixer control that spans 2 registers.
2781 *
2782 * Returns 0 for success.
2783 */
2784int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2785 struct snd_ctl_elem_value *ucontrol)
2786{
2787 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2788 struct soc_mixer_control *mc =
2789 (struct soc_mixer_control *)kcontrol->private_value;
2790
2791 unsigned int reg = mc->reg;
2792 unsigned int reg2 = mc->rreg;
2793 unsigned int shift = mc->shift;
2794 unsigned int rshift = mc->rshift;
2795 int max = mc->max;
2796 int min = mc->min;
2797 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002798 int err = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002799 unsigned short val, val_mask, val2 = 0;
2800
2801 val_mask = mask << shift;
2802 val = (ucontrol->value.integer.value[0] + min) & mask;
2803 val = val << shift;
2804
Mukund Navadad0558522012-11-09 11:53:40 +05302805 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2806 if (err < 0)
2807 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002808
2809 if (snd_soc_volsw_is_stereo(mc)) {
2810 val_mask = mask << rshift;
2811 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2812 val2 = val2 << rshift;
2813
2814 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2815 return err;
2816 }
2817 return 0;
2818}
2819EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2820
2821/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002822 * snd_soc_info_volsw_s8 - signed mixer info callback
2823 * @kcontrol: mixer control
2824 * @uinfo: control element information
2825 *
2826 * Callback to provide information about a signed mixer control.
2827 *
2828 * Returns 0 for success.
2829 */
2830int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2831 struct snd_ctl_elem_info *uinfo)
2832{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002833 struct soc_mixer_control *mc =
2834 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002835 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002836 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002837
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002838 if (!mc->platform_max)
2839 mc->platform_max = mc->max;
2840 platform_max = mc->platform_max;
2841
Mark Browne13ac2e2008-05-28 17:58:05 +01002842 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2843 uinfo->count = 2;
2844 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002845 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002846 return 0;
2847}
2848EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2849
2850/**
2851 * snd_soc_get_volsw_s8 - signed mixer get callback
2852 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002853 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002854 *
2855 * Callback to get the value of a signed mixer control.
2856 *
2857 * Returns 0 for success.
2858 */
2859int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2860 struct snd_ctl_elem_value *ucontrol)
2861{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002862 struct soc_mixer_control *mc =
2863 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002864 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002865 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002866 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002867 int val = snd_soc_read(codec, reg);
2868
2869 ucontrol->value.integer.value[0] =
2870 ((signed char)(val & 0xff))-min;
2871 ucontrol->value.integer.value[1] =
2872 ((signed char)((val >> 8) & 0xff))-min;
2873 return 0;
2874}
2875EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2876
2877/**
2878 * snd_soc_put_volsw_sgn - signed mixer put callback
2879 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002880 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002881 *
2882 * Callback to set the value of a signed mixer control.
2883 *
2884 * Returns 0 for success.
2885 */
2886int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2887 struct snd_ctl_elem_value *ucontrol)
2888{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002889 struct soc_mixer_control *mc =
2890 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002891 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002892 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002893 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002894 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002895
2896 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2897 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2898
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002899 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002900}
2901EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2902
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002903/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002904 * snd_soc_info_volsw_range - single mixer info callback with range.
2905 * @kcontrol: mixer control
2906 * @uinfo: control element information
2907 *
2908 * Callback to provide information, within a range, about a single
2909 * mixer control.
2910 *
2911 * returns 0 for success.
2912 */
2913int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2914 struct snd_ctl_elem_info *uinfo)
2915{
2916 struct soc_mixer_control *mc =
2917 (struct soc_mixer_control *)kcontrol->private_value;
2918 int platform_max;
2919 int min = mc->min;
2920
2921 if (!mc->platform_max)
2922 mc->platform_max = mc->max;
2923 platform_max = mc->platform_max;
2924
2925 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002926 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002927 uinfo->value.integer.min = 0;
2928 uinfo->value.integer.max = platform_max - min;
2929
2930 return 0;
2931}
2932EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2933
2934/**
2935 * snd_soc_put_volsw_range - single mixer put value callback with range.
2936 * @kcontrol: mixer control
2937 * @ucontrol: control element information
2938 *
2939 * Callback to set the value, within a range, for a single mixer control.
2940 *
2941 * Returns 0 for success.
2942 */
2943int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2944 struct snd_ctl_elem_value *ucontrol)
2945{
2946 struct soc_mixer_control *mc =
2947 (struct soc_mixer_control *)kcontrol->private_value;
2948 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2949 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002950 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002951 unsigned int shift = mc->shift;
2952 int min = mc->min;
2953 int max = mc->max;
2954 unsigned int mask = (1 << fls(max)) - 1;
2955 unsigned int invert = mc->invert;
2956 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002957 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002958
2959 val = ((ucontrol->value.integer.value[0] + min) & mask);
2960 if (invert)
2961 val = max - val;
2962 val_mask = mask << shift;
2963 val = val << shift;
2964
Mark Brown9bde4f02012-12-19 16:05:00 +00002965 ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2966 if (ret != 0)
2967 return ret;
2968
2969 if (snd_soc_volsw_is_stereo(mc)) {
2970 val = ((ucontrol->value.integer.value[1] + min) & mask);
2971 if (invert)
2972 val = max - val;
2973 val_mask = mask << shift;
2974 val = val << shift;
2975
2976 ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
2977 }
2978
2979 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002980}
2981EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2982
2983/**
2984 * snd_soc_get_volsw_range - single mixer get callback with range
2985 * @kcontrol: mixer control
2986 * @ucontrol: control element information
2987 *
2988 * Callback to get the value, within a range, of a single mixer control.
2989 *
2990 * Returns 0 for success.
2991 */
2992int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2993 struct snd_ctl_elem_value *ucontrol)
2994{
2995 struct soc_mixer_control *mc =
2996 (struct soc_mixer_control *)kcontrol->private_value;
2997 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2998 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002999 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003000 unsigned int shift = mc->shift;
3001 int min = mc->min;
3002 int max = mc->max;
3003 unsigned int mask = (1 << fls(max)) - 1;
3004 unsigned int invert = mc->invert;
3005
3006 ucontrol->value.integer.value[0] =
3007 (snd_soc_read(codec, reg) >> shift) & mask;
3008 if (invert)
3009 ucontrol->value.integer.value[0] =
3010 max - ucontrol->value.integer.value[0];
3011 ucontrol->value.integer.value[0] =
3012 ucontrol->value.integer.value[0] - min;
3013
Mark Brown9bde4f02012-12-19 16:05:00 +00003014 if (snd_soc_volsw_is_stereo(mc)) {
3015 ucontrol->value.integer.value[1] =
3016 (snd_soc_read(codec, rreg) >> shift) & mask;
3017 if (invert)
3018 ucontrol->value.integer.value[1] =
3019 max - ucontrol->value.integer.value[1];
3020 ucontrol->value.integer.value[1] =
3021 ucontrol->value.integer.value[1] - min;
3022 }
3023
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003024 return 0;
3025}
3026EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3027
3028/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003029 * snd_soc_limit_volume - Set new limit to an existing volume control.
3030 *
3031 * @codec: where to look for the control
3032 * @name: Name of the control
3033 * @max: new maximum limit
3034 *
3035 * Return 0 for success, else error.
3036 */
3037int snd_soc_limit_volume(struct snd_soc_codec *codec,
3038 const char *name, int max)
3039{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003040 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003041 struct snd_kcontrol *kctl;
3042 struct soc_mixer_control *mc;
3043 int found = 0;
3044 int ret = -EINVAL;
3045
3046 /* Sanity check for name and max */
3047 if (unlikely(!name || max <= 0))
3048 return -EINVAL;
3049
3050 list_for_each_entry(kctl, &card->controls, list) {
3051 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3052 found = 1;
3053 break;
3054 }
3055 }
3056 if (found) {
3057 mc = (struct soc_mixer_control *)kctl->private_value;
3058 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003059 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003060 ret = 0;
3061 }
3062 }
3063 return ret;
3064}
3065EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3066
Mark Brown71d08512011-10-10 18:31:26 +01003067int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3068 struct snd_ctl_elem_info *uinfo)
3069{
3070 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3071 struct soc_bytes *params = (void *)kcontrol->private_value;
3072
3073 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3074 uinfo->count = params->num_regs * codec->val_bytes;
3075
3076 return 0;
3077}
3078EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3079
3080int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3081 struct snd_ctl_elem_value *ucontrol)
3082{
3083 struct soc_bytes *params = (void *)kcontrol->private_value;
3084 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3085 int ret;
3086
3087 if (codec->using_regmap)
3088 ret = regmap_raw_read(codec->control_data, params->base,
3089 ucontrol->value.bytes.data,
3090 params->num_regs * codec->val_bytes);
3091 else
3092 ret = -EINVAL;
3093
Mark Brownf831b052012-02-17 16:20:33 -08003094 /* Hide any masked bytes to ensure consistent data reporting */
3095 if (ret == 0 && params->mask) {
3096 switch (codec->val_bytes) {
3097 case 1:
3098 ucontrol->value.bytes.data[0] &= ~params->mask;
3099 break;
3100 case 2:
3101 ((u16 *)(&ucontrol->value.bytes.data))[0]
3102 &= ~params->mask;
3103 break;
3104 case 4:
3105 ((u32 *)(&ucontrol->value.bytes.data))[0]
3106 &= ~params->mask;
3107 break;
3108 default:
3109 return -EINVAL;
3110 }
3111 }
3112
Mark Brown71d08512011-10-10 18:31:26 +01003113 return ret;
3114}
3115EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3116
3117int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3118 struct snd_ctl_elem_value *ucontrol)
3119{
3120 struct soc_bytes *params = (void *)kcontrol->private_value;
3121 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08003122 int ret, len;
3123 unsigned int val;
3124 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003125
Mark Brownf831b052012-02-17 16:20:33 -08003126 if (!codec->using_regmap)
3127 return -EINVAL;
3128
Mark Brownf831b052012-02-17 16:20:33 -08003129 len = params->num_regs * codec->val_bytes;
3130
Mark Brownb5a8fe42013-01-20 21:42:22 +09003131 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3132 if (!data)
3133 return -ENOMEM;
3134
Mark Brownf831b052012-02-17 16:20:33 -08003135 /*
3136 * If we've got a mask then we need to preserve the register
3137 * bits. We shouldn't modify the incoming data so take a
3138 * copy.
3139 */
3140 if (params->mask) {
3141 ret = regmap_read(codec->control_data, params->base, &val);
3142 if (ret != 0)
3143 return ret;
3144
3145 val &= params->mask;
3146
Mark Brownf831b052012-02-17 16:20:33 -08003147 switch (codec->val_bytes) {
3148 case 1:
3149 ((u8 *)data)[0] &= ~params->mask;
3150 ((u8 *)data)[0] |= val;
3151 break;
3152 case 2:
3153 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3154 ((u16 *)data)[0] |= cpu_to_be16(val);
3155 break;
3156 case 4:
3157 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3158 ((u32 *)data)[0] |= cpu_to_be32(val);
3159 break;
3160 default:
3161 return -EINVAL;
3162 }
3163 }
3164
3165 ret = regmap_raw_write(codec->control_data, params->base,
3166 data, len);
3167
Mark Brownb5a8fe42013-01-20 21:42:22 +09003168 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003169
3170 return ret;
3171}
3172EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3173
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003174/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003175 * snd_soc_info_xr_sx - signed multi register info callback
3176 * @kcontrol: mreg control
3177 * @uinfo: control element information
3178 *
3179 * Callback to provide information of a control that can
3180 * span multiple codec registers which together
3181 * forms a single signed value in a MSB/LSB manner.
3182 *
3183 * Returns 0 for success.
3184 */
3185int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3186 struct snd_ctl_elem_info *uinfo)
3187{
3188 struct soc_mreg_control *mc =
3189 (struct soc_mreg_control *)kcontrol->private_value;
3190 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3191 uinfo->count = 1;
3192 uinfo->value.integer.min = mc->min;
3193 uinfo->value.integer.max = mc->max;
3194
3195 return 0;
3196}
3197EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3198
3199/**
3200 * snd_soc_get_xr_sx - signed multi register get callback
3201 * @kcontrol: mreg control
3202 * @ucontrol: control element information
3203 *
3204 * Callback to get the value of a control that can span
3205 * multiple codec registers which together forms a single
3206 * signed value in a MSB/LSB manner. The control supports
3207 * specifying total no of bits used to allow for bitfields
3208 * across the multiple codec registers.
3209 *
3210 * Returns 0 for success.
3211 */
3212int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3213 struct snd_ctl_elem_value *ucontrol)
3214{
3215 struct soc_mreg_control *mc =
3216 (struct soc_mreg_control *)kcontrol->private_value;
3217 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3218 unsigned int regbase = mc->regbase;
3219 unsigned int regcount = mc->regcount;
3220 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3221 unsigned int regwmask = (1<<regwshift)-1;
3222 unsigned int invert = mc->invert;
3223 unsigned long mask = (1UL<<mc->nbits)-1;
3224 long min = mc->min;
3225 long max = mc->max;
3226 long val = 0;
3227 unsigned long regval;
3228 unsigned int i;
3229
3230 for (i = 0; i < regcount; i++) {
3231 regval = snd_soc_read(codec, regbase+i) & regwmask;
3232 val |= regval << (regwshift*(regcount-i-1));
3233 }
3234 val &= mask;
3235 if (min < 0 && val > max)
3236 val |= ~mask;
3237 if (invert)
3238 val = max - val;
3239 ucontrol->value.integer.value[0] = val;
3240
3241 return 0;
3242}
3243EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3244
3245/**
3246 * snd_soc_put_xr_sx - signed multi register get callback
3247 * @kcontrol: mreg control
3248 * @ucontrol: control element information
3249 *
3250 * Callback to set the value of a control that can span
3251 * multiple codec registers which together forms a single
3252 * signed value in a MSB/LSB manner. The control supports
3253 * specifying total no of bits used to allow for bitfields
3254 * across the multiple codec registers.
3255 *
3256 * Returns 0 for success.
3257 */
3258int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3259 struct snd_ctl_elem_value *ucontrol)
3260{
3261 struct soc_mreg_control *mc =
3262 (struct soc_mreg_control *)kcontrol->private_value;
3263 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3264 unsigned int regbase = mc->regbase;
3265 unsigned int regcount = mc->regcount;
3266 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3267 unsigned int regwmask = (1<<regwshift)-1;
3268 unsigned int invert = mc->invert;
3269 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003270 long max = mc->max;
3271 long val = ucontrol->value.integer.value[0];
3272 unsigned int i, regval, regmask;
3273 int err;
3274
3275 if (invert)
3276 val = max - val;
3277 val &= mask;
3278 for (i = 0; i < regcount; i++) {
3279 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3280 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3281 err = snd_soc_update_bits_locked(codec, regbase+i,
3282 regmask, regval);
3283 if (err < 0)
3284 return err;
3285 }
3286
3287 return 0;
3288}
3289EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3290
3291/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003292 * snd_soc_get_strobe - strobe get callback
3293 * @kcontrol: mixer control
3294 * @ucontrol: control element information
3295 *
3296 * Callback get the value of a strobe mixer control.
3297 *
3298 * Returns 0 for success.
3299 */
3300int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3301 struct snd_ctl_elem_value *ucontrol)
3302{
3303 struct soc_mixer_control *mc =
3304 (struct soc_mixer_control *)kcontrol->private_value;
3305 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3306 unsigned int reg = mc->reg;
3307 unsigned int shift = mc->shift;
3308 unsigned int mask = 1 << shift;
3309 unsigned int invert = mc->invert != 0;
3310 unsigned int val = snd_soc_read(codec, reg) & mask;
3311
3312 if (shift != 0 && val != 0)
3313 val = val >> shift;
3314 ucontrol->value.enumerated.item[0] = val ^ invert;
3315
3316 return 0;
3317}
3318EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3319
3320/**
3321 * snd_soc_put_strobe - strobe put callback
3322 * @kcontrol: mixer control
3323 * @ucontrol: control element information
3324 *
3325 * Callback strobe a register bit to high then low (or the inverse)
3326 * in one pass of a single mixer enum control.
3327 *
3328 * Returns 1 for success.
3329 */
3330int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3331 struct snd_ctl_elem_value *ucontrol)
3332{
3333 struct soc_mixer_control *mc =
3334 (struct soc_mixer_control *)kcontrol->private_value;
3335 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3336 unsigned int reg = mc->reg;
3337 unsigned int shift = mc->shift;
3338 unsigned int mask = 1 << shift;
3339 unsigned int invert = mc->invert != 0;
3340 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3341 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3342 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3343 int err;
3344
3345 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3346 if (err < 0)
3347 return err;
3348
3349 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3350 return err;
3351}
3352EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3353
3354/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003355 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3356 * @dai: DAI
3357 * @clk_id: DAI specific clock ID
3358 * @freq: new clock frequency in Hz
3359 * @dir: new clock direction - input/output.
3360 *
3361 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3362 */
3363int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3364 unsigned int freq, int dir)
3365{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003366 if (dai->driver && dai->driver->ops->set_sysclk)
3367 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003368 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003369 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003370 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003371 else
3372 return -EINVAL;
3373}
3374EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3375
3376/**
Mark Brownec4ee522011-03-07 20:58:11 +00003377 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3378 * @codec: CODEC
3379 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003380 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003381 * @freq: new clock frequency in Hz
3382 * @dir: new clock direction - input/output.
3383 *
3384 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3385 */
3386int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003387 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003388{
3389 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003390 return codec->driver->set_sysclk(codec, clk_id, source,
3391 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003392 else
3393 return -EINVAL;
3394}
3395EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3396
3397/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003398 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3399 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003400 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003401 * @div: new clock divisor.
3402 *
3403 * Configures the clock dividers. This is used to derive the best DAI bit and
3404 * frame clocks from the system or master clock. It's best to set the DAI bit
3405 * and frame clocks as low as possible to save system power.
3406 */
3407int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3408 int div_id, int div)
3409{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003410 if (dai->driver && dai->driver->ops->set_clkdiv)
3411 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003412 else
3413 return -EINVAL;
3414}
3415EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3416
3417/**
3418 * snd_soc_dai_set_pll - configure DAI PLL.
3419 * @dai: DAI
3420 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003421 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003422 * @freq_in: PLL input clock frequency in Hz
3423 * @freq_out: requested PLL output clock frequency in Hz
3424 *
3425 * Configures and enables PLL to generate output clock based on input clock.
3426 */
Mark Brown85488032009-09-05 18:52:16 +01003427int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3428 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003429{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003430 if (dai->driver && dai->driver->ops->set_pll)
3431 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003432 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003433 else if (dai->codec && dai->codec->driver->set_pll)
3434 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3435 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003436 else
3437 return -EINVAL;
3438}
3439EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3440
Mark Brownec4ee522011-03-07 20:58:11 +00003441/*
3442 * snd_soc_codec_set_pll - configure codec PLL.
3443 * @codec: CODEC
3444 * @pll_id: DAI specific PLL ID
3445 * @source: DAI specific source for the PLL
3446 * @freq_in: PLL input clock frequency in Hz
3447 * @freq_out: requested PLL output clock frequency in Hz
3448 *
3449 * Configures and enables PLL to generate output clock based on input clock.
3450 */
3451int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3452 unsigned int freq_in, unsigned int freq_out)
3453{
3454 if (codec->driver->set_pll)
3455 return codec->driver->set_pll(codec, pll_id, source,
3456 freq_in, freq_out);
3457 else
3458 return -EINVAL;
3459}
3460EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3461
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003462/**
3463 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3464 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003465 * @fmt: SND_SOC_DAIFMT_ format value.
3466 *
3467 * Configures the DAI hardware format and clocking.
3468 */
3469int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3470{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003471 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003472 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003473 if (dai->driver->ops->set_fmt == NULL)
3474 return -ENOTSUPP;
3475 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003476}
3477EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3478
3479/**
3480 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3481 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003482 * @tx_mask: bitmask representing active TX slots.
3483 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003484 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003485 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003486 *
3487 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3488 * specific.
3489 */
3490int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003491 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003492{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003493 if (dai->driver && dai->driver->ops->set_tdm_slot)
3494 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003495 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003496 else
3497 return -EINVAL;
3498}
3499EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3500
3501/**
Barry Song472df3c2009-09-12 01:16:29 +08003502 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3503 * @dai: DAI
3504 * @tx_num: how many TX channels
3505 * @tx_slot: pointer to an array which imply the TX slot number channel
3506 * 0~num-1 uses
3507 * @rx_num: how many RX channels
3508 * @rx_slot: pointer to an array which imply the RX slot number channel
3509 * 0~num-1 uses
3510 *
3511 * configure the relationship between channel number and TDM slot number.
3512 */
3513int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3514 unsigned int tx_num, unsigned int *tx_slot,
3515 unsigned int rx_num, unsigned int *rx_slot)
3516{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003517 if (dai->driver && dai->driver->ops->set_channel_map)
3518 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003519 rx_num, rx_slot);
3520 else
3521 return -EINVAL;
3522}
3523EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3524
3525/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003526 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3527 * @dai: DAI
3528 * @tristate: tristate enable
3529 *
3530 * Tristates the DAI so that others can use it.
3531 */
3532int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3533{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003534 if (dai->driver && dai->driver->ops->set_tristate)
3535 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003536 else
3537 return -EINVAL;
3538}
3539EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3540
3541/**
3542 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3543 * @dai: DAI
3544 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003545 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003546 *
3547 * Mutes the DAI DAC.
3548 */
Mark Brownda183962013-02-06 15:44:07 +00003549int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3550 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003551{
Mark Brownda183962013-02-06 15:44:07 +00003552 if (!dai->driver)
3553 return -ENOTSUPP;
3554
3555 if (dai->driver->ops->mute_stream)
3556 return dai->driver->ops->mute_stream(dai, mute, direction);
3557 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3558 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003559 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003560 else
Mark Brown04570c62012-04-13 19:16:03 +01003561 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003562}
3563EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3564
Mark Brownc5af3a22008-11-28 13:29:45 +00003565/**
3566 * snd_soc_register_card - Register a card with the ASoC core
3567 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003568 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003569 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003570 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303571int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003572{
Mark Brownb19e6e72012-03-14 21:18:39 +00003573 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003574
Mark Brownc5af3a22008-11-28 13:29:45 +00003575 if (!card->name || !card->dev)
3576 return -EINVAL;
3577
Stephen Warren5a504962011-12-21 10:40:59 -07003578 for (i = 0; i < card->num_links; i++) {
3579 struct snd_soc_dai_link *link = &card->dai_link[i];
3580
3581 /*
3582 * Codec must be specified by 1 of name or OF node,
3583 * not both or neither.
3584 */
3585 if (!!link->codec_name == !!link->codec_of_node) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003586 dev_err(card->dev, "ASoC: Neither/both codec"
3587 " name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003588 return -EINVAL;
3589 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003590 /* Codec DAI name must be specified */
3591 if (!link->codec_dai_name) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003592 dev_err(card->dev, "ASoC: codec_dai_name not"
3593 " set for %s\n", link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003594 return -EINVAL;
3595 }
Stephen Warren5a504962011-12-21 10:40:59 -07003596
3597 /*
3598 * Platform may be specified by either name or OF node, but
3599 * can be left unspecified, and a dummy platform will be used.
3600 */
3601 if (link->platform_name && link->platform_of_node) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003602 dev_err(card->dev, "ASoC: Both platform name/of_node"
3603 " are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003604 return -EINVAL;
3605 }
3606
3607 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003608 * CPU device may be specified by either name or OF node, but
3609 * can be left unspecified, and will be matched based on DAI
3610 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003611 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003612 if (link->cpu_name && link->cpu_of_node) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003613 dev_err(card->dev, "ASoC: Neither/both "
3614 "cpu name/of_node are set for %s\n",link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003615 return -EINVAL;
3616 }
3617 /*
3618 * At least one of CPU DAI name or CPU device name/node must be
3619 * specified
3620 */
3621 if (!link->cpu_dai_name &&
3622 !(link->cpu_name || link->cpu_of_node)) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003623 dev_err(card->dev, "ASoC: Neither cpu_dai_name nor "
3624 "cpu_name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003625 return -EINVAL;
3626 }
3627 }
3628
Mark Browned77cc12011-05-03 18:25:34 +01003629 dev_set_drvdata(card->dev, card);
3630
Stephen Warren111c6412011-01-28 14:26:35 -07003631 snd_soc_initialize_card_lists(card);
3632
Vinod Koul150dd2f2011-01-13 22:48:32 +05303633 soc_init_card_debugfs(card);
3634
Mark Brown181a6892012-03-14 20:18:49 +00003635 card->rtd = devm_kzalloc(card->dev,
3636 sizeof(struct snd_soc_pcm_runtime) *
3637 (card->num_links + card->num_aux_devs),
3638 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003639 if (card->rtd == NULL)
3640 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003641 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003642 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003643
3644 for (i = 0; i < card->num_links; i++)
3645 card->rtd[i].dai_link = &card->dai_link[i];
3646
Mark Brownc5af3a22008-11-28 13:29:45 +00003647 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003648 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003649 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003650 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003651 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003652
Mark Brownb19e6e72012-03-14 21:18:39 +00003653 ret = snd_soc_instantiate_card(card);
3654 if (ret != 0)
3655 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003656
Mark Brownb19e6e72012-03-14 21:18:39 +00003657 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003658}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303659EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003660
3661/**
3662 * snd_soc_unregister_card - Unregister a card with the ASoC core
3663 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003664 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003665 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003666 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303667int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003668{
Vinod Koulb0e26482011-01-13 22:48:02 +05303669 if (card->instantiated)
3670 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003671 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003672
3673 return 0;
3674}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303675EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003676
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003677/*
3678 * Simplify DAI link configuration by removing ".-1" from device names
3679 * and sanitizing names.
3680 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003681static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003682{
3683 char *found, name[NAME_SIZE];
3684 int id1, id2;
3685
3686 if (dev_name(dev) == NULL)
3687 return NULL;
3688
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003689 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003690
3691 /* are we a "%s.%d" name (platform and SPI components) */
3692 found = strstr(name, dev->driver->name);
3693 if (found) {
3694 /* get ID */
3695 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3696
3697 /* discard ID from name if ID == -1 */
3698 if (*id == -1)
3699 found[strlen(dev->driver->name)] = '\0';
3700 }
3701
3702 } else {
3703 /* I2C component devices are named "bus-addr" */
3704 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3705 char tmp[NAME_SIZE];
3706
3707 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003708 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003709
3710 /* sanitize component name for DAI link creation */
3711 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003712 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003713 } else
3714 *id = 0;
3715 }
3716
3717 return kstrdup(name, GFP_KERNEL);
3718}
3719
3720/*
3721 * Simplify DAI link naming for single devices with multiple DAIs by removing
3722 * any ".-1" and using the DAI name (instead of device name).
3723 */
3724static inline char *fmt_multiple_name(struct device *dev,
3725 struct snd_soc_dai_driver *dai_drv)
3726{
3727 if (dai_drv->name == NULL) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003728 dev_err(dev, "ASoC: error - multiple DAI %s registered with"
3729 " no name\n", dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003730 return NULL;
3731 }
3732
3733 return kstrdup(dai_drv->name, GFP_KERNEL);
3734}
3735
Mark Brown91151712008-11-30 23:31:24 +00003736/**
3737 * snd_soc_register_dai - Register a DAI with the ASoC core
3738 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003739 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003740 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003741int snd_soc_register_dai(struct device *dev,
3742 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003743{
Mark Brown054880f2012-04-09 17:29:19 +01003744 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003745 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003746
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003747 dev_dbg(dev, "ASoC: dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003748
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003749 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3750 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003751 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003752
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003753 /* create DAI component name */
3754 dai->name = fmt_single_name(dev, &dai->id);
3755 if (dai->name == NULL) {
3756 kfree(dai);
3757 return -ENOMEM;
3758 }
3759
3760 dai->dev = dev;
3761 dai->driver = dai_drv;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003762 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003763 if (!dai->driver->ops)
3764 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003765
3766 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003767
3768 list_for_each_entry(codec, &codec_list, list) {
3769 if (codec->dev == dev) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003770 dev_dbg(dev, "ASoC: Mapped DAI %s to CODEC %s\n",
Mark Brown054880f2012-04-09 17:29:19 +01003771 dai->name, codec->name);
3772 dai->codec = codec;
3773 break;
3774 }
3775 }
3776
Peter Ujfalusi5f800082012-08-07 10:24:13 +03003777 if (!dai->codec)
3778 dai->dapm.idle_bias_off = 1;
3779
Mark Brown91151712008-11-30 23:31:24 +00003780 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003781
Mark Brown91151712008-11-30 23:31:24 +00003782 mutex_unlock(&client_mutex);
3783
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003784 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003785
3786 return 0;
3787}
3788EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3789
3790/**
3791 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3792 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003793 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003794 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003795void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003796{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003797 struct snd_soc_dai *dai;
3798
3799 list_for_each_entry(dai, &dai_list, list) {
3800 if (dev == dai->dev)
3801 goto found;
3802 }
3803 return;
3804
3805found:
Mark Brown91151712008-11-30 23:31:24 +00003806 mutex_lock(&client_mutex);
3807 list_del(&dai->list);
3808 mutex_unlock(&client_mutex);
3809
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003810 dev_dbg(dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003811 kfree(dai->name);
3812 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003813}
3814EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3815
3816/**
3817 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3818 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003819 * @dai: Array of DAIs to register
3820 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003821 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003822int snd_soc_register_dais(struct device *dev,
3823 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003824{
Mark Brown054880f2012-04-09 17:29:19 +01003825 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003826 struct snd_soc_dai *dai;
3827 int i, ret = 0;
3828
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003829 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003830
3831 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003832
3833 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003834 if (dai == NULL) {
3835 ret = -ENOMEM;
3836 goto err;
3837 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003838
3839 /* create DAI component name */
3840 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3841 if (dai->name == NULL) {
3842 kfree(dai);
3843 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003844 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003845 }
3846
3847 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003848 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003849 if (dai->driver->id)
3850 dai->id = dai->driver->id;
3851 else
3852 dai->id = i;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003853 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003854 if (!dai->driver->ops)
3855 dai->driver->ops = &null_dai_ops;
3856
3857 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003858
3859 list_for_each_entry(codec, &codec_list, list) {
3860 if (codec->dev == dev) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003861 dev_dbg(dev, "ASoC: Mapped DAI %s to "
3862 "CODEC %s\n", dai->name, codec->name);
Mark Brown054880f2012-04-09 17:29:19 +01003863 dai->codec = codec;
3864 break;
3865 }
3866 }
3867
Peter Ujfalusi5f800082012-08-07 10:24:13 +03003868 if (!dai->codec)
3869 dai->dapm.idle_bias_off = 1;
3870
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003871 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003872
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003873 mutex_unlock(&client_mutex);
3874
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003875 dev_dbg(dai->dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003876 }
3877
3878 return 0;
3879
3880err:
3881 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003882 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003883
3884 return ret;
3885}
3886EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3887
3888/**
3889 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3890 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003891 * @dai: Array of DAIs to unregister
3892 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003893 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003894void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003895{
3896 int i;
3897
3898 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003899 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003900}
3901EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3902
Mark Brown12a48a8c2008-12-03 19:40:30 +00003903/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003904 * snd_soc_add_platform - Add a platform to the ASoC core
3905 * @dev: The parent device for the platform
3906 * @platform: The platform to add
3907 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00003908 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003909int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01003910 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003911{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003912 /* create platform component name */
3913 platform->name = fmt_single_name(dev, &platform->id);
3914 if (platform->name == NULL) {
3915 kfree(platform);
3916 return -ENOMEM;
3917 }
3918
3919 platform->dev = dev;
3920 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003921 platform->dapm.dev = dev;
3922 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003923 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00003924 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003925
3926 mutex_lock(&client_mutex);
3927 list_add(&platform->list, &platform_list);
3928 mutex_unlock(&client_mutex);
3929
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003930 dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003931
3932 return 0;
3933}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003934EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3935
3936/**
3937 * snd_soc_register_platform - Register a platform with the ASoC core
3938 *
3939 * @platform: platform to register
3940 */
3941int snd_soc_register_platform(struct device *dev,
3942 const struct snd_soc_platform_driver *platform_drv)
3943{
3944 struct snd_soc_platform *platform;
3945 int ret;
3946
3947 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3948
3949 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3950 if (platform == NULL)
3951 return -ENOMEM;
3952
3953 ret = snd_soc_add_platform(dev, platform, platform_drv);
3954 if (ret)
3955 kfree(platform);
3956
3957 return ret;
3958}
Mark Brown12a48a8c2008-12-03 19:40:30 +00003959EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3960
3961/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003962 * snd_soc_remove_platform - Remove a platform from the ASoC core
3963 * @platform: the platform to remove
3964 */
3965void snd_soc_remove_platform(struct snd_soc_platform *platform)
3966{
3967 mutex_lock(&client_mutex);
3968 list_del(&platform->list);
3969 mutex_unlock(&client_mutex);
3970
3971 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3972 platform->name);
3973 kfree(platform->name);
3974}
3975EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3976
3977struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3978{
3979 struct snd_soc_platform *platform;
3980
3981 list_for_each_entry(platform, &platform_list, list) {
3982 if (dev == platform->dev)
3983 return platform;
3984 }
3985
3986 return NULL;
3987}
3988EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3989
3990/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00003991 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3992 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003993 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003994 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003995void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003996{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003997 struct snd_soc_platform *platform;
3998
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003999 platform = snd_soc_lookup_platform(dev);
4000 if (!platform)
4001 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004002
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004003 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004004 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004005}
4006EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4007
Mark Brown151ab222009-05-09 16:22:58 +01004008static u64 codec_format_map[] = {
4009 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4010 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4011 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4012 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4013 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4014 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4015 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4016 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4017 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4018 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4019 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4020 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4021 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4022 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4023 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4024 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4025};
4026
4027/* Fix up the DAI formats for endianness: codecs don't actually see
4028 * the endianness of the data but we're using the CPU format
4029 * definitions which do need to include endianness so we ensure that
4030 * codec DAIs always have both big and little endian variants set.
4031 */
4032static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4033{
4034 int i;
4035
4036 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4037 if (stream->formats & codec_format_map[i])
4038 stream->formats |= codec_format_map[i];
4039}
4040
Mark Brown0d0cf002008-12-10 14:32:45 +00004041/**
4042 * snd_soc_register_codec - Register a codec with the ASoC core
4043 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004044 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004045 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004046int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004047 const struct snd_soc_codec_driver *codec_drv,
4048 struct snd_soc_dai_driver *dai_drv,
4049 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004050{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004051 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004052 struct snd_soc_codec *codec;
4053 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004054
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004055 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004056
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004057 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4058 if (codec == NULL)
4059 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004060
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004061 /* create CODEC component name */
4062 codec->name = fmt_single_name(dev, &codec->id);
4063 if (codec->name == NULL) {
Kuninori Morimotof790b942013-02-25 00:40:09 -08004064 ret = -ENOMEM;
4065 goto fail_codec;
Mark Brown151ab222009-05-09 16:22:58 +01004066 }
4067
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00004068 if (codec_drv->compress_type)
4069 codec->compress_type = codec_drv->compress_type;
4070 else
4071 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
4072
Mark Brownc3acec22010-12-02 16:15:29 +00004073 codec->write = codec_drv->write;
4074 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004075 codec->volatile_register = codec_drv->volatile_register;
4076 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004077 codec->writable_register = codec_drv->writable_register;
Mark Brown5124e692012-02-08 13:20:50 +00004078 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004079 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4080 codec->dapm.dev = dev;
4081 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00004082 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01004083 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004084 codec->dev = dev;
4085 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004086 codec->num_dai = num_dai;
4087 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004088
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004089 /* allocate CODEC register cache */
4090 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004091 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00004092 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004093 /* it is necessary to make a copy of the default register cache
4094 * because in the case of using a compression type that requires
Bill Pembertonf6e65742012-11-19 13:25:33 -05004095 * the default register cache to be marked as the
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004096 * kernel might have freed the array by the time we initialize
4097 * the cache.
4098 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00004099 if (codec_drv->reg_cache_default) {
4100 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
4101 reg_size, GFP_KERNEL);
4102 if (!codec->reg_def_copy) {
4103 ret = -ENOMEM;
Kuninori Morimotof790b942013-02-25 00:40:09 -08004104 goto fail_codec_name;
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00004105 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004106 }
4107 }
4108
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004109 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
4110 if (!codec->volatile_register)
4111 codec->volatile_register = snd_soc_default_volatile_register;
4112 if (!codec->readable_register)
4113 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004114 if (!codec->writable_register)
4115 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004116 }
4117
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004118 for (i = 0; i < num_dai; i++) {
4119 fixup_codec_formats(&dai_drv[i].playback);
4120 fixup_codec_formats(&dai_drv[i].capture);
4121 }
4122
Mark Brown054880f2012-04-09 17:29:19 +01004123 mutex_lock(&client_mutex);
4124 list_add(&codec->list, &codec_list);
4125 mutex_unlock(&client_mutex);
4126
Mark Brown26b01cc2010-08-18 20:20:55 +01004127 /* register any DAIs */
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004128 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4129 if (ret < 0) {
4130 dev_err(codec->dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4131 goto fail_codec_name;
Mark Brown26b01cc2010-08-18 20:20:55 +01004132 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004133
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004134 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004135 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004136
Kuninori Morimotof790b942013-02-25 00:40:09 -08004137fail_codec_name:
Kuninori Morimotoe1328a82013-03-07 17:42:33 -08004138 mutex_lock(&client_mutex);
4139 list_del(&codec->list);
4140 mutex_unlock(&client_mutex);
4141
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004142 kfree(codec->name);
Kuninori Morimotof790b942013-02-25 00:40:09 -08004143fail_codec:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004144 kfree(codec);
4145 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004146}
4147EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4148
4149/**
4150 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4151 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004152 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004153 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004154void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004155{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004156 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004157
4158 list_for_each_entry(codec, &codec_list, list) {
4159 if (dev == codec->dev)
4160 goto found;
4161 }
4162 return;
4163
4164found:
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004165 snd_soc_unregister_dais(dev, codec->num_dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004166
Mark Brown0d0cf002008-12-10 14:32:45 +00004167 mutex_lock(&client_mutex);
4168 list_del(&codec->list);
4169 mutex_unlock(&client_mutex);
4170
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004171 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004172
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004173 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004174 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01004175 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004176 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004177}
4178EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4179
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004180/* Retrieve a card's name from device tree */
4181int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4182 const char *propname)
4183{
4184 struct device_node *np = card->dev->of_node;
4185 int ret;
4186
4187 ret = of_property_read_string_index(np, propname, 0, &card->name);
4188 /*
4189 * EINVAL means the property does not exist. This is fine providing
4190 * card->name was previously set, which is checked later in
4191 * snd_soc_register_card.
4192 */
4193 if (ret < 0 && ret != -EINVAL) {
4194 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004195 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004196 propname, ret);
4197 return ret;
4198 }
4199
4200 return 0;
4201}
4202EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4203
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004204int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4205 const char *propname)
4206{
4207 struct device_node *np = card->dev->of_node;
4208 int num_routes;
4209 struct snd_soc_dapm_route *routes;
4210 int i, ret;
4211
4212 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004213 if (num_routes < 0 || num_routes & 1) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004214 dev_err(card->dev, "ASoC: Property '%s' does not exist or its"
4215 " length is not even\n", propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004216 return -EINVAL;
4217 }
4218 num_routes /= 2;
4219 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004220 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004221 propname);
4222 return -EINVAL;
4223 }
4224
4225 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4226 GFP_KERNEL);
4227 if (!routes) {
4228 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004229 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004230 return -EINVAL;
4231 }
4232
4233 for (i = 0; i < num_routes; i++) {
4234 ret = of_property_read_string_index(np, propname,
4235 2 * i, &routes[i].sink);
4236 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004237 dev_err(card->dev,
4238 "ASoC: Property '%s' index %d could not be read: %d\n",
4239 propname, 2 * i, ret);
Matthias Kaehlckeb761c0c2012-07-11 17:36:34 +02004240 kfree(routes);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004241 return -EINVAL;
4242 }
4243 ret = of_property_read_string_index(np, propname,
4244 (2 * i) + 1, &routes[i].source);
4245 if (ret) {
4246 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004247 "ASoC: Property '%s' index %d could not be read: %d\n",
4248 propname, (2 * i) + 1, ret);
Matthias Kaehlckeb761c0c2012-07-11 17:36:34 +02004249 kfree(routes);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004250 return -EINVAL;
4251 }
4252 }
4253
4254 card->num_dapm_routes = num_routes;
4255 card->dapm_routes = routes;
4256
4257 return 0;
4258}
4259EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4260
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004261unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4262 const char *prefix)
4263{
4264 int ret, i;
4265 char prop[128];
4266 unsigned int format = 0;
4267 int bit, frame;
4268 const char *str;
4269 struct {
4270 char *name;
4271 unsigned int val;
4272 } of_fmt_table[] = {
4273 { "i2s", SND_SOC_DAIFMT_I2S },
4274 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4275 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4276 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4277 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4278 { "ac97", SND_SOC_DAIFMT_AC97 },
4279 { "pdm", SND_SOC_DAIFMT_PDM},
4280 { "msb", SND_SOC_DAIFMT_MSB },
4281 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004282 };
4283
4284 if (!prefix)
4285 prefix = "";
4286
4287 /*
4288 * check "[prefix]format = xxx"
4289 * SND_SOC_DAIFMT_FORMAT_MASK area
4290 */
4291 snprintf(prop, sizeof(prop), "%sformat", prefix);
4292 ret = of_property_read_string(np, prop, &str);
4293 if (ret == 0) {
4294 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4295 if (strcmp(str, of_fmt_table[i].name) == 0) {
4296 format |= of_fmt_table[i].val;
4297 break;
4298 }
4299 }
4300 }
4301
4302 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004303 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004304 * SND_SOC_DAIFMT_CLOCK_MASK area
4305 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004306 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4307 if (of_get_property(np, prop, NULL))
4308 format |= SND_SOC_DAIFMT_CONT;
4309 else
4310 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004311
4312 /*
4313 * check "[prefix]bitclock-inversion"
4314 * check "[prefix]frame-inversion"
4315 * SND_SOC_DAIFMT_INV_MASK area
4316 */
4317 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4318 bit = !!of_get_property(np, prop, NULL);
4319
4320 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4321 frame = !!of_get_property(np, prop, NULL);
4322
4323 switch ((bit << 4) + frame) {
4324 case 0x11:
4325 format |= SND_SOC_DAIFMT_IB_IF;
4326 break;
4327 case 0x10:
4328 format |= SND_SOC_DAIFMT_IB_NF;
4329 break;
4330 case 0x01:
4331 format |= SND_SOC_DAIFMT_NB_IF;
4332 break;
4333 default:
4334 /* SND_SOC_DAIFMT_NB_NF is default */
4335 break;
4336 }
4337
4338 /*
4339 * check "[prefix]bitclock-master"
4340 * check "[prefix]frame-master"
4341 * SND_SOC_DAIFMT_MASTER_MASK area
4342 */
4343 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4344 bit = !!of_get_property(np, prop, NULL);
4345
4346 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4347 frame = !!of_get_property(np, prop, NULL);
4348
4349 switch ((bit << 4) + frame) {
4350 case 0x11:
4351 format |= SND_SOC_DAIFMT_CBM_CFM;
4352 break;
4353 case 0x10:
4354 format |= SND_SOC_DAIFMT_CBM_CFS;
4355 break;
4356 case 0x01:
4357 format |= SND_SOC_DAIFMT_CBS_CFM;
4358 break;
4359 default:
4360 format |= SND_SOC_DAIFMT_CBS_CFS;
4361 break;
4362 }
4363
4364 return format;
4365}
4366EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4367
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004368static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004369{
Mark Brown384c89e2008-12-03 17:34:03 +00004370#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004371 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4372 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004373 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004374 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004375 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004376
Mark Brown8a9dab12011-01-10 22:25:21 +00004377 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004378 &codec_list_fops))
4379 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4380
Mark Brown8a9dab12011-01-10 22:25:21 +00004381 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004382 &dai_list_fops))
4383 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004384
Mark Brown8a9dab12011-01-10 22:25:21 +00004385 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004386 &platform_list_fops))
4387 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004388#endif
4389
Mark Brownfb257892011-04-28 10:57:54 +01004390 snd_soc_util_init();
4391
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004392 return platform_driver_register(&soc_driver);
4393}
Mark Brown4abe8e12010-10-12 17:41:03 +01004394module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004395
Mark Brown7d8c16a2008-11-30 22:11:24 +00004396static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004397{
Mark Brownfb257892011-04-28 10:57:54 +01004398 snd_soc_util_exit();
4399
Mark Brown384c89e2008-12-03 17:34:03 +00004400#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004401 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004402#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004403 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004404}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004405module_exit(snd_soc_exit);
4406
4407/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004408MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004409MODULE_DESCRIPTION("ALSA SoC Core");
4410MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004411MODULE_ALIAS("platform:soc-audio");