blob: 7b1a4fd932d742f48465301ef834fe14f7b02cdb [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>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020042#include <sound/initval.h>
43
Mark Browna8b1d342010-11-03 18:05:58 -040044#define CREATE_TRACE_POINTS
45#include <trace/events/asoc.h>
46
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000047#define NAME_SIZE 32
48
Frank Mandarinodb2a4162006-10-06 18:31:09 +020049static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
50
Mark Brown384c89e2008-12-03 17:34:03 +000051#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000052struct dentry *snd_soc_debugfs_root;
53EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000054#endif
55
Mark Brownc5af3a22008-11-28 13:29:45 +000056static DEFINE_MUTEX(client_mutex);
Mark Brown91151712008-11-30 23:31:24 +000057static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000058static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000059static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000060
Frank Mandarinodb2a4162006-10-06 18:31:09 +020061/*
62 * This is a timeout to do a DAPM powerdown after a stream is closed().
63 * It can be used to eliminate pops between different playback streams, e.g.
64 * between two audio tracks.
65 */
66static int pmdown_time = 5000;
67module_param(pmdown_time, int, 0);
68MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
69
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000070/* returns the minimum number of bytes needed to represent
71 * a particular given value */
72static int min_bytes_needed(unsigned long val)
73{
74 int c = 0;
75 int i;
76
77 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
78 if (val & (1UL << i))
79 break;
80 c = (sizeof val * 8) - c;
81 if (!c || (c % 8))
82 c = (c + 8) / 8;
83 else
84 c /= 8;
85 return c;
86}
87
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000088/* fill buf which is 'len' bytes with a formatted
89 * string of the form 'reg: value\n' */
90static int format_register_str(struct snd_soc_codec *codec,
91 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +000092{
Stephen Warren00b317a2011-04-01 14:50:44 -060093 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
94 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000095 int ret;
96 char tmpbuf[len + 1];
97 char regbuf[regsize + 1];
98
99 /* since tmpbuf is allocated on the stack, warn the callers if they
100 * try to abuse this function */
101 WARN_ON(len > 63);
102
103 /* +2 for ': ' and + 1 for '\n' */
104 if (wordsize + regsize + 2 + 1 != len)
105 return -EINVAL;
106
Mark Brown25032c12011-08-01 13:52:48 +0900107 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000108 if (ret < 0) {
109 memset(regbuf, 'X', regsize);
110 regbuf[regsize] = '\0';
111 } else {
112 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
113 }
114
115 /* prepare the buffer */
116 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
117 /* copy it back to the caller without the '\0' */
118 memcpy(buf, tmpbuf, len);
119
120 return 0;
121}
122
123/* codec register dump */
124static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
125 size_t count, loff_t pos)
126{
127 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000128 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000129 int len;
130 size_t total = 0;
131 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000132
Stephen Warren00b317a2011-04-01 14:50:44 -0600133 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
134 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000135
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000136 len = wordsize + regsize + 2 + 1;
137
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000138 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000139 return 0;
140
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000141 if (codec->driver->reg_cache_step)
142 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000143
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000144 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausenb92d1502011-08-27 18:24:14 +0200145 if (!snd_soc_codec_readable_register(codec, i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000146 continue;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000147 if (codec->driver->display_register) {
148 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000149 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100150 } else {
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000151 /* only support larger than PAGE_SIZE bytes debugfs
152 * entries for the default case */
153 if (p >= pos) {
154 if (total + len >= count - 1)
155 break;
156 format_register_str(codec, i, buf + total, len);
157 total += len;
158 }
159 p += len;
Mark Brown5164d742010-07-14 16:14:33 +0100160 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000161 }
162
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000163 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000164
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000165 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000166}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000167
Mark Brown2624d5f2009-11-03 21:56:13 +0000168static ssize_t codec_reg_show(struct device *dev,
169 struct device_attribute *attr, char *buf)
170{
Mark Brown36ae1a92012-01-06 17:12:45 -0800171 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000172
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000173 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000174}
175
176static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
177
Mark Browndbe21402010-02-12 11:37:24 +0000178static ssize_t pmdown_time_show(struct device *dev,
179 struct device_attribute *attr, char *buf)
180{
Mark Brown36ae1a92012-01-06 17:12:45 -0800181 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000182
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000183 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000184}
185
186static ssize_t pmdown_time_set(struct device *dev,
187 struct device_attribute *attr,
188 const char *buf, size_t count)
189{
Mark Brown36ae1a92012-01-06 17:12:45 -0800190 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700191 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000192
Mark Brownc593b522010-10-27 20:11:17 -0700193 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
194 if (ret)
195 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000196
197 return count;
198}
199
200static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
201
Mark Brown2624d5f2009-11-03 21:56:13 +0000202#ifdef CONFIG_DEBUG_FS
203static int codec_reg_open_file(struct inode *inode, struct file *file)
204{
205 file->private_data = inode->i_private;
206 return 0;
207}
208
209static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000210 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000211{
212 ssize_t ret;
213 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000214 char *buf;
215
216 if (*ppos < 0 || !count)
217 return -EINVAL;
218
219 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000220 if (!buf)
221 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000222
223 ret = soc_codec_reg_show(codec, buf, count, *ppos);
224 if (ret >= 0) {
225 if (copy_to_user(user_buf, buf, ret)) {
226 kfree(buf);
227 return -EFAULT;
228 }
229 *ppos += ret;
230 }
231
Mark Brown2624d5f2009-11-03 21:56:13 +0000232 kfree(buf);
233 return ret;
234}
235
236static ssize_t codec_reg_write_file(struct file *file,
237 const char __user *user_buf, size_t count, loff_t *ppos)
238{
239 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700240 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000241 char *start = buf;
242 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000243 struct snd_soc_codec *codec = file->private_data;
244
245 buf_size = min(count, (sizeof(buf)-1));
246 if (copy_from_user(buf, user_buf, buf_size))
247 return -EFAULT;
248 buf[buf_size] = 0;
249
Mark Brown2624d5f2009-11-03 21:56:13 +0000250 while (*start == ' ')
251 start++;
252 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000253 while (*start == ' ')
254 start++;
255 if (strict_strtoul(start, 16, &value))
256 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000257
258 /* Userspace has been fiddling around behind the kernel's back */
259 add_taint(TAINT_USER);
260
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000261 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000262 return buf_size;
263}
264
265static const struct file_operations codec_reg_fops = {
266 .open = codec_reg_open_file,
267 .read = codec_reg_read_file,
268 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200269 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000270};
271
272static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
273{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200274 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
275
276 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
277 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000278 if (!codec->debugfs_codec_root) {
Liam Girdwood64e60f92012-02-15 15:15:37 +0000279 dev_warn(codec->dev, "Failed to create codec debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000280 return;
281 }
282
Mark Brownaaee8ef2011-01-26 20:53:50 +0000283 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
284 &codec->cache_sync);
285 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
286 &codec->cache_only);
287
Mark Brown2624d5f2009-11-03 21:56:13 +0000288 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
289 codec->debugfs_codec_root,
290 codec, &codec_reg_fops);
291 if (!codec->debugfs_reg)
Liam Girdwood64e60f92012-02-15 15:15:37 +0000292 dev_warn(codec->dev, "Failed to create codec register debugfs file\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000293
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200294 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000295}
296
297static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
298{
299 debugfs_remove_recursive(codec->debugfs_codec_root);
300}
301
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000302static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
303{
304 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
305
306 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
307 debugfs_card_root);
308 if (!platform->debugfs_platform_root) {
309 dev_warn(platform->dev,
310 "Failed to create platform debugfs directory\n");
311 return;
312 }
313
314 snd_soc_dapm_debugfs_init(&platform->dapm,
315 platform->debugfs_platform_root);
316}
317
318static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
319{
320 debugfs_remove_recursive(platform->debugfs_platform_root);
321}
322
Mark Brownc3c5a192010-09-15 18:15:14 +0100323static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
324 size_t count, loff_t *ppos)
325{
326 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100327 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100328 struct snd_soc_codec *codec;
329
330 if (!buf)
331 return -ENOMEM;
332
Mark Brown2b194f9d2010-10-13 10:52:16 +0100333 list_for_each_entry(codec, &codec_list, list) {
334 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
335 codec->name);
336 if (len >= 0)
337 ret += len;
338 if (ret > PAGE_SIZE) {
339 ret = PAGE_SIZE;
340 break;
341 }
342 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100343
344 if (ret >= 0)
345 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
346
347 kfree(buf);
348
349 return ret;
350}
351
352static const struct file_operations codec_list_fops = {
353 .read = codec_list_read_file,
354 .llseek = default_llseek,/* read accesses f_pos */
355};
356
Mark Brownf3208782010-09-15 18:19:07 +0100357static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
358 size_t count, loff_t *ppos)
359{
360 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100361 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100362 struct snd_soc_dai *dai;
363
364 if (!buf)
365 return -ENOMEM;
366
Mark Brown2b194f9d2010-10-13 10:52:16 +0100367 list_for_each_entry(dai, &dai_list, list) {
368 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
369 if (len >= 0)
370 ret += len;
371 if (ret > PAGE_SIZE) {
372 ret = PAGE_SIZE;
373 break;
374 }
375 }
Mark Brownf3208782010-09-15 18:19:07 +0100376
Mark Brown2b194f9d2010-10-13 10:52:16 +0100377 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100378
379 kfree(buf);
380
381 return ret;
382}
383
384static const struct file_operations dai_list_fops = {
385 .read = dai_list_read_file,
386 .llseek = default_llseek,/* read accesses f_pos */
387};
388
Mark Brown19c7ac22010-09-15 18:22:40 +0100389static ssize_t platform_list_read_file(struct file *file,
390 char __user *user_buf,
391 size_t count, loff_t *ppos)
392{
393 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100394 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100395 struct snd_soc_platform *platform;
396
397 if (!buf)
398 return -ENOMEM;
399
Mark Brown2b194f9d2010-10-13 10:52:16 +0100400 list_for_each_entry(platform, &platform_list, list) {
401 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
402 platform->name);
403 if (len >= 0)
404 ret += len;
405 if (ret > PAGE_SIZE) {
406 ret = PAGE_SIZE;
407 break;
408 }
409 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100410
Mark Brown2b194f9d2010-10-13 10:52:16 +0100411 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100412
413 kfree(buf);
414
415 return ret;
416}
417
418static const struct file_operations platform_list_fops = {
419 .read = platform_list_read_file,
420 .llseek = default_llseek,/* read accesses f_pos */
421};
422
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200423static void soc_init_card_debugfs(struct snd_soc_card *card)
424{
425 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000426 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200427 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200428 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100429 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200430 return;
431 }
432
433 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
434 card->debugfs_card_root,
435 &card->pop_time);
436 if (!card->debugfs_pop_time)
437 dev_warn(card->dev,
438 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200439}
440
441static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
442{
443 debugfs_remove_recursive(card->debugfs_card_root);
444}
445
Mark Brown2624d5f2009-11-03 21:56:13 +0000446#else
447
448static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
449{
450}
451
452static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
453{
454}
Axel Linb95fccb2010-11-09 17:06:44 +0800455
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000456static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
457{
458}
459
460static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
461{
462}
463
Axel Linb95fccb2010-11-09 17:06:44 +0800464static inline void soc_init_card_debugfs(struct snd_soc_card *card)
465{
466}
467
468static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
469{
470}
Mark Brown2624d5f2009-11-03 21:56:13 +0000471#endif
472
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200473#ifdef CONFIG_SND_SOC_AC97_BUS
474/* unregister ac97 codec */
475static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
476{
477 if (codec->ac97->dev.bus)
478 device_unregister(&codec->ac97->dev);
479 return 0;
480}
481
482/* stop no dev release warning */
483static void soc_ac97_device_release(struct device *dev){}
484
485/* register ac97 codec to bus */
486static int soc_ac97_dev_register(struct snd_soc_codec *codec)
487{
488 int err;
489
490 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100491 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200492 codec->ac97->dev.release = soc_ac97_device_release;
493
Kay Sieversbb072bf2008-11-02 03:50:35 +0100494 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000495 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200496 err = device_register(&codec->ac97->dev);
497 if (err < 0) {
498 snd_printk(KERN_ERR "Can't register ac97 bus\n");
499 codec->ac97->dev.bus = NULL;
500 return err;
501 }
502 return 0;
503}
504#endif
505
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000506#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200507/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000508int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200509{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000510 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200511 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200512 int i;
513
Daniel Macke3509ff2009-06-03 17:44:49 +0200514 /* If the initialization of this soc device failed, there is no codec
515 * associated with it. Just bail out in this case.
516 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000517 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200518 return 0;
519
Andy Green6ed25972008-06-13 16:24:05 +0100520 /* Due to the resume being scheduled into a workqueue we could
521 * suspend before that's finished - wait for it to complete.
522 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000523 snd_power_lock(card->snd_card);
524 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
525 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100526
527 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000528 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100529
Mark Browna00f90f2010-12-02 16:24:24 +0000530 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000531 for (i = 0; i < card->num_rtd; i++) {
532 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
533 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100534
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000535 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100536 continue;
537
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000538 if (drv->ops->digital_mute && dai->playback_active)
539 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200540 }
541
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100542 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000543 for (i = 0; i < card->num_rtd; i++) {
544 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100545 continue;
546
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000547 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100548 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100549
Mark Brown87506542008-11-18 20:50:34 +0000550 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000551 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200552
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000553 for (i = 0; i < card->num_rtd; i++) {
554 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
555 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100556
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000557 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100558 continue;
559
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000560 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
561 cpu_dai->driver->suspend(cpu_dai);
562 if (platform->driver->suspend && !platform->suspended) {
563 platform->driver->suspend(cpu_dai);
564 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100565 }
566 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200567
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000568 /* close any waiting streams and save state */
569 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +0100570 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200571 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000572 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100573
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000574 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000575
576 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100577 continue;
578
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800579 snd_soc_dapm_stream_event(&card->rtd[i],
580 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800581 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000582
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800583 snd_soc_dapm_stream_event(&card->rtd[i],
584 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800585 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000586 }
587
588 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200589 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000590 /* If there are paths active then the CODEC will be held with
591 * bias _ON and should not be suspended. */
592 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200593 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000594 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000595 /*
596 * If the CODEC is capable of idle
597 * bias off then being in STANDBY
598 * means it's doing something,
599 * otherwise fall through.
600 */
601 if (codec->dapm.idle_bias_off) {
602 dev_dbg(codec->dev,
603 "idle_bias_off CODEC on over suspend\n");
604 break;
605 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000606 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100607 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900609 codec->cache_sync = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000610 break;
611 default:
612 dev_dbg(codec->dev, "CODEC is on over suspend\n");
613 break;
614 }
615 }
616 }
617
618 for (i = 0; i < card->num_rtd; i++) {
619 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
620
621 if (card->rtd[i].dai_link->ignore_suspend)
622 continue;
623
624 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
625 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200626 }
627
Mark Brown87506542008-11-18 20:50:34 +0000628 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000629 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200630
631 return 0;
632}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000633EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200634
Andy Green6ed25972008-06-13 16:24:05 +0100635/* deferred resume work, so resume can complete before we finished
636 * setting our codec back up, which can be very slow on I2C
637 */
638static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200639{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000640 struct snd_soc_card *card =
641 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200642 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200643 int i;
644
Andy Green6ed25972008-06-13 16:24:05 +0100645 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
646 * so userspace apps are blocked from touching us
647 */
648
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000649 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100650
Mark Brown99497882010-05-07 20:24:05 +0100651 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000652 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100653
Mark Brown87506542008-11-18 20:50:34 +0000654 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000655 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200656
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000657 /* resume AC97 DAIs */
658 for (i = 0; i < card->num_rtd; i++) {
659 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100660
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000661 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100662 continue;
663
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000664 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
665 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200666 }
667
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200668 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000669 /* If the CODEC was idle over suspend then it will have been
670 * left with bias OFF or STANDBY and suspended so we must now
671 * resume. Otherwise the suspend was suppressed.
672 */
673 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200674 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000675 case SND_SOC_BIAS_STANDBY:
676 case SND_SOC_BIAS_OFF:
677 codec->driver->resume(codec);
678 codec->suspended = 0;
679 break;
680 default:
681 dev_dbg(codec->dev, "CODEC was on over suspend\n");
682 break;
683 }
Mark Brown1547aba2010-05-07 21:11:40 +0100684 }
685 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200686
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000687 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100688
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000689 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100690 continue;
691
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800692 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000693 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800694 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000695
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800696 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000697 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800698 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200699 }
700
Mark Brown3ff3f642008-05-19 12:32:25 +0200701 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000702 for (i = 0; i < card->num_rtd; i++) {
703 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
704 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100705
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000706 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100707 continue;
708
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000709 if (drv->ops->digital_mute && dai->playback_active)
710 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200711 }
712
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000713 for (i = 0; i < card->num_rtd; i++) {
714 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
715 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100716
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000717 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100718 continue;
719
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000720 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
721 cpu_dai->driver->resume(cpu_dai);
722 if (platform->driver->resume && platform->suspended) {
723 platform->driver->resume(cpu_dai);
724 platform->suspended = 0;
725 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200726 }
727
Mark Brown87506542008-11-18 20:50:34 +0000728 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000729 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200730
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000731 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100732
733 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000734 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100735}
736
737/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000738int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100739{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000740 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600741 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200742
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800743 /* If the initialization of this soc device failed, there is no codec
744 * associated with it. Just bail out in this case.
745 */
746 if (list_empty(&card->codec_dev_list))
747 return 0;
748
Mark Brown64ab9ba2009-03-31 11:27:03 +0100749 /* AC97 devices might have other drivers hanging off them so
750 * need to resume immediately. Other drivers don't have that
751 * problem and may take a substantial amount of time to resume
752 * due to I/O costs and anti-pop so handle them out of line.
753 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000754 for (i = 0; i < card->num_rtd; i++) {
755 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600756 ac97_control |= cpu_dai->driver->ac97_control;
757 }
758 if (ac97_control) {
759 dev_dbg(dev, "Resuming AC97 immediately\n");
760 soc_resume_deferred(&card->deferred_resume_work);
761 } else {
762 dev_dbg(dev, "Scheduling resume work\n");
763 if (!schedule_work(&card->deferred_resume_work))
764 dev_err(dev, "resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100765 }
Andy Green6ed25972008-06-13 16:24:05 +0100766
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200767 return 0;
768}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000769EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200770#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000771#define snd_soc_suspend NULL
772#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200773#endif
774
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100775static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800776};
777
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000778static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200779{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000780 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
781 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000782 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000783 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000784 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100785 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200786
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000787 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000788
Mark Brownb19e6e72012-03-14 21:18:39 +0000789 /* Find CPU DAI from registered DAIs*/
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000790 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700791 if (dai_link->cpu_dai_of_node) {
792 if (cpu_dai->dev->of_node != dai_link->cpu_dai_of_node)
793 continue;
794 } else {
795 if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
796 continue;
797 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700798
799 rtd->cpu_dai = cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000800 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000801
802 if (!rtd->cpu_dai) {
803 dev_dbg(card->dev, "CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000804 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000805 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000806 }
807
Mark Brownb19e6e72012-03-14 21:18:39 +0000808 /* Find CODEC from registered CODECs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000809 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700810 if (dai_link->codec_of_node) {
811 if (codec->dev->of_node != dai_link->codec_of_node)
812 continue;
813 } else {
814 if (strcmp(codec->name, dai_link->codec_name))
815 continue;
816 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000817
Stephen Warren2610ab72011-12-07 13:58:27 -0700818 rtd->codec = codec;
819
820 /*
821 * CODEC found, so find CODEC DAI from registered DAIs from
822 * this CODEC
823 */
824 list_for_each_entry(codec_dai, &dai_list, list) {
825 if (codec->dev == codec_dai->dev &&
826 !strcmp(codec_dai->name,
827 dai_link->codec_dai_name)) {
828
829 rtd->codec_dai = codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000830 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000831 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000832
833 if (!rtd->codec_dai) {
834 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
Stephen Warren2610ab72011-12-07 13:58:27 -0700835 dai_link->codec_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000836 return -EPROBE_DEFER;
837 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000838 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000839
Mark Brownb19e6e72012-03-14 21:18:39 +0000840 if (!rtd->codec) {
841 dev_dbg(card->dev, "CODEC %s not registered\n",
842 dai_link->codec_name);
843 return -EPROBE_DEFER;
844 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100845
846 /* if there's no platform we match on the empty platform */
847 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700848 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100849 platform_name = "snd-soc-dummy";
850
Mark Brownb19e6e72012-03-14 21:18:39 +0000851 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000852 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700853 if (dai_link->platform_of_node) {
854 if (platform->dev->of_node !=
855 dai_link->platform_of_node)
856 continue;
857 } else {
858 if (strcmp(platform->name, platform_name))
859 continue;
860 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700861
862 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000863 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000864 if (!rtd->platform) {
865 dev_dbg(card->dev, "platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000866 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000867 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000868 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000869
870 card->num_rtd++;
871
872 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000873}
874
Jarkko Nikula589c3562010-12-06 16:27:07 +0200875static void soc_remove_codec(struct snd_soc_codec *codec)
876{
877 int err;
878
879 if (codec->driver->remove) {
880 err = codec->driver->remove(codec);
881 if (err < 0)
882 dev_err(codec->dev,
883 "asoc: failed to remove %s: %d\n",
884 codec->name, err);
885 }
886
887 /* Make sure all DAPM widgets are freed */
888 snd_soc_dapm_free(&codec->dapm);
889
890 soc_cleanup_codec_debugfs(codec);
891 codec->probed = 0;
892 list_del(&codec->card_list);
893 module_put(codec->dev->driver->owner);
894}
895
Liam Girdwood0168bf02011-06-07 16:08:05 +0100896static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000897{
898 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
899 struct snd_soc_codec *codec = rtd->codec;
900 struct snd_soc_platform *platform = rtd->platform;
901 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
902 int err;
903
904 /* unregister the rtd device */
905 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800906 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
907 device_remove_file(rtd->dev, &dev_attr_codec_reg);
908 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000909 rtd->dev_registered = 0;
910 }
911
912 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100913 if (codec_dai && codec_dai->probed &&
914 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000915 if (codec_dai->driver->remove) {
916 err = codec_dai->driver->remove(codec_dai);
917 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200918 pr_err("asoc: failed to remove %s: %d\n",
919 codec_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000920 }
921 codec_dai->probed = 0;
922 list_del(&codec_dai->card_list);
923 }
924
925 /* remove the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100926 if (platform && platform->probed &&
927 platform->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000928 if (platform->driver->remove) {
929 err = platform->driver->remove(platform);
930 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200931 pr_err("asoc: failed to remove %s: %d\n",
932 platform->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000933 }
Liam Girdwood675c4962012-01-16 15:25:37 +0000934
935 /* Make sure all DAPM widgets are freed */
936 snd_soc_dapm_free(&platform->dapm);
937
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000938 soc_cleanup_platform_debugfs(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000939 platform->probed = 0;
940 list_del(&platform->card_list);
941 module_put(platform->dev->driver->owner);
942 }
943
944 /* remove the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100945 if (codec && codec->probed &&
946 codec->driver->remove_order == order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200947 soc_remove_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000948
949 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100950 if (cpu_dai && cpu_dai->probed &&
951 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000952 if (cpu_dai->driver->remove) {
953 err = cpu_dai->driver->remove(cpu_dai);
954 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200955 pr_err("asoc: failed to remove %s: %d\n",
956 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000957 }
958 cpu_dai->probed = 0;
959 list_del(&cpu_dai->card_list);
960 module_put(cpu_dai->dev->driver->owner);
961 }
962}
963
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900964static void soc_remove_dai_links(struct snd_soc_card *card)
965{
Liam Girdwood0168bf02011-06-07 16:08:05 +0100966 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900967
Liam Girdwood0168bf02011-06-07 16:08:05 +0100968 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
969 order++) {
970 for (dai = 0; dai < card->num_rtd; dai++)
971 soc_remove_dai_link(card, dai, order);
972 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900973 card->num_rtd = 0;
974}
975
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200976static void soc_set_name_prefix(struct snd_soc_card *card,
977 struct snd_soc_codec *codec)
978{
979 int i;
980
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000981 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200982 return;
983
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000984 for (i = 0; i < card->num_configs; i++) {
985 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200986 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
987 codec->name_prefix = map->name_prefix;
988 break;
989 }
990 }
991}
992
Jarkko Nikula589c3562010-12-06 16:27:07 +0200993static int soc_probe_codec(struct snd_soc_card *card,
994 struct snd_soc_codec *codec)
995{
996 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +0000997 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -0800998 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +0200999
1000 codec->card = card;
1001 codec->dapm.card = card;
1002 soc_set_name_prefix(card, codec);
1003
Jarkko Nikula70d29332011-01-27 16:24:22 +02001004 if (!try_module_get(codec->dev->driver->owner))
1005 return -ENODEV;
1006
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001007 soc_init_codec_debugfs(codec);
1008
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001009 if (driver->dapm_widgets)
1010 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1011 driver->num_dapm_widgets);
1012
Mark Brown888df392012-02-16 19:37:51 -08001013 /* Create DAPM widgets for each DAI stream */
1014 list_for_each_entry(dai, &dai_list, list) {
1015 if (dai->dev != codec->dev)
1016 continue;
1017
1018 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1019 }
1020
Mark Brown33c5f962011-08-22 18:40:30 +01001021 codec->dapm.idle_bias_off = driver->idle_bias_off;
1022
Mark Brown89b95ac02011-03-07 16:38:44 +00001023 if (driver->probe) {
1024 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001025 if (ret < 0) {
1026 dev_err(codec->dev,
1027 "asoc: failed to probe CODEC %s: %d\n",
1028 codec->name, ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001029 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001030 }
1031 }
1032
Mark Brownb7af1da2011-04-07 19:18:44 +09001033 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001034 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001035 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001036 if (driver->dapm_routes)
1037 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1038 driver->num_dapm_routes);
1039
Jarkko Nikula589c3562010-12-06 16:27:07 +02001040 /* mark codec as probed and add to card codec list */
1041 codec->probed = 1;
1042 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001043 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001044
Jarkko Nikula70d29332011-01-27 16:24:22 +02001045 return 0;
1046
1047err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001048 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001049 module_put(codec->dev->driver->owner);
1050
Jarkko Nikula589c3562010-12-06 16:27:07 +02001051 return ret;
1052}
1053
Liam Girdwood956245e2011-07-01 16:54:08 +01001054static int soc_probe_platform(struct snd_soc_card *card,
1055 struct snd_soc_platform *platform)
1056{
1057 int ret = 0;
1058 const struct snd_soc_platform_driver *driver = platform->driver;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001059 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001060
1061 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001062 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001063
1064 if (!try_module_get(platform->dev->driver->owner))
1065 return -ENODEV;
1066
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001067 soc_init_platform_debugfs(platform);
1068
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001069 if (driver->dapm_widgets)
1070 snd_soc_dapm_new_controls(&platform->dapm,
1071 driver->dapm_widgets, driver->num_dapm_widgets);
1072
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001073 /* Create DAPM widgets for each DAI stream */
1074 list_for_each_entry(dai, &dai_list, list) {
1075 if (dai->dev != platform->dev)
1076 continue;
1077
1078 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1079 }
1080
Liam Girdwood956245e2011-07-01 16:54:08 +01001081 if (driver->probe) {
1082 ret = driver->probe(platform);
1083 if (ret < 0) {
1084 dev_err(platform->dev,
1085 "asoc: failed to probe platform %s: %d\n",
1086 platform->name, ret);
1087 goto err_probe;
1088 }
1089 }
1090
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001091 if (driver->controls)
1092 snd_soc_add_platform_controls(platform, driver->controls,
1093 driver->num_controls);
1094 if (driver->dapm_routes)
1095 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1096 driver->num_dapm_routes);
1097
Liam Girdwood956245e2011-07-01 16:54:08 +01001098 /* mark platform as probed and add to card platform list */
1099 platform->probed = 1;
1100 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001101 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001102
1103 return 0;
1104
1105err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001106 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001107 module_put(platform->dev->driver->owner);
1108
1109 return ret;
1110}
1111
Mark Brown36ae1a92012-01-06 17:12:45 -08001112static void rtd_release(struct device *dev)
1113{
1114 kfree(dev);
1115}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001116
Jarkko Nikula589c3562010-12-06 16:27:07 +02001117static int soc_post_component_init(struct snd_soc_card *card,
1118 struct snd_soc_codec *codec,
1119 int num, int dailess)
1120{
1121 struct snd_soc_dai_link *dai_link = NULL;
1122 struct snd_soc_aux_dev *aux_dev = NULL;
1123 struct snd_soc_pcm_runtime *rtd;
1124 const char *temp, *name;
1125 int ret = 0;
1126
1127 if (!dailess) {
1128 dai_link = &card->dai_link[num];
1129 rtd = &card->rtd[num];
1130 name = dai_link->name;
1131 } else {
1132 aux_dev = &card->aux_dev[num];
1133 rtd = &card->rtd_aux[num];
1134 name = aux_dev->name;
1135 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001136 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001137
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001138 /* Make sure all DAPM widgets are instantiated */
1139 snd_soc_dapm_new_widgets(&codec->dapm);
1140
Jarkko Nikula589c3562010-12-06 16:27:07 +02001141 /* machine controls, routes and widgets are not prefixed */
1142 temp = codec->name_prefix;
1143 codec->name_prefix = NULL;
1144
1145 /* do machine specific initialization */
1146 if (!dailess && dai_link->init)
1147 ret = dai_link->init(rtd);
1148 else if (dailess && aux_dev->init)
1149 ret = aux_dev->init(&codec->dapm);
1150 if (ret < 0) {
1151 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1152 return ret;
1153 }
1154 codec->name_prefix = temp;
1155
Jarkko Nikula589c3562010-12-06 16:27:07 +02001156 /* register the rtd device */
1157 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001158
1159 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1160 if (!rtd->dev)
1161 return -ENOMEM;
1162 device_initialize(rtd->dev);
1163 rtd->dev->parent = card->dev;
1164 rtd->dev->release = rtd_release;
1165 rtd->dev->init_name = name;
1166 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001167 mutex_init(&rtd->pcm_mutex);
Mark Brown36ae1a92012-01-06 17:12:45 -08001168 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001169 if (ret < 0) {
1170 dev_err(card->dev,
1171 "asoc: failed to register runtime device: %d\n", ret);
1172 return ret;
1173 }
1174 rtd->dev_registered = 1;
1175
1176 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001177 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001178 if (ret < 0)
1179 dev_err(codec->dev,
1180 "asoc: failed to add codec dapm sysfs entries: %d\n",
1181 ret);
1182
1183 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001184 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001185 if (ret < 0)
1186 dev_err(codec->dev,
1187 "asoc: failed to add codec sysfs files: %d\n", ret);
1188
1189 return 0;
1190}
1191
Liam Girdwood0168bf02011-06-07 16:08:05 +01001192static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001193{
1194 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1195 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1196 struct snd_soc_codec *codec = rtd->codec;
1197 struct snd_soc_platform *platform = rtd->platform;
1198 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1199 int ret;
1200
Liam Girdwood0168bf02011-06-07 16:08:05 +01001201 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1202 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001203
1204 /* config components */
1205 codec_dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001206 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001207 codec_dai->card = card;
1208 cpu_dai->card = card;
1209
1210 /* set default power off timeout */
1211 rtd->pmdown_time = pmdown_time;
1212
1213 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001214 if (!cpu_dai->probed &&
1215 cpu_dai->driver->probe_order == order) {
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001216 cpu_dai->dapm.card = card;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001217 if (!try_module_get(cpu_dai->dev->driver->owner))
1218 return -ENODEV;
1219
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001220 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1221
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001222 if (cpu_dai->driver->probe) {
1223 ret = cpu_dai->driver->probe(cpu_dai);
1224 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001225 pr_err("asoc: failed to probe CPU DAI %s: %d\n",
1226 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001227 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001228 return ret;
1229 }
1230 }
1231 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001232 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001233 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1234 }
1235
1236 /* probe the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001237 if (!codec->probed &&
1238 codec->driver->probe_order == order) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001239 ret = soc_probe_codec(card, codec);
1240 if (ret < 0)
1241 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001242 }
1243
1244 /* probe the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001245 if (!platform->probed &&
1246 platform->driver->probe_order == order) {
Liam Girdwood956245e2011-07-01 16:54:08 +01001247 ret = soc_probe_platform(card, platform);
1248 if (ret < 0)
1249 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001250 }
1251
1252 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001253 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001254 if (codec_dai->driver->probe) {
1255 ret = codec_dai->driver->probe(codec_dai);
1256 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001257 pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
1258 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001259 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001260 }
1261 }
1262
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001263 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001264 codec_dai->probed = 1;
1265 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001266 }
1267
Liam Girdwood0168bf02011-06-07 16:08:05 +01001268 /* complete DAI probe during last probe */
1269 if (order != SND_SOC_COMP_ORDER_LAST)
1270 return 0;
1271
Jarkko Nikula589c3562010-12-06 16:27:07 +02001272 ret = soc_post_component_init(card, codec, num, 0);
1273 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001274 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001275
Mark Brown36ae1a92012-01-06 17:12:45 -08001276 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001277 if (ret < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -02001278 pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001279
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001280 /* create the pcm */
1281 ret = soc_new_pcm(rtd, num);
1282 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001283 pr_err("asoc: can't create pcm %s :%d\n",
1284 dai_link->stream_name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001285 return ret;
1286 }
1287
1288 /* add platform data for AC97 devices */
1289 if (rtd->codec_dai->driver->ac97_control)
1290 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1291
1292 return 0;
1293}
1294
1295#ifdef CONFIG_SND_SOC_AC97_BUS
1296static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1297{
1298 int ret;
1299
1300 /* Only instantiate AC97 if not already done by the adaptor
1301 * for the generic AC97 subsystem.
1302 */
1303 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001304 /*
1305 * It is possible that the AC97 device is already registered to
1306 * the device subsystem. This happens when the device is created
1307 * via snd_ac97_mixer(). Currently only SoC codec that does so
1308 * is the generic AC97 glue but others migh emerge.
1309 *
1310 * In those cases we don't try to register the device again.
1311 */
1312 if (!rtd->codec->ac97_created)
1313 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001314
1315 ret = soc_ac97_dev_register(rtd->codec);
1316 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001317 pr_err("asoc: AC97 device register failed:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001318 return ret;
1319 }
1320
1321 rtd->codec->ac97_registered = 1;
1322 }
1323 return 0;
1324}
1325
1326static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1327{
1328 if (codec->ac97_registered) {
1329 soc_ac97_dev_unregister(codec);
1330 codec->ac97_registered = 0;
1331 }
1332}
1333#endif
1334
Mark Brownb19e6e72012-03-14 21:18:39 +00001335static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1336{
1337 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1338 struct snd_soc_codec *codec;
1339
1340 /* find CODEC from registered CODECs*/
1341 list_for_each_entry(codec, &codec_list, list) {
1342 if (!strcmp(codec->name, aux_dev->codec_name))
1343 return 0;
1344 }
1345
1346 return -EPROBE_DEFER;
1347}
1348
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001349static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1350{
1351 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001352 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001353 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001354
1355 /* find CODEC from registered CODECs*/
1356 list_for_each_entry(codec, &codec_list, list) {
1357 if (!strcmp(codec->name, aux_dev->codec_name)) {
1358 if (codec->probed) {
1359 dev_err(codec->dev,
1360 "asoc: codec already probed");
1361 ret = -EBUSY;
1362 goto out;
1363 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001364 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001365 }
1366 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001367 /* codec not found */
1368 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001369 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001370
Jarkko Nikula676ad982010-12-03 09:18:22 +02001371found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001372 ret = soc_probe_codec(card, codec);
1373 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001374 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001375
Jarkko Nikula589c3562010-12-06 16:27:07 +02001376 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001377
1378out:
1379 return ret;
1380}
1381
1382static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1383{
1384 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1385 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001386
1387 /* unregister the rtd device */
1388 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001389 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1390 device_del(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001391 rtd->dev_registered = 0;
1392 }
1393
Jarkko Nikula589c3562010-12-06 16:27:07 +02001394 if (codec && codec->probed)
1395 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001396}
1397
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001398static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1399 enum snd_soc_compress_type compress_type)
1400{
1401 int ret;
1402
1403 if (codec->cache_init)
1404 return 0;
1405
1406 /* override the compress_type if necessary */
1407 if (compress_type && codec->compress_type != compress_type)
1408 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001409 ret = snd_soc_cache_init(codec);
1410 if (ret < 0) {
1411 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1412 ret);
1413 return ret;
1414 }
1415 codec->cache_init = 1;
1416 return 0;
1417}
1418
Mark Brownb19e6e72012-03-14 21:18:39 +00001419static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001420{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001421 struct snd_soc_codec *codec;
1422 struct snd_soc_codec_conf *codec_conf;
1423 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001424 struct snd_soc_dai_link *dai_link;
Liam Girdwood0168bf02011-06-07 16:08:05 +01001425 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001426
Liam Girdwood01b9d992012-03-07 10:38:25 +00001427 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001428
Mark Brownb19e6e72012-03-14 21:18:39 +00001429 /* bind DAIs */
1430 for (i = 0; i < card->num_links; i++) {
1431 ret = soc_bind_dai_link(card, i);
1432 if (ret != 0)
1433 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001434 }
1435
Mark Brownb19e6e72012-03-14 21:18:39 +00001436 /* check aux_devs too */
1437 for (i = 0; i < card->num_aux_devs; i++) {
1438 ret = soc_check_aux_dev(card, i);
1439 if (ret != 0)
1440 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001441 }
1442
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001443 /* initialize the register cache for each available codec */
1444 list_for_each_entry(codec, &codec_list, list) {
1445 if (codec->cache_init)
1446 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001447 /* by default we don't override the compress_type */
1448 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001449 /* check to see if we need to override the compress_type */
1450 for (i = 0; i < card->num_configs; ++i) {
1451 codec_conf = &card->codec_conf[i];
1452 if (!strcmp(codec->name, codec_conf->dev_name)) {
1453 compress_type = codec_conf->compress_type;
1454 if (compress_type && compress_type
1455 != codec->compress_type)
1456 break;
1457 }
1458 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001459 ret = snd_soc_init_codec_cache(codec, compress_type);
Mark Brownb19e6e72012-03-14 21:18:39 +00001460 if (ret < 0)
1461 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001462 }
1463
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001464 /* card bind complete so register a sound card */
1465 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1466 card->owner, 0, &card->snd_card);
1467 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001468 pr_err("asoc: can't create sound card for card %s: %d\n",
1469 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001470 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001471 }
1472 card->snd_card->dev = card->dev;
1473
Mark Browne37a4972011-03-02 18:21:57 +00001474 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1475 card->dapm.dev = card->dev;
1476 card->dapm.card = card;
1477 list_add(&card->dapm.list, &card->dapm_list);
1478
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001479#ifdef CONFIG_DEBUG_FS
1480 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1481#endif
1482
Mark Brown88ee1c62011-02-02 10:43:26 +00001483#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001484 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001485 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001486#endif
Andy Green6ed25972008-06-13 16:24:05 +01001487
Mark Brown9a841eb2011-04-12 17:51:37 -07001488 if (card->dapm_widgets)
1489 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1490 card->num_dapm_widgets);
1491
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001492 /* initialise the sound card only once */
1493 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001494 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001495 if (ret < 0)
1496 goto card_probe_error;
1497 }
1498
Liam Girdwood0168bf02011-06-07 16:08:05 +01001499 /* early DAI link probe */
1500 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1501 order++) {
1502 for (i = 0; i < card->num_links; i++) {
1503 ret = soc_probe_dai_link(card, i, order);
1504 if (ret < 0) {
1505 pr_err("asoc: failed to instantiate card %s: %d\n",
Mark Brown321de0d2010-09-21 15:09:37 +01001506 card->name, ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001507 goto probe_dai_err;
1508 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001509 }
1510 }
1511
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001512 for (i = 0; i < card->num_aux_devs; i++) {
1513 ret = soc_probe_aux_dev(card, i);
1514 if (ret < 0) {
1515 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1516 card->name, ret);
1517 goto probe_aux_dev_err;
1518 }
1519 }
1520
Mark Brown888df392012-02-16 19:37:51 -08001521 snd_soc_dapm_link_dai_widgets(card);
1522
Mark Brownb7af1da2011-04-07 19:18:44 +09001523 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001524 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001525
Mark Brownb8ad29d2011-03-02 18:35:51 +00001526 if (card->dapm_routes)
1527 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1528 card->num_dapm_routes);
1529
Mark Brownb90d2f92011-10-10 13:38:06 +01001530 snd_soc_dapm_new_widgets(&card->dapm);
1531
Mark Brown75d9ac42011-09-27 16:41:01 +01001532 for (i = 0; i < card->num_links; i++) {
1533 dai_link = &card->dai_link[i];
1534
1535 if (dai_link->dai_fmt) {
1536 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1537 dai_link->dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001538 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001539 dev_warn(card->rtd[i].codec_dai->dev,
1540 "Failed to set DAI format: %d\n",
1541 ret);
1542
1543 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1544 dai_link->dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001545 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001546 dev_warn(card->rtd[i].cpu_dai->dev,
1547 "Failed to set DAI format: %d\n",
1548 ret);
1549 }
1550 }
1551
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001552 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001553 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001554 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1555 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001556 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1557 "%s", card->driver_name ? card->driver_name : card->name);
1558 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1559 switch (card->snd_card->driver[i]) {
1560 case '_':
1561 case '-':
1562 case '\0':
1563 break;
1564 default:
1565 if (!isalnum(card->snd_card->driver[i]))
1566 card->snd_card->driver[i] = '_';
1567 break;
1568 }
1569 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001570
Mark Brown28e9ad92011-03-02 18:36:34 +00001571 if (card->late_probe) {
1572 ret = card->late_probe(card);
1573 if (ret < 0) {
1574 dev_err(card->dev, "%s late_probe() failed: %d\n",
1575 card->name, ret);
1576 goto probe_aux_dev_err;
1577 }
1578 }
1579
Mark Brown2dc00212011-10-08 13:59:44 +01001580 snd_soc_dapm_new_widgets(&card->dapm);
1581
Stephen Warren16332812011-11-23 12:42:04 -07001582 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001583 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001584 snd_soc_dapm_auto_nc_codec_pins(codec);
1585
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001586 ret = snd_card_register(card->snd_card);
1587 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001588 pr_err("asoc: failed to register soundcard for %s: %d\n",
1589 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001590 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001591 }
1592
1593#ifdef CONFIG_SND_SOC_AC97_BUS
1594 /* register any AC97 codecs */
1595 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001596 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1597 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001598 pr_err("asoc: failed to register AC97 %s: %d\n",
1599 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001600 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001601 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001602 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001603 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001604 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001605#endif
1606
Mark Brown435c5e22008-12-04 15:32:53 +00001607 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001608 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001609 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001610
1611 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001612
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001613probe_aux_dev_err:
1614 for (i = 0; i < card->num_aux_devs; i++)
1615 soc_remove_aux_dev(card, i);
1616
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001617probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001618 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001619
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001620card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001621 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001622 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001623
1624 snd_card_free(card->snd_card);
1625
Mark Brownb19e6e72012-03-14 21:18:39 +00001626base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001627 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001628
Mark Brownb19e6e72012-03-14 21:18:39 +00001629 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001630}
1631
1632/* probes a new socdev */
1633static int soc_probe(struct platform_device *pdev)
1634{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001635 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001636 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001637
Vinod Koul70a7ca32011-01-14 19:22:48 +05301638 /*
1639 * no card, so machine driver should be registering card
1640 * we should not be here in that case so ret error
1641 */
1642 if (!card)
1643 return -EINVAL;
1644
Mark Brownfe4085e2012-03-02 13:07:41 +00001645 dev_warn(&pdev->dev,
1646 "ASoC machine %s should use snd_soc_register_card()\n",
1647 card->name);
1648
Mark Brown435c5e22008-12-04 15:32:53 +00001649 /* Bodge while we unpick instantiation */
1650 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001651
Mark Brown435c5e22008-12-04 15:32:53 +00001652 ret = snd_soc_register_card(card);
1653 if (ret != 0) {
1654 dev_err(&pdev->dev, "Failed to register card\n");
1655 return ret;
1656 }
1657
1658 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001659}
1660
Vinod Koulb0e26482011-01-13 22:48:02 +05301661static int soc_cleanup_card_resources(struct snd_soc_card *card)
1662{
Vinod Koulb0e26482011-01-13 22:48:02 +05301663 int i;
1664
1665 /* make sure any delayed work runs */
1666 for (i = 0; i < card->num_rtd; i++) {
1667 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1668 flush_delayed_work_sync(&rtd->delayed_work);
1669 }
1670
1671 /* remove auxiliary devices */
1672 for (i = 0; i < card->num_aux_devs; i++)
1673 soc_remove_aux_dev(card, i);
1674
1675 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001676 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301677
1678 soc_cleanup_card_debugfs(card);
1679
1680 /* remove the card */
1681 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001682 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301683
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001684 snd_soc_dapm_free(&card->dapm);
1685
Vinod Koulb0e26482011-01-13 22:48:02 +05301686 snd_card_free(card->snd_card);
1687 return 0;
1688
1689}
1690
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001691/* removes a socdev */
1692static int soc_remove(struct platform_device *pdev)
1693{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001694 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001695
Mark Brownc5af3a22008-11-28 13:29:45 +00001696 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001697 return 0;
1698}
1699
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001700int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001701{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001702 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001703 int i;
Mark Brown51737472009-06-22 13:16:51 +01001704
1705 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001706 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001707
1708 /* Flush out pmdown_time work - we actually do want to run it
1709 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001710 for (i = 0; i < card->num_rtd; i++) {
1711 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001712 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001713 }
Mark Brown51737472009-06-22 13:16:51 +01001714
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001715 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001716
1717 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001718}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001719EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001720
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001721const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301722 .suspend = snd_soc_suspend,
1723 .resume = snd_soc_resume,
1724 .freeze = snd_soc_suspend,
1725 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001726 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301727 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001728};
Stephen Warrendeb26072011-04-05 19:35:30 -06001729EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001730
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001731/* ASoC platform driver */
1732static struct platform_driver soc_driver = {
1733 .driver = {
1734 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001735 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001736 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001737 },
1738 .probe = soc_probe,
1739 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001740};
1741
Mark Brown096e49d2009-07-05 15:12:22 +01001742/**
1743 * snd_soc_codec_volatile_register: Report if a register is volatile.
1744 *
1745 * @codec: CODEC to query.
1746 * @reg: Register to query.
1747 *
1748 * Boolean function indiciating if a CODEC register is volatile.
1749 */
Mark Brown181e0552011-01-24 14:05:25 +00001750int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1751 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001752{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001753 if (codec->volatile_register)
1754 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001755 else
1756 return 0;
1757}
1758EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1759
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001760/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001761 * snd_soc_codec_readable_register: Report if a register is readable.
1762 *
1763 * @codec: CODEC to query.
1764 * @reg: Register to query.
1765 *
1766 * Boolean function indicating if a CODEC register is readable.
1767 */
1768int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1769 unsigned int reg)
1770{
1771 if (codec->readable_register)
1772 return codec->readable_register(codec, reg);
1773 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001774 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001775}
1776EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1777
1778/**
1779 * snd_soc_codec_writable_register: Report if a register is writable.
1780 *
1781 * @codec: CODEC to query.
1782 * @reg: Register to query.
1783 *
1784 * Boolean function indicating if a CODEC register is writable.
1785 */
1786int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1787 unsigned int reg)
1788{
1789 if (codec->writable_register)
1790 return codec->writable_register(codec, reg);
1791 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001792 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001793}
1794EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1795
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001796int snd_soc_platform_read(struct snd_soc_platform *platform,
1797 unsigned int reg)
1798{
1799 unsigned int ret;
1800
1801 if (!platform->driver->read) {
1802 dev_err(platform->dev, "platform has no read back\n");
1803 return -1;
1804 }
1805
1806 ret = platform->driver->read(platform, reg);
1807 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001808 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001809
1810 return ret;
1811}
1812EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1813
1814int snd_soc_platform_write(struct snd_soc_platform *platform,
1815 unsigned int reg, unsigned int val)
1816{
1817 if (!platform->driver->write) {
1818 dev_err(platform->dev, "platform has no write back\n");
1819 return -1;
1820 }
1821
1822 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001823 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001824 return platform->driver->write(platform, reg, val);
1825}
1826EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1827
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001828/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001829 * snd_soc_new_ac97_codec - initailise AC97 device
1830 * @codec: audio codec
1831 * @ops: AC97 bus operations
1832 * @num: AC97 codec number
1833 *
1834 * Initialises AC97 codec resources for use by ad-hoc devices only.
1835 */
1836int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1837 struct snd_ac97_bus_ops *ops, int num)
1838{
1839 mutex_lock(&codec->mutex);
1840
1841 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1842 if (codec->ac97 == NULL) {
1843 mutex_unlock(&codec->mutex);
1844 return -ENOMEM;
1845 }
1846
1847 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1848 if (codec->ac97->bus == NULL) {
1849 kfree(codec->ac97);
1850 codec->ac97 = NULL;
1851 mutex_unlock(&codec->mutex);
1852 return -ENOMEM;
1853 }
1854
1855 codec->ac97->bus->ops = ops;
1856 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001857
1858 /*
1859 * Mark the AC97 device to be created by us. This way we ensure that the
1860 * device will be registered with the device subsystem later on.
1861 */
1862 codec->ac97_created = 1;
1863
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001864 mutex_unlock(&codec->mutex);
1865 return 0;
1866}
1867EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1868
1869/**
1870 * snd_soc_free_ac97_codec - free AC97 codec device
1871 * @codec: audio codec
1872 *
1873 * Frees AC97 codec device resources.
1874 */
1875void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1876{
1877 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001878#ifdef CONFIG_SND_SOC_AC97_BUS
1879 soc_unregister_ac97_dai_link(codec);
1880#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001881 kfree(codec->ac97->bus);
1882 kfree(codec->ac97);
1883 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001884 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001885 mutex_unlock(&codec->mutex);
1886}
1887EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1888
Mark Brownc3753702010-11-01 15:41:57 -04001889unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1890{
1891 unsigned int ret;
1892
Mark Brownc3acec22010-12-02 16:15:29 +00001893 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04001894 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04001895 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04001896
1897 return ret;
1898}
1899EXPORT_SYMBOL_GPL(snd_soc_read);
1900
1901unsigned int snd_soc_write(struct snd_soc_codec *codec,
1902 unsigned int reg, unsigned int val)
1903{
1904 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04001905 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00001906 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04001907}
1908EXPORT_SYMBOL_GPL(snd_soc_write);
1909
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00001910unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
1911 unsigned int reg, const void *data, size_t len)
1912{
1913 return codec->bulk_write_raw(codec, reg, data, len);
1914}
1915EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
1916
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001917/**
1918 * snd_soc_update_bits - update codec register bits
1919 * @codec: audio codec
1920 * @reg: codec register
1921 * @mask: register mask
1922 * @value: new value
1923 *
1924 * Writes new register value.
1925 *
Timur Tabi180c3292011-01-10 15:58:13 -06001926 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001927 */
1928int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001929 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001930{
Mark Brown8a713da2011-12-03 12:33:55 +00001931 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001932 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06001933 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001934
Mark Brown8a713da2011-12-03 12:33:55 +00001935 if (codec->using_regmap) {
1936 ret = regmap_update_bits_check(codec->control_data, reg,
1937 mask, value, &change);
1938 } else {
1939 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06001940 if (ret < 0)
1941 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00001942
1943 old = ret;
1944 new = (old & ~mask) | (value & mask);
1945 change = old != new;
1946 if (change)
1947 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06001948 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001949
Mark Brown8a713da2011-12-03 12:33:55 +00001950 if (ret < 0)
1951 return ret;
1952
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001953 return change;
1954}
1955EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1956
1957/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001958 * snd_soc_update_bits_locked - update codec register bits
1959 * @codec: audio codec
1960 * @reg: codec register
1961 * @mask: register mask
1962 * @value: new value
1963 *
1964 * Writes new register value, and takes the codec mutex.
1965 *
1966 * Returns 1 for change else 0.
1967 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001968int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1969 unsigned short reg, unsigned int mask,
1970 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001971{
1972 int change;
1973
1974 mutex_lock(&codec->mutex);
1975 change = snd_soc_update_bits(codec, reg, mask, value);
1976 mutex_unlock(&codec->mutex);
1977
1978 return change;
1979}
Mark Browndd1b3d52009-12-04 14:22:03 +00001980EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001981
1982/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001983 * snd_soc_test_bits - test register for change
1984 * @codec: audio codec
1985 * @reg: codec register
1986 * @mask: register mask
1987 * @value: new value
1988 *
1989 * Tests a register with a new value and checks if the new value is
1990 * different from the old value.
1991 *
1992 * Returns 1 for change else 0.
1993 */
1994int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001995 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001996{
1997 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001998 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001999
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002000 old = snd_soc_read(codec, reg);
2001 new = (old & ~mask) | value;
2002 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002003
2004 return change;
2005}
2006EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2007
2008/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002009 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2010 * @substream: the pcm substream
2011 * @hw: the hardware parameters
2012 *
2013 * Sets the substream runtime hardware parameters.
2014 */
2015int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2016 const struct snd_pcm_hardware *hw)
2017{
2018 struct snd_pcm_runtime *runtime = substream->runtime;
2019 runtime->hw.info = hw->info;
2020 runtime->hw.formats = hw->formats;
2021 runtime->hw.period_bytes_min = hw->period_bytes_min;
2022 runtime->hw.period_bytes_max = hw->period_bytes_max;
2023 runtime->hw.periods_min = hw->periods_min;
2024 runtime->hw.periods_max = hw->periods_max;
2025 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2026 runtime->hw.fifo_size = hw->fifo_size;
2027 return 0;
2028}
2029EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2030
2031/**
2032 * snd_soc_cnew - create new control
2033 * @_template: control template
2034 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002035 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002036 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002037 *
2038 * Create a new mixer control from a template control.
2039 *
2040 * Returns 0 for success, else error.
2041 */
2042struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002043 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002044 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002045{
2046 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002047 struct snd_kcontrol *kcontrol;
2048 char *name = NULL;
2049 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002050
2051 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002052 template.index = 0;
2053
Mark Brownefb7ac32011-03-08 17:23:24 +00002054 if (!long_name)
2055 long_name = template.name;
2056
2057 if (prefix) {
2058 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08002059 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00002060 if (!name)
2061 return NULL;
2062
2063 snprintf(name, name_len, "%s %s", prefix, long_name);
2064
2065 template.name = name;
2066 } else {
2067 template.name = long_name;
2068 }
2069
2070 kcontrol = snd_ctl_new1(&template, data);
2071
2072 kfree(name);
2073
2074 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002075}
2076EXPORT_SYMBOL_GPL(snd_soc_cnew);
2077
Liam Girdwood022658b2012-02-03 17:43:09 +00002078static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2079 const struct snd_kcontrol_new *controls, int num_controls,
2080 const char *prefix, void *data)
2081{
2082 int err, i;
2083
2084 for (i = 0; i < num_controls; i++) {
2085 const struct snd_kcontrol_new *control = &controls[i];
2086 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2087 control->name, prefix));
2088 if (err < 0) {
2089 dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2090 return err;
2091 }
2092 }
2093
2094 return 0;
2095}
2096
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002097/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002098 * snd_soc_add_codec_controls - add an array of controls to a codec.
2099 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002100 * duplicating this code.
2101 *
2102 * @codec: codec to add controls to
2103 * @controls: array of controls to add
2104 * @num_controls: number of elements in the array
2105 *
2106 * Return 0 for success, else error.
2107 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002108int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002109 const struct snd_kcontrol_new *controls, int num_controls)
2110{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002111 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002112
Liam Girdwood022658b2012-02-03 17:43:09 +00002113 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2114 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002115}
Liam Girdwood022658b2012-02-03 17:43:09 +00002116EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002117
2118/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002119 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002120 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002121 *
2122 * @platform: platform to add controls to
2123 * @controls: array of controls to add
2124 * @num_controls: number of elements in the array
2125 *
2126 * Return 0 for success, else error.
2127 */
2128int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2129 const struct snd_kcontrol_new *controls, int num_controls)
2130{
2131 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002132
Liam Girdwood022658b2012-02-03 17:43:09 +00002133 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2134 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002135}
2136EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2137
2138/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002139 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2140 * Convenience function to add a list of controls.
2141 *
2142 * @soc_card: SoC card to add controls to
2143 * @controls: array of controls to add
2144 * @num_controls: number of elements in the array
2145 *
2146 * Return 0 for success, else error.
2147 */
2148int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2149 const struct snd_kcontrol_new *controls, int num_controls)
2150{
2151 struct snd_card *card = soc_card->snd_card;
2152
2153 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2154 NULL, soc_card);
2155}
2156EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2157
2158/**
2159 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2160 * Convienience function to add a list of controls.
2161 *
2162 * @dai: DAI to add controls to
2163 * @controls: array of controls to add
2164 * @num_controls: number of elements in the array
2165 *
2166 * Return 0 for success, else error.
2167 */
2168int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2169 const struct snd_kcontrol_new *controls, int num_controls)
2170{
2171 struct snd_card *card = dai->card->snd_card;
2172
2173 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2174 NULL, dai);
2175}
2176EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2177
2178/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002179 * snd_soc_info_enum_double - enumerated double mixer info callback
2180 * @kcontrol: mixer control
2181 * @uinfo: control element information
2182 *
2183 * Callback to provide information about a double enumerated
2184 * mixer control.
2185 *
2186 * Returns 0 for success.
2187 */
2188int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2189 struct snd_ctl_elem_info *uinfo)
2190{
2191 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2192
2193 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2194 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002195 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002196
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002197 if (uinfo->value.enumerated.item > e->max - 1)
2198 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002199 strcpy(uinfo->value.enumerated.name,
2200 e->texts[uinfo->value.enumerated.item]);
2201 return 0;
2202}
2203EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2204
2205/**
2206 * snd_soc_get_enum_double - enumerated double mixer get callback
2207 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002208 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002209 *
2210 * Callback to get the value of a double enumerated mixer.
2211 *
2212 * Returns 0 for success.
2213 */
2214int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2215 struct snd_ctl_elem_value *ucontrol)
2216{
2217 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2218 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002219 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002220
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002221 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002222 ;
2223 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002224 ucontrol->value.enumerated.item[0]
2225 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002226 if (e->shift_l != e->shift_r)
2227 ucontrol->value.enumerated.item[1] =
2228 (val >> e->shift_r) & (bitmask - 1);
2229
2230 return 0;
2231}
2232EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2233
2234/**
2235 * snd_soc_put_enum_double - enumerated double mixer put callback
2236 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002237 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002238 *
2239 * Callback to set the value of a double enumerated mixer.
2240 *
2241 * Returns 0 for success.
2242 */
2243int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2244 struct snd_ctl_elem_value *ucontrol)
2245{
2246 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2247 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002248 unsigned int val;
2249 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002250
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002251 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002252 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002253 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002254 return -EINVAL;
2255 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2256 mask = (bitmask - 1) << e->shift_l;
2257 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002258 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002259 return -EINVAL;
2260 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2261 mask |= (bitmask - 1) << e->shift_r;
2262 }
2263
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002264 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002265}
2266EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2267
2268/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002269 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2270 * @kcontrol: mixer control
2271 * @ucontrol: control element information
2272 *
2273 * Callback to get the value of a double semi enumerated mixer.
2274 *
2275 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2276 * used for handling bitfield coded enumeration for example.
2277 *
2278 * Returns 0 for success.
2279 */
2280int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2281 struct snd_ctl_elem_value *ucontrol)
2282{
2283 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002284 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002285 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002286
2287 reg_val = snd_soc_read(codec, e->reg);
2288 val = (reg_val >> e->shift_l) & e->mask;
2289 for (mux = 0; mux < e->max; mux++) {
2290 if (val == e->values[mux])
2291 break;
2292 }
2293 ucontrol->value.enumerated.item[0] = mux;
2294 if (e->shift_l != e->shift_r) {
2295 val = (reg_val >> e->shift_r) & e->mask;
2296 for (mux = 0; mux < e->max; mux++) {
2297 if (val == e->values[mux])
2298 break;
2299 }
2300 ucontrol->value.enumerated.item[1] = mux;
2301 }
2302
2303 return 0;
2304}
2305EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2306
2307/**
2308 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2309 * @kcontrol: mixer control
2310 * @ucontrol: control element information
2311 *
2312 * Callback to set the value of a double semi enumerated mixer.
2313 *
2314 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2315 * used for handling bitfield coded enumeration for example.
2316 *
2317 * Returns 0 for success.
2318 */
2319int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2320 struct snd_ctl_elem_value *ucontrol)
2321{
2322 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002323 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002324 unsigned int val;
2325 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002326
2327 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2328 return -EINVAL;
2329 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2330 mask = e->mask << e->shift_l;
2331 if (e->shift_l != e->shift_r) {
2332 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2333 return -EINVAL;
2334 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2335 mask |= e->mask << e->shift_r;
2336 }
2337
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002338 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002339}
2340EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2341
2342/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002343 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2344 * @kcontrol: mixer control
2345 * @uinfo: control element information
2346 *
2347 * Callback to provide information about an external enumerated
2348 * single mixer.
2349 *
2350 * Returns 0 for success.
2351 */
2352int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2353 struct snd_ctl_elem_info *uinfo)
2354{
2355 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2356
2357 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2358 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002359 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002360
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002361 if (uinfo->value.enumerated.item > e->max - 1)
2362 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002363 strcpy(uinfo->value.enumerated.name,
2364 e->texts[uinfo->value.enumerated.item]);
2365 return 0;
2366}
2367EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2368
2369/**
2370 * snd_soc_info_volsw_ext - external single mixer info callback
2371 * @kcontrol: mixer control
2372 * @uinfo: control element information
2373 *
2374 * Callback to provide information about a single external mixer control.
2375 *
2376 * Returns 0 for success.
2377 */
2378int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2379 struct snd_ctl_elem_info *uinfo)
2380{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002381 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002382
Mark Brownfd5dfad2009-04-15 21:37:46 +01002383 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002384 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2385 else
2386 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2387
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002388 uinfo->count = 1;
2389 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002390 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002391 return 0;
2392}
2393EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2394
2395/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002396 * snd_soc_info_volsw - single mixer info callback
2397 * @kcontrol: mixer control
2398 * @uinfo: control element information
2399 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002400 * Callback to provide information about a single mixer control, or a double
2401 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002402 *
2403 * Returns 0 for success.
2404 */
2405int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2406 struct snd_ctl_elem_info *uinfo)
2407{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002408 struct soc_mixer_control *mc =
2409 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002410 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002411
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002412 if (!mc->platform_max)
2413 mc->platform_max = mc->max;
2414 platform_max = mc->platform_max;
2415
2416 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002417 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2418 else
2419 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2420
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002421 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002422 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002423 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002424 return 0;
2425}
2426EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2427
2428/**
2429 * snd_soc_get_volsw - single mixer get callback
2430 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002431 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002432 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002433 * Callback to get the value of a single mixer control, or a double mixer
2434 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002435 *
2436 * Returns 0 for success.
2437 */
2438int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2439 struct snd_ctl_elem_value *ucontrol)
2440{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002441 struct soc_mixer_control *mc =
2442 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002443 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002444 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002445 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002446 unsigned int shift = mc->shift;
2447 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002448 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002449 unsigned int mask = (1 << fls(max)) - 1;
2450 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002451
2452 ucontrol->value.integer.value[0] =
2453 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002454 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002455 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002456 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002457
2458 if (snd_soc_volsw_is_stereo(mc)) {
2459 if (reg == reg2)
2460 ucontrol->value.integer.value[1] =
2461 (snd_soc_read(codec, reg) >> rshift) & mask;
2462 else
2463 ucontrol->value.integer.value[1] =
2464 (snd_soc_read(codec, reg2) >> shift) & mask;
2465 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002466 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002467 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002468 }
2469
2470 return 0;
2471}
2472EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2473
2474/**
2475 * snd_soc_put_volsw - single mixer put callback
2476 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002477 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002478 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002479 * Callback to set the value of a single mixer control, or a double mixer
2480 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002481 *
2482 * Returns 0 for success.
2483 */
2484int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2485 struct snd_ctl_elem_value *ucontrol)
2486{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002487 struct soc_mixer_control *mc =
2488 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002489 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002490 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002491 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002492 unsigned int shift = mc->shift;
2493 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002494 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002495 unsigned int mask = (1 << fls(max)) - 1;
2496 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002497 int err;
2498 bool type_2r = 0;
2499 unsigned int val2 = 0;
2500 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002501
2502 val = (ucontrol->value.integer.value[0] & mask);
2503 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002504 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002505 val_mask = mask << shift;
2506 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002507 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002508 val2 = (ucontrol->value.integer.value[1] & mask);
2509 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002510 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002511 if (reg == reg2) {
2512 val_mask |= mask << rshift;
2513 val |= val2 << rshift;
2514 } else {
2515 val2 = val2 << shift;
2516 type_2r = 1;
2517 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002518 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002519 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002520 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002521 return err;
2522
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002523 if (type_2r)
2524 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2525
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002526 return err;
2527}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002528EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002529
Mark Browne13ac2e2008-05-28 17:58:05 +01002530/**
Brian Austin1d99f242012-03-30 10:43:55 -05002531 * snd_soc_get_volsw_sx - single mixer get callback
2532 * @kcontrol: mixer control
2533 * @ucontrol: control element information
2534 *
2535 * Callback to get the value of a single mixer control, or a double mixer
2536 * control that spans 2 registers.
2537 *
2538 * Returns 0 for success.
2539 */
2540int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2541 struct snd_ctl_elem_value *ucontrol)
2542{
2543 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2544 struct soc_mixer_control *mc =
2545 (struct soc_mixer_control *)kcontrol->private_value;
2546
2547 unsigned int reg = mc->reg;
2548 unsigned int reg2 = mc->rreg;
2549 unsigned int shift = mc->shift;
2550 unsigned int rshift = mc->rshift;
2551 int max = mc->max;
2552 int min = mc->min;
2553 int mask = (1 << (fls(min + max) - 1)) - 1;
2554
2555 ucontrol->value.integer.value[0] =
2556 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2557
2558 if (snd_soc_volsw_is_stereo(mc))
2559 ucontrol->value.integer.value[1] =
2560 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2561
2562 return 0;
2563}
2564EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2565
2566/**
2567 * snd_soc_put_volsw_sx - double mixer set callback
2568 * @kcontrol: mixer control
2569 * @uinfo: control element information
2570 *
2571 * Callback to set the value of a double mixer control that spans 2 registers.
2572 *
2573 * Returns 0 for success.
2574 */
2575int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2576 struct snd_ctl_elem_value *ucontrol)
2577{
2578 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2579 struct soc_mixer_control *mc =
2580 (struct soc_mixer_control *)kcontrol->private_value;
2581
2582 unsigned int reg = mc->reg;
2583 unsigned int reg2 = mc->rreg;
2584 unsigned int shift = mc->shift;
2585 unsigned int rshift = mc->rshift;
2586 int max = mc->max;
2587 int min = mc->min;
2588 int mask = (1 << (fls(min + max) - 1)) - 1;
2589 int err;
2590 unsigned short val, val_mask, val2 = 0;
2591
2592 val_mask = mask << shift;
2593 val = (ucontrol->value.integer.value[0] + min) & mask;
2594 val = val << shift;
2595
2596 if (snd_soc_update_bits_locked(codec, reg, val_mask, val))
2597 return err;
2598
2599 if (snd_soc_volsw_is_stereo(mc)) {
2600 val_mask = mask << rshift;
2601 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2602 val2 = val2 << rshift;
2603
2604 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2605 return err;
2606 }
2607 return 0;
2608}
2609EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2610
2611/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002612 * snd_soc_info_volsw_s8 - signed mixer info callback
2613 * @kcontrol: mixer control
2614 * @uinfo: control element information
2615 *
2616 * Callback to provide information about a signed mixer control.
2617 *
2618 * Returns 0 for success.
2619 */
2620int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2621 struct snd_ctl_elem_info *uinfo)
2622{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002623 struct soc_mixer_control *mc =
2624 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002625 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002626 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002627
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002628 if (!mc->platform_max)
2629 mc->platform_max = mc->max;
2630 platform_max = mc->platform_max;
2631
Mark Browne13ac2e2008-05-28 17:58:05 +01002632 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2633 uinfo->count = 2;
2634 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002635 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002636 return 0;
2637}
2638EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2639
2640/**
2641 * snd_soc_get_volsw_s8 - signed mixer get callback
2642 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002643 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002644 *
2645 * Callback to get the value of a signed mixer control.
2646 *
2647 * Returns 0 for success.
2648 */
2649int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2650 struct snd_ctl_elem_value *ucontrol)
2651{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002652 struct soc_mixer_control *mc =
2653 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002654 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002655 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002656 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002657 int val = snd_soc_read(codec, reg);
2658
2659 ucontrol->value.integer.value[0] =
2660 ((signed char)(val & 0xff))-min;
2661 ucontrol->value.integer.value[1] =
2662 ((signed char)((val >> 8) & 0xff))-min;
2663 return 0;
2664}
2665EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2666
2667/**
2668 * snd_soc_put_volsw_sgn - signed mixer put callback
2669 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002670 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002671 *
2672 * Callback to set the value of a signed mixer control.
2673 *
2674 * Returns 0 for success.
2675 */
2676int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2677 struct snd_ctl_elem_value *ucontrol)
2678{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002679 struct soc_mixer_control *mc =
2680 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002681 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002682 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002683 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002684 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002685
2686 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2687 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2688
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002689 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002690}
2691EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2692
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002693/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002694 * snd_soc_limit_volume - Set new limit to an existing volume control.
2695 *
2696 * @codec: where to look for the control
2697 * @name: Name of the control
2698 * @max: new maximum limit
2699 *
2700 * Return 0 for success, else error.
2701 */
2702int snd_soc_limit_volume(struct snd_soc_codec *codec,
2703 const char *name, int max)
2704{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002705 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002706 struct snd_kcontrol *kctl;
2707 struct soc_mixer_control *mc;
2708 int found = 0;
2709 int ret = -EINVAL;
2710
2711 /* Sanity check for name and max */
2712 if (unlikely(!name || max <= 0))
2713 return -EINVAL;
2714
2715 list_for_each_entry(kctl, &card->controls, list) {
2716 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2717 found = 1;
2718 break;
2719 }
2720 }
2721 if (found) {
2722 mc = (struct soc_mixer_control *)kctl->private_value;
2723 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002724 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002725 ret = 0;
2726 }
2727 }
2728 return ret;
2729}
2730EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2731
Mark Brown71d08512011-10-10 18:31:26 +01002732int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2733 struct snd_ctl_elem_info *uinfo)
2734{
2735 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2736 struct soc_bytes *params = (void *)kcontrol->private_value;
2737
2738 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2739 uinfo->count = params->num_regs * codec->val_bytes;
2740
2741 return 0;
2742}
2743EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
2744
2745int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
2746 struct snd_ctl_elem_value *ucontrol)
2747{
2748 struct soc_bytes *params = (void *)kcontrol->private_value;
2749 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2750 int ret;
2751
2752 if (codec->using_regmap)
2753 ret = regmap_raw_read(codec->control_data, params->base,
2754 ucontrol->value.bytes.data,
2755 params->num_regs * codec->val_bytes);
2756 else
2757 ret = -EINVAL;
2758
Mark Brownf831b052012-02-17 16:20:33 -08002759 /* Hide any masked bytes to ensure consistent data reporting */
2760 if (ret == 0 && params->mask) {
2761 switch (codec->val_bytes) {
2762 case 1:
2763 ucontrol->value.bytes.data[0] &= ~params->mask;
2764 break;
2765 case 2:
2766 ((u16 *)(&ucontrol->value.bytes.data))[0]
2767 &= ~params->mask;
2768 break;
2769 case 4:
2770 ((u32 *)(&ucontrol->value.bytes.data))[0]
2771 &= ~params->mask;
2772 break;
2773 default:
2774 return -EINVAL;
2775 }
2776 }
2777
Mark Brown71d08512011-10-10 18:31:26 +01002778 return ret;
2779}
2780EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
2781
2782int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
2783 struct snd_ctl_elem_value *ucontrol)
2784{
2785 struct soc_bytes *params = (void *)kcontrol->private_value;
2786 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08002787 int ret, len;
2788 unsigned int val;
2789 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01002790
Mark Brownf831b052012-02-17 16:20:33 -08002791 if (!codec->using_regmap)
2792 return -EINVAL;
2793
2794 data = ucontrol->value.bytes.data;
2795 len = params->num_regs * codec->val_bytes;
2796
2797 /*
2798 * If we've got a mask then we need to preserve the register
2799 * bits. We shouldn't modify the incoming data so take a
2800 * copy.
2801 */
2802 if (params->mask) {
2803 ret = regmap_read(codec->control_data, params->base, &val);
2804 if (ret != 0)
2805 return ret;
2806
2807 val &= params->mask;
2808
2809 data = kmemdup(data, len, GFP_KERNEL);
2810 if (!data)
2811 return -ENOMEM;
2812
2813 switch (codec->val_bytes) {
2814 case 1:
2815 ((u8 *)data)[0] &= ~params->mask;
2816 ((u8 *)data)[0] |= val;
2817 break;
2818 case 2:
2819 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
2820 ((u16 *)data)[0] |= cpu_to_be16(val);
2821 break;
2822 case 4:
2823 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
2824 ((u32 *)data)[0] |= cpu_to_be32(val);
2825 break;
2826 default:
2827 return -EINVAL;
2828 }
2829 }
2830
2831 ret = regmap_raw_write(codec->control_data, params->base,
2832 data, len);
2833
2834 if (params->mask)
2835 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01002836
2837 return ret;
2838}
2839EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
2840
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002841/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002842 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2843 * @dai: DAI
2844 * @clk_id: DAI specific clock ID
2845 * @freq: new clock frequency in Hz
2846 * @dir: new clock direction - input/output.
2847 *
2848 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2849 */
2850int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2851 unsigned int freq, int dir)
2852{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002853 if (dai->driver && dai->driver->ops->set_sysclk)
2854 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002855 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002856 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00002857 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002858 else
2859 return -EINVAL;
2860}
2861EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2862
2863/**
Mark Brownec4ee522011-03-07 20:58:11 +00002864 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2865 * @codec: CODEC
2866 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01002867 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00002868 * @freq: new clock frequency in Hz
2869 * @dir: new clock direction - input/output.
2870 *
2871 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2872 */
2873int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01002874 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00002875{
2876 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002877 return codec->driver->set_sysclk(codec, clk_id, source,
2878 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002879 else
2880 return -EINVAL;
2881}
2882EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2883
2884/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002885 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2886 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002887 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002888 * @div: new clock divisor.
2889 *
2890 * Configures the clock dividers. This is used to derive the best DAI bit and
2891 * frame clocks from the system or master clock. It's best to set the DAI bit
2892 * and frame clocks as low as possible to save system power.
2893 */
2894int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2895 int div_id, int div)
2896{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002897 if (dai->driver && dai->driver->ops->set_clkdiv)
2898 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002899 else
2900 return -EINVAL;
2901}
2902EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2903
2904/**
2905 * snd_soc_dai_set_pll - configure DAI PLL.
2906 * @dai: DAI
2907 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002908 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002909 * @freq_in: PLL input clock frequency in Hz
2910 * @freq_out: requested PLL output clock frequency in Hz
2911 *
2912 * Configures and enables PLL to generate output clock based on input clock.
2913 */
Mark Brown85488032009-09-05 18:52:16 +01002914int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2915 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002916{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002917 if (dai->driver && dai->driver->ops->set_pll)
2918 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002919 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00002920 else if (dai->codec && dai->codec->driver->set_pll)
2921 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2922 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002923 else
2924 return -EINVAL;
2925}
2926EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2927
Mark Brownec4ee522011-03-07 20:58:11 +00002928/*
2929 * snd_soc_codec_set_pll - configure codec PLL.
2930 * @codec: CODEC
2931 * @pll_id: DAI specific PLL ID
2932 * @source: DAI specific source for the PLL
2933 * @freq_in: PLL input clock frequency in Hz
2934 * @freq_out: requested PLL output clock frequency in Hz
2935 *
2936 * Configures and enables PLL to generate output clock based on input clock.
2937 */
2938int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2939 unsigned int freq_in, unsigned int freq_out)
2940{
2941 if (codec->driver->set_pll)
2942 return codec->driver->set_pll(codec, pll_id, source,
2943 freq_in, freq_out);
2944 else
2945 return -EINVAL;
2946}
2947EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2948
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002949/**
2950 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2951 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002952 * @fmt: SND_SOC_DAIFMT_ format value.
2953 *
2954 * Configures the DAI hardware format and clocking.
2955 */
2956int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2957{
Shawn Guo5e4ba562012-03-09 00:59:40 +08002958 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002959 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08002960 if (dai->driver->ops->set_fmt == NULL)
2961 return -ENOTSUPP;
2962 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002963}
2964EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2965
2966/**
2967 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2968 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002969 * @tx_mask: bitmask representing active TX slots.
2970 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002971 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002972 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002973 *
2974 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2975 * specific.
2976 */
2977int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002978 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002979{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002980 if (dai->driver && dai->driver->ops->set_tdm_slot)
2981 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002982 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002983 else
2984 return -EINVAL;
2985}
2986EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2987
2988/**
Barry Song472df3c2009-09-12 01:16:29 +08002989 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2990 * @dai: DAI
2991 * @tx_num: how many TX channels
2992 * @tx_slot: pointer to an array which imply the TX slot number channel
2993 * 0~num-1 uses
2994 * @rx_num: how many RX channels
2995 * @rx_slot: pointer to an array which imply the RX slot number channel
2996 * 0~num-1 uses
2997 *
2998 * configure the relationship between channel number and TDM slot number.
2999 */
3000int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3001 unsigned int tx_num, unsigned int *tx_slot,
3002 unsigned int rx_num, unsigned int *rx_slot)
3003{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003004 if (dai->driver && dai->driver->ops->set_channel_map)
3005 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003006 rx_num, rx_slot);
3007 else
3008 return -EINVAL;
3009}
3010EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3011
3012/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003013 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3014 * @dai: DAI
3015 * @tristate: tristate enable
3016 *
3017 * Tristates the DAI so that others can use it.
3018 */
3019int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3020{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003021 if (dai->driver && dai->driver->ops->set_tristate)
3022 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003023 else
3024 return -EINVAL;
3025}
3026EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3027
3028/**
3029 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3030 * @dai: DAI
3031 * @mute: mute enable
3032 *
3033 * Mutes the DAI DAC.
3034 */
3035int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3036{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003037 if (dai->driver && dai->driver->ops->digital_mute)
3038 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003039 else
3040 return -EINVAL;
3041}
3042EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3043
Mark Brownc5af3a22008-11-28 13:29:45 +00003044/**
3045 * snd_soc_register_card - Register a card with the ASoC core
3046 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003047 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003048 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003049 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303050int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003051{
Mark Brownb19e6e72012-03-14 21:18:39 +00003052 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003053
Mark Brownc5af3a22008-11-28 13:29:45 +00003054 if (!card->name || !card->dev)
3055 return -EINVAL;
3056
Stephen Warren5a504962011-12-21 10:40:59 -07003057 for (i = 0; i < card->num_links; i++) {
3058 struct snd_soc_dai_link *link = &card->dai_link[i];
3059
3060 /*
3061 * Codec must be specified by 1 of name or OF node,
3062 * not both or neither.
3063 */
3064 if (!!link->codec_name == !!link->codec_of_node) {
3065 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003066 "Neither/both codec name/of_node are set for %s\n",
3067 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003068 return -EINVAL;
3069 }
3070
3071 /*
3072 * Platform may be specified by either name or OF node, but
3073 * can be left unspecified, and a dummy platform will be used.
3074 */
3075 if (link->platform_name && link->platform_of_node) {
3076 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003077 "Both platform name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003078 return -EINVAL;
3079 }
3080
3081 /*
3082 * CPU DAI must be specified by 1 of name or OF node,
3083 * not both or neither.
3084 */
3085 if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
3086 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003087 "Neither/both cpu_dai name/of_node are set for %s\n",
3088 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003089 return -EINVAL;
3090 }
3091 }
3092
Mark Browned77cc12011-05-03 18:25:34 +01003093 dev_set_drvdata(card->dev, card);
3094
Stephen Warren111c6412011-01-28 14:26:35 -07003095 snd_soc_initialize_card_lists(card);
3096
Vinod Koul150dd2f2011-01-13 22:48:32 +05303097 soc_init_card_debugfs(card);
3098
Mark Brown181a6892012-03-14 20:18:49 +00003099 card->rtd = devm_kzalloc(card->dev,
3100 sizeof(struct snd_soc_pcm_runtime) *
3101 (card->num_links + card->num_aux_devs),
3102 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003103 if (card->rtd == NULL)
3104 return -ENOMEM;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003105 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003106
3107 for (i = 0; i < card->num_links; i++)
3108 card->rtd[i].dai_link = &card->dai_link[i];
3109
Mark Brownc5af3a22008-11-28 13:29:45 +00003110 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003111 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003112 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003113 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003114 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003115
Mark Brownb19e6e72012-03-14 21:18:39 +00003116 ret = snd_soc_instantiate_card(card);
3117 if (ret != 0)
3118 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003119
Mark Brownb19e6e72012-03-14 21:18:39 +00003120 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003121}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303122EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003123
3124/**
3125 * snd_soc_unregister_card - Unregister a card with the ASoC core
3126 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003127 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003128 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003129 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303130int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003131{
Vinod Koulb0e26482011-01-13 22:48:02 +05303132 if (card->instantiated)
3133 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003134 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3135
3136 return 0;
3137}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303138EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003139
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003140/*
3141 * Simplify DAI link configuration by removing ".-1" from device names
3142 * and sanitizing names.
3143 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003144static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003145{
3146 char *found, name[NAME_SIZE];
3147 int id1, id2;
3148
3149 if (dev_name(dev) == NULL)
3150 return NULL;
3151
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003152 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003153
3154 /* are we a "%s.%d" name (platform and SPI components) */
3155 found = strstr(name, dev->driver->name);
3156 if (found) {
3157 /* get ID */
3158 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3159
3160 /* discard ID from name if ID == -1 */
3161 if (*id == -1)
3162 found[strlen(dev->driver->name)] = '\0';
3163 }
3164
3165 } else {
3166 /* I2C component devices are named "bus-addr" */
3167 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3168 char tmp[NAME_SIZE];
3169
3170 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003171 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003172
3173 /* sanitize component name for DAI link creation */
3174 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003175 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003176 } else
3177 *id = 0;
3178 }
3179
3180 return kstrdup(name, GFP_KERNEL);
3181}
3182
3183/*
3184 * Simplify DAI link naming for single devices with multiple DAIs by removing
3185 * any ".-1" and using the DAI name (instead of device name).
3186 */
3187static inline char *fmt_multiple_name(struct device *dev,
3188 struct snd_soc_dai_driver *dai_drv)
3189{
3190 if (dai_drv->name == NULL) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02003191 pr_err("asoc: error - multiple DAI %s registered with no name\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003192 dev_name(dev));
3193 return NULL;
3194 }
3195
3196 return kstrdup(dai_drv->name, GFP_KERNEL);
3197}
3198
Mark Brown91151712008-11-30 23:31:24 +00003199/**
3200 * snd_soc_register_dai - Register a DAI with the ASoC core
3201 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003202 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003203 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003204int snd_soc_register_dai(struct device *dev,
3205 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003206{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003207 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003208
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003209 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003210
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003211 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3212 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003213 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003214
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003215 /* create DAI component name */
3216 dai->name = fmt_single_name(dev, &dai->id);
3217 if (dai->name == NULL) {
3218 kfree(dai);
3219 return -ENOMEM;
3220 }
3221
3222 dai->dev = dev;
3223 dai->driver = dai_drv;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003224 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003225 if (!dai->driver->ops)
3226 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003227
3228 mutex_lock(&client_mutex);
3229 list_add(&dai->list, &dai_list);
3230 mutex_unlock(&client_mutex);
3231
3232 pr_debug("Registered DAI '%s'\n", dai->name);
3233
3234 return 0;
3235}
3236EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3237
3238/**
3239 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3240 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003241 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003242 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003243void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003244{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003245 struct snd_soc_dai *dai;
3246
3247 list_for_each_entry(dai, &dai_list, list) {
3248 if (dev == dai->dev)
3249 goto found;
3250 }
3251 return;
3252
3253found:
Mark Brown91151712008-11-30 23:31:24 +00003254 mutex_lock(&client_mutex);
3255 list_del(&dai->list);
3256 mutex_unlock(&client_mutex);
3257
3258 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003259 kfree(dai->name);
3260 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003261}
3262EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3263
3264/**
3265 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3266 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003267 * @dai: Array of DAIs to register
3268 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003269 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003270int snd_soc_register_dais(struct device *dev,
3271 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003272{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003273 struct snd_soc_dai *dai;
3274 int i, ret = 0;
3275
Liam Girdwood720ffa42010-08-18 00:30:30 +01003276 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003277
3278 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003279
3280 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003281 if (dai == NULL) {
3282 ret = -ENOMEM;
3283 goto err;
3284 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003285
3286 /* create DAI component name */
3287 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3288 if (dai->name == NULL) {
3289 kfree(dai);
3290 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003291 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003292 }
3293
3294 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003295 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003296 if (dai->driver->id)
3297 dai->id = dai->driver->id;
3298 else
3299 dai->id = i;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003300 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003301 if (!dai->driver->ops)
3302 dai->driver->ops = &null_dai_ops;
3303
3304 mutex_lock(&client_mutex);
3305 list_add(&dai->list, &dai_list);
3306 mutex_unlock(&client_mutex);
3307
3308 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003309 }
3310
3311 return 0;
3312
3313err:
3314 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003315 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003316
3317 return ret;
3318}
3319EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3320
3321/**
3322 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3323 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003324 * @dai: Array of DAIs to unregister
3325 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003326 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003327void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003328{
3329 int i;
3330
3331 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003332 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003333}
3334EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3335
Mark Brown12a48a8c2008-12-03 19:40:30 +00003336/**
3337 * snd_soc_register_platform - Register a platform with the ASoC core
3338 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003339 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003340 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003341int snd_soc_register_platform(struct device *dev,
3342 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003343{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003344 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003345
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003346 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3347
3348 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3349 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003350 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003351
3352 /* create platform component name */
3353 platform->name = fmt_single_name(dev, &platform->id);
3354 if (platform->name == NULL) {
3355 kfree(platform);
3356 return -ENOMEM;
3357 }
3358
3359 platform->dev = dev;
3360 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003361 platform->dapm.dev = dev;
3362 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003363 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00003364 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003365
3366 mutex_lock(&client_mutex);
3367 list_add(&platform->list, &platform_list);
3368 mutex_unlock(&client_mutex);
3369
3370 pr_debug("Registered platform '%s'\n", platform->name);
3371
3372 return 0;
3373}
3374EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3375
3376/**
3377 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3378 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003379 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003380 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003381void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003382{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003383 struct snd_soc_platform *platform;
3384
3385 list_for_each_entry(platform, &platform_list, list) {
3386 if (dev == platform->dev)
3387 goto found;
3388 }
3389 return;
3390
3391found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003392 mutex_lock(&client_mutex);
3393 list_del(&platform->list);
3394 mutex_unlock(&client_mutex);
3395
3396 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003397 kfree(platform->name);
3398 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003399}
3400EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3401
Mark Brown151ab222009-05-09 16:22:58 +01003402static u64 codec_format_map[] = {
3403 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3404 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3405 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3406 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3407 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3408 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3409 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3410 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3411 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3412 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3413 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3414 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3415 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3416 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3417 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3418 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3419};
3420
3421/* Fix up the DAI formats for endianness: codecs don't actually see
3422 * the endianness of the data but we're using the CPU format
3423 * definitions which do need to include endianness so we ensure that
3424 * codec DAIs always have both big and little endian variants set.
3425 */
3426static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3427{
3428 int i;
3429
3430 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3431 if (stream->formats & codec_format_map[i])
3432 stream->formats |= codec_format_map[i];
3433}
3434
Mark Brown0d0cf002008-12-10 14:32:45 +00003435/**
3436 * snd_soc_register_codec - Register a codec with the ASoC core
3437 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003438 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003439 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003440int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003441 const struct snd_soc_codec_driver *codec_drv,
3442 struct snd_soc_dai_driver *dai_drv,
3443 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003444{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003445 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003446 struct snd_soc_codec *codec;
3447 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003448
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003449 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003450
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003451 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3452 if (codec == NULL)
3453 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003454
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003455 /* create CODEC component name */
3456 codec->name = fmt_single_name(dev, &codec->id);
3457 if (codec->name == NULL) {
3458 kfree(codec);
3459 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003460 }
3461
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003462 if (codec_drv->compress_type)
3463 codec->compress_type = codec_drv->compress_type;
3464 else
3465 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3466
Mark Brownc3acec22010-12-02 16:15:29 +00003467 codec->write = codec_drv->write;
3468 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003469 codec->volatile_register = codec_drv->volatile_register;
3470 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003471 codec->writable_register = codec_drv->writable_register;
Mark Brown5124e692012-02-08 13:20:50 +00003472 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003473 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3474 codec->dapm.dev = dev;
3475 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003476 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003477 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003478 codec->dev = dev;
3479 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003480 codec->num_dai = num_dai;
3481 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003482
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003483 /* allocate CODEC register cache */
3484 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003485 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003486 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003487 /* it is necessary to make a copy of the default register cache
3488 * because in the case of using a compression type that requires
3489 * the default register cache to be marked as __devinitconst the
3490 * kernel might have freed the array by the time we initialize
3491 * the cache.
3492 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003493 if (codec_drv->reg_cache_default) {
3494 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3495 reg_size, GFP_KERNEL);
3496 if (!codec->reg_def_copy) {
3497 ret = -ENOMEM;
3498 goto fail;
3499 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003500 }
3501 }
3502
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003503 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3504 if (!codec->volatile_register)
3505 codec->volatile_register = snd_soc_default_volatile_register;
3506 if (!codec->readable_register)
3507 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003508 if (!codec->writable_register)
3509 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003510 }
3511
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003512 for (i = 0; i < num_dai; i++) {
3513 fixup_codec_formats(&dai_drv[i].playback);
3514 fixup_codec_formats(&dai_drv[i].capture);
3515 }
3516
Mark Brown26b01cc2010-08-18 20:20:55 +01003517 /* register any DAIs */
3518 if (num_dai) {
3519 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3520 if (ret < 0)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003521 goto fail;
Mark Brown26b01cc2010-08-18 20:20:55 +01003522 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003523
Mark Brown0d0cf002008-12-10 14:32:45 +00003524 mutex_lock(&client_mutex);
3525 list_add(&codec->list, &codec_list);
Mark Brown0d0cf002008-12-10 14:32:45 +00003526 mutex_unlock(&client_mutex);
3527
3528 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003529 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003530
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003531fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003532 kfree(codec->reg_def_copy);
3533 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003534 kfree(codec->name);
3535 kfree(codec);
3536 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003537}
3538EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3539
3540/**
3541 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3542 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003543 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003544 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003545void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003546{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003547 struct snd_soc_codec *codec;
3548 int i;
3549
3550 list_for_each_entry(codec, &codec_list, list) {
3551 if (dev == codec->dev)
3552 goto found;
3553 }
3554 return;
3555
3556found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003557 if (codec->num_dai)
3558 for (i = 0; i < codec->num_dai; i++)
3559 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003560
Mark Brown0d0cf002008-12-10 14:32:45 +00003561 mutex_lock(&client_mutex);
3562 list_del(&codec->list);
3563 mutex_unlock(&client_mutex);
3564
3565 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003566
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003567 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003568 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003569 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003570 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003571}
3572EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3573
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003574/* Retrieve a card's name from device tree */
3575int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3576 const char *propname)
3577{
3578 struct device_node *np = card->dev->of_node;
3579 int ret;
3580
3581 ret = of_property_read_string_index(np, propname, 0, &card->name);
3582 /*
3583 * EINVAL means the property does not exist. This is fine providing
3584 * card->name was previously set, which is checked later in
3585 * snd_soc_register_card.
3586 */
3587 if (ret < 0 && ret != -EINVAL) {
3588 dev_err(card->dev,
3589 "Property '%s' could not be read: %d\n",
3590 propname, ret);
3591 return ret;
3592 }
3593
3594 return 0;
3595}
3596EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3597
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003598int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3599 const char *propname)
3600{
3601 struct device_node *np = card->dev->of_node;
3602 int num_routes;
3603 struct snd_soc_dapm_route *routes;
3604 int i, ret;
3605
3606 num_routes = of_property_count_strings(np, propname);
3607 if (num_routes & 1) {
3608 dev_err(card->dev,
3609 "Property '%s's length is not even\n",
3610 propname);
3611 return -EINVAL;
3612 }
3613 num_routes /= 2;
3614 if (!num_routes) {
3615 dev_err(card->dev,
3616 "Property '%s's length is zero\n",
3617 propname);
3618 return -EINVAL;
3619 }
3620
3621 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3622 GFP_KERNEL);
3623 if (!routes) {
3624 dev_err(card->dev,
3625 "Could not allocate DAPM route table\n");
3626 return -EINVAL;
3627 }
3628
3629 for (i = 0; i < num_routes; i++) {
3630 ret = of_property_read_string_index(np, propname,
3631 2 * i, &routes[i].sink);
3632 if (ret) {
3633 dev_err(card->dev,
3634 "Property '%s' index %d could not be read: %d\n",
3635 propname, 2 * i, ret);
3636 return -EINVAL;
3637 }
3638 ret = of_property_read_string_index(np, propname,
3639 (2 * i) + 1, &routes[i].source);
3640 if (ret) {
3641 dev_err(card->dev,
3642 "Property '%s' index %d could not be read: %d\n",
3643 propname, (2 * i) + 1, ret);
3644 return -EINVAL;
3645 }
3646 }
3647
3648 card->num_dapm_routes = num_routes;
3649 card->dapm_routes = routes;
3650
3651 return 0;
3652}
3653EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3654
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003655static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003656{
Mark Brown384c89e2008-12-03 17:34:03 +00003657#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003658 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3659 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02003660 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00003661 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00003662 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003663
Mark Brown8a9dab12011-01-10 22:25:21 +00003664 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01003665 &codec_list_fops))
3666 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3667
Mark Brown8a9dab12011-01-10 22:25:21 +00003668 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01003669 &dai_list_fops))
3670 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003671
Mark Brown8a9dab12011-01-10 22:25:21 +00003672 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01003673 &platform_list_fops))
3674 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003675#endif
3676
Mark Brownfb257892011-04-28 10:57:54 +01003677 snd_soc_util_init();
3678
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003679 return platform_driver_register(&soc_driver);
3680}
Mark Brown4abe8e12010-10-12 17:41:03 +01003681module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003682
Mark Brown7d8c16a2008-11-30 22:11:24 +00003683static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003684{
Mark Brownfb257892011-04-28 10:57:54 +01003685 snd_soc_util_exit();
3686
Mark Brown384c89e2008-12-03 17:34:03 +00003687#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003688 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00003689#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003690 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003691}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003692module_exit(snd_soc_exit);
3693
3694/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003695MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003696MODULE_DESCRIPTION("ALSA SoC Core");
3697MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003698MODULE_ALIAS("platform:soc-audio");