blob: fe16135250f8fdbcccc5f18a83e52b6d3611161a [file] [log] [blame]
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwood0664d882006-12-18 14:39:02 +01005 * Copyright 2005 Openedhand Ltd.
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00006 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
Liam Girdwood0664d882006-12-18 14:39:02 +01008 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01009 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood0664d882006-12-18 14:39:02 +010010 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020012 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +020018 * TODO:
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
23 */
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/init.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
30#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070031#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020032#include <linux/platform_device.h>
Mark Brownf0e8ed82011-09-20 11:41:54 +010033#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070035#include <linux/of.h>
Marek Vasut474828a2009-07-22 13:01:03 +020036#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020037#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000038#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020039#include <sound/pcm.h>
40#include <sound/pcm_params.h>
41#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010042#include <sound/soc-dpcm.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020043#include <sound/initval.h>
44
Mark Browna8b1d342010-11-03 18:05:58 -040045#define CREATE_TRACE_POINTS
46#include <trace/events/asoc.h>
47
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000048#define NAME_SIZE 32
49
Frank Mandarinodb2a4162006-10-06 18:31:09 +020050static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
51
Mark Brown384c89e2008-12-03 17:34:03 +000052#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000053struct dentry *snd_soc_debugfs_root;
54EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000055#endif
56
Mark Brownc5af3a22008-11-28 13:29:45 +000057static DEFINE_MUTEX(client_mutex);
Mark Brown91151712008-11-30 23:31:24 +000058static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000059static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000060static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000061
Frank Mandarinodb2a4162006-10-06 18:31:09 +020062/*
63 * This is a timeout to do a DAPM powerdown after a stream is closed().
64 * It can be used to eliminate pops between different playback streams, e.g.
65 * between two audio tracks.
66 */
67static int pmdown_time = 5000;
68module_param(pmdown_time, int, 0);
69MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
70
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000071/* returns the minimum number of bytes needed to represent
72 * a particular given value */
73static int min_bytes_needed(unsigned long val)
74{
75 int c = 0;
76 int i;
77
78 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
79 if (val & (1UL << i))
80 break;
81 c = (sizeof val * 8) - c;
82 if (!c || (c % 8))
83 c = (c + 8) / 8;
84 else
85 c /= 8;
86 return c;
87}
88
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000089/* fill buf which is 'len' bytes with a formatted
90 * string of the form 'reg: value\n' */
91static int format_register_str(struct snd_soc_codec *codec,
92 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +000093{
Stephen Warren00b317a2011-04-01 14:50:44 -060094 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
95 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000096 int ret;
97 char tmpbuf[len + 1];
98 char regbuf[regsize + 1];
99
100 /* since tmpbuf is allocated on the stack, warn the callers if they
101 * try to abuse this function */
102 WARN_ON(len > 63);
103
104 /* +2 for ': ' and + 1 for '\n' */
105 if (wordsize + regsize + 2 + 1 != len)
106 return -EINVAL;
107
Mark Brown25032c12011-08-01 13:52:48 +0900108 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000109 if (ret < 0) {
110 memset(regbuf, 'X', regsize);
111 regbuf[regsize] = '\0';
112 } else {
113 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
114 }
115
116 /* prepare the buffer */
117 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
118 /* copy it back to the caller without the '\0' */
119 memcpy(buf, tmpbuf, len);
120
121 return 0;
122}
123
124/* codec register dump */
125static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
126 size_t count, loff_t pos)
127{
128 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000129 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000130 int len;
131 size_t total = 0;
132 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000133
Stephen Warren00b317a2011-04-01 14:50:44 -0600134 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
135 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000136
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000137 len = wordsize + regsize + 2 + 1;
138
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000139 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000140 return 0;
141
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000142 if (codec->driver->reg_cache_step)
143 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000144
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000145 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausenb92d1502011-08-27 18:24:14 +0200146 if (!snd_soc_codec_readable_register(codec, i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000147 continue;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000148 if (codec->driver->display_register) {
149 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000150 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100151 } else {
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000152 /* only support larger than PAGE_SIZE bytes debugfs
153 * entries for the default case */
154 if (p >= pos) {
155 if (total + len >= count - 1)
156 break;
157 format_register_str(codec, i, buf + total, len);
158 total += len;
159 }
160 p += len;
Mark Brown5164d742010-07-14 16:14:33 +0100161 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000162 }
163
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000164 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000165
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000166 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000167}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000168
Mark Brown2624d5f2009-11-03 21:56:13 +0000169static ssize_t codec_reg_show(struct device *dev,
170 struct device_attribute *attr, char *buf)
171{
Mark Brown36ae1a92012-01-06 17:12:45 -0800172 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000173
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000174 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000175}
176
177static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
178
Mark Browndbe21402010-02-12 11:37:24 +0000179static ssize_t pmdown_time_show(struct device *dev,
180 struct device_attribute *attr, char *buf)
181{
Mark Brown36ae1a92012-01-06 17:12:45 -0800182 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000183
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000184 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000185}
186
187static ssize_t pmdown_time_set(struct device *dev,
188 struct device_attribute *attr,
189 const char *buf, size_t count)
190{
Mark Brown36ae1a92012-01-06 17:12:45 -0800191 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700192 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000193
Mark Brownc593b522010-10-27 20:11:17 -0700194 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
195 if (ret)
196 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000197
198 return count;
199}
200
201static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
202
Mark Brown2624d5f2009-11-03 21:56:13 +0000203#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000204static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000205 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000206{
207 ssize_t ret;
208 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000209 char *buf;
210
211 if (*ppos < 0 || !count)
212 return -EINVAL;
213
214 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000215 if (!buf)
216 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000217
218 ret = soc_codec_reg_show(codec, buf, count, *ppos);
219 if (ret >= 0) {
220 if (copy_to_user(user_buf, buf, ret)) {
221 kfree(buf);
222 return -EFAULT;
223 }
224 *ppos += ret;
225 }
226
Mark Brown2624d5f2009-11-03 21:56:13 +0000227 kfree(buf);
228 return ret;
229}
230
231static ssize_t codec_reg_write_file(struct file *file,
232 const char __user *user_buf, size_t count, loff_t *ppos)
233{
234 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700235 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000236 char *start = buf;
237 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000238 struct snd_soc_codec *codec = file->private_data;
239
240 buf_size = min(count, (sizeof(buf)-1));
241 if (copy_from_user(buf, user_buf, buf_size))
242 return -EFAULT;
243 buf[buf_size] = 0;
244
Mark Brown2624d5f2009-11-03 21:56:13 +0000245 while (*start == ' ')
246 start++;
247 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000248 while (*start == ' ')
249 start++;
250 if (strict_strtoul(start, 16, &value))
251 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000252
253 /* Userspace has been fiddling around behind the kernel's back */
254 add_taint(TAINT_USER);
255
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000256 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000257 return buf_size;
258}
259
260static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700261 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000262 .read = codec_reg_read_file,
263 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200264 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000265};
266
267static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
268{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200269 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
270
271 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
272 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000273 if (!codec->debugfs_codec_root) {
Liam Girdwood64e60f92012-02-15 15:15:37 +0000274 dev_warn(codec->dev, "Failed to create codec debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000275 return;
276 }
277
Mark Brownaaee8ef2011-01-26 20:53:50 +0000278 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
279 &codec->cache_sync);
280 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
281 &codec->cache_only);
282
Mark Brown2624d5f2009-11-03 21:56:13 +0000283 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
284 codec->debugfs_codec_root,
285 codec, &codec_reg_fops);
286 if (!codec->debugfs_reg)
Liam Girdwood64e60f92012-02-15 15:15:37 +0000287 dev_warn(codec->dev, "Failed to create codec register debugfs file\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000288
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200289 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000290}
291
292static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
293{
294 debugfs_remove_recursive(codec->debugfs_codec_root);
295}
296
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000297static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
298{
299 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
300
301 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
302 debugfs_card_root);
303 if (!platform->debugfs_platform_root) {
304 dev_warn(platform->dev,
305 "Failed to create platform debugfs directory\n");
306 return;
307 }
308
309 snd_soc_dapm_debugfs_init(&platform->dapm,
310 platform->debugfs_platform_root);
311}
312
313static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
314{
315 debugfs_remove_recursive(platform->debugfs_platform_root);
316}
317
Mark Brownc3c5a192010-09-15 18:15:14 +0100318static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
319 size_t count, loff_t *ppos)
320{
321 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100322 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100323 struct snd_soc_codec *codec;
324
325 if (!buf)
326 return -ENOMEM;
327
Mark Brown2b194f9d2010-10-13 10:52:16 +0100328 list_for_each_entry(codec, &codec_list, list) {
329 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
330 codec->name);
331 if (len >= 0)
332 ret += len;
333 if (ret > PAGE_SIZE) {
334 ret = PAGE_SIZE;
335 break;
336 }
337 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100338
339 if (ret >= 0)
340 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
341
342 kfree(buf);
343
344 return ret;
345}
346
347static const struct file_operations codec_list_fops = {
348 .read = codec_list_read_file,
349 .llseek = default_llseek,/* read accesses f_pos */
350};
351
Mark Brownf3208782010-09-15 18:19:07 +0100352static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
353 size_t count, loff_t *ppos)
354{
355 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100356 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100357 struct snd_soc_dai *dai;
358
359 if (!buf)
360 return -ENOMEM;
361
Mark Brown2b194f9d2010-10-13 10:52:16 +0100362 list_for_each_entry(dai, &dai_list, list) {
363 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
364 if (len >= 0)
365 ret += len;
366 if (ret > PAGE_SIZE) {
367 ret = PAGE_SIZE;
368 break;
369 }
370 }
Mark Brownf3208782010-09-15 18:19:07 +0100371
Mark Brown2b194f9d2010-10-13 10:52:16 +0100372 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100373
374 kfree(buf);
375
376 return ret;
377}
378
379static const struct file_operations dai_list_fops = {
380 .read = dai_list_read_file,
381 .llseek = default_llseek,/* read accesses f_pos */
382};
383
Mark Brown19c7ac22010-09-15 18:22:40 +0100384static ssize_t platform_list_read_file(struct file *file,
385 char __user *user_buf,
386 size_t count, loff_t *ppos)
387{
388 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100389 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100390 struct snd_soc_platform *platform;
391
392 if (!buf)
393 return -ENOMEM;
394
Mark Brown2b194f9d2010-10-13 10:52:16 +0100395 list_for_each_entry(platform, &platform_list, list) {
396 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
397 platform->name);
398 if (len >= 0)
399 ret += len;
400 if (ret > PAGE_SIZE) {
401 ret = PAGE_SIZE;
402 break;
403 }
404 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100405
Mark Brown2b194f9d2010-10-13 10:52:16 +0100406 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100407
408 kfree(buf);
409
410 return ret;
411}
412
413static const struct file_operations platform_list_fops = {
414 .read = platform_list_read_file,
415 .llseek = default_llseek,/* read accesses f_pos */
416};
417
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200418static void soc_init_card_debugfs(struct snd_soc_card *card)
419{
420 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000421 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200422 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200423 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100424 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200425 return;
426 }
427
428 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
429 card->debugfs_card_root,
430 &card->pop_time);
431 if (!card->debugfs_pop_time)
432 dev_warn(card->dev,
433 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200434}
435
436static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
437{
438 debugfs_remove_recursive(card->debugfs_card_root);
439}
440
Mark Brown2624d5f2009-11-03 21:56:13 +0000441#else
442
443static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
444{
445}
446
447static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
448{
449}
Axel Linb95fccb2010-11-09 17:06:44 +0800450
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000451static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
452{
453}
454
455static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
456{
457}
458
Axel Linb95fccb2010-11-09 17:06:44 +0800459static inline void soc_init_card_debugfs(struct snd_soc_card *card)
460{
461}
462
463static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
464{
465}
Mark Brown2624d5f2009-11-03 21:56:13 +0000466#endif
467
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100468struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
469 const char *dai_link, int stream)
470{
471 int i;
472
473 for (i = 0; i < card->num_links; i++) {
474 if (card->rtd[i].dai_link->no_pcm &&
475 !strcmp(card->rtd[i].dai_link->name, dai_link))
476 return card->rtd[i].pcm->streams[stream].substream;
477 }
478 dev_dbg(card->dev, "failed to find dai link %s\n", dai_link);
479 return NULL;
480}
481EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
482
483struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
484 const char *dai_link)
485{
486 int i;
487
488 for (i = 0; i < card->num_links; i++) {
489 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
490 return &card->rtd[i];
491 }
492 dev_dbg(card->dev, "failed to find rtd %s\n", dai_link);
493 return NULL;
494}
495EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
496
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200497#ifdef CONFIG_SND_SOC_AC97_BUS
498/* unregister ac97 codec */
499static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
500{
501 if (codec->ac97->dev.bus)
502 device_unregister(&codec->ac97->dev);
503 return 0;
504}
505
506/* stop no dev release warning */
507static void soc_ac97_device_release(struct device *dev){}
508
509/* register ac97 codec to bus */
510static int soc_ac97_dev_register(struct snd_soc_codec *codec)
511{
512 int err;
513
514 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100515 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200516 codec->ac97->dev.release = soc_ac97_device_release;
517
Kay Sieversbb072bf2008-11-02 03:50:35 +0100518 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000519 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200520 err = device_register(&codec->ac97->dev);
521 if (err < 0) {
522 snd_printk(KERN_ERR "Can't register ac97 bus\n");
523 codec->ac97->dev.bus = NULL;
524 return err;
525 }
526 return 0;
527}
528#endif
529
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000530#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200531/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000532int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200533{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000534 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200535 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200536 int i;
537
Daniel Macke3509ff2009-06-03 17:44:49 +0200538 /* If the initialization of this soc device failed, there is no codec
539 * associated with it. Just bail out in this case.
540 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000541 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200542 return 0;
543
Andy Green6ed25972008-06-13 16:24:05 +0100544 /* Due to the resume being scheduled into a workqueue we could
545 * suspend before that's finished - wait for it to complete.
546 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000547 snd_power_lock(card->snd_card);
548 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
549 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100550
551 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100553
Mark Browna00f90f2010-12-02 16:24:24 +0000554 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000555 for (i = 0; i < card->num_rtd; i++) {
556 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
557 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100558
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000559 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100560 continue;
561
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 if (drv->ops->digital_mute && dai->playback_active)
563 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200564 }
565
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100566 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000567 for (i = 0; i < card->num_rtd; i++) {
568 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100569 continue;
570
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000571 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100572 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100573
Mark Brown87506542008-11-18 20:50:34 +0000574 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000575 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200576
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000577 for (i = 0; i < card->num_rtd; i++) {
578 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
579 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100580
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000581 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100582 continue;
583
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000584 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
585 cpu_dai->driver->suspend(cpu_dai);
586 if (platform->driver->suspend && !platform->suspended) {
587 platform->driver->suspend(cpu_dai);
588 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100589 }
590 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200591
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000592 /* close any waiting streams and save state */
593 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +0100594 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200595 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000596 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100597
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599
600 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100601 continue;
602
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800603 snd_soc_dapm_stream_event(&card->rtd[i],
604 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800605 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000606
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800607 snd_soc_dapm_stream_event(&card->rtd[i],
608 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800609 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000610 }
611
612 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200613 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000614 /* If there are paths active then the CODEC will be held with
615 * bias _ON and should not be suspended. */
616 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200617 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000618 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000619 /*
620 * If the CODEC is capable of idle
621 * bias off then being in STANDBY
622 * means it's doing something,
623 * otherwise fall through.
624 */
625 if (codec->dapm.idle_bias_off) {
626 dev_dbg(codec->dev,
627 "idle_bias_off CODEC on over suspend\n");
628 break;
629 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000630 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100631 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000632 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900633 codec->cache_sync = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000634 break;
635 default:
636 dev_dbg(codec->dev, "CODEC is on over suspend\n");
637 break;
638 }
639 }
640 }
641
642 for (i = 0; i < card->num_rtd; i++) {
643 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
644
645 if (card->rtd[i].dai_link->ignore_suspend)
646 continue;
647
648 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
649 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200650 }
651
Mark Brown87506542008-11-18 20:50:34 +0000652 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000653 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200654
655 return 0;
656}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000657EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200658
Andy Green6ed25972008-06-13 16:24:05 +0100659/* deferred resume work, so resume can complete before we finished
660 * setting our codec back up, which can be very slow on I2C
661 */
662static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200663{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000664 struct snd_soc_card *card =
665 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200666 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200667 int i;
668
Andy Green6ed25972008-06-13 16:24:05 +0100669 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
670 * so userspace apps are blocked from touching us
671 */
672
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000673 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100674
Mark Brown99497882010-05-07 20:24:05 +0100675 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000676 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100677
Mark Brown87506542008-11-18 20:50:34 +0000678 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000679 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200680
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000681 /* resume AC97 DAIs */
682 for (i = 0; i < card->num_rtd; i++) {
683 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100684
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000685 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100686 continue;
687
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000688 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
689 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690 }
691
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200692 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000693 /* If the CODEC was idle over suspend then it will have been
694 * left with bias OFF or STANDBY and suspended so we must now
695 * resume. Otherwise the suspend was suppressed.
696 */
697 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200698 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000699 case SND_SOC_BIAS_STANDBY:
700 case SND_SOC_BIAS_OFF:
701 codec->driver->resume(codec);
702 codec->suspended = 0;
703 break;
704 default:
705 dev_dbg(codec->dev, "CODEC was on over suspend\n");
706 break;
707 }
Mark Brown1547aba2010-05-07 21:11:40 +0100708 }
709 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200710
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000711 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100712
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000713 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100714 continue;
715
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800716 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000717 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800718 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000719
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800720 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000721 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800722 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200723 }
724
Mark Brown3ff3f642008-05-19 12:32:25 +0200725 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000726 for (i = 0; i < card->num_rtd; i++) {
727 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
728 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100729
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000730 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100731 continue;
732
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000733 if (drv->ops->digital_mute && dai->playback_active)
734 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200735 }
736
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000737 for (i = 0; i < card->num_rtd; i++) {
738 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
739 struct snd_soc_platform *platform = card->rtd[i].platform;
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 (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
745 cpu_dai->driver->resume(cpu_dai);
746 if (platform->driver->resume && platform->suspended) {
747 platform->driver->resume(cpu_dai);
748 platform->suspended = 0;
749 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200750 }
751
Mark Brown87506542008-11-18 20:50:34 +0000752 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000753 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200754
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000755 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100756
757 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000758 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100759}
760
761/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000762int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100763{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000764 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600765 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200766
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800767 /* If the initialization of this soc device failed, there is no codec
768 * associated with it. Just bail out in this case.
769 */
770 if (list_empty(&card->codec_dev_list))
771 return 0;
772
Mark Brown64ab9ba2009-03-31 11:27:03 +0100773 /* AC97 devices might have other drivers hanging off them so
774 * need to resume immediately. Other drivers don't have that
775 * problem and may take a substantial amount of time to resume
776 * due to I/O costs and anti-pop so handle them out of line.
777 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000778 for (i = 0; i < card->num_rtd; i++) {
779 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600780 ac97_control |= cpu_dai->driver->ac97_control;
781 }
782 if (ac97_control) {
783 dev_dbg(dev, "Resuming AC97 immediately\n");
784 soc_resume_deferred(&card->deferred_resume_work);
785 } else {
786 dev_dbg(dev, "Scheduling resume work\n");
787 if (!schedule_work(&card->deferred_resume_work))
788 dev_err(dev, "resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100789 }
Andy Green6ed25972008-06-13 16:24:05 +0100790
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200791 return 0;
792}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000793EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200794#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000795#define snd_soc_suspend NULL
796#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200797#endif
798
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100799static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800800};
801
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000802static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200803{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000804 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
805 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000806 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000807 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000808 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100809 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200810
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000811 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000812
Mark Brownb19e6e72012-03-14 21:18:39 +0000813 /* Find CPU DAI from registered DAIs*/
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000814 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600815 if (dai_link->cpu_of_node &&
816 (cpu_dai->dev->of_node != dai_link->cpu_of_node))
817 continue;
818 if (dai_link->cpu_name &&
819 strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
820 continue;
821 if (dai_link->cpu_dai_name &&
822 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
823 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700824
825 rtd->cpu_dai = cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000826 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000827
828 if (!rtd->cpu_dai) {
829 dev_dbg(card->dev, "CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000830 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000831 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000832 }
833
Mark Brownb19e6e72012-03-14 21:18:39 +0000834 /* Find CODEC from registered CODECs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000835 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700836 if (dai_link->codec_of_node) {
837 if (codec->dev->of_node != dai_link->codec_of_node)
838 continue;
839 } else {
840 if (strcmp(codec->name, dai_link->codec_name))
841 continue;
842 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000843
Stephen Warren2610ab72011-12-07 13:58:27 -0700844 rtd->codec = codec;
845
846 /*
847 * CODEC found, so find CODEC DAI from registered DAIs from
848 * this CODEC
849 */
850 list_for_each_entry(codec_dai, &dai_list, list) {
851 if (codec->dev == codec_dai->dev &&
852 !strcmp(codec_dai->name,
853 dai_link->codec_dai_name)) {
854
855 rtd->codec_dai = codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000856 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000857 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000858
859 if (!rtd->codec_dai) {
860 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
Stephen Warren2610ab72011-12-07 13:58:27 -0700861 dai_link->codec_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000862 return -EPROBE_DEFER;
863 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000864 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000865
Mark Brownb19e6e72012-03-14 21:18:39 +0000866 if (!rtd->codec) {
867 dev_dbg(card->dev, "CODEC %s not registered\n",
868 dai_link->codec_name);
869 return -EPROBE_DEFER;
870 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100871
872 /* if there's no platform we match on the empty platform */
873 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700874 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100875 platform_name = "snd-soc-dummy";
876
Mark Brownb19e6e72012-03-14 21:18:39 +0000877 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000878 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700879 if (dai_link->platform_of_node) {
880 if (platform->dev->of_node !=
881 dai_link->platform_of_node)
882 continue;
883 } else {
884 if (strcmp(platform->name, platform_name))
885 continue;
886 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700887
888 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000889 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000890 if (!rtd->platform) {
891 dev_dbg(card->dev, "platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000892 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000893 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000894 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000895
896 card->num_rtd++;
897
898 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000899}
900
Stephen Warrend12cd192012-06-08 12:34:22 -0600901static int soc_remove_platform(struct snd_soc_platform *platform)
902{
903 int ret;
904
905 if (platform->driver->remove) {
906 ret = platform->driver->remove(platform);
907 if (ret < 0)
908 pr_err("asoc: failed to remove %s: %d\n",
909 platform->name, ret);
910 }
911
912 /* Make sure all DAPM widgets are freed */
913 snd_soc_dapm_free(&platform->dapm);
914
915 soc_cleanup_platform_debugfs(platform);
916 platform->probed = 0;
917 list_del(&platform->card_list);
918 module_put(platform->dev->driver->owner);
919
920 return 0;
921}
922
Jarkko Nikula589c3562010-12-06 16:27:07 +0200923static void soc_remove_codec(struct snd_soc_codec *codec)
924{
925 int err;
926
927 if (codec->driver->remove) {
928 err = codec->driver->remove(codec);
929 if (err < 0)
930 dev_err(codec->dev,
931 "asoc: failed to remove %s: %d\n",
932 codec->name, err);
933 }
934
935 /* Make sure all DAPM widgets are freed */
936 snd_soc_dapm_free(&codec->dapm);
937
938 soc_cleanup_codec_debugfs(codec);
939 codec->probed = 0;
940 list_del(&codec->card_list);
941 module_put(codec->dev->driver->owner);
942}
943
Stephen Warren62ae68f2012-06-08 12:34:23 -0600944static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000945{
946 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000947 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
948 int err;
949
950 /* unregister the rtd device */
951 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800952 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
953 device_remove_file(rtd->dev, &dev_attr_codec_reg);
954 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000955 rtd->dev_registered = 0;
956 }
957
958 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100959 if (codec_dai && codec_dai->probed &&
960 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000961 if (codec_dai->driver->remove) {
962 err = codec_dai->driver->remove(codec_dai);
963 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200964 pr_err("asoc: failed to remove %s: %d\n",
965 codec_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000966 }
967 codec_dai->probed = 0;
968 list_del(&codec_dai->card_list);
969 }
970
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000971 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100972 if (cpu_dai && cpu_dai->probed &&
973 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000974 if (cpu_dai->driver->remove) {
975 err = cpu_dai->driver->remove(cpu_dai);
976 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200977 pr_err("asoc: failed to remove %s: %d\n",
978 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000979 }
980 cpu_dai->probed = 0;
981 list_del(&cpu_dai->card_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -0600982
Stephen Warren18d75642012-06-08 12:34:21 -0600983 if (!cpu_dai->codec) {
984 snd_soc_dapm_free(&cpu_dai->dapm);
Stephen Warrena9db7db2012-06-08 12:34:20 -0600985 module_put(cpu_dai->dev->driver->owner);
Stephen Warren18d75642012-06-08 12:34:21 -0600986 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000987 }
988}
989
Stephen Warren62ae68f2012-06-08 12:34:23 -0600990static void soc_remove_link_components(struct snd_soc_card *card, int num,
991 int order)
992{
993 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
994 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
995 struct snd_soc_dai *codec_dai = rtd->codec_dai;
996 struct snd_soc_platform *platform = rtd->platform;
997 struct snd_soc_codec *codec;
998
999 /* remove the platform */
1000 if (platform && platform->probed &&
1001 platform->driver->remove_order == order) {
1002 soc_remove_platform(platform);
1003 }
1004
1005 /* remove the CODEC-side CODEC */
1006 if (codec_dai) {
1007 codec = codec_dai->codec;
1008 if (codec && codec->probed &&
1009 codec->driver->remove_order == order)
1010 soc_remove_codec(codec);
1011 }
1012
1013 /* remove any CPU-side CODEC */
1014 if (cpu_dai) {
1015 codec = cpu_dai->codec;
1016 if (codec && codec->probed &&
1017 codec->driver->remove_order == order)
1018 soc_remove_codec(codec);
1019 }
1020}
1021
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001022static void soc_remove_dai_links(struct snd_soc_card *card)
1023{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001024 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001025
Liam Girdwood0168bf02011-06-07 16:08:05 +01001026 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1027 order++) {
1028 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001029 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001030 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001031
1032 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1033 order++) {
1034 for (dai = 0; dai < card->num_rtd; dai++)
1035 soc_remove_link_components(card, dai, order);
1036 }
1037
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001038 card->num_rtd = 0;
1039}
1040
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001041static void soc_set_name_prefix(struct snd_soc_card *card,
1042 struct snd_soc_codec *codec)
1043{
1044 int i;
1045
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001046 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001047 return;
1048
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001049 for (i = 0; i < card->num_configs; i++) {
1050 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001051 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1052 codec->name_prefix = map->name_prefix;
1053 break;
1054 }
1055 }
1056}
1057
Jarkko Nikula589c3562010-12-06 16:27:07 +02001058static int soc_probe_codec(struct snd_soc_card *card,
1059 struct snd_soc_codec *codec)
1060{
1061 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001062 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001063 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001064
1065 codec->card = card;
1066 codec->dapm.card = card;
1067 soc_set_name_prefix(card, codec);
1068
Jarkko Nikula70d29332011-01-27 16:24:22 +02001069 if (!try_module_get(codec->dev->driver->owner))
1070 return -ENODEV;
1071
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001072 soc_init_codec_debugfs(codec);
1073
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001074 if (driver->dapm_widgets)
1075 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1076 driver->num_dapm_widgets);
1077
Mark Brown888df392012-02-16 19:37:51 -08001078 /* Create DAPM widgets for each DAI stream */
1079 list_for_each_entry(dai, &dai_list, list) {
1080 if (dai->dev != codec->dev)
1081 continue;
1082
1083 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1084 }
1085
Mark Brown33c5f962011-08-22 18:40:30 +01001086 codec->dapm.idle_bias_off = driver->idle_bias_off;
1087
Mark Brown89b95ac02011-03-07 16:38:44 +00001088 if (driver->probe) {
1089 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001090 if (ret < 0) {
1091 dev_err(codec->dev,
1092 "asoc: failed to probe CODEC %s: %d\n",
1093 codec->name, ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001094 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001095 }
1096 }
1097
Mark Brownb7af1da2011-04-07 19:18:44 +09001098 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001099 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001100 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001101 if (driver->dapm_routes)
1102 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1103 driver->num_dapm_routes);
1104
Jarkko Nikula589c3562010-12-06 16:27:07 +02001105 /* mark codec as probed and add to card codec list */
1106 codec->probed = 1;
1107 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001108 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001109
Jarkko Nikula70d29332011-01-27 16:24:22 +02001110 return 0;
1111
1112err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001113 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001114 module_put(codec->dev->driver->owner);
1115
Jarkko Nikula589c3562010-12-06 16:27:07 +02001116 return ret;
1117}
1118
Liam Girdwood956245e2011-07-01 16:54:08 +01001119static int soc_probe_platform(struct snd_soc_card *card,
1120 struct snd_soc_platform *platform)
1121{
1122 int ret = 0;
1123 const struct snd_soc_platform_driver *driver = platform->driver;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001124 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001125
1126 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001127 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001128
1129 if (!try_module_get(platform->dev->driver->owner))
1130 return -ENODEV;
1131
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001132 soc_init_platform_debugfs(platform);
1133
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001134 if (driver->dapm_widgets)
1135 snd_soc_dapm_new_controls(&platform->dapm,
1136 driver->dapm_widgets, driver->num_dapm_widgets);
1137
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001138 /* Create DAPM widgets for each DAI stream */
1139 list_for_each_entry(dai, &dai_list, list) {
1140 if (dai->dev != platform->dev)
1141 continue;
1142
1143 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1144 }
1145
Stephen Warren3fec6b62012-04-05 12:28:01 -06001146 platform->dapm.idle_bias_off = 1;
1147
Liam Girdwood956245e2011-07-01 16:54:08 +01001148 if (driver->probe) {
1149 ret = driver->probe(platform);
1150 if (ret < 0) {
1151 dev_err(platform->dev,
1152 "asoc: failed to probe platform %s: %d\n",
1153 platform->name, ret);
1154 goto err_probe;
1155 }
1156 }
1157
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001158 if (driver->controls)
1159 snd_soc_add_platform_controls(platform, driver->controls,
1160 driver->num_controls);
1161 if (driver->dapm_routes)
1162 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1163 driver->num_dapm_routes);
1164
Liam Girdwood956245e2011-07-01 16:54:08 +01001165 /* mark platform as probed and add to card platform list */
1166 platform->probed = 1;
1167 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001168 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001169
1170 return 0;
1171
1172err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001173 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001174 module_put(platform->dev->driver->owner);
1175
1176 return ret;
1177}
1178
Mark Brown36ae1a92012-01-06 17:12:45 -08001179static void rtd_release(struct device *dev)
1180{
1181 kfree(dev);
1182}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001183
Jarkko Nikula589c3562010-12-06 16:27:07 +02001184static int soc_post_component_init(struct snd_soc_card *card,
1185 struct snd_soc_codec *codec,
1186 int num, int dailess)
1187{
1188 struct snd_soc_dai_link *dai_link = NULL;
1189 struct snd_soc_aux_dev *aux_dev = NULL;
1190 struct snd_soc_pcm_runtime *rtd;
1191 const char *temp, *name;
1192 int ret = 0;
1193
1194 if (!dailess) {
1195 dai_link = &card->dai_link[num];
1196 rtd = &card->rtd[num];
1197 name = dai_link->name;
1198 } else {
1199 aux_dev = &card->aux_dev[num];
1200 rtd = &card->rtd_aux[num];
1201 name = aux_dev->name;
1202 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001203 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001204
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001205 /* Make sure all DAPM widgets are instantiated */
1206 snd_soc_dapm_new_widgets(&codec->dapm);
1207
Jarkko Nikula589c3562010-12-06 16:27:07 +02001208 /* machine controls, routes and widgets are not prefixed */
1209 temp = codec->name_prefix;
1210 codec->name_prefix = NULL;
1211
1212 /* do machine specific initialization */
1213 if (!dailess && dai_link->init)
1214 ret = dai_link->init(rtd);
1215 else if (dailess && aux_dev->init)
1216 ret = aux_dev->init(&codec->dapm);
1217 if (ret < 0) {
1218 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1219 return ret;
1220 }
1221 codec->name_prefix = temp;
1222
Jarkko Nikula589c3562010-12-06 16:27:07 +02001223 /* register the rtd device */
1224 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001225
1226 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1227 if (!rtd->dev)
1228 return -ENOMEM;
1229 device_initialize(rtd->dev);
1230 rtd->dev->parent = card->dev;
1231 rtd->dev->release = rtd_release;
1232 rtd->dev->init_name = name;
1233 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001234 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001235 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1236 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1237 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1238 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001239 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001240 if (ret < 0) {
1241 dev_err(card->dev,
1242 "asoc: failed to register runtime device: %d\n", ret);
1243 return ret;
1244 }
1245 rtd->dev_registered = 1;
1246
1247 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001248 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001249 if (ret < 0)
1250 dev_err(codec->dev,
1251 "asoc: failed to add codec dapm sysfs entries: %d\n",
1252 ret);
1253
1254 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001255 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001256 if (ret < 0)
1257 dev_err(codec->dev,
1258 "asoc: failed to add codec sysfs files: %d\n", ret);
1259
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001260#ifdef CONFIG_DEBUG_FS
1261 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001262 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001263 goto out;
1264
1265 ret = soc_dpcm_debugfs_add(rtd);
1266 if (ret < 0)
1267 dev_err(rtd->dev, "asoc: failed to add dpcm sysfs entries: %d\n", ret);
1268
1269out:
1270#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001271 return 0;
1272}
1273
Stephen Warren62ae68f2012-06-08 12:34:23 -06001274static int soc_probe_link_components(struct snd_soc_card *card, int num,
1275 int order)
1276{
1277 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1278 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1279 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1280 struct snd_soc_platform *platform = rtd->platform;
1281 int ret;
1282
1283 /* probe the CPU-side component, if it is a CODEC */
1284 if (cpu_dai->codec &&
1285 !cpu_dai->codec->probed &&
1286 cpu_dai->codec->driver->probe_order == order) {
1287 ret = soc_probe_codec(card, cpu_dai->codec);
1288 if (ret < 0)
1289 return ret;
1290 }
1291
1292 /* probe the CODEC-side component */
1293 if (!codec_dai->codec->probed &&
1294 codec_dai->codec->driver->probe_order == order) {
1295 ret = soc_probe_codec(card, codec_dai->codec);
1296 if (ret < 0)
1297 return ret;
1298 }
1299
1300 /* probe the platform */
1301 if (!platform->probed &&
1302 platform->driver->probe_order == order) {
1303 ret = soc_probe_platform(card, platform);
1304 if (ret < 0)
1305 return ret;
1306 }
1307
1308 return 0;
1309}
1310
1311static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001312{
1313 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1314 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1315 struct snd_soc_codec *codec = rtd->codec;
1316 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001317 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1318 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1319 struct snd_soc_dapm_widget *play_w, *capture_w;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001320 int ret;
1321
Liam Girdwood0168bf02011-06-07 16:08:05 +01001322 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1323 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001324
1325 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001326 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001327 codec_dai->card = card;
1328 cpu_dai->card = card;
1329
1330 /* set default power off timeout */
1331 rtd->pmdown_time = pmdown_time;
1332
1333 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001334 if (!cpu_dai->probed &&
1335 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001336 if (!cpu_dai->codec) {
1337 cpu_dai->dapm.card = card;
1338 if (!try_module_get(cpu_dai->dev->driver->owner))
1339 return -ENODEV;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001340
Stephen Warren18d75642012-06-08 12:34:21 -06001341 list_add(&cpu_dai->dapm.list, &card->dapm_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001342 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1343 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001344
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001345 if (cpu_dai->driver->probe) {
1346 ret = cpu_dai->driver->probe(cpu_dai);
1347 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001348 pr_err("asoc: failed to probe CPU DAI %s: %d\n",
1349 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001350 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001351 return ret;
1352 }
1353 }
1354 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001355 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001356 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1357 }
1358
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001359 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001360 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001361 if (codec_dai->driver->probe) {
1362 ret = codec_dai->driver->probe(codec_dai);
1363 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001364 pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
1365 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001366 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001367 }
1368 }
1369
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001370 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001371 codec_dai->probed = 1;
1372 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001373 }
1374
Liam Girdwood0168bf02011-06-07 16:08:05 +01001375 /* complete DAI probe during last probe */
1376 if (order != SND_SOC_COMP_ORDER_LAST)
1377 return 0;
1378
Jarkko Nikula589c3562010-12-06 16:27:07 +02001379 ret = soc_post_component_init(card, codec, num, 0);
1380 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001381 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001382
Mark Brown36ae1a92012-01-06 17:12:45 -08001383 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001384 if (ret < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -02001385 pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001386
Mark Brownc74184e2012-04-04 22:12:09 +01001387 if (!dai_link->params) {
1388 /* create the pcm */
1389 ret = soc_new_pcm(rtd, num);
1390 if (ret < 0) {
1391 pr_err("asoc: can't create pcm %s :%d\n",
1392 dai_link->stream_name, ret);
1393 return ret;
1394 }
1395 } else {
1396 /* link the DAI widgets */
1397 play_w = codec_dai->playback_widget;
1398 capture_w = cpu_dai->capture_widget;
1399 if (play_w && capture_w) {
1400 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1401 capture_w, play_w);
1402 if (ret != 0) {
1403 dev_err(card->dev, "Can't link %s to %s: %d\n",
1404 play_w->name, capture_w->name, ret);
1405 return ret;
1406 }
1407 }
1408
1409 play_w = cpu_dai->playback_widget;
1410 capture_w = codec_dai->capture_widget;
1411 if (play_w && capture_w) {
1412 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1413 capture_w, play_w);
1414 if (ret != 0) {
1415 dev_err(card->dev, "Can't link %s to %s: %d\n",
1416 play_w->name, capture_w->name, ret);
1417 return ret;
1418 }
1419 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001420 }
1421
1422 /* add platform data for AC97 devices */
1423 if (rtd->codec_dai->driver->ac97_control)
1424 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1425
1426 return 0;
1427}
1428
1429#ifdef CONFIG_SND_SOC_AC97_BUS
1430static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1431{
1432 int ret;
1433
1434 /* Only instantiate AC97 if not already done by the adaptor
1435 * for the generic AC97 subsystem.
1436 */
1437 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001438 /*
1439 * It is possible that the AC97 device is already registered to
1440 * the device subsystem. This happens when the device is created
1441 * via snd_ac97_mixer(). Currently only SoC codec that does so
1442 * is the generic AC97 glue but others migh emerge.
1443 *
1444 * In those cases we don't try to register the device again.
1445 */
1446 if (!rtd->codec->ac97_created)
1447 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001448
1449 ret = soc_ac97_dev_register(rtd->codec);
1450 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001451 pr_err("asoc: AC97 device register failed:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001452 return ret;
1453 }
1454
1455 rtd->codec->ac97_registered = 1;
1456 }
1457 return 0;
1458}
1459
1460static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1461{
1462 if (codec->ac97_registered) {
1463 soc_ac97_dev_unregister(codec);
1464 codec->ac97_registered = 0;
1465 }
1466}
1467#endif
1468
Mark Brownb19e6e72012-03-14 21:18:39 +00001469static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1470{
1471 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1472 struct snd_soc_codec *codec;
1473
1474 /* find CODEC from registered CODECs*/
1475 list_for_each_entry(codec, &codec_list, list) {
1476 if (!strcmp(codec->name, aux_dev->codec_name))
1477 return 0;
1478 }
1479
1480 return -EPROBE_DEFER;
1481}
1482
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001483static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1484{
1485 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001486 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001487 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001488
1489 /* find CODEC from registered CODECs*/
1490 list_for_each_entry(codec, &codec_list, list) {
1491 if (!strcmp(codec->name, aux_dev->codec_name)) {
1492 if (codec->probed) {
1493 dev_err(codec->dev,
1494 "asoc: codec already probed");
1495 ret = -EBUSY;
1496 goto out;
1497 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001498 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001499 }
1500 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001501 /* codec not found */
1502 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001503 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001504
Jarkko Nikula676ad982010-12-03 09:18:22 +02001505found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001506 ret = soc_probe_codec(card, codec);
1507 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001508 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001509
Jarkko Nikula589c3562010-12-06 16:27:07 +02001510 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001511
1512out:
1513 return ret;
1514}
1515
1516static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1517{
1518 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1519 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001520
1521 /* unregister the rtd device */
1522 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001523 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1524 device_del(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001525 rtd->dev_registered = 0;
1526 }
1527
Jarkko Nikula589c3562010-12-06 16:27:07 +02001528 if (codec && codec->probed)
1529 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001530}
1531
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001532static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1533 enum snd_soc_compress_type compress_type)
1534{
1535 int ret;
1536
1537 if (codec->cache_init)
1538 return 0;
1539
1540 /* override the compress_type if necessary */
1541 if (compress_type && codec->compress_type != compress_type)
1542 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001543 ret = snd_soc_cache_init(codec);
1544 if (ret < 0) {
1545 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1546 ret);
1547 return ret;
1548 }
1549 codec->cache_init = 1;
1550 return 0;
1551}
1552
Mark Brownb19e6e72012-03-14 21:18:39 +00001553static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001554{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001555 struct snd_soc_codec *codec;
1556 struct snd_soc_codec_conf *codec_conf;
1557 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001558 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001559 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001560
Liam Girdwood01b9d992012-03-07 10:38:25 +00001561 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001562
Mark Brownb19e6e72012-03-14 21:18:39 +00001563 /* bind DAIs */
1564 for (i = 0; i < card->num_links; i++) {
1565 ret = soc_bind_dai_link(card, i);
1566 if (ret != 0)
1567 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001568 }
1569
Mark Brownb19e6e72012-03-14 21:18:39 +00001570 /* check aux_devs too */
1571 for (i = 0; i < card->num_aux_devs; i++) {
1572 ret = soc_check_aux_dev(card, i);
1573 if (ret != 0)
1574 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001575 }
1576
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001577 /* initialize the register cache for each available codec */
1578 list_for_each_entry(codec, &codec_list, list) {
1579 if (codec->cache_init)
1580 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001581 /* by default we don't override the compress_type */
1582 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001583 /* check to see if we need to override the compress_type */
1584 for (i = 0; i < card->num_configs; ++i) {
1585 codec_conf = &card->codec_conf[i];
1586 if (!strcmp(codec->name, codec_conf->dev_name)) {
1587 compress_type = codec_conf->compress_type;
1588 if (compress_type && compress_type
1589 != codec->compress_type)
1590 break;
1591 }
1592 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001593 ret = snd_soc_init_codec_cache(codec, compress_type);
Mark Brownb19e6e72012-03-14 21:18:39 +00001594 if (ret < 0)
1595 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001596 }
1597
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001598 /* card bind complete so register a sound card */
1599 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1600 card->owner, 0, &card->snd_card);
1601 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001602 pr_err("asoc: can't create sound card for card %s: %d\n",
1603 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001604 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001605 }
1606 card->snd_card->dev = card->dev;
1607
Mark Browne37a4972011-03-02 18:21:57 +00001608 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1609 card->dapm.dev = card->dev;
1610 card->dapm.card = card;
1611 list_add(&card->dapm.list, &card->dapm_list);
1612
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001613#ifdef CONFIG_DEBUG_FS
1614 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1615#endif
1616
Mark Brown88ee1c62011-02-02 10:43:26 +00001617#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001618 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001619 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001620#endif
Andy Green6ed25972008-06-13 16:24:05 +01001621
Mark Brown9a841eb2011-04-12 17:51:37 -07001622 if (card->dapm_widgets)
1623 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1624 card->num_dapm_widgets);
1625
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001626 /* initialise the sound card only once */
1627 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001628 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001629 if (ret < 0)
1630 goto card_probe_error;
1631 }
1632
Stephen Warren62ae68f2012-06-08 12:34:23 -06001633 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001634 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1635 order++) {
1636 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001637 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001638 if (ret < 0) {
1639 pr_err("asoc: failed to instantiate card %s: %d\n",
Stephen Warren62ae68f2012-06-08 12:34:23 -06001640 card->name, ret);
1641 goto probe_dai_err;
1642 }
1643 }
1644 }
1645
1646 /* probe all DAI links on this card */
1647 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1648 order++) {
1649 for (i = 0; i < card->num_links; i++) {
1650 ret = soc_probe_link_dais(card, i, order);
1651 if (ret < 0) {
1652 pr_err("asoc: failed to instantiate card %s: %d\n",
1653 card->name, ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001654 goto probe_dai_err;
1655 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001656 }
1657 }
1658
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001659 for (i = 0; i < card->num_aux_devs; i++) {
1660 ret = soc_probe_aux_dev(card, i);
1661 if (ret < 0) {
1662 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1663 card->name, ret);
1664 goto probe_aux_dev_err;
1665 }
1666 }
1667
Mark Brown888df392012-02-16 19:37:51 -08001668 snd_soc_dapm_link_dai_widgets(card);
1669
Mark Brownb7af1da2011-04-07 19:18:44 +09001670 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001671 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001672
Mark Brownb8ad29d2011-03-02 18:35:51 +00001673 if (card->dapm_routes)
1674 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1675 card->num_dapm_routes);
1676
Mark Brownb90d2f92011-10-10 13:38:06 +01001677 snd_soc_dapm_new_widgets(&card->dapm);
1678
Mark Brown75d9ac42011-09-27 16:41:01 +01001679 for (i = 0; i < card->num_links; i++) {
1680 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001681 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001682
Mark Brownf04209a2012-04-09 16:29:21 +01001683 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001684 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001685 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001686 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001687 dev_warn(card->rtd[i].codec_dai->dev,
1688 "Failed to set DAI format: %d\n",
1689 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001690 }
1691
1692 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001693 if (dai_fmt &&
1694 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001695 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1696 dai_fmt);
1697 if (ret != 0 && ret != -ENOTSUPP)
1698 dev_warn(card->rtd[i].cpu_dai->dev,
1699 "Failed to set DAI format: %d\n",
1700 ret);
1701 } else if (dai_fmt) {
1702 /* Flip the polarity for the "CPU" end */
1703 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1704 switch (dai_link->dai_fmt &
1705 SND_SOC_DAIFMT_MASTER_MASK) {
1706 case SND_SOC_DAIFMT_CBM_CFM:
1707 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1708 break;
1709 case SND_SOC_DAIFMT_CBM_CFS:
1710 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1711 break;
1712 case SND_SOC_DAIFMT_CBS_CFM:
1713 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1714 break;
1715 case SND_SOC_DAIFMT_CBS_CFS:
1716 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1717 break;
1718 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001719
1720 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001721 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001722 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001723 dev_warn(card->rtd[i].cpu_dai->dev,
1724 "Failed to set DAI format: %d\n",
1725 ret);
1726 }
1727 }
1728
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001729 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001730 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001731 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1732 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001733 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1734 "%s", card->driver_name ? card->driver_name : card->name);
1735 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1736 switch (card->snd_card->driver[i]) {
1737 case '_':
1738 case '-':
1739 case '\0':
1740 break;
1741 default:
1742 if (!isalnum(card->snd_card->driver[i]))
1743 card->snd_card->driver[i] = '_';
1744 break;
1745 }
1746 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001747
Mark Brown28e9ad92011-03-02 18:36:34 +00001748 if (card->late_probe) {
1749 ret = card->late_probe(card);
1750 if (ret < 0) {
1751 dev_err(card->dev, "%s late_probe() failed: %d\n",
1752 card->name, ret);
1753 goto probe_aux_dev_err;
1754 }
1755 }
1756
Mark Brown2dc00212011-10-08 13:59:44 +01001757 snd_soc_dapm_new_widgets(&card->dapm);
1758
Stephen Warren16332812011-11-23 12:42:04 -07001759 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001760 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001761 snd_soc_dapm_auto_nc_codec_pins(codec);
1762
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001763 ret = snd_card_register(card->snd_card);
1764 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001765 pr_err("asoc: failed to register soundcard for %s: %d\n",
1766 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001767 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001768 }
1769
1770#ifdef CONFIG_SND_SOC_AC97_BUS
1771 /* register any AC97 codecs */
1772 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001773 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1774 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001775 pr_err("asoc: failed to register AC97 %s: %d\n",
1776 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001777 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001778 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001779 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001780 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001781 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001782#endif
1783
Mark Brown435c5e22008-12-04 15:32:53 +00001784 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001785 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001786 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001787
1788 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001789
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001790probe_aux_dev_err:
1791 for (i = 0; i < card->num_aux_devs; i++)
1792 soc_remove_aux_dev(card, i);
1793
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001794probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001795 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001796
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001797card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001798 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001799 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001800
1801 snd_card_free(card->snd_card);
1802
Mark Brownb19e6e72012-03-14 21:18:39 +00001803base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001804 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001805
Mark Brownb19e6e72012-03-14 21:18:39 +00001806 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001807}
1808
1809/* probes a new socdev */
1810static int soc_probe(struct platform_device *pdev)
1811{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001812 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001813 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001814
Vinod Koul70a7ca32011-01-14 19:22:48 +05301815 /*
1816 * no card, so machine driver should be registering card
1817 * we should not be here in that case so ret error
1818 */
1819 if (!card)
1820 return -EINVAL;
1821
Mark Brownfe4085e2012-03-02 13:07:41 +00001822 dev_warn(&pdev->dev,
1823 "ASoC machine %s should use snd_soc_register_card()\n",
1824 card->name);
1825
Mark Brown435c5e22008-12-04 15:32:53 +00001826 /* Bodge while we unpick instantiation */
1827 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001828
Mark Brown435c5e22008-12-04 15:32:53 +00001829 ret = snd_soc_register_card(card);
1830 if (ret != 0) {
1831 dev_err(&pdev->dev, "Failed to register card\n");
1832 return ret;
1833 }
1834
1835 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001836}
1837
Vinod Koulb0e26482011-01-13 22:48:02 +05301838static int soc_cleanup_card_resources(struct snd_soc_card *card)
1839{
Vinod Koulb0e26482011-01-13 22:48:02 +05301840 int i;
1841
1842 /* make sure any delayed work runs */
1843 for (i = 0; i < card->num_rtd; i++) {
1844 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1845 flush_delayed_work_sync(&rtd->delayed_work);
1846 }
1847
1848 /* remove auxiliary devices */
1849 for (i = 0; i < card->num_aux_devs; i++)
1850 soc_remove_aux_dev(card, i);
1851
1852 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001853 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301854
1855 soc_cleanup_card_debugfs(card);
1856
1857 /* remove the card */
1858 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001859 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301860
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001861 snd_soc_dapm_free(&card->dapm);
1862
Vinod Koulb0e26482011-01-13 22:48:02 +05301863 snd_card_free(card->snd_card);
1864 return 0;
1865
1866}
1867
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001868/* removes a socdev */
1869static int soc_remove(struct platform_device *pdev)
1870{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001871 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001872
Mark Brownc5af3a22008-11-28 13:29:45 +00001873 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001874 return 0;
1875}
1876
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001877int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001878{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001879 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001880 int i;
Mark Brown51737472009-06-22 13:16:51 +01001881
1882 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001883 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001884
1885 /* Flush out pmdown_time work - we actually do want to run it
1886 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001887 for (i = 0; i < card->num_rtd; i++) {
1888 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001889 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001890 }
Mark Brown51737472009-06-22 13:16:51 +01001891
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001892 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001893
1894 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001895}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001896EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001897
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001898const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301899 .suspend = snd_soc_suspend,
1900 .resume = snd_soc_resume,
1901 .freeze = snd_soc_suspend,
1902 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001903 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301904 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001905};
Stephen Warrendeb26072011-04-05 19:35:30 -06001906EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001907
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001908/* ASoC platform driver */
1909static struct platform_driver soc_driver = {
1910 .driver = {
1911 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001912 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001913 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001914 },
1915 .probe = soc_probe,
1916 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001917};
1918
Mark Brown096e49d2009-07-05 15:12:22 +01001919/**
1920 * snd_soc_codec_volatile_register: Report if a register is volatile.
1921 *
1922 * @codec: CODEC to query.
1923 * @reg: Register to query.
1924 *
1925 * Boolean function indiciating if a CODEC register is volatile.
1926 */
Mark Brown181e0552011-01-24 14:05:25 +00001927int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1928 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001929{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001930 if (codec->volatile_register)
1931 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001932 else
1933 return 0;
1934}
1935EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1936
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001937/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001938 * snd_soc_codec_readable_register: Report if a register is readable.
1939 *
1940 * @codec: CODEC to query.
1941 * @reg: Register to query.
1942 *
1943 * Boolean function indicating if a CODEC register is readable.
1944 */
1945int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1946 unsigned int reg)
1947{
1948 if (codec->readable_register)
1949 return codec->readable_register(codec, reg);
1950 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001951 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001952}
1953EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1954
1955/**
1956 * snd_soc_codec_writable_register: Report if a register is writable.
1957 *
1958 * @codec: CODEC to query.
1959 * @reg: Register to query.
1960 *
1961 * Boolean function indicating if a CODEC register is writable.
1962 */
1963int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1964 unsigned int reg)
1965{
1966 if (codec->writable_register)
1967 return codec->writable_register(codec, reg);
1968 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001969 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001970}
1971EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1972
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001973int snd_soc_platform_read(struct snd_soc_platform *platform,
1974 unsigned int reg)
1975{
1976 unsigned int ret;
1977
1978 if (!platform->driver->read) {
1979 dev_err(platform->dev, "platform has no read back\n");
1980 return -1;
1981 }
1982
1983 ret = platform->driver->read(platform, reg);
1984 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001985 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001986
1987 return ret;
1988}
1989EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1990
1991int snd_soc_platform_write(struct snd_soc_platform *platform,
1992 unsigned int reg, unsigned int val)
1993{
1994 if (!platform->driver->write) {
1995 dev_err(platform->dev, "platform has no write back\n");
1996 return -1;
1997 }
1998
1999 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002000 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002001 return platform->driver->write(platform, reg, val);
2002}
2003EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2004
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002005/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002006 * snd_soc_new_ac97_codec - initailise AC97 device
2007 * @codec: audio codec
2008 * @ops: AC97 bus operations
2009 * @num: AC97 codec number
2010 *
2011 * Initialises AC97 codec resources for use by ad-hoc devices only.
2012 */
2013int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2014 struct snd_ac97_bus_ops *ops, int num)
2015{
2016 mutex_lock(&codec->mutex);
2017
2018 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2019 if (codec->ac97 == NULL) {
2020 mutex_unlock(&codec->mutex);
2021 return -ENOMEM;
2022 }
2023
2024 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2025 if (codec->ac97->bus == NULL) {
2026 kfree(codec->ac97);
2027 codec->ac97 = NULL;
2028 mutex_unlock(&codec->mutex);
2029 return -ENOMEM;
2030 }
2031
2032 codec->ac97->bus->ops = ops;
2033 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002034
2035 /*
2036 * Mark the AC97 device to be created by us. This way we ensure that the
2037 * device will be registered with the device subsystem later on.
2038 */
2039 codec->ac97_created = 1;
2040
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002041 mutex_unlock(&codec->mutex);
2042 return 0;
2043}
2044EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2045
2046/**
2047 * snd_soc_free_ac97_codec - free AC97 codec device
2048 * @codec: audio codec
2049 *
2050 * Frees AC97 codec device resources.
2051 */
2052void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2053{
2054 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002055#ifdef CONFIG_SND_SOC_AC97_BUS
2056 soc_unregister_ac97_dai_link(codec);
2057#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002058 kfree(codec->ac97->bus);
2059 kfree(codec->ac97);
2060 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002061 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002062 mutex_unlock(&codec->mutex);
2063}
2064EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2065
Mark Brownc3753702010-11-01 15:41:57 -04002066unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2067{
2068 unsigned int ret;
2069
Mark Brownc3acec22010-12-02 16:15:29 +00002070 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002071 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002072 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002073
2074 return ret;
2075}
2076EXPORT_SYMBOL_GPL(snd_soc_read);
2077
2078unsigned int snd_soc_write(struct snd_soc_codec *codec,
2079 unsigned int reg, unsigned int val)
2080{
2081 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002082 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002083 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002084}
2085EXPORT_SYMBOL_GPL(snd_soc_write);
2086
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00002087unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2088 unsigned int reg, const void *data, size_t len)
2089{
2090 return codec->bulk_write_raw(codec, reg, data, len);
2091}
2092EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2093
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002094/**
2095 * snd_soc_update_bits - update codec register bits
2096 * @codec: audio codec
2097 * @reg: codec register
2098 * @mask: register mask
2099 * @value: new value
2100 *
2101 * Writes new register value.
2102 *
Timur Tabi180c3292011-01-10 15:58:13 -06002103 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002104 */
2105int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002106 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002107{
Mark Brown8a713da2011-12-03 12:33:55 +00002108 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002109 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002110 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002111
Mark Brown8a713da2011-12-03 12:33:55 +00002112 if (codec->using_regmap) {
2113 ret = regmap_update_bits_check(codec->control_data, reg,
2114 mask, value, &change);
2115 } else {
2116 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06002117 if (ret < 0)
2118 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00002119
2120 old = ret;
2121 new = (old & ~mask) | (value & mask);
2122 change = old != new;
2123 if (change)
2124 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06002125 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002126
Mark Brown8a713da2011-12-03 12:33:55 +00002127 if (ret < 0)
2128 return ret;
2129
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002130 return change;
2131}
2132EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2133
2134/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002135 * snd_soc_update_bits_locked - update codec register bits
2136 * @codec: audio codec
2137 * @reg: codec register
2138 * @mask: register mask
2139 * @value: new value
2140 *
2141 * Writes new register value, and takes the codec mutex.
2142 *
2143 * Returns 1 for change else 0.
2144 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002145int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2146 unsigned short reg, unsigned int mask,
2147 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002148{
2149 int change;
2150
2151 mutex_lock(&codec->mutex);
2152 change = snd_soc_update_bits(codec, reg, mask, value);
2153 mutex_unlock(&codec->mutex);
2154
2155 return change;
2156}
Mark Browndd1b3d52009-12-04 14:22:03 +00002157EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002158
2159/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002160 * snd_soc_test_bits - test register for change
2161 * @codec: audio codec
2162 * @reg: codec register
2163 * @mask: register mask
2164 * @value: new value
2165 *
2166 * Tests a register with a new value and checks if the new value is
2167 * different from the old value.
2168 *
2169 * Returns 1 for change else 0.
2170 */
2171int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002172 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002173{
2174 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002175 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002176
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002177 old = snd_soc_read(codec, reg);
2178 new = (old & ~mask) | value;
2179 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002180
2181 return change;
2182}
2183EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2184
2185/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002186 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2187 * @substream: the pcm substream
2188 * @hw: the hardware parameters
2189 *
2190 * Sets the substream runtime hardware parameters.
2191 */
2192int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2193 const struct snd_pcm_hardware *hw)
2194{
2195 struct snd_pcm_runtime *runtime = substream->runtime;
2196 runtime->hw.info = hw->info;
2197 runtime->hw.formats = hw->formats;
2198 runtime->hw.period_bytes_min = hw->period_bytes_min;
2199 runtime->hw.period_bytes_max = hw->period_bytes_max;
2200 runtime->hw.periods_min = hw->periods_min;
2201 runtime->hw.periods_max = hw->periods_max;
2202 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2203 runtime->hw.fifo_size = hw->fifo_size;
2204 return 0;
2205}
2206EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2207
2208/**
2209 * snd_soc_cnew - create new control
2210 * @_template: control template
2211 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002212 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002213 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002214 *
2215 * Create a new mixer control from a template control.
2216 *
2217 * Returns 0 for success, else error.
2218 */
2219struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002220 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002221 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002222{
2223 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002224 struct snd_kcontrol *kcontrol;
2225 char *name = NULL;
2226 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002227
2228 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002229 template.index = 0;
2230
Mark Brownefb7ac32011-03-08 17:23:24 +00002231 if (!long_name)
2232 long_name = template.name;
2233
2234 if (prefix) {
2235 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08002236 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00002237 if (!name)
2238 return NULL;
2239
2240 snprintf(name, name_len, "%s %s", prefix, long_name);
2241
2242 template.name = name;
2243 } else {
2244 template.name = long_name;
2245 }
2246
2247 kcontrol = snd_ctl_new1(&template, data);
2248
2249 kfree(name);
2250
2251 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002252}
2253EXPORT_SYMBOL_GPL(snd_soc_cnew);
2254
Liam Girdwood022658b2012-02-03 17:43:09 +00002255static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2256 const struct snd_kcontrol_new *controls, int num_controls,
2257 const char *prefix, void *data)
2258{
2259 int err, i;
2260
2261 for (i = 0; i < num_controls; i++) {
2262 const struct snd_kcontrol_new *control = &controls[i];
2263 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2264 control->name, prefix));
2265 if (err < 0) {
2266 dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2267 return err;
2268 }
2269 }
2270
2271 return 0;
2272}
2273
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002274/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002275 * snd_soc_add_codec_controls - add an array of controls to a codec.
2276 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002277 * duplicating this code.
2278 *
2279 * @codec: codec to add controls to
2280 * @controls: array of controls to add
2281 * @num_controls: number of elements in the array
2282 *
2283 * Return 0 for success, else error.
2284 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002285int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002286 const struct snd_kcontrol_new *controls, int num_controls)
2287{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002288 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002289
Liam Girdwood022658b2012-02-03 17:43:09 +00002290 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2291 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002292}
Liam Girdwood022658b2012-02-03 17:43:09 +00002293EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002294
2295/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002296 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002297 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002298 *
2299 * @platform: platform to add controls to
2300 * @controls: array of controls to add
2301 * @num_controls: number of elements in the array
2302 *
2303 * Return 0 for success, else error.
2304 */
2305int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2306 const struct snd_kcontrol_new *controls, int num_controls)
2307{
2308 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002309
Liam Girdwood022658b2012-02-03 17:43:09 +00002310 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2311 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002312}
2313EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2314
2315/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002316 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2317 * Convenience function to add a list of controls.
2318 *
2319 * @soc_card: SoC card to add controls to
2320 * @controls: array of controls to add
2321 * @num_controls: number of elements in the array
2322 *
2323 * Return 0 for success, else error.
2324 */
2325int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2326 const struct snd_kcontrol_new *controls, int num_controls)
2327{
2328 struct snd_card *card = soc_card->snd_card;
2329
2330 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2331 NULL, soc_card);
2332}
2333EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2334
2335/**
2336 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2337 * Convienience function to add a list of controls.
2338 *
2339 * @dai: DAI to add controls to
2340 * @controls: array of controls to add
2341 * @num_controls: number of elements in the array
2342 *
2343 * Return 0 for success, else error.
2344 */
2345int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2346 const struct snd_kcontrol_new *controls, int num_controls)
2347{
2348 struct snd_card *card = dai->card->snd_card;
2349
2350 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2351 NULL, dai);
2352}
2353EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2354
2355/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002356 * snd_soc_info_enum_double - enumerated double mixer info callback
2357 * @kcontrol: mixer control
2358 * @uinfo: control element information
2359 *
2360 * Callback to provide information about a double enumerated
2361 * mixer control.
2362 *
2363 * Returns 0 for success.
2364 */
2365int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2366 struct snd_ctl_elem_info *uinfo)
2367{
2368 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2369
2370 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2371 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002372 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002373
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002374 if (uinfo->value.enumerated.item > e->max - 1)
2375 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002376 strcpy(uinfo->value.enumerated.name,
2377 e->texts[uinfo->value.enumerated.item]);
2378 return 0;
2379}
2380EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2381
2382/**
2383 * snd_soc_get_enum_double - enumerated double mixer get callback
2384 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002385 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002386 *
2387 * Callback to get the value of a double enumerated mixer.
2388 *
2389 * Returns 0 for success.
2390 */
2391int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2392 struct snd_ctl_elem_value *ucontrol)
2393{
2394 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2395 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002396 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002397
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002398 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002399 ;
2400 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002401 ucontrol->value.enumerated.item[0]
2402 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002403 if (e->shift_l != e->shift_r)
2404 ucontrol->value.enumerated.item[1] =
2405 (val >> e->shift_r) & (bitmask - 1);
2406
2407 return 0;
2408}
2409EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2410
2411/**
2412 * snd_soc_put_enum_double - enumerated double mixer put callback
2413 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002414 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002415 *
2416 * Callback to set the value of a double enumerated mixer.
2417 *
2418 * Returns 0 for success.
2419 */
2420int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2421 struct snd_ctl_elem_value *ucontrol)
2422{
2423 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2424 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002425 unsigned int val;
2426 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002427
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002428 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002429 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002430 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002431 return -EINVAL;
2432 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2433 mask = (bitmask - 1) << e->shift_l;
2434 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002435 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002436 return -EINVAL;
2437 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2438 mask |= (bitmask - 1) << e->shift_r;
2439 }
2440
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002441 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002442}
2443EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2444
2445/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002446 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2447 * @kcontrol: mixer control
2448 * @ucontrol: control element information
2449 *
2450 * Callback to get the value of a double semi enumerated mixer.
2451 *
2452 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2453 * used for handling bitfield coded enumeration for example.
2454 *
2455 * Returns 0 for success.
2456 */
2457int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2458 struct snd_ctl_elem_value *ucontrol)
2459{
2460 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002461 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002462 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002463
2464 reg_val = snd_soc_read(codec, e->reg);
2465 val = (reg_val >> e->shift_l) & e->mask;
2466 for (mux = 0; mux < e->max; mux++) {
2467 if (val == e->values[mux])
2468 break;
2469 }
2470 ucontrol->value.enumerated.item[0] = mux;
2471 if (e->shift_l != e->shift_r) {
2472 val = (reg_val >> e->shift_r) & e->mask;
2473 for (mux = 0; mux < e->max; mux++) {
2474 if (val == e->values[mux])
2475 break;
2476 }
2477 ucontrol->value.enumerated.item[1] = mux;
2478 }
2479
2480 return 0;
2481}
2482EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2483
2484/**
2485 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2486 * @kcontrol: mixer control
2487 * @ucontrol: control element information
2488 *
2489 * Callback to set the value of a double semi enumerated mixer.
2490 *
2491 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2492 * used for handling bitfield coded enumeration for example.
2493 *
2494 * Returns 0 for success.
2495 */
2496int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2497 struct snd_ctl_elem_value *ucontrol)
2498{
2499 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002500 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002501 unsigned int val;
2502 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002503
2504 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2505 return -EINVAL;
2506 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2507 mask = e->mask << e->shift_l;
2508 if (e->shift_l != e->shift_r) {
2509 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2510 return -EINVAL;
2511 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2512 mask |= e->mask << e->shift_r;
2513 }
2514
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002515 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002516}
2517EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2518
2519/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002520 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2521 * @kcontrol: mixer control
2522 * @uinfo: control element information
2523 *
2524 * Callback to provide information about an external enumerated
2525 * single mixer.
2526 *
2527 * Returns 0 for success.
2528 */
2529int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2530 struct snd_ctl_elem_info *uinfo)
2531{
2532 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2533
2534 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2535 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002536 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002537
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002538 if (uinfo->value.enumerated.item > e->max - 1)
2539 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002540 strcpy(uinfo->value.enumerated.name,
2541 e->texts[uinfo->value.enumerated.item]);
2542 return 0;
2543}
2544EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2545
2546/**
2547 * snd_soc_info_volsw_ext - external single mixer info callback
2548 * @kcontrol: mixer control
2549 * @uinfo: control element information
2550 *
2551 * Callback to provide information about a single external mixer control.
2552 *
2553 * Returns 0 for success.
2554 */
2555int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2556 struct snd_ctl_elem_info *uinfo)
2557{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002558 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002559
Mark Brownfd5dfad2009-04-15 21:37:46 +01002560 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002561 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2562 else
2563 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2564
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002565 uinfo->count = 1;
2566 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002567 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002568 return 0;
2569}
2570EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2571
2572/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002573 * snd_soc_info_volsw - single mixer info callback
2574 * @kcontrol: mixer control
2575 * @uinfo: control element information
2576 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002577 * Callback to provide information about a single mixer control, or a double
2578 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002579 *
2580 * Returns 0 for success.
2581 */
2582int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2583 struct snd_ctl_elem_info *uinfo)
2584{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002585 struct soc_mixer_control *mc =
2586 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002587 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002588
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002589 if (!mc->platform_max)
2590 mc->platform_max = mc->max;
2591 platform_max = mc->platform_max;
2592
2593 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002594 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2595 else
2596 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2597
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002598 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002599 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002600 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002601 return 0;
2602}
2603EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2604
2605/**
2606 * snd_soc_get_volsw - single mixer get callback
2607 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002608 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002609 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002610 * Callback to get the value of a single mixer control, or a double mixer
2611 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002612 *
2613 * Returns 0 for success.
2614 */
2615int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2616 struct snd_ctl_elem_value *ucontrol)
2617{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002618 struct soc_mixer_control *mc =
2619 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002620 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002621 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002622 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002623 unsigned int shift = mc->shift;
2624 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002625 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002626 unsigned int mask = (1 << fls(max)) - 1;
2627 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002628
2629 ucontrol->value.integer.value[0] =
2630 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002631 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002632 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002633 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002634
2635 if (snd_soc_volsw_is_stereo(mc)) {
2636 if (reg == reg2)
2637 ucontrol->value.integer.value[1] =
2638 (snd_soc_read(codec, reg) >> rshift) & mask;
2639 else
2640 ucontrol->value.integer.value[1] =
2641 (snd_soc_read(codec, reg2) >> shift) & mask;
2642 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002643 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002644 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002645 }
2646
2647 return 0;
2648}
2649EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2650
2651/**
2652 * snd_soc_put_volsw - single mixer put callback
2653 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002654 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002655 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002656 * Callback to set the value of a single mixer control, or a double mixer
2657 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002658 *
2659 * Returns 0 for success.
2660 */
2661int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2662 struct snd_ctl_elem_value *ucontrol)
2663{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002664 struct soc_mixer_control *mc =
2665 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002666 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002667 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002668 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002669 unsigned int shift = mc->shift;
2670 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002671 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002672 unsigned int mask = (1 << fls(max)) - 1;
2673 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002674 int err;
2675 bool type_2r = 0;
2676 unsigned int val2 = 0;
2677 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002678
2679 val = (ucontrol->value.integer.value[0] & mask);
2680 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002681 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002682 val_mask = mask << shift;
2683 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002684 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002685 val2 = (ucontrol->value.integer.value[1] & mask);
2686 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002687 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002688 if (reg == reg2) {
2689 val_mask |= mask << rshift;
2690 val |= val2 << rshift;
2691 } else {
2692 val2 = val2 << shift;
2693 type_2r = 1;
2694 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002695 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002696 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002697 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002698 return err;
2699
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002700 if (type_2r)
2701 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2702
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002703 return err;
2704}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002705EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002706
Mark Browne13ac2e2008-05-28 17:58:05 +01002707/**
Brian Austin1d99f242012-03-30 10:43:55 -05002708 * snd_soc_get_volsw_sx - single mixer get callback
2709 * @kcontrol: mixer control
2710 * @ucontrol: control element information
2711 *
2712 * Callback to get the value of a single mixer control, or a double mixer
2713 * control that spans 2 registers.
2714 *
2715 * Returns 0 for success.
2716 */
2717int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2718 struct snd_ctl_elem_value *ucontrol)
2719{
2720 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2721 struct soc_mixer_control *mc =
2722 (struct soc_mixer_control *)kcontrol->private_value;
2723
2724 unsigned int reg = mc->reg;
2725 unsigned int reg2 = mc->rreg;
2726 unsigned int shift = mc->shift;
2727 unsigned int rshift = mc->rshift;
2728 int max = mc->max;
2729 int min = mc->min;
2730 int mask = (1 << (fls(min + max) - 1)) - 1;
2731
2732 ucontrol->value.integer.value[0] =
2733 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2734
2735 if (snd_soc_volsw_is_stereo(mc))
2736 ucontrol->value.integer.value[1] =
2737 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2738
2739 return 0;
2740}
2741EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2742
2743/**
2744 * snd_soc_put_volsw_sx - double mixer set callback
2745 * @kcontrol: mixer control
2746 * @uinfo: control element information
2747 *
2748 * Callback to set the value of a double mixer control that spans 2 registers.
2749 *
2750 * Returns 0 for success.
2751 */
2752int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2753 struct snd_ctl_elem_value *ucontrol)
2754{
2755 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2756 struct soc_mixer_control *mc =
2757 (struct soc_mixer_control *)kcontrol->private_value;
2758
2759 unsigned int reg = mc->reg;
2760 unsigned int reg2 = mc->rreg;
2761 unsigned int shift = mc->shift;
2762 unsigned int rshift = mc->rshift;
2763 int max = mc->max;
2764 int min = mc->min;
2765 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002766 int err = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002767 unsigned short val, val_mask, val2 = 0;
2768
2769 val_mask = mask << shift;
2770 val = (ucontrol->value.integer.value[0] + min) & mask;
2771 val = val << shift;
2772
2773 if (snd_soc_update_bits_locked(codec, reg, val_mask, val))
2774 return err;
2775
2776 if (snd_soc_volsw_is_stereo(mc)) {
2777 val_mask = mask << rshift;
2778 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2779 val2 = val2 << rshift;
2780
2781 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2782 return err;
2783 }
2784 return 0;
2785}
2786EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2787
2788/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002789 * snd_soc_info_volsw_s8 - signed mixer info callback
2790 * @kcontrol: mixer control
2791 * @uinfo: control element information
2792 *
2793 * Callback to provide information about a signed mixer control.
2794 *
2795 * Returns 0 for success.
2796 */
2797int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2798 struct snd_ctl_elem_info *uinfo)
2799{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002800 struct soc_mixer_control *mc =
2801 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002802 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002803 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002804
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002805 if (!mc->platform_max)
2806 mc->platform_max = mc->max;
2807 platform_max = mc->platform_max;
2808
Mark Browne13ac2e2008-05-28 17:58:05 +01002809 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2810 uinfo->count = 2;
2811 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002812 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002813 return 0;
2814}
2815EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2816
2817/**
2818 * snd_soc_get_volsw_s8 - signed mixer get callback
2819 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002820 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002821 *
2822 * Callback to get the value of a signed mixer control.
2823 *
2824 * Returns 0 for success.
2825 */
2826int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2827 struct snd_ctl_elem_value *ucontrol)
2828{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002829 struct soc_mixer_control *mc =
2830 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002831 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002832 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002833 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002834 int val = snd_soc_read(codec, reg);
2835
2836 ucontrol->value.integer.value[0] =
2837 ((signed char)(val & 0xff))-min;
2838 ucontrol->value.integer.value[1] =
2839 ((signed char)((val >> 8) & 0xff))-min;
2840 return 0;
2841}
2842EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2843
2844/**
2845 * snd_soc_put_volsw_sgn - signed mixer put callback
2846 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002847 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002848 *
2849 * Callback to set the value of a signed mixer control.
2850 *
2851 * Returns 0 for success.
2852 */
2853int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2854 struct snd_ctl_elem_value *ucontrol)
2855{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002856 struct soc_mixer_control *mc =
2857 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002858 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002859 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002860 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002861 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002862
2863 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2864 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2865
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002866 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002867}
2868EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2869
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002870/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002871 * snd_soc_info_volsw_range - single mixer info callback with range.
2872 * @kcontrol: mixer control
2873 * @uinfo: control element information
2874 *
2875 * Callback to provide information, within a range, about a single
2876 * mixer control.
2877 *
2878 * returns 0 for success.
2879 */
2880int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2881 struct snd_ctl_elem_info *uinfo)
2882{
2883 struct soc_mixer_control *mc =
2884 (struct soc_mixer_control *)kcontrol->private_value;
2885 int platform_max;
2886 int min = mc->min;
2887
2888 if (!mc->platform_max)
2889 mc->platform_max = mc->max;
2890 platform_max = mc->platform_max;
2891
2892 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2893 uinfo->count = 1;
2894 uinfo->value.integer.min = 0;
2895 uinfo->value.integer.max = platform_max - min;
2896
2897 return 0;
2898}
2899EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2900
2901/**
2902 * snd_soc_put_volsw_range - single mixer put value callback with range.
2903 * @kcontrol: mixer control
2904 * @ucontrol: control element information
2905 *
2906 * Callback to set the value, within a range, for a single mixer control.
2907 *
2908 * Returns 0 for success.
2909 */
2910int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2911 struct snd_ctl_elem_value *ucontrol)
2912{
2913 struct soc_mixer_control *mc =
2914 (struct soc_mixer_control *)kcontrol->private_value;
2915 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2916 unsigned int reg = mc->reg;
2917 unsigned int shift = mc->shift;
2918 int min = mc->min;
2919 int max = mc->max;
2920 unsigned int mask = (1 << fls(max)) - 1;
2921 unsigned int invert = mc->invert;
2922 unsigned int val, val_mask;
2923
2924 val = ((ucontrol->value.integer.value[0] + min) & mask);
2925 if (invert)
2926 val = max - val;
2927 val_mask = mask << shift;
2928 val = val << shift;
2929
2930 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2931}
2932EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2933
2934/**
2935 * snd_soc_get_volsw_range - single mixer get callback with range
2936 * @kcontrol: mixer control
2937 * @ucontrol: control element information
2938 *
2939 * Callback to get the value, within a range, of a single mixer control.
2940 *
2941 * Returns 0 for success.
2942 */
2943int snd_soc_get_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;
2950 unsigned int shift = mc->shift;
2951 int min = mc->min;
2952 int max = mc->max;
2953 unsigned int mask = (1 << fls(max)) - 1;
2954 unsigned int invert = mc->invert;
2955
2956 ucontrol->value.integer.value[0] =
2957 (snd_soc_read(codec, reg) >> shift) & mask;
2958 if (invert)
2959 ucontrol->value.integer.value[0] =
2960 max - ucontrol->value.integer.value[0];
2961 ucontrol->value.integer.value[0] =
2962 ucontrol->value.integer.value[0] - min;
2963
2964 return 0;
2965}
2966EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2967
2968/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002969 * snd_soc_limit_volume - Set new limit to an existing volume control.
2970 *
2971 * @codec: where to look for the control
2972 * @name: Name of the control
2973 * @max: new maximum limit
2974 *
2975 * Return 0 for success, else error.
2976 */
2977int snd_soc_limit_volume(struct snd_soc_codec *codec,
2978 const char *name, int max)
2979{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002980 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002981 struct snd_kcontrol *kctl;
2982 struct soc_mixer_control *mc;
2983 int found = 0;
2984 int ret = -EINVAL;
2985
2986 /* Sanity check for name and max */
2987 if (unlikely(!name || max <= 0))
2988 return -EINVAL;
2989
2990 list_for_each_entry(kctl, &card->controls, list) {
2991 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2992 found = 1;
2993 break;
2994 }
2995 }
2996 if (found) {
2997 mc = (struct soc_mixer_control *)kctl->private_value;
2998 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002999 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003000 ret = 0;
3001 }
3002 }
3003 return ret;
3004}
3005EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3006
Mark Brown71d08512011-10-10 18:31:26 +01003007int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3008 struct snd_ctl_elem_info *uinfo)
3009{
3010 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3011 struct soc_bytes *params = (void *)kcontrol->private_value;
3012
3013 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3014 uinfo->count = params->num_regs * codec->val_bytes;
3015
3016 return 0;
3017}
3018EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3019
3020int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3021 struct snd_ctl_elem_value *ucontrol)
3022{
3023 struct soc_bytes *params = (void *)kcontrol->private_value;
3024 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3025 int ret;
3026
3027 if (codec->using_regmap)
3028 ret = regmap_raw_read(codec->control_data, params->base,
3029 ucontrol->value.bytes.data,
3030 params->num_regs * codec->val_bytes);
3031 else
3032 ret = -EINVAL;
3033
Mark Brownf831b052012-02-17 16:20:33 -08003034 /* Hide any masked bytes to ensure consistent data reporting */
3035 if (ret == 0 && params->mask) {
3036 switch (codec->val_bytes) {
3037 case 1:
3038 ucontrol->value.bytes.data[0] &= ~params->mask;
3039 break;
3040 case 2:
3041 ((u16 *)(&ucontrol->value.bytes.data))[0]
3042 &= ~params->mask;
3043 break;
3044 case 4:
3045 ((u32 *)(&ucontrol->value.bytes.data))[0]
3046 &= ~params->mask;
3047 break;
3048 default:
3049 return -EINVAL;
3050 }
3051 }
3052
Mark Brown71d08512011-10-10 18:31:26 +01003053 return ret;
3054}
3055EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3056
3057int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3058 struct snd_ctl_elem_value *ucontrol)
3059{
3060 struct soc_bytes *params = (void *)kcontrol->private_value;
3061 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08003062 int ret, len;
3063 unsigned int val;
3064 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003065
Mark Brownf831b052012-02-17 16:20:33 -08003066 if (!codec->using_regmap)
3067 return -EINVAL;
3068
3069 data = ucontrol->value.bytes.data;
3070 len = params->num_regs * codec->val_bytes;
3071
3072 /*
3073 * If we've got a mask then we need to preserve the register
3074 * bits. We shouldn't modify the incoming data so take a
3075 * copy.
3076 */
3077 if (params->mask) {
3078 ret = regmap_read(codec->control_data, params->base, &val);
3079 if (ret != 0)
3080 return ret;
3081
3082 val &= params->mask;
3083
3084 data = kmemdup(data, len, GFP_KERNEL);
3085 if (!data)
3086 return -ENOMEM;
3087
3088 switch (codec->val_bytes) {
3089 case 1:
3090 ((u8 *)data)[0] &= ~params->mask;
3091 ((u8 *)data)[0] |= val;
3092 break;
3093 case 2:
3094 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3095 ((u16 *)data)[0] |= cpu_to_be16(val);
3096 break;
3097 case 4:
3098 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3099 ((u32 *)data)[0] |= cpu_to_be32(val);
3100 break;
3101 default:
3102 return -EINVAL;
3103 }
3104 }
3105
3106 ret = regmap_raw_write(codec->control_data, params->base,
3107 data, len);
3108
3109 if (params->mask)
3110 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003111
3112 return ret;
3113}
3114EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3115
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003116/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003117 * snd_soc_info_xr_sx - signed multi register info callback
3118 * @kcontrol: mreg control
3119 * @uinfo: control element information
3120 *
3121 * Callback to provide information of a control that can
3122 * span multiple codec registers which together
3123 * forms a single signed value in a MSB/LSB manner.
3124 *
3125 * Returns 0 for success.
3126 */
3127int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3128 struct snd_ctl_elem_info *uinfo)
3129{
3130 struct soc_mreg_control *mc =
3131 (struct soc_mreg_control *)kcontrol->private_value;
3132 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3133 uinfo->count = 1;
3134 uinfo->value.integer.min = mc->min;
3135 uinfo->value.integer.max = mc->max;
3136
3137 return 0;
3138}
3139EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3140
3141/**
3142 * snd_soc_get_xr_sx - signed multi register get callback
3143 * @kcontrol: mreg control
3144 * @ucontrol: control element information
3145 *
3146 * Callback to get the value of a control that can span
3147 * multiple codec registers which together forms a single
3148 * signed value in a MSB/LSB manner. The control supports
3149 * specifying total no of bits used to allow for bitfields
3150 * across the multiple codec registers.
3151 *
3152 * Returns 0 for success.
3153 */
3154int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3155 struct snd_ctl_elem_value *ucontrol)
3156{
3157 struct soc_mreg_control *mc =
3158 (struct soc_mreg_control *)kcontrol->private_value;
3159 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3160 unsigned int regbase = mc->regbase;
3161 unsigned int regcount = mc->regcount;
3162 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3163 unsigned int regwmask = (1<<regwshift)-1;
3164 unsigned int invert = mc->invert;
3165 unsigned long mask = (1UL<<mc->nbits)-1;
3166 long min = mc->min;
3167 long max = mc->max;
3168 long val = 0;
3169 unsigned long regval;
3170 unsigned int i;
3171
3172 for (i = 0; i < regcount; i++) {
3173 regval = snd_soc_read(codec, regbase+i) & regwmask;
3174 val |= regval << (regwshift*(regcount-i-1));
3175 }
3176 val &= mask;
3177 if (min < 0 && val > max)
3178 val |= ~mask;
3179 if (invert)
3180 val = max - val;
3181 ucontrol->value.integer.value[0] = val;
3182
3183 return 0;
3184}
3185EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3186
3187/**
3188 * snd_soc_put_xr_sx - signed multi register get callback
3189 * @kcontrol: mreg control
3190 * @ucontrol: control element information
3191 *
3192 * Callback to set the value of a control that can span
3193 * multiple codec registers which together forms a single
3194 * signed value in a MSB/LSB manner. The control supports
3195 * specifying total no of bits used to allow for bitfields
3196 * across the multiple codec registers.
3197 *
3198 * Returns 0 for success.
3199 */
3200int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3201 struct snd_ctl_elem_value *ucontrol)
3202{
3203 struct soc_mreg_control *mc =
3204 (struct soc_mreg_control *)kcontrol->private_value;
3205 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3206 unsigned int regbase = mc->regbase;
3207 unsigned int regcount = mc->regcount;
3208 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3209 unsigned int regwmask = (1<<regwshift)-1;
3210 unsigned int invert = mc->invert;
3211 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003212 long max = mc->max;
3213 long val = ucontrol->value.integer.value[0];
3214 unsigned int i, regval, regmask;
3215 int err;
3216
3217 if (invert)
3218 val = max - val;
3219 val &= mask;
3220 for (i = 0; i < regcount; i++) {
3221 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3222 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3223 err = snd_soc_update_bits_locked(codec, regbase+i,
3224 regmask, regval);
3225 if (err < 0)
3226 return err;
3227 }
3228
3229 return 0;
3230}
3231EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3232
3233/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003234 * snd_soc_get_strobe - strobe get callback
3235 * @kcontrol: mixer control
3236 * @ucontrol: control element information
3237 *
3238 * Callback get the value of a strobe mixer control.
3239 *
3240 * Returns 0 for success.
3241 */
3242int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3243 struct snd_ctl_elem_value *ucontrol)
3244{
3245 struct soc_mixer_control *mc =
3246 (struct soc_mixer_control *)kcontrol->private_value;
3247 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3248 unsigned int reg = mc->reg;
3249 unsigned int shift = mc->shift;
3250 unsigned int mask = 1 << shift;
3251 unsigned int invert = mc->invert != 0;
3252 unsigned int val = snd_soc_read(codec, reg) & mask;
3253
3254 if (shift != 0 && val != 0)
3255 val = val >> shift;
3256 ucontrol->value.enumerated.item[0] = val ^ invert;
3257
3258 return 0;
3259}
3260EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3261
3262/**
3263 * snd_soc_put_strobe - strobe put callback
3264 * @kcontrol: mixer control
3265 * @ucontrol: control element information
3266 *
3267 * Callback strobe a register bit to high then low (or the inverse)
3268 * in one pass of a single mixer enum control.
3269 *
3270 * Returns 1 for success.
3271 */
3272int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3273 struct snd_ctl_elem_value *ucontrol)
3274{
3275 struct soc_mixer_control *mc =
3276 (struct soc_mixer_control *)kcontrol->private_value;
3277 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3278 unsigned int reg = mc->reg;
3279 unsigned int shift = mc->shift;
3280 unsigned int mask = 1 << shift;
3281 unsigned int invert = mc->invert != 0;
3282 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3283 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3284 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3285 int err;
3286
3287 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3288 if (err < 0)
3289 return err;
3290
3291 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3292 return err;
3293}
3294EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3295
3296/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003297 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3298 * @dai: DAI
3299 * @clk_id: DAI specific clock ID
3300 * @freq: new clock frequency in Hz
3301 * @dir: new clock direction - input/output.
3302 *
3303 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3304 */
3305int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3306 unsigned int freq, int dir)
3307{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003308 if (dai->driver && dai->driver->ops->set_sysclk)
3309 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003310 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003311 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003312 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003313 else
3314 return -EINVAL;
3315}
3316EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3317
3318/**
Mark Brownec4ee522011-03-07 20:58:11 +00003319 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3320 * @codec: CODEC
3321 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003322 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003323 * @freq: new clock frequency in Hz
3324 * @dir: new clock direction - input/output.
3325 *
3326 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3327 */
3328int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003329 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003330{
3331 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003332 return codec->driver->set_sysclk(codec, clk_id, source,
3333 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003334 else
3335 return -EINVAL;
3336}
3337EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3338
3339/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003340 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3341 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003342 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003343 * @div: new clock divisor.
3344 *
3345 * Configures the clock dividers. This is used to derive the best DAI bit and
3346 * frame clocks from the system or master clock. It's best to set the DAI bit
3347 * and frame clocks as low as possible to save system power.
3348 */
3349int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3350 int div_id, int div)
3351{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003352 if (dai->driver && dai->driver->ops->set_clkdiv)
3353 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003354 else
3355 return -EINVAL;
3356}
3357EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3358
3359/**
3360 * snd_soc_dai_set_pll - configure DAI PLL.
3361 * @dai: DAI
3362 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003363 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003364 * @freq_in: PLL input clock frequency in Hz
3365 * @freq_out: requested PLL output clock frequency in Hz
3366 *
3367 * Configures and enables PLL to generate output clock based on input clock.
3368 */
Mark Brown85488032009-09-05 18:52:16 +01003369int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3370 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003371{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003372 if (dai->driver && dai->driver->ops->set_pll)
3373 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003374 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003375 else if (dai->codec && dai->codec->driver->set_pll)
3376 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3377 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003378 else
3379 return -EINVAL;
3380}
3381EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3382
Mark Brownec4ee522011-03-07 20:58:11 +00003383/*
3384 * snd_soc_codec_set_pll - configure codec PLL.
3385 * @codec: CODEC
3386 * @pll_id: DAI specific PLL ID
3387 * @source: DAI specific source for the PLL
3388 * @freq_in: PLL input clock frequency in Hz
3389 * @freq_out: requested PLL output clock frequency in Hz
3390 *
3391 * Configures and enables PLL to generate output clock based on input clock.
3392 */
3393int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3394 unsigned int freq_in, unsigned int freq_out)
3395{
3396 if (codec->driver->set_pll)
3397 return codec->driver->set_pll(codec, pll_id, source,
3398 freq_in, freq_out);
3399 else
3400 return -EINVAL;
3401}
3402EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3403
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003404/**
3405 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3406 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003407 * @fmt: SND_SOC_DAIFMT_ format value.
3408 *
3409 * Configures the DAI hardware format and clocking.
3410 */
3411int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3412{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003413 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003414 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003415 if (dai->driver->ops->set_fmt == NULL)
3416 return -ENOTSUPP;
3417 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003418}
3419EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3420
3421/**
3422 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3423 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003424 * @tx_mask: bitmask representing active TX slots.
3425 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003426 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003427 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003428 *
3429 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3430 * specific.
3431 */
3432int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003433 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003434{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003435 if (dai->driver && dai->driver->ops->set_tdm_slot)
3436 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003437 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003438 else
3439 return -EINVAL;
3440}
3441EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3442
3443/**
Barry Song472df3c2009-09-12 01:16:29 +08003444 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3445 * @dai: DAI
3446 * @tx_num: how many TX channels
3447 * @tx_slot: pointer to an array which imply the TX slot number channel
3448 * 0~num-1 uses
3449 * @rx_num: how many RX channels
3450 * @rx_slot: pointer to an array which imply the RX slot number channel
3451 * 0~num-1 uses
3452 *
3453 * configure the relationship between channel number and TDM slot number.
3454 */
3455int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3456 unsigned int tx_num, unsigned int *tx_slot,
3457 unsigned int rx_num, unsigned int *rx_slot)
3458{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003459 if (dai->driver && dai->driver->ops->set_channel_map)
3460 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003461 rx_num, rx_slot);
3462 else
3463 return -EINVAL;
3464}
3465EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3466
3467/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003468 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3469 * @dai: DAI
3470 * @tristate: tristate enable
3471 *
3472 * Tristates the DAI so that others can use it.
3473 */
3474int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3475{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003476 if (dai->driver && dai->driver->ops->set_tristate)
3477 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003478 else
3479 return -EINVAL;
3480}
3481EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3482
3483/**
3484 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3485 * @dai: DAI
3486 * @mute: mute enable
3487 *
3488 * Mutes the DAI DAC.
3489 */
3490int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3491{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003492 if (dai->driver && dai->driver->ops->digital_mute)
3493 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003494 else
Mark Brown04570c62012-04-13 19:16:03 +01003495 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003496}
3497EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3498
Mark Brownc5af3a22008-11-28 13:29:45 +00003499/**
3500 * snd_soc_register_card - Register a card with the ASoC core
3501 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003502 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003503 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003504 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303505int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003506{
Mark Brownb19e6e72012-03-14 21:18:39 +00003507 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003508
Mark Brownc5af3a22008-11-28 13:29:45 +00003509 if (!card->name || !card->dev)
3510 return -EINVAL;
3511
Stephen Warren5a504962011-12-21 10:40:59 -07003512 for (i = 0; i < card->num_links; i++) {
3513 struct snd_soc_dai_link *link = &card->dai_link[i];
3514
3515 /*
3516 * Codec must be specified by 1 of name or OF node,
3517 * not both or neither.
3518 */
3519 if (!!link->codec_name == !!link->codec_of_node) {
3520 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003521 "Neither/both codec name/of_node are set for %s\n",
3522 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003523 return -EINVAL;
3524 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003525 /* Codec DAI name must be specified */
3526 if (!link->codec_dai_name) {
3527 dev_err(card->dev, "codec_dai_name not set for %s\n",
3528 link->name);
3529 return -EINVAL;
3530 }
Stephen Warren5a504962011-12-21 10:40:59 -07003531
3532 /*
3533 * Platform may be specified by either name or OF node, but
3534 * can be left unspecified, and a dummy platform will be used.
3535 */
3536 if (link->platform_name && link->platform_of_node) {
3537 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003538 "Both platform name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003539 return -EINVAL;
3540 }
3541
3542 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003543 * CPU device may be specified by either name or OF node, but
3544 * can be left unspecified, and will be matched based on DAI
3545 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003546 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003547 if (link->cpu_name && link->cpu_of_node) {
Stephen Warren5a504962011-12-21 10:40:59 -07003548 dev_err(card->dev,
Stephen Warrenbc926572012-05-25 18:22:11 -06003549 "Neither/both cpu name/of_node are set for %s\n",
3550 link->name);
3551 return -EINVAL;
3552 }
3553 /*
3554 * At least one of CPU DAI name or CPU device name/node must be
3555 * specified
3556 */
3557 if (!link->cpu_dai_name &&
3558 !(link->cpu_name || link->cpu_of_node)) {
3559 dev_err(card->dev,
3560 "Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003561 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003562 return -EINVAL;
3563 }
3564 }
3565
Mark Browned77cc12011-05-03 18:25:34 +01003566 dev_set_drvdata(card->dev, card);
3567
Stephen Warren111c6412011-01-28 14:26:35 -07003568 snd_soc_initialize_card_lists(card);
3569
Vinod Koul150dd2f2011-01-13 22:48:32 +05303570 soc_init_card_debugfs(card);
3571
Mark Brown181a6892012-03-14 20:18:49 +00003572 card->rtd = devm_kzalloc(card->dev,
3573 sizeof(struct snd_soc_pcm_runtime) *
3574 (card->num_links + card->num_aux_devs),
3575 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003576 if (card->rtd == NULL)
3577 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003578 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003579 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003580
3581 for (i = 0; i < card->num_links; i++)
3582 card->rtd[i].dai_link = &card->dai_link[i];
3583
Mark Brownc5af3a22008-11-28 13:29:45 +00003584 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003585 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003586 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003587 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003588 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003589
Mark Brownb19e6e72012-03-14 21:18:39 +00003590 ret = snd_soc_instantiate_card(card);
3591 if (ret != 0)
3592 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003593
Mark Brownb19e6e72012-03-14 21:18:39 +00003594 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003595}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303596EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003597
3598/**
3599 * snd_soc_unregister_card - Unregister a card with the ASoC core
3600 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003601 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003602 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003603 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303604int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003605{
Vinod Koulb0e26482011-01-13 22:48:02 +05303606 if (card->instantiated)
3607 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003608 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3609
3610 return 0;
3611}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303612EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003613
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003614/*
3615 * Simplify DAI link configuration by removing ".-1" from device names
3616 * and sanitizing names.
3617 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003618static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003619{
3620 char *found, name[NAME_SIZE];
3621 int id1, id2;
3622
3623 if (dev_name(dev) == NULL)
3624 return NULL;
3625
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003626 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003627
3628 /* are we a "%s.%d" name (platform and SPI components) */
3629 found = strstr(name, dev->driver->name);
3630 if (found) {
3631 /* get ID */
3632 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3633
3634 /* discard ID from name if ID == -1 */
3635 if (*id == -1)
3636 found[strlen(dev->driver->name)] = '\0';
3637 }
3638
3639 } else {
3640 /* I2C component devices are named "bus-addr" */
3641 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3642 char tmp[NAME_SIZE];
3643
3644 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003645 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003646
3647 /* sanitize component name for DAI link creation */
3648 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003649 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003650 } else
3651 *id = 0;
3652 }
3653
3654 return kstrdup(name, GFP_KERNEL);
3655}
3656
3657/*
3658 * Simplify DAI link naming for single devices with multiple DAIs by removing
3659 * any ".-1" and using the DAI name (instead of device name).
3660 */
3661static inline char *fmt_multiple_name(struct device *dev,
3662 struct snd_soc_dai_driver *dai_drv)
3663{
3664 if (dai_drv->name == NULL) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02003665 pr_err("asoc: error - multiple DAI %s registered with no name\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003666 dev_name(dev));
3667 return NULL;
3668 }
3669
3670 return kstrdup(dai_drv->name, GFP_KERNEL);
3671}
3672
Mark Brown91151712008-11-30 23:31:24 +00003673/**
3674 * snd_soc_register_dai - Register a DAI with the ASoC core
3675 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003676 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003677 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003678int snd_soc_register_dai(struct device *dev,
3679 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003680{
Mark Brown054880f2012-04-09 17:29:19 +01003681 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003682 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003683
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003684 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003685
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003686 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3687 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003688 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003689
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003690 /* create DAI component name */
3691 dai->name = fmt_single_name(dev, &dai->id);
3692 if (dai->name == NULL) {
3693 kfree(dai);
3694 return -ENOMEM;
3695 }
3696
3697 dai->dev = dev;
3698 dai->driver = dai_drv;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003699 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003700 if (!dai->driver->ops)
3701 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003702
3703 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003704
3705 list_for_each_entry(codec, &codec_list, list) {
3706 if (codec->dev == dev) {
3707 dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3708 dai->name, codec->name);
3709 dai->codec = codec;
3710 break;
3711 }
3712 }
3713
Mark Brown91151712008-11-30 23:31:24 +00003714 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003715
Mark Brown91151712008-11-30 23:31:24 +00003716 mutex_unlock(&client_mutex);
3717
3718 pr_debug("Registered DAI '%s'\n", dai->name);
3719
3720 return 0;
3721}
3722EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3723
3724/**
3725 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3726 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003727 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003728 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003729void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003730{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003731 struct snd_soc_dai *dai;
3732
3733 list_for_each_entry(dai, &dai_list, list) {
3734 if (dev == dai->dev)
3735 goto found;
3736 }
3737 return;
3738
3739found:
Mark Brown91151712008-11-30 23:31:24 +00003740 mutex_lock(&client_mutex);
3741 list_del(&dai->list);
3742 mutex_unlock(&client_mutex);
3743
3744 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003745 kfree(dai->name);
3746 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003747}
3748EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3749
3750/**
3751 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3752 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003753 * @dai: Array of DAIs to register
3754 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003755 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003756int snd_soc_register_dais(struct device *dev,
3757 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003758{
Mark Brown054880f2012-04-09 17:29:19 +01003759 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003760 struct snd_soc_dai *dai;
3761 int i, ret = 0;
3762
Liam Girdwood720ffa42010-08-18 00:30:30 +01003763 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003764
3765 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003766
3767 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003768 if (dai == NULL) {
3769 ret = -ENOMEM;
3770 goto err;
3771 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003772
3773 /* create DAI component name */
3774 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3775 if (dai->name == NULL) {
3776 kfree(dai);
3777 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003778 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003779 }
3780
3781 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003782 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003783 if (dai->driver->id)
3784 dai->id = dai->driver->id;
3785 else
3786 dai->id = i;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003787 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003788 if (!dai->driver->ops)
3789 dai->driver->ops = &null_dai_ops;
3790
3791 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003792
3793 list_for_each_entry(codec, &codec_list, list) {
3794 if (codec->dev == dev) {
3795 dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3796 dai->name, codec->name);
3797 dai->codec = codec;
3798 break;
3799 }
3800 }
3801
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003802 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003803
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003804 mutex_unlock(&client_mutex);
3805
3806 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003807 }
3808
3809 return 0;
3810
3811err:
3812 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003813 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003814
3815 return ret;
3816}
3817EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3818
3819/**
3820 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3821 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003822 * @dai: Array of DAIs to unregister
3823 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003824 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003825void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003826{
3827 int i;
3828
3829 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003830 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003831}
3832EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3833
Mark Brown12a48a8c2008-12-03 19:40:30 +00003834/**
3835 * snd_soc_register_platform - Register a platform with the ASoC core
3836 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003837 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003838 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003839int snd_soc_register_platform(struct device *dev,
3840 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003841{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003842 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003843
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003844 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3845
3846 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3847 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003848 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003849
3850 /* create platform component name */
3851 platform->name = fmt_single_name(dev, &platform->id);
3852 if (platform->name == NULL) {
3853 kfree(platform);
3854 return -ENOMEM;
3855 }
3856
3857 platform->dev = dev;
3858 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003859 platform->dapm.dev = dev;
3860 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003861 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00003862 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003863
3864 mutex_lock(&client_mutex);
3865 list_add(&platform->list, &platform_list);
3866 mutex_unlock(&client_mutex);
3867
3868 pr_debug("Registered platform '%s'\n", platform->name);
3869
3870 return 0;
3871}
3872EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3873
3874/**
3875 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3876 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003877 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003878 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003879void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003880{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003881 struct snd_soc_platform *platform;
3882
3883 list_for_each_entry(platform, &platform_list, list) {
3884 if (dev == platform->dev)
3885 goto found;
3886 }
3887 return;
3888
3889found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003890 mutex_lock(&client_mutex);
3891 list_del(&platform->list);
3892 mutex_unlock(&client_mutex);
3893
3894 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003895 kfree(platform->name);
3896 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003897}
3898EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3899
Mark Brown151ab222009-05-09 16:22:58 +01003900static u64 codec_format_map[] = {
3901 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3902 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3903 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3904 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3905 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3906 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3907 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3908 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3909 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3910 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3911 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3912 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3913 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3914 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3915 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3916 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3917};
3918
3919/* Fix up the DAI formats for endianness: codecs don't actually see
3920 * the endianness of the data but we're using the CPU format
3921 * definitions which do need to include endianness so we ensure that
3922 * codec DAIs always have both big and little endian variants set.
3923 */
3924static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3925{
3926 int i;
3927
3928 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3929 if (stream->formats & codec_format_map[i])
3930 stream->formats |= codec_format_map[i];
3931}
3932
Mark Brown0d0cf002008-12-10 14:32:45 +00003933/**
3934 * snd_soc_register_codec - Register a codec with the ASoC core
3935 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003936 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003937 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003938int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003939 const struct snd_soc_codec_driver *codec_drv,
3940 struct snd_soc_dai_driver *dai_drv,
3941 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003942{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003943 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003944 struct snd_soc_codec *codec;
3945 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003946
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003947 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003948
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003949 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3950 if (codec == NULL)
3951 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003952
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003953 /* create CODEC component name */
3954 codec->name = fmt_single_name(dev, &codec->id);
3955 if (codec->name == NULL) {
3956 kfree(codec);
3957 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003958 }
3959
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003960 if (codec_drv->compress_type)
3961 codec->compress_type = codec_drv->compress_type;
3962 else
3963 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3964
Mark Brownc3acec22010-12-02 16:15:29 +00003965 codec->write = codec_drv->write;
3966 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003967 codec->volatile_register = codec_drv->volatile_register;
3968 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003969 codec->writable_register = codec_drv->writable_register;
Mark Brown5124e692012-02-08 13:20:50 +00003970 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003971 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3972 codec->dapm.dev = dev;
3973 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003974 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003975 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003976 codec->dev = dev;
3977 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003978 codec->num_dai = num_dai;
3979 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003980
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003981 /* allocate CODEC register cache */
3982 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003983 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003984 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003985 /* it is necessary to make a copy of the default register cache
3986 * because in the case of using a compression type that requires
3987 * the default register cache to be marked as __devinitconst the
3988 * kernel might have freed the array by the time we initialize
3989 * the cache.
3990 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003991 if (codec_drv->reg_cache_default) {
3992 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3993 reg_size, GFP_KERNEL);
3994 if (!codec->reg_def_copy) {
3995 ret = -ENOMEM;
3996 goto fail;
3997 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003998 }
3999 }
4000
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004001 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
4002 if (!codec->volatile_register)
4003 codec->volatile_register = snd_soc_default_volatile_register;
4004 if (!codec->readable_register)
4005 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004006 if (!codec->writable_register)
4007 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004008 }
4009
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004010 for (i = 0; i < num_dai; i++) {
4011 fixup_codec_formats(&dai_drv[i].playback);
4012 fixup_codec_formats(&dai_drv[i].capture);
4013 }
4014
Mark Brown054880f2012-04-09 17:29:19 +01004015 mutex_lock(&client_mutex);
4016 list_add(&codec->list, &codec_list);
4017 mutex_unlock(&client_mutex);
4018
Mark Brown26b01cc2010-08-18 20:20:55 +01004019 /* register any DAIs */
4020 if (num_dai) {
4021 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4022 if (ret < 0)
Mark Brown054880f2012-04-09 17:29:19 +01004023 dev_err(codec->dev, "Failed to regster DAIs: %d\n",
4024 ret);
Mark Brown26b01cc2010-08-18 20:20:55 +01004025 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004026
Mark Brown0d0cf002008-12-10 14:32:45 +00004027 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004028 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004029
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00004030fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004031 kfree(codec->reg_def_copy);
4032 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004033 kfree(codec->name);
4034 kfree(codec);
4035 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004036}
4037EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4038
4039/**
4040 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4041 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004042 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004043 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004044void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004045{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004046 struct snd_soc_codec *codec;
4047 int i;
4048
4049 list_for_each_entry(codec, &codec_list, list) {
4050 if (dev == codec->dev)
4051 goto found;
4052 }
4053 return;
4054
4055found:
Mark Brown26b01cc2010-08-18 20:20:55 +01004056 if (codec->num_dai)
4057 for (i = 0; i < codec->num_dai; i++)
4058 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004059
Mark Brown0d0cf002008-12-10 14:32:45 +00004060 mutex_lock(&client_mutex);
4061 list_del(&codec->list);
4062 mutex_unlock(&client_mutex);
4063
4064 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004065
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004066 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004067 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01004068 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004069 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004070}
4071EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4072
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004073/* Retrieve a card's name from device tree */
4074int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4075 const char *propname)
4076{
4077 struct device_node *np = card->dev->of_node;
4078 int ret;
4079
4080 ret = of_property_read_string_index(np, propname, 0, &card->name);
4081 /*
4082 * EINVAL means the property does not exist. This is fine providing
4083 * card->name was previously set, which is checked later in
4084 * snd_soc_register_card.
4085 */
4086 if (ret < 0 && ret != -EINVAL) {
4087 dev_err(card->dev,
4088 "Property '%s' could not be read: %d\n",
4089 propname, ret);
4090 return ret;
4091 }
4092
4093 return 0;
4094}
4095EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4096
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004097int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4098 const char *propname)
4099{
4100 struct device_node *np = card->dev->of_node;
4101 int num_routes;
4102 struct snd_soc_dapm_route *routes;
4103 int i, ret;
4104
4105 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004106 if (num_routes < 0 || num_routes & 1) {
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004107 dev_err(card->dev,
Richard Zhaoc34ce322012-04-24 15:24:43 +08004108 "Property '%s' does not exist or its length is not even\n",
4109 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004110 return -EINVAL;
4111 }
4112 num_routes /= 2;
4113 if (!num_routes) {
4114 dev_err(card->dev,
4115 "Property '%s's length is zero\n",
4116 propname);
4117 return -EINVAL;
4118 }
4119
4120 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4121 GFP_KERNEL);
4122 if (!routes) {
4123 dev_err(card->dev,
4124 "Could not allocate DAPM route table\n");
4125 return -EINVAL;
4126 }
4127
4128 for (i = 0; i < num_routes; i++) {
4129 ret = of_property_read_string_index(np, propname,
4130 2 * i, &routes[i].sink);
4131 if (ret) {
4132 dev_err(card->dev,
4133 "Property '%s' index %d could not be read: %d\n",
4134 propname, 2 * i, ret);
4135 return -EINVAL;
4136 }
4137 ret = of_property_read_string_index(np, propname,
4138 (2 * i) + 1, &routes[i].source);
4139 if (ret) {
4140 dev_err(card->dev,
4141 "Property '%s' index %d could not be read: %d\n",
4142 propname, (2 * i) + 1, ret);
4143 return -EINVAL;
4144 }
4145 }
4146
4147 card->num_dapm_routes = num_routes;
4148 card->dapm_routes = routes;
4149
4150 return 0;
4151}
4152EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4153
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004154static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004155{
Mark Brown384c89e2008-12-03 17:34:03 +00004156#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004157 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4158 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004159 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004160 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004161 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004162
Mark Brown8a9dab12011-01-10 22:25:21 +00004163 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004164 &codec_list_fops))
4165 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4166
Mark Brown8a9dab12011-01-10 22:25:21 +00004167 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004168 &dai_list_fops))
4169 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004170
Mark Brown8a9dab12011-01-10 22:25:21 +00004171 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004172 &platform_list_fops))
4173 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004174#endif
4175
Mark Brownfb257892011-04-28 10:57:54 +01004176 snd_soc_util_init();
4177
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004178 return platform_driver_register(&soc_driver);
4179}
Mark Brown4abe8e12010-10-12 17:41:03 +01004180module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004181
Mark Brown7d8c16a2008-11-30 22:11:24 +00004182static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004183{
Mark Brownfb257892011-04-28 10:57:54 +01004184 snd_soc_util_exit();
4185
Mark Brown384c89e2008-12-03 17:34:03 +00004186#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004187 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004188#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004189 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004190}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004191module_exit(snd_soc_exit);
4192
4193/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004194MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004195MODULE_DESCRIPTION("ALSA SoC Core");
4196MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004197MODULE_ALIAS("platform:soc-audio");