blob: c7a00fd8cc6677996d89121467bb63db435f0296 [file] [log] [blame]
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwood0664d882006-12-18 14:39:02 +01005 * Copyright 2005 Openedhand Ltd.
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00006 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
Liam Girdwood0664d882006-12-18 14:39:02 +01008 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01009 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood0664d882006-12-18 14:39:02 +010010 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020012 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +020018 * TODO:
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
23 */
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/init.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
30#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070031#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020032#include <linux/platform_device.h>
Mark Brownf0e8ed82011-09-20 11:41:54 +010033#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070035#include <linux/of.h>
Marek Vasut474828a2009-07-22 13:01:03 +020036#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020037#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000038#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020039#include <sound/pcm.h>
40#include <sound/pcm_params.h>
41#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010042#include <sound/soc-dpcm.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020043#include <sound/initval.h>
44
Mark Browna8b1d342010-11-03 18:05:58 -040045#define CREATE_TRACE_POINTS
46#include <trace/events/asoc.h>
47
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000048#define NAME_SIZE 32
49
Frank Mandarinodb2a4162006-10-06 18:31:09 +020050static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
51
Mark Brown384c89e2008-12-03 17:34:03 +000052#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000053struct dentry *snd_soc_debugfs_root;
54EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000055#endif
56
Mark Brownc5af3a22008-11-28 13:29:45 +000057static DEFINE_MUTEX(client_mutex);
Mark Brown91151712008-11-30 23:31:24 +000058static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000059static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000060static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000061
Frank Mandarinodb2a4162006-10-06 18:31:09 +020062/*
63 * This is a timeout to do a DAPM powerdown after a stream is closed().
64 * It can be used to eliminate pops between different playback streams, e.g.
65 * between two audio tracks.
66 */
67static int pmdown_time = 5000;
68module_param(pmdown_time, int, 0);
69MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
70
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000071/* returns the minimum number of bytes needed to represent
72 * a particular given value */
73static int min_bytes_needed(unsigned long val)
74{
75 int c = 0;
76 int i;
77
78 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
79 if (val & (1UL << i))
80 break;
81 c = (sizeof val * 8) - c;
82 if (!c || (c % 8))
83 c = (c + 8) / 8;
84 else
85 c /= 8;
86 return c;
87}
88
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000089/* fill buf which is 'len' bytes with a formatted
90 * string of the form 'reg: value\n' */
91static int format_register_str(struct snd_soc_codec *codec,
92 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +000093{
Stephen Warren00b317a2011-04-01 14:50:44 -060094 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
95 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000096 int ret;
97 char tmpbuf[len + 1];
98 char regbuf[regsize + 1];
99
100 /* since tmpbuf is allocated on the stack, warn the callers if they
101 * try to abuse this function */
102 WARN_ON(len > 63);
103
104 /* +2 for ': ' and + 1 for '\n' */
105 if (wordsize + regsize + 2 + 1 != len)
106 return -EINVAL;
107
Mark Brown25032c12011-08-01 13:52:48 +0900108 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000109 if (ret < 0) {
110 memset(regbuf, 'X', regsize);
111 regbuf[regsize] = '\0';
112 } else {
113 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
114 }
115
116 /* prepare the buffer */
117 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
118 /* copy it back to the caller without the '\0' */
119 memcpy(buf, tmpbuf, len);
120
121 return 0;
122}
123
124/* codec register dump */
125static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
126 size_t count, loff_t pos)
127{
128 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000129 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000130 int len;
131 size_t total = 0;
132 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000133
Stephen Warren00b317a2011-04-01 14:50:44 -0600134 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
135 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000136
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000137 len = wordsize + regsize + 2 + 1;
138
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000139 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000140 return 0;
141
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000142 if (codec->driver->reg_cache_step)
143 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000144
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000145 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausenb92d1502011-08-27 18:24:14 +0200146 if (!snd_soc_codec_readable_register(codec, i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000147 continue;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000148 if (codec->driver->display_register) {
149 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000150 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100151 } else {
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000152 /* only support larger than PAGE_SIZE bytes debugfs
153 * entries for the default case */
154 if (p >= pos) {
155 if (total + len >= count - 1)
156 break;
157 format_register_str(codec, i, buf + total, len);
158 total += len;
159 }
160 p += len;
Mark Brown5164d742010-07-14 16:14:33 +0100161 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000162 }
163
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000164 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000165
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000166 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000167}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000168
Mark Brown2624d5f2009-11-03 21:56:13 +0000169static ssize_t codec_reg_show(struct device *dev,
170 struct device_attribute *attr, char *buf)
171{
Mark Brown36ae1a92012-01-06 17:12:45 -0800172 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000173
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000174 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000175}
176
177static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
178
Mark Browndbe21402010-02-12 11:37:24 +0000179static ssize_t pmdown_time_show(struct device *dev,
180 struct device_attribute *attr, char *buf)
181{
Mark Brown36ae1a92012-01-06 17:12:45 -0800182 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000183
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000184 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000185}
186
187static ssize_t pmdown_time_set(struct device *dev,
188 struct device_attribute *attr,
189 const char *buf, size_t count)
190{
Mark Brown36ae1a92012-01-06 17:12:45 -0800191 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700192 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000193
Mark Brownc593b522010-10-27 20:11:17 -0700194 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
195 if (ret)
196 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000197
198 return count;
199}
200
201static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
202
Mark Brown2624d5f2009-11-03 21:56:13 +0000203#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000204static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000205 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000206{
207 ssize_t ret;
208 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000209 char *buf;
210
211 if (*ppos < 0 || !count)
212 return -EINVAL;
213
214 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000215 if (!buf)
216 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000217
218 ret = soc_codec_reg_show(codec, buf, count, *ppos);
219 if (ret >= 0) {
220 if (copy_to_user(user_buf, buf, ret)) {
221 kfree(buf);
222 return -EFAULT;
223 }
224 *ppos += ret;
225 }
226
Mark Brown2624d5f2009-11-03 21:56:13 +0000227 kfree(buf);
228 return ret;
229}
230
231static ssize_t codec_reg_write_file(struct file *file,
232 const char __user *user_buf, size_t count, loff_t *ppos)
233{
234 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700235 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000236 char *start = buf;
237 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000238 struct snd_soc_codec *codec = file->private_data;
239
240 buf_size = min(count, (sizeof(buf)-1));
241 if (copy_from_user(buf, user_buf, buf_size))
242 return -EFAULT;
243 buf[buf_size] = 0;
244
Mark Brown2624d5f2009-11-03 21:56:13 +0000245 while (*start == ' ')
246 start++;
247 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000248 while (*start == ' ')
249 start++;
250 if (strict_strtoul(start, 16, &value))
251 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000252
253 /* Userspace has been fiddling around behind the kernel's back */
254 add_taint(TAINT_USER);
255
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000256 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000257 return buf_size;
258}
259
260static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700261 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000262 .read = codec_reg_read_file,
263 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200264 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000265};
266
267static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
268{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200269 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
270
271 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
272 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000273 if (!codec->debugfs_codec_root) {
Liam Girdwood64e60f92012-02-15 15:15:37 +0000274 dev_warn(codec->dev, "Failed to create codec debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000275 return;
276 }
277
Mark Brownaaee8ef2011-01-26 20:53:50 +0000278 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
279 &codec->cache_sync);
280 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
281 &codec->cache_only);
282
Mark Brown2624d5f2009-11-03 21:56:13 +0000283 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
284 codec->debugfs_codec_root,
285 codec, &codec_reg_fops);
286 if (!codec->debugfs_reg)
Liam Girdwood64e60f92012-02-15 15:15:37 +0000287 dev_warn(codec->dev, "Failed to create codec register debugfs file\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000288
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200289 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000290}
291
292static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
293{
294 debugfs_remove_recursive(codec->debugfs_codec_root);
295}
296
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000297static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
298{
299 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
300
301 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
302 debugfs_card_root);
303 if (!platform->debugfs_platform_root) {
304 dev_warn(platform->dev,
305 "Failed to create platform debugfs directory\n");
306 return;
307 }
308
309 snd_soc_dapm_debugfs_init(&platform->dapm,
310 platform->debugfs_platform_root);
311}
312
313static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
314{
315 debugfs_remove_recursive(platform->debugfs_platform_root);
316}
317
Mark Brownc3c5a192010-09-15 18:15:14 +0100318static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
319 size_t count, loff_t *ppos)
320{
321 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100322 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100323 struct snd_soc_codec *codec;
324
325 if (!buf)
326 return -ENOMEM;
327
Mark Brown2b194f9d2010-10-13 10:52:16 +0100328 list_for_each_entry(codec, &codec_list, list) {
329 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
330 codec->name);
331 if (len >= 0)
332 ret += len;
333 if (ret > PAGE_SIZE) {
334 ret = PAGE_SIZE;
335 break;
336 }
337 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100338
339 if (ret >= 0)
340 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
341
342 kfree(buf);
343
344 return ret;
345}
346
347static const struct file_operations codec_list_fops = {
348 .read = codec_list_read_file,
349 .llseek = default_llseek,/* read accesses f_pos */
350};
351
Mark Brownf3208782010-09-15 18:19:07 +0100352static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
353 size_t count, loff_t *ppos)
354{
355 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100356 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100357 struct snd_soc_dai *dai;
358
359 if (!buf)
360 return -ENOMEM;
361
Mark Brown2b194f9d2010-10-13 10:52:16 +0100362 list_for_each_entry(dai, &dai_list, list) {
363 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
364 if (len >= 0)
365 ret += len;
366 if (ret > PAGE_SIZE) {
367 ret = PAGE_SIZE;
368 break;
369 }
370 }
Mark Brownf3208782010-09-15 18:19:07 +0100371
Mark Brown2b194f9d2010-10-13 10:52:16 +0100372 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100373
374 kfree(buf);
375
376 return ret;
377}
378
379static const struct file_operations dai_list_fops = {
380 .read = dai_list_read_file,
381 .llseek = default_llseek,/* read accesses f_pos */
382};
383
Mark Brown19c7ac22010-09-15 18:22:40 +0100384static ssize_t platform_list_read_file(struct file *file,
385 char __user *user_buf,
386 size_t count, loff_t *ppos)
387{
388 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100389 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100390 struct snd_soc_platform *platform;
391
392 if (!buf)
393 return -ENOMEM;
394
Mark Brown2b194f9d2010-10-13 10:52:16 +0100395 list_for_each_entry(platform, &platform_list, list) {
396 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
397 platform->name);
398 if (len >= 0)
399 ret += len;
400 if (ret > PAGE_SIZE) {
401 ret = PAGE_SIZE;
402 break;
403 }
404 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100405
Mark Brown2b194f9d2010-10-13 10:52:16 +0100406 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100407
408 kfree(buf);
409
410 return ret;
411}
412
413static const struct file_operations platform_list_fops = {
414 .read = platform_list_read_file,
415 .llseek = default_llseek,/* read accesses f_pos */
416};
417
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200418static void soc_init_card_debugfs(struct snd_soc_card *card)
419{
420 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000421 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200422 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200423 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100424 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200425 return;
426 }
427
428 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
429 card->debugfs_card_root,
430 &card->pop_time);
431 if (!card->debugfs_pop_time)
432 dev_warn(card->dev,
433 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200434}
435
436static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
437{
438 debugfs_remove_recursive(card->debugfs_card_root);
439}
440
Mark Brown2624d5f2009-11-03 21:56:13 +0000441#else
442
443static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
444{
445}
446
447static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
448{
449}
Axel Linb95fccb2010-11-09 17:06:44 +0800450
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000451static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
452{
453}
454
455static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
456{
457}
458
Axel Linb95fccb2010-11-09 17:06:44 +0800459static inline void soc_init_card_debugfs(struct snd_soc_card *card)
460{
461}
462
463static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
464{
465}
Mark Brown2624d5f2009-11-03 21:56:13 +0000466#endif
467
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100468struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
469 const char *dai_link, int stream)
470{
471 int i;
472
473 for (i = 0; i < card->num_links; i++) {
474 if (card->rtd[i].dai_link->no_pcm &&
475 !strcmp(card->rtd[i].dai_link->name, dai_link))
476 return card->rtd[i].pcm->streams[stream].substream;
477 }
478 dev_dbg(card->dev, "failed to find dai link %s\n", dai_link);
479 return NULL;
480}
481EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
482
483struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
484 const char *dai_link)
485{
486 int i;
487
488 for (i = 0; i < card->num_links; i++) {
489 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
490 return &card->rtd[i];
491 }
492 dev_dbg(card->dev, "failed to find rtd %s\n", dai_link);
493 return NULL;
494}
495EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
496
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200497#ifdef CONFIG_SND_SOC_AC97_BUS
498/* unregister ac97 codec */
499static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
500{
501 if (codec->ac97->dev.bus)
502 device_unregister(&codec->ac97->dev);
503 return 0;
504}
505
506/* stop no dev release warning */
507static void soc_ac97_device_release(struct device *dev){}
508
509/* register ac97 codec to bus */
510static int soc_ac97_dev_register(struct snd_soc_codec *codec)
511{
512 int err;
513
514 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100515 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200516 codec->ac97->dev.release = soc_ac97_device_release;
517
Kay Sieversbb072bf2008-11-02 03:50:35 +0100518 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000519 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200520 err = device_register(&codec->ac97->dev);
521 if (err < 0) {
522 snd_printk(KERN_ERR "Can't register ac97 bus\n");
523 codec->ac97->dev.bus = NULL;
524 return err;
525 }
526 return 0;
527}
528#endif
529
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000530#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200531/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000532int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200533{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000534 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200535 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200536 int i;
537
Daniel Macke3509ff2009-06-03 17:44:49 +0200538 /* If the initialization of this soc device failed, there is no codec
539 * associated with it. Just bail out in this case.
540 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000541 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200542 return 0;
543
Andy Green6ed25972008-06-13 16:24:05 +0100544 /* Due to the resume being scheduled into a workqueue we could
545 * suspend before that's finished - wait for it to complete.
546 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000547 snd_power_lock(card->snd_card);
548 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
549 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100550
551 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100553
Mark Browna00f90f2010-12-02 16:24:24 +0000554 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000555 for (i = 0; i < card->num_rtd; i++) {
556 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
557 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100558
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000559 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100560 continue;
561
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 if (drv->ops->digital_mute && dai->playback_active)
563 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200564 }
565
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100566 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000567 for (i = 0; i < card->num_rtd; i++) {
568 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100569 continue;
570
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000571 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100572 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100573
Mark Brown87506542008-11-18 20:50:34 +0000574 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000575 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200576
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000577 for (i = 0; i < card->num_rtd; i++) {
578 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
579 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100580
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000581 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100582 continue;
583
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000584 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
585 cpu_dai->driver->suspend(cpu_dai);
586 if (platform->driver->suspend && !platform->suspended) {
587 platform->driver->suspend(cpu_dai);
588 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100589 }
590 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200591
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000592 /* close any waiting streams and save state */
593 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +0100594 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200595 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000596 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100597
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599
600 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100601 continue;
602
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800603 snd_soc_dapm_stream_event(&card->rtd[i],
604 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800605 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000606
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800607 snd_soc_dapm_stream_event(&card->rtd[i],
608 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800609 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000610 }
611
612 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200613 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000614 /* If there are paths active then the CODEC will be held with
615 * bias _ON and should not be suspended. */
616 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200617 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000618 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000619 /*
620 * If the CODEC is capable of idle
621 * bias off then being in STANDBY
622 * means it's doing something,
623 * otherwise fall through.
624 */
625 if (codec->dapm.idle_bias_off) {
626 dev_dbg(codec->dev,
627 "idle_bias_off CODEC on over suspend\n");
628 break;
629 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000630 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100631 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000632 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900633 codec->cache_sync = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000634 break;
635 default:
636 dev_dbg(codec->dev, "CODEC is on over suspend\n");
637 break;
638 }
639 }
640 }
641
642 for (i = 0; i < card->num_rtd; i++) {
643 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
644
645 if (card->rtd[i].dai_link->ignore_suspend)
646 continue;
647
648 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
649 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200650 }
651
Mark Brown87506542008-11-18 20:50:34 +0000652 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000653 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200654
655 return 0;
656}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000657EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200658
Andy Green6ed25972008-06-13 16:24:05 +0100659/* deferred resume work, so resume can complete before we finished
660 * setting our codec back up, which can be very slow on I2C
661 */
662static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200663{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000664 struct snd_soc_card *card =
665 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200666 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200667 int i;
668
Andy Green6ed25972008-06-13 16:24:05 +0100669 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
670 * so userspace apps are blocked from touching us
671 */
672
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000673 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100674
Mark Brown99497882010-05-07 20:24:05 +0100675 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000676 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100677
Mark Brown87506542008-11-18 20:50:34 +0000678 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000679 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200680
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000681 /* resume AC97 DAIs */
682 for (i = 0; i < card->num_rtd; i++) {
683 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100684
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000685 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100686 continue;
687
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000688 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
689 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690 }
691
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200692 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000693 /* If the CODEC was idle over suspend then it will have been
694 * left with bias OFF or STANDBY and suspended so we must now
695 * resume. Otherwise the suspend was suppressed.
696 */
697 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200698 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000699 case SND_SOC_BIAS_STANDBY:
700 case SND_SOC_BIAS_OFF:
701 codec->driver->resume(codec);
702 codec->suspended = 0;
703 break;
704 default:
705 dev_dbg(codec->dev, "CODEC was on over suspend\n");
706 break;
707 }
Mark Brown1547aba2010-05-07 21:11:40 +0100708 }
709 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200710
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000711 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100712
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000713 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100714 continue;
715
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800716 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000717 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800718 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000719
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800720 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000721 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800722 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200723 }
724
Mark Brown3ff3f642008-05-19 12:32:25 +0200725 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000726 for (i = 0; i < card->num_rtd; i++) {
727 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
728 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100729
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000730 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100731 continue;
732
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000733 if (drv->ops->digital_mute && dai->playback_active)
734 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200735 }
736
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000737 for (i = 0; i < card->num_rtd; i++) {
738 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
739 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100740
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000741 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100742 continue;
743
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000744 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
745 cpu_dai->driver->resume(cpu_dai);
746 if (platform->driver->resume && platform->suspended) {
747 platform->driver->resume(cpu_dai);
748 platform->suspended = 0;
749 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200750 }
751
Mark Brown87506542008-11-18 20:50:34 +0000752 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000753 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200754
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000755 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100756
757 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000758 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100759}
760
761/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000762int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100763{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000764 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600765 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200766
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800767 /* If the initialization of this soc device failed, there is no codec
768 * associated with it. Just bail out in this case.
769 */
770 if (list_empty(&card->codec_dev_list))
771 return 0;
772
Mark Brown64ab9ba2009-03-31 11:27:03 +0100773 /* AC97 devices might have other drivers hanging off them so
774 * need to resume immediately. Other drivers don't have that
775 * problem and may take a substantial amount of time to resume
776 * due to I/O costs and anti-pop so handle them out of line.
777 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000778 for (i = 0; i < card->num_rtd; i++) {
779 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600780 ac97_control |= cpu_dai->driver->ac97_control;
781 }
782 if (ac97_control) {
783 dev_dbg(dev, "Resuming AC97 immediately\n");
784 soc_resume_deferred(&card->deferred_resume_work);
785 } else {
786 dev_dbg(dev, "Scheduling resume work\n");
787 if (!schedule_work(&card->deferred_resume_work))
788 dev_err(dev, "resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100789 }
Andy Green6ed25972008-06-13 16:24:05 +0100790
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200791 return 0;
792}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000793EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200794#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000795#define snd_soc_suspend NULL
796#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200797#endif
798
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100799static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800800};
801
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000802static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200803{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000804 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
805 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000806 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000807 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000808 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100809 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200810
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000811 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000812
Mark Brownb19e6e72012-03-14 21:18:39 +0000813 /* Find CPU DAI from registered DAIs*/
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000814 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600815 if (dai_link->cpu_of_node &&
816 (cpu_dai->dev->of_node != dai_link->cpu_of_node))
817 continue;
818 if (dai_link->cpu_name &&
819 strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
820 continue;
821 if (dai_link->cpu_dai_name &&
822 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
823 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700824
825 rtd->cpu_dai = cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000826 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000827
828 if (!rtd->cpu_dai) {
829 dev_dbg(card->dev, "CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000830 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000831 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000832 }
833
Mark Brownb19e6e72012-03-14 21:18:39 +0000834 /* Find CODEC from registered CODECs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000835 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700836 if (dai_link->codec_of_node) {
837 if (codec->dev->of_node != dai_link->codec_of_node)
838 continue;
839 } else {
840 if (strcmp(codec->name, dai_link->codec_name))
841 continue;
842 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000843
Stephen Warren2610ab72011-12-07 13:58:27 -0700844 rtd->codec = codec;
845
846 /*
847 * CODEC found, so find CODEC DAI from registered DAIs from
848 * this CODEC
849 */
850 list_for_each_entry(codec_dai, &dai_list, list) {
851 if (codec->dev == codec_dai->dev &&
852 !strcmp(codec_dai->name,
853 dai_link->codec_dai_name)) {
854
855 rtd->codec_dai = codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000856 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000857 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000858
859 if (!rtd->codec_dai) {
860 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
Stephen Warren2610ab72011-12-07 13:58:27 -0700861 dai_link->codec_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000862 return -EPROBE_DEFER;
863 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000864 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000865
Mark Brownb19e6e72012-03-14 21:18:39 +0000866 if (!rtd->codec) {
867 dev_dbg(card->dev, "CODEC %s not registered\n",
868 dai_link->codec_name);
869 return -EPROBE_DEFER;
870 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100871
872 /* if there's no platform we match on the empty platform */
873 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700874 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100875 platform_name = "snd-soc-dummy";
876
Mark Brownb19e6e72012-03-14 21:18:39 +0000877 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000878 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700879 if (dai_link->platform_of_node) {
880 if (platform->dev->of_node !=
881 dai_link->platform_of_node)
882 continue;
883 } else {
884 if (strcmp(platform->name, platform_name))
885 continue;
886 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700887
888 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000889 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000890 if (!rtd->platform) {
891 dev_dbg(card->dev, "platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000892 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000893 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000894 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000895
896 card->num_rtd++;
897
898 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000899}
900
Stephen Warrend12cd192012-06-08 12:34:22 -0600901static int soc_remove_platform(struct snd_soc_platform *platform)
902{
903 int ret;
904
905 if (platform->driver->remove) {
906 ret = platform->driver->remove(platform);
907 if (ret < 0)
908 pr_err("asoc: failed to remove %s: %d\n",
909 platform->name, ret);
910 }
911
912 /* Make sure all DAPM widgets are freed */
913 snd_soc_dapm_free(&platform->dapm);
914
915 soc_cleanup_platform_debugfs(platform);
916 platform->probed = 0;
917 list_del(&platform->card_list);
918 module_put(platform->dev->driver->owner);
919
920 return 0;
921}
922
Jarkko Nikula589c3562010-12-06 16:27:07 +0200923static void soc_remove_codec(struct snd_soc_codec *codec)
924{
925 int err;
926
927 if (codec->driver->remove) {
928 err = codec->driver->remove(codec);
929 if (err < 0)
930 dev_err(codec->dev,
931 "asoc: failed to remove %s: %d\n",
932 codec->name, err);
933 }
934
935 /* Make sure all DAPM widgets are freed */
936 snd_soc_dapm_free(&codec->dapm);
937
938 soc_cleanup_codec_debugfs(codec);
939 codec->probed = 0;
940 list_del(&codec->card_list);
941 module_put(codec->dev->driver->owner);
942}
943
Stephen Warren62ae68f2012-06-08 12:34:23 -0600944static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000945{
946 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000947 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
948 int err;
949
950 /* unregister the rtd device */
951 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800952 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
953 device_remove_file(rtd->dev, &dev_attr_codec_reg);
954 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000955 rtd->dev_registered = 0;
956 }
957
958 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100959 if (codec_dai && codec_dai->probed &&
960 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000961 if (codec_dai->driver->remove) {
962 err = codec_dai->driver->remove(codec_dai);
963 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200964 pr_err("asoc: failed to remove %s: %d\n",
965 codec_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000966 }
967 codec_dai->probed = 0;
968 list_del(&codec_dai->card_list);
969 }
970
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000971 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100972 if (cpu_dai && cpu_dai->probed &&
973 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000974 if (cpu_dai->driver->remove) {
975 err = cpu_dai->driver->remove(cpu_dai);
976 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200977 pr_err("asoc: failed to remove %s: %d\n",
978 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000979 }
980 cpu_dai->probed = 0;
981 list_del(&cpu_dai->card_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -0600982
Stephen Warren18d75642012-06-08 12:34:21 -0600983 if (!cpu_dai->codec) {
984 snd_soc_dapm_free(&cpu_dai->dapm);
Stephen Warrena9db7db2012-06-08 12:34:20 -0600985 module_put(cpu_dai->dev->driver->owner);
Stephen Warren18d75642012-06-08 12:34:21 -0600986 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000987 }
988}
989
Stephen Warren62ae68f2012-06-08 12:34:23 -0600990static void soc_remove_link_components(struct snd_soc_card *card, int num,
991 int order)
992{
993 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
994 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
995 struct snd_soc_dai *codec_dai = rtd->codec_dai;
996 struct snd_soc_platform *platform = rtd->platform;
997 struct snd_soc_codec *codec;
998
999 /* remove the platform */
1000 if (platform && platform->probed &&
1001 platform->driver->remove_order == order) {
1002 soc_remove_platform(platform);
1003 }
1004
1005 /* remove the CODEC-side CODEC */
1006 if (codec_dai) {
1007 codec = codec_dai->codec;
1008 if (codec && codec->probed &&
1009 codec->driver->remove_order == order)
1010 soc_remove_codec(codec);
1011 }
1012
1013 /* remove any CPU-side CODEC */
1014 if (cpu_dai) {
1015 codec = cpu_dai->codec;
1016 if (codec && codec->probed &&
1017 codec->driver->remove_order == order)
1018 soc_remove_codec(codec);
1019 }
1020}
1021
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001022static void soc_remove_dai_links(struct snd_soc_card *card)
1023{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001024 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001025
Liam Girdwood0168bf02011-06-07 16:08:05 +01001026 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1027 order++) {
1028 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001029 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001030 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001031
1032 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1033 order++) {
1034 for (dai = 0; dai < card->num_rtd; dai++)
1035 soc_remove_link_components(card, dai, order);
1036 }
1037
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001038 card->num_rtd = 0;
1039}
1040
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001041static void soc_set_name_prefix(struct snd_soc_card *card,
1042 struct snd_soc_codec *codec)
1043{
1044 int i;
1045
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001046 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001047 return;
1048
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001049 for (i = 0; i < card->num_configs; i++) {
1050 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001051 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1052 codec->name_prefix = map->name_prefix;
1053 break;
1054 }
1055 }
1056}
1057
Jarkko Nikula589c3562010-12-06 16:27:07 +02001058static int soc_probe_codec(struct snd_soc_card *card,
1059 struct snd_soc_codec *codec)
1060{
1061 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001062 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001063 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001064
1065 codec->card = card;
1066 codec->dapm.card = card;
1067 soc_set_name_prefix(card, codec);
1068
Jarkko Nikula70d29332011-01-27 16:24:22 +02001069 if (!try_module_get(codec->dev->driver->owner))
1070 return -ENODEV;
1071
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001072 soc_init_codec_debugfs(codec);
1073
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001074 if (driver->dapm_widgets)
1075 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1076 driver->num_dapm_widgets);
1077
Mark Brown888df392012-02-16 19:37:51 -08001078 /* Create DAPM widgets for each DAI stream */
1079 list_for_each_entry(dai, &dai_list, list) {
1080 if (dai->dev != codec->dev)
1081 continue;
1082
1083 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1084 }
1085
Mark Brown33c5f962011-08-22 18:40:30 +01001086 codec->dapm.idle_bias_off = driver->idle_bias_off;
1087
Mark Brown89b95ac02011-03-07 16:38:44 +00001088 if (driver->probe) {
1089 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001090 if (ret < 0) {
1091 dev_err(codec->dev,
1092 "asoc: failed to probe CODEC %s: %d\n",
1093 codec->name, ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001094 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001095 }
1096 }
1097
Mark Brown38cbf952012-06-22 13:04:02 +01001098 /* If the driver didn't set I/O up try regmap */
Mark Brown98d30882012-08-01 20:05:47 +01001099 if (!codec->write && dev_get_regmap(codec->dev, NULL))
Mark Brown38cbf952012-06-22 13:04:02 +01001100 snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
1101
Mark Brownb7af1da2011-04-07 19:18:44 +09001102 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001103 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001104 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001105 if (driver->dapm_routes)
1106 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1107 driver->num_dapm_routes);
1108
Jarkko Nikula589c3562010-12-06 16:27:07 +02001109 /* mark codec as probed and add to card codec list */
1110 codec->probed = 1;
1111 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001112 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001113
Jarkko Nikula70d29332011-01-27 16:24:22 +02001114 return 0;
1115
1116err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001117 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001118 module_put(codec->dev->driver->owner);
1119
Jarkko Nikula589c3562010-12-06 16:27:07 +02001120 return ret;
1121}
1122
Liam Girdwood956245e2011-07-01 16:54:08 +01001123static int soc_probe_platform(struct snd_soc_card *card,
1124 struct snd_soc_platform *platform)
1125{
1126 int ret = 0;
1127 const struct snd_soc_platform_driver *driver = platform->driver;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001128 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001129
1130 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001131 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001132
1133 if (!try_module_get(platform->dev->driver->owner))
1134 return -ENODEV;
1135
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001136 soc_init_platform_debugfs(platform);
1137
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001138 if (driver->dapm_widgets)
1139 snd_soc_dapm_new_controls(&platform->dapm,
1140 driver->dapm_widgets, driver->num_dapm_widgets);
1141
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001142 /* Create DAPM widgets for each DAI stream */
1143 list_for_each_entry(dai, &dai_list, list) {
1144 if (dai->dev != platform->dev)
1145 continue;
1146
1147 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1148 }
1149
Stephen Warren3fec6b62012-04-05 12:28:01 -06001150 platform->dapm.idle_bias_off = 1;
1151
Liam Girdwood956245e2011-07-01 16:54:08 +01001152 if (driver->probe) {
1153 ret = driver->probe(platform);
1154 if (ret < 0) {
1155 dev_err(platform->dev,
1156 "asoc: failed to probe platform %s: %d\n",
1157 platform->name, ret);
1158 goto err_probe;
1159 }
1160 }
1161
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001162 if (driver->controls)
1163 snd_soc_add_platform_controls(platform, driver->controls,
1164 driver->num_controls);
1165 if (driver->dapm_routes)
1166 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1167 driver->num_dapm_routes);
1168
Liam Girdwood956245e2011-07-01 16:54:08 +01001169 /* mark platform as probed and add to card platform list */
1170 platform->probed = 1;
1171 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001172 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001173
1174 return 0;
1175
1176err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001177 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001178 module_put(platform->dev->driver->owner);
1179
1180 return ret;
1181}
1182
Mark Brown36ae1a92012-01-06 17:12:45 -08001183static void rtd_release(struct device *dev)
1184{
1185 kfree(dev);
1186}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001187
Jarkko Nikula589c3562010-12-06 16:27:07 +02001188static int soc_post_component_init(struct snd_soc_card *card,
1189 struct snd_soc_codec *codec,
1190 int num, int dailess)
1191{
1192 struct snd_soc_dai_link *dai_link = NULL;
1193 struct snd_soc_aux_dev *aux_dev = NULL;
1194 struct snd_soc_pcm_runtime *rtd;
1195 const char *temp, *name;
1196 int ret = 0;
1197
1198 if (!dailess) {
1199 dai_link = &card->dai_link[num];
1200 rtd = &card->rtd[num];
1201 name = dai_link->name;
1202 } else {
1203 aux_dev = &card->aux_dev[num];
1204 rtd = &card->rtd_aux[num];
1205 name = aux_dev->name;
1206 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001207 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001208
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001209 /* Make sure all DAPM widgets are instantiated */
1210 snd_soc_dapm_new_widgets(&codec->dapm);
1211
Jarkko Nikula589c3562010-12-06 16:27:07 +02001212 /* machine controls, routes and widgets are not prefixed */
1213 temp = codec->name_prefix;
1214 codec->name_prefix = NULL;
1215
1216 /* do machine specific initialization */
1217 if (!dailess && dai_link->init)
1218 ret = dai_link->init(rtd);
1219 else if (dailess && aux_dev->init)
1220 ret = aux_dev->init(&codec->dapm);
1221 if (ret < 0) {
1222 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1223 return ret;
1224 }
1225 codec->name_prefix = temp;
1226
Jarkko Nikula589c3562010-12-06 16:27:07 +02001227 /* register the rtd device */
1228 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001229
1230 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1231 if (!rtd->dev)
1232 return -ENOMEM;
1233 device_initialize(rtd->dev);
1234 rtd->dev->parent = card->dev;
1235 rtd->dev->release = rtd_release;
1236 rtd->dev->init_name = name;
1237 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001238 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001239 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1240 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1241 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1242 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001243 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001244 if (ret < 0) {
1245 dev_err(card->dev,
1246 "asoc: failed to register runtime device: %d\n", ret);
1247 return ret;
1248 }
1249 rtd->dev_registered = 1;
1250
1251 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001252 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001253 if (ret < 0)
1254 dev_err(codec->dev,
1255 "asoc: failed to add codec dapm sysfs entries: %d\n",
1256 ret);
1257
1258 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001259 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001260 if (ret < 0)
1261 dev_err(codec->dev,
1262 "asoc: failed to add codec sysfs files: %d\n", ret);
1263
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001264#ifdef CONFIG_DEBUG_FS
1265 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001266 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001267 goto out;
1268
1269 ret = soc_dpcm_debugfs_add(rtd);
1270 if (ret < 0)
1271 dev_err(rtd->dev, "asoc: failed to add dpcm sysfs entries: %d\n", ret);
1272
1273out:
1274#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001275 return 0;
1276}
1277
Stephen Warren62ae68f2012-06-08 12:34:23 -06001278static int soc_probe_link_components(struct snd_soc_card *card, int num,
1279 int order)
1280{
1281 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1282 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1283 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1284 struct snd_soc_platform *platform = rtd->platform;
1285 int ret;
1286
1287 /* probe the CPU-side component, if it is a CODEC */
1288 if (cpu_dai->codec &&
1289 !cpu_dai->codec->probed &&
1290 cpu_dai->codec->driver->probe_order == order) {
1291 ret = soc_probe_codec(card, cpu_dai->codec);
1292 if (ret < 0)
1293 return ret;
1294 }
1295
1296 /* probe the CODEC-side component */
1297 if (!codec_dai->codec->probed &&
1298 codec_dai->codec->driver->probe_order == order) {
1299 ret = soc_probe_codec(card, codec_dai->codec);
1300 if (ret < 0)
1301 return ret;
1302 }
1303
1304 /* probe the platform */
1305 if (!platform->probed &&
1306 platform->driver->probe_order == order) {
1307 ret = soc_probe_platform(card, platform);
1308 if (ret < 0)
1309 return ret;
1310 }
1311
1312 return 0;
1313}
1314
1315static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001316{
1317 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1318 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1319 struct snd_soc_codec *codec = rtd->codec;
1320 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001321 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1322 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1323 struct snd_soc_dapm_widget *play_w, *capture_w;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001324 int ret;
1325
Liam Girdwood0168bf02011-06-07 16:08:05 +01001326 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1327 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001328
1329 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001330 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001331 codec_dai->card = card;
1332 cpu_dai->card = card;
1333
1334 /* set default power off timeout */
1335 rtd->pmdown_time = pmdown_time;
1336
1337 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001338 if (!cpu_dai->probed &&
1339 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001340 if (!cpu_dai->codec) {
1341 cpu_dai->dapm.card = card;
1342 if (!try_module_get(cpu_dai->dev->driver->owner))
1343 return -ENODEV;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001344
Stephen Warren18d75642012-06-08 12:34:21 -06001345 list_add(&cpu_dai->dapm.list, &card->dapm_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001346 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1347 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001348
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001349 if (cpu_dai->driver->probe) {
1350 ret = cpu_dai->driver->probe(cpu_dai);
1351 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001352 pr_err("asoc: failed to probe CPU DAI %s: %d\n",
1353 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001354 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001355 return ret;
1356 }
1357 }
1358 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001359 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001360 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1361 }
1362
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001363 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001364 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001365 if (codec_dai->driver->probe) {
1366 ret = codec_dai->driver->probe(codec_dai);
1367 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001368 pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
1369 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001370 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001371 }
1372 }
1373
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001374 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001375 codec_dai->probed = 1;
1376 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001377 }
1378
Liam Girdwood0168bf02011-06-07 16:08:05 +01001379 /* complete DAI probe during last probe */
1380 if (order != SND_SOC_COMP_ORDER_LAST)
1381 return 0;
1382
Jarkko Nikula589c3562010-12-06 16:27:07 +02001383 ret = soc_post_component_init(card, codec, num, 0);
1384 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001385 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001386
Mark Brown36ae1a92012-01-06 17:12:45 -08001387 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001388 if (ret < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -02001389 pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001390
Namarta Kohli1245b702012-08-16 17:10:41 +05301391 if (cpu_dai->driver->compress_dai) {
1392 /*create compress_device"*/
1393 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001394 if (ret < 0) {
Namarta Kohli1245b702012-08-16 17:10:41 +05301395 pr_err("asoc: can't create compress %s\n",
1396 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001397 return ret;
1398 }
1399 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301400
1401 if (!dai_link->params) {
1402 /* create the pcm */
1403 ret = soc_new_pcm(rtd, num);
1404 if (ret < 0) {
1405 pr_err("asoc: can't create pcm %s :%d\n",
1406 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001407 return ret;
1408 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301409 } else {
1410 /* link the DAI widgets */
1411 play_w = codec_dai->playback_widget;
1412 capture_w = cpu_dai->capture_widget;
1413 if (play_w && capture_w) {
1414 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Mark Brownc74184e2012-04-04 22:12:09 +01001415 capture_w, play_w);
Namarta Kohli1245b702012-08-16 17:10:41 +05301416 if (ret != 0) {
1417 dev_err(card->dev, "Can't link %s to %s: %d\n",
1418 play_w->name, capture_w->name, ret);
1419 return ret;
1420 }
1421 }
1422
1423 play_w = cpu_dai->playback_widget;
1424 capture_w = codec_dai->capture_widget;
1425 if (play_w && capture_w) {
1426 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1427 capture_w, play_w);
1428 if (ret != 0) {
1429 dev_err(card->dev, "Can't link %s to %s: %d\n",
1430 play_w->name, capture_w->name, ret);
1431 return ret;
1432 }
Mark Brownc74184e2012-04-04 22:12:09 +01001433 }
1434 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001435 }
1436
1437 /* add platform data for AC97 devices */
1438 if (rtd->codec_dai->driver->ac97_control)
1439 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1440
1441 return 0;
1442}
1443
1444#ifdef CONFIG_SND_SOC_AC97_BUS
1445static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1446{
1447 int ret;
1448
1449 /* Only instantiate AC97 if not already done by the adaptor
1450 * for the generic AC97 subsystem.
1451 */
1452 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001453 /*
1454 * It is possible that the AC97 device is already registered to
1455 * the device subsystem. This happens when the device is created
1456 * via snd_ac97_mixer(). Currently only SoC codec that does so
1457 * is the generic AC97 glue but others migh emerge.
1458 *
1459 * In those cases we don't try to register the device again.
1460 */
1461 if (!rtd->codec->ac97_created)
1462 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001463
1464 ret = soc_ac97_dev_register(rtd->codec);
1465 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001466 pr_err("asoc: AC97 device register failed:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001467 return ret;
1468 }
1469
1470 rtd->codec->ac97_registered = 1;
1471 }
1472 return 0;
1473}
1474
1475static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1476{
1477 if (codec->ac97_registered) {
1478 soc_ac97_dev_unregister(codec);
1479 codec->ac97_registered = 0;
1480 }
1481}
1482#endif
1483
Mark Brownb19e6e72012-03-14 21:18:39 +00001484static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1485{
1486 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1487 struct snd_soc_codec *codec;
1488
1489 /* find CODEC from registered CODECs*/
1490 list_for_each_entry(codec, &codec_list, list) {
1491 if (!strcmp(codec->name, aux_dev->codec_name))
1492 return 0;
1493 }
1494
1495 return -EPROBE_DEFER;
1496}
1497
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001498static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1499{
1500 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001501 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001502 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001503
1504 /* find CODEC from registered CODECs*/
1505 list_for_each_entry(codec, &codec_list, list) {
1506 if (!strcmp(codec->name, aux_dev->codec_name)) {
1507 if (codec->probed) {
1508 dev_err(codec->dev,
1509 "asoc: codec already probed");
1510 ret = -EBUSY;
1511 goto out;
1512 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001513 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001514 }
1515 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001516 /* codec not found */
1517 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001518 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001519
Jarkko Nikula676ad982010-12-03 09:18:22 +02001520found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001521 ret = soc_probe_codec(card, codec);
1522 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001523 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001524
Jarkko Nikula589c3562010-12-06 16:27:07 +02001525 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001526
1527out:
1528 return ret;
1529}
1530
1531static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1532{
1533 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1534 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001535
1536 /* unregister the rtd device */
1537 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001538 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1539 device_del(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001540 rtd->dev_registered = 0;
1541 }
1542
Jarkko Nikula589c3562010-12-06 16:27:07 +02001543 if (codec && codec->probed)
1544 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001545}
1546
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001547static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1548 enum snd_soc_compress_type compress_type)
1549{
1550 int ret;
1551
1552 if (codec->cache_init)
1553 return 0;
1554
1555 /* override the compress_type if necessary */
1556 if (compress_type && codec->compress_type != compress_type)
1557 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001558 ret = snd_soc_cache_init(codec);
1559 if (ret < 0) {
1560 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1561 ret);
1562 return ret;
1563 }
1564 codec->cache_init = 1;
1565 return 0;
1566}
1567
Mark Brownb19e6e72012-03-14 21:18:39 +00001568static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001569{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001570 struct snd_soc_codec *codec;
1571 struct snd_soc_codec_conf *codec_conf;
1572 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001573 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001574 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001575
Liam Girdwood01b9d992012-03-07 10:38:25 +00001576 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001577
Mark Brownb19e6e72012-03-14 21:18:39 +00001578 /* bind DAIs */
1579 for (i = 0; i < card->num_links; i++) {
1580 ret = soc_bind_dai_link(card, i);
1581 if (ret != 0)
1582 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001583 }
1584
Mark Brownb19e6e72012-03-14 21:18:39 +00001585 /* check aux_devs too */
1586 for (i = 0; i < card->num_aux_devs; i++) {
1587 ret = soc_check_aux_dev(card, i);
1588 if (ret != 0)
1589 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001590 }
1591
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001592 /* initialize the register cache for each available codec */
1593 list_for_each_entry(codec, &codec_list, list) {
1594 if (codec->cache_init)
1595 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001596 /* by default we don't override the compress_type */
1597 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001598 /* check to see if we need to override the compress_type */
1599 for (i = 0; i < card->num_configs; ++i) {
1600 codec_conf = &card->codec_conf[i];
1601 if (!strcmp(codec->name, codec_conf->dev_name)) {
1602 compress_type = codec_conf->compress_type;
1603 if (compress_type && compress_type
1604 != codec->compress_type)
1605 break;
1606 }
1607 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001608 ret = snd_soc_init_codec_cache(codec, compress_type);
Mark Brownb19e6e72012-03-14 21:18:39 +00001609 if (ret < 0)
1610 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001611 }
1612
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001613 /* card bind complete so register a sound card */
1614 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1615 card->owner, 0, &card->snd_card);
1616 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001617 pr_err("asoc: can't create sound card for card %s: %d\n",
1618 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001619 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001620 }
1621 card->snd_card->dev = card->dev;
1622
Mark Browne37a4972011-03-02 18:21:57 +00001623 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1624 card->dapm.dev = card->dev;
1625 card->dapm.card = card;
1626 list_add(&card->dapm.list, &card->dapm_list);
1627
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001628#ifdef CONFIG_DEBUG_FS
1629 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1630#endif
1631
Mark Brown88ee1c62011-02-02 10:43:26 +00001632#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001633 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001634 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001635#endif
Andy Green6ed25972008-06-13 16:24:05 +01001636
Mark Brown9a841eb2011-04-12 17:51:37 -07001637 if (card->dapm_widgets)
1638 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1639 card->num_dapm_widgets);
1640
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001641 /* initialise the sound card only once */
1642 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001643 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001644 if (ret < 0)
1645 goto card_probe_error;
1646 }
1647
Stephen Warren62ae68f2012-06-08 12:34:23 -06001648 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001649 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1650 order++) {
1651 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001652 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001653 if (ret < 0) {
1654 pr_err("asoc: failed to instantiate card %s: %d\n",
Stephen Warren62ae68f2012-06-08 12:34:23 -06001655 card->name, ret);
1656 goto probe_dai_err;
1657 }
1658 }
1659 }
1660
1661 /* probe all DAI links on this card */
1662 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1663 order++) {
1664 for (i = 0; i < card->num_links; i++) {
1665 ret = soc_probe_link_dais(card, i, order);
1666 if (ret < 0) {
1667 pr_err("asoc: failed to instantiate card %s: %d\n",
1668 card->name, ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001669 goto probe_dai_err;
1670 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001671 }
1672 }
1673
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001674 for (i = 0; i < card->num_aux_devs; i++) {
1675 ret = soc_probe_aux_dev(card, i);
1676 if (ret < 0) {
1677 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1678 card->name, ret);
1679 goto probe_aux_dev_err;
1680 }
1681 }
1682
Mark Brown888df392012-02-16 19:37:51 -08001683 snd_soc_dapm_link_dai_widgets(card);
1684
Mark Brownb7af1da2011-04-07 19:18:44 +09001685 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001686 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001687
Mark Brownb8ad29d2011-03-02 18:35:51 +00001688 if (card->dapm_routes)
1689 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1690 card->num_dapm_routes);
1691
Mark Brownb90d2f92011-10-10 13:38:06 +01001692 snd_soc_dapm_new_widgets(&card->dapm);
1693
Mark Brown75d9ac42011-09-27 16:41:01 +01001694 for (i = 0; i < card->num_links; i++) {
1695 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001696 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001697
Mark Brownf04209a2012-04-09 16:29:21 +01001698 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001699 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001700 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001701 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001702 dev_warn(card->rtd[i].codec_dai->dev,
1703 "Failed to set DAI format: %d\n",
1704 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001705 }
1706
1707 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001708 if (dai_fmt &&
1709 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001710 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1711 dai_fmt);
1712 if (ret != 0 && ret != -ENOTSUPP)
1713 dev_warn(card->rtd[i].cpu_dai->dev,
1714 "Failed to set DAI format: %d\n",
1715 ret);
1716 } else if (dai_fmt) {
1717 /* Flip the polarity for the "CPU" end */
1718 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1719 switch (dai_link->dai_fmt &
1720 SND_SOC_DAIFMT_MASTER_MASK) {
1721 case SND_SOC_DAIFMT_CBM_CFM:
1722 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1723 break;
1724 case SND_SOC_DAIFMT_CBM_CFS:
1725 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1726 break;
1727 case SND_SOC_DAIFMT_CBS_CFM:
1728 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1729 break;
1730 case SND_SOC_DAIFMT_CBS_CFS:
1731 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1732 break;
1733 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001734
1735 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001736 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001737 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001738 dev_warn(card->rtd[i].cpu_dai->dev,
1739 "Failed to set DAI format: %d\n",
1740 ret);
1741 }
1742 }
1743
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001744 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001745 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001746 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1747 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001748 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1749 "%s", card->driver_name ? card->driver_name : card->name);
1750 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1751 switch (card->snd_card->driver[i]) {
1752 case '_':
1753 case '-':
1754 case '\0':
1755 break;
1756 default:
1757 if (!isalnum(card->snd_card->driver[i]))
1758 card->snd_card->driver[i] = '_';
1759 break;
1760 }
1761 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001762
Mark Brown28e9ad92011-03-02 18:36:34 +00001763 if (card->late_probe) {
1764 ret = card->late_probe(card);
1765 if (ret < 0) {
1766 dev_err(card->dev, "%s late_probe() failed: %d\n",
1767 card->name, ret);
1768 goto probe_aux_dev_err;
1769 }
1770 }
1771
Mark Brown2dc00212011-10-08 13:59:44 +01001772 snd_soc_dapm_new_widgets(&card->dapm);
1773
Stephen Warren16332812011-11-23 12:42:04 -07001774 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001775 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001776 snd_soc_dapm_auto_nc_codec_pins(codec);
1777
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001778 ret = snd_card_register(card->snd_card);
1779 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001780 pr_err("asoc: failed to register soundcard for %s: %d\n",
1781 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001782 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001783 }
1784
1785#ifdef CONFIG_SND_SOC_AC97_BUS
1786 /* register any AC97 codecs */
1787 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001788 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1789 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001790 pr_err("asoc: failed to register AC97 %s: %d\n",
1791 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001792 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001793 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001794 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001795 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001796 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001797#endif
1798
Mark Brown435c5e22008-12-04 15:32:53 +00001799 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001800 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001801 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001802
1803 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001804
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001805probe_aux_dev_err:
1806 for (i = 0; i < card->num_aux_devs; i++)
1807 soc_remove_aux_dev(card, i);
1808
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001809probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001810 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001811
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001812card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001813 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001814 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001815
1816 snd_card_free(card->snd_card);
1817
Mark Brownb19e6e72012-03-14 21:18:39 +00001818base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001819 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001820
Mark Brownb19e6e72012-03-14 21:18:39 +00001821 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001822}
1823
1824/* probes a new socdev */
1825static int soc_probe(struct platform_device *pdev)
1826{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001827 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001828
Vinod Koul70a7ca32011-01-14 19:22:48 +05301829 /*
1830 * no card, so machine driver should be registering card
1831 * we should not be here in that case so ret error
1832 */
1833 if (!card)
1834 return -EINVAL;
1835
Mark Brownfe4085e2012-03-02 13:07:41 +00001836 dev_warn(&pdev->dev,
1837 "ASoC machine %s should use snd_soc_register_card()\n",
1838 card->name);
1839
Mark Brown435c5e22008-12-04 15:32:53 +00001840 /* Bodge while we unpick instantiation */
1841 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001842
Mark Brown28d528c2012-08-09 18:45:23 +01001843 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001844}
1845
Vinod Koulb0e26482011-01-13 22:48:02 +05301846static int soc_cleanup_card_resources(struct snd_soc_card *card)
1847{
Vinod Koulb0e26482011-01-13 22:48:02 +05301848 int i;
1849
1850 /* make sure any delayed work runs */
1851 for (i = 0; i < card->num_rtd; i++) {
1852 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1853 flush_delayed_work_sync(&rtd->delayed_work);
1854 }
1855
1856 /* remove auxiliary devices */
1857 for (i = 0; i < card->num_aux_devs; i++)
1858 soc_remove_aux_dev(card, i);
1859
1860 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001861 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301862
1863 soc_cleanup_card_debugfs(card);
1864
1865 /* remove the card */
1866 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001867 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301868
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001869 snd_soc_dapm_free(&card->dapm);
1870
Vinod Koulb0e26482011-01-13 22:48:02 +05301871 snd_card_free(card->snd_card);
1872 return 0;
1873
1874}
1875
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001876/* removes a socdev */
1877static int soc_remove(struct platform_device *pdev)
1878{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001879 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001880
Mark Brownc5af3a22008-11-28 13:29:45 +00001881 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001882 return 0;
1883}
1884
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001885int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001886{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001887 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001888 int i;
Mark Brown51737472009-06-22 13:16:51 +01001889
1890 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001891 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001892
1893 /* Flush out pmdown_time work - we actually do want to run it
1894 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001895 for (i = 0; i < card->num_rtd; i++) {
1896 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001897 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001898 }
Mark Brown51737472009-06-22 13:16:51 +01001899
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001900 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001901
1902 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001903}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001904EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001905
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001906const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301907 .suspend = snd_soc_suspend,
1908 .resume = snd_soc_resume,
1909 .freeze = snd_soc_suspend,
1910 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001911 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301912 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001913};
Stephen Warrendeb26072011-04-05 19:35:30 -06001914EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001915
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001916/* ASoC platform driver */
1917static struct platform_driver soc_driver = {
1918 .driver = {
1919 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001920 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001921 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001922 },
1923 .probe = soc_probe,
1924 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001925};
1926
Mark Brown096e49d2009-07-05 15:12:22 +01001927/**
1928 * snd_soc_codec_volatile_register: Report if a register is volatile.
1929 *
1930 * @codec: CODEC to query.
1931 * @reg: Register to query.
1932 *
1933 * Boolean function indiciating if a CODEC register is volatile.
1934 */
Mark Brown181e0552011-01-24 14:05:25 +00001935int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1936 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001937{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001938 if (codec->volatile_register)
1939 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001940 else
1941 return 0;
1942}
1943EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1944
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001945/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001946 * snd_soc_codec_readable_register: Report if a register is readable.
1947 *
1948 * @codec: CODEC to query.
1949 * @reg: Register to query.
1950 *
1951 * Boolean function indicating if a CODEC register is readable.
1952 */
1953int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1954 unsigned int reg)
1955{
1956 if (codec->readable_register)
1957 return codec->readable_register(codec, reg);
1958 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001959 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001960}
1961EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1962
1963/**
1964 * snd_soc_codec_writable_register: Report if a register is writable.
1965 *
1966 * @codec: CODEC to query.
1967 * @reg: Register to query.
1968 *
1969 * Boolean function indicating if a CODEC register is writable.
1970 */
1971int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1972 unsigned int reg)
1973{
1974 if (codec->writable_register)
1975 return codec->writable_register(codec, reg);
1976 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001977 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001978}
1979EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1980
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001981int snd_soc_platform_read(struct snd_soc_platform *platform,
1982 unsigned int reg)
1983{
1984 unsigned int ret;
1985
1986 if (!platform->driver->read) {
1987 dev_err(platform->dev, "platform has no read back\n");
1988 return -1;
1989 }
1990
1991 ret = platform->driver->read(platform, reg);
1992 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001993 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001994
1995 return ret;
1996}
1997EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1998
1999int snd_soc_platform_write(struct snd_soc_platform *platform,
2000 unsigned int reg, unsigned int val)
2001{
2002 if (!platform->driver->write) {
2003 dev_err(platform->dev, "platform has no write back\n");
2004 return -1;
2005 }
2006
2007 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002008 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002009 return platform->driver->write(platform, reg, val);
2010}
2011EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2012
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002013/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002014 * snd_soc_new_ac97_codec - initailise AC97 device
2015 * @codec: audio codec
2016 * @ops: AC97 bus operations
2017 * @num: AC97 codec number
2018 *
2019 * Initialises AC97 codec resources for use by ad-hoc devices only.
2020 */
2021int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2022 struct snd_ac97_bus_ops *ops, int num)
2023{
2024 mutex_lock(&codec->mutex);
2025
2026 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2027 if (codec->ac97 == NULL) {
2028 mutex_unlock(&codec->mutex);
2029 return -ENOMEM;
2030 }
2031
2032 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2033 if (codec->ac97->bus == NULL) {
2034 kfree(codec->ac97);
2035 codec->ac97 = NULL;
2036 mutex_unlock(&codec->mutex);
2037 return -ENOMEM;
2038 }
2039
2040 codec->ac97->bus->ops = ops;
2041 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002042
2043 /*
2044 * Mark the AC97 device to be created by us. This way we ensure that the
2045 * device will be registered with the device subsystem later on.
2046 */
2047 codec->ac97_created = 1;
2048
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002049 mutex_unlock(&codec->mutex);
2050 return 0;
2051}
2052EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2053
2054/**
2055 * snd_soc_free_ac97_codec - free AC97 codec device
2056 * @codec: audio codec
2057 *
2058 * Frees AC97 codec device resources.
2059 */
2060void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2061{
2062 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002063#ifdef CONFIG_SND_SOC_AC97_BUS
2064 soc_unregister_ac97_dai_link(codec);
2065#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002066 kfree(codec->ac97->bus);
2067 kfree(codec->ac97);
2068 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002069 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002070 mutex_unlock(&codec->mutex);
2071}
2072EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2073
Mark Brownc3753702010-11-01 15:41:57 -04002074unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2075{
2076 unsigned int ret;
2077
Mark Brownc3acec22010-12-02 16:15:29 +00002078 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002079 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002080 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002081
2082 return ret;
2083}
2084EXPORT_SYMBOL_GPL(snd_soc_read);
2085
2086unsigned int snd_soc_write(struct snd_soc_codec *codec,
2087 unsigned int reg, unsigned int val)
2088{
2089 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002090 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002091 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002092}
2093EXPORT_SYMBOL_GPL(snd_soc_write);
2094
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00002095unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2096 unsigned int reg, const void *data, size_t len)
2097{
2098 return codec->bulk_write_raw(codec, reg, data, len);
2099}
2100EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2101
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002102/**
2103 * snd_soc_update_bits - update codec register bits
2104 * @codec: audio codec
2105 * @reg: codec register
2106 * @mask: register mask
2107 * @value: new value
2108 *
2109 * Writes new register value.
2110 *
Timur Tabi180c3292011-01-10 15:58:13 -06002111 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002112 */
2113int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002114 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002115{
Mark Brown8a713da2011-12-03 12:33:55 +00002116 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002117 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002118 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002119
Mark Brown8a713da2011-12-03 12:33:55 +00002120 if (codec->using_regmap) {
2121 ret = regmap_update_bits_check(codec->control_data, reg,
2122 mask, value, &change);
2123 } else {
2124 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06002125 if (ret < 0)
2126 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00002127
2128 old = ret;
2129 new = (old & ~mask) | (value & mask);
2130 change = old != new;
2131 if (change)
2132 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06002133 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002134
Mark Brown8a713da2011-12-03 12:33:55 +00002135 if (ret < 0)
2136 return ret;
2137
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002138 return change;
2139}
2140EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2141
2142/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002143 * snd_soc_update_bits_locked - update codec register bits
2144 * @codec: audio codec
2145 * @reg: codec register
2146 * @mask: register mask
2147 * @value: new value
2148 *
2149 * Writes new register value, and takes the codec mutex.
2150 *
2151 * Returns 1 for change else 0.
2152 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002153int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2154 unsigned short reg, unsigned int mask,
2155 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002156{
2157 int change;
2158
2159 mutex_lock(&codec->mutex);
2160 change = snd_soc_update_bits(codec, reg, mask, value);
2161 mutex_unlock(&codec->mutex);
2162
2163 return change;
2164}
Mark Browndd1b3d52009-12-04 14:22:03 +00002165EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002166
2167/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002168 * snd_soc_test_bits - test register for change
2169 * @codec: audio codec
2170 * @reg: codec register
2171 * @mask: register mask
2172 * @value: new value
2173 *
2174 * Tests a register with a new value and checks if the new value is
2175 * different from the old value.
2176 *
2177 * Returns 1 for change else 0.
2178 */
2179int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002180 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002181{
2182 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002183 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002184
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002185 old = snd_soc_read(codec, reg);
2186 new = (old & ~mask) | value;
2187 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002188
2189 return change;
2190}
2191EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2192
2193/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002194 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2195 * @substream: the pcm substream
2196 * @hw: the hardware parameters
2197 *
2198 * Sets the substream runtime hardware parameters.
2199 */
2200int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2201 const struct snd_pcm_hardware *hw)
2202{
2203 struct snd_pcm_runtime *runtime = substream->runtime;
2204 runtime->hw.info = hw->info;
2205 runtime->hw.formats = hw->formats;
2206 runtime->hw.period_bytes_min = hw->period_bytes_min;
2207 runtime->hw.period_bytes_max = hw->period_bytes_max;
2208 runtime->hw.periods_min = hw->periods_min;
2209 runtime->hw.periods_max = hw->periods_max;
2210 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2211 runtime->hw.fifo_size = hw->fifo_size;
2212 return 0;
2213}
2214EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2215
2216/**
2217 * snd_soc_cnew - create new control
2218 * @_template: control template
2219 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002220 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002221 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002222 *
2223 * Create a new mixer control from a template control.
2224 *
2225 * Returns 0 for success, else error.
2226 */
2227struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002228 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002229 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002230{
2231 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002232 struct snd_kcontrol *kcontrol;
2233 char *name = NULL;
2234 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002235
2236 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002237 template.index = 0;
2238
Mark Brownefb7ac32011-03-08 17:23:24 +00002239 if (!long_name)
2240 long_name = template.name;
2241
2242 if (prefix) {
2243 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08002244 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00002245 if (!name)
2246 return NULL;
2247
2248 snprintf(name, name_len, "%s %s", prefix, long_name);
2249
2250 template.name = name;
2251 } else {
2252 template.name = long_name;
2253 }
2254
2255 kcontrol = snd_ctl_new1(&template, data);
2256
2257 kfree(name);
2258
2259 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002260}
2261EXPORT_SYMBOL_GPL(snd_soc_cnew);
2262
Liam Girdwood022658b2012-02-03 17:43:09 +00002263static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2264 const struct snd_kcontrol_new *controls, int num_controls,
2265 const char *prefix, void *data)
2266{
2267 int err, i;
2268
2269 for (i = 0; i < num_controls; i++) {
2270 const struct snd_kcontrol_new *control = &controls[i];
2271 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2272 control->name, prefix));
2273 if (err < 0) {
2274 dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2275 return err;
2276 }
2277 }
2278
2279 return 0;
2280}
2281
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002282/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002283 * snd_soc_add_codec_controls - add an array of controls to a codec.
2284 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002285 * duplicating this code.
2286 *
2287 * @codec: codec to add controls to
2288 * @controls: array of controls to add
2289 * @num_controls: number of elements in the array
2290 *
2291 * Return 0 for success, else error.
2292 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002293int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002294 const struct snd_kcontrol_new *controls, int num_controls)
2295{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002296 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002297
Liam Girdwood022658b2012-02-03 17:43:09 +00002298 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2299 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002300}
Liam Girdwood022658b2012-02-03 17:43:09 +00002301EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002302
2303/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002304 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002305 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002306 *
2307 * @platform: platform to add controls to
2308 * @controls: array of controls to add
2309 * @num_controls: number of elements in the array
2310 *
2311 * Return 0 for success, else error.
2312 */
2313int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2314 const struct snd_kcontrol_new *controls, int num_controls)
2315{
2316 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002317
Liam Girdwood022658b2012-02-03 17:43:09 +00002318 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2319 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002320}
2321EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2322
2323/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002324 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2325 * Convenience function to add a list of controls.
2326 *
2327 * @soc_card: SoC card to add controls to
2328 * @controls: array of controls to add
2329 * @num_controls: number of elements in the array
2330 *
2331 * Return 0 for success, else error.
2332 */
2333int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2334 const struct snd_kcontrol_new *controls, int num_controls)
2335{
2336 struct snd_card *card = soc_card->snd_card;
2337
2338 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2339 NULL, soc_card);
2340}
2341EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2342
2343/**
2344 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2345 * Convienience function to add a list of controls.
2346 *
2347 * @dai: DAI to add controls to
2348 * @controls: array of controls to add
2349 * @num_controls: number of elements in the array
2350 *
2351 * Return 0 for success, else error.
2352 */
2353int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2354 const struct snd_kcontrol_new *controls, int num_controls)
2355{
2356 struct snd_card *card = dai->card->snd_card;
2357
2358 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2359 NULL, dai);
2360}
2361EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2362
2363/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002364 * snd_soc_info_enum_double - enumerated double mixer info callback
2365 * @kcontrol: mixer control
2366 * @uinfo: control element information
2367 *
2368 * Callback to provide information about a double enumerated
2369 * mixer control.
2370 *
2371 * Returns 0 for success.
2372 */
2373int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2374 struct snd_ctl_elem_info *uinfo)
2375{
2376 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2377
2378 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2379 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002380 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002381
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002382 if (uinfo->value.enumerated.item > e->max - 1)
2383 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002384 strcpy(uinfo->value.enumerated.name,
2385 e->texts[uinfo->value.enumerated.item]);
2386 return 0;
2387}
2388EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2389
2390/**
2391 * snd_soc_get_enum_double - enumerated double mixer get callback
2392 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002393 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002394 *
2395 * Callback to get the value of a double enumerated mixer.
2396 *
2397 * Returns 0 for success.
2398 */
2399int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2400 struct snd_ctl_elem_value *ucontrol)
2401{
2402 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2403 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002404 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002405
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002406 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002407 ;
2408 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002409 ucontrol->value.enumerated.item[0]
2410 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002411 if (e->shift_l != e->shift_r)
2412 ucontrol->value.enumerated.item[1] =
2413 (val >> e->shift_r) & (bitmask - 1);
2414
2415 return 0;
2416}
2417EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2418
2419/**
2420 * snd_soc_put_enum_double - enumerated double mixer put callback
2421 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002422 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002423 *
2424 * Callback to set the value of a double enumerated mixer.
2425 *
2426 * Returns 0 for success.
2427 */
2428int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2429 struct snd_ctl_elem_value *ucontrol)
2430{
2431 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2432 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002433 unsigned int val;
2434 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002435
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002436 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002437 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002438 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002439 return -EINVAL;
2440 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2441 mask = (bitmask - 1) << e->shift_l;
2442 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002443 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002444 return -EINVAL;
2445 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2446 mask |= (bitmask - 1) << e->shift_r;
2447 }
2448
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002449 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002450}
2451EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2452
2453/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002454 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2455 * @kcontrol: mixer control
2456 * @ucontrol: control element information
2457 *
2458 * Callback to get the value of a double semi enumerated mixer.
2459 *
2460 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2461 * used for handling bitfield coded enumeration for example.
2462 *
2463 * Returns 0 for success.
2464 */
2465int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2466 struct snd_ctl_elem_value *ucontrol)
2467{
2468 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002469 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002470 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002471
2472 reg_val = snd_soc_read(codec, e->reg);
2473 val = (reg_val >> e->shift_l) & e->mask;
2474 for (mux = 0; mux < e->max; mux++) {
2475 if (val == e->values[mux])
2476 break;
2477 }
2478 ucontrol->value.enumerated.item[0] = mux;
2479 if (e->shift_l != e->shift_r) {
2480 val = (reg_val >> e->shift_r) & e->mask;
2481 for (mux = 0; mux < e->max; mux++) {
2482 if (val == e->values[mux])
2483 break;
2484 }
2485 ucontrol->value.enumerated.item[1] = mux;
2486 }
2487
2488 return 0;
2489}
2490EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2491
2492/**
2493 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2494 * @kcontrol: mixer control
2495 * @ucontrol: control element information
2496 *
2497 * Callback to set the value of a double semi enumerated mixer.
2498 *
2499 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2500 * used for handling bitfield coded enumeration for example.
2501 *
2502 * Returns 0 for success.
2503 */
2504int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2505 struct snd_ctl_elem_value *ucontrol)
2506{
2507 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002508 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002509 unsigned int val;
2510 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002511
2512 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2513 return -EINVAL;
2514 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2515 mask = e->mask << e->shift_l;
2516 if (e->shift_l != e->shift_r) {
2517 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2518 return -EINVAL;
2519 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2520 mask |= e->mask << e->shift_r;
2521 }
2522
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002523 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002524}
2525EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2526
2527/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002528 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2529 * @kcontrol: mixer control
2530 * @uinfo: control element information
2531 *
2532 * Callback to provide information about an external enumerated
2533 * single mixer.
2534 *
2535 * Returns 0 for success.
2536 */
2537int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2538 struct snd_ctl_elem_info *uinfo)
2539{
2540 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2541
2542 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2543 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002544 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002545
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002546 if (uinfo->value.enumerated.item > e->max - 1)
2547 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002548 strcpy(uinfo->value.enumerated.name,
2549 e->texts[uinfo->value.enumerated.item]);
2550 return 0;
2551}
2552EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2553
2554/**
2555 * snd_soc_info_volsw_ext - external single mixer info callback
2556 * @kcontrol: mixer control
2557 * @uinfo: control element information
2558 *
2559 * Callback to provide information about a single external mixer control.
2560 *
2561 * Returns 0 for success.
2562 */
2563int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2564 struct snd_ctl_elem_info *uinfo)
2565{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002566 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002567
Mark Brownfd5dfad2009-04-15 21:37:46 +01002568 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002569 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2570 else
2571 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2572
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002573 uinfo->count = 1;
2574 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002575 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002576 return 0;
2577}
2578EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2579
2580/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002581 * snd_soc_info_volsw - single mixer info callback
2582 * @kcontrol: mixer control
2583 * @uinfo: control element information
2584 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002585 * Callback to provide information about a single mixer control, or a double
2586 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002587 *
2588 * Returns 0 for success.
2589 */
2590int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2591 struct snd_ctl_elem_info *uinfo)
2592{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002593 struct soc_mixer_control *mc =
2594 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002595 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002596
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002597 if (!mc->platform_max)
2598 mc->platform_max = mc->max;
2599 platform_max = mc->platform_max;
2600
2601 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002602 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2603 else
2604 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2605
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002606 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002607 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002608 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002609 return 0;
2610}
2611EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2612
2613/**
2614 * snd_soc_get_volsw - single mixer get callback
2615 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002616 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002617 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002618 * Callback to get the value of a single mixer control, or a double mixer
2619 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002620 *
2621 * Returns 0 for success.
2622 */
2623int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2624 struct snd_ctl_elem_value *ucontrol)
2625{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002626 struct soc_mixer_control *mc =
2627 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002628 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002629 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002630 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002631 unsigned int shift = mc->shift;
2632 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002633 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002634 unsigned int mask = (1 << fls(max)) - 1;
2635 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002636
2637 ucontrol->value.integer.value[0] =
2638 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002639 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002640 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002641 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002642
2643 if (snd_soc_volsw_is_stereo(mc)) {
2644 if (reg == reg2)
2645 ucontrol->value.integer.value[1] =
2646 (snd_soc_read(codec, reg) >> rshift) & mask;
2647 else
2648 ucontrol->value.integer.value[1] =
2649 (snd_soc_read(codec, reg2) >> shift) & mask;
2650 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002651 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002652 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002653 }
2654
2655 return 0;
2656}
2657EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2658
2659/**
2660 * snd_soc_put_volsw - single mixer put callback
2661 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002662 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002663 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002664 * Callback to set the value of a single mixer control, or a double mixer
2665 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002666 *
2667 * Returns 0 for success.
2668 */
2669int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2670 struct snd_ctl_elem_value *ucontrol)
2671{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002672 struct soc_mixer_control *mc =
2673 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002674 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002675 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002676 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002677 unsigned int shift = mc->shift;
2678 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002679 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002680 unsigned int mask = (1 << fls(max)) - 1;
2681 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002682 int err;
2683 bool type_2r = 0;
2684 unsigned int val2 = 0;
2685 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002686
2687 val = (ucontrol->value.integer.value[0] & mask);
2688 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002689 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002690 val_mask = mask << shift;
2691 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002692 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002693 val2 = (ucontrol->value.integer.value[1] & mask);
2694 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002695 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002696 if (reg == reg2) {
2697 val_mask |= mask << rshift;
2698 val |= val2 << rshift;
2699 } else {
2700 val2 = val2 << shift;
2701 type_2r = 1;
2702 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002703 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002704 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002705 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002706 return err;
2707
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002708 if (type_2r)
2709 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2710
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002711 return err;
2712}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002713EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002714
Mark Browne13ac2e2008-05-28 17:58:05 +01002715/**
Brian Austin1d99f242012-03-30 10:43:55 -05002716 * snd_soc_get_volsw_sx - single mixer get callback
2717 * @kcontrol: mixer control
2718 * @ucontrol: control element information
2719 *
2720 * Callback to get the value of a single mixer control, or a double mixer
2721 * control that spans 2 registers.
2722 *
2723 * Returns 0 for success.
2724 */
2725int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2726 struct snd_ctl_elem_value *ucontrol)
2727{
2728 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2729 struct soc_mixer_control *mc =
2730 (struct soc_mixer_control *)kcontrol->private_value;
2731
2732 unsigned int reg = mc->reg;
2733 unsigned int reg2 = mc->rreg;
2734 unsigned int shift = mc->shift;
2735 unsigned int rshift = mc->rshift;
2736 int max = mc->max;
2737 int min = mc->min;
2738 int mask = (1 << (fls(min + max) - 1)) - 1;
2739
2740 ucontrol->value.integer.value[0] =
2741 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2742
2743 if (snd_soc_volsw_is_stereo(mc))
2744 ucontrol->value.integer.value[1] =
2745 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2746
2747 return 0;
2748}
2749EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2750
2751/**
2752 * snd_soc_put_volsw_sx - double mixer set callback
2753 * @kcontrol: mixer control
2754 * @uinfo: control element information
2755 *
2756 * Callback to set the value of a double mixer control that spans 2 registers.
2757 *
2758 * Returns 0 for success.
2759 */
2760int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2761 struct snd_ctl_elem_value *ucontrol)
2762{
2763 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2764 struct soc_mixer_control *mc =
2765 (struct soc_mixer_control *)kcontrol->private_value;
2766
2767 unsigned int reg = mc->reg;
2768 unsigned int reg2 = mc->rreg;
2769 unsigned int shift = mc->shift;
2770 unsigned int rshift = mc->rshift;
2771 int max = mc->max;
2772 int min = mc->min;
2773 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002774 int err = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002775 unsigned short val, val_mask, val2 = 0;
2776
2777 val_mask = mask << shift;
2778 val = (ucontrol->value.integer.value[0] + min) & mask;
2779 val = val << shift;
2780
2781 if (snd_soc_update_bits_locked(codec, reg, val_mask, val))
2782 return err;
2783
2784 if (snd_soc_volsw_is_stereo(mc)) {
2785 val_mask = mask << rshift;
2786 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2787 val2 = val2 << rshift;
2788
2789 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2790 return err;
2791 }
2792 return 0;
2793}
2794EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2795
2796/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002797 * snd_soc_info_volsw_s8 - signed mixer info callback
2798 * @kcontrol: mixer control
2799 * @uinfo: control element information
2800 *
2801 * Callback to provide information about a signed mixer control.
2802 *
2803 * Returns 0 for success.
2804 */
2805int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2806 struct snd_ctl_elem_info *uinfo)
2807{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002808 struct soc_mixer_control *mc =
2809 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002810 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002811 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002812
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002813 if (!mc->platform_max)
2814 mc->platform_max = mc->max;
2815 platform_max = mc->platform_max;
2816
Mark Browne13ac2e2008-05-28 17:58:05 +01002817 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2818 uinfo->count = 2;
2819 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002820 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002821 return 0;
2822}
2823EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2824
2825/**
2826 * snd_soc_get_volsw_s8 - signed mixer get callback
2827 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002828 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002829 *
2830 * Callback to get the value of a signed mixer control.
2831 *
2832 * Returns 0 for success.
2833 */
2834int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2835 struct snd_ctl_elem_value *ucontrol)
2836{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002837 struct soc_mixer_control *mc =
2838 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002839 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002840 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002841 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002842 int val = snd_soc_read(codec, reg);
2843
2844 ucontrol->value.integer.value[0] =
2845 ((signed char)(val & 0xff))-min;
2846 ucontrol->value.integer.value[1] =
2847 ((signed char)((val >> 8) & 0xff))-min;
2848 return 0;
2849}
2850EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2851
2852/**
2853 * snd_soc_put_volsw_sgn - signed mixer put callback
2854 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002855 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002856 *
2857 * Callback to set the value of a signed mixer control.
2858 *
2859 * Returns 0 for success.
2860 */
2861int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2862 struct snd_ctl_elem_value *ucontrol)
2863{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002864 struct soc_mixer_control *mc =
2865 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002866 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002867 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002868 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002869 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002870
2871 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2872 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2873
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002874 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002875}
2876EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2877
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002878/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002879 * snd_soc_info_volsw_range - single mixer info callback with range.
2880 * @kcontrol: mixer control
2881 * @uinfo: control element information
2882 *
2883 * Callback to provide information, within a range, about a single
2884 * mixer control.
2885 *
2886 * returns 0 for success.
2887 */
2888int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2889 struct snd_ctl_elem_info *uinfo)
2890{
2891 struct soc_mixer_control *mc =
2892 (struct soc_mixer_control *)kcontrol->private_value;
2893 int platform_max;
2894 int min = mc->min;
2895
2896 if (!mc->platform_max)
2897 mc->platform_max = mc->max;
2898 platform_max = mc->platform_max;
2899
2900 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2901 uinfo->count = 1;
2902 uinfo->value.integer.min = 0;
2903 uinfo->value.integer.max = platform_max - min;
2904
2905 return 0;
2906}
2907EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2908
2909/**
2910 * snd_soc_put_volsw_range - single mixer put value callback with range.
2911 * @kcontrol: mixer control
2912 * @ucontrol: control element information
2913 *
2914 * Callback to set the value, within a range, for a single mixer control.
2915 *
2916 * Returns 0 for success.
2917 */
2918int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2919 struct snd_ctl_elem_value *ucontrol)
2920{
2921 struct soc_mixer_control *mc =
2922 (struct soc_mixer_control *)kcontrol->private_value;
2923 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2924 unsigned int reg = mc->reg;
2925 unsigned int shift = mc->shift;
2926 int min = mc->min;
2927 int max = mc->max;
2928 unsigned int mask = (1 << fls(max)) - 1;
2929 unsigned int invert = mc->invert;
2930 unsigned int val, val_mask;
2931
2932 val = ((ucontrol->value.integer.value[0] + min) & mask);
2933 if (invert)
2934 val = max - val;
2935 val_mask = mask << shift;
2936 val = val << shift;
2937
2938 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2939}
2940EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2941
2942/**
2943 * snd_soc_get_volsw_range - single mixer get callback with range
2944 * @kcontrol: mixer control
2945 * @ucontrol: control element information
2946 *
2947 * Callback to get the value, within a range, of a single mixer control.
2948 *
2949 * Returns 0 for success.
2950 */
2951int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2952 struct snd_ctl_elem_value *ucontrol)
2953{
2954 struct soc_mixer_control *mc =
2955 (struct soc_mixer_control *)kcontrol->private_value;
2956 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2957 unsigned int reg = mc->reg;
2958 unsigned int shift = mc->shift;
2959 int min = mc->min;
2960 int max = mc->max;
2961 unsigned int mask = (1 << fls(max)) - 1;
2962 unsigned int invert = mc->invert;
2963
2964 ucontrol->value.integer.value[0] =
2965 (snd_soc_read(codec, reg) >> shift) & mask;
2966 if (invert)
2967 ucontrol->value.integer.value[0] =
2968 max - ucontrol->value.integer.value[0];
2969 ucontrol->value.integer.value[0] =
2970 ucontrol->value.integer.value[0] - min;
2971
2972 return 0;
2973}
2974EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2975
2976/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002977 * snd_soc_limit_volume - Set new limit to an existing volume control.
2978 *
2979 * @codec: where to look for the control
2980 * @name: Name of the control
2981 * @max: new maximum limit
2982 *
2983 * Return 0 for success, else error.
2984 */
2985int snd_soc_limit_volume(struct snd_soc_codec *codec,
2986 const char *name, int max)
2987{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002988 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002989 struct snd_kcontrol *kctl;
2990 struct soc_mixer_control *mc;
2991 int found = 0;
2992 int ret = -EINVAL;
2993
2994 /* Sanity check for name and max */
2995 if (unlikely(!name || max <= 0))
2996 return -EINVAL;
2997
2998 list_for_each_entry(kctl, &card->controls, list) {
2999 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3000 found = 1;
3001 break;
3002 }
3003 }
3004 if (found) {
3005 mc = (struct soc_mixer_control *)kctl->private_value;
3006 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003007 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003008 ret = 0;
3009 }
3010 }
3011 return ret;
3012}
3013EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3014
Mark Brown71d08512011-10-10 18:31:26 +01003015int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3016 struct snd_ctl_elem_info *uinfo)
3017{
3018 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3019 struct soc_bytes *params = (void *)kcontrol->private_value;
3020
3021 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3022 uinfo->count = params->num_regs * codec->val_bytes;
3023
3024 return 0;
3025}
3026EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3027
3028int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3029 struct snd_ctl_elem_value *ucontrol)
3030{
3031 struct soc_bytes *params = (void *)kcontrol->private_value;
3032 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3033 int ret;
3034
3035 if (codec->using_regmap)
3036 ret = regmap_raw_read(codec->control_data, params->base,
3037 ucontrol->value.bytes.data,
3038 params->num_regs * codec->val_bytes);
3039 else
3040 ret = -EINVAL;
3041
Mark Brownf831b052012-02-17 16:20:33 -08003042 /* Hide any masked bytes to ensure consistent data reporting */
3043 if (ret == 0 && params->mask) {
3044 switch (codec->val_bytes) {
3045 case 1:
3046 ucontrol->value.bytes.data[0] &= ~params->mask;
3047 break;
3048 case 2:
3049 ((u16 *)(&ucontrol->value.bytes.data))[0]
3050 &= ~params->mask;
3051 break;
3052 case 4:
3053 ((u32 *)(&ucontrol->value.bytes.data))[0]
3054 &= ~params->mask;
3055 break;
3056 default:
3057 return -EINVAL;
3058 }
3059 }
3060
Mark Brown71d08512011-10-10 18:31:26 +01003061 return ret;
3062}
3063EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3064
3065int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3066 struct snd_ctl_elem_value *ucontrol)
3067{
3068 struct soc_bytes *params = (void *)kcontrol->private_value;
3069 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08003070 int ret, len;
3071 unsigned int val;
3072 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003073
Mark Brownf831b052012-02-17 16:20:33 -08003074 if (!codec->using_regmap)
3075 return -EINVAL;
3076
3077 data = ucontrol->value.bytes.data;
3078 len = params->num_regs * codec->val_bytes;
3079
3080 /*
3081 * If we've got a mask then we need to preserve the register
3082 * bits. We shouldn't modify the incoming data so take a
3083 * copy.
3084 */
3085 if (params->mask) {
3086 ret = regmap_read(codec->control_data, params->base, &val);
3087 if (ret != 0)
3088 return ret;
3089
3090 val &= params->mask;
3091
3092 data = kmemdup(data, len, GFP_KERNEL);
3093 if (!data)
3094 return -ENOMEM;
3095
3096 switch (codec->val_bytes) {
3097 case 1:
3098 ((u8 *)data)[0] &= ~params->mask;
3099 ((u8 *)data)[0] |= val;
3100 break;
3101 case 2:
3102 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3103 ((u16 *)data)[0] |= cpu_to_be16(val);
3104 break;
3105 case 4:
3106 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3107 ((u32 *)data)[0] |= cpu_to_be32(val);
3108 break;
3109 default:
3110 return -EINVAL;
3111 }
3112 }
3113
3114 ret = regmap_raw_write(codec->control_data, params->base,
3115 data, len);
3116
3117 if (params->mask)
3118 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003119
3120 return ret;
3121}
3122EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3123
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003124/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003125 * snd_soc_info_xr_sx - signed multi register info callback
3126 * @kcontrol: mreg control
3127 * @uinfo: control element information
3128 *
3129 * Callback to provide information of a control that can
3130 * span multiple codec registers which together
3131 * forms a single signed value in a MSB/LSB manner.
3132 *
3133 * Returns 0 for success.
3134 */
3135int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3136 struct snd_ctl_elem_info *uinfo)
3137{
3138 struct soc_mreg_control *mc =
3139 (struct soc_mreg_control *)kcontrol->private_value;
3140 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3141 uinfo->count = 1;
3142 uinfo->value.integer.min = mc->min;
3143 uinfo->value.integer.max = mc->max;
3144
3145 return 0;
3146}
3147EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3148
3149/**
3150 * snd_soc_get_xr_sx - signed multi register get callback
3151 * @kcontrol: mreg control
3152 * @ucontrol: control element information
3153 *
3154 * Callback to get the value of a control that can span
3155 * multiple codec registers which together forms a single
3156 * signed value in a MSB/LSB manner. The control supports
3157 * specifying total no of bits used to allow for bitfields
3158 * across the multiple codec registers.
3159 *
3160 * Returns 0 for success.
3161 */
3162int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3163 struct snd_ctl_elem_value *ucontrol)
3164{
3165 struct soc_mreg_control *mc =
3166 (struct soc_mreg_control *)kcontrol->private_value;
3167 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3168 unsigned int regbase = mc->regbase;
3169 unsigned int regcount = mc->regcount;
3170 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3171 unsigned int regwmask = (1<<regwshift)-1;
3172 unsigned int invert = mc->invert;
3173 unsigned long mask = (1UL<<mc->nbits)-1;
3174 long min = mc->min;
3175 long max = mc->max;
3176 long val = 0;
3177 unsigned long regval;
3178 unsigned int i;
3179
3180 for (i = 0; i < regcount; i++) {
3181 regval = snd_soc_read(codec, regbase+i) & regwmask;
3182 val |= regval << (regwshift*(regcount-i-1));
3183 }
3184 val &= mask;
3185 if (min < 0 && val > max)
3186 val |= ~mask;
3187 if (invert)
3188 val = max - val;
3189 ucontrol->value.integer.value[0] = val;
3190
3191 return 0;
3192}
3193EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3194
3195/**
3196 * snd_soc_put_xr_sx - signed multi register get callback
3197 * @kcontrol: mreg control
3198 * @ucontrol: control element information
3199 *
3200 * Callback to set the value of a control that can span
3201 * multiple codec registers which together forms a single
3202 * signed value in a MSB/LSB manner. The control supports
3203 * specifying total no of bits used to allow for bitfields
3204 * across the multiple codec registers.
3205 *
3206 * Returns 0 for success.
3207 */
3208int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3209 struct snd_ctl_elem_value *ucontrol)
3210{
3211 struct soc_mreg_control *mc =
3212 (struct soc_mreg_control *)kcontrol->private_value;
3213 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3214 unsigned int regbase = mc->regbase;
3215 unsigned int regcount = mc->regcount;
3216 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3217 unsigned int regwmask = (1<<regwshift)-1;
3218 unsigned int invert = mc->invert;
3219 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003220 long max = mc->max;
3221 long val = ucontrol->value.integer.value[0];
3222 unsigned int i, regval, regmask;
3223 int err;
3224
3225 if (invert)
3226 val = max - val;
3227 val &= mask;
3228 for (i = 0; i < regcount; i++) {
3229 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3230 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3231 err = snd_soc_update_bits_locked(codec, regbase+i,
3232 regmask, regval);
3233 if (err < 0)
3234 return err;
3235 }
3236
3237 return 0;
3238}
3239EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3240
3241/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003242 * snd_soc_get_strobe - strobe get callback
3243 * @kcontrol: mixer control
3244 * @ucontrol: control element information
3245 *
3246 * Callback get the value of a strobe mixer control.
3247 *
3248 * Returns 0 for success.
3249 */
3250int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3251 struct snd_ctl_elem_value *ucontrol)
3252{
3253 struct soc_mixer_control *mc =
3254 (struct soc_mixer_control *)kcontrol->private_value;
3255 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3256 unsigned int reg = mc->reg;
3257 unsigned int shift = mc->shift;
3258 unsigned int mask = 1 << shift;
3259 unsigned int invert = mc->invert != 0;
3260 unsigned int val = snd_soc_read(codec, reg) & mask;
3261
3262 if (shift != 0 && val != 0)
3263 val = val >> shift;
3264 ucontrol->value.enumerated.item[0] = val ^ invert;
3265
3266 return 0;
3267}
3268EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3269
3270/**
3271 * snd_soc_put_strobe - strobe put callback
3272 * @kcontrol: mixer control
3273 * @ucontrol: control element information
3274 *
3275 * Callback strobe a register bit to high then low (or the inverse)
3276 * in one pass of a single mixer enum control.
3277 *
3278 * Returns 1 for success.
3279 */
3280int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3281 struct snd_ctl_elem_value *ucontrol)
3282{
3283 struct soc_mixer_control *mc =
3284 (struct soc_mixer_control *)kcontrol->private_value;
3285 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3286 unsigned int reg = mc->reg;
3287 unsigned int shift = mc->shift;
3288 unsigned int mask = 1 << shift;
3289 unsigned int invert = mc->invert != 0;
3290 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3291 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3292 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3293 int err;
3294
3295 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3296 if (err < 0)
3297 return err;
3298
3299 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3300 return err;
3301}
3302EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3303
3304/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003305 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3306 * @dai: DAI
3307 * @clk_id: DAI specific clock ID
3308 * @freq: new clock frequency in Hz
3309 * @dir: new clock direction - input/output.
3310 *
3311 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3312 */
3313int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3314 unsigned int freq, int dir)
3315{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003316 if (dai->driver && dai->driver->ops->set_sysclk)
3317 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003318 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003319 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003320 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003321 else
3322 return -EINVAL;
3323}
3324EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3325
3326/**
Mark Brownec4ee522011-03-07 20:58:11 +00003327 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3328 * @codec: CODEC
3329 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003330 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003331 * @freq: new clock frequency in Hz
3332 * @dir: new clock direction - input/output.
3333 *
3334 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3335 */
3336int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003337 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003338{
3339 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003340 return codec->driver->set_sysclk(codec, clk_id, source,
3341 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003342 else
3343 return -EINVAL;
3344}
3345EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3346
3347/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003348 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3349 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003350 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003351 * @div: new clock divisor.
3352 *
3353 * Configures the clock dividers. This is used to derive the best DAI bit and
3354 * frame clocks from the system or master clock. It's best to set the DAI bit
3355 * and frame clocks as low as possible to save system power.
3356 */
3357int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3358 int div_id, int div)
3359{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003360 if (dai->driver && dai->driver->ops->set_clkdiv)
3361 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003362 else
3363 return -EINVAL;
3364}
3365EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3366
3367/**
3368 * snd_soc_dai_set_pll - configure DAI PLL.
3369 * @dai: DAI
3370 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003371 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003372 * @freq_in: PLL input clock frequency in Hz
3373 * @freq_out: requested PLL output clock frequency in Hz
3374 *
3375 * Configures and enables PLL to generate output clock based on input clock.
3376 */
Mark Brown85488032009-09-05 18:52:16 +01003377int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3378 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003379{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003380 if (dai->driver && dai->driver->ops->set_pll)
3381 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003382 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003383 else if (dai->codec && dai->codec->driver->set_pll)
3384 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3385 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003386 else
3387 return -EINVAL;
3388}
3389EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3390
Mark Brownec4ee522011-03-07 20:58:11 +00003391/*
3392 * snd_soc_codec_set_pll - configure codec PLL.
3393 * @codec: CODEC
3394 * @pll_id: DAI specific PLL ID
3395 * @source: DAI specific source for the PLL
3396 * @freq_in: PLL input clock frequency in Hz
3397 * @freq_out: requested PLL output clock frequency in Hz
3398 *
3399 * Configures and enables PLL to generate output clock based on input clock.
3400 */
3401int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3402 unsigned int freq_in, unsigned int freq_out)
3403{
3404 if (codec->driver->set_pll)
3405 return codec->driver->set_pll(codec, pll_id, source,
3406 freq_in, freq_out);
3407 else
3408 return -EINVAL;
3409}
3410EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3411
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003412/**
3413 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3414 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003415 * @fmt: SND_SOC_DAIFMT_ format value.
3416 *
3417 * Configures the DAI hardware format and clocking.
3418 */
3419int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3420{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003421 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003422 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003423 if (dai->driver->ops->set_fmt == NULL)
3424 return -ENOTSUPP;
3425 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003426}
3427EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3428
3429/**
3430 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3431 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003432 * @tx_mask: bitmask representing active TX slots.
3433 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003434 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003435 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003436 *
3437 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3438 * specific.
3439 */
3440int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003441 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003442{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003443 if (dai->driver && dai->driver->ops->set_tdm_slot)
3444 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003445 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003446 else
3447 return -EINVAL;
3448}
3449EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3450
3451/**
Barry Song472df3c2009-09-12 01:16:29 +08003452 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3453 * @dai: DAI
3454 * @tx_num: how many TX channels
3455 * @tx_slot: pointer to an array which imply the TX slot number channel
3456 * 0~num-1 uses
3457 * @rx_num: how many RX channels
3458 * @rx_slot: pointer to an array which imply the RX slot number channel
3459 * 0~num-1 uses
3460 *
3461 * configure the relationship between channel number and TDM slot number.
3462 */
3463int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3464 unsigned int tx_num, unsigned int *tx_slot,
3465 unsigned int rx_num, unsigned int *rx_slot)
3466{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003467 if (dai->driver && dai->driver->ops->set_channel_map)
3468 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003469 rx_num, rx_slot);
3470 else
3471 return -EINVAL;
3472}
3473EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3474
3475/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003476 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3477 * @dai: DAI
3478 * @tristate: tristate enable
3479 *
3480 * Tristates the DAI so that others can use it.
3481 */
3482int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3483{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003484 if (dai->driver && dai->driver->ops->set_tristate)
3485 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003486 else
3487 return -EINVAL;
3488}
3489EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3490
3491/**
3492 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3493 * @dai: DAI
3494 * @mute: mute enable
3495 *
3496 * Mutes the DAI DAC.
3497 */
3498int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3499{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003500 if (dai->driver && dai->driver->ops->digital_mute)
3501 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003502 else
Mark Brown04570c62012-04-13 19:16:03 +01003503 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003504}
3505EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3506
Mark Brownc5af3a22008-11-28 13:29:45 +00003507/**
3508 * snd_soc_register_card - Register a card with the ASoC core
3509 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003510 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003511 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003512 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303513int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003514{
Mark Brownb19e6e72012-03-14 21:18:39 +00003515 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003516
Mark Brownc5af3a22008-11-28 13:29:45 +00003517 if (!card->name || !card->dev)
3518 return -EINVAL;
3519
Stephen Warren5a504962011-12-21 10:40:59 -07003520 for (i = 0; i < card->num_links; i++) {
3521 struct snd_soc_dai_link *link = &card->dai_link[i];
3522
3523 /*
3524 * Codec must be specified by 1 of name or OF node,
3525 * not both or neither.
3526 */
3527 if (!!link->codec_name == !!link->codec_of_node) {
3528 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003529 "Neither/both codec name/of_node are set for %s\n",
3530 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003531 return -EINVAL;
3532 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003533 /* Codec DAI name must be specified */
3534 if (!link->codec_dai_name) {
3535 dev_err(card->dev, "codec_dai_name not set for %s\n",
3536 link->name);
3537 return -EINVAL;
3538 }
Stephen Warren5a504962011-12-21 10:40:59 -07003539
3540 /*
3541 * Platform may be specified by either name or OF node, but
3542 * can be left unspecified, and a dummy platform will be used.
3543 */
3544 if (link->platform_name && link->platform_of_node) {
3545 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003546 "Both platform name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003547 return -EINVAL;
3548 }
3549
3550 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003551 * CPU device may be specified by either name or OF node, but
3552 * can be left unspecified, and will be matched based on DAI
3553 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003554 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003555 if (link->cpu_name && link->cpu_of_node) {
Stephen Warren5a504962011-12-21 10:40:59 -07003556 dev_err(card->dev,
Stephen Warrenbc926572012-05-25 18:22:11 -06003557 "Neither/both cpu name/of_node are set for %s\n",
3558 link->name);
3559 return -EINVAL;
3560 }
3561 /*
3562 * At least one of CPU DAI name or CPU device name/node must be
3563 * specified
3564 */
3565 if (!link->cpu_dai_name &&
3566 !(link->cpu_name || link->cpu_of_node)) {
3567 dev_err(card->dev,
3568 "Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003569 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003570 return -EINVAL;
3571 }
3572 }
3573
Mark Browned77cc12011-05-03 18:25:34 +01003574 dev_set_drvdata(card->dev, card);
3575
Stephen Warren111c6412011-01-28 14:26:35 -07003576 snd_soc_initialize_card_lists(card);
3577
Vinod Koul150dd2f2011-01-13 22:48:32 +05303578 soc_init_card_debugfs(card);
3579
Mark Brown181a6892012-03-14 20:18:49 +00003580 card->rtd = devm_kzalloc(card->dev,
3581 sizeof(struct snd_soc_pcm_runtime) *
3582 (card->num_links + card->num_aux_devs),
3583 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003584 if (card->rtd == NULL)
3585 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003586 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003587 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003588
3589 for (i = 0; i < card->num_links; i++)
3590 card->rtd[i].dai_link = &card->dai_link[i];
3591
Mark Brownc5af3a22008-11-28 13:29:45 +00003592 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003593 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003594 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003595 mutex_init(&card->mutex);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003596 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003597
Mark Brownb19e6e72012-03-14 21:18:39 +00003598 ret = snd_soc_instantiate_card(card);
3599 if (ret != 0)
3600 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003601
Mark Brownb19e6e72012-03-14 21:18:39 +00003602 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003603}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303604EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003605
3606/**
3607 * snd_soc_unregister_card - Unregister a card with the ASoC core
3608 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003609 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003610 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003611 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303612int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003613{
Vinod Koulb0e26482011-01-13 22:48:02 +05303614 if (card->instantiated)
3615 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003616 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3617
3618 return 0;
3619}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303620EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003621
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003622/*
3623 * Simplify DAI link configuration by removing ".-1" from device names
3624 * and sanitizing names.
3625 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003626static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003627{
3628 char *found, name[NAME_SIZE];
3629 int id1, id2;
3630
3631 if (dev_name(dev) == NULL)
3632 return NULL;
3633
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003634 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003635
3636 /* are we a "%s.%d" name (platform and SPI components) */
3637 found = strstr(name, dev->driver->name);
3638 if (found) {
3639 /* get ID */
3640 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3641
3642 /* discard ID from name if ID == -1 */
3643 if (*id == -1)
3644 found[strlen(dev->driver->name)] = '\0';
3645 }
3646
3647 } else {
3648 /* I2C component devices are named "bus-addr" */
3649 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3650 char tmp[NAME_SIZE];
3651
3652 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003653 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003654
3655 /* sanitize component name for DAI link creation */
3656 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003657 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003658 } else
3659 *id = 0;
3660 }
3661
3662 return kstrdup(name, GFP_KERNEL);
3663}
3664
3665/*
3666 * Simplify DAI link naming for single devices with multiple DAIs by removing
3667 * any ".-1" and using the DAI name (instead of device name).
3668 */
3669static inline char *fmt_multiple_name(struct device *dev,
3670 struct snd_soc_dai_driver *dai_drv)
3671{
3672 if (dai_drv->name == NULL) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02003673 pr_err("asoc: error - multiple DAI %s registered with no name\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003674 dev_name(dev));
3675 return NULL;
3676 }
3677
3678 return kstrdup(dai_drv->name, GFP_KERNEL);
3679}
3680
Mark Brown91151712008-11-30 23:31:24 +00003681/**
3682 * snd_soc_register_dai - Register a DAI with the ASoC core
3683 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003684 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003685 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003686int snd_soc_register_dai(struct device *dev,
3687 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003688{
Mark Brown054880f2012-04-09 17:29:19 +01003689 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003690 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003691
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003692 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003693
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003694 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3695 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003696 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003697
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003698 /* create DAI component name */
3699 dai->name = fmt_single_name(dev, &dai->id);
3700 if (dai->name == NULL) {
3701 kfree(dai);
3702 return -ENOMEM;
3703 }
3704
3705 dai->dev = dev;
3706 dai->driver = dai_drv;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003707 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003708 if (!dai->driver->ops)
3709 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003710
3711 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003712
3713 list_for_each_entry(codec, &codec_list, list) {
3714 if (codec->dev == dev) {
3715 dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3716 dai->name, codec->name);
3717 dai->codec = codec;
3718 break;
3719 }
3720 }
3721
Peter Ujfalusi5f800082012-08-07 10:24:13 +03003722 if (!dai->codec)
3723 dai->dapm.idle_bias_off = 1;
3724
Mark Brown91151712008-11-30 23:31:24 +00003725 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003726
Mark Brown91151712008-11-30 23:31:24 +00003727 mutex_unlock(&client_mutex);
3728
3729 pr_debug("Registered DAI '%s'\n", dai->name);
3730
3731 return 0;
3732}
3733EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3734
3735/**
3736 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3737 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003738 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003739 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003740void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003741{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003742 struct snd_soc_dai *dai;
3743
3744 list_for_each_entry(dai, &dai_list, list) {
3745 if (dev == dai->dev)
3746 goto found;
3747 }
3748 return;
3749
3750found:
Mark Brown91151712008-11-30 23:31:24 +00003751 mutex_lock(&client_mutex);
3752 list_del(&dai->list);
3753 mutex_unlock(&client_mutex);
3754
3755 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003756 kfree(dai->name);
3757 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003758}
3759EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3760
3761/**
3762 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3763 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003764 * @dai: Array of DAIs to register
3765 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003766 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003767int snd_soc_register_dais(struct device *dev,
3768 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003769{
Mark Brown054880f2012-04-09 17:29:19 +01003770 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003771 struct snd_soc_dai *dai;
3772 int i, ret = 0;
3773
Liam Girdwood720ffa42010-08-18 00:30:30 +01003774 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003775
3776 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003777
3778 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003779 if (dai == NULL) {
3780 ret = -ENOMEM;
3781 goto err;
3782 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003783
3784 /* create DAI component name */
3785 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3786 if (dai->name == NULL) {
3787 kfree(dai);
3788 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003789 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003790 }
3791
3792 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003793 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003794 if (dai->driver->id)
3795 dai->id = dai->driver->id;
3796 else
3797 dai->id = i;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003798 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003799 if (!dai->driver->ops)
3800 dai->driver->ops = &null_dai_ops;
3801
3802 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003803
3804 list_for_each_entry(codec, &codec_list, list) {
3805 if (codec->dev == dev) {
3806 dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3807 dai->name, codec->name);
3808 dai->codec = codec;
3809 break;
3810 }
3811 }
3812
Peter Ujfalusi5f800082012-08-07 10:24:13 +03003813 if (!dai->codec)
3814 dai->dapm.idle_bias_off = 1;
3815
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003816 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003817
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003818 mutex_unlock(&client_mutex);
3819
3820 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003821 }
3822
3823 return 0;
3824
3825err:
3826 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003827 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003828
3829 return ret;
3830}
3831EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3832
3833/**
3834 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3835 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003836 * @dai: Array of DAIs to unregister
3837 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003838 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003839void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003840{
3841 int i;
3842
3843 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003844 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003845}
3846EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3847
Mark Brown12a48a8c2008-12-03 19:40:30 +00003848/**
3849 * snd_soc_register_platform - Register a platform with the ASoC core
3850 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003851 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003852 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003853int snd_soc_register_platform(struct device *dev,
3854 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003855{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003856 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003857
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003858 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3859
3860 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3861 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003862 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003863
3864 /* create platform component name */
3865 platform->name = fmt_single_name(dev, &platform->id);
3866 if (platform->name == NULL) {
3867 kfree(platform);
3868 return -ENOMEM;
3869 }
3870
3871 platform->dev = dev;
3872 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003873 platform->dapm.dev = dev;
3874 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003875 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00003876 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003877
3878 mutex_lock(&client_mutex);
3879 list_add(&platform->list, &platform_list);
3880 mutex_unlock(&client_mutex);
3881
3882 pr_debug("Registered platform '%s'\n", platform->name);
3883
3884 return 0;
3885}
3886EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3887
3888/**
3889 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3890 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003891 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003892 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003893void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003894{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003895 struct snd_soc_platform *platform;
3896
3897 list_for_each_entry(platform, &platform_list, list) {
3898 if (dev == platform->dev)
3899 goto found;
3900 }
3901 return;
3902
3903found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003904 mutex_lock(&client_mutex);
3905 list_del(&platform->list);
3906 mutex_unlock(&client_mutex);
3907
3908 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003909 kfree(platform->name);
3910 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003911}
3912EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3913
Mark Brown151ab222009-05-09 16:22:58 +01003914static u64 codec_format_map[] = {
3915 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3916 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3917 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3918 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3919 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3920 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3921 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3922 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3923 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3924 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3925 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3926 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3927 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3928 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3929 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3930 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3931};
3932
3933/* Fix up the DAI formats for endianness: codecs don't actually see
3934 * the endianness of the data but we're using the CPU format
3935 * definitions which do need to include endianness so we ensure that
3936 * codec DAIs always have both big and little endian variants set.
3937 */
3938static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3939{
3940 int i;
3941
3942 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3943 if (stream->formats & codec_format_map[i])
3944 stream->formats |= codec_format_map[i];
3945}
3946
Mark Brown0d0cf002008-12-10 14:32:45 +00003947/**
3948 * snd_soc_register_codec - Register a codec with the ASoC core
3949 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003950 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003951 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003952int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003953 const struct snd_soc_codec_driver *codec_drv,
3954 struct snd_soc_dai_driver *dai_drv,
3955 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003956{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003957 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003958 struct snd_soc_codec *codec;
3959 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003960
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003961 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003962
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003963 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3964 if (codec == NULL)
3965 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003966
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003967 /* create CODEC component name */
3968 codec->name = fmt_single_name(dev, &codec->id);
3969 if (codec->name == NULL) {
3970 kfree(codec);
3971 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003972 }
3973
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003974 if (codec_drv->compress_type)
3975 codec->compress_type = codec_drv->compress_type;
3976 else
3977 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3978
Mark Brownc3acec22010-12-02 16:15:29 +00003979 codec->write = codec_drv->write;
3980 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003981 codec->volatile_register = codec_drv->volatile_register;
3982 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003983 codec->writable_register = codec_drv->writable_register;
Mark Brown5124e692012-02-08 13:20:50 +00003984 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003985 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3986 codec->dapm.dev = dev;
3987 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003988 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003989 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003990 codec->dev = dev;
3991 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003992 codec->num_dai = num_dai;
3993 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003994
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003995 /* allocate CODEC register cache */
3996 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003997 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003998 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003999 /* it is necessary to make a copy of the default register cache
4000 * because in the case of using a compression type that requires
4001 * the default register cache to be marked as __devinitconst the
4002 * kernel might have freed the array by the time we initialize
4003 * the cache.
4004 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00004005 if (codec_drv->reg_cache_default) {
4006 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
4007 reg_size, GFP_KERNEL);
4008 if (!codec->reg_def_copy) {
4009 ret = -ENOMEM;
4010 goto fail;
4011 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004012 }
4013 }
4014
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004015 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
4016 if (!codec->volatile_register)
4017 codec->volatile_register = snd_soc_default_volatile_register;
4018 if (!codec->readable_register)
4019 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004020 if (!codec->writable_register)
4021 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004022 }
4023
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004024 for (i = 0; i < num_dai; i++) {
4025 fixup_codec_formats(&dai_drv[i].playback);
4026 fixup_codec_formats(&dai_drv[i].capture);
4027 }
4028
Mark Brown054880f2012-04-09 17:29:19 +01004029 mutex_lock(&client_mutex);
4030 list_add(&codec->list, &codec_list);
4031 mutex_unlock(&client_mutex);
4032
Mark Brown26b01cc2010-08-18 20:20:55 +01004033 /* register any DAIs */
4034 if (num_dai) {
4035 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4036 if (ret < 0)
Mark Brown054880f2012-04-09 17:29:19 +01004037 dev_err(codec->dev, "Failed to regster DAIs: %d\n",
4038 ret);
Mark Brown26b01cc2010-08-18 20:20:55 +01004039 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004040
Mark Brown0d0cf002008-12-10 14:32:45 +00004041 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004042 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004043
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00004044fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004045 kfree(codec->reg_def_copy);
4046 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004047 kfree(codec->name);
4048 kfree(codec);
4049 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004050}
4051EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4052
4053/**
4054 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4055 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004056 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004057 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004058void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004059{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004060 struct snd_soc_codec *codec;
4061 int i;
4062
4063 list_for_each_entry(codec, &codec_list, list) {
4064 if (dev == codec->dev)
4065 goto found;
4066 }
4067 return;
4068
4069found:
Mark Brown26b01cc2010-08-18 20:20:55 +01004070 if (codec->num_dai)
4071 for (i = 0; i < codec->num_dai; i++)
4072 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004073
Mark Brown0d0cf002008-12-10 14:32:45 +00004074 mutex_lock(&client_mutex);
4075 list_del(&codec->list);
4076 mutex_unlock(&client_mutex);
4077
4078 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004079
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004080 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004081 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01004082 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004083 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004084}
4085EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4086
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004087/* Retrieve a card's name from device tree */
4088int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4089 const char *propname)
4090{
4091 struct device_node *np = card->dev->of_node;
4092 int ret;
4093
4094 ret = of_property_read_string_index(np, propname, 0, &card->name);
4095 /*
4096 * EINVAL means the property does not exist. This is fine providing
4097 * card->name was previously set, which is checked later in
4098 * snd_soc_register_card.
4099 */
4100 if (ret < 0 && ret != -EINVAL) {
4101 dev_err(card->dev,
4102 "Property '%s' could not be read: %d\n",
4103 propname, ret);
4104 return ret;
4105 }
4106
4107 return 0;
4108}
4109EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4110
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004111int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4112 const char *propname)
4113{
4114 struct device_node *np = card->dev->of_node;
4115 int num_routes;
4116 struct snd_soc_dapm_route *routes;
4117 int i, ret;
4118
4119 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004120 if (num_routes < 0 || num_routes & 1) {
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004121 dev_err(card->dev,
Richard Zhaoc34ce322012-04-24 15:24:43 +08004122 "Property '%s' does not exist or its length is not even\n",
4123 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004124 return -EINVAL;
4125 }
4126 num_routes /= 2;
4127 if (!num_routes) {
4128 dev_err(card->dev,
4129 "Property '%s's length is zero\n",
4130 propname);
4131 return -EINVAL;
4132 }
4133
4134 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4135 GFP_KERNEL);
4136 if (!routes) {
4137 dev_err(card->dev,
4138 "Could not allocate DAPM route table\n");
4139 return -EINVAL;
4140 }
4141
4142 for (i = 0; i < num_routes; i++) {
4143 ret = of_property_read_string_index(np, propname,
4144 2 * i, &routes[i].sink);
4145 if (ret) {
4146 dev_err(card->dev,
4147 "Property '%s' index %d could not be read: %d\n",
4148 propname, 2 * i, ret);
Matthias Kaehlckeb761c0c2012-07-11 17:36:34 +02004149 kfree(routes);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004150 return -EINVAL;
4151 }
4152 ret = of_property_read_string_index(np, propname,
4153 (2 * i) + 1, &routes[i].source);
4154 if (ret) {
4155 dev_err(card->dev,
4156 "Property '%s' index %d could not be read: %d\n",
4157 propname, (2 * i) + 1, ret);
Matthias Kaehlckeb761c0c2012-07-11 17:36:34 +02004158 kfree(routes);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004159 return -EINVAL;
4160 }
4161 }
4162
4163 card->num_dapm_routes = num_routes;
4164 card->dapm_routes = routes;
4165
4166 return 0;
4167}
4168EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4169
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004170static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004171{
Mark Brown384c89e2008-12-03 17:34:03 +00004172#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004173 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4174 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004175 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004176 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004177 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004178
Mark Brown8a9dab12011-01-10 22:25:21 +00004179 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004180 &codec_list_fops))
4181 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4182
Mark Brown8a9dab12011-01-10 22:25:21 +00004183 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004184 &dai_list_fops))
4185 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004186
Mark Brown8a9dab12011-01-10 22:25:21 +00004187 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004188 &platform_list_fops))
4189 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004190#endif
4191
Mark Brownfb257892011-04-28 10:57:54 +01004192 snd_soc_util_init();
4193
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004194 return platform_driver_register(&soc_driver);
4195}
Mark Brown4abe8e12010-10-12 17:41:03 +01004196module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004197
Mark Brown7d8c16a2008-11-30 22:11:24 +00004198static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004199{
Mark Brownfb257892011-04-28 10:57:54 +01004200 snd_soc_util_exit();
4201
Mark Brown384c89e2008-12-03 17:34:03 +00004202#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004203 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004204#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004205 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004206}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004207module_exit(snd_soc_exit);
4208
4209/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004210MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004211MODULE_DESCRIPTION("ALSA SoC Core");
4212MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004213MODULE_ALIAS("platform:soc-audio");