blob: 77f35292f1b072d31af32c64babb36b6023acef7 [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
Mark Brown2624d5f2009-11-03 21:56:13 +0000203static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000204 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000205{
206 ssize_t ret;
207 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000208 char *buf;
209
210 if (*ppos < 0 || !count)
211 return -EINVAL;
212
213 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000214 if (!buf)
215 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000216
217 ret = soc_codec_reg_show(codec, buf, count, *ppos);
218 if (ret >= 0) {
219 if (copy_to_user(user_buf, buf, ret)) {
220 kfree(buf);
221 return -EFAULT;
222 }
223 *ppos += ret;
224 }
225
Mark Brown2624d5f2009-11-03 21:56:13 +0000226 kfree(buf);
227 return ret;
228}
229
230static ssize_t codec_reg_write_file(struct file *file,
231 const char __user *user_buf, size_t count, loff_t *ppos)
232{
233 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700234 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000235 char *start = buf;
236 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000237 struct snd_soc_codec *codec = file->private_data;
238
239 buf_size = min(count, (sizeof(buf)-1));
240 if (copy_from_user(buf, user_buf, buf_size))
241 return -EFAULT;
242 buf[buf_size] = 0;
243
Mark Brown2624d5f2009-11-03 21:56:13 +0000244 while (*start == ' ')
245 start++;
246 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000247 while (*start == ' ')
248 start++;
249 if (strict_strtoul(start, 16, &value))
250 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000251
252 /* Userspace has been fiddling around behind the kernel's back */
253 add_taint(TAINT_USER);
254
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000255 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000256 return buf_size;
257}
258
259static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700260 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000261 .read = codec_reg_read_file,
262 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200263 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000264};
265
266static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
267{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200268 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
269
270 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
271 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000272 if (!codec->debugfs_codec_root) {
Liam Girdwood64e60f92012-02-15 15:15:37 +0000273 dev_warn(codec->dev, "Failed to create codec debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000274 return;
275 }
276
Mark Brownaaee8ef2011-01-26 20:53:50 +0000277 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
278 &codec->cache_sync);
279 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
280 &codec->cache_only);
281
Mark Brown2624d5f2009-11-03 21:56:13 +0000282 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
283 codec->debugfs_codec_root,
284 codec, &codec_reg_fops);
285 if (!codec->debugfs_reg)
Liam Girdwood64e60f92012-02-15 15:15:37 +0000286 dev_warn(codec->dev, "Failed to create codec register debugfs file\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000287
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200288 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000289}
290
291static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
292{
293 debugfs_remove_recursive(codec->debugfs_codec_root);
294}
295
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000296static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
297{
298 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
299
300 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
301 debugfs_card_root);
302 if (!platform->debugfs_platform_root) {
303 dev_warn(platform->dev,
304 "Failed to create platform debugfs directory\n");
305 return;
306 }
307
308 snd_soc_dapm_debugfs_init(&platform->dapm,
309 platform->debugfs_platform_root);
310}
311
312static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
313{
314 debugfs_remove_recursive(platform->debugfs_platform_root);
315}
316
Mark Brownc3c5a192010-09-15 18:15:14 +0100317static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
318 size_t count, loff_t *ppos)
319{
320 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100321 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100322 struct snd_soc_codec *codec;
323
324 if (!buf)
325 return -ENOMEM;
326
Mark Brown2b194f9d2010-10-13 10:52:16 +0100327 list_for_each_entry(codec, &codec_list, list) {
328 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
329 codec->name);
330 if (len >= 0)
331 ret += len;
332 if (ret > PAGE_SIZE) {
333 ret = PAGE_SIZE;
334 break;
335 }
336 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100337
338 if (ret >= 0)
339 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
340
341 kfree(buf);
342
343 return ret;
344}
345
346static const struct file_operations codec_list_fops = {
347 .read = codec_list_read_file,
348 .llseek = default_llseek,/* read accesses f_pos */
349};
350
Mark Brownf3208782010-09-15 18:19:07 +0100351static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
352 size_t count, loff_t *ppos)
353{
354 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100355 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100356 struct snd_soc_dai *dai;
357
358 if (!buf)
359 return -ENOMEM;
360
Mark Brown2b194f9d2010-10-13 10:52:16 +0100361 list_for_each_entry(dai, &dai_list, list) {
362 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
363 if (len >= 0)
364 ret += len;
365 if (ret > PAGE_SIZE) {
366 ret = PAGE_SIZE;
367 break;
368 }
369 }
Mark Brownf3208782010-09-15 18:19:07 +0100370
Mark Brown2b194f9d2010-10-13 10:52:16 +0100371 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100372
373 kfree(buf);
374
375 return ret;
376}
377
378static const struct file_operations dai_list_fops = {
379 .read = dai_list_read_file,
380 .llseek = default_llseek,/* read accesses f_pos */
381};
382
Mark Brown19c7ac22010-09-15 18:22:40 +0100383static ssize_t platform_list_read_file(struct file *file,
384 char __user *user_buf,
385 size_t count, loff_t *ppos)
386{
387 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100388 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100389 struct snd_soc_platform *platform;
390
391 if (!buf)
392 return -ENOMEM;
393
Mark Brown2b194f9d2010-10-13 10:52:16 +0100394 list_for_each_entry(platform, &platform_list, list) {
395 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
396 platform->name);
397 if (len >= 0)
398 ret += len;
399 if (ret > PAGE_SIZE) {
400 ret = PAGE_SIZE;
401 break;
402 }
403 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100404
Mark Brown2b194f9d2010-10-13 10:52:16 +0100405 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100406
407 kfree(buf);
408
409 return ret;
410}
411
412static const struct file_operations platform_list_fops = {
413 .read = platform_list_read_file,
414 .llseek = default_llseek,/* read accesses f_pos */
415};
416
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200417static void soc_init_card_debugfs(struct snd_soc_card *card)
418{
419 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000420 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200421 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200422 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100423 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200424 return;
425 }
426
427 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
428 card->debugfs_card_root,
429 &card->pop_time);
430 if (!card->debugfs_pop_time)
431 dev_warn(card->dev,
432 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200433}
434
435static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
436{
437 debugfs_remove_recursive(card->debugfs_card_root);
438}
439
Mark Brown2624d5f2009-11-03 21:56:13 +0000440#else
441
442static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
443{
444}
445
446static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
447{
448}
Axel Linb95fccb2010-11-09 17:06:44 +0800449
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000450static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
451{
452}
453
454static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
455{
456}
457
Axel Linb95fccb2010-11-09 17:06:44 +0800458static inline void soc_init_card_debugfs(struct snd_soc_card *card)
459{
460}
461
462static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
463{
464}
Mark Brown2624d5f2009-11-03 21:56:13 +0000465#endif
466
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200467#ifdef CONFIG_SND_SOC_AC97_BUS
468/* unregister ac97 codec */
469static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
470{
471 if (codec->ac97->dev.bus)
472 device_unregister(&codec->ac97->dev);
473 return 0;
474}
475
476/* stop no dev release warning */
477static void soc_ac97_device_release(struct device *dev){}
478
479/* register ac97 codec to bus */
480static int soc_ac97_dev_register(struct snd_soc_codec *codec)
481{
482 int err;
483
484 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100485 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200486 codec->ac97->dev.release = soc_ac97_device_release;
487
Kay Sieversbb072bf2008-11-02 03:50:35 +0100488 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000489 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200490 err = device_register(&codec->ac97->dev);
491 if (err < 0) {
492 snd_printk(KERN_ERR "Can't register ac97 bus\n");
493 codec->ac97->dev.bus = NULL;
494 return err;
495 }
496 return 0;
497}
498#endif
499
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000500#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200501/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000502int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200503{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000504 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200505 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200506 int i;
507
Daniel Macke3509ff2009-06-03 17:44:49 +0200508 /* If the initialization of this soc device failed, there is no codec
509 * associated with it. Just bail out in this case.
510 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000511 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200512 return 0;
513
Andy Green6ed25972008-06-13 16:24:05 +0100514 /* Due to the resume being scheduled into a workqueue we could
515 * suspend before that's finished - wait for it to complete.
516 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000517 snd_power_lock(card->snd_card);
518 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
519 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100520
521 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000522 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100523
Mark Browna00f90f2010-12-02 16:24:24 +0000524 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000525 for (i = 0; i < card->num_rtd; i++) {
526 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
527 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100528
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000529 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100530 continue;
531
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000532 if (drv->ops->digital_mute && dai->playback_active)
533 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200534 }
535
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100536 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000537 for (i = 0; i < card->num_rtd; i++) {
538 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100539 continue;
540
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000541 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100542 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100543
Mark Brown87506542008-11-18 20:50:34 +0000544 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000545 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200546
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000547 for (i = 0; i < card->num_rtd; i++) {
548 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
549 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100550
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000551 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100552 continue;
553
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000554 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
555 cpu_dai->driver->suspend(cpu_dai);
556 if (platform->driver->suspend && !platform->suspended) {
557 platform->driver->suspend(cpu_dai);
558 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100559 }
560 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200561
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 /* close any waiting streams and save state */
563 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +0100564 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200565 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000566 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100567
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000568 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569
570 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100571 continue;
572
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800573 snd_soc_dapm_stream_event(&card->rtd[i],
574 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800575 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000576
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800577 snd_soc_dapm_stream_event(&card->rtd[i],
578 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800579 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000580 }
581
582 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200583 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000584 /* If there are paths active then the CODEC will be held with
585 * bias _ON and should not be suspended. */
586 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200587 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000588 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000589 /*
590 * If the CODEC is capable of idle
591 * bias off then being in STANDBY
592 * means it's doing something,
593 * otherwise fall through.
594 */
595 if (codec->dapm.idle_bias_off) {
596 dev_dbg(codec->dev,
597 "idle_bias_off CODEC on over suspend\n");
598 break;
599 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000600 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100601 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000602 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900603 codec->cache_sync = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000604 break;
605 default:
606 dev_dbg(codec->dev, "CODEC is on over suspend\n");
607 break;
608 }
609 }
610 }
611
612 for (i = 0; i < card->num_rtd; i++) {
613 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
614
615 if (card->rtd[i].dai_link->ignore_suspend)
616 continue;
617
618 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
619 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200620 }
621
Mark Brown87506542008-11-18 20:50:34 +0000622 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000623 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200624
625 return 0;
626}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000627EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200628
Andy Green6ed25972008-06-13 16:24:05 +0100629/* deferred resume work, so resume can complete before we finished
630 * setting our codec back up, which can be very slow on I2C
631 */
632static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200633{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000634 struct snd_soc_card *card =
635 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200636 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200637 int i;
638
Andy Green6ed25972008-06-13 16:24:05 +0100639 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
640 * so userspace apps are blocked from touching us
641 */
642
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000643 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100644
Mark Brown99497882010-05-07 20:24:05 +0100645 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000646 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100647
Mark Brown87506542008-11-18 20:50:34 +0000648 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000649 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200650
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000651 /* resume AC97 DAIs */
652 for (i = 0; i < card->num_rtd; i++) {
653 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100654
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000655 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100656 continue;
657
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000658 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
659 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200660 }
661
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200662 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000663 /* If the CODEC was idle over suspend then it will have been
664 * left with bias OFF or STANDBY and suspended so we must now
665 * resume. Otherwise the suspend was suppressed.
666 */
667 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200668 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000669 case SND_SOC_BIAS_STANDBY:
670 case SND_SOC_BIAS_OFF:
671 codec->driver->resume(codec);
672 codec->suspended = 0;
673 break;
674 default:
675 dev_dbg(codec->dev, "CODEC was on over suspend\n");
676 break;
677 }
Mark Brown1547aba2010-05-07 21:11:40 +0100678 }
679 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200680
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000681 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100682
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000683 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100684 continue;
685
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800686 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000687 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800688 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000689
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800690 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000691 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800692 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200693 }
694
Mark Brown3ff3f642008-05-19 12:32:25 +0200695 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000696 for (i = 0; i < card->num_rtd; i++) {
697 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
698 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100699
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000700 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100701 continue;
702
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000703 if (drv->ops->digital_mute && dai->playback_active)
704 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200705 }
706
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000707 for (i = 0; i < card->num_rtd; i++) {
708 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
709 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100710
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000711 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100712 continue;
713
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000714 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
715 cpu_dai->driver->resume(cpu_dai);
716 if (platform->driver->resume && platform->suspended) {
717 platform->driver->resume(cpu_dai);
718 platform->suspended = 0;
719 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200720 }
721
Mark Brown87506542008-11-18 20:50:34 +0000722 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000723 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200724
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000725 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100726
727 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000728 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100729}
730
731/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000732int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100733{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000734 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600735 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200736
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800737 /* If the initialization of this soc device failed, there is no codec
738 * associated with it. Just bail out in this case.
739 */
740 if (list_empty(&card->codec_dev_list))
741 return 0;
742
Mark Brown64ab9ba2009-03-31 11:27:03 +0100743 /* AC97 devices might have other drivers hanging off them so
744 * need to resume immediately. Other drivers don't have that
745 * problem and may take a substantial amount of time to resume
746 * due to I/O costs and anti-pop so handle them out of line.
747 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000748 for (i = 0; i < card->num_rtd; i++) {
749 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600750 ac97_control |= cpu_dai->driver->ac97_control;
751 }
752 if (ac97_control) {
753 dev_dbg(dev, "Resuming AC97 immediately\n");
754 soc_resume_deferred(&card->deferred_resume_work);
755 } else {
756 dev_dbg(dev, "Scheduling resume work\n");
757 if (!schedule_work(&card->deferred_resume_work))
758 dev_err(dev, "resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100759 }
Andy Green6ed25972008-06-13 16:24:05 +0100760
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200761 return 0;
762}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000763EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200764#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000765#define snd_soc_suspend NULL
766#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200767#endif
768
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100769static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800770};
771
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000772static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200773{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000774 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
775 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000776 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000777 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000778 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100779 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200780
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000781 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000782
Mark Brownb19e6e72012-03-14 21:18:39 +0000783 /* Find CPU DAI from registered DAIs*/
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000784 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700785 if (dai_link->cpu_dai_of_node) {
786 if (cpu_dai->dev->of_node != dai_link->cpu_dai_of_node)
787 continue;
788 } else {
789 if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
790 continue;
791 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700792
793 rtd->cpu_dai = cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000794 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000795
796 if (!rtd->cpu_dai) {
797 dev_dbg(card->dev, "CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000798 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000799 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000800 }
801
Mark Brownb19e6e72012-03-14 21:18:39 +0000802 /* Find CODEC from registered CODECs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000803 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700804 if (dai_link->codec_of_node) {
805 if (codec->dev->of_node != dai_link->codec_of_node)
806 continue;
807 } else {
808 if (strcmp(codec->name, dai_link->codec_name))
809 continue;
810 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000811
Stephen Warren2610ab72011-12-07 13:58:27 -0700812 rtd->codec = codec;
813
814 /*
815 * CODEC found, so find CODEC DAI from registered DAIs from
816 * this CODEC
817 */
818 list_for_each_entry(codec_dai, &dai_list, list) {
819 if (codec->dev == codec_dai->dev &&
820 !strcmp(codec_dai->name,
821 dai_link->codec_dai_name)) {
822
823 rtd->codec_dai = codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000824 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000825 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000826
827 if (!rtd->codec_dai) {
828 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
Stephen Warren2610ab72011-12-07 13:58:27 -0700829 dai_link->codec_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000830 return -EPROBE_DEFER;
831 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000832 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000833
Mark Brownb19e6e72012-03-14 21:18:39 +0000834 if (!rtd->codec) {
835 dev_dbg(card->dev, "CODEC %s not registered\n",
836 dai_link->codec_name);
837 return -EPROBE_DEFER;
838 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100839
840 /* if there's no platform we match on the empty platform */
841 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700842 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100843 platform_name = "snd-soc-dummy";
844
Mark Brownb19e6e72012-03-14 21:18:39 +0000845 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000846 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700847 if (dai_link->platform_of_node) {
848 if (platform->dev->of_node !=
849 dai_link->platform_of_node)
850 continue;
851 } else {
852 if (strcmp(platform->name, platform_name))
853 continue;
854 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700855
856 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000857 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000858 if (!rtd->platform) {
859 dev_dbg(card->dev, "platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000860 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000861 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000862 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000863
864 card->num_rtd++;
865
866 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000867}
868
Jarkko Nikula589c3562010-12-06 16:27:07 +0200869static void soc_remove_codec(struct snd_soc_codec *codec)
870{
871 int err;
872
873 if (codec->driver->remove) {
874 err = codec->driver->remove(codec);
875 if (err < 0)
876 dev_err(codec->dev,
877 "asoc: failed to remove %s: %d\n",
878 codec->name, err);
879 }
880
881 /* Make sure all DAPM widgets are freed */
882 snd_soc_dapm_free(&codec->dapm);
883
884 soc_cleanup_codec_debugfs(codec);
885 codec->probed = 0;
886 list_del(&codec->card_list);
887 module_put(codec->dev->driver->owner);
888}
889
Liam Girdwood0168bf02011-06-07 16:08:05 +0100890static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000891{
892 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
893 struct snd_soc_codec *codec = rtd->codec;
894 struct snd_soc_platform *platform = rtd->platform;
895 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
896 int err;
897
898 /* unregister the rtd device */
899 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800900 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
901 device_remove_file(rtd->dev, &dev_attr_codec_reg);
902 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000903 rtd->dev_registered = 0;
904 }
905
906 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100907 if (codec_dai && codec_dai->probed &&
908 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000909 if (codec_dai->driver->remove) {
910 err = codec_dai->driver->remove(codec_dai);
911 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200912 pr_err("asoc: failed to remove %s: %d\n",
913 codec_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000914 }
915 codec_dai->probed = 0;
916 list_del(&codec_dai->card_list);
917 }
918
919 /* remove the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100920 if (platform && platform->probed &&
921 platform->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000922 if (platform->driver->remove) {
923 err = platform->driver->remove(platform);
924 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200925 pr_err("asoc: failed to remove %s: %d\n",
926 platform->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000927 }
Liam Girdwood675c4962012-01-16 15:25:37 +0000928
929 /* Make sure all DAPM widgets are freed */
930 snd_soc_dapm_free(&platform->dapm);
931
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000932 soc_cleanup_platform_debugfs(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000933 platform->probed = 0;
934 list_del(&platform->card_list);
935 module_put(platform->dev->driver->owner);
936 }
937
938 /* remove the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100939 if (codec && codec->probed &&
940 codec->driver->remove_order == order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200941 soc_remove_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000942
943 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100944 if (cpu_dai && cpu_dai->probed &&
945 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000946 if (cpu_dai->driver->remove) {
947 err = cpu_dai->driver->remove(cpu_dai);
948 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200949 pr_err("asoc: failed to remove %s: %d\n",
950 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000951 }
952 cpu_dai->probed = 0;
953 list_del(&cpu_dai->card_list);
954 module_put(cpu_dai->dev->driver->owner);
955 }
956}
957
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900958static void soc_remove_dai_links(struct snd_soc_card *card)
959{
Liam Girdwood0168bf02011-06-07 16:08:05 +0100960 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900961
Liam Girdwood0168bf02011-06-07 16:08:05 +0100962 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
963 order++) {
964 for (dai = 0; dai < card->num_rtd; dai++)
965 soc_remove_dai_link(card, dai, order);
966 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900967 card->num_rtd = 0;
968}
969
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200970static void soc_set_name_prefix(struct snd_soc_card *card,
971 struct snd_soc_codec *codec)
972{
973 int i;
974
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000975 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200976 return;
977
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000978 for (i = 0; i < card->num_configs; i++) {
979 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200980 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
981 codec->name_prefix = map->name_prefix;
982 break;
983 }
984 }
985}
986
Jarkko Nikula589c3562010-12-06 16:27:07 +0200987static int soc_probe_codec(struct snd_soc_card *card,
988 struct snd_soc_codec *codec)
989{
990 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +0000991 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -0800992 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +0200993
994 codec->card = card;
995 codec->dapm.card = card;
996 soc_set_name_prefix(card, codec);
997
Jarkko Nikula70d29332011-01-27 16:24:22 +0200998 if (!try_module_get(codec->dev->driver->owner))
999 return -ENODEV;
1000
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001001 soc_init_codec_debugfs(codec);
1002
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001003 if (driver->dapm_widgets)
1004 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1005 driver->num_dapm_widgets);
1006
Mark Brown888df392012-02-16 19:37:51 -08001007 /* Create DAPM widgets for each DAI stream */
1008 list_for_each_entry(dai, &dai_list, list) {
1009 if (dai->dev != codec->dev)
1010 continue;
1011
1012 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1013 }
1014
Mark Brown33c5f962011-08-22 18:40:30 +01001015 codec->dapm.idle_bias_off = driver->idle_bias_off;
1016
Mark Brown89b95ac02011-03-07 16:38:44 +00001017 if (driver->probe) {
1018 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001019 if (ret < 0) {
1020 dev_err(codec->dev,
1021 "asoc: failed to probe CODEC %s: %d\n",
1022 codec->name, ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001023 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001024 }
1025 }
1026
Mark Brownb7af1da2011-04-07 19:18:44 +09001027 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001028 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001029 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001030 if (driver->dapm_routes)
1031 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1032 driver->num_dapm_routes);
1033
Jarkko Nikula589c3562010-12-06 16:27:07 +02001034 /* mark codec as probed and add to card codec list */
1035 codec->probed = 1;
1036 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001037 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001038
Jarkko Nikula70d29332011-01-27 16:24:22 +02001039 return 0;
1040
1041err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001042 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001043 module_put(codec->dev->driver->owner);
1044
Jarkko Nikula589c3562010-12-06 16:27:07 +02001045 return ret;
1046}
1047
Liam Girdwood956245e2011-07-01 16:54:08 +01001048static int soc_probe_platform(struct snd_soc_card *card,
1049 struct snd_soc_platform *platform)
1050{
1051 int ret = 0;
1052 const struct snd_soc_platform_driver *driver = platform->driver;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001053 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001054
1055 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001056 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001057
1058 if (!try_module_get(platform->dev->driver->owner))
1059 return -ENODEV;
1060
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001061 soc_init_platform_debugfs(platform);
1062
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001063 if (driver->dapm_widgets)
1064 snd_soc_dapm_new_controls(&platform->dapm,
1065 driver->dapm_widgets, driver->num_dapm_widgets);
1066
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001067 /* Create DAPM widgets for each DAI stream */
1068 list_for_each_entry(dai, &dai_list, list) {
1069 if (dai->dev != platform->dev)
1070 continue;
1071
1072 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1073 }
1074
Liam Girdwood956245e2011-07-01 16:54:08 +01001075 if (driver->probe) {
1076 ret = driver->probe(platform);
1077 if (ret < 0) {
1078 dev_err(platform->dev,
1079 "asoc: failed to probe platform %s: %d\n",
1080 platform->name, ret);
1081 goto err_probe;
1082 }
1083 }
1084
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001085 if (driver->controls)
1086 snd_soc_add_platform_controls(platform, driver->controls,
1087 driver->num_controls);
1088 if (driver->dapm_routes)
1089 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1090 driver->num_dapm_routes);
1091
Liam Girdwood956245e2011-07-01 16:54:08 +01001092 /* mark platform as probed and add to card platform list */
1093 platform->probed = 1;
1094 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001095 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001096
1097 return 0;
1098
1099err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001100 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001101 module_put(platform->dev->driver->owner);
1102
1103 return ret;
1104}
1105
Mark Brown36ae1a92012-01-06 17:12:45 -08001106static void rtd_release(struct device *dev)
1107{
1108 kfree(dev);
1109}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001110
Jarkko Nikula589c3562010-12-06 16:27:07 +02001111static int soc_post_component_init(struct snd_soc_card *card,
1112 struct snd_soc_codec *codec,
1113 int num, int dailess)
1114{
1115 struct snd_soc_dai_link *dai_link = NULL;
1116 struct snd_soc_aux_dev *aux_dev = NULL;
1117 struct snd_soc_pcm_runtime *rtd;
1118 const char *temp, *name;
1119 int ret = 0;
1120
1121 if (!dailess) {
1122 dai_link = &card->dai_link[num];
1123 rtd = &card->rtd[num];
1124 name = dai_link->name;
1125 } else {
1126 aux_dev = &card->aux_dev[num];
1127 rtd = &card->rtd_aux[num];
1128 name = aux_dev->name;
1129 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001130 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001131
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001132 /* Make sure all DAPM widgets are instantiated */
1133 snd_soc_dapm_new_widgets(&codec->dapm);
1134
Jarkko Nikula589c3562010-12-06 16:27:07 +02001135 /* machine controls, routes and widgets are not prefixed */
1136 temp = codec->name_prefix;
1137 codec->name_prefix = NULL;
1138
1139 /* do machine specific initialization */
1140 if (!dailess && dai_link->init)
1141 ret = dai_link->init(rtd);
1142 else if (dailess && aux_dev->init)
1143 ret = aux_dev->init(&codec->dapm);
1144 if (ret < 0) {
1145 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1146 return ret;
1147 }
1148 codec->name_prefix = temp;
1149
Jarkko Nikula589c3562010-12-06 16:27:07 +02001150 /* register the rtd device */
1151 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001152
1153 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1154 if (!rtd->dev)
1155 return -ENOMEM;
1156 device_initialize(rtd->dev);
1157 rtd->dev->parent = card->dev;
1158 rtd->dev->release = rtd_release;
1159 rtd->dev->init_name = name;
1160 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001161 mutex_init(&rtd->pcm_mutex);
Mark Brown36ae1a92012-01-06 17:12:45 -08001162 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001163 if (ret < 0) {
1164 dev_err(card->dev,
1165 "asoc: failed to register runtime device: %d\n", ret);
1166 return ret;
1167 }
1168 rtd->dev_registered = 1;
1169
1170 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001171 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001172 if (ret < 0)
1173 dev_err(codec->dev,
1174 "asoc: failed to add codec dapm sysfs entries: %d\n",
1175 ret);
1176
1177 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001178 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001179 if (ret < 0)
1180 dev_err(codec->dev,
1181 "asoc: failed to add codec sysfs files: %d\n", ret);
1182
1183 return 0;
1184}
1185
Liam Girdwood0168bf02011-06-07 16:08:05 +01001186static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001187{
1188 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1189 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1190 struct snd_soc_codec *codec = rtd->codec;
1191 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001192 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1193 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1194 struct snd_soc_dapm_widget *play_w, *capture_w;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001195 int ret;
1196
Liam Girdwood0168bf02011-06-07 16:08:05 +01001197 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1198 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001199
1200 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001201 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001202 codec_dai->card = card;
1203 cpu_dai->card = card;
1204
1205 /* set default power off timeout */
1206 rtd->pmdown_time = pmdown_time;
1207
1208 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001209 if (!cpu_dai->probed &&
1210 cpu_dai->driver->probe_order == order) {
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001211 cpu_dai->dapm.card = card;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001212 if (!try_module_get(cpu_dai->dev->driver->owner))
1213 return -ENODEV;
1214
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001215 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1216
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001217 if (cpu_dai->driver->probe) {
1218 ret = cpu_dai->driver->probe(cpu_dai);
1219 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001220 pr_err("asoc: failed to probe CPU DAI %s: %d\n",
1221 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001222 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001223 return ret;
1224 }
1225 }
1226 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001227 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001228 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1229 }
1230
1231 /* probe the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001232 if (!codec->probed &&
1233 codec->driver->probe_order == order) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001234 ret = soc_probe_codec(card, codec);
1235 if (ret < 0)
1236 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001237 }
1238
1239 /* probe the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001240 if (!platform->probed &&
1241 platform->driver->probe_order == order) {
Liam Girdwood956245e2011-07-01 16:54:08 +01001242 ret = soc_probe_platform(card, platform);
1243 if (ret < 0)
1244 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001245 }
1246
1247 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001248 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001249 if (codec_dai->driver->probe) {
1250 ret = codec_dai->driver->probe(codec_dai);
1251 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001252 pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
1253 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001254 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001255 }
1256 }
1257
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001258 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001259 codec_dai->probed = 1;
1260 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001261 }
1262
Liam Girdwood0168bf02011-06-07 16:08:05 +01001263 /* complete DAI probe during last probe */
1264 if (order != SND_SOC_COMP_ORDER_LAST)
1265 return 0;
1266
Jarkko Nikula589c3562010-12-06 16:27:07 +02001267 ret = soc_post_component_init(card, codec, num, 0);
1268 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001269 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001270
Mark Brown36ae1a92012-01-06 17:12:45 -08001271 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001272 if (ret < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -02001273 pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001274
Mark Brownc74184e2012-04-04 22:12:09 +01001275 if (!dai_link->params) {
1276 /* create the pcm */
1277 ret = soc_new_pcm(rtd, num);
1278 if (ret < 0) {
1279 pr_err("asoc: can't create pcm %s :%d\n",
1280 dai_link->stream_name, ret);
1281 return ret;
1282 }
1283 } else {
1284 /* link the DAI widgets */
1285 play_w = codec_dai->playback_widget;
1286 capture_w = cpu_dai->capture_widget;
1287 if (play_w && capture_w) {
1288 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1289 capture_w, play_w);
1290 if (ret != 0) {
1291 dev_err(card->dev, "Can't link %s to %s: %d\n",
1292 play_w->name, capture_w->name, ret);
1293 return ret;
1294 }
1295 }
1296
1297 play_w = cpu_dai->playback_widget;
1298 capture_w = codec_dai->capture_widget;
1299 if (play_w && capture_w) {
1300 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1301 capture_w, play_w);
1302 if (ret != 0) {
1303 dev_err(card->dev, "Can't link %s to %s: %d\n",
1304 play_w->name, capture_w->name, ret);
1305 return ret;
1306 }
1307 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001308 }
1309
1310 /* add platform data for AC97 devices */
1311 if (rtd->codec_dai->driver->ac97_control)
1312 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1313
1314 return 0;
1315}
1316
1317#ifdef CONFIG_SND_SOC_AC97_BUS
1318static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1319{
1320 int ret;
1321
1322 /* Only instantiate AC97 if not already done by the adaptor
1323 * for the generic AC97 subsystem.
1324 */
1325 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001326 /*
1327 * It is possible that the AC97 device is already registered to
1328 * the device subsystem. This happens when the device is created
1329 * via snd_ac97_mixer(). Currently only SoC codec that does so
1330 * is the generic AC97 glue but others migh emerge.
1331 *
1332 * In those cases we don't try to register the device again.
1333 */
1334 if (!rtd->codec->ac97_created)
1335 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001336
1337 ret = soc_ac97_dev_register(rtd->codec);
1338 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001339 pr_err("asoc: AC97 device register failed:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001340 return ret;
1341 }
1342
1343 rtd->codec->ac97_registered = 1;
1344 }
1345 return 0;
1346}
1347
1348static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1349{
1350 if (codec->ac97_registered) {
1351 soc_ac97_dev_unregister(codec);
1352 codec->ac97_registered = 0;
1353 }
1354}
1355#endif
1356
Mark Brownb19e6e72012-03-14 21:18:39 +00001357static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1358{
1359 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1360 struct snd_soc_codec *codec;
1361
1362 /* find CODEC from registered CODECs*/
1363 list_for_each_entry(codec, &codec_list, list) {
1364 if (!strcmp(codec->name, aux_dev->codec_name))
1365 return 0;
1366 }
1367
1368 return -EPROBE_DEFER;
1369}
1370
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001371static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1372{
1373 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001374 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001375 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001376
1377 /* find CODEC from registered CODECs*/
1378 list_for_each_entry(codec, &codec_list, list) {
1379 if (!strcmp(codec->name, aux_dev->codec_name)) {
1380 if (codec->probed) {
1381 dev_err(codec->dev,
1382 "asoc: codec already probed");
1383 ret = -EBUSY;
1384 goto out;
1385 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001386 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001387 }
1388 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001389 /* codec not found */
1390 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001391 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001392
Jarkko Nikula676ad982010-12-03 09:18:22 +02001393found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001394 ret = soc_probe_codec(card, codec);
1395 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001396 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001397
Jarkko Nikula589c3562010-12-06 16:27:07 +02001398 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001399
1400out:
1401 return ret;
1402}
1403
1404static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1405{
1406 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1407 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001408
1409 /* unregister the rtd device */
1410 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001411 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1412 device_del(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001413 rtd->dev_registered = 0;
1414 }
1415
Jarkko Nikula589c3562010-12-06 16:27:07 +02001416 if (codec && codec->probed)
1417 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001418}
1419
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001420static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1421 enum snd_soc_compress_type compress_type)
1422{
1423 int ret;
1424
1425 if (codec->cache_init)
1426 return 0;
1427
1428 /* override the compress_type if necessary */
1429 if (compress_type && codec->compress_type != compress_type)
1430 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001431 ret = snd_soc_cache_init(codec);
1432 if (ret < 0) {
1433 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1434 ret);
1435 return ret;
1436 }
1437 codec->cache_init = 1;
1438 return 0;
1439}
1440
Mark Brownb19e6e72012-03-14 21:18:39 +00001441static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001442{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001443 struct snd_soc_codec *codec;
1444 struct snd_soc_codec_conf *codec_conf;
1445 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001446 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001447 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001448
Liam Girdwood01b9d992012-03-07 10:38:25 +00001449 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001450
Mark Brownb19e6e72012-03-14 21:18:39 +00001451 /* bind DAIs */
1452 for (i = 0; i < card->num_links; i++) {
1453 ret = soc_bind_dai_link(card, i);
1454 if (ret != 0)
1455 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001456 }
1457
Mark Brownb19e6e72012-03-14 21:18:39 +00001458 /* check aux_devs too */
1459 for (i = 0; i < card->num_aux_devs; i++) {
1460 ret = soc_check_aux_dev(card, i);
1461 if (ret != 0)
1462 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001463 }
1464
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001465 /* initialize the register cache for each available codec */
1466 list_for_each_entry(codec, &codec_list, list) {
1467 if (codec->cache_init)
1468 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001469 /* by default we don't override the compress_type */
1470 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001471 /* check to see if we need to override the compress_type */
1472 for (i = 0; i < card->num_configs; ++i) {
1473 codec_conf = &card->codec_conf[i];
1474 if (!strcmp(codec->name, codec_conf->dev_name)) {
1475 compress_type = codec_conf->compress_type;
1476 if (compress_type && compress_type
1477 != codec->compress_type)
1478 break;
1479 }
1480 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001481 ret = snd_soc_init_codec_cache(codec, compress_type);
Mark Brownb19e6e72012-03-14 21:18:39 +00001482 if (ret < 0)
1483 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001484 }
1485
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001486 /* card bind complete so register a sound card */
1487 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1488 card->owner, 0, &card->snd_card);
1489 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001490 pr_err("asoc: can't create sound card for card %s: %d\n",
1491 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001492 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001493 }
1494 card->snd_card->dev = card->dev;
1495
Mark Browne37a4972011-03-02 18:21:57 +00001496 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1497 card->dapm.dev = card->dev;
1498 card->dapm.card = card;
1499 list_add(&card->dapm.list, &card->dapm_list);
1500
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001501#ifdef CONFIG_DEBUG_FS
1502 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1503#endif
1504
Mark Brown88ee1c62011-02-02 10:43:26 +00001505#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001506 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001507 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001508#endif
Andy Green6ed25972008-06-13 16:24:05 +01001509
Mark Brown9a841eb2011-04-12 17:51:37 -07001510 if (card->dapm_widgets)
1511 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1512 card->num_dapm_widgets);
1513
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001514 /* initialise the sound card only once */
1515 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001516 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001517 if (ret < 0)
1518 goto card_probe_error;
1519 }
1520
Liam Girdwood0168bf02011-06-07 16:08:05 +01001521 /* early DAI link probe */
1522 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1523 order++) {
1524 for (i = 0; i < card->num_links; i++) {
1525 ret = soc_probe_dai_link(card, i, order);
1526 if (ret < 0) {
1527 pr_err("asoc: failed to instantiate card %s: %d\n",
Mark Brown321de0d2010-09-21 15:09:37 +01001528 card->name, ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001529 goto probe_dai_err;
1530 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001531 }
1532 }
1533
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001534 for (i = 0; i < card->num_aux_devs; i++) {
1535 ret = soc_probe_aux_dev(card, i);
1536 if (ret < 0) {
1537 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1538 card->name, ret);
1539 goto probe_aux_dev_err;
1540 }
1541 }
1542
Mark Brown888df392012-02-16 19:37:51 -08001543 snd_soc_dapm_link_dai_widgets(card);
1544
Mark Brownb7af1da2011-04-07 19:18:44 +09001545 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001546 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001547
Mark Brownb8ad29d2011-03-02 18:35:51 +00001548 if (card->dapm_routes)
1549 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1550 card->num_dapm_routes);
1551
Mark Brownb90d2f92011-10-10 13:38:06 +01001552 snd_soc_dapm_new_widgets(&card->dapm);
1553
Mark Brown75d9ac42011-09-27 16:41:01 +01001554 for (i = 0; i < card->num_links; i++) {
1555 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001556 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001557
Mark Brownf04209a2012-04-09 16:29:21 +01001558 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001559 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001560 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001561 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001562 dev_warn(card->rtd[i].codec_dai->dev,
1563 "Failed to set DAI format: %d\n",
1564 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001565 }
1566
1567 /* If this is a regular CPU link there will be a platform */
1568 if (dai_fmt && dai_link->platform_name) {
1569 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1570 dai_fmt);
1571 if (ret != 0 && ret != -ENOTSUPP)
1572 dev_warn(card->rtd[i].cpu_dai->dev,
1573 "Failed to set DAI format: %d\n",
1574 ret);
1575 } else if (dai_fmt) {
1576 /* Flip the polarity for the "CPU" end */
1577 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1578 switch (dai_link->dai_fmt &
1579 SND_SOC_DAIFMT_MASTER_MASK) {
1580 case SND_SOC_DAIFMT_CBM_CFM:
1581 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1582 break;
1583 case SND_SOC_DAIFMT_CBM_CFS:
1584 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1585 break;
1586 case SND_SOC_DAIFMT_CBS_CFM:
1587 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1588 break;
1589 case SND_SOC_DAIFMT_CBS_CFS:
1590 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1591 break;
1592 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001593
1594 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001595 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001596 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001597 dev_warn(card->rtd[i].cpu_dai->dev,
1598 "Failed to set DAI format: %d\n",
1599 ret);
1600 }
1601 }
1602
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001603 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001604 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001605 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1606 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001607 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1608 "%s", card->driver_name ? card->driver_name : card->name);
1609 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1610 switch (card->snd_card->driver[i]) {
1611 case '_':
1612 case '-':
1613 case '\0':
1614 break;
1615 default:
1616 if (!isalnum(card->snd_card->driver[i]))
1617 card->snd_card->driver[i] = '_';
1618 break;
1619 }
1620 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001621
Mark Brown28e9ad92011-03-02 18:36:34 +00001622 if (card->late_probe) {
1623 ret = card->late_probe(card);
1624 if (ret < 0) {
1625 dev_err(card->dev, "%s late_probe() failed: %d\n",
1626 card->name, ret);
1627 goto probe_aux_dev_err;
1628 }
1629 }
1630
Mark Brown2dc00212011-10-08 13:59:44 +01001631 snd_soc_dapm_new_widgets(&card->dapm);
1632
Stephen Warren16332812011-11-23 12:42:04 -07001633 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001634 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001635 snd_soc_dapm_auto_nc_codec_pins(codec);
1636
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001637 ret = snd_card_register(card->snd_card);
1638 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001639 pr_err("asoc: failed to register soundcard for %s: %d\n",
1640 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001641 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001642 }
1643
1644#ifdef CONFIG_SND_SOC_AC97_BUS
1645 /* register any AC97 codecs */
1646 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001647 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1648 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001649 pr_err("asoc: failed to register AC97 %s: %d\n",
1650 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001651 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001652 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001653 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001654 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001655 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001656#endif
1657
Mark Brown435c5e22008-12-04 15:32:53 +00001658 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001659 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001660 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001661
1662 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001663
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001664probe_aux_dev_err:
1665 for (i = 0; i < card->num_aux_devs; i++)
1666 soc_remove_aux_dev(card, i);
1667
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001668probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001669 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001670
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001671card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001672 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001673 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001674
1675 snd_card_free(card->snd_card);
1676
Mark Brownb19e6e72012-03-14 21:18:39 +00001677base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001678 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001679
Mark Brownb19e6e72012-03-14 21:18:39 +00001680 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001681}
1682
1683/* probes a new socdev */
1684static int soc_probe(struct platform_device *pdev)
1685{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001686 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001687 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001688
Vinod Koul70a7ca32011-01-14 19:22:48 +05301689 /*
1690 * no card, so machine driver should be registering card
1691 * we should not be here in that case so ret error
1692 */
1693 if (!card)
1694 return -EINVAL;
1695
Mark Brownfe4085e2012-03-02 13:07:41 +00001696 dev_warn(&pdev->dev,
1697 "ASoC machine %s should use snd_soc_register_card()\n",
1698 card->name);
1699
Mark Brown435c5e22008-12-04 15:32:53 +00001700 /* Bodge while we unpick instantiation */
1701 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001702
Mark Brown435c5e22008-12-04 15:32:53 +00001703 ret = snd_soc_register_card(card);
1704 if (ret != 0) {
1705 dev_err(&pdev->dev, "Failed to register card\n");
1706 return ret;
1707 }
1708
1709 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001710}
1711
Vinod Koulb0e26482011-01-13 22:48:02 +05301712static int soc_cleanup_card_resources(struct snd_soc_card *card)
1713{
Vinod Koulb0e26482011-01-13 22:48:02 +05301714 int i;
1715
1716 /* make sure any delayed work runs */
1717 for (i = 0; i < card->num_rtd; i++) {
1718 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1719 flush_delayed_work_sync(&rtd->delayed_work);
1720 }
1721
1722 /* remove auxiliary devices */
1723 for (i = 0; i < card->num_aux_devs; i++)
1724 soc_remove_aux_dev(card, i);
1725
1726 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001727 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301728
1729 soc_cleanup_card_debugfs(card);
1730
1731 /* remove the card */
1732 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001733 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301734
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001735 snd_soc_dapm_free(&card->dapm);
1736
Vinod Koulb0e26482011-01-13 22:48:02 +05301737 snd_card_free(card->snd_card);
1738 return 0;
1739
1740}
1741
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001742/* removes a socdev */
1743static int soc_remove(struct platform_device *pdev)
1744{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001745 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001746
Mark Brownc5af3a22008-11-28 13:29:45 +00001747 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001748 return 0;
1749}
1750
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001751int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001752{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001753 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001754 int i;
Mark Brown51737472009-06-22 13:16:51 +01001755
1756 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001757 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001758
1759 /* Flush out pmdown_time work - we actually do want to run it
1760 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001761 for (i = 0; i < card->num_rtd; i++) {
1762 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001763 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001764 }
Mark Brown51737472009-06-22 13:16:51 +01001765
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001766 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001767
1768 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001769}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001770EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001771
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001772const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301773 .suspend = snd_soc_suspend,
1774 .resume = snd_soc_resume,
1775 .freeze = snd_soc_suspend,
1776 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001777 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301778 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001779};
Stephen Warrendeb26072011-04-05 19:35:30 -06001780EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001781
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001782/* ASoC platform driver */
1783static struct platform_driver soc_driver = {
1784 .driver = {
1785 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001786 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001787 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001788 },
1789 .probe = soc_probe,
1790 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001791};
1792
Mark Brown096e49d2009-07-05 15:12:22 +01001793/**
1794 * snd_soc_codec_volatile_register: Report if a register is volatile.
1795 *
1796 * @codec: CODEC to query.
1797 * @reg: Register to query.
1798 *
1799 * Boolean function indiciating if a CODEC register is volatile.
1800 */
Mark Brown181e0552011-01-24 14:05:25 +00001801int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1802 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001803{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001804 if (codec->volatile_register)
1805 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001806 else
1807 return 0;
1808}
1809EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1810
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001811/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001812 * snd_soc_codec_readable_register: Report if a register is readable.
1813 *
1814 * @codec: CODEC to query.
1815 * @reg: Register to query.
1816 *
1817 * Boolean function indicating if a CODEC register is readable.
1818 */
1819int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1820 unsigned int reg)
1821{
1822 if (codec->readable_register)
1823 return codec->readable_register(codec, reg);
1824 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001825 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001826}
1827EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1828
1829/**
1830 * snd_soc_codec_writable_register: Report if a register is writable.
1831 *
1832 * @codec: CODEC to query.
1833 * @reg: Register to query.
1834 *
1835 * Boolean function indicating if a CODEC register is writable.
1836 */
1837int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1838 unsigned int reg)
1839{
1840 if (codec->writable_register)
1841 return codec->writable_register(codec, reg);
1842 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001843 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001844}
1845EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1846
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001847int snd_soc_platform_read(struct snd_soc_platform *platform,
1848 unsigned int reg)
1849{
1850 unsigned int ret;
1851
1852 if (!platform->driver->read) {
1853 dev_err(platform->dev, "platform has no read back\n");
1854 return -1;
1855 }
1856
1857 ret = platform->driver->read(platform, reg);
1858 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001859 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001860
1861 return ret;
1862}
1863EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1864
1865int snd_soc_platform_write(struct snd_soc_platform *platform,
1866 unsigned int reg, unsigned int val)
1867{
1868 if (!platform->driver->write) {
1869 dev_err(platform->dev, "platform has no write back\n");
1870 return -1;
1871 }
1872
1873 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001874 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001875 return platform->driver->write(platform, reg, val);
1876}
1877EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1878
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001879/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001880 * snd_soc_new_ac97_codec - initailise AC97 device
1881 * @codec: audio codec
1882 * @ops: AC97 bus operations
1883 * @num: AC97 codec number
1884 *
1885 * Initialises AC97 codec resources for use by ad-hoc devices only.
1886 */
1887int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1888 struct snd_ac97_bus_ops *ops, int num)
1889{
1890 mutex_lock(&codec->mutex);
1891
1892 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1893 if (codec->ac97 == NULL) {
1894 mutex_unlock(&codec->mutex);
1895 return -ENOMEM;
1896 }
1897
1898 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1899 if (codec->ac97->bus == NULL) {
1900 kfree(codec->ac97);
1901 codec->ac97 = NULL;
1902 mutex_unlock(&codec->mutex);
1903 return -ENOMEM;
1904 }
1905
1906 codec->ac97->bus->ops = ops;
1907 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001908
1909 /*
1910 * Mark the AC97 device to be created by us. This way we ensure that the
1911 * device will be registered with the device subsystem later on.
1912 */
1913 codec->ac97_created = 1;
1914
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001915 mutex_unlock(&codec->mutex);
1916 return 0;
1917}
1918EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1919
1920/**
1921 * snd_soc_free_ac97_codec - free AC97 codec device
1922 * @codec: audio codec
1923 *
1924 * Frees AC97 codec device resources.
1925 */
1926void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1927{
1928 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001929#ifdef CONFIG_SND_SOC_AC97_BUS
1930 soc_unregister_ac97_dai_link(codec);
1931#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001932 kfree(codec->ac97->bus);
1933 kfree(codec->ac97);
1934 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001935 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001936 mutex_unlock(&codec->mutex);
1937}
1938EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1939
Mark Brownc3753702010-11-01 15:41:57 -04001940unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1941{
1942 unsigned int ret;
1943
Mark Brownc3acec22010-12-02 16:15:29 +00001944 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04001945 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04001946 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04001947
1948 return ret;
1949}
1950EXPORT_SYMBOL_GPL(snd_soc_read);
1951
1952unsigned int snd_soc_write(struct snd_soc_codec *codec,
1953 unsigned int reg, unsigned int val)
1954{
1955 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04001956 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00001957 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04001958}
1959EXPORT_SYMBOL_GPL(snd_soc_write);
1960
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00001961unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
1962 unsigned int reg, const void *data, size_t len)
1963{
1964 return codec->bulk_write_raw(codec, reg, data, len);
1965}
1966EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
1967
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001968/**
1969 * snd_soc_update_bits - update codec register bits
1970 * @codec: audio codec
1971 * @reg: codec register
1972 * @mask: register mask
1973 * @value: new value
1974 *
1975 * Writes new register value.
1976 *
Timur Tabi180c3292011-01-10 15:58:13 -06001977 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001978 */
1979int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001980 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001981{
Mark Brown8a713da2011-12-03 12:33:55 +00001982 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001983 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06001984 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001985
Mark Brown8a713da2011-12-03 12:33:55 +00001986 if (codec->using_regmap) {
1987 ret = regmap_update_bits_check(codec->control_data, reg,
1988 mask, value, &change);
1989 } else {
1990 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06001991 if (ret < 0)
1992 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00001993
1994 old = ret;
1995 new = (old & ~mask) | (value & mask);
1996 change = old != new;
1997 if (change)
1998 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06001999 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002000
Mark Brown8a713da2011-12-03 12:33:55 +00002001 if (ret < 0)
2002 return ret;
2003
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002004 return change;
2005}
2006EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2007
2008/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002009 * snd_soc_update_bits_locked - update codec register bits
2010 * @codec: audio codec
2011 * @reg: codec register
2012 * @mask: register mask
2013 * @value: new value
2014 *
2015 * Writes new register value, and takes the codec mutex.
2016 *
2017 * Returns 1 for change else 0.
2018 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002019int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2020 unsigned short reg, unsigned int mask,
2021 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002022{
2023 int change;
2024
2025 mutex_lock(&codec->mutex);
2026 change = snd_soc_update_bits(codec, reg, mask, value);
2027 mutex_unlock(&codec->mutex);
2028
2029 return change;
2030}
Mark Browndd1b3d52009-12-04 14:22:03 +00002031EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002032
2033/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002034 * snd_soc_test_bits - test register for change
2035 * @codec: audio codec
2036 * @reg: codec register
2037 * @mask: register mask
2038 * @value: new value
2039 *
2040 * Tests a register with a new value and checks if the new value is
2041 * different from the old value.
2042 *
2043 * Returns 1 for change else 0.
2044 */
2045int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002046 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002047{
2048 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002049 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002050
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002051 old = snd_soc_read(codec, reg);
2052 new = (old & ~mask) | value;
2053 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002054
2055 return change;
2056}
2057EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2058
2059/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002060 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2061 * @substream: the pcm substream
2062 * @hw: the hardware parameters
2063 *
2064 * Sets the substream runtime hardware parameters.
2065 */
2066int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2067 const struct snd_pcm_hardware *hw)
2068{
2069 struct snd_pcm_runtime *runtime = substream->runtime;
2070 runtime->hw.info = hw->info;
2071 runtime->hw.formats = hw->formats;
2072 runtime->hw.period_bytes_min = hw->period_bytes_min;
2073 runtime->hw.period_bytes_max = hw->period_bytes_max;
2074 runtime->hw.periods_min = hw->periods_min;
2075 runtime->hw.periods_max = hw->periods_max;
2076 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2077 runtime->hw.fifo_size = hw->fifo_size;
2078 return 0;
2079}
2080EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2081
2082/**
2083 * snd_soc_cnew - create new control
2084 * @_template: control template
2085 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002086 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002087 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002088 *
2089 * Create a new mixer control from a template control.
2090 *
2091 * Returns 0 for success, else error.
2092 */
2093struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002094 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002095 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002096{
2097 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002098 struct snd_kcontrol *kcontrol;
2099 char *name = NULL;
2100 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002101
2102 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002103 template.index = 0;
2104
Mark Brownefb7ac32011-03-08 17:23:24 +00002105 if (!long_name)
2106 long_name = template.name;
2107
2108 if (prefix) {
2109 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08002110 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00002111 if (!name)
2112 return NULL;
2113
2114 snprintf(name, name_len, "%s %s", prefix, long_name);
2115
2116 template.name = name;
2117 } else {
2118 template.name = long_name;
2119 }
2120
2121 kcontrol = snd_ctl_new1(&template, data);
2122
2123 kfree(name);
2124
2125 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002126}
2127EXPORT_SYMBOL_GPL(snd_soc_cnew);
2128
Liam Girdwood022658b2012-02-03 17:43:09 +00002129static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2130 const struct snd_kcontrol_new *controls, int num_controls,
2131 const char *prefix, void *data)
2132{
2133 int err, i;
2134
2135 for (i = 0; i < num_controls; i++) {
2136 const struct snd_kcontrol_new *control = &controls[i];
2137 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2138 control->name, prefix));
2139 if (err < 0) {
2140 dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2141 return err;
2142 }
2143 }
2144
2145 return 0;
2146}
2147
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002148/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002149 * snd_soc_add_codec_controls - add an array of controls to a codec.
2150 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002151 * duplicating this code.
2152 *
2153 * @codec: codec to add controls to
2154 * @controls: array of controls to add
2155 * @num_controls: number of elements in the array
2156 *
2157 * Return 0 for success, else error.
2158 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002159int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002160 const struct snd_kcontrol_new *controls, int num_controls)
2161{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002162 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002163
Liam Girdwood022658b2012-02-03 17:43:09 +00002164 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2165 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002166}
Liam Girdwood022658b2012-02-03 17:43:09 +00002167EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002168
2169/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002170 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002171 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002172 *
2173 * @platform: platform to add controls to
2174 * @controls: array of controls to add
2175 * @num_controls: number of elements in the array
2176 *
2177 * Return 0 for success, else error.
2178 */
2179int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2180 const struct snd_kcontrol_new *controls, int num_controls)
2181{
2182 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002183
Liam Girdwood022658b2012-02-03 17:43:09 +00002184 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2185 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002186}
2187EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2188
2189/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002190 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2191 * Convenience function to add a list of controls.
2192 *
2193 * @soc_card: SoC card to add controls to
2194 * @controls: array of controls to add
2195 * @num_controls: number of elements in the array
2196 *
2197 * Return 0 for success, else error.
2198 */
2199int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2200 const struct snd_kcontrol_new *controls, int num_controls)
2201{
2202 struct snd_card *card = soc_card->snd_card;
2203
2204 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2205 NULL, soc_card);
2206}
2207EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2208
2209/**
2210 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2211 * Convienience function to add a list of controls.
2212 *
2213 * @dai: DAI to add controls to
2214 * @controls: array of controls to add
2215 * @num_controls: number of elements in the array
2216 *
2217 * Return 0 for success, else error.
2218 */
2219int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2220 const struct snd_kcontrol_new *controls, int num_controls)
2221{
2222 struct snd_card *card = dai->card->snd_card;
2223
2224 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2225 NULL, dai);
2226}
2227EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2228
2229/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002230 * snd_soc_info_enum_double - enumerated double mixer info callback
2231 * @kcontrol: mixer control
2232 * @uinfo: control element information
2233 *
2234 * Callback to provide information about a double enumerated
2235 * mixer control.
2236 *
2237 * Returns 0 for success.
2238 */
2239int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2240 struct snd_ctl_elem_info *uinfo)
2241{
2242 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2243
2244 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2245 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002246 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002247
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002248 if (uinfo->value.enumerated.item > e->max - 1)
2249 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002250 strcpy(uinfo->value.enumerated.name,
2251 e->texts[uinfo->value.enumerated.item]);
2252 return 0;
2253}
2254EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2255
2256/**
2257 * snd_soc_get_enum_double - enumerated double mixer get callback
2258 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002259 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002260 *
2261 * Callback to get the value of a double enumerated mixer.
2262 *
2263 * Returns 0 for success.
2264 */
2265int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2266 struct snd_ctl_elem_value *ucontrol)
2267{
2268 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2269 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002270 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002271
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002272 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002273 ;
2274 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002275 ucontrol->value.enumerated.item[0]
2276 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002277 if (e->shift_l != e->shift_r)
2278 ucontrol->value.enumerated.item[1] =
2279 (val >> e->shift_r) & (bitmask - 1);
2280
2281 return 0;
2282}
2283EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2284
2285/**
2286 * snd_soc_put_enum_double - enumerated double mixer put callback
2287 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002288 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002289 *
2290 * Callback to set the value of a double enumerated mixer.
2291 *
2292 * Returns 0 for success.
2293 */
2294int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2295 struct snd_ctl_elem_value *ucontrol)
2296{
2297 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2298 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002299 unsigned int val;
2300 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002301
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002302 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002303 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002304 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002305 return -EINVAL;
2306 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2307 mask = (bitmask - 1) << e->shift_l;
2308 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002309 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002310 return -EINVAL;
2311 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2312 mask |= (bitmask - 1) << e->shift_r;
2313 }
2314
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002315 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002316}
2317EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2318
2319/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002320 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2321 * @kcontrol: mixer control
2322 * @ucontrol: control element information
2323 *
2324 * Callback to get the value of a double semi enumerated mixer.
2325 *
2326 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2327 * used for handling bitfield coded enumeration for example.
2328 *
2329 * Returns 0 for success.
2330 */
2331int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2332 struct snd_ctl_elem_value *ucontrol)
2333{
2334 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002335 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002336 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002337
2338 reg_val = snd_soc_read(codec, e->reg);
2339 val = (reg_val >> e->shift_l) & e->mask;
2340 for (mux = 0; mux < e->max; mux++) {
2341 if (val == e->values[mux])
2342 break;
2343 }
2344 ucontrol->value.enumerated.item[0] = mux;
2345 if (e->shift_l != e->shift_r) {
2346 val = (reg_val >> e->shift_r) & e->mask;
2347 for (mux = 0; mux < e->max; mux++) {
2348 if (val == e->values[mux])
2349 break;
2350 }
2351 ucontrol->value.enumerated.item[1] = mux;
2352 }
2353
2354 return 0;
2355}
2356EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2357
2358/**
2359 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2360 * @kcontrol: mixer control
2361 * @ucontrol: control element information
2362 *
2363 * Callback to set the value of a double semi enumerated mixer.
2364 *
2365 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2366 * used for handling bitfield coded enumeration for example.
2367 *
2368 * Returns 0 for success.
2369 */
2370int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2371 struct snd_ctl_elem_value *ucontrol)
2372{
2373 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002374 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002375 unsigned int val;
2376 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002377
2378 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2379 return -EINVAL;
2380 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2381 mask = e->mask << e->shift_l;
2382 if (e->shift_l != e->shift_r) {
2383 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2384 return -EINVAL;
2385 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2386 mask |= e->mask << e->shift_r;
2387 }
2388
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002389 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002390}
2391EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2392
2393/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002394 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2395 * @kcontrol: mixer control
2396 * @uinfo: control element information
2397 *
2398 * Callback to provide information about an external enumerated
2399 * single mixer.
2400 *
2401 * Returns 0 for success.
2402 */
2403int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2404 struct snd_ctl_elem_info *uinfo)
2405{
2406 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2407
2408 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2409 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002410 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002411
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002412 if (uinfo->value.enumerated.item > e->max - 1)
2413 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002414 strcpy(uinfo->value.enumerated.name,
2415 e->texts[uinfo->value.enumerated.item]);
2416 return 0;
2417}
2418EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2419
2420/**
2421 * snd_soc_info_volsw_ext - external single mixer info callback
2422 * @kcontrol: mixer control
2423 * @uinfo: control element information
2424 *
2425 * Callback to provide information about a single external mixer control.
2426 *
2427 * Returns 0 for success.
2428 */
2429int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2430 struct snd_ctl_elem_info *uinfo)
2431{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002432 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002433
Mark Brownfd5dfad2009-04-15 21:37:46 +01002434 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002435 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2436 else
2437 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2438
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002439 uinfo->count = 1;
2440 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002441 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002442 return 0;
2443}
2444EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2445
2446/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002447 * snd_soc_info_volsw - single mixer info callback
2448 * @kcontrol: mixer control
2449 * @uinfo: control element information
2450 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002451 * Callback to provide information about a single mixer control, or a double
2452 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002453 *
2454 * Returns 0 for success.
2455 */
2456int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2457 struct snd_ctl_elem_info *uinfo)
2458{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002459 struct soc_mixer_control *mc =
2460 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002461 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002462
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002463 if (!mc->platform_max)
2464 mc->platform_max = mc->max;
2465 platform_max = mc->platform_max;
2466
2467 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002468 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2469 else
2470 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2471
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002472 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002473 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002474 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002475 return 0;
2476}
2477EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2478
2479/**
2480 * snd_soc_get_volsw - single mixer get callback
2481 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002482 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002483 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002484 * Callback to get the value of a single mixer control, or a double mixer
2485 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002486 *
2487 * Returns 0 for success.
2488 */
2489int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2490 struct snd_ctl_elem_value *ucontrol)
2491{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002492 struct soc_mixer_control *mc =
2493 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002494 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002495 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002496 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002497 unsigned int shift = mc->shift;
2498 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002499 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002500 unsigned int mask = (1 << fls(max)) - 1;
2501 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002502
2503 ucontrol->value.integer.value[0] =
2504 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002505 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002506 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002507 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002508
2509 if (snd_soc_volsw_is_stereo(mc)) {
2510 if (reg == reg2)
2511 ucontrol->value.integer.value[1] =
2512 (snd_soc_read(codec, reg) >> rshift) & mask;
2513 else
2514 ucontrol->value.integer.value[1] =
2515 (snd_soc_read(codec, reg2) >> shift) & mask;
2516 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002517 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002518 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002519 }
2520
2521 return 0;
2522}
2523EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2524
2525/**
2526 * snd_soc_put_volsw - single mixer put callback
2527 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002528 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002529 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002530 * Callback to set the value of a single mixer control, or a double mixer
2531 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002532 *
2533 * Returns 0 for success.
2534 */
2535int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2536 struct snd_ctl_elem_value *ucontrol)
2537{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002538 struct soc_mixer_control *mc =
2539 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002540 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002541 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002542 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002543 unsigned int shift = mc->shift;
2544 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002545 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002546 unsigned int mask = (1 << fls(max)) - 1;
2547 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002548 int err;
2549 bool type_2r = 0;
2550 unsigned int val2 = 0;
2551 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002552
2553 val = (ucontrol->value.integer.value[0] & mask);
2554 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002555 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002556 val_mask = mask << shift;
2557 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002558 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002559 val2 = (ucontrol->value.integer.value[1] & mask);
2560 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002561 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002562 if (reg == reg2) {
2563 val_mask |= mask << rshift;
2564 val |= val2 << rshift;
2565 } else {
2566 val2 = val2 << shift;
2567 type_2r = 1;
2568 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002569 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002570 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002571 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002572 return err;
2573
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002574 if (type_2r)
2575 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2576
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002577 return err;
2578}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002579EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002580
Mark Browne13ac2e2008-05-28 17:58:05 +01002581/**
Brian Austin1d99f242012-03-30 10:43:55 -05002582 * snd_soc_get_volsw_sx - single mixer get callback
2583 * @kcontrol: mixer control
2584 * @ucontrol: control element information
2585 *
2586 * Callback to get the value of a single mixer control, or a double mixer
2587 * control that spans 2 registers.
2588 *
2589 * Returns 0 for success.
2590 */
2591int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2592 struct snd_ctl_elem_value *ucontrol)
2593{
2594 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2595 struct soc_mixer_control *mc =
2596 (struct soc_mixer_control *)kcontrol->private_value;
2597
2598 unsigned int reg = mc->reg;
2599 unsigned int reg2 = mc->rreg;
2600 unsigned int shift = mc->shift;
2601 unsigned int rshift = mc->rshift;
2602 int max = mc->max;
2603 int min = mc->min;
2604 int mask = (1 << (fls(min + max) - 1)) - 1;
2605
2606 ucontrol->value.integer.value[0] =
2607 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2608
2609 if (snd_soc_volsw_is_stereo(mc))
2610 ucontrol->value.integer.value[1] =
2611 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2612
2613 return 0;
2614}
2615EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2616
2617/**
2618 * snd_soc_put_volsw_sx - double mixer set callback
2619 * @kcontrol: mixer control
2620 * @uinfo: control element information
2621 *
2622 * Callback to set the value of a double mixer control that spans 2 registers.
2623 *
2624 * Returns 0 for success.
2625 */
2626int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2627 struct snd_ctl_elem_value *ucontrol)
2628{
2629 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2630 struct soc_mixer_control *mc =
2631 (struct soc_mixer_control *)kcontrol->private_value;
2632
2633 unsigned int reg = mc->reg;
2634 unsigned int reg2 = mc->rreg;
2635 unsigned int shift = mc->shift;
2636 unsigned int rshift = mc->rshift;
2637 int max = mc->max;
2638 int min = mc->min;
2639 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002640 int err = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002641 unsigned short val, val_mask, val2 = 0;
2642
2643 val_mask = mask << shift;
2644 val = (ucontrol->value.integer.value[0] + min) & mask;
2645 val = val << shift;
2646
2647 if (snd_soc_update_bits_locked(codec, reg, val_mask, val))
2648 return err;
2649
2650 if (snd_soc_volsw_is_stereo(mc)) {
2651 val_mask = mask << rshift;
2652 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2653 val2 = val2 << rshift;
2654
2655 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2656 return err;
2657 }
2658 return 0;
2659}
2660EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2661
2662/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002663 * snd_soc_info_volsw_s8 - signed mixer info callback
2664 * @kcontrol: mixer control
2665 * @uinfo: control element information
2666 *
2667 * Callback to provide information about a signed mixer control.
2668 *
2669 * Returns 0 for success.
2670 */
2671int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2672 struct snd_ctl_elem_info *uinfo)
2673{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002674 struct soc_mixer_control *mc =
2675 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002676 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002677 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002678
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002679 if (!mc->platform_max)
2680 mc->platform_max = mc->max;
2681 platform_max = mc->platform_max;
2682
Mark Browne13ac2e2008-05-28 17:58:05 +01002683 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2684 uinfo->count = 2;
2685 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002686 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002687 return 0;
2688}
2689EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2690
2691/**
2692 * snd_soc_get_volsw_s8 - signed mixer get callback
2693 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002694 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002695 *
2696 * Callback to get the value of a signed mixer control.
2697 *
2698 * Returns 0 for success.
2699 */
2700int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2701 struct snd_ctl_elem_value *ucontrol)
2702{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002703 struct soc_mixer_control *mc =
2704 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002705 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002706 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002707 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002708 int val = snd_soc_read(codec, reg);
2709
2710 ucontrol->value.integer.value[0] =
2711 ((signed char)(val & 0xff))-min;
2712 ucontrol->value.integer.value[1] =
2713 ((signed char)((val >> 8) & 0xff))-min;
2714 return 0;
2715}
2716EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2717
2718/**
2719 * snd_soc_put_volsw_sgn - signed mixer put callback
2720 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002721 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002722 *
2723 * Callback to set the value of a signed mixer control.
2724 *
2725 * Returns 0 for success.
2726 */
2727int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2728 struct snd_ctl_elem_value *ucontrol)
2729{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002730 struct soc_mixer_control *mc =
2731 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002732 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002733 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002734 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002735 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002736
2737 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2738 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2739
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002740 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002741}
2742EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2743
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002744/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002745 * snd_soc_limit_volume - Set new limit to an existing volume control.
2746 *
2747 * @codec: where to look for the control
2748 * @name: Name of the control
2749 * @max: new maximum limit
2750 *
2751 * Return 0 for success, else error.
2752 */
2753int snd_soc_limit_volume(struct snd_soc_codec *codec,
2754 const char *name, int max)
2755{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002756 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002757 struct snd_kcontrol *kctl;
2758 struct soc_mixer_control *mc;
2759 int found = 0;
2760 int ret = -EINVAL;
2761
2762 /* Sanity check for name and max */
2763 if (unlikely(!name || max <= 0))
2764 return -EINVAL;
2765
2766 list_for_each_entry(kctl, &card->controls, list) {
2767 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2768 found = 1;
2769 break;
2770 }
2771 }
2772 if (found) {
2773 mc = (struct soc_mixer_control *)kctl->private_value;
2774 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002775 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002776 ret = 0;
2777 }
2778 }
2779 return ret;
2780}
2781EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2782
Mark Brown71d08512011-10-10 18:31:26 +01002783int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2784 struct snd_ctl_elem_info *uinfo)
2785{
2786 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2787 struct soc_bytes *params = (void *)kcontrol->private_value;
2788
2789 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2790 uinfo->count = params->num_regs * codec->val_bytes;
2791
2792 return 0;
2793}
2794EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
2795
2796int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
2797 struct snd_ctl_elem_value *ucontrol)
2798{
2799 struct soc_bytes *params = (void *)kcontrol->private_value;
2800 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2801 int ret;
2802
2803 if (codec->using_regmap)
2804 ret = regmap_raw_read(codec->control_data, params->base,
2805 ucontrol->value.bytes.data,
2806 params->num_regs * codec->val_bytes);
2807 else
2808 ret = -EINVAL;
2809
Mark Brownf831b052012-02-17 16:20:33 -08002810 /* Hide any masked bytes to ensure consistent data reporting */
2811 if (ret == 0 && params->mask) {
2812 switch (codec->val_bytes) {
2813 case 1:
2814 ucontrol->value.bytes.data[0] &= ~params->mask;
2815 break;
2816 case 2:
2817 ((u16 *)(&ucontrol->value.bytes.data))[0]
2818 &= ~params->mask;
2819 break;
2820 case 4:
2821 ((u32 *)(&ucontrol->value.bytes.data))[0]
2822 &= ~params->mask;
2823 break;
2824 default:
2825 return -EINVAL;
2826 }
2827 }
2828
Mark Brown71d08512011-10-10 18:31:26 +01002829 return ret;
2830}
2831EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
2832
2833int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
2834 struct snd_ctl_elem_value *ucontrol)
2835{
2836 struct soc_bytes *params = (void *)kcontrol->private_value;
2837 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08002838 int ret, len;
2839 unsigned int val;
2840 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01002841
Mark Brownf831b052012-02-17 16:20:33 -08002842 if (!codec->using_regmap)
2843 return -EINVAL;
2844
2845 data = ucontrol->value.bytes.data;
2846 len = params->num_regs * codec->val_bytes;
2847
2848 /*
2849 * If we've got a mask then we need to preserve the register
2850 * bits. We shouldn't modify the incoming data so take a
2851 * copy.
2852 */
2853 if (params->mask) {
2854 ret = regmap_read(codec->control_data, params->base, &val);
2855 if (ret != 0)
2856 return ret;
2857
2858 val &= params->mask;
2859
2860 data = kmemdup(data, len, GFP_KERNEL);
2861 if (!data)
2862 return -ENOMEM;
2863
2864 switch (codec->val_bytes) {
2865 case 1:
2866 ((u8 *)data)[0] &= ~params->mask;
2867 ((u8 *)data)[0] |= val;
2868 break;
2869 case 2:
2870 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
2871 ((u16 *)data)[0] |= cpu_to_be16(val);
2872 break;
2873 case 4:
2874 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
2875 ((u32 *)data)[0] |= cpu_to_be32(val);
2876 break;
2877 default:
2878 return -EINVAL;
2879 }
2880 }
2881
2882 ret = regmap_raw_write(codec->control_data, params->base,
2883 data, len);
2884
2885 if (params->mask)
2886 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01002887
2888 return ret;
2889}
2890EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
2891
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002892/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002893 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2894 * @dai: DAI
2895 * @clk_id: DAI specific clock ID
2896 * @freq: new clock frequency in Hz
2897 * @dir: new clock direction - input/output.
2898 *
2899 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2900 */
2901int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2902 unsigned int freq, int dir)
2903{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002904 if (dai->driver && dai->driver->ops->set_sysclk)
2905 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002906 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002907 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00002908 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002909 else
2910 return -EINVAL;
2911}
2912EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2913
2914/**
Mark Brownec4ee522011-03-07 20:58:11 +00002915 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2916 * @codec: CODEC
2917 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01002918 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00002919 * @freq: new clock frequency in Hz
2920 * @dir: new clock direction - input/output.
2921 *
2922 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2923 */
2924int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01002925 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00002926{
2927 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002928 return codec->driver->set_sysclk(codec, clk_id, source,
2929 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002930 else
2931 return -EINVAL;
2932}
2933EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2934
2935/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002936 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2937 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002938 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002939 * @div: new clock divisor.
2940 *
2941 * Configures the clock dividers. This is used to derive the best DAI bit and
2942 * frame clocks from the system or master clock. It's best to set the DAI bit
2943 * and frame clocks as low as possible to save system power.
2944 */
2945int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2946 int div_id, int div)
2947{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002948 if (dai->driver && dai->driver->ops->set_clkdiv)
2949 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002950 else
2951 return -EINVAL;
2952}
2953EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2954
2955/**
2956 * snd_soc_dai_set_pll - configure DAI PLL.
2957 * @dai: DAI
2958 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002959 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002960 * @freq_in: PLL input clock frequency in Hz
2961 * @freq_out: requested PLL output clock frequency in Hz
2962 *
2963 * Configures and enables PLL to generate output clock based on input clock.
2964 */
Mark Brown85488032009-09-05 18:52:16 +01002965int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2966 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002967{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002968 if (dai->driver && dai->driver->ops->set_pll)
2969 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002970 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00002971 else if (dai->codec && dai->codec->driver->set_pll)
2972 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2973 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002974 else
2975 return -EINVAL;
2976}
2977EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2978
Mark Brownec4ee522011-03-07 20:58:11 +00002979/*
2980 * snd_soc_codec_set_pll - configure codec PLL.
2981 * @codec: CODEC
2982 * @pll_id: DAI specific PLL ID
2983 * @source: DAI specific source for the PLL
2984 * @freq_in: PLL input clock frequency in Hz
2985 * @freq_out: requested PLL output clock frequency in Hz
2986 *
2987 * Configures and enables PLL to generate output clock based on input clock.
2988 */
2989int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2990 unsigned int freq_in, unsigned int freq_out)
2991{
2992 if (codec->driver->set_pll)
2993 return codec->driver->set_pll(codec, pll_id, source,
2994 freq_in, freq_out);
2995 else
2996 return -EINVAL;
2997}
2998EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2999
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003000/**
3001 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3002 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003003 * @fmt: SND_SOC_DAIFMT_ format value.
3004 *
3005 * Configures the DAI hardware format and clocking.
3006 */
3007int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3008{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003009 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003010 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003011 if (dai->driver->ops->set_fmt == NULL)
3012 return -ENOTSUPP;
3013 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003014}
3015EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3016
3017/**
3018 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3019 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003020 * @tx_mask: bitmask representing active TX slots.
3021 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003022 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003023 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003024 *
3025 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3026 * specific.
3027 */
3028int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003029 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003030{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003031 if (dai->driver && dai->driver->ops->set_tdm_slot)
3032 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003033 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003034 else
3035 return -EINVAL;
3036}
3037EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3038
3039/**
Barry Song472df3c2009-09-12 01:16:29 +08003040 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3041 * @dai: DAI
3042 * @tx_num: how many TX channels
3043 * @tx_slot: pointer to an array which imply the TX slot number channel
3044 * 0~num-1 uses
3045 * @rx_num: how many RX channels
3046 * @rx_slot: pointer to an array which imply the RX slot number channel
3047 * 0~num-1 uses
3048 *
3049 * configure the relationship between channel number and TDM slot number.
3050 */
3051int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3052 unsigned int tx_num, unsigned int *tx_slot,
3053 unsigned int rx_num, unsigned int *rx_slot)
3054{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003055 if (dai->driver && dai->driver->ops->set_channel_map)
3056 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003057 rx_num, rx_slot);
3058 else
3059 return -EINVAL;
3060}
3061EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3062
3063/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003064 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3065 * @dai: DAI
3066 * @tristate: tristate enable
3067 *
3068 * Tristates the DAI so that others can use it.
3069 */
3070int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3071{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003072 if (dai->driver && dai->driver->ops->set_tristate)
3073 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003074 else
3075 return -EINVAL;
3076}
3077EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3078
3079/**
3080 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3081 * @dai: DAI
3082 * @mute: mute enable
3083 *
3084 * Mutes the DAI DAC.
3085 */
3086int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3087{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003088 if (dai->driver && dai->driver->ops->digital_mute)
3089 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003090 else
Mark Brown04570c62012-04-13 19:16:03 +01003091 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003092}
3093EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3094
Mark Brownc5af3a22008-11-28 13:29:45 +00003095/**
3096 * snd_soc_register_card - Register a card with the ASoC core
3097 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003098 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003099 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003100 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303101int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003102{
Mark Brownb19e6e72012-03-14 21:18:39 +00003103 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003104
Mark Brownc5af3a22008-11-28 13:29:45 +00003105 if (!card->name || !card->dev)
3106 return -EINVAL;
3107
Stephen Warren5a504962011-12-21 10:40:59 -07003108 for (i = 0; i < card->num_links; i++) {
3109 struct snd_soc_dai_link *link = &card->dai_link[i];
3110
3111 /*
3112 * Codec must be specified by 1 of name or OF node,
3113 * not both or neither.
3114 */
3115 if (!!link->codec_name == !!link->codec_of_node) {
3116 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003117 "Neither/both codec name/of_node are set for %s\n",
3118 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003119 return -EINVAL;
3120 }
3121
3122 /*
3123 * Platform may be specified by either name or OF node, but
3124 * can be left unspecified, and a dummy platform will be used.
3125 */
3126 if (link->platform_name && link->platform_of_node) {
3127 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003128 "Both platform name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003129 return -EINVAL;
3130 }
3131
3132 /*
3133 * CPU DAI must be specified by 1 of name or OF node,
3134 * not both or neither.
3135 */
3136 if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
3137 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003138 "Neither/both cpu_dai name/of_node are set for %s\n",
3139 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003140 return -EINVAL;
3141 }
3142 }
3143
Mark Browned77cc12011-05-03 18:25:34 +01003144 dev_set_drvdata(card->dev, card);
3145
Stephen Warren111c6412011-01-28 14:26:35 -07003146 snd_soc_initialize_card_lists(card);
3147
Vinod Koul150dd2f2011-01-13 22:48:32 +05303148 soc_init_card_debugfs(card);
3149
Mark Brown181a6892012-03-14 20:18:49 +00003150 card->rtd = devm_kzalloc(card->dev,
3151 sizeof(struct snd_soc_pcm_runtime) *
3152 (card->num_links + card->num_aux_devs),
3153 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003154 if (card->rtd == NULL)
3155 return -ENOMEM;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003156 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003157
3158 for (i = 0; i < card->num_links; i++)
3159 card->rtd[i].dai_link = &card->dai_link[i];
3160
Mark Brownc5af3a22008-11-28 13:29:45 +00003161 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003162 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003163 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003164 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003165 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003166
Mark Brownb19e6e72012-03-14 21:18:39 +00003167 ret = snd_soc_instantiate_card(card);
3168 if (ret != 0)
3169 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003170
Mark Brownb19e6e72012-03-14 21:18:39 +00003171 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003172}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303173EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003174
3175/**
3176 * snd_soc_unregister_card - Unregister a card with the ASoC core
3177 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003178 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003179 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003180 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303181int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003182{
Vinod Koulb0e26482011-01-13 22:48:02 +05303183 if (card->instantiated)
3184 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003185 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3186
3187 return 0;
3188}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303189EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003190
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003191/*
3192 * Simplify DAI link configuration by removing ".-1" from device names
3193 * and sanitizing names.
3194 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003195static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003196{
3197 char *found, name[NAME_SIZE];
3198 int id1, id2;
3199
3200 if (dev_name(dev) == NULL)
3201 return NULL;
3202
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003203 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003204
3205 /* are we a "%s.%d" name (platform and SPI components) */
3206 found = strstr(name, dev->driver->name);
3207 if (found) {
3208 /* get ID */
3209 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3210
3211 /* discard ID from name if ID == -1 */
3212 if (*id == -1)
3213 found[strlen(dev->driver->name)] = '\0';
3214 }
3215
3216 } else {
3217 /* I2C component devices are named "bus-addr" */
3218 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3219 char tmp[NAME_SIZE];
3220
3221 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003222 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003223
3224 /* sanitize component name for DAI link creation */
3225 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003226 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003227 } else
3228 *id = 0;
3229 }
3230
3231 return kstrdup(name, GFP_KERNEL);
3232}
3233
3234/*
3235 * Simplify DAI link naming for single devices with multiple DAIs by removing
3236 * any ".-1" and using the DAI name (instead of device name).
3237 */
3238static inline char *fmt_multiple_name(struct device *dev,
3239 struct snd_soc_dai_driver *dai_drv)
3240{
3241 if (dai_drv->name == NULL) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02003242 pr_err("asoc: error - multiple DAI %s registered with no name\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003243 dev_name(dev));
3244 return NULL;
3245 }
3246
3247 return kstrdup(dai_drv->name, GFP_KERNEL);
3248}
3249
Mark Brown91151712008-11-30 23:31:24 +00003250/**
3251 * snd_soc_register_dai - Register a DAI with the ASoC core
3252 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003253 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003254 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003255int snd_soc_register_dai(struct device *dev,
3256 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003257{
Mark Brown054880f2012-04-09 17:29:19 +01003258 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003259 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003260
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003261 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003262
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003263 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3264 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003265 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003266
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003267 /* create DAI component name */
3268 dai->name = fmt_single_name(dev, &dai->id);
3269 if (dai->name == NULL) {
3270 kfree(dai);
3271 return -ENOMEM;
3272 }
3273
3274 dai->dev = dev;
3275 dai->driver = dai_drv;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003276 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003277 if (!dai->driver->ops)
3278 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003279
3280 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003281
3282 list_for_each_entry(codec, &codec_list, list) {
3283 if (codec->dev == dev) {
3284 dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3285 dai->name, codec->name);
3286 dai->codec = codec;
3287 break;
3288 }
3289 }
3290
Mark Brown91151712008-11-30 23:31:24 +00003291 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003292
Mark Brown91151712008-11-30 23:31:24 +00003293 mutex_unlock(&client_mutex);
3294
3295 pr_debug("Registered DAI '%s'\n", dai->name);
3296
3297 return 0;
3298}
3299EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3300
3301/**
3302 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3303 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003304 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003305 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003306void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003307{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003308 struct snd_soc_dai *dai;
3309
3310 list_for_each_entry(dai, &dai_list, list) {
3311 if (dev == dai->dev)
3312 goto found;
3313 }
3314 return;
3315
3316found:
Mark Brown91151712008-11-30 23:31:24 +00003317 mutex_lock(&client_mutex);
3318 list_del(&dai->list);
3319 mutex_unlock(&client_mutex);
3320
3321 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003322 kfree(dai->name);
3323 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003324}
3325EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3326
3327/**
3328 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3329 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003330 * @dai: Array of DAIs to register
3331 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003332 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003333int snd_soc_register_dais(struct device *dev,
3334 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003335{
Mark Brown054880f2012-04-09 17:29:19 +01003336 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003337 struct snd_soc_dai *dai;
3338 int i, ret = 0;
3339
Liam Girdwood720ffa42010-08-18 00:30:30 +01003340 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003341
3342 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003343
3344 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003345 if (dai == NULL) {
3346 ret = -ENOMEM;
3347 goto err;
3348 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003349
3350 /* create DAI component name */
3351 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3352 if (dai->name == NULL) {
3353 kfree(dai);
3354 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003355 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003356 }
3357
3358 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003359 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003360 if (dai->driver->id)
3361 dai->id = dai->driver->id;
3362 else
3363 dai->id = i;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003364 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003365 if (!dai->driver->ops)
3366 dai->driver->ops = &null_dai_ops;
3367
3368 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003369
3370 list_for_each_entry(codec, &codec_list, list) {
3371 if (codec->dev == dev) {
3372 dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3373 dai->name, codec->name);
3374 dai->codec = codec;
3375 break;
3376 }
3377 }
3378
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003379 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003380
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003381 mutex_unlock(&client_mutex);
3382
3383 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003384 }
3385
3386 return 0;
3387
3388err:
3389 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003390 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003391
3392 return ret;
3393}
3394EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3395
3396/**
3397 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3398 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003399 * @dai: Array of DAIs to unregister
3400 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003401 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003402void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003403{
3404 int i;
3405
3406 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003407 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003408}
3409EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3410
Mark Brown12a48a8c2008-12-03 19:40:30 +00003411/**
3412 * snd_soc_register_platform - Register a platform with the ASoC core
3413 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003414 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003415 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003416int snd_soc_register_platform(struct device *dev,
3417 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003418{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003419 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003420
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003421 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3422
3423 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3424 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003425 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003426
3427 /* create platform component name */
3428 platform->name = fmt_single_name(dev, &platform->id);
3429 if (platform->name == NULL) {
3430 kfree(platform);
3431 return -ENOMEM;
3432 }
3433
3434 platform->dev = dev;
3435 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003436 platform->dapm.dev = dev;
3437 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003438 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00003439 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003440
3441 mutex_lock(&client_mutex);
3442 list_add(&platform->list, &platform_list);
3443 mutex_unlock(&client_mutex);
3444
3445 pr_debug("Registered platform '%s'\n", platform->name);
3446
3447 return 0;
3448}
3449EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3450
3451/**
3452 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3453 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003454 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003455 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003456void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003457{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003458 struct snd_soc_platform *platform;
3459
3460 list_for_each_entry(platform, &platform_list, list) {
3461 if (dev == platform->dev)
3462 goto found;
3463 }
3464 return;
3465
3466found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003467 mutex_lock(&client_mutex);
3468 list_del(&platform->list);
3469 mutex_unlock(&client_mutex);
3470
3471 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003472 kfree(platform->name);
3473 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003474}
3475EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3476
Mark Brown151ab222009-05-09 16:22:58 +01003477static u64 codec_format_map[] = {
3478 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3479 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3480 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3481 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3482 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3483 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3484 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3485 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3486 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3487 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3488 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3489 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3490 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3491 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3492 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3493 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3494};
3495
3496/* Fix up the DAI formats for endianness: codecs don't actually see
3497 * the endianness of the data but we're using the CPU format
3498 * definitions which do need to include endianness so we ensure that
3499 * codec DAIs always have both big and little endian variants set.
3500 */
3501static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3502{
3503 int i;
3504
3505 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3506 if (stream->formats & codec_format_map[i])
3507 stream->formats |= codec_format_map[i];
3508}
3509
Mark Brown0d0cf002008-12-10 14:32:45 +00003510/**
3511 * snd_soc_register_codec - Register a codec with the ASoC core
3512 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003513 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003514 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003515int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003516 const struct snd_soc_codec_driver *codec_drv,
3517 struct snd_soc_dai_driver *dai_drv,
3518 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003519{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003520 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003521 struct snd_soc_codec *codec;
3522 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003523
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003524 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003525
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003526 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3527 if (codec == NULL)
3528 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003529
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003530 /* create CODEC component name */
3531 codec->name = fmt_single_name(dev, &codec->id);
3532 if (codec->name == NULL) {
3533 kfree(codec);
3534 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003535 }
3536
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003537 if (codec_drv->compress_type)
3538 codec->compress_type = codec_drv->compress_type;
3539 else
3540 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3541
Mark Brownc3acec22010-12-02 16:15:29 +00003542 codec->write = codec_drv->write;
3543 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003544 codec->volatile_register = codec_drv->volatile_register;
3545 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003546 codec->writable_register = codec_drv->writable_register;
Mark Brown5124e692012-02-08 13:20:50 +00003547 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003548 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3549 codec->dapm.dev = dev;
3550 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003551 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003552 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003553 codec->dev = dev;
3554 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003555 codec->num_dai = num_dai;
3556 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003557
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003558 /* allocate CODEC register cache */
3559 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003560 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003561 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003562 /* it is necessary to make a copy of the default register cache
3563 * because in the case of using a compression type that requires
3564 * the default register cache to be marked as __devinitconst the
3565 * kernel might have freed the array by the time we initialize
3566 * the cache.
3567 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003568 if (codec_drv->reg_cache_default) {
3569 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3570 reg_size, GFP_KERNEL);
3571 if (!codec->reg_def_copy) {
3572 ret = -ENOMEM;
3573 goto fail;
3574 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003575 }
3576 }
3577
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003578 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3579 if (!codec->volatile_register)
3580 codec->volatile_register = snd_soc_default_volatile_register;
3581 if (!codec->readable_register)
3582 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003583 if (!codec->writable_register)
3584 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003585 }
3586
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003587 for (i = 0; i < num_dai; i++) {
3588 fixup_codec_formats(&dai_drv[i].playback);
3589 fixup_codec_formats(&dai_drv[i].capture);
3590 }
3591
Mark Brown054880f2012-04-09 17:29:19 +01003592 mutex_lock(&client_mutex);
3593 list_add(&codec->list, &codec_list);
3594 mutex_unlock(&client_mutex);
3595
Mark Brown26b01cc2010-08-18 20:20:55 +01003596 /* register any DAIs */
3597 if (num_dai) {
3598 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3599 if (ret < 0)
Mark Brown054880f2012-04-09 17:29:19 +01003600 dev_err(codec->dev, "Failed to regster DAIs: %d\n",
3601 ret);
Mark Brown26b01cc2010-08-18 20:20:55 +01003602 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003603
Mark Brown0d0cf002008-12-10 14:32:45 +00003604 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003605 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003606
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003607fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003608 kfree(codec->reg_def_copy);
3609 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003610 kfree(codec->name);
3611 kfree(codec);
3612 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003613}
3614EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3615
3616/**
3617 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3618 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003619 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003620 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003621void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003622{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003623 struct snd_soc_codec *codec;
3624 int i;
3625
3626 list_for_each_entry(codec, &codec_list, list) {
3627 if (dev == codec->dev)
3628 goto found;
3629 }
3630 return;
3631
3632found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003633 if (codec->num_dai)
3634 for (i = 0; i < codec->num_dai; i++)
3635 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003636
Mark Brown0d0cf002008-12-10 14:32:45 +00003637 mutex_lock(&client_mutex);
3638 list_del(&codec->list);
3639 mutex_unlock(&client_mutex);
3640
3641 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003642
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003643 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003644 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003645 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003646 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003647}
3648EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3649
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003650/* Retrieve a card's name from device tree */
3651int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3652 const char *propname)
3653{
3654 struct device_node *np = card->dev->of_node;
3655 int ret;
3656
3657 ret = of_property_read_string_index(np, propname, 0, &card->name);
3658 /*
3659 * EINVAL means the property does not exist. This is fine providing
3660 * card->name was previously set, which is checked later in
3661 * snd_soc_register_card.
3662 */
3663 if (ret < 0 && ret != -EINVAL) {
3664 dev_err(card->dev,
3665 "Property '%s' could not be read: %d\n",
3666 propname, ret);
3667 return ret;
3668 }
3669
3670 return 0;
3671}
3672EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3673
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003674int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3675 const char *propname)
3676{
3677 struct device_node *np = card->dev->of_node;
3678 int num_routes;
3679 struct snd_soc_dapm_route *routes;
3680 int i, ret;
3681
3682 num_routes = of_property_count_strings(np, propname);
3683 if (num_routes & 1) {
3684 dev_err(card->dev,
3685 "Property '%s's length is not even\n",
3686 propname);
3687 return -EINVAL;
3688 }
3689 num_routes /= 2;
3690 if (!num_routes) {
3691 dev_err(card->dev,
3692 "Property '%s's length is zero\n",
3693 propname);
3694 return -EINVAL;
3695 }
3696
3697 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3698 GFP_KERNEL);
3699 if (!routes) {
3700 dev_err(card->dev,
3701 "Could not allocate DAPM route table\n");
3702 return -EINVAL;
3703 }
3704
3705 for (i = 0; i < num_routes; i++) {
3706 ret = of_property_read_string_index(np, propname,
3707 2 * i, &routes[i].sink);
3708 if (ret) {
3709 dev_err(card->dev,
3710 "Property '%s' index %d could not be read: %d\n",
3711 propname, 2 * i, ret);
3712 return -EINVAL;
3713 }
3714 ret = of_property_read_string_index(np, propname,
3715 (2 * i) + 1, &routes[i].source);
3716 if (ret) {
3717 dev_err(card->dev,
3718 "Property '%s' index %d could not be read: %d\n",
3719 propname, (2 * i) + 1, ret);
3720 return -EINVAL;
3721 }
3722 }
3723
3724 card->num_dapm_routes = num_routes;
3725 card->dapm_routes = routes;
3726
3727 return 0;
3728}
3729EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3730
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003731static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003732{
Mark Brown384c89e2008-12-03 17:34:03 +00003733#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003734 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3735 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02003736 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00003737 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00003738 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003739
Mark Brown8a9dab12011-01-10 22:25:21 +00003740 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01003741 &codec_list_fops))
3742 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3743
Mark Brown8a9dab12011-01-10 22:25:21 +00003744 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01003745 &dai_list_fops))
3746 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003747
Mark Brown8a9dab12011-01-10 22:25:21 +00003748 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01003749 &platform_list_fops))
3750 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003751#endif
3752
Mark Brownfb257892011-04-28 10:57:54 +01003753 snd_soc_util_init();
3754
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003755 return platform_driver_register(&soc_driver);
3756}
Mark Brown4abe8e12010-10-12 17:41:03 +01003757module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003758
Mark Brown7d8c16a2008-11-30 22:11:24 +00003759static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003760{
Mark Brownfb257892011-04-28 10:57:54 +01003761 snd_soc_util_exit();
3762
Mark Brown384c89e2008-12-03 17:34:03 +00003763#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003764 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00003765#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003766 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003767}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003768module_exit(snd_soc_exit);
3769
3770/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003771MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003772MODULE_DESCRIPTION("ALSA SoC Core");
3773MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003774MODULE_ALIAS("platform:soc-audio");