blob: 3d803f3cd27292e0f98f2c50672a376cb6dae4f7 [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 Brown9949788b2010-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 Brown9949788b2010-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
Jarkko Nikula589c3562010-12-06 16:27:07 +0200901static void soc_remove_codec(struct snd_soc_codec *codec)
902{
903 int err;
904
905 if (codec->driver->remove) {
906 err = codec->driver->remove(codec);
907 if (err < 0)
908 dev_err(codec->dev,
909 "asoc: failed to remove %s: %d\n",
910 codec->name, err);
911 }
912
913 /* Make sure all DAPM widgets are freed */
914 snd_soc_dapm_free(&codec->dapm);
915
916 soc_cleanup_codec_debugfs(codec);
917 codec->probed = 0;
918 list_del(&codec->card_list);
919 module_put(codec->dev->driver->owner);
920}
921
Liam Girdwood0168bf02011-06-07 16:08:05 +0100922static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000923{
924 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
925 struct snd_soc_codec *codec = rtd->codec;
926 struct snd_soc_platform *platform = rtd->platform;
927 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
928 int err;
929
930 /* unregister the rtd device */
931 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800932 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
933 device_remove_file(rtd->dev, &dev_attr_codec_reg);
934 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000935 rtd->dev_registered = 0;
936 }
937
938 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100939 if (codec_dai && codec_dai->probed &&
940 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000941 if (codec_dai->driver->remove) {
942 err = codec_dai->driver->remove(codec_dai);
943 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200944 pr_err("asoc: failed to remove %s: %d\n",
945 codec_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000946 }
947 codec_dai->probed = 0;
948 list_del(&codec_dai->card_list);
949 }
950
951 /* remove the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100952 if (platform && platform->probed &&
953 platform->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000954 if (platform->driver->remove) {
955 err = platform->driver->remove(platform);
956 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200957 pr_err("asoc: failed to remove %s: %d\n",
958 platform->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000959 }
Liam Girdwood675c496b2012-01-16 15:25:37 +0000960
961 /* Make sure all DAPM widgets are freed */
962 snd_soc_dapm_free(&platform->dapm);
963
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000964 soc_cleanup_platform_debugfs(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000965 platform->probed = 0;
966 list_del(&platform->card_list);
967 module_put(platform->dev->driver->owner);
968 }
969
970 /* remove the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100971 if (codec && codec->probed &&
972 codec->driver->remove_order == order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200973 soc_remove_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000974
975 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100976 if (cpu_dai && cpu_dai->probed &&
977 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000978 if (cpu_dai->driver->remove) {
979 err = cpu_dai->driver->remove(cpu_dai);
980 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200981 pr_err("asoc: failed to remove %s: %d\n",
982 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000983 }
984 cpu_dai->probed = 0;
985 list_del(&cpu_dai->card_list);
986 module_put(cpu_dai->dev->driver->owner);
987 }
988}
989
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900990static void soc_remove_dai_links(struct snd_soc_card *card)
991{
Liam Girdwood0168bf02011-06-07 16:08:05 +0100992 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900993
Liam Girdwood0168bf02011-06-07 16:08:05 +0100994 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
995 order++) {
996 for (dai = 0; dai < card->num_rtd; dai++)
997 soc_remove_dai_link(card, dai, order);
998 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900999 card->num_rtd = 0;
1000}
1001
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001002static void soc_set_name_prefix(struct snd_soc_card *card,
1003 struct snd_soc_codec *codec)
1004{
1005 int i;
1006
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001007 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001008 return;
1009
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001010 for (i = 0; i < card->num_configs; i++) {
1011 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001012 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1013 codec->name_prefix = map->name_prefix;
1014 break;
1015 }
1016 }
1017}
1018
Jarkko Nikula589c3562010-12-06 16:27:07 +02001019static int soc_probe_codec(struct snd_soc_card *card,
1020 struct snd_soc_codec *codec)
1021{
1022 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001023 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001024 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001025
1026 codec->card = card;
1027 codec->dapm.card = card;
1028 soc_set_name_prefix(card, codec);
1029
Jarkko Nikula70d293312011-01-27 16:24:22 +02001030 if (!try_module_get(codec->dev->driver->owner))
1031 return -ENODEV;
1032
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001033 soc_init_codec_debugfs(codec);
1034
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001035 if (driver->dapm_widgets)
1036 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1037 driver->num_dapm_widgets);
1038
Mark Brown888df392012-02-16 19:37:51 -08001039 /* Create DAPM widgets for each DAI stream */
1040 list_for_each_entry(dai, &dai_list, list) {
1041 if (dai->dev != codec->dev)
1042 continue;
1043
1044 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1045 }
1046
Mark Brown33c5f962011-08-22 18:40:30 +01001047 codec->dapm.idle_bias_off = driver->idle_bias_off;
1048
Mark Brown89b95ac02011-03-07 16:38:44 +00001049 if (driver->probe) {
1050 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001051 if (ret < 0) {
1052 dev_err(codec->dev,
1053 "asoc: failed to probe CODEC %s: %d\n",
1054 codec->name, ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001055 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001056 }
1057 }
1058
Mark Brownb7af1da2011-04-07 19:18:44 +09001059 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001060 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001061 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001062 if (driver->dapm_routes)
1063 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1064 driver->num_dapm_routes);
1065
Jarkko Nikula589c3562010-12-06 16:27:07 +02001066 /* mark codec as probed and add to card codec list */
1067 codec->probed = 1;
1068 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001069 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001070
Jarkko Nikula70d293312011-01-27 16:24:22 +02001071 return 0;
1072
1073err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001074 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001075 module_put(codec->dev->driver->owner);
1076
Jarkko Nikula589c3562010-12-06 16:27:07 +02001077 return ret;
1078}
1079
Liam Girdwood956245e2011-07-01 16:54:08 +01001080static int soc_probe_platform(struct snd_soc_card *card,
1081 struct snd_soc_platform *platform)
1082{
1083 int ret = 0;
1084 const struct snd_soc_platform_driver *driver = platform->driver;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001085 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001086
1087 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001088 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001089
1090 if (!try_module_get(platform->dev->driver->owner))
1091 return -ENODEV;
1092
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001093 soc_init_platform_debugfs(platform);
1094
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001095 if (driver->dapm_widgets)
1096 snd_soc_dapm_new_controls(&platform->dapm,
1097 driver->dapm_widgets, driver->num_dapm_widgets);
1098
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001099 /* Create DAPM widgets for each DAI stream */
1100 list_for_each_entry(dai, &dai_list, list) {
1101 if (dai->dev != platform->dev)
1102 continue;
1103
1104 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1105 }
1106
Stephen Warren3fec6b62012-04-05 12:28:01 -06001107 platform->dapm.idle_bias_off = 1;
1108
Liam Girdwood956245e2011-07-01 16:54:08 +01001109 if (driver->probe) {
1110 ret = driver->probe(platform);
1111 if (ret < 0) {
1112 dev_err(platform->dev,
1113 "asoc: failed to probe platform %s: %d\n",
1114 platform->name, ret);
1115 goto err_probe;
1116 }
1117 }
1118
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001119 if (driver->controls)
1120 snd_soc_add_platform_controls(platform, driver->controls,
1121 driver->num_controls);
1122 if (driver->dapm_routes)
1123 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1124 driver->num_dapm_routes);
1125
Liam Girdwood956245e2011-07-01 16:54:08 +01001126 /* mark platform as probed and add to card platform list */
1127 platform->probed = 1;
1128 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001129 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001130
1131 return 0;
1132
1133err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001134 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001135 module_put(platform->dev->driver->owner);
1136
1137 return ret;
1138}
1139
Mark Brown36ae1a92012-01-06 17:12:45 -08001140static void rtd_release(struct device *dev)
1141{
1142 kfree(dev);
1143}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001144
Jarkko Nikula589c3562010-12-06 16:27:07 +02001145static int soc_post_component_init(struct snd_soc_card *card,
1146 struct snd_soc_codec *codec,
1147 int num, int dailess)
1148{
1149 struct snd_soc_dai_link *dai_link = NULL;
1150 struct snd_soc_aux_dev *aux_dev = NULL;
1151 struct snd_soc_pcm_runtime *rtd;
1152 const char *temp, *name;
1153 int ret = 0;
1154
1155 if (!dailess) {
1156 dai_link = &card->dai_link[num];
1157 rtd = &card->rtd[num];
1158 name = dai_link->name;
1159 } else {
1160 aux_dev = &card->aux_dev[num];
1161 rtd = &card->rtd_aux[num];
1162 name = aux_dev->name;
1163 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001164 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001165
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001166 /* Make sure all DAPM widgets are instantiated */
1167 snd_soc_dapm_new_widgets(&codec->dapm);
1168
Jarkko Nikula589c3562010-12-06 16:27:07 +02001169 /* machine controls, routes and widgets are not prefixed */
1170 temp = codec->name_prefix;
1171 codec->name_prefix = NULL;
1172
1173 /* do machine specific initialization */
1174 if (!dailess && dai_link->init)
1175 ret = dai_link->init(rtd);
1176 else if (dailess && aux_dev->init)
1177 ret = aux_dev->init(&codec->dapm);
1178 if (ret < 0) {
1179 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1180 return ret;
1181 }
1182 codec->name_prefix = temp;
1183
Jarkko Nikula589c3562010-12-06 16:27:07 +02001184 /* register the rtd device */
1185 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001186
1187 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1188 if (!rtd->dev)
1189 return -ENOMEM;
1190 device_initialize(rtd->dev);
1191 rtd->dev->parent = card->dev;
1192 rtd->dev->release = rtd_release;
1193 rtd->dev->init_name = name;
1194 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001195 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001196 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1197 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1198 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1199 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001200 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001201 if (ret < 0) {
1202 dev_err(card->dev,
1203 "asoc: failed to register runtime device: %d\n", ret);
1204 return ret;
1205 }
1206 rtd->dev_registered = 1;
1207
1208 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001209 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001210 if (ret < 0)
1211 dev_err(codec->dev,
1212 "asoc: failed to add codec dapm sysfs entries: %d\n",
1213 ret);
1214
1215 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001216 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001217 if (ret < 0)
1218 dev_err(codec->dev,
1219 "asoc: failed to add codec sysfs files: %d\n", ret);
1220
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001221#ifdef CONFIG_DEBUG_FS
1222 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001223 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001224 goto out;
1225
1226 ret = soc_dpcm_debugfs_add(rtd);
1227 if (ret < 0)
1228 dev_err(rtd->dev, "asoc: failed to add dpcm sysfs entries: %d\n", ret);
1229
1230out:
1231#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001232 return 0;
1233}
1234
Liam Girdwood0168bf02011-06-07 16:08:05 +01001235static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001236{
1237 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1238 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1239 struct snd_soc_codec *codec = rtd->codec;
1240 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001241 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1242 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1243 struct snd_soc_dapm_widget *play_w, *capture_w;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001244 int ret;
1245
Liam Girdwood0168bf02011-06-07 16:08:05 +01001246 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1247 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001248
1249 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001250 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001251 codec_dai->card = card;
1252 cpu_dai->card = card;
1253
1254 /* set default power off timeout */
1255 rtd->pmdown_time = pmdown_time;
1256
1257 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001258 if (!cpu_dai->probed &&
1259 cpu_dai->driver->probe_order == order) {
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001260 cpu_dai->dapm.card = card;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001261 if (!try_module_get(cpu_dai->dev->driver->owner))
1262 return -ENODEV;
1263
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001264 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1265
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001266 if (cpu_dai->driver->probe) {
1267 ret = cpu_dai->driver->probe(cpu_dai);
1268 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001269 pr_err("asoc: failed to probe CPU DAI %s: %d\n",
1270 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001271 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001272 return ret;
1273 }
1274 }
1275 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001276 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001277 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1278 }
1279
1280 /* probe the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001281 if (!codec->probed &&
1282 codec->driver->probe_order == order) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001283 ret = soc_probe_codec(card, codec);
1284 if (ret < 0)
1285 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001286 }
1287
1288 /* probe the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001289 if (!platform->probed &&
1290 platform->driver->probe_order == order) {
Liam Girdwood956245e2011-07-01 16:54:08 +01001291 ret = soc_probe_platform(card, platform);
1292 if (ret < 0)
1293 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001294 }
1295
1296 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001297 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001298 if (codec_dai->driver->probe) {
1299 ret = codec_dai->driver->probe(codec_dai);
1300 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001301 pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
1302 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001303 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001304 }
1305 }
1306
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001307 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001308 codec_dai->probed = 1;
1309 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001310 }
1311
Liam Girdwood0168bf02011-06-07 16:08:05 +01001312 /* complete DAI probe during last probe */
1313 if (order != SND_SOC_COMP_ORDER_LAST)
1314 return 0;
1315
Jarkko Nikula589c3562010-12-06 16:27:07 +02001316 ret = soc_post_component_init(card, codec, num, 0);
1317 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001318 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001319
Mark Brown36ae1a92012-01-06 17:12:45 -08001320 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001321 if (ret < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -02001322 pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001323
Mark Brownc74184e2012-04-04 22:12:09 +01001324 if (!dai_link->params) {
1325 /* create the pcm */
1326 ret = soc_new_pcm(rtd, num);
1327 if (ret < 0) {
1328 pr_err("asoc: can't create pcm %s :%d\n",
1329 dai_link->stream_name, ret);
1330 return ret;
1331 }
1332 } else {
1333 /* link the DAI widgets */
1334 play_w = codec_dai->playback_widget;
1335 capture_w = cpu_dai->capture_widget;
1336 if (play_w && capture_w) {
1337 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1338 capture_w, play_w);
1339 if (ret != 0) {
1340 dev_err(card->dev, "Can't link %s to %s: %d\n",
1341 play_w->name, capture_w->name, ret);
1342 return ret;
1343 }
1344 }
1345
1346 play_w = cpu_dai->playback_widget;
1347 capture_w = codec_dai->capture_widget;
1348 if (play_w && capture_w) {
1349 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1350 capture_w, play_w);
1351 if (ret != 0) {
1352 dev_err(card->dev, "Can't link %s to %s: %d\n",
1353 play_w->name, capture_w->name, ret);
1354 return ret;
1355 }
1356 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001357 }
1358
1359 /* add platform data for AC97 devices */
1360 if (rtd->codec_dai->driver->ac97_control)
1361 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1362
1363 return 0;
1364}
1365
1366#ifdef CONFIG_SND_SOC_AC97_BUS
1367static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1368{
1369 int ret;
1370
1371 /* Only instantiate AC97 if not already done by the adaptor
1372 * for the generic AC97 subsystem.
1373 */
1374 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001375 /*
1376 * It is possible that the AC97 device is already registered to
1377 * the device subsystem. This happens when the device is created
1378 * via snd_ac97_mixer(). Currently only SoC codec that does so
1379 * is the generic AC97 glue but others migh emerge.
1380 *
1381 * In those cases we don't try to register the device again.
1382 */
1383 if (!rtd->codec->ac97_created)
1384 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001385
1386 ret = soc_ac97_dev_register(rtd->codec);
1387 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001388 pr_err("asoc: AC97 device register failed:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001389 return ret;
1390 }
1391
1392 rtd->codec->ac97_registered = 1;
1393 }
1394 return 0;
1395}
1396
1397static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1398{
1399 if (codec->ac97_registered) {
1400 soc_ac97_dev_unregister(codec);
1401 codec->ac97_registered = 0;
1402 }
1403}
1404#endif
1405
Mark Brownb19e6e72012-03-14 21:18:39 +00001406static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1407{
1408 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1409 struct snd_soc_codec *codec;
1410
1411 /* find CODEC from registered CODECs*/
1412 list_for_each_entry(codec, &codec_list, list) {
1413 if (!strcmp(codec->name, aux_dev->codec_name))
1414 return 0;
1415 }
1416
1417 return -EPROBE_DEFER;
1418}
1419
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001420static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1421{
1422 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001423 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001424 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001425
1426 /* find CODEC from registered CODECs*/
1427 list_for_each_entry(codec, &codec_list, list) {
1428 if (!strcmp(codec->name, aux_dev->codec_name)) {
1429 if (codec->probed) {
1430 dev_err(codec->dev,
1431 "asoc: codec already probed");
1432 ret = -EBUSY;
1433 goto out;
1434 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001435 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001436 }
1437 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001438 /* codec not found */
1439 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001440 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001441
Jarkko Nikula676ad982010-12-03 09:18:22 +02001442found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001443 ret = soc_probe_codec(card, codec);
1444 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001445 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001446
Jarkko Nikula589c3562010-12-06 16:27:07 +02001447 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001448
1449out:
1450 return ret;
1451}
1452
1453static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1454{
1455 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1456 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001457
1458 /* unregister the rtd device */
1459 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001460 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1461 device_del(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001462 rtd->dev_registered = 0;
1463 }
1464
Jarkko Nikula589c3562010-12-06 16:27:07 +02001465 if (codec && codec->probed)
1466 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001467}
1468
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001469static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1470 enum snd_soc_compress_type compress_type)
1471{
1472 int ret;
1473
1474 if (codec->cache_init)
1475 return 0;
1476
1477 /* override the compress_type if necessary */
1478 if (compress_type && codec->compress_type != compress_type)
1479 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001480 ret = snd_soc_cache_init(codec);
1481 if (ret < 0) {
1482 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1483 ret);
1484 return ret;
1485 }
1486 codec->cache_init = 1;
1487 return 0;
1488}
1489
Mark Brownb19e6e72012-03-14 21:18:39 +00001490static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001491{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001492 struct snd_soc_codec *codec;
1493 struct snd_soc_codec_conf *codec_conf;
1494 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001495 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001496 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001497
Liam Girdwood01b9d992012-03-07 10:38:25 +00001498 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001499
Mark Brownb19e6e72012-03-14 21:18:39 +00001500 /* bind DAIs */
1501 for (i = 0; i < card->num_links; i++) {
1502 ret = soc_bind_dai_link(card, i);
1503 if (ret != 0)
1504 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001505 }
1506
Mark Brownb19e6e72012-03-14 21:18:39 +00001507 /* check aux_devs too */
1508 for (i = 0; i < card->num_aux_devs; i++) {
1509 ret = soc_check_aux_dev(card, i);
1510 if (ret != 0)
1511 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001512 }
1513
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001514 /* initialize the register cache for each available codec */
1515 list_for_each_entry(codec, &codec_list, list) {
1516 if (codec->cache_init)
1517 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001518 /* by default we don't override the compress_type */
1519 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001520 /* check to see if we need to override the compress_type */
1521 for (i = 0; i < card->num_configs; ++i) {
1522 codec_conf = &card->codec_conf[i];
1523 if (!strcmp(codec->name, codec_conf->dev_name)) {
1524 compress_type = codec_conf->compress_type;
1525 if (compress_type && compress_type
1526 != codec->compress_type)
1527 break;
1528 }
1529 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001530 ret = snd_soc_init_codec_cache(codec, compress_type);
Mark Brownb19e6e72012-03-14 21:18:39 +00001531 if (ret < 0)
1532 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001533 }
1534
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001535 /* card bind complete so register a sound card */
1536 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1537 card->owner, 0, &card->snd_card);
1538 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001539 pr_err("asoc: can't create sound card for card %s: %d\n",
1540 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001541 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001542 }
1543 card->snd_card->dev = card->dev;
1544
Mark Browne37a4972011-03-02 18:21:57 +00001545 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1546 card->dapm.dev = card->dev;
1547 card->dapm.card = card;
1548 list_add(&card->dapm.list, &card->dapm_list);
1549
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001550#ifdef CONFIG_DEBUG_FS
1551 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1552#endif
1553
Mark Brown88ee1c62011-02-02 10:43:26 +00001554#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001555 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001556 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001557#endif
Andy Green6ed25972008-06-13 16:24:05 +01001558
Mark Brown9a841eb2011-04-12 17:51:37 -07001559 if (card->dapm_widgets)
1560 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1561 card->num_dapm_widgets);
1562
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001563 /* initialise the sound card only once */
1564 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001565 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001566 if (ret < 0)
1567 goto card_probe_error;
1568 }
1569
Liam Girdwood0168bf02011-06-07 16:08:05 +01001570 /* early DAI link probe */
1571 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1572 order++) {
1573 for (i = 0; i < card->num_links; i++) {
1574 ret = soc_probe_dai_link(card, i, order);
1575 if (ret < 0) {
1576 pr_err("asoc: failed to instantiate card %s: %d\n",
Mark Brown321de0d2010-09-21 15:09:37 +01001577 card->name, ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001578 goto probe_dai_err;
1579 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001580 }
1581 }
1582
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001583 for (i = 0; i < card->num_aux_devs; i++) {
1584 ret = soc_probe_aux_dev(card, i);
1585 if (ret < 0) {
1586 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1587 card->name, ret);
1588 goto probe_aux_dev_err;
1589 }
1590 }
1591
Mark Brown888df392012-02-16 19:37:51 -08001592 snd_soc_dapm_link_dai_widgets(card);
1593
Mark Brownb7af1da2011-04-07 19:18:44 +09001594 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001595 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001596
Mark Brownb8ad29d2011-03-02 18:35:51 +00001597 if (card->dapm_routes)
1598 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1599 card->num_dapm_routes);
1600
Mark Brownb90d2f92011-10-10 13:38:06 +01001601 snd_soc_dapm_new_widgets(&card->dapm);
1602
Mark Brown75d9ac42011-09-27 16:41:01 +01001603 for (i = 0; i < card->num_links; i++) {
1604 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001605 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001606
Mark Brownf04209a2012-04-09 16:29:21 +01001607 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001608 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001609 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001610 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001611 dev_warn(card->rtd[i].codec_dai->dev,
1612 "Failed to set DAI format: %d\n",
1613 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001614 }
1615
1616 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001617 if (dai_fmt &&
1618 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001619 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1620 dai_fmt);
1621 if (ret != 0 && ret != -ENOTSUPP)
1622 dev_warn(card->rtd[i].cpu_dai->dev,
1623 "Failed to set DAI format: %d\n",
1624 ret);
1625 } else if (dai_fmt) {
1626 /* Flip the polarity for the "CPU" end */
1627 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1628 switch (dai_link->dai_fmt &
1629 SND_SOC_DAIFMT_MASTER_MASK) {
1630 case SND_SOC_DAIFMT_CBM_CFM:
1631 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1632 break;
1633 case SND_SOC_DAIFMT_CBM_CFS:
1634 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1635 break;
1636 case SND_SOC_DAIFMT_CBS_CFM:
1637 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1638 break;
1639 case SND_SOC_DAIFMT_CBS_CFS:
1640 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1641 break;
1642 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001643
1644 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001645 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001646 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001647 dev_warn(card->rtd[i].cpu_dai->dev,
1648 "Failed to set DAI format: %d\n",
1649 ret);
1650 }
1651 }
1652
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001653 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001654 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001655 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1656 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001657 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1658 "%s", card->driver_name ? card->driver_name : card->name);
1659 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1660 switch (card->snd_card->driver[i]) {
1661 case '_':
1662 case '-':
1663 case '\0':
1664 break;
1665 default:
1666 if (!isalnum(card->snd_card->driver[i]))
1667 card->snd_card->driver[i] = '_';
1668 break;
1669 }
1670 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001671
Mark Brown28e9ad92011-03-02 18:36:34 +00001672 if (card->late_probe) {
1673 ret = card->late_probe(card);
1674 if (ret < 0) {
1675 dev_err(card->dev, "%s late_probe() failed: %d\n",
1676 card->name, ret);
1677 goto probe_aux_dev_err;
1678 }
1679 }
1680
Mark Brown2dc00212011-10-08 13:59:44 +01001681 snd_soc_dapm_new_widgets(&card->dapm);
1682
Stephen Warren16332812011-11-23 12:42:04 -07001683 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001684 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001685 snd_soc_dapm_auto_nc_codec_pins(codec);
1686
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001687 ret = snd_card_register(card->snd_card);
1688 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001689 pr_err("asoc: failed to register soundcard for %s: %d\n",
1690 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001691 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001692 }
1693
1694#ifdef CONFIG_SND_SOC_AC97_BUS
1695 /* register any AC97 codecs */
1696 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001697 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1698 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001699 pr_err("asoc: failed to register AC97 %s: %d\n",
1700 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001701 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001702 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001703 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001704 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001705 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001706#endif
1707
Mark Brown435c5e22008-12-04 15:32:53 +00001708 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001709 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001710 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001711
1712 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001713
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001714probe_aux_dev_err:
1715 for (i = 0; i < card->num_aux_devs; i++)
1716 soc_remove_aux_dev(card, i);
1717
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001718probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001719 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001720
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001721card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001722 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001723 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001724
1725 snd_card_free(card->snd_card);
1726
Mark Brownb19e6e72012-03-14 21:18:39 +00001727base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001728 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001729
Mark Brownb19e6e72012-03-14 21:18:39 +00001730 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001731}
1732
1733/* probes a new socdev */
1734static int soc_probe(struct platform_device *pdev)
1735{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001736 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001737 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001738
Vinod Koul70a7ca32011-01-14 19:22:48 +05301739 /*
1740 * no card, so machine driver should be registering card
1741 * we should not be here in that case so ret error
1742 */
1743 if (!card)
1744 return -EINVAL;
1745
Mark Brownfe4085e2012-03-02 13:07:41 +00001746 dev_warn(&pdev->dev,
1747 "ASoC machine %s should use snd_soc_register_card()\n",
1748 card->name);
1749
Mark Brown435c5e22008-12-04 15:32:53 +00001750 /* Bodge while we unpick instantiation */
1751 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001752
Mark Brown435c5e22008-12-04 15:32:53 +00001753 ret = snd_soc_register_card(card);
1754 if (ret != 0) {
1755 dev_err(&pdev->dev, "Failed to register card\n");
1756 return ret;
1757 }
1758
1759 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001760}
1761
Vinod Koulb0e26482011-01-13 22:48:02 +05301762static int soc_cleanup_card_resources(struct snd_soc_card *card)
1763{
Vinod Koulb0e26482011-01-13 22:48:02 +05301764 int i;
1765
1766 /* make sure any delayed work runs */
1767 for (i = 0; i < card->num_rtd; i++) {
1768 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1769 flush_delayed_work_sync(&rtd->delayed_work);
1770 }
1771
1772 /* remove auxiliary devices */
1773 for (i = 0; i < card->num_aux_devs; i++)
1774 soc_remove_aux_dev(card, i);
1775
1776 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001777 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301778
1779 soc_cleanup_card_debugfs(card);
1780
1781 /* remove the card */
1782 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001783 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301784
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001785 snd_soc_dapm_free(&card->dapm);
1786
Vinod Koulb0e26482011-01-13 22:48:02 +05301787 snd_card_free(card->snd_card);
1788 return 0;
1789
1790}
1791
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001792/* removes a socdev */
1793static int soc_remove(struct platform_device *pdev)
1794{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001795 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001796
Mark Brownc5af3a22008-11-28 13:29:45 +00001797 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001798 return 0;
1799}
1800
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001801int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001802{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001803 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001804 int i;
Mark Brown51737472009-06-22 13:16:51 +01001805
1806 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001807 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001808
1809 /* Flush out pmdown_time work - we actually do want to run it
1810 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001811 for (i = 0; i < card->num_rtd; i++) {
1812 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001813 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001814 }
Mark Brown51737472009-06-22 13:16:51 +01001815
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001816 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001817
1818 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001819}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001820EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001821
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001822const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301823 .suspend = snd_soc_suspend,
1824 .resume = snd_soc_resume,
1825 .freeze = snd_soc_suspend,
1826 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001827 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301828 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001829};
Stephen Warrendeb26072011-04-05 19:35:30 -06001830EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001831
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001832/* ASoC platform driver */
1833static struct platform_driver soc_driver = {
1834 .driver = {
1835 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001836 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001837 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001838 },
1839 .probe = soc_probe,
1840 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001841};
1842
Mark Brown096e49d2009-07-05 15:12:22 +01001843/**
1844 * snd_soc_codec_volatile_register: Report if a register is volatile.
1845 *
1846 * @codec: CODEC to query.
1847 * @reg: Register to query.
1848 *
1849 * Boolean function indiciating if a CODEC register is volatile.
1850 */
Mark Brown181e0552011-01-24 14:05:25 +00001851int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1852 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001853{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001854 if (codec->volatile_register)
1855 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001856 else
1857 return 0;
1858}
1859EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1860
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001861/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001862 * snd_soc_codec_readable_register: Report if a register is readable.
1863 *
1864 * @codec: CODEC to query.
1865 * @reg: Register to query.
1866 *
1867 * Boolean function indicating if a CODEC register is readable.
1868 */
1869int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1870 unsigned int reg)
1871{
1872 if (codec->readable_register)
1873 return codec->readable_register(codec, reg);
1874 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001875 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001876}
1877EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1878
1879/**
1880 * snd_soc_codec_writable_register: Report if a register is writable.
1881 *
1882 * @codec: CODEC to query.
1883 * @reg: Register to query.
1884 *
1885 * Boolean function indicating if a CODEC register is writable.
1886 */
1887int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1888 unsigned int reg)
1889{
1890 if (codec->writable_register)
1891 return codec->writable_register(codec, reg);
1892 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001893 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001894}
1895EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1896
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001897int snd_soc_platform_read(struct snd_soc_platform *platform,
1898 unsigned int reg)
1899{
1900 unsigned int ret;
1901
1902 if (!platform->driver->read) {
1903 dev_err(platform->dev, "platform has no read back\n");
1904 return -1;
1905 }
1906
1907 ret = platform->driver->read(platform, reg);
1908 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001909 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001910
1911 return ret;
1912}
1913EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1914
1915int snd_soc_platform_write(struct snd_soc_platform *platform,
1916 unsigned int reg, unsigned int val)
1917{
1918 if (!platform->driver->write) {
1919 dev_err(platform->dev, "platform has no write back\n");
1920 return -1;
1921 }
1922
1923 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001924 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001925 return platform->driver->write(platform, reg, val);
1926}
1927EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1928
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001929/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001930 * snd_soc_new_ac97_codec - initailise AC97 device
1931 * @codec: audio codec
1932 * @ops: AC97 bus operations
1933 * @num: AC97 codec number
1934 *
1935 * Initialises AC97 codec resources for use by ad-hoc devices only.
1936 */
1937int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1938 struct snd_ac97_bus_ops *ops, int num)
1939{
1940 mutex_lock(&codec->mutex);
1941
1942 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1943 if (codec->ac97 == NULL) {
1944 mutex_unlock(&codec->mutex);
1945 return -ENOMEM;
1946 }
1947
1948 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1949 if (codec->ac97->bus == NULL) {
1950 kfree(codec->ac97);
1951 codec->ac97 = NULL;
1952 mutex_unlock(&codec->mutex);
1953 return -ENOMEM;
1954 }
1955
1956 codec->ac97->bus->ops = ops;
1957 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001958
1959 /*
1960 * Mark the AC97 device to be created by us. This way we ensure that the
1961 * device will be registered with the device subsystem later on.
1962 */
1963 codec->ac97_created = 1;
1964
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001965 mutex_unlock(&codec->mutex);
1966 return 0;
1967}
1968EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1969
1970/**
1971 * snd_soc_free_ac97_codec - free AC97 codec device
1972 * @codec: audio codec
1973 *
1974 * Frees AC97 codec device resources.
1975 */
1976void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1977{
1978 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001979#ifdef CONFIG_SND_SOC_AC97_BUS
1980 soc_unregister_ac97_dai_link(codec);
1981#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001982 kfree(codec->ac97->bus);
1983 kfree(codec->ac97);
1984 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001985 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001986 mutex_unlock(&codec->mutex);
1987}
1988EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1989
Mark Brownc3753702010-11-01 15:41:57 -04001990unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1991{
1992 unsigned int ret;
1993
Mark Brownc3acec22010-12-02 16:15:29 +00001994 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04001995 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04001996 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04001997
1998 return ret;
1999}
2000EXPORT_SYMBOL_GPL(snd_soc_read);
2001
2002unsigned int snd_soc_write(struct snd_soc_codec *codec,
2003 unsigned int reg, unsigned int val)
2004{
2005 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002006 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002007 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002008}
2009EXPORT_SYMBOL_GPL(snd_soc_write);
2010
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00002011unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2012 unsigned int reg, const void *data, size_t len)
2013{
2014 return codec->bulk_write_raw(codec, reg, data, len);
2015}
2016EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2017
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002018/**
2019 * snd_soc_update_bits - update codec register bits
2020 * @codec: audio codec
2021 * @reg: codec register
2022 * @mask: register mask
2023 * @value: new value
2024 *
2025 * Writes new register value.
2026 *
Timur Tabi180c3292011-01-10 15:58:13 -06002027 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002028 */
2029int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002030 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002031{
Mark Brown8a713da2011-12-03 12:33:55 +00002032 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002033 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002034 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002035
Mark Brown8a713da2011-12-03 12:33:55 +00002036 if (codec->using_regmap) {
2037 ret = regmap_update_bits_check(codec->control_data, reg,
2038 mask, value, &change);
2039 } else {
2040 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06002041 if (ret < 0)
2042 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00002043
2044 old = ret;
2045 new = (old & ~mask) | (value & mask);
2046 change = old != new;
2047 if (change)
2048 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06002049 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002050
Mark Brown8a713da2011-12-03 12:33:55 +00002051 if (ret < 0)
2052 return ret;
2053
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002054 return change;
2055}
2056EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2057
2058/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002059 * snd_soc_update_bits_locked - update codec register bits
2060 * @codec: audio codec
2061 * @reg: codec register
2062 * @mask: register mask
2063 * @value: new value
2064 *
2065 * Writes new register value, and takes the codec mutex.
2066 *
2067 * Returns 1 for change else 0.
2068 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002069int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2070 unsigned short reg, unsigned int mask,
2071 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002072{
2073 int change;
2074
2075 mutex_lock(&codec->mutex);
2076 change = snd_soc_update_bits(codec, reg, mask, value);
2077 mutex_unlock(&codec->mutex);
2078
2079 return change;
2080}
Mark Browndd1b3d52009-12-04 14:22:03 +00002081EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002082
2083/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002084 * snd_soc_test_bits - test register for change
2085 * @codec: audio codec
2086 * @reg: codec register
2087 * @mask: register mask
2088 * @value: new value
2089 *
2090 * Tests a register with a new value and checks if the new value is
2091 * different from the old value.
2092 *
2093 * Returns 1 for change else 0.
2094 */
2095int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002096 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002097{
2098 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002099 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002100
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002101 old = snd_soc_read(codec, reg);
2102 new = (old & ~mask) | value;
2103 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002104
2105 return change;
2106}
2107EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2108
2109/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002110 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2111 * @substream: the pcm substream
2112 * @hw: the hardware parameters
2113 *
2114 * Sets the substream runtime hardware parameters.
2115 */
2116int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2117 const struct snd_pcm_hardware *hw)
2118{
2119 struct snd_pcm_runtime *runtime = substream->runtime;
2120 runtime->hw.info = hw->info;
2121 runtime->hw.formats = hw->formats;
2122 runtime->hw.period_bytes_min = hw->period_bytes_min;
2123 runtime->hw.period_bytes_max = hw->period_bytes_max;
2124 runtime->hw.periods_min = hw->periods_min;
2125 runtime->hw.periods_max = hw->periods_max;
2126 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2127 runtime->hw.fifo_size = hw->fifo_size;
2128 return 0;
2129}
2130EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2131
2132/**
2133 * snd_soc_cnew - create new control
2134 * @_template: control template
2135 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002136 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002137 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002138 *
2139 * Create a new mixer control from a template control.
2140 *
2141 * Returns 0 for success, else error.
2142 */
2143struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002144 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002145 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002146{
2147 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002148 struct snd_kcontrol *kcontrol;
2149 char *name = NULL;
2150 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002151
2152 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002153 template.index = 0;
2154
Mark Brownefb7ac32011-03-08 17:23:24 +00002155 if (!long_name)
2156 long_name = template.name;
2157
2158 if (prefix) {
2159 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08002160 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00002161 if (!name)
2162 return NULL;
2163
2164 snprintf(name, name_len, "%s %s", prefix, long_name);
2165
2166 template.name = name;
2167 } else {
2168 template.name = long_name;
2169 }
2170
2171 kcontrol = snd_ctl_new1(&template, data);
2172
2173 kfree(name);
2174
2175 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002176}
2177EXPORT_SYMBOL_GPL(snd_soc_cnew);
2178
Liam Girdwood022658b2012-02-03 17:43:09 +00002179static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2180 const struct snd_kcontrol_new *controls, int num_controls,
2181 const char *prefix, void *data)
2182{
2183 int err, i;
2184
2185 for (i = 0; i < num_controls; i++) {
2186 const struct snd_kcontrol_new *control = &controls[i];
2187 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2188 control->name, prefix));
2189 if (err < 0) {
2190 dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2191 return err;
2192 }
2193 }
2194
2195 return 0;
2196}
2197
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002198/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002199 * snd_soc_add_codec_controls - add an array of controls to a codec.
2200 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002201 * duplicating this code.
2202 *
2203 * @codec: codec to add controls to
2204 * @controls: array of controls to add
2205 * @num_controls: number of elements in the array
2206 *
2207 * Return 0 for success, else error.
2208 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002209int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002210 const struct snd_kcontrol_new *controls, int num_controls)
2211{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002212 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002213
Liam Girdwood022658b2012-02-03 17:43:09 +00002214 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2215 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002216}
Liam Girdwood022658b2012-02-03 17:43:09 +00002217EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002218
2219/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002220 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002221 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002222 *
2223 * @platform: platform to add controls to
2224 * @controls: array of controls to add
2225 * @num_controls: number of elements in the array
2226 *
2227 * Return 0 for success, else error.
2228 */
2229int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2230 const struct snd_kcontrol_new *controls, int num_controls)
2231{
2232 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002233
Liam Girdwood022658b2012-02-03 17:43:09 +00002234 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2235 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002236}
2237EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2238
2239/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002240 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2241 * Convenience function to add a list of controls.
2242 *
2243 * @soc_card: SoC card to add controls to
2244 * @controls: array of controls to add
2245 * @num_controls: number of elements in the array
2246 *
2247 * Return 0 for success, else error.
2248 */
2249int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2250 const struct snd_kcontrol_new *controls, int num_controls)
2251{
2252 struct snd_card *card = soc_card->snd_card;
2253
2254 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2255 NULL, soc_card);
2256}
2257EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2258
2259/**
2260 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2261 * Convienience function to add a list of controls.
2262 *
2263 * @dai: DAI to add controls to
2264 * @controls: array of controls to add
2265 * @num_controls: number of elements in the array
2266 *
2267 * Return 0 for success, else error.
2268 */
2269int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2270 const struct snd_kcontrol_new *controls, int num_controls)
2271{
2272 struct snd_card *card = dai->card->snd_card;
2273
2274 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2275 NULL, dai);
2276}
2277EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2278
2279/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002280 * snd_soc_info_enum_double - enumerated double mixer info callback
2281 * @kcontrol: mixer control
2282 * @uinfo: control element information
2283 *
2284 * Callback to provide information about a double enumerated
2285 * mixer control.
2286 *
2287 * Returns 0 for success.
2288 */
2289int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2290 struct snd_ctl_elem_info *uinfo)
2291{
2292 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2293
2294 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2295 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002296 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002297
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002298 if (uinfo->value.enumerated.item > e->max - 1)
2299 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002300 strcpy(uinfo->value.enumerated.name,
2301 e->texts[uinfo->value.enumerated.item]);
2302 return 0;
2303}
2304EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2305
2306/**
2307 * snd_soc_get_enum_double - enumerated double mixer get callback
2308 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002309 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002310 *
2311 * Callback to get the value of a double enumerated mixer.
2312 *
2313 * Returns 0 for success.
2314 */
2315int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2316 struct snd_ctl_elem_value *ucontrol)
2317{
2318 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2319 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002320 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002321
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002322 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002323 ;
2324 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002325 ucontrol->value.enumerated.item[0]
2326 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002327 if (e->shift_l != e->shift_r)
2328 ucontrol->value.enumerated.item[1] =
2329 (val >> e->shift_r) & (bitmask - 1);
2330
2331 return 0;
2332}
2333EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2334
2335/**
2336 * snd_soc_put_enum_double - enumerated double mixer put callback
2337 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002338 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002339 *
2340 * Callback to set the value of a double enumerated mixer.
2341 *
2342 * Returns 0 for success.
2343 */
2344int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2345 struct snd_ctl_elem_value *ucontrol)
2346{
2347 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2348 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002349 unsigned int val;
2350 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002351
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002352 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002353 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002354 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002355 return -EINVAL;
2356 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2357 mask = (bitmask - 1) << e->shift_l;
2358 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002359 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002360 return -EINVAL;
2361 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2362 mask |= (bitmask - 1) << e->shift_r;
2363 }
2364
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002365 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002366}
2367EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2368
2369/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002370 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2371 * @kcontrol: mixer control
2372 * @ucontrol: control element information
2373 *
2374 * Callback to get the value of a double semi enumerated mixer.
2375 *
2376 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2377 * used for handling bitfield coded enumeration for example.
2378 *
2379 * Returns 0 for success.
2380 */
2381int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2382 struct snd_ctl_elem_value *ucontrol)
2383{
2384 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002385 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002386 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002387
2388 reg_val = snd_soc_read(codec, e->reg);
2389 val = (reg_val >> e->shift_l) & e->mask;
2390 for (mux = 0; mux < e->max; mux++) {
2391 if (val == e->values[mux])
2392 break;
2393 }
2394 ucontrol->value.enumerated.item[0] = mux;
2395 if (e->shift_l != e->shift_r) {
2396 val = (reg_val >> e->shift_r) & e->mask;
2397 for (mux = 0; mux < e->max; mux++) {
2398 if (val == e->values[mux])
2399 break;
2400 }
2401 ucontrol->value.enumerated.item[1] = mux;
2402 }
2403
2404 return 0;
2405}
2406EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2407
2408/**
2409 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2410 * @kcontrol: mixer control
2411 * @ucontrol: control element information
2412 *
2413 * Callback to set the value of a double semi enumerated mixer.
2414 *
2415 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2416 * used for handling bitfield coded enumeration for example.
2417 *
2418 * Returns 0 for success.
2419 */
2420int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2421 struct snd_ctl_elem_value *ucontrol)
2422{
2423 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002424 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002425 unsigned int val;
2426 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002427
2428 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2429 return -EINVAL;
2430 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2431 mask = e->mask << e->shift_l;
2432 if (e->shift_l != e->shift_r) {
2433 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2434 return -EINVAL;
2435 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2436 mask |= e->mask << e->shift_r;
2437 }
2438
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002439 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002440}
2441EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2442
2443/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002444 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2445 * @kcontrol: mixer control
2446 * @uinfo: control element information
2447 *
2448 * Callback to provide information about an external enumerated
2449 * single mixer.
2450 *
2451 * Returns 0 for success.
2452 */
2453int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2454 struct snd_ctl_elem_info *uinfo)
2455{
2456 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2457
2458 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2459 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002460 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002461
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002462 if (uinfo->value.enumerated.item > e->max - 1)
2463 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002464 strcpy(uinfo->value.enumerated.name,
2465 e->texts[uinfo->value.enumerated.item]);
2466 return 0;
2467}
2468EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2469
2470/**
2471 * snd_soc_info_volsw_ext - external single mixer info callback
2472 * @kcontrol: mixer control
2473 * @uinfo: control element information
2474 *
2475 * Callback to provide information about a single external mixer control.
2476 *
2477 * Returns 0 for success.
2478 */
2479int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2480 struct snd_ctl_elem_info *uinfo)
2481{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002482 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002483
Mark Brownfd5dfad2009-04-15 21:37:46 +01002484 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002485 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2486 else
2487 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2488
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002489 uinfo->count = 1;
2490 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002491 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002492 return 0;
2493}
2494EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2495
2496/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002497 * snd_soc_info_volsw - single mixer info callback
2498 * @kcontrol: mixer control
2499 * @uinfo: control element information
2500 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002501 * Callback to provide information about a single mixer control, or a double
2502 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002503 *
2504 * Returns 0 for success.
2505 */
2506int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2507 struct snd_ctl_elem_info *uinfo)
2508{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002509 struct soc_mixer_control *mc =
2510 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002511 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002512
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002513 if (!mc->platform_max)
2514 mc->platform_max = mc->max;
2515 platform_max = mc->platform_max;
2516
2517 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002518 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2519 else
2520 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2521
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002522 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002523 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002524 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002525 return 0;
2526}
2527EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2528
2529/**
2530 * snd_soc_get_volsw - single mixer get callback
2531 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002532 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002533 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002534 * Callback to get the value of a single mixer control, or a double mixer
2535 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002536 *
2537 * Returns 0 for success.
2538 */
2539int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2540 struct snd_ctl_elem_value *ucontrol)
2541{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002542 struct soc_mixer_control *mc =
2543 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002544 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002545 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002546 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002547 unsigned int shift = mc->shift;
2548 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002549 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002550 unsigned int mask = (1 << fls(max)) - 1;
2551 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002552
2553 ucontrol->value.integer.value[0] =
2554 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002555 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002556 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002557 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002558
2559 if (snd_soc_volsw_is_stereo(mc)) {
2560 if (reg == reg2)
2561 ucontrol->value.integer.value[1] =
2562 (snd_soc_read(codec, reg) >> rshift) & mask;
2563 else
2564 ucontrol->value.integer.value[1] =
2565 (snd_soc_read(codec, reg2) >> shift) & mask;
2566 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002567 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002568 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002569 }
2570
2571 return 0;
2572}
2573EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2574
2575/**
2576 * snd_soc_put_volsw - single mixer put callback
2577 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002578 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002579 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002580 * Callback to set the value of a single mixer control, or a double mixer
2581 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002582 *
2583 * Returns 0 for success.
2584 */
2585int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2586 struct snd_ctl_elem_value *ucontrol)
2587{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002588 struct soc_mixer_control *mc =
2589 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002590 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002591 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002592 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002593 unsigned int shift = mc->shift;
2594 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002595 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002596 unsigned int mask = (1 << fls(max)) - 1;
2597 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002598 int err;
2599 bool type_2r = 0;
2600 unsigned int val2 = 0;
2601 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002602
2603 val = (ucontrol->value.integer.value[0] & mask);
2604 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002605 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002606 val_mask = mask << shift;
2607 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002608 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002609 val2 = (ucontrol->value.integer.value[1] & mask);
2610 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002611 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002612 if (reg == reg2) {
2613 val_mask |= mask << rshift;
2614 val |= val2 << rshift;
2615 } else {
2616 val2 = val2 << shift;
2617 type_2r = 1;
2618 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002619 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002620 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002621 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002622 return err;
2623
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002624 if (type_2r)
2625 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2626
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002627 return err;
2628}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002629EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002630
Mark Browne13ac2e2008-05-28 17:58:05 +01002631/**
Brian Austin1d99f242012-03-30 10:43:55 -05002632 * snd_soc_get_volsw_sx - single mixer get callback
2633 * @kcontrol: mixer control
2634 * @ucontrol: control element information
2635 *
2636 * Callback to get the value of a single mixer control, or a double mixer
2637 * control that spans 2 registers.
2638 *
2639 * Returns 0 for success.
2640 */
2641int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2642 struct snd_ctl_elem_value *ucontrol)
2643{
2644 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2645 struct soc_mixer_control *mc =
2646 (struct soc_mixer_control *)kcontrol->private_value;
2647
2648 unsigned int reg = mc->reg;
2649 unsigned int reg2 = mc->rreg;
2650 unsigned int shift = mc->shift;
2651 unsigned int rshift = mc->rshift;
2652 int max = mc->max;
2653 int min = mc->min;
2654 int mask = (1 << (fls(min + max) - 1)) - 1;
2655
2656 ucontrol->value.integer.value[0] =
2657 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2658
2659 if (snd_soc_volsw_is_stereo(mc))
2660 ucontrol->value.integer.value[1] =
2661 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2662
2663 return 0;
2664}
2665EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2666
2667/**
2668 * snd_soc_put_volsw_sx - double mixer set callback
2669 * @kcontrol: mixer control
2670 * @uinfo: control element information
2671 *
2672 * Callback to set the value of a double mixer control that spans 2 registers.
2673 *
2674 * Returns 0 for success.
2675 */
2676int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2677 struct snd_ctl_elem_value *ucontrol)
2678{
2679 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2680 struct soc_mixer_control *mc =
2681 (struct soc_mixer_control *)kcontrol->private_value;
2682
2683 unsigned int reg = mc->reg;
2684 unsigned int reg2 = mc->rreg;
2685 unsigned int shift = mc->shift;
2686 unsigned int rshift = mc->rshift;
2687 int max = mc->max;
2688 int min = mc->min;
2689 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002690 int err = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002691 unsigned short val, val_mask, val2 = 0;
2692
2693 val_mask = mask << shift;
2694 val = (ucontrol->value.integer.value[0] + min) & mask;
2695 val = val << shift;
2696
2697 if (snd_soc_update_bits_locked(codec, reg, val_mask, val))
2698 return err;
2699
2700 if (snd_soc_volsw_is_stereo(mc)) {
2701 val_mask = mask << rshift;
2702 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2703 val2 = val2 << rshift;
2704
2705 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2706 return err;
2707 }
2708 return 0;
2709}
2710EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2711
2712/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002713 * snd_soc_info_volsw_s8 - signed mixer info callback
2714 * @kcontrol: mixer control
2715 * @uinfo: control element information
2716 *
2717 * Callback to provide information about a signed mixer control.
2718 *
2719 * Returns 0 for success.
2720 */
2721int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2722 struct snd_ctl_elem_info *uinfo)
2723{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002724 struct soc_mixer_control *mc =
2725 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002726 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002727 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002728
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002729 if (!mc->platform_max)
2730 mc->platform_max = mc->max;
2731 platform_max = mc->platform_max;
2732
Mark Browne13ac2e2008-05-28 17:58:05 +01002733 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2734 uinfo->count = 2;
2735 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002736 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002737 return 0;
2738}
2739EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2740
2741/**
2742 * snd_soc_get_volsw_s8 - signed mixer get callback
2743 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002744 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002745 *
2746 * Callback to get the value of a signed mixer control.
2747 *
2748 * Returns 0 for success.
2749 */
2750int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2751 struct snd_ctl_elem_value *ucontrol)
2752{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002753 struct soc_mixer_control *mc =
2754 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002755 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002756 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002757 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002758 int val = snd_soc_read(codec, reg);
2759
2760 ucontrol->value.integer.value[0] =
2761 ((signed char)(val & 0xff))-min;
2762 ucontrol->value.integer.value[1] =
2763 ((signed char)((val >> 8) & 0xff))-min;
2764 return 0;
2765}
2766EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2767
2768/**
2769 * snd_soc_put_volsw_sgn - signed mixer put callback
2770 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002771 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002772 *
2773 * Callback to set the value of a signed mixer control.
2774 *
2775 * Returns 0 for success.
2776 */
2777int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2778 struct snd_ctl_elem_value *ucontrol)
2779{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002780 struct soc_mixer_control *mc =
2781 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002782 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002783 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002784 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002785 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002786
2787 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2788 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2789
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002790 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002791}
2792EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2793
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002794/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002795 * snd_soc_info_volsw_range - single mixer info callback with range.
2796 * @kcontrol: mixer control
2797 * @uinfo: control element information
2798 *
2799 * Callback to provide information, within a range, about a single
2800 * mixer control.
2801 *
2802 * returns 0 for success.
2803 */
2804int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2805 struct snd_ctl_elem_info *uinfo)
2806{
2807 struct soc_mixer_control *mc =
2808 (struct soc_mixer_control *)kcontrol->private_value;
2809 int platform_max;
2810 int min = mc->min;
2811
2812 if (!mc->platform_max)
2813 mc->platform_max = mc->max;
2814 platform_max = mc->platform_max;
2815
2816 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2817 uinfo->count = 1;
2818 uinfo->value.integer.min = 0;
2819 uinfo->value.integer.max = platform_max - min;
2820
2821 return 0;
2822}
2823EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2824
2825/**
2826 * snd_soc_put_volsw_range - single mixer put value callback with range.
2827 * @kcontrol: mixer control
2828 * @ucontrol: control element information
2829 *
2830 * Callback to set the value, within a range, for a single mixer control.
2831 *
2832 * Returns 0 for success.
2833 */
2834int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2835 struct snd_ctl_elem_value *ucontrol)
2836{
2837 struct soc_mixer_control *mc =
2838 (struct soc_mixer_control *)kcontrol->private_value;
2839 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2840 unsigned int reg = mc->reg;
2841 unsigned int shift = mc->shift;
2842 int min = mc->min;
2843 int max = mc->max;
2844 unsigned int mask = (1 << fls(max)) - 1;
2845 unsigned int invert = mc->invert;
2846 unsigned int val, val_mask;
2847
2848 val = ((ucontrol->value.integer.value[0] + min) & mask);
2849 if (invert)
2850 val = max - val;
2851 val_mask = mask << shift;
2852 val = val << shift;
2853
2854 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2855}
2856EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2857
2858/**
2859 * snd_soc_get_volsw_range - single mixer get callback with range
2860 * @kcontrol: mixer control
2861 * @ucontrol: control element information
2862 *
2863 * Callback to get the value, within a range, of a single mixer control.
2864 *
2865 * Returns 0 for success.
2866 */
2867int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2868 struct snd_ctl_elem_value *ucontrol)
2869{
2870 struct soc_mixer_control *mc =
2871 (struct soc_mixer_control *)kcontrol->private_value;
2872 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2873 unsigned int reg = mc->reg;
2874 unsigned int shift = mc->shift;
2875 int min = mc->min;
2876 int max = mc->max;
2877 unsigned int mask = (1 << fls(max)) - 1;
2878 unsigned int invert = mc->invert;
2879
2880 ucontrol->value.integer.value[0] =
2881 (snd_soc_read(codec, reg) >> shift) & mask;
2882 if (invert)
2883 ucontrol->value.integer.value[0] =
2884 max - ucontrol->value.integer.value[0];
2885 ucontrol->value.integer.value[0] =
2886 ucontrol->value.integer.value[0] - min;
2887
2888 return 0;
2889}
2890EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2891
2892/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002893 * snd_soc_limit_volume - Set new limit to an existing volume control.
2894 *
2895 * @codec: where to look for the control
2896 * @name: Name of the control
2897 * @max: new maximum limit
2898 *
2899 * Return 0 for success, else error.
2900 */
2901int snd_soc_limit_volume(struct snd_soc_codec *codec,
2902 const char *name, int max)
2903{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002904 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002905 struct snd_kcontrol *kctl;
2906 struct soc_mixer_control *mc;
2907 int found = 0;
2908 int ret = -EINVAL;
2909
2910 /* Sanity check for name and max */
2911 if (unlikely(!name || max <= 0))
2912 return -EINVAL;
2913
2914 list_for_each_entry(kctl, &card->controls, list) {
2915 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2916 found = 1;
2917 break;
2918 }
2919 }
2920 if (found) {
2921 mc = (struct soc_mixer_control *)kctl->private_value;
2922 if (max <= mc->max) {
Peter Ujfalusid11bb4a92010-05-10 14:39:24 +03002923 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002924 ret = 0;
2925 }
2926 }
2927 return ret;
2928}
2929EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2930
Mark Brown71d08512011-10-10 18:31:26 +01002931int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2932 struct snd_ctl_elem_info *uinfo)
2933{
2934 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2935 struct soc_bytes *params = (void *)kcontrol->private_value;
2936
2937 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2938 uinfo->count = params->num_regs * codec->val_bytes;
2939
2940 return 0;
2941}
2942EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
2943
2944int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
2945 struct snd_ctl_elem_value *ucontrol)
2946{
2947 struct soc_bytes *params = (void *)kcontrol->private_value;
2948 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2949 int ret;
2950
2951 if (codec->using_regmap)
2952 ret = regmap_raw_read(codec->control_data, params->base,
2953 ucontrol->value.bytes.data,
2954 params->num_regs * codec->val_bytes);
2955 else
2956 ret = -EINVAL;
2957
Mark Brownf831b052012-02-17 16:20:33 -08002958 /* Hide any masked bytes to ensure consistent data reporting */
2959 if (ret == 0 && params->mask) {
2960 switch (codec->val_bytes) {
2961 case 1:
2962 ucontrol->value.bytes.data[0] &= ~params->mask;
2963 break;
2964 case 2:
2965 ((u16 *)(&ucontrol->value.bytes.data))[0]
2966 &= ~params->mask;
2967 break;
2968 case 4:
2969 ((u32 *)(&ucontrol->value.bytes.data))[0]
2970 &= ~params->mask;
2971 break;
2972 default:
2973 return -EINVAL;
2974 }
2975 }
2976
Mark Brown71d08512011-10-10 18:31:26 +01002977 return ret;
2978}
2979EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
2980
2981int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
2982 struct snd_ctl_elem_value *ucontrol)
2983{
2984 struct soc_bytes *params = (void *)kcontrol->private_value;
2985 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08002986 int ret, len;
2987 unsigned int val;
2988 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01002989
Mark Brownf831b052012-02-17 16:20:33 -08002990 if (!codec->using_regmap)
2991 return -EINVAL;
2992
2993 data = ucontrol->value.bytes.data;
2994 len = params->num_regs * codec->val_bytes;
2995
2996 /*
2997 * If we've got a mask then we need to preserve the register
2998 * bits. We shouldn't modify the incoming data so take a
2999 * copy.
3000 */
3001 if (params->mask) {
3002 ret = regmap_read(codec->control_data, params->base, &val);
3003 if (ret != 0)
3004 return ret;
3005
3006 val &= params->mask;
3007
3008 data = kmemdup(data, len, GFP_KERNEL);
3009 if (!data)
3010 return -ENOMEM;
3011
3012 switch (codec->val_bytes) {
3013 case 1:
3014 ((u8 *)data)[0] &= ~params->mask;
3015 ((u8 *)data)[0] |= val;
3016 break;
3017 case 2:
3018 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3019 ((u16 *)data)[0] |= cpu_to_be16(val);
3020 break;
3021 case 4:
3022 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3023 ((u32 *)data)[0] |= cpu_to_be32(val);
3024 break;
3025 default:
3026 return -EINVAL;
3027 }
3028 }
3029
3030 ret = regmap_raw_write(codec->control_data, params->base,
3031 data, len);
3032
3033 if (params->mask)
3034 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003035
3036 return ret;
3037}
3038EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3039
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003040/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003041 * snd_soc_info_xr_sx - signed multi register info callback
3042 * @kcontrol: mreg control
3043 * @uinfo: control element information
3044 *
3045 * Callback to provide information of a control that can
3046 * span multiple codec registers which together
3047 * forms a single signed value in a MSB/LSB manner.
3048 *
3049 * Returns 0 for success.
3050 */
3051int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3052 struct snd_ctl_elem_info *uinfo)
3053{
3054 struct soc_mreg_control *mc =
3055 (struct soc_mreg_control *)kcontrol->private_value;
3056 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3057 uinfo->count = 1;
3058 uinfo->value.integer.min = mc->min;
3059 uinfo->value.integer.max = mc->max;
3060
3061 return 0;
3062}
3063EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3064
3065/**
3066 * snd_soc_get_xr_sx - signed multi register get callback
3067 * @kcontrol: mreg control
3068 * @ucontrol: control element information
3069 *
3070 * Callback to get the value of a control that can span
3071 * multiple codec registers which together forms a single
3072 * signed value in a MSB/LSB manner. The control supports
3073 * specifying total no of bits used to allow for bitfields
3074 * across the multiple codec registers.
3075 *
3076 * Returns 0 for success.
3077 */
3078int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3079 struct snd_ctl_elem_value *ucontrol)
3080{
3081 struct soc_mreg_control *mc =
3082 (struct soc_mreg_control *)kcontrol->private_value;
3083 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3084 unsigned int regbase = mc->regbase;
3085 unsigned int regcount = mc->regcount;
3086 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3087 unsigned int regwmask = (1<<regwshift)-1;
3088 unsigned int invert = mc->invert;
3089 unsigned long mask = (1UL<<mc->nbits)-1;
3090 long min = mc->min;
3091 long max = mc->max;
3092 long val = 0;
3093 unsigned long regval;
3094 unsigned int i;
3095
3096 for (i = 0; i < regcount; i++) {
3097 regval = snd_soc_read(codec, regbase+i) & regwmask;
3098 val |= regval << (regwshift*(regcount-i-1));
3099 }
3100 val &= mask;
3101 if (min < 0 && val > max)
3102 val |= ~mask;
3103 if (invert)
3104 val = max - val;
3105 ucontrol->value.integer.value[0] = val;
3106
3107 return 0;
3108}
3109EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3110
3111/**
3112 * snd_soc_put_xr_sx - signed multi register get callback
3113 * @kcontrol: mreg control
3114 * @ucontrol: control element information
3115 *
3116 * Callback to set the value of a control that can span
3117 * multiple codec registers which together forms a single
3118 * signed value in a MSB/LSB manner. The control supports
3119 * specifying total no of bits used to allow for bitfields
3120 * across the multiple codec registers.
3121 *
3122 * Returns 0 for success.
3123 */
3124int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3125 struct snd_ctl_elem_value *ucontrol)
3126{
3127 struct soc_mreg_control *mc =
3128 (struct soc_mreg_control *)kcontrol->private_value;
3129 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3130 unsigned int regbase = mc->regbase;
3131 unsigned int regcount = mc->regcount;
3132 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3133 unsigned int regwmask = (1<<regwshift)-1;
3134 unsigned int invert = mc->invert;
3135 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003136 long max = mc->max;
3137 long val = ucontrol->value.integer.value[0];
3138 unsigned int i, regval, regmask;
3139 int err;
3140
3141 if (invert)
3142 val = max - val;
3143 val &= mask;
3144 for (i = 0; i < regcount; i++) {
3145 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3146 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3147 err = snd_soc_update_bits_locked(codec, regbase+i,
3148 regmask, regval);
3149 if (err < 0)
3150 return err;
3151 }
3152
3153 return 0;
3154}
3155EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3156
3157/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003158 * snd_soc_get_strobe - strobe get callback
3159 * @kcontrol: mixer control
3160 * @ucontrol: control element information
3161 *
3162 * Callback get the value of a strobe mixer control.
3163 *
3164 * Returns 0 for success.
3165 */
3166int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3167 struct snd_ctl_elem_value *ucontrol)
3168{
3169 struct soc_mixer_control *mc =
3170 (struct soc_mixer_control *)kcontrol->private_value;
3171 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3172 unsigned int reg = mc->reg;
3173 unsigned int shift = mc->shift;
3174 unsigned int mask = 1 << shift;
3175 unsigned int invert = mc->invert != 0;
3176 unsigned int val = snd_soc_read(codec, reg) & mask;
3177
3178 if (shift != 0 && val != 0)
3179 val = val >> shift;
3180 ucontrol->value.enumerated.item[0] = val ^ invert;
3181
3182 return 0;
3183}
3184EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3185
3186/**
3187 * snd_soc_put_strobe - strobe put callback
3188 * @kcontrol: mixer control
3189 * @ucontrol: control element information
3190 *
3191 * Callback strobe a register bit to high then low (or the inverse)
3192 * in one pass of a single mixer enum control.
3193 *
3194 * Returns 1 for success.
3195 */
3196int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3197 struct snd_ctl_elem_value *ucontrol)
3198{
3199 struct soc_mixer_control *mc =
3200 (struct soc_mixer_control *)kcontrol->private_value;
3201 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3202 unsigned int reg = mc->reg;
3203 unsigned int shift = mc->shift;
3204 unsigned int mask = 1 << shift;
3205 unsigned int invert = mc->invert != 0;
3206 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3207 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3208 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3209 int err;
3210
3211 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3212 if (err < 0)
3213 return err;
3214
3215 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3216 return err;
3217}
3218EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3219
3220/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003221 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3222 * @dai: DAI
3223 * @clk_id: DAI specific clock ID
3224 * @freq: new clock frequency in Hz
3225 * @dir: new clock direction - input/output.
3226 *
3227 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3228 */
3229int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3230 unsigned int freq, int dir)
3231{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003232 if (dai->driver && dai->driver->ops->set_sysclk)
3233 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003234 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003235 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003236 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003237 else
3238 return -EINVAL;
3239}
3240EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3241
3242/**
Mark Brownec4ee522011-03-07 20:58:11 +00003243 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3244 * @codec: CODEC
3245 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003246 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003247 * @freq: new clock frequency in Hz
3248 * @dir: new clock direction - input/output.
3249 *
3250 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3251 */
3252int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003253 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003254{
3255 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003256 return codec->driver->set_sysclk(codec, clk_id, source,
3257 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003258 else
3259 return -EINVAL;
3260}
3261EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3262
3263/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003264 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3265 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003266 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003267 * @div: new clock divisor.
3268 *
3269 * Configures the clock dividers. This is used to derive the best DAI bit and
3270 * frame clocks from the system or master clock. It's best to set the DAI bit
3271 * and frame clocks as low as possible to save system power.
3272 */
3273int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3274 int div_id, int div)
3275{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003276 if (dai->driver && dai->driver->ops->set_clkdiv)
3277 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003278 else
3279 return -EINVAL;
3280}
3281EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3282
3283/**
3284 * snd_soc_dai_set_pll - configure DAI PLL.
3285 * @dai: DAI
3286 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003287 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003288 * @freq_in: PLL input clock frequency in Hz
3289 * @freq_out: requested PLL output clock frequency in Hz
3290 *
3291 * Configures and enables PLL to generate output clock based on input clock.
3292 */
Mark Brown85488032009-09-05 18:52:16 +01003293int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3294 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003295{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003296 if (dai->driver && dai->driver->ops->set_pll)
3297 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003298 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003299 else if (dai->codec && dai->codec->driver->set_pll)
3300 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3301 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003302 else
3303 return -EINVAL;
3304}
3305EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3306
Mark Brownec4ee522011-03-07 20:58:11 +00003307/*
3308 * snd_soc_codec_set_pll - configure codec PLL.
3309 * @codec: CODEC
3310 * @pll_id: DAI specific PLL ID
3311 * @source: DAI specific source for the PLL
3312 * @freq_in: PLL input clock frequency in Hz
3313 * @freq_out: requested PLL output clock frequency in Hz
3314 *
3315 * Configures and enables PLL to generate output clock based on input clock.
3316 */
3317int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3318 unsigned int freq_in, unsigned int freq_out)
3319{
3320 if (codec->driver->set_pll)
3321 return codec->driver->set_pll(codec, pll_id, source,
3322 freq_in, freq_out);
3323 else
3324 return -EINVAL;
3325}
3326EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3327
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003328/**
3329 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3330 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003331 * @fmt: SND_SOC_DAIFMT_ format value.
3332 *
3333 * Configures the DAI hardware format and clocking.
3334 */
3335int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3336{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003337 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003338 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003339 if (dai->driver->ops->set_fmt == NULL)
3340 return -ENOTSUPP;
3341 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003342}
3343EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3344
3345/**
3346 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3347 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003348 * @tx_mask: bitmask representing active TX slots.
3349 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003350 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003351 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003352 *
3353 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3354 * specific.
3355 */
3356int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003357 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003358{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003359 if (dai->driver && dai->driver->ops->set_tdm_slot)
3360 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003361 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003362 else
3363 return -EINVAL;
3364}
3365EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3366
3367/**
Barry Song472df3c2009-09-12 01:16:29 +08003368 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3369 * @dai: DAI
3370 * @tx_num: how many TX channels
3371 * @tx_slot: pointer to an array which imply the TX slot number channel
3372 * 0~num-1 uses
3373 * @rx_num: how many RX channels
3374 * @rx_slot: pointer to an array which imply the RX slot number channel
3375 * 0~num-1 uses
3376 *
3377 * configure the relationship between channel number and TDM slot number.
3378 */
3379int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3380 unsigned int tx_num, unsigned int *tx_slot,
3381 unsigned int rx_num, unsigned int *rx_slot)
3382{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003383 if (dai->driver && dai->driver->ops->set_channel_map)
3384 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003385 rx_num, rx_slot);
3386 else
3387 return -EINVAL;
3388}
3389EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3390
3391/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003392 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3393 * @dai: DAI
3394 * @tristate: tristate enable
3395 *
3396 * Tristates the DAI so that others can use it.
3397 */
3398int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3399{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003400 if (dai->driver && dai->driver->ops->set_tristate)
3401 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003402 else
3403 return -EINVAL;
3404}
3405EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3406
3407/**
3408 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3409 * @dai: DAI
3410 * @mute: mute enable
3411 *
3412 * Mutes the DAI DAC.
3413 */
3414int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3415{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003416 if (dai->driver && dai->driver->ops->digital_mute)
3417 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003418 else
Mark Brown04570c62012-04-13 19:16:03 +01003419 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003420}
3421EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3422
Mark Brownc5af3a22008-11-28 13:29:45 +00003423/**
3424 * snd_soc_register_card - Register a card with the ASoC core
3425 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003426 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003427 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003428 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303429int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003430{
Mark Brownb19e6e72012-03-14 21:18:39 +00003431 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003432
Mark Brownc5af3a22008-11-28 13:29:45 +00003433 if (!card->name || !card->dev)
3434 return -EINVAL;
3435
Stephen Warren5a504962011-12-21 10:40:59 -07003436 for (i = 0; i < card->num_links; i++) {
3437 struct snd_soc_dai_link *link = &card->dai_link[i];
3438
3439 /*
3440 * Codec must be specified by 1 of name or OF node,
3441 * not both or neither.
3442 */
3443 if (!!link->codec_name == !!link->codec_of_node) {
3444 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003445 "Neither/both codec name/of_node are set for %s\n",
3446 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003447 return -EINVAL;
3448 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003449 /* Codec DAI name must be specified */
3450 if (!link->codec_dai_name) {
3451 dev_err(card->dev, "codec_dai_name not set for %s\n",
3452 link->name);
3453 return -EINVAL;
3454 }
Stephen Warren5a504962011-12-21 10:40:59 -07003455
3456 /*
3457 * Platform may be specified by either name or OF node, but
3458 * can be left unspecified, and a dummy platform will be used.
3459 */
3460 if (link->platform_name && link->platform_of_node) {
3461 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003462 "Both platform name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003463 return -EINVAL;
3464 }
3465
3466 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003467 * CPU device may be specified by either name or OF node, but
3468 * can be left unspecified, and will be matched based on DAI
3469 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003470 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003471 if (link->cpu_name && link->cpu_of_node) {
Stephen Warren5a504962011-12-21 10:40:59 -07003472 dev_err(card->dev,
Stephen Warrenbc926572012-05-25 18:22:11 -06003473 "Neither/both cpu name/of_node are set for %s\n",
3474 link->name);
3475 return -EINVAL;
3476 }
3477 /*
3478 * At least one of CPU DAI name or CPU device name/node must be
3479 * specified
3480 */
3481 if (!link->cpu_dai_name &&
3482 !(link->cpu_name || link->cpu_of_node)) {
3483 dev_err(card->dev,
3484 "Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003485 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003486 return -EINVAL;
3487 }
3488 }
3489
Mark Browned77cc12011-05-03 18:25:34 +01003490 dev_set_drvdata(card->dev, card);
3491
Stephen Warren111c6412011-01-28 14:26:35 -07003492 snd_soc_initialize_card_lists(card);
3493
Vinod Koul150dd2f2011-01-13 22:48:32 +05303494 soc_init_card_debugfs(card);
3495
Mark Brown181a6892012-03-14 20:18:49 +00003496 card->rtd = devm_kzalloc(card->dev,
3497 sizeof(struct snd_soc_pcm_runtime) *
3498 (card->num_links + card->num_aux_devs),
3499 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003500 if (card->rtd == NULL)
3501 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003502 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003503 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003504
3505 for (i = 0; i < card->num_links; i++)
3506 card->rtd[i].dai_link = &card->dai_link[i];
3507
Mark Brownc5af3a22008-11-28 13:29:45 +00003508 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003509 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003510 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003511 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003512 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003513
Mark Brownb19e6e72012-03-14 21:18:39 +00003514 ret = snd_soc_instantiate_card(card);
3515 if (ret != 0)
3516 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003517
Mark Brownb19e6e72012-03-14 21:18:39 +00003518 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003519}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303520EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003521
3522/**
3523 * snd_soc_unregister_card - Unregister a card with the ASoC core
3524 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003525 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003526 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003527 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303528int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003529{
Vinod Koulb0e26482011-01-13 22:48:02 +05303530 if (card->instantiated)
3531 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003532 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3533
3534 return 0;
3535}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303536EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003537
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003538/*
3539 * Simplify DAI link configuration by removing ".-1" from device names
3540 * and sanitizing names.
3541 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003542static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003543{
3544 char *found, name[NAME_SIZE];
3545 int id1, id2;
3546
3547 if (dev_name(dev) == NULL)
3548 return NULL;
3549
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003550 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003551
3552 /* are we a "%s.%d" name (platform and SPI components) */
3553 found = strstr(name, dev->driver->name);
3554 if (found) {
3555 /* get ID */
3556 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3557
3558 /* discard ID from name if ID == -1 */
3559 if (*id == -1)
3560 found[strlen(dev->driver->name)] = '\0';
3561 }
3562
3563 } else {
3564 /* I2C component devices are named "bus-addr" */
3565 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3566 char tmp[NAME_SIZE];
3567
3568 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003569 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003570
3571 /* sanitize component name for DAI link creation */
3572 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003573 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003574 } else
3575 *id = 0;
3576 }
3577
3578 return kstrdup(name, GFP_KERNEL);
3579}
3580
3581/*
3582 * Simplify DAI link naming for single devices with multiple DAIs by removing
3583 * any ".-1" and using the DAI name (instead of device name).
3584 */
3585static inline char *fmt_multiple_name(struct device *dev,
3586 struct snd_soc_dai_driver *dai_drv)
3587{
3588 if (dai_drv->name == NULL) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02003589 pr_err("asoc: error - multiple DAI %s registered with no name\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003590 dev_name(dev));
3591 return NULL;
3592 }
3593
3594 return kstrdup(dai_drv->name, GFP_KERNEL);
3595}
3596
Mark Brown91151712008-11-30 23:31:24 +00003597/**
3598 * snd_soc_register_dai - Register a DAI with the ASoC core
3599 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003600 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003601 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003602int snd_soc_register_dai(struct device *dev,
3603 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003604{
Mark Brown054880f2012-04-09 17:29:19 +01003605 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003606 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003607
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003608 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003609
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003610 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3611 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003612 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003613
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003614 /* create DAI component name */
3615 dai->name = fmt_single_name(dev, &dai->id);
3616 if (dai->name == NULL) {
3617 kfree(dai);
3618 return -ENOMEM;
3619 }
3620
3621 dai->dev = dev;
3622 dai->driver = dai_drv;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003623 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003624 if (!dai->driver->ops)
3625 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003626
3627 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003628
3629 list_for_each_entry(codec, &codec_list, list) {
3630 if (codec->dev == dev) {
3631 dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3632 dai->name, codec->name);
3633 dai->codec = codec;
3634 break;
3635 }
3636 }
3637
Mark Brown91151712008-11-30 23:31:24 +00003638 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003639
Mark Brown91151712008-11-30 23:31:24 +00003640 mutex_unlock(&client_mutex);
3641
3642 pr_debug("Registered DAI '%s'\n", dai->name);
3643
3644 return 0;
3645}
3646EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3647
3648/**
3649 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3650 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003651 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003652 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003653void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003654{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003655 struct snd_soc_dai *dai;
3656
3657 list_for_each_entry(dai, &dai_list, list) {
3658 if (dev == dai->dev)
3659 goto found;
3660 }
3661 return;
3662
3663found:
Mark Brown91151712008-11-30 23:31:24 +00003664 mutex_lock(&client_mutex);
3665 list_del(&dai->list);
3666 mutex_unlock(&client_mutex);
3667
3668 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003669 kfree(dai->name);
3670 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003671}
3672EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3673
3674/**
3675 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3676 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003677 * @dai: Array of DAIs to register
3678 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003679 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003680int snd_soc_register_dais(struct device *dev,
3681 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003682{
Mark Brown054880f2012-04-09 17:29:19 +01003683 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003684 struct snd_soc_dai *dai;
3685 int i, ret = 0;
3686
Liam Girdwood720ffa42010-08-18 00:30:30 +01003687 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003688
3689 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003690
3691 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003692 if (dai == NULL) {
3693 ret = -ENOMEM;
3694 goto err;
3695 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003696
3697 /* create DAI component name */
3698 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3699 if (dai->name == NULL) {
3700 kfree(dai);
3701 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003702 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003703 }
3704
3705 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003706 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003707 if (dai->driver->id)
3708 dai->id = dai->driver->id;
3709 else
3710 dai->id = i;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003711 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003712 if (!dai->driver->ops)
3713 dai->driver->ops = &null_dai_ops;
3714
3715 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003716
3717 list_for_each_entry(codec, &codec_list, list) {
3718 if (codec->dev == dev) {
3719 dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3720 dai->name, codec->name);
3721 dai->codec = codec;
3722 break;
3723 }
3724 }
3725
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003726 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003727
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003728 mutex_unlock(&client_mutex);
3729
3730 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003731 }
3732
3733 return 0;
3734
3735err:
3736 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003737 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003738
3739 return ret;
3740}
3741EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3742
3743/**
3744 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3745 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003746 * @dai: Array of DAIs to unregister
3747 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003748 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003749void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003750{
3751 int i;
3752
3753 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003754 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003755}
3756EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3757
Mark Brown12a48a8c2008-12-03 19:40:30 +00003758/**
3759 * snd_soc_register_platform - Register a platform with the ASoC core
3760 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003761 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003762 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003763int snd_soc_register_platform(struct device *dev,
3764 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003765{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003766 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003767
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003768 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3769
3770 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3771 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003772 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003773
3774 /* create platform component name */
3775 platform->name = fmt_single_name(dev, &platform->id);
3776 if (platform->name == NULL) {
3777 kfree(platform);
3778 return -ENOMEM;
3779 }
3780
3781 platform->dev = dev;
3782 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003783 platform->dapm.dev = dev;
3784 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003785 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00003786 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003787
3788 mutex_lock(&client_mutex);
3789 list_add(&platform->list, &platform_list);
3790 mutex_unlock(&client_mutex);
3791
3792 pr_debug("Registered platform '%s'\n", platform->name);
3793
3794 return 0;
3795}
3796EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3797
3798/**
3799 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3800 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003801 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003802 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003803void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003804{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003805 struct snd_soc_platform *platform;
3806
3807 list_for_each_entry(platform, &platform_list, list) {
3808 if (dev == platform->dev)
3809 goto found;
3810 }
3811 return;
3812
3813found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003814 mutex_lock(&client_mutex);
3815 list_del(&platform->list);
3816 mutex_unlock(&client_mutex);
3817
3818 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003819 kfree(platform->name);
3820 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003821}
3822EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3823
Mark Brown151ab222009-05-09 16:22:58 +01003824static u64 codec_format_map[] = {
3825 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3826 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3827 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3828 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3829 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3830 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3831 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3832 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3833 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3834 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3835 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3836 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3837 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3838 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3839 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3840 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3841};
3842
3843/* Fix up the DAI formats for endianness: codecs don't actually see
3844 * the endianness of the data but we're using the CPU format
3845 * definitions which do need to include endianness so we ensure that
3846 * codec DAIs always have both big and little endian variants set.
3847 */
3848static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3849{
3850 int i;
3851
3852 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3853 if (stream->formats & codec_format_map[i])
3854 stream->formats |= codec_format_map[i];
3855}
3856
Mark Brown0d0cf002008-12-10 14:32:45 +00003857/**
3858 * snd_soc_register_codec - Register a codec with the ASoC core
3859 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003860 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003861 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003862int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003863 const struct snd_soc_codec_driver *codec_drv,
3864 struct snd_soc_dai_driver *dai_drv,
3865 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003866{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003867 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003868 struct snd_soc_codec *codec;
3869 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003870
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003871 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003872
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003873 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3874 if (codec == NULL)
3875 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003876
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003877 /* create CODEC component name */
3878 codec->name = fmt_single_name(dev, &codec->id);
3879 if (codec->name == NULL) {
3880 kfree(codec);
3881 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003882 }
3883
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003884 if (codec_drv->compress_type)
3885 codec->compress_type = codec_drv->compress_type;
3886 else
3887 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3888
Mark Brownc3acec22010-12-02 16:15:29 +00003889 codec->write = codec_drv->write;
3890 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003891 codec->volatile_register = codec_drv->volatile_register;
3892 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003893 codec->writable_register = codec_drv->writable_register;
Mark Brown5124e692012-02-08 13:20:50 +00003894 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003895 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3896 codec->dapm.dev = dev;
3897 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003898 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003899 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003900 codec->dev = dev;
3901 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003902 codec->num_dai = num_dai;
3903 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003904
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003905 /* allocate CODEC register cache */
3906 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003907 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003908 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003909 /* it is necessary to make a copy of the default register cache
3910 * because in the case of using a compression type that requires
3911 * the default register cache to be marked as __devinitconst the
3912 * kernel might have freed the array by the time we initialize
3913 * the cache.
3914 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003915 if (codec_drv->reg_cache_default) {
3916 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3917 reg_size, GFP_KERNEL);
3918 if (!codec->reg_def_copy) {
3919 ret = -ENOMEM;
3920 goto fail;
3921 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003922 }
3923 }
3924
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003925 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3926 if (!codec->volatile_register)
3927 codec->volatile_register = snd_soc_default_volatile_register;
3928 if (!codec->readable_register)
3929 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003930 if (!codec->writable_register)
3931 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003932 }
3933
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003934 for (i = 0; i < num_dai; i++) {
3935 fixup_codec_formats(&dai_drv[i].playback);
3936 fixup_codec_formats(&dai_drv[i].capture);
3937 }
3938
Mark Brown054880f2012-04-09 17:29:19 +01003939 mutex_lock(&client_mutex);
3940 list_add(&codec->list, &codec_list);
3941 mutex_unlock(&client_mutex);
3942
Mark Brown26b01cc2010-08-18 20:20:55 +01003943 /* register any DAIs */
3944 if (num_dai) {
3945 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3946 if (ret < 0)
Mark Brown054880f2012-04-09 17:29:19 +01003947 dev_err(codec->dev, "Failed to regster DAIs: %d\n",
3948 ret);
Mark Brown26b01cc2010-08-18 20:20:55 +01003949 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003950
Mark Brown0d0cf002008-12-10 14:32:45 +00003951 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003952 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003953
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003954fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003955 kfree(codec->reg_def_copy);
3956 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003957 kfree(codec->name);
3958 kfree(codec);
3959 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003960}
3961EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3962
3963/**
3964 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3965 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003966 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003967 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003968void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003969{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003970 struct snd_soc_codec *codec;
3971 int i;
3972
3973 list_for_each_entry(codec, &codec_list, list) {
3974 if (dev == codec->dev)
3975 goto found;
3976 }
3977 return;
3978
3979found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003980 if (codec->num_dai)
3981 for (i = 0; i < codec->num_dai; i++)
3982 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003983
Mark Brown0d0cf002008-12-10 14:32:45 +00003984 mutex_lock(&client_mutex);
3985 list_del(&codec->list);
3986 mutex_unlock(&client_mutex);
3987
3988 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003989
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003990 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003991 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003992 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003993 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003994}
3995EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3996
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003997/* Retrieve a card's name from device tree */
3998int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3999 const char *propname)
4000{
4001 struct device_node *np = card->dev->of_node;
4002 int ret;
4003
4004 ret = of_property_read_string_index(np, propname, 0, &card->name);
4005 /*
4006 * EINVAL means the property does not exist. This is fine providing
4007 * card->name was previously set, which is checked later in
4008 * snd_soc_register_card.
4009 */
4010 if (ret < 0 && ret != -EINVAL) {
4011 dev_err(card->dev,
4012 "Property '%s' could not be read: %d\n",
4013 propname, ret);
4014 return ret;
4015 }
4016
4017 return 0;
4018}
4019EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4020
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004021int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4022 const char *propname)
4023{
4024 struct device_node *np = card->dev->of_node;
4025 int num_routes;
4026 struct snd_soc_dapm_route *routes;
4027 int i, ret;
4028
4029 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004030 if (num_routes < 0 || num_routes & 1) {
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004031 dev_err(card->dev,
Richard Zhaoc34ce322012-04-24 15:24:43 +08004032 "Property '%s' does not exist or its length is not even\n",
4033 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004034 return -EINVAL;
4035 }
4036 num_routes /= 2;
4037 if (!num_routes) {
4038 dev_err(card->dev,
4039 "Property '%s's length is zero\n",
4040 propname);
4041 return -EINVAL;
4042 }
4043
4044 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4045 GFP_KERNEL);
4046 if (!routes) {
4047 dev_err(card->dev,
4048 "Could not allocate DAPM route table\n");
4049 return -EINVAL;
4050 }
4051
4052 for (i = 0; i < num_routes; i++) {
4053 ret = of_property_read_string_index(np, propname,
4054 2 * i, &routes[i].sink);
4055 if (ret) {
4056 dev_err(card->dev,
4057 "Property '%s' index %d could not be read: %d\n",
4058 propname, 2 * i, ret);
4059 return -EINVAL;
4060 }
4061 ret = of_property_read_string_index(np, propname,
4062 (2 * i) + 1, &routes[i].source);
4063 if (ret) {
4064 dev_err(card->dev,
4065 "Property '%s' index %d could not be read: %d\n",
4066 propname, (2 * i) + 1, ret);
4067 return -EINVAL;
4068 }
4069 }
4070
4071 card->num_dapm_routes = num_routes;
4072 card->dapm_routes = routes;
4073
4074 return 0;
4075}
4076EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4077
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004078static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004079{
Mark Brown384c89e2008-12-03 17:34:03 +00004080#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004081 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4082 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004083 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004084 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004085 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004086
Mark Brown8a9dab12011-01-10 22:25:21 +00004087 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004088 &codec_list_fops))
4089 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4090
Mark Brown8a9dab12011-01-10 22:25:21 +00004091 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004092 &dai_list_fops))
4093 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004094
Mark Brown8a9dab12011-01-10 22:25:21 +00004095 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004096 &platform_list_fops))
4097 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004098#endif
4099
Mark Brownfb257892011-04-28 10:57:54 +01004100 snd_soc_util_init();
4101
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004102 return platform_driver_register(&soc_driver);
4103}
Mark Brown4abe8e12010-10-12 17:41:03 +01004104module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004105
Mark Brown7d8c16a2008-11-30 22:11:24 +00004106static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004107{
Mark Brownfb257892011-04-28 10:57:54 +01004108 snd_soc_util_exit();
4109
Mark Brown384c89e2008-12-03 17:34:03 +00004110#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004111 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004112#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004113 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004114}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004115module_exit(snd_soc_exit);
4116
4117/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004118MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004119MODULE_DESCRIPTION("ALSA SoC Core");
4120MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004121MODULE_ALIAS("platform:soc-audio");