blob: 621c5bdea483b1afc30314c0b28e34b524739c65 [file] [log] [blame]
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
Liam Girdwood0664d882006-12-18 14:39:02 +01005 * Copyright 2005 Openedhand Ltd.
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00006 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
Liam Girdwood0664d882006-12-18 14:39:02 +01008 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01009 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood0664d882006-12-18 14:39:02 +010010 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020012 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
Frank Mandarinodb2a4162006-10-06 18:31:09 +020018 * TODO:
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
23 */
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/init.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
30#include <linux/bitops.h>
Troy Kisky12ef1932008-10-13 17:42:14 -070031#include <linux/debugfs.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020032#include <linux/platform_device.h>
Mark Brownf0e8ed82011-09-20 11:41:54 +010033#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070035#include <linux/of.h>
Marek Vasut474828a2009-07-22 13:01:03 +020036#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020037#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000038#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020039#include <sound/pcm.h>
40#include <sound/pcm_params.h>
41#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010042#include <sound/soc-dpcm.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020043#include <sound/initval.h>
44
Mark Browna8b1d342010-11-03 18:05:58 -040045#define CREATE_TRACE_POINTS
46#include <trace/events/asoc.h>
47
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000048#define NAME_SIZE 32
49
Frank Mandarinodb2a4162006-10-06 18:31:09 +020050static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
51
Mark Brown384c89e2008-12-03 17:34:03 +000052#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000053struct dentry *snd_soc_debugfs_root;
54EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000055#endif
56
Mark Brownc5af3a22008-11-28 13:29:45 +000057static DEFINE_MUTEX(client_mutex);
Mark Brown91151712008-11-30 23:31:24 +000058static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000059static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000060static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000061
Frank Mandarinodb2a4162006-10-06 18:31:09 +020062/*
63 * This is a timeout to do a DAPM powerdown after a stream is closed().
64 * It can be used to eliminate pops between different playback streams, e.g.
65 * between two audio tracks.
66 */
67static int pmdown_time = 5000;
68module_param(pmdown_time, int, 0);
69MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
70
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000071/* returns the minimum number of bytes needed to represent
72 * a particular given value */
73static int min_bytes_needed(unsigned long val)
74{
75 int c = 0;
76 int i;
77
78 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
79 if (val & (1UL << i))
80 break;
81 c = (sizeof val * 8) - c;
82 if (!c || (c % 8))
83 c = (c + 8) / 8;
84 else
85 c /= 8;
86 return c;
87}
88
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000089/* fill buf which is 'len' bytes with a formatted
90 * string of the form 'reg: value\n' */
91static int format_register_str(struct snd_soc_codec *codec,
92 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +000093{
Stephen Warren00b317a2011-04-01 14:50:44 -060094 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
95 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000096 int ret;
97 char tmpbuf[len + 1];
98 char regbuf[regsize + 1];
99
100 /* since tmpbuf is allocated on the stack, warn the callers if they
101 * try to abuse this function */
102 WARN_ON(len > 63);
103
104 /* +2 for ': ' and + 1 for '\n' */
105 if (wordsize + regsize + 2 + 1 != len)
106 return -EINVAL;
107
Mark Brown25032c12011-08-01 13:52:48 +0900108 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000109 if (ret < 0) {
110 memset(regbuf, 'X', regsize);
111 regbuf[regsize] = '\0';
112 } else {
113 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
114 }
115
116 /* prepare the buffer */
117 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
118 /* copy it back to the caller without the '\0' */
119 memcpy(buf, tmpbuf, len);
120
121 return 0;
122}
123
124/* codec register dump */
125static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
126 size_t count, loff_t pos)
127{
128 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000129 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000130 int len;
131 size_t total = 0;
132 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000133
Stephen Warren00b317a2011-04-01 14:50:44 -0600134 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
135 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000136
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000137 len = wordsize + regsize + 2 + 1;
138
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000139 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000140 return 0;
141
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000142 if (codec->driver->reg_cache_step)
143 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000144
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000145 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausenb92d1502011-08-27 18:24:14 +0200146 if (!snd_soc_codec_readable_register(codec, i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000147 continue;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000148 if (codec->driver->display_register) {
149 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000150 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100151 } else {
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000152 /* only support larger than PAGE_SIZE bytes debugfs
153 * entries for the default case */
154 if (p >= pos) {
155 if (total + len >= count - 1)
156 break;
157 format_register_str(codec, i, buf + total, len);
158 total += len;
159 }
160 p += len;
Mark Brown5164d742010-07-14 16:14:33 +0100161 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000162 }
163
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000164 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000165
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000166 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000167}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000168
Mark Brown2624d5f2009-11-03 21:56:13 +0000169static ssize_t codec_reg_show(struct device *dev,
170 struct device_attribute *attr, char *buf)
171{
Mark Brown36ae1a92012-01-06 17:12:45 -0800172 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000173
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000174 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000175}
176
177static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
178
Mark Browndbe21402010-02-12 11:37:24 +0000179static ssize_t pmdown_time_show(struct device *dev,
180 struct device_attribute *attr, char *buf)
181{
Mark Brown36ae1a92012-01-06 17:12:45 -0800182 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000183
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000184 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000185}
186
187static ssize_t pmdown_time_set(struct device *dev,
188 struct device_attribute *attr,
189 const char *buf, size_t count)
190{
Mark Brown36ae1a92012-01-06 17:12:45 -0800191 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700192 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000193
Mark Brownc593b522010-10-27 20:11:17 -0700194 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
195 if (ret)
196 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000197
198 return count;
199}
200
201static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
202
Mark Brown2624d5f2009-11-03 21:56:13 +0000203#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000204static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000205 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000206{
207 ssize_t ret;
208 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000209 char *buf;
210
211 if (*ppos < 0 || !count)
212 return -EINVAL;
213
214 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000215 if (!buf)
216 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000217
218 ret = soc_codec_reg_show(codec, buf, count, *ppos);
219 if (ret >= 0) {
220 if (copy_to_user(user_buf, buf, ret)) {
221 kfree(buf);
222 return -EFAULT;
223 }
224 *ppos += ret;
225 }
226
Mark Brown2624d5f2009-11-03 21:56:13 +0000227 kfree(buf);
228 return ret;
229}
230
231static ssize_t codec_reg_write_file(struct file *file,
232 const char __user *user_buf, size_t count, loff_t *ppos)
233{
234 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700235 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000236 char *start = buf;
237 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000238 struct snd_soc_codec *codec = file->private_data;
239
240 buf_size = min(count, (sizeof(buf)-1));
241 if (copy_from_user(buf, user_buf, buf_size))
242 return -EFAULT;
243 buf[buf_size] = 0;
244
Mark Brown2624d5f2009-11-03 21:56:13 +0000245 while (*start == ' ')
246 start++;
247 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000248 while (*start == ' ')
249 start++;
250 if (strict_strtoul(start, 16, &value))
251 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000252
253 /* Userspace has been fiddling around behind the kernel's back */
254 add_taint(TAINT_USER);
255
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000256 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000257 return buf_size;
258}
259
260static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700261 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000262 .read = codec_reg_read_file,
263 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200264 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000265};
266
267static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
268{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200269 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
270
271 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
272 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000273 if (!codec->debugfs_codec_root) {
Liam Girdwood64e60f92012-02-15 15:15:37 +0000274 dev_warn(codec->dev, "Failed to create codec debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000275 return;
276 }
277
Mark Brownaaee8ef2011-01-26 20:53:50 +0000278 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
279 &codec->cache_sync);
280 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
281 &codec->cache_only);
282
Mark Brown2624d5f2009-11-03 21:56:13 +0000283 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
284 codec->debugfs_codec_root,
285 codec, &codec_reg_fops);
286 if (!codec->debugfs_reg)
Liam Girdwood64e60f92012-02-15 15:15:37 +0000287 dev_warn(codec->dev, "Failed to create codec register debugfs file\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000288
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200289 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000290}
291
292static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
293{
294 debugfs_remove_recursive(codec->debugfs_codec_root);
295}
296
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000297static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
298{
299 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
300
301 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
302 debugfs_card_root);
303 if (!platform->debugfs_platform_root) {
304 dev_warn(platform->dev,
305 "Failed to create platform debugfs directory\n");
306 return;
307 }
308
309 snd_soc_dapm_debugfs_init(&platform->dapm,
310 platform->debugfs_platform_root);
311}
312
313static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
314{
315 debugfs_remove_recursive(platform->debugfs_platform_root);
316}
317
Mark Brownc3c5a192010-09-15 18:15:14 +0100318static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
319 size_t count, loff_t *ppos)
320{
321 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100322 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100323 struct snd_soc_codec *codec;
324
325 if (!buf)
326 return -ENOMEM;
327
Mark Brown2b194f9d2010-10-13 10:52:16 +0100328 list_for_each_entry(codec, &codec_list, list) {
329 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
330 codec->name);
331 if (len >= 0)
332 ret += len;
333 if (ret > PAGE_SIZE) {
334 ret = PAGE_SIZE;
335 break;
336 }
337 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100338
339 if (ret >= 0)
340 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
341
342 kfree(buf);
343
344 return ret;
345}
346
347static const struct file_operations codec_list_fops = {
348 .read = codec_list_read_file,
349 .llseek = default_llseek,/* read accesses f_pos */
350};
351
Mark Brownf3208782010-09-15 18:19:07 +0100352static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
353 size_t count, loff_t *ppos)
354{
355 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100356 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100357 struct snd_soc_dai *dai;
358
359 if (!buf)
360 return -ENOMEM;
361
Mark Brown2b194f9d2010-10-13 10:52:16 +0100362 list_for_each_entry(dai, &dai_list, list) {
363 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
364 if (len >= 0)
365 ret += len;
366 if (ret > PAGE_SIZE) {
367 ret = PAGE_SIZE;
368 break;
369 }
370 }
Mark Brownf3208782010-09-15 18:19:07 +0100371
Mark Brown2b194f9d2010-10-13 10:52:16 +0100372 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100373
374 kfree(buf);
375
376 return ret;
377}
378
379static const struct file_operations dai_list_fops = {
380 .read = dai_list_read_file,
381 .llseek = default_llseek,/* read accesses f_pos */
382};
383
Mark Brown19c7ac22010-09-15 18:22:40 +0100384static ssize_t platform_list_read_file(struct file *file,
385 char __user *user_buf,
386 size_t count, loff_t *ppos)
387{
388 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100389 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100390 struct snd_soc_platform *platform;
391
392 if (!buf)
393 return -ENOMEM;
394
Mark Brown2b194f9d2010-10-13 10:52:16 +0100395 list_for_each_entry(platform, &platform_list, list) {
396 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
397 platform->name);
398 if (len >= 0)
399 ret += len;
400 if (ret > PAGE_SIZE) {
401 ret = PAGE_SIZE;
402 break;
403 }
404 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100405
Mark Brown2b194f9d2010-10-13 10:52:16 +0100406 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100407
408 kfree(buf);
409
410 return ret;
411}
412
413static const struct file_operations platform_list_fops = {
414 .read = platform_list_read_file,
415 .llseek = default_llseek,/* read accesses f_pos */
416};
417
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200418static void soc_init_card_debugfs(struct snd_soc_card *card)
419{
420 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000421 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200422 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200423 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100424 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200425 return;
426 }
427
428 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
429 card->debugfs_card_root,
430 &card->pop_time);
431 if (!card->debugfs_pop_time)
432 dev_warn(card->dev,
433 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200434}
435
436static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
437{
438 debugfs_remove_recursive(card->debugfs_card_root);
439}
440
Mark Brown2624d5f2009-11-03 21:56:13 +0000441#else
442
443static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
444{
445}
446
447static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
448{
449}
Axel Linb95fccb2010-11-09 17:06:44 +0800450
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000451static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
452{
453}
454
455static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
456{
457}
458
Axel Linb95fccb2010-11-09 17:06:44 +0800459static inline void soc_init_card_debugfs(struct snd_soc_card *card)
460{
461}
462
463static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
464{
465}
Mark Brown2624d5f2009-11-03 21:56:13 +0000466#endif
467
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100468struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
469 const char *dai_link, int stream)
470{
471 int i;
472
473 for (i = 0; i < card->num_links; i++) {
474 if (card->rtd[i].dai_link->no_pcm &&
475 !strcmp(card->rtd[i].dai_link->name, dai_link))
476 return card->rtd[i].pcm->streams[stream].substream;
477 }
478 dev_dbg(card->dev, "failed to find dai link %s\n", dai_link);
479 return NULL;
480}
481EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
482
483struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
484 const char *dai_link)
485{
486 int i;
487
488 for (i = 0; i < card->num_links; i++) {
489 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
490 return &card->rtd[i];
491 }
492 dev_dbg(card->dev, "failed to find rtd %s\n", dai_link);
493 return NULL;
494}
495EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
496
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200497#ifdef CONFIG_SND_SOC_AC97_BUS
498/* unregister ac97 codec */
499static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
500{
501 if (codec->ac97->dev.bus)
502 device_unregister(&codec->ac97->dev);
503 return 0;
504}
505
506/* stop no dev release warning */
507static void soc_ac97_device_release(struct device *dev){}
508
509/* register ac97 codec to bus */
510static int soc_ac97_dev_register(struct snd_soc_codec *codec)
511{
512 int err;
513
514 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100515 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200516 codec->ac97->dev.release = soc_ac97_device_release;
517
Kay Sieversbb072bf2008-11-02 03:50:35 +0100518 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000519 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200520 err = device_register(&codec->ac97->dev);
521 if (err < 0) {
522 snd_printk(KERN_ERR "Can't register ac97 bus\n");
523 codec->ac97->dev.bus = NULL;
524 return err;
525 }
526 return 0;
527}
528#endif
529
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000530#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200531/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000532int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200533{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000534 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200535 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200536 int i;
537
Daniel Macke3509ff2009-06-03 17:44:49 +0200538 /* If the initialization of this soc device failed, there is no codec
539 * associated with it. Just bail out in this case.
540 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000541 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200542 return 0;
543
Andy Green6ed25972008-06-13 16:24:05 +0100544 /* Due to the resume being scheduled into a workqueue we could
545 * suspend before that's finished - wait for it to complete.
546 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000547 snd_power_lock(card->snd_card);
548 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
549 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100550
551 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100553
Mark Browna00f90f2010-12-02 16:24:24 +0000554 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000555 for (i = 0; i < card->num_rtd; i++) {
556 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
557 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100558
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000559 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100560 continue;
561
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 if (drv->ops->digital_mute && dai->playback_active)
563 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200564 }
565
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100566 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000567 for (i = 0; i < card->num_rtd; i++) {
568 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100569 continue;
570
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000571 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100572 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100573
Mark Brown87506542008-11-18 20:50:34 +0000574 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000575 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200576
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000577 for (i = 0; i < card->num_rtd; i++) {
578 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
579 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100580
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000581 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100582 continue;
583
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000584 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
585 cpu_dai->driver->suspend(cpu_dai);
586 if (platform->driver->suspend && !platform->suspended) {
587 platform->driver->suspend(cpu_dai);
588 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100589 }
590 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200591
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000592 /* close any waiting streams and save state */
593 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +0100594 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200595 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000596 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100597
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599
600 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100601 continue;
602
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800603 snd_soc_dapm_stream_event(&card->rtd[i],
604 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800605 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000606
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800607 snd_soc_dapm_stream_event(&card->rtd[i],
608 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800609 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000610 }
611
612 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200613 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000614 /* If there are paths active then the CODEC will be held with
615 * bias _ON and should not be suspended. */
616 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200617 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000618 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000619 /*
620 * If the CODEC is capable of idle
621 * bias off then being in STANDBY
622 * means it's doing something,
623 * otherwise fall through.
624 */
625 if (codec->dapm.idle_bias_off) {
626 dev_dbg(codec->dev,
627 "idle_bias_off CODEC on over suspend\n");
628 break;
629 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000630 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100631 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000632 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900633 codec->cache_sync = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000634 break;
635 default:
636 dev_dbg(codec->dev, "CODEC is on over suspend\n");
637 break;
638 }
639 }
640 }
641
642 for (i = 0; i < card->num_rtd; i++) {
643 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
644
645 if (card->rtd[i].dai_link->ignore_suspend)
646 continue;
647
648 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
649 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200650 }
651
Mark Brown87506542008-11-18 20:50:34 +0000652 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000653 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200654
655 return 0;
656}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000657EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200658
Andy Green6ed25972008-06-13 16:24:05 +0100659/* deferred resume work, so resume can complete before we finished
660 * setting our codec back up, which can be very slow on I2C
661 */
662static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200663{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000664 struct snd_soc_card *card =
665 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200666 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200667 int i;
668
Andy Green6ed25972008-06-13 16:24:05 +0100669 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
670 * so userspace apps are blocked from touching us
671 */
672
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000673 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100674
Mark Brown99497882010-05-07 20:24:05 +0100675 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000676 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100677
Mark Brown87506542008-11-18 20:50:34 +0000678 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000679 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200680
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000681 /* resume AC97 DAIs */
682 for (i = 0; i < card->num_rtd; i++) {
683 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100684
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000685 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100686 continue;
687
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000688 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
689 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690 }
691
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200692 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000693 /* If the CODEC was idle over suspend then it will have been
694 * left with bias OFF or STANDBY and suspended so we must now
695 * resume. Otherwise the suspend was suppressed.
696 */
697 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200698 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000699 case SND_SOC_BIAS_STANDBY:
700 case SND_SOC_BIAS_OFF:
701 codec->driver->resume(codec);
702 codec->suspended = 0;
703 break;
704 default:
705 dev_dbg(codec->dev, "CODEC was on over suspend\n");
706 break;
707 }
Mark Brown1547aba2010-05-07 21:11:40 +0100708 }
709 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200710
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000711 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100712
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000713 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100714 continue;
715
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800716 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000717 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800718 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000719
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800720 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000721 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800722 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200723 }
724
Mark Brown3ff3f642008-05-19 12:32:25 +0200725 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000726 for (i = 0; i < card->num_rtd; i++) {
727 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
728 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100729
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000730 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100731 continue;
732
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000733 if (drv->ops->digital_mute && dai->playback_active)
734 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200735 }
736
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000737 for (i = 0; i < card->num_rtd; i++) {
738 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
739 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100740
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000741 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100742 continue;
743
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000744 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
745 cpu_dai->driver->resume(cpu_dai);
746 if (platform->driver->resume && platform->suspended) {
747 platform->driver->resume(cpu_dai);
748 platform->suspended = 0;
749 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200750 }
751
Mark Brown87506542008-11-18 20:50:34 +0000752 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000753 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200754
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000755 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100756
757 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000758 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100759}
760
761/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000762int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100763{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000764 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600765 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200766
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800767 /* If the initialization of this soc device failed, there is no codec
768 * associated with it. Just bail out in this case.
769 */
770 if (list_empty(&card->codec_dev_list))
771 return 0;
772
Mark Brown64ab9ba2009-03-31 11:27:03 +0100773 /* AC97 devices might have other drivers hanging off them so
774 * need to resume immediately. Other drivers don't have that
775 * problem and may take a substantial amount of time to resume
776 * due to I/O costs and anti-pop so handle them out of line.
777 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000778 for (i = 0; i < card->num_rtd; i++) {
779 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600780 ac97_control |= cpu_dai->driver->ac97_control;
781 }
782 if (ac97_control) {
783 dev_dbg(dev, "Resuming AC97 immediately\n");
784 soc_resume_deferred(&card->deferred_resume_work);
785 } else {
786 dev_dbg(dev, "Scheduling resume work\n");
787 if (!schedule_work(&card->deferred_resume_work))
788 dev_err(dev, "resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100789 }
Andy Green6ed25972008-06-13 16:24:05 +0100790
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200791 return 0;
792}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000793EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200794#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000795#define snd_soc_suspend NULL
796#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200797#endif
798
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100799static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800800};
801
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000802static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200803{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000804 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
805 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000806 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000807 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000808 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100809 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200810
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000811 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000812
Mark Brownb19e6e72012-03-14 21:18:39 +0000813 /* Find CPU DAI from registered DAIs*/
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000814 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600815 if (dai_link->cpu_of_node &&
816 (cpu_dai->dev->of_node != dai_link->cpu_of_node))
817 continue;
818 if (dai_link->cpu_name &&
819 strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
820 continue;
821 if (dai_link->cpu_dai_name &&
822 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
823 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700824
825 rtd->cpu_dai = cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000826 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000827
828 if (!rtd->cpu_dai) {
829 dev_dbg(card->dev, "CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000830 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000831 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000832 }
833
Mark Brownb19e6e72012-03-14 21:18:39 +0000834 /* Find CODEC from registered CODECs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000835 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700836 if (dai_link->codec_of_node) {
837 if (codec->dev->of_node != dai_link->codec_of_node)
838 continue;
839 } else {
840 if (strcmp(codec->name, dai_link->codec_name))
841 continue;
842 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000843
Stephen Warren2610ab72011-12-07 13:58:27 -0700844 rtd->codec = codec;
845
846 /*
847 * CODEC found, so find CODEC DAI from registered DAIs from
848 * this CODEC
849 */
850 list_for_each_entry(codec_dai, &dai_list, list) {
851 if (codec->dev == codec_dai->dev &&
852 !strcmp(codec_dai->name,
853 dai_link->codec_dai_name)) {
854
855 rtd->codec_dai = codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000856 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000857 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000858
859 if (!rtd->codec_dai) {
860 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
Stephen Warren2610ab72011-12-07 13:58:27 -0700861 dai_link->codec_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000862 return -EPROBE_DEFER;
863 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000864 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000865
Mark Brownb19e6e72012-03-14 21:18:39 +0000866 if (!rtd->codec) {
867 dev_dbg(card->dev, "CODEC %s not registered\n",
868 dai_link->codec_name);
869 return -EPROBE_DEFER;
870 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100871
872 /* if there's no platform we match on the empty platform */
873 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700874 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100875 platform_name = "snd-soc-dummy";
876
Mark Brownb19e6e72012-03-14 21:18:39 +0000877 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000878 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700879 if (dai_link->platform_of_node) {
880 if (platform->dev->of_node !=
881 dai_link->platform_of_node)
882 continue;
883 } else {
884 if (strcmp(platform->name, platform_name))
885 continue;
886 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700887
888 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000889 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000890 if (!rtd->platform) {
891 dev_dbg(card->dev, "platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000892 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000893 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000894 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000895
896 card->num_rtd++;
897
898 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000899}
900
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 Girdwood675c4962012-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);
Stephen Warrena9db7db2012-06-08 12:34:20 -0600986
Stephen Warren18d75642012-06-08 12:34:21 -0600987 if (!cpu_dai->codec) {
988 snd_soc_dapm_free(&cpu_dai->dapm);
Stephen Warrena9db7db2012-06-08 12:34:20 -0600989 module_put(cpu_dai->dev->driver->owner);
Stephen Warren18d75642012-06-08 12:34:21 -0600990 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000991 }
992}
993
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900994static void soc_remove_dai_links(struct snd_soc_card *card)
995{
Liam Girdwood0168bf02011-06-07 16:08:05 +0100996 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900997
Liam Girdwood0168bf02011-06-07 16:08:05 +0100998 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
999 order++) {
1000 for (dai = 0; dai < card->num_rtd; dai++)
1001 soc_remove_dai_link(card, dai, order);
1002 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001003 card->num_rtd = 0;
1004}
1005
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001006static void soc_set_name_prefix(struct snd_soc_card *card,
1007 struct snd_soc_codec *codec)
1008{
1009 int i;
1010
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001011 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001012 return;
1013
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001014 for (i = 0; i < card->num_configs; i++) {
1015 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001016 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1017 codec->name_prefix = map->name_prefix;
1018 break;
1019 }
1020 }
1021}
1022
Jarkko Nikula589c3562010-12-06 16:27:07 +02001023static int soc_probe_codec(struct snd_soc_card *card,
1024 struct snd_soc_codec *codec)
1025{
1026 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001027 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001028 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001029
1030 codec->card = card;
1031 codec->dapm.card = card;
1032 soc_set_name_prefix(card, codec);
1033
Jarkko Nikula70d29332011-01-27 16:24:22 +02001034 if (!try_module_get(codec->dev->driver->owner))
1035 return -ENODEV;
1036
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001037 soc_init_codec_debugfs(codec);
1038
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001039 if (driver->dapm_widgets)
1040 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1041 driver->num_dapm_widgets);
1042
Mark Brown888df392012-02-16 19:37:51 -08001043 /* Create DAPM widgets for each DAI stream */
1044 list_for_each_entry(dai, &dai_list, list) {
1045 if (dai->dev != codec->dev)
1046 continue;
1047
1048 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1049 }
1050
Mark Brown33c5f962011-08-22 18:40:30 +01001051 codec->dapm.idle_bias_off = driver->idle_bias_off;
1052
Mark Brown89b95ac02011-03-07 16:38:44 +00001053 if (driver->probe) {
1054 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001055 if (ret < 0) {
1056 dev_err(codec->dev,
1057 "asoc: failed to probe CODEC %s: %d\n",
1058 codec->name, ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001059 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001060 }
1061 }
1062
Mark Brownb7af1da2011-04-07 19:18:44 +09001063 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001064 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001065 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001066 if (driver->dapm_routes)
1067 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1068 driver->num_dapm_routes);
1069
Jarkko Nikula589c3562010-12-06 16:27:07 +02001070 /* mark codec as probed and add to card codec list */
1071 codec->probed = 1;
1072 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001073 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001074
Jarkko Nikula70d29332011-01-27 16:24:22 +02001075 return 0;
1076
1077err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001078 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001079 module_put(codec->dev->driver->owner);
1080
Jarkko Nikula589c3562010-12-06 16:27:07 +02001081 return ret;
1082}
1083
Liam Girdwood956245e2011-07-01 16:54:08 +01001084static int soc_probe_platform(struct snd_soc_card *card,
1085 struct snd_soc_platform *platform)
1086{
1087 int ret = 0;
1088 const struct snd_soc_platform_driver *driver = platform->driver;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001089 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001090
1091 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001092 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001093
1094 if (!try_module_get(platform->dev->driver->owner))
1095 return -ENODEV;
1096
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001097 soc_init_platform_debugfs(platform);
1098
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001099 if (driver->dapm_widgets)
1100 snd_soc_dapm_new_controls(&platform->dapm,
1101 driver->dapm_widgets, driver->num_dapm_widgets);
1102
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001103 /* Create DAPM widgets for each DAI stream */
1104 list_for_each_entry(dai, &dai_list, list) {
1105 if (dai->dev != platform->dev)
1106 continue;
1107
1108 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1109 }
1110
Stephen Warren3fec6b62012-04-05 12:28:01 -06001111 platform->dapm.idle_bias_off = 1;
1112
Liam Girdwood956245e2011-07-01 16:54:08 +01001113 if (driver->probe) {
1114 ret = driver->probe(platform);
1115 if (ret < 0) {
1116 dev_err(platform->dev,
1117 "asoc: failed to probe platform %s: %d\n",
1118 platform->name, ret);
1119 goto err_probe;
1120 }
1121 }
1122
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001123 if (driver->controls)
1124 snd_soc_add_platform_controls(platform, driver->controls,
1125 driver->num_controls);
1126 if (driver->dapm_routes)
1127 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1128 driver->num_dapm_routes);
1129
Liam Girdwood956245e2011-07-01 16:54:08 +01001130 /* mark platform as probed and add to card platform list */
1131 platform->probed = 1;
1132 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001133 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001134
1135 return 0;
1136
1137err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001138 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001139 module_put(platform->dev->driver->owner);
1140
1141 return ret;
1142}
1143
Mark Brown36ae1a92012-01-06 17:12:45 -08001144static void rtd_release(struct device *dev)
1145{
1146 kfree(dev);
1147}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001148
Jarkko Nikula589c3562010-12-06 16:27:07 +02001149static int soc_post_component_init(struct snd_soc_card *card,
1150 struct snd_soc_codec *codec,
1151 int num, int dailess)
1152{
1153 struct snd_soc_dai_link *dai_link = NULL;
1154 struct snd_soc_aux_dev *aux_dev = NULL;
1155 struct snd_soc_pcm_runtime *rtd;
1156 const char *temp, *name;
1157 int ret = 0;
1158
1159 if (!dailess) {
1160 dai_link = &card->dai_link[num];
1161 rtd = &card->rtd[num];
1162 name = dai_link->name;
1163 } else {
1164 aux_dev = &card->aux_dev[num];
1165 rtd = &card->rtd_aux[num];
1166 name = aux_dev->name;
1167 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001168 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001169
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001170 /* Make sure all DAPM widgets are instantiated */
1171 snd_soc_dapm_new_widgets(&codec->dapm);
1172
Jarkko Nikula589c3562010-12-06 16:27:07 +02001173 /* machine controls, routes and widgets are not prefixed */
1174 temp = codec->name_prefix;
1175 codec->name_prefix = NULL;
1176
1177 /* do machine specific initialization */
1178 if (!dailess && dai_link->init)
1179 ret = dai_link->init(rtd);
1180 else if (dailess && aux_dev->init)
1181 ret = aux_dev->init(&codec->dapm);
1182 if (ret < 0) {
1183 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1184 return ret;
1185 }
1186 codec->name_prefix = temp;
1187
Jarkko Nikula589c3562010-12-06 16:27:07 +02001188 /* register the rtd device */
1189 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001190
1191 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1192 if (!rtd->dev)
1193 return -ENOMEM;
1194 device_initialize(rtd->dev);
1195 rtd->dev->parent = card->dev;
1196 rtd->dev->release = rtd_release;
1197 rtd->dev->init_name = name;
1198 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001199 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001200 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1201 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1202 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1203 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001204 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001205 if (ret < 0) {
1206 dev_err(card->dev,
1207 "asoc: failed to register runtime device: %d\n", ret);
1208 return ret;
1209 }
1210 rtd->dev_registered = 1;
1211
1212 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001213 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001214 if (ret < 0)
1215 dev_err(codec->dev,
1216 "asoc: failed to add codec dapm sysfs entries: %d\n",
1217 ret);
1218
1219 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001220 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001221 if (ret < 0)
1222 dev_err(codec->dev,
1223 "asoc: failed to add codec sysfs files: %d\n", ret);
1224
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001225#ifdef CONFIG_DEBUG_FS
1226 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001227 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001228 goto out;
1229
1230 ret = soc_dpcm_debugfs_add(rtd);
1231 if (ret < 0)
1232 dev_err(rtd->dev, "asoc: failed to add dpcm sysfs entries: %d\n", ret);
1233
1234out:
1235#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001236 return 0;
1237}
1238
Liam Girdwood0168bf02011-06-07 16:08:05 +01001239static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001240{
1241 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1242 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1243 struct snd_soc_codec *codec = rtd->codec;
1244 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001245 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1246 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1247 struct snd_soc_dapm_widget *play_w, *capture_w;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001248 int ret;
1249
Liam Girdwood0168bf02011-06-07 16:08:05 +01001250 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1251 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001252
1253 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001254 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001255 codec_dai->card = card;
1256 cpu_dai->card = card;
1257
1258 /* set default power off timeout */
1259 rtd->pmdown_time = pmdown_time;
1260
1261 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001262 if (!cpu_dai->probed &&
1263 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001264 if (!cpu_dai->codec) {
1265 cpu_dai->dapm.card = card;
1266 if (!try_module_get(cpu_dai->dev->driver->owner))
1267 return -ENODEV;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001268
Stephen Warren18d75642012-06-08 12:34:21 -06001269 list_add(&cpu_dai->dapm.list, &card->dapm_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001270 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1271 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001272
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001273 if (cpu_dai->driver->probe) {
1274 ret = cpu_dai->driver->probe(cpu_dai);
1275 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001276 pr_err("asoc: failed to probe CPU DAI %s: %d\n",
1277 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001278 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001279 return ret;
1280 }
1281 }
1282 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001283 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001284 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1285 }
1286
1287 /* probe the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001288 if (!codec->probed &&
1289 codec->driver->probe_order == order) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001290 ret = soc_probe_codec(card, codec);
1291 if (ret < 0)
1292 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001293 }
1294
1295 /* probe the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001296 if (!platform->probed &&
1297 platform->driver->probe_order == order) {
Liam Girdwood956245e2011-07-01 16:54:08 +01001298 ret = soc_probe_platform(card, platform);
1299 if (ret < 0)
1300 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001301 }
1302
1303 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001304 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001305 if (codec_dai->driver->probe) {
1306 ret = codec_dai->driver->probe(codec_dai);
1307 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001308 pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
1309 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001310 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001311 }
1312 }
1313
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001314 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001315 codec_dai->probed = 1;
1316 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001317 }
1318
Liam Girdwood0168bf02011-06-07 16:08:05 +01001319 /* complete DAI probe during last probe */
1320 if (order != SND_SOC_COMP_ORDER_LAST)
1321 return 0;
1322
Jarkko Nikula589c3562010-12-06 16:27:07 +02001323 ret = soc_post_component_init(card, codec, num, 0);
1324 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001325 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001326
Mark Brown36ae1a92012-01-06 17:12:45 -08001327 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001328 if (ret < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -02001329 pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001330
Mark Brownc74184e2012-04-04 22:12:09 +01001331 if (!dai_link->params) {
1332 /* create the pcm */
1333 ret = soc_new_pcm(rtd, num);
1334 if (ret < 0) {
1335 pr_err("asoc: can't create pcm %s :%d\n",
1336 dai_link->stream_name, ret);
1337 return ret;
1338 }
1339 } else {
1340 /* link the DAI widgets */
1341 play_w = codec_dai->playback_widget;
1342 capture_w = cpu_dai->capture_widget;
1343 if (play_w && capture_w) {
1344 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1345 capture_w, play_w);
1346 if (ret != 0) {
1347 dev_err(card->dev, "Can't link %s to %s: %d\n",
1348 play_w->name, capture_w->name, ret);
1349 return ret;
1350 }
1351 }
1352
1353 play_w = cpu_dai->playback_widget;
1354 capture_w = codec_dai->capture_widget;
1355 if (play_w && capture_w) {
1356 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1357 capture_w, play_w);
1358 if (ret != 0) {
1359 dev_err(card->dev, "Can't link %s to %s: %d\n",
1360 play_w->name, capture_w->name, ret);
1361 return ret;
1362 }
1363 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001364 }
1365
1366 /* add platform data for AC97 devices */
1367 if (rtd->codec_dai->driver->ac97_control)
1368 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1369
1370 return 0;
1371}
1372
1373#ifdef CONFIG_SND_SOC_AC97_BUS
1374static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1375{
1376 int ret;
1377
1378 /* Only instantiate AC97 if not already done by the adaptor
1379 * for the generic AC97 subsystem.
1380 */
1381 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001382 /*
1383 * It is possible that the AC97 device is already registered to
1384 * the device subsystem. This happens when the device is created
1385 * via snd_ac97_mixer(). Currently only SoC codec that does so
1386 * is the generic AC97 glue but others migh emerge.
1387 *
1388 * In those cases we don't try to register the device again.
1389 */
1390 if (!rtd->codec->ac97_created)
1391 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001392
1393 ret = soc_ac97_dev_register(rtd->codec);
1394 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001395 pr_err("asoc: AC97 device register failed:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001396 return ret;
1397 }
1398
1399 rtd->codec->ac97_registered = 1;
1400 }
1401 return 0;
1402}
1403
1404static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1405{
1406 if (codec->ac97_registered) {
1407 soc_ac97_dev_unregister(codec);
1408 codec->ac97_registered = 0;
1409 }
1410}
1411#endif
1412
Mark Brownb19e6e72012-03-14 21:18:39 +00001413static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1414{
1415 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1416 struct snd_soc_codec *codec;
1417
1418 /* find CODEC from registered CODECs*/
1419 list_for_each_entry(codec, &codec_list, list) {
1420 if (!strcmp(codec->name, aux_dev->codec_name))
1421 return 0;
1422 }
1423
1424 return -EPROBE_DEFER;
1425}
1426
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001427static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1428{
1429 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001430 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001431 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001432
1433 /* find CODEC from registered CODECs*/
1434 list_for_each_entry(codec, &codec_list, list) {
1435 if (!strcmp(codec->name, aux_dev->codec_name)) {
1436 if (codec->probed) {
1437 dev_err(codec->dev,
1438 "asoc: codec already probed");
1439 ret = -EBUSY;
1440 goto out;
1441 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001442 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001443 }
1444 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001445 /* codec not found */
1446 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001447 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001448
Jarkko Nikula676ad982010-12-03 09:18:22 +02001449found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001450 ret = soc_probe_codec(card, codec);
1451 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001452 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001453
Jarkko Nikula589c3562010-12-06 16:27:07 +02001454 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001455
1456out:
1457 return ret;
1458}
1459
1460static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1461{
1462 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1463 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001464
1465 /* unregister the rtd device */
1466 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001467 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1468 device_del(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001469 rtd->dev_registered = 0;
1470 }
1471
Jarkko Nikula589c3562010-12-06 16:27:07 +02001472 if (codec && codec->probed)
1473 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001474}
1475
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001476static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1477 enum snd_soc_compress_type compress_type)
1478{
1479 int ret;
1480
1481 if (codec->cache_init)
1482 return 0;
1483
1484 /* override the compress_type if necessary */
1485 if (compress_type && codec->compress_type != compress_type)
1486 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001487 ret = snd_soc_cache_init(codec);
1488 if (ret < 0) {
1489 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1490 ret);
1491 return ret;
1492 }
1493 codec->cache_init = 1;
1494 return 0;
1495}
1496
Mark Brownb19e6e72012-03-14 21:18:39 +00001497static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001498{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001499 struct snd_soc_codec *codec;
1500 struct snd_soc_codec_conf *codec_conf;
1501 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001502 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001503 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001504
Liam Girdwood01b9d992012-03-07 10:38:25 +00001505 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001506
Mark Brownb19e6e72012-03-14 21:18:39 +00001507 /* bind DAIs */
1508 for (i = 0; i < card->num_links; i++) {
1509 ret = soc_bind_dai_link(card, i);
1510 if (ret != 0)
1511 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001512 }
1513
Mark Brownb19e6e72012-03-14 21:18:39 +00001514 /* check aux_devs too */
1515 for (i = 0; i < card->num_aux_devs; i++) {
1516 ret = soc_check_aux_dev(card, i);
1517 if (ret != 0)
1518 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001519 }
1520
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001521 /* initialize the register cache for each available codec */
1522 list_for_each_entry(codec, &codec_list, list) {
1523 if (codec->cache_init)
1524 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001525 /* by default we don't override the compress_type */
1526 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001527 /* check to see if we need to override the compress_type */
1528 for (i = 0; i < card->num_configs; ++i) {
1529 codec_conf = &card->codec_conf[i];
1530 if (!strcmp(codec->name, codec_conf->dev_name)) {
1531 compress_type = codec_conf->compress_type;
1532 if (compress_type && compress_type
1533 != codec->compress_type)
1534 break;
1535 }
1536 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001537 ret = snd_soc_init_codec_cache(codec, compress_type);
Mark Brownb19e6e72012-03-14 21:18:39 +00001538 if (ret < 0)
1539 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001540 }
1541
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001542 /* card bind complete so register a sound card */
1543 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1544 card->owner, 0, &card->snd_card);
1545 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001546 pr_err("asoc: can't create sound card for card %s: %d\n",
1547 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001548 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001549 }
1550 card->snd_card->dev = card->dev;
1551
Mark Browne37a4972011-03-02 18:21:57 +00001552 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1553 card->dapm.dev = card->dev;
1554 card->dapm.card = card;
1555 list_add(&card->dapm.list, &card->dapm_list);
1556
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001557#ifdef CONFIG_DEBUG_FS
1558 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1559#endif
1560
Mark Brown88ee1c62011-02-02 10:43:26 +00001561#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001562 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001563 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001564#endif
Andy Green6ed25972008-06-13 16:24:05 +01001565
Mark Brown9a841eb2011-04-12 17:51:37 -07001566 if (card->dapm_widgets)
1567 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1568 card->num_dapm_widgets);
1569
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001570 /* initialise the sound card only once */
1571 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001572 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001573 if (ret < 0)
1574 goto card_probe_error;
1575 }
1576
Liam Girdwood0168bf02011-06-07 16:08:05 +01001577 /* early DAI link probe */
1578 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1579 order++) {
1580 for (i = 0; i < card->num_links; i++) {
1581 ret = soc_probe_dai_link(card, i, order);
1582 if (ret < 0) {
1583 pr_err("asoc: failed to instantiate card %s: %d\n",
Mark Brown321de0d2010-09-21 15:09:37 +01001584 card->name, ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001585 goto probe_dai_err;
1586 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001587 }
1588 }
1589
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001590 for (i = 0; i < card->num_aux_devs; i++) {
1591 ret = soc_probe_aux_dev(card, i);
1592 if (ret < 0) {
1593 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1594 card->name, ret);
1595 goto probe_aux_dev_err;
1596 }
1597 }
1598
Mark Brown888df392012-02-16 19:37:51 -08001599 snd_soc_dapm_link_dai_widgets(card);
1600
Mark Brownb7af1da2011-04-07 19:18:44 +09001601 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001602 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001603
Mark Brownb8ad29d2011-03-02 18:35:51 +00001604 if (card->dapm_routes)
1605 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1606 card->num_dapm_routes);
1607
Mark Brownb90d2f92011-10-10 13:38:06 +01001608 snd_soc_dapm_new_widgets(&card->dapm);
1609
Mark Brown75d9ac42011-09-27 16:41:01 +01001610 for (i = 0; i < card->num_links; i++) {
1611 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001612 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001613
Mark Brownf04209a2012-04-09 16:29:21 +01001614 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001615 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001616 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001617 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001618 dev_warn(card->rtd[i].codec_dai->dev,
1619 "Failed to set DAI format: %d\n",
1620 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001621 }
1622
1623 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001624 if (dai_fmt &&
1625 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001626 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1627 dai_fmt);
1628 if (ret != 0 && ret != -ENOTSUPP)
1629 dev_warn(card->rtd[i].cpu_dai->dev,
1630 "Failed to set DAI format: %d\n",
1631 ret);
1632 } else if (dai_fmt) {
1633 /* Flip the polarity for the "CPU" end */
1634 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1635 switch (dai_link->dai_fmt &
1636 SND_SOC_DAIFMT_MASTER_MASK) {
1637 case SND_SOC_DAIFMT_CBM_CFM:
1638 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1639 break;
1640 case SND_SOC_DAIFMT_CBM_CFS:
1641 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1642 break;
1643 case SND_SOC_DAIFMT_CBS_CFM:
1644 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1645 break;
1646 case SND_SOC_DAIFMT_CBS_CFS:
1647 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1648 break;
1649 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001650
1651 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001652 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001653 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001654 dev_warn(card->rtd[i].cpu_dai->dev,
1655 "Failed to set DAI format: %d\n",
1656 ret);
1657 }
1658 }
1659
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001660 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001661 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001662 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1663 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001664 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1665 "%s", card->driver_name ? card->driver_name : card->name);
1666 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1667 switch (card->snd_card->driver[i]) {
1668 case '_':
1669 case '-':
1670 case '\0':
1671 break;
1672 default:
1673 if (!isalnum(card->snd_card->driver[i]))
1674 card->snd_card->driver[i] = '_';
1675 break;
1676 }
1677 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001678
Mark Brown28e9ad92011-03-02 18:36:34 +00001679 if (card->late_probe) {
1680 ret = card->late_probe(card);
1681 if (ret < 0) {
1682 dev_err(card->dev, "%s late_probe() failed: %d\n",
1683 card->name, ret);
1684 goto probe_aux_dev_err;
1685 }
1686 }
1687
Mark Brown2dc00212011-10-08 13:59:44 +01001688 snd_soc_dapm_new_widgets(&card->dapm);
1689
Stephen Warren16332812011-11-23 12:42:04 -07001690 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001691 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001692 snd_soc_dapm_auto_nc_codec_pins(codec);
1693
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001694 ret = snd_card_register(card->snd_card);
1695 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001696 pr_err("asoc: failed to register soundcard for %s: %d\n",
1697 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001698 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001699 }
1700
1701#ifdef CONFIG_SND_SOC_AC97_BUS
1702 /* register any AC97 codecs */
1703 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001704 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1705 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001706 pr_err("asoc: failed to register AC97 %s: %d\n",
1707 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001708 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001709 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001710 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001711 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001712 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001713#endif
1714
Mark Brown435c5e22008-12-04 15:32:53 +00001715 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001716 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001717 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001718
1719 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001720
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001721probe_aux_dev_err:
1722 for (i = 0; i < card->num_aux_devs; i++)
1723 soc_remove_aux_dev(card, i);
1724
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001725probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001726 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001727
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001728card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001729 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001730 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001731
1732 snd_card_free(card->snd_card);
1733
Mark Brownb19e6e72012-03-14 21:18:39 +00001734base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001735 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001736
Mark Brownb19e6e72012-03-14 21:18:39 +00001737 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001738}
1739
1740/* probes a new socdev */
1741static int soc_probe(struct platform_device *pdev)
1742{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001743 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001744 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001745
Vinod Koul70a7ca32011-01-14 19:22:48 +05301746 /*
1747 * no card, so machine driver should be registering card
1748 * we should not be here in that case so ret error
1749 */
1750 if (!card)
1751 return -EINVAL;
1752
Mark Brownfe4085e2012-03-02 13:07:41 +00001753 dev_warn(&pdev->dev,
1754 "ASoC machine %s should use snd_soc_register_card()\n",
1755 card->name);
1756
Mark Brown435c5e22008-12-04 15:32:53 +00001757 /* Bodge while we unpick instantiation */
1758 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001759
Mark Brown435c5e22008-12-04 15:32:53 +00001760 ret = snd_soc_register_card(card);
1761 if (ret != 0) {
1762 dev_err(&pdev->dev, "Failed to register card\n");
1763 return ret;
1764 }
1765
1766 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001767}
1768
Vinod Koulb0e26482011-01-13 22:48:02 +05301769static int soc_cleanup_card_resources(struct snd_soc_card *card)
1770{
Vinod Koulb0e26482011-01-13 22:48:02 +05301771 int i;
1772
1773 /* make sure any delayed work runs */
1774 for (i = 0; i < card->num_rtd; i++) {
1775 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1776 flush_delayed_work_sync(&rtd->delayed_work);
1777 }
1778
1779 /* remove auxiliary devices */
1780 for (i = 0; i < card->num_aux_devs; i++)
1781 soc_remove_aux_dev(card, i);
1782
1783 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001784 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301785
1786 soc_cleanup_card_debugfs(card);
1787
1788 /* remove the card */
1789 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001790 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301791
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001792 snd_soc_dapm_free(&card->dapm);
1793
Vinod Koulb0e26482011-01-13 22:48:02 +05301794 snd_card_free(card->snd_card);
1795 return 0;
1796
1797}
1798
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001799/* removes a socdev */
1800static int soc_remove(struct platform_device *pdev)
1801{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001802 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001803
Mark Brownc5af3a22008-11-28 13:29:45 +00001804 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001805 return 0;
1806}
1807
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001808int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001809{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001810 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001811 int i;
Mark Brown51737472009-06-22 13:16:51 +01001812
1813 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001814 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001815
1816 /* Flush out pmdown_time work - we actually do want to run it
1817 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001818 for (i = 0; i < card->num_rtd; i++) {
1819 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001820 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001821 }
Mark Brown51737472009-06-22 13:16:51 +01001822
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001823 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001824
1825 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001826}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001827EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001828
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001829const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301830 .suspend = snd_soc_suspend,
1831 .resume = snd_soc_resume,
1832 .freeze = snd_soc_suspend,
1833 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001834 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301835 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001836};
Stephen Warrendeb26072011-04-05 19:35:30 -06001837EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001838
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001839/* ASoC platform driver */
1840static struct platform_driver soc_driver = {
1841 .driver = {
1842 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001843 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001844 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001845 },
1846 .probe = soc_probe,
1847 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001848};
1849
Mark Brown096e49d2009-07-05 15:12:22 +01001850/**
1851 * snd_soc_codec_volatile_register: Report if a register is volatile.
1852 *
1853 * @codec: CODEC to query.
1854 * @reg: Register to query.
1855 *
1856 * Boolean function indiciating if a CODEC register is volatile.
1857 */
Mark Brown181e0552011-01-24 14:05:25 +00001858int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1859 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001860{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001861 if (codec->volatile_register)
1862 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001863 else
1864 return 0;
1865}
1866EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1867
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001868/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001869 * snd_soc_codec_readable_register: Report if a register is readable.
1870 *
1871 * @codec: CODEC to query.
1872 * @reg: Register to query.
1873 *
1874 * Boolean function indicating if a CODEC register is readable.
1875 */
1876int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1877 unsigned int reg)
1878{
1879 if (codec->readable_register)
1880 return codec->readable_register(codec, reg);
1881 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001882 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001883}
1884EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1885
1886/**
1887 * snd_soc_codec_writable_register: Report if a register is writable.
1888 *
1889 * @codec: CODEC to query.
1890 * @reg: Register to query.
1891 *
1892 * Boolean function indicating if a CODEC register is writable.
1893 */
1894int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1895 unsigned int reg)
1896{
1897 if (codec->writable_register)
1898 return codec->writable_register(codec, reg);
1899 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001900 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001901}
1902EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1903
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001904int snd_soc_platform_read(struct snd_soc_platform *platform,
1905 unsigned int reg)
1906{
1907 unsigned int ret;
1908
1909 if (!platform->driver->read) {
1910 dev_err(platform->dev, "platform has no read back\n");
1911 return -1;
1912 }
1913
1914 ret = platform->driver->read(platform, reg);
1915 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001916 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001917
1918 return ret;
1919}
1920EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1921
1922int snd_soc_platform_write(struct snd_soc_platform *platform,
1923 unsigned int reg, unsigned int val)
1924{
1925 if (!platform->driver->write) {
1926 dev_err(platform->dev, "platform has no write back\n");
1927 return -1;
1928 }
1929
1930 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001931 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001932 return platform->driver->write(platform, reg, val);
1933}
1934EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1935
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001936/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001937 * snd_soc_new_ac97_codec - initailise AC97 device
1938 * @codec: audio codec
1939 * @ops: AC97 bus operations
1940 * @num: AC97 codec number
1941 *
1942 * Initialises AC97 codec resources for use by ad-hoc devices only.
1943 */
1944int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1945 struct snd_ac97_bus_ops *ops, int num)
1946{
1947 mutex_lock(&codec->mutex);
1948
1949 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1950 if (codec->ac97 == NULL) {
1951 mutex_unlock(&codec->mutex);
1952 return -ENOMEM;
1953 }
1954
1955 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1956 if (codec->ac97->bus == NULL) {
1957 kfree(codec->ac97);
1958 codec->ac97 = NULL;
1959 mutex_unlock(&codec->mutex);
1960 return -ENOMEM;
1961 }
1962
1963 codec->ac97->bus->ops = ops;
1964 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001965
1966 /*
1967 * Mark the AC97 device to be created by us. This way we ensure that the
1968 * device will be registered with the device subsystem later on.
1969 */
1970 codec->ac97_created = 1;
1971
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001972 mutex_unlock(&codec->mutex);
1973 return 0;
1974}
1975EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1976
1977/**
1978 * snd_soc_free_ac97_codec - free AC97 codec device
1979 * @codec: audio codec
1980 *
1981 * Frees AC97 codec device resources.
1982 */
1983void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1984{
1985 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001986#ifdef CONFIG_SND_SOC_AC97_BUS
1987 soc_unregister_ac97_dai_link(codec);
1988#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001989 kfree(codec->ac97->bus);
1990 kfree(codec->ac97);
1991 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001992 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001993 mutex_unlock(&codec->mutex);
1994}
1995EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1996
Mark Brownc3753702010-11-01 15:41:57 -04001997unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1998{
1999 unsigned int ret;
2000
Mark Brownc3acec22010-12-02 16:15:29 +00002001 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002002 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002003 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002004
2005 return ret;
2006}
2007EXPORT_SYMBOL_GPL(snd_soc_read);
2008
2009unsigned int snd_soc_write(struct snd_soc_codec *codec,
2010 unsigned int reg, unsigned int val)
2011{
2012 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002013 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002014 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002015}
2016EXPORT_SYMBOL_GPL(snd_soc_write);
2017
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00002018unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2019 unsigned int reg, const void *data, size_t len)
2020{
2021 return codec->bulk_write_raw(codec, reg, data, len);
2022}
2023EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2024
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002025/**
2026 * snd_soc_update_bits - update codec register bits
2027 * @codec: audio codec
2028 * @reg: codec register
2029 * @mask: register mask
2030 * @value: new value
2031 *
2032 * Writes new register value.
2033 *
Timur Tabi180c3292011-01-10 15:58:13 -06002034 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002035 */
2036int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002037 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002038{
Mark Brown8a713da2011-12-03 12:33:55 +00002039 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002040 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002041 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002042
Mark Brown8a713da2011-12-03 12:33:55 +00002043 if (codec->using_regmap) {
2044 ret = regmap_update_bits_check(codec->control_data, reg,
2045 mask, value, &change);
2046 } else {
2047 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06002048 if (ret < 0)
2049 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00002050
2051 old = ret;
2052 new = (old & ~mask) | (value & mask);
2053 change = old != new;
2054 if (change)
2055 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06002056 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002057
Mark Brown8a713da2011-12-03 12:33:55 +00002058 if (ret < 0)
2059 return ret;
2060
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002061 return change;
2062}
2063EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2064
2065/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002066 * snd_soc_update_bits_locked - update codec register bits
2067 * @codec: audio codec
2068 * @reg: codec register
2069 * @mask: register mask
2070 * @value: new value
2071 *
2072 * Writes new register value, and takes the codec mutex.
2073 *
2074 * Returns 1 for change else 0.
2075 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002076int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2077 unsigned short reg, unsigned int mask,
2078 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002079{
2080 int change;
2081
2082 mutex_lock(&codec->mutex);
2083 change = snd_soc_update_bits(codec, reg, mask, value);
2084 mutex_unlock(&codec->mutex);
2085
2086 return change;
2087}
Mark Browndd1b3d52009-12-04 14:22:03 +00002088EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002089
2090/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002091 * snd_soc_test_bits - test register for change
2092 * @codec: audio codec
2093 * @reg: codec register
2094 * @mask: register mask
2095 * @value: new value
2096 *
2097 * Tests a register with a new value and checks if the new value is
2098 * different from the old value.
2099 *
2100 * Returns 1 for change else 0.
2101 */
2102int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002103 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002104{
2105 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002106 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002107
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002108 old = snd_soc_read(codec, reg);
2109 new = (old & ~mask) | value;
2110 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002111
2112 return change;
2113}
2114EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2115
2116/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002117 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2118 * @substream: the pcm substream
2119 * @hw: the hardware parameters
2120 *
2121 * Sets the substream runtime hardware parameters.
2122 */
2123int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2124 const struct snd_pcm_hardware *hw)
2125{
2126 struct snd_pcm_runtime *runtime = substream->runtime;
2127 runtime->hw.info = hw->info;
2128 runtime->hw.formats = hw->formats;
2129 runtime->hw.period_bytes_min = hw->period_bytes_min;
2130 runtime->hw.period_bytes_max = hw->period_bytes_max;
2131 runtime->hw.periods_min = hw->periods_min;
2132 runtime->hw.periods_max = hw->periods_max;
2133 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2134 runtime->hw.fifo_size = hw->fifo_size;
2135 return 0;
2136}
2137EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2138
2139/**
2140 * snd_soc_cnew - create new control
2141 * @_template: control template
2142 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002143 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002144 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002145 *
2146 * Create a new mixer control from a template control.
2147 *
2148 * Returns 0 for success, else error.
2149 */
2150struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002151 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002152 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002153{
2154 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002155 struct snd_kcontrol *kcontrol;
2156 char *name = NULL;
2157 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002158
2159 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002160 template.index = 0;
2161
Mark Brownefb7ac32011-03-08 17:23:24 +00002162 if (!long_name)
2163 long_name = template.name;
2164
2165 if (prefix) {
2166 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08002167 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00002168 if (!name)
2169 return NULL;
2170
2171 snprintf(name, name_len, "%s %s", prefix, long_name);
2172
2173 template.name = name;
2174 } else {
2175 template.name = long_name;
2176 }
2177
2178 kcontrol = snd_ctl_new1(&template, data);
2179
2180 kfree(name);
2181
2182 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002183}
2184EXPORT_SYMBOL_GPL(snd_soc_cnew);
2185
Liam Girdwood022658b2012-02-03 17:43:09 +00002186static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2187 const struct snd_kcontrol_new *controls, int num_controls,
2188 const char *prefix, void *data)
2189{
2190 int err, i;
2191
2192 for (i = 0; i < num_controls; i++) {
2193 const struct snd_kcontrol_new *control = &controls[i];
2194 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2195 control->name, prefix));
2196 if (err < 0) {
2197 dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2198 return err;
2199 }
2200 }
2201
2202 return 0;
2203}
2204
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002205/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002206 * snd_soc_add_codec_controls - add an array of controls to a codec.
2207 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002208 * duplicating this code.
2209 *
2210 * @codec: codec to add controls to
2211 * @controls: array of controls to add
2212 * @num_controls: number of elements in the array
2213 *
2214 * Return 0 for success, else error.
2215 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002216int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002217 const struct snd_kcontrol_new *controls, int num_controls)
2218{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002219 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002220
Liam Girdwood022658b2012-02-03 17:43:09 +00002221 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2222 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002223}
Liam Girdwood022658b2012-02-03 17:43:09 +00002224EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002225
2226/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002227 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002228 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002229 *
2230 * @platform: platform to add controls to
2231 * @controls: array of controls to add
2232 * @num_controls: number of elements in the array
2233 *
2234 * Return 0 for success, else error.
2235 */
2236int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2237 const struct snd_kcontrol_new *controls, int num_controls)
2238{
2239 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002240
Liam Girdwood022658b2012-02-03 17:43:09 +00002241 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2242 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002243}
2244EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2245
2246/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002247 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2248 * Convenience function to add a list of controls.
2249 *
2250 * @soc_card: SoC card to add controls to
2251 * @controls: array of controls to add
2252 * @num_controls: number of elements in the array
2253 *
2254 * Return 0 for success, else error.
2255 */
2256int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2257 const struct snd_kcontrol_new *controls, int num_controls)
2258{
2259 struct snd_card *card = soc_card->snd_card;
2260
2261 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2262 NULL, soc_card);
2263}
2264EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2265
2266/**
2267 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2268 * Convienience function to add a list of controls.
2269 *
2270 * @dai: DAI to add controls to
2271 * @controls: array of controls to add
2272 * @num_controls: number of elements in the array
2273 *
2274 * Return 0 for success, else error.
2275 */
2276int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2277 const struct snd_kcontrol_new *controls, int num_controls)
2278{
2279 struct snd_card *card = dai->card->snd_card;
2280
2281 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2282 NULL, dai);
2283}
2284EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2285
2286/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002287 * snd_soc_info_enum_double - enumerated double mixer info callback
2288 * @kcontrol: mixer control
2289 * @uinfo: control element information
2290 *
2291 * Callback to provide information about a double enumerated
2292 * mixer control.
2293 *
2294 * Returns 0 for success.
2295 */
2296int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2297 struct snd_ctl_elem_info *uinfo)
2298{
2299 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2300
2301 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2302 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002303 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002304
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002305 if (uinfo->value.enumerated.item > e->max - 1)
2306 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002307 strcpy(uinfo->value.enumerated.name,
2308 e->texts[uinfo->value.enumerated.item]);
2309 return 0;
2310}
2311EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2312
2313/**
2314 * snd_soc_get_enum_double - enumerated double mixer get callback
2315 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002316 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002317 *
2318 * Callback to get the value of a double enumerated mixer.
2319 *
2320 * Returns 0 for success.
2321 */
2322int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2323 struct snd_ctl_elem_value *ucontrol)
2324{
2325 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2326 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002327 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002328
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002329 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002330 ;
2331 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002332 ucontrol->value.enumerated.item[0]
2333 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002334 if (e->shift_l != e->shift_r)
2335 ucontrol->value.enumerated.item[1] =
2336 (val >> e->shift_r) & (bitmask - 1);
2337
2338 return 0;
2339}
2340EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2341
2342/**
2343 * snd_soc_put_enum_double - enumerated double mixer put callback
2344 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002345 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002346 *
2347 * Callback to set the value of a double enumerated mixer.
2348 *
2349 * Returns 0 for success.
2350 */
2351int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2352 struct snd_ctl_elem_value *ucontrol)
2353{
2354 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2355 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002356 unsigned int val;
2357 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002358
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002359 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002360 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002361 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002362 return -EINVAL;
2363 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2364 mask = (bitmask - 1) << e->shift_l;
2365 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002366 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002367 return -EINVAL;
2368 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2369 mask |= (bitmask - 1) << e->shift_r;
2370 }
2371
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002372 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002373}
2374EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2375
2376/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002377 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2378 * @kcontrol: mixer control
2379 * @ucontrol: control element information
2380 *
2381 * Callback to get the value of a double semi enumerated mixer.
2382 *
2383 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2384 * used for handling bitfield coded enumeration for example.
2385 *
2386 * Returns 0 for success.
2387 */
2388int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2389 struct snd_ctl_elem_value *ucontrol)
2390{
2391 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002392 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002393 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002394
2395 reg_val = snd_soc_read(codec, e->reg);
2396 val = (reg_val >> e->shift_l) & e->mask;
2397 for (mux = 0; mux < e->max; mux++) {
2398 if (val == e->values[mux])
2399 break;
2400 }
2401 ucontrol->value.enumerated.item[0] = mux;
2402 if (e->shift_l != e->shift_r) {
2403 val = (reg_val >> e->shift_r) & e->mask;
2404 for (mux = 0; mux < e->max; mux++) {
2405 if (val == e->values[mux])
2406 break;
2407 }
2408 ucontrol->value.enumerated.item[1] = mux;
2409 }
2410
2411 return 0;
2412}
2413EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2414
2415/**
2416 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2417 * @kcontrol: mixer control
2418 * @ucontrol: control element information
2419 *
2420 * Callback to set the value of a double semi enumerated mixer.
2421 *
2422 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2423 * used for handling bitfield coded enumeration for example.
2424 *
2425 * Returns 0 for success.
2426 */
2427int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2428 struct snd_ctl_elem_value *ucontrol)
2429{
2430 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002431 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002432 unsigned int val;
2433 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002434
2435 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2436 return -EINVAL;
2437 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2438 mask = e->mask << e->shift_l;
2439 if (e->shift_l != e->shift_r) {
2440 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2441 return -EINVAL;
2442 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2443 mask |= e->mask << e->shift_r;
2444 }
2445
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002446 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002447}
2448EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2449
2450/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002451 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2452 * @kcontrol: mixer control
2453 * @uinfo: control element information
2454 *
2455 * Callback to provide information about an external enumerated
2456 * single mixer.
2457 *
2458 * Returns 0 for success.
2459 */
2460int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2461 struct snd_ctl_elem_info *uinfo)
2462{
2463 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2464
2465 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2466 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002467 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002468
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002469 if (uinfo->value.enumerated.item > e->max - 1)
2470 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002471 strcpy(uinfo->value.enumerated.name,
2472 e->texts[uinfo->value.enumerated.item]);
2473 return 0;
2474}
2475EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2476
2477/**
2478 * snd_soc_info_volsw_ext - external single mixer info callback
2479 * @kcontrol: mixer control
2480 * @uinfo: control element information
2481 *
2482 * Callback to provide information about a single external mixer control.
2483 *
2484 * Returns 0 for success.
2485 */
2486int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2487 struct snd_ctl_elem_info *uinfo)
2488{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002489 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002490
Mark Brownfd5dfad2009-04-15 21:37:46 +01002491 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002492 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2493 else
2494 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2495
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002496 uinfo->count = 1;
2497 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002498 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002499 return 0;
2500}
2501EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2502
2503/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002504 * snd_soc_info_volsw - single mixer info callback
2505 * @kcontrol: mixer control
2506 * @uinfo: control element information
2507 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002508 * Callback to provide information about a single mixer control, or a double
2509 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002510 *
2511 * Returns 0 for success.
2512 */
2513int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2514 struct snd_ctl_elem_info *uinfo)
2515{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002516 struct soc_mixer_control *mc =
2517 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002518 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002519
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002520 if (!mc->platform_max)
2521 mc->platform_max = mc->max;
2522 platform_max = mc->platform_max;
2523
2524 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002525 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2526 else
2527 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2528
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002529 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002530 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002531 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002532 return 0;
2533}
2534EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2535
2536/**
2537 * snd_soc_get_volsw - single mixer get callback
2538 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002539 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002540 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002541 * Callback to get the value of a single mixer control, or a double mixer
2542 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002543 *
2544 * Returns 0 for success.
2545 */
2546int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2547 struct snd_ctl_elem_value *ucontrol)
2548{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002549 struct soc_mixer_control *mc =
2550 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002551 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002552 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002553 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002554 unsigned int shift = mc->shift;
2555 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002556 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002557 unsigned int mask = (1 << fls(max)) - 1;
2558 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002559
2560 ucontrol->value.integer.value[0] =
2561 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002562 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002563 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002564 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002565
2566 if (snd_soc_volsw_is_stereo(mc)) {
2567 if (reg == reg2)
2568 ucontrol->value.integer.value[1] =
2569 (snd_soc_read(codec, reg) >> rshift) & mask;
2570 else
2571 ucontrol->value.integer.value[1] =
2572 (snd_soc_read(codec, reg2) >> shift) & mask;
2573 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002574 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002575 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002576 }
2577
2578 return 0;
2579}
2580EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2581
2582/**
2583 * snd_soc_put_volsw - single mixer put callback
2584 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002585 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002586 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002587 * Callback to set the value of a single mixer control, or a double mixer
2588 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002589 *
2590 * Returns 0 for success.
2591 */
2592int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2593 struct snd_ctl_elem_value *ucontrol)
2594{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002595 struct soc_mixer_control *mc =
2596 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002597 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002598 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002599 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002600 unsigned int shift = mc->shift;
2601 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002602 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002603 unsigned int mask = (1 << fls(max)) - 1;
2604 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002605 int err;
2606 bool type_2r = 0;
2607 unsigned int val2 = 0;
2608 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002609
2610 val = (ucontrol->value.integer.value[0] & mask);
2611 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002612 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002613 val_mask = mask << shift;
2614 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002615 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002616 val2 = (ucontrol->value.integer.value[1] & mask);
2617 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002618 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002619 if (reg == reg2) {
2620 val_mask |= mask << rshift;
2621 val |= val2 << rshift;
2622 } else {
2623 val2 = val2 << shift;
2624 type_2r = 1;
2625 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002626 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002627 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002628 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002629 return err;
2630
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002631 if (type_2r)
2632 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2633
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002634 return err;
2635}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002636EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002637
Mark Browne13ac2e2008-05-28 17:58:05 +01002638/**
Brian Austin1d99f242012-03-30 10:43:55 -05002639 * snd_soc_get_volsw_sx - single mixer get callback
2640 * @kcontrol: mixer control
2641 * @ucontrol: control element information
2642 *
2643 * Callback to get the value of a single mixer control, or a double mixer
2644 * control that spans 2 registers.
2645 *
2646 * Returns 0 for success.
2647 */
2648int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2649 struct snd_ctl_elem_value *ucontrol)
2650{
2651 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2652 struct soc_mixer_control *mc =
2653 (struct soc_mixer_control *)kcontrol->private_value;
2654
2655 unsigned int reg = mc->reg;
2656 unsigned int reg2 = mc->rreg;
2657 unsigned int shift = mc->shift;
2658 unsigned int rshift = mc->rshift;
2659 int max = mc->max;
2660 int min = mc->min;
2661 int mask = (1 << (fls(min + max) - 1)) - 1;
2662
2663 ucontrol->value.integer.value[0] =
2664 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2665
2666 if (snd_soc_volsw_is_stereo(mc))
2667 ucontrol->value.integer.value[1] =
2668 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2669
2670 return 0;
2671}
2672EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2673
2674/**
2675 * snd_soc_put_volsw_sx - double mixer set callback
2676 * @kcontrol: mixer control
2677 * @uinfo: control element information
2678 *
2679 * Callback to set the value of a double mixer control that spans 2 registers.
2680 *
2681 * Returns 0 for success.
2682 */
2683int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2684 struct snd_ctl_elem_value *ucontrol)
2685{
2686 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2687 struct soc_mixer_control *mc =
2688 (struct soc_mixer_control *)kcontrol->private_value;
2689
2690 unsigned int reg = mc->reg;
2691 unsigned int reg2 = mc->rreg;
2692 unsigned int shift = mc->shift;
2693 unsigned int rshift = mc->rshift;
2694 int max = mc->max;
2695 int min = mc->min;
2696 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002697 int err = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002698 unsigned short val, val_mask, val2 = 0;
2699
2700 val_mask = mask << shift;
2701 val = (ucontrol->value.integer.value[0] + min) & mask;
2702 val = val << shift;
2703
2704 if (snd_soc_update_bits_locked(codec, reg, val_mask, val))
2705 return err;
2706
2707 if (snd_soc_volsw_is_stereo(mc)) {
2708 val_mask = mask << rshift;
2709 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2710 val2 = val2 << rshift;
2711
2712 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2713 return err;
2714 }
2715 return 0;
2716}
2717EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2718
2719/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002720 * snd_soc_info_volsw_s8 - signed mixer info callback
2721 * @kcontrol: mixer control
2722 * @uinfo: control element information
2723 *
2724 * Callback to provide information about a signed mixer control.
2725 *
2726 * Returns 0 for success.
2727 */
2728int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2729 struct snd_ctl_elem_info *uinfo)
2730{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002731 struct soc_mixer_control *mc =
2732 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002733 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002734 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002735
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002736 if (!mc->platform_max)
2737 mc->platform_max = mc->max;
2738 platform_max = mc->platform_max;
2739
Mark Browne13ac2e2008-05-28 17:58:05 +01002740 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2741 uinfo->count = 2;
2742 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002743 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002744 return 0;
2745}
2746EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2747
2748/**
2749 * snd_soc_get_volsw_s8 - signed mixer get callback
2750 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002751 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002752 *
2753 * Callback to get the value of a signed mixer control.
2754 *
2755 * Returns 0 for success.
2756 */
2757int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2758 struct snd_ctl_elem_value *ucontrol)
2759{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002760 struct soc_mixer_control *mc =
2761 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002762 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002763 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002764 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002765 int val = snd_soc_read(codec, reg);
2766
2767 ucontrol->value.integer.value[0] =
2768 ((signed char)(val & 0xff))-min;
2769 ucontrol->value.integer.value[1] =
2770 ((signed char)((val >> 8) & 0xff))-min;
2771 return 0;
2772}
2773EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2774
2775/**
2776 * snd_soc_put_volsw_sgn - signed mixer put callback
2777 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002778 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002779 *
2780 * Callback to set the value of a signed mixer control.
2781 *
2782 * Returns 0 for success.
2783 */
2784int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2785 struct snd_ctl_elem_value *ucontrol)
2786{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002787 struct soc_mixer_control *mc =
2788 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002789 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002790 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002791 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002792 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002793
2794 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2795 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2796
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002797 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002798}
2799EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2800
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002801/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002802 * snd_soc_info_volsw_range - single mixer info callback with range.
2803 * @kcontrol: mixer control
2804 * @uinfo: control element information
2805 *
2806 * Callback to provide information, within a range, about a single
2807 * mixer control.
2808 *
2809 * returns 0 for success.
2810 */
2811int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2812 struct snd_ctl_elem_info *uinfo)
2813{
2814 struct soc_mixer_control *mc =
2815 (struct soc_mixer_control *)kcontrol->private_value;
2816 int platform_max;
2817 int min = mc->min;
2818
2819 if (!mc->platform_max)
2820 mc->platform_max = mc->max;
2821 platform_max = mc->platform_max;
2822
2823 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2824 uinfo->count = 1;
2825 uinfo->value.integer.min = 0;
2826 uinfo->value.integer.max = platform_max - min;
2827
2828 return 0;
2829}
2830EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2831
2832/**
2833 * snd_soc_put_volsw_range - single mixer put value callback with range.
2834 * @kcontrol: mixer control
2835 * @ucontrol: control element information
2836 *
2837 * Callback to set the value, within a range, for a single mixer control.
2838 *
2839 * Returns 0 for success.
2840 */
2841int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2842 struct snd_ctl_elem_value *ucontrol)
2843{
2844 struct soc_mixer_control *mc =
2845 (struct soc_mixer_control *)kcontrol->private_value;
2846 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2847 unsigned int reg = mc->reg;
2848 unsigned int shift = mc->shift;
2849 int min = mc->min;
2850 int max = mc->max;
2851 unsigned int mask = (1 << fls(max)) - 1;
2852 unsigned int invert = mc->invert;
2853 unsigned int val, val_mask;
2854
2855 val = ((ucontrol->value.integer.value[0] + min) & mask);
2856 if (invert)
2857 val = max - val;
2858 val_mask = mask << shift;
2859 val = val << shift;
2860
2861 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2862}
2863EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2864
2865/**
2866 * snd_soc_get_volsw_range - single mixer get callback with range
2867 * @kcontrol: mixer control
2868 * @ucontrol: control element information
2869 *
2870 * Callback to get the value, within a range, of a single mixer control.
2871 *
2872 * Returns 0 for success.
2873 */
2874int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2875 struct snd_ctl_elem_value *ucontrol)
2876{
2877 struct soc_mixer_control *mc =
2878 (struct soc_mixer_control *)kcontrol->private_value;
2879 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2880 unsigned int reg = mc->reg;
2881 unsigned int shift = mc->shift;
2882 int min = mc->min;
2883 int max = mc->max;
2884 unsigned int mask = (1 << fls(max)) - 1;
2885 unsigned int invert = mc->invert;
2886
2887 ucontrol->value.integer.value[0] =
2888 (snd_soc_read(codec, reg) >> shift) & mask;
2889 if (invert)
2890 ucontrol->value.integer.value[0] =
2891 max - ucontrol->value.integer.value[0];
2892 ucontrol->value.integer.value[0] =
2893 ucontrol->value.integer.value[0] - min;
2894
2895 return 0;
2896}
2897EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2898
2899/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002900 * snd_soc_limit_volume - Set new limit to an existing volume control.
2901 *
2902 * @codec: where to look for the control
2903 * @name: Name of the control
2904 * @max: new maximum limit
2905 *
2906 * Return 0 for success, else error.
2907 */
2908int snd_soc_limit_volume(struct snd_soc_codec *codec,
2909 const char *name, int max)
2910{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002911 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002912 struct snd_kcontrol *kctl;
2913 struct soc_mixer_control *mc;
2914 int found = 0;
2915 int ret = -EINVAL;
2916
2917 /* Sanity check for name and max */
2918 if (unlikely(!name || max <= 0))
2919 return -EINVAL;
2920
2921 list_for_each_entry(kctl, &card->controls, list) {
2922 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2923 found = 1;
2924 break;
2925 }
2926 }
2927 if (found) {
2928 mc = (struct soc_mixer_control *)kctl->private_value;
2929 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002930 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002931 ret = 0;
2932 }
2933 }
2934 return ret;
2935}
2936EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2937
Mark Brown71d08512011-10-10 18:31:26 +01002938int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2939 struct snd_ctl_elem_info *uinfo)
2940{
2941 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2942 struct soc_bytes *params = (void *)kcontrol->private_value;
2943
2944 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2945 uinfo->count = params->num_regs * codec->val_bytes;
2946
2947 return 0;
2948}
2949EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
2950
2951int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
2952 struct snd_ctl_elem_value *ucontrol)
2953{
2954 struct soc_bytes *params = (void *)kcontrol->private_value;
2955 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2956 int ret;
2957
2958 if (codec->using_regmap)
2959 ret = regmap_raw_read(codec->control_data, params->base,
2960 ucontrol->value.bytes.data,
2961 params->num_regs * codec->val_bytes);
2962 else
2963 ret = -EINVAL;
2964
Mark Brownf831b052012-02-17 16:20:33 -08002965 /* Hide any masked bytes to ensure consistent data reporting */
2966 if (ret == 0 && params->mask) {
2967 switch (codec->val_bytes) {
2968 case 1:
2969 ucontrol->value.bytes.data[0] &= ~params->mask;
2970 break;
2971 case 2:
2972 ((u16 *)(&ucontrol->value.bytes.data))[0]
2973 &= ~params->mask;
2974 break;
2975 case 4:
2976 ((u32 *)(&ucontrol->value.bytes.data))[0]
2977 &= ~params->mask;
2978 break;
2979 default:
2980 return -EINVAL;
2981 }
2982 }
2983
Mark Brown71d08512011-10-10 18:31:26 +01002984 return ret;
2985}
2986EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
2987
2988int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
2989 struct snd_ctl_elem_value *ucontrol)
2990{
2991 struct soc_bytes *params = (void *)kcontrol->private_value;
2992 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08002993 int ret, len;
2994 unsigned int val;
2995 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01002996
Mark Brownf831b052012-02-17 16:20:33 -08002997 if (!codec->using_regmap)
2998 return -EINVAL;
2999
3000 data = ucontrol->value.bytes.data;
3001 len = params->num_regs * codec->val_bytes;
3002
3003 /*
3004 * If we've got a mask then we need to preserve the register
3005 * bits. We shouldn't modify the incoming data so take a
3006 * copy.
3007 */
3008 if (params->mask) {
3009 ret = regmap_read(codec->control_data, params->base, &val);
3010 if (ret != 0)
3011 return ret;
3012
3013 val &= params->mask;
3014
3015 data = kmemdup(data, len, GFP_KERNEL);
3016 if (!data)
3017 return -ENOMEM;
3018
3019 switch (codec->val_bytes) {
3020 case 1:
3021 ((u8 *)data)[0] &= ~params->mask;
3022 ((u8 *)data)[0] |= val;
3023 break;
3024 case 2:
3025 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3026 ((u16 *)data)[0] |= cpu_to_be16(val);
3027 break;
3028 case 4:
3029 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3030 ((u32 *)data)[0] |= cpu_to_be32(val);
3031 break;
3032 default:
3033 return -EINVAL;
3034 }
3035 }
3036
3037 ret = regmap_raw_write(codec->control_data, params->base,
3038 data, len);
3039
3040 if (params->mask)
3041 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003042
3043 return ret;
3044}
3045EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3046
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003047/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003048 * snd_soc_info_xr_sx - signed multi register info callback
3049 * @kcontrol: mreg control
3050 * @uinfo: control element information
3051 *
3052 * Callback to provide information of a control that can
3053 * span multiple codec registers which together
3054 * forms a single signed value in a MSB/LSB manner.
3055 *
3056 * Returns 0 for success.
3057 */
3058int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3059 struct snd_ctl_elem_info *uinfo)
3060{
3061 struct soc_mreg_control *mc =
3062 (struct soc_mreg_control *)kcontrol->private_value;
3063 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3064 uinfo->count = 1;
3065 uinfo->value.integer.min = mc->min;
3066 uinfo->value.integer.max = mc->max;
3067
3068 return 0;
3069}
3070EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3071
3072/**
3073 * snd_soc_get_xr_sx - signed multi register get callback
3074 * @kcontrol: mreg control
3075 * @ucontrol: control element information
3076 *
3077 * Callback to get the value of a control that can span
3078 * multiple codec registers which together forms a single
3079 * signed value in a MSB/LSB manner. The control supports
3080 * specifying total no of bits used to allow for bitfields
3081 * across the multiple codec registers.
3082 *
3083 * Returns 0 for success.
3084 */
3085int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3086 struct snd_ctl_elem_value *ucontrol)
3087{
3088 struct soc_mreg_control *mc =
3089 (struct soc_mreg_control *)kcontrol->private_value;
3090 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3091 unsigned int regbase = mc->regbase;
3092 unsigned int regcount = mc->regcount;
3093 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3094 unsigned int regwmask = (1<<regwshift)-1;
3095 unsigned int invert = mc->invert;
3096 unsigned long mask = (1UL<<mc->nbits)-1;
3097 long min = mc->min;
3098 long max = mc->max;
3099 long val = 0;
3100 unsigned long regval;
3101 unsigned int i;
3102
3103 for (i = 0; i < regcount; i++) {
3104 regval = snd_soc_read(codec, regbase+i) & regwmask;
3105 val |= regval << (regwshift*(regcount-i-1));
3106 }
3107 val &= mask;
3108 if (min < 0 && val > max)
3109 val |= ~mask;
3110 if (invert)
3111 val = max - val;
3112 ucontrol->value.integer.value[0] = val;
3113
3114 return 0;
3115}
3116EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3117
3118/**
3119 * snd_soc_put_xr_sx - signed multi register get callback
3120 * @kcontrol: mreg control
3121 * @ucontrol: control element information
3122 *
3123 * Callback to set the value of a control that can span
3124 * multiple codec registers which together forms a single
3125 * signed value in a MSB/LSB manner. The control supports
3126 * specifying total no of bits used to allow for bitfields
3127 * across the multiple codec registers.
3128 *
3129 * Returns 0 for success.
3130 */
3131int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3132 struct snd_ctl_elem_value *ucontrol)
3133{
3134 struct soc_mreg_control *mc =
3135 (struct soc_mreg_control *)kcontrol->private_value;
3136 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3137 unsigned int regbase = mc->regbase;
3138 unsigned int regcount = mc->regcount;
3139 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3140 unsigned int regwmask = (1<<regwshift)-1;
3141 unsigned int invert = mc->invert;
3142 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003143 long max = mc->max;
3144 long val = ucontrol->value.integer.value[0];
3145 unsigned int i, regval, regmask;
3146 int err;
3147
3148 if (invert)
3149 val = max - val;
3150 val &= mask;
3151 for (i = 0; i < regcount; i++) {
3152 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3153 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3154 err = snd_soc_update_bits_locked(codec, regbase+i,
3155 regmask, regval);
3156 if (err < 0)
3157 return err;
3158 }
3159
3160 return 0;
3161}
3162EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3163
3164/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003165 * snd_soc_get_strobe - strobe get callback
3166 * @kcontrol: mixer control
3167 * @ucontrol: control element information
3168 *
3169 * Callback get the value of a strobe mixer control.
3170 *
3171 * Returns 0 for success.
3172 */
3173int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3174 struct snd_ctl_elem_value *ucontrol)
3175{
3176 struct soc_mixer_control *mc =
3177 (struct soc_mixer_control *)kcontrol->private_value;
3178 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3179 unsigned int reg = mc->reg;
3180 unsigned int shift = mc->shift;
3181 unsigned int mask = 1 << shift;
3182 unsigned int invert = mc->invert != 0;
3183 unsigned int val = snd_soc_read(codec, reg) & mask;
3184
3185 if (shift != 0 && val != 0)
3186 val = val >> shift;
3187 ucontrol->value.enumerated.item[0] = val ^ invert;
3188
3189 return 0;
3190}
3191EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3192
3193/**
3194 * snd_soc_put_strobe - strobe put callback
3195 * @kcontrol: mixer control
3196 * @ucontrol: control element information
3197 *
3198 * Callback strobe a register bit to high then low (or the inverse)
3199 * in one pass of a single mixer enum control.
3200 *
3201 * Returns 1 for success.
3202 */
3203int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3204 struct snd_ctl_elem_value *ucontrol)
3205{
3206 struct soc_mixer_control *mc =
3207 (struct soc_mixer_control *)kcontrol->private_value;
3208 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3209 unsigned int reg = mc->reg;
3210 unsigned int shift = mc->shift;
3211 unsigned int mask = 1 << shift;
3212 unsigned int invert = mc->invert != 0;
3213 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3214 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3215 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3216 int err;
3217
3218 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3219 if (err < 0)
3220 return err;
3221
3222 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3223 return err;
3224}
3225EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3226
3227/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003228 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3229 * @dai: DAI
3230 * @clk_id: DAI specific clock ID
3231 * @freq: new clock frequency in Hz
3232 * @dir: new clock direction - input/output.
3233 *
3234 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3235 */
3236int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3237 unsigned int freq, int dir)
3238{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003239 if (dai->driver && dai->driver->ops->set_sysclk)
3240 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003241 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003242 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003243 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003244 else
3245 return -EINVAL;
3246}
3247EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3248
3249/**
Mark Brownec4ee522011-03-07 20:58:11 +00003250 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3251 * @codec: CODEC
3252 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003253 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003254 * @freq: new clock frequency in Hz
3255 * @dir: new clock direction - input/output.
3256 *
3257 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3258 */
3259int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003260 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003261{
3262 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003263 return codec->driver->set_sysclk(codec, clk_id, source,
3264 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003265 else
3266 return -EINVAL;
3267}
3268EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3269
3270/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003271 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3272 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003273 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003274 * @div: new clock divisor.
3275 *
3276 * Configures the clock dividers. This is used to derive the best DAI bit and
3277 * frame clocks from the system or master clock. It's best to set the DAI bit
3278 * and frame clocks as low as possible to save system power.
3279 */
3280int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3281 int div_id, int div)
3282{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003283 if (dai->driver && dai->driver->ops->set_clkdiv)
3284 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003285 else
3286 return -EINVAL;
3287}
3288EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3289
3290/**
3291 * snd_soc_dai_set_pll - configure DAI PLL.
3292 * @dai: DAI
3293 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003294 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003295 * @freq_in: PLL input clock frequency in Hz
3296 * @freq_out: requested PLL output clock frequency in Hz
3297 *
3298 * Configures and enables PLL to generate output clock based on input clock.
3299 */
Mark Brown85488032009-09-05 18:52:16 +01003300int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3301 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003302{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003303 if (dai->driver && dai->driver->ops->set_pll)
3304 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003305 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003306 else if (dai->codec && dai->codec->driver->set_pll)
3307 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3308 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003309 else
3310 return -EINVAL;
3311}
3312EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3313
Mark Brownec4ee522011-03-07 20:58:11 +00003314/*
3315 * snd_soc_codec_set_pll - configure codec PLL.
3316 * @codec: CODEC
3317 * @pll_id: DAI specific PLL ID
3318 * @source: DAI specific source for the PLL
3319 * @freq_in: PLL input clock frequency in Hz
3320 * @freq_out: requested PLL output clock frequency in Hz
3321 *
3322 * Configures and enables PLL to generate output clock based on input clock.
3323 */
3324int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3325 unsigned int freq_in, unsigned int freq_out)
3326{
3327 if (codec->driver->set_pll)
3328 return codec->driver->set_pll(codec, pll_id, source,
3329 freq_in, freq_out);
3330 else
3331 return -EINVAL;
3332}
3333EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3334
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003335/**
3336 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3337 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003338 * @fmt: SND_SOC_DAIFMT_ format value.
3339 *
3340 * Configures the DAI hardware format and clocking.
3341 */
3342int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3343{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003344 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003345 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003346 if (dai->driver->ops->set_fmt == NULL)
3347 return -ENOTSUPP;
3348 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003349}
3350EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3351
3352/**
3353 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3354 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003355 * @tx_mask: bitmask representing active TX slots.
3356 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003357 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003358 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003359 *
3360 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3361 * specific.
3362 */
3363int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003364 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003365{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003366 if (dai->driver && dai->driver->ops->set_tdm_slot)
3367 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003368 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003369 else
3370 return -EINVAL;
3371}
3372EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3373
3374/**
Barry Song472df3c2009-09-12 01:16:29 +08003375 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3376 * @dai: DAI
3377 * @tx_num: how many TX channels
3378 * @tx_slot: pointer to an array which imply the TX slot number channel
3379 * 0~num-1 uses
3380 * @rx_num: how many RX channels
3381 * @rx_slot: pointer to an array which imply the RX slot number channel
3382 * 0~num-1 uses
3383 *
3384 * configure the relationship between channel number and TDM slot number.
3385 */
3386int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3387 unsigned int tx_num, unsigned int *tx_slot,
3388 unsigned int rx_num, unsigned int *rx_slot)
3389{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003390 if (dai->driver && dai->driver->ops->set_channel_map)
3391 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003392 rx_num, rx_slot);
3393 else
3394 return -EINVAL;
3395}
3396EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3397
3398/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003399 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3400 * @dai: DAI
3401 * @tristate: tristate enable
3402 *
3403 * Tristates the DAI so that others can use it.
3404 */
3405int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3406{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003407 if (dai->driver && dai->driver->ops->set_tristate)
3408 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003409 else
3410 return -EINVAL;
3411}
3412EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3413
3414/**
3415 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3416 * @dai: DAI
3417 * @mute: mute enable
3418 *
3419 * Mutes the DAI DAC.
3420 */
3421int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3422{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003423 if (dai->driver && dai->driver->ops->digital_mute)
3424 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003425 else
Mark Brown04570c62012-04-13 19:16:03 +01003426 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003427}
3428EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3429
Mark Brownc5af3a22008-11-28 13:29:45 +00003430/**
3431 * snd_soc_register_card - Register a card with the ASoC core
3432 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003433 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003434 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003435 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303436int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003437{
Mark Brownb19e6e72012-03-14 21:18:39 +00003438 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003439
Mark Brownc5af3a22008-11-28 13:29:45 +00003440 if (!card->name || !card->dev)
3441 return -EINVAL;
3442
Stephen Warren5a504962011-12-21 10:40:59 -07003443 for (i = 0; i < card->num_links; i++) {
3444 struct snd_soc_dai_link *link = &card->dai_link[i];
3445
3446 /*
3447 * Codec must be specified by 1 of name or OF node,
3448 * not both or neither.
3449 */
3450 if (!!link->codec_name == !!link->codec_of_node) {
3451 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003452 "Neither/both codec name/of_node are set for %s\n",
3453 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003454 return -EINVAL;
3455 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003456 /* Codec DAI name must be specified */
3457 if (!link->codec_dai_name) {
3458 dev_err(card->dev, "codec_dai_name not set for %s\n",
3459 link->name);
3460 return -EINVAL;
3461 }
Stephen Warren5a504962011-12-21 10:40:59 -07003462
3463 /*
3464 * Platform may be specified by either name or OF node, but
3465 * can be left unspecified, and a dummy platform will be used.
3466 */
3467 if (link->platform_name && link->platform_of_node) {
3468 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003469 "Both platform name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003470 return -EINVAL;
3471 }
3472
3473 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003474 * CPU device may be specified by either name or OF node, but
3475 * can be left unspecified, and will be matched based on DAI
3476 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003477 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003478 if (link->cpu_name && link->cpu_of_node) {
Stephen Warren5a504962011-12-21 10:40:59 -07003479 dev_err(card->dev,
Stephen Warrenbc926572012-05-25 18:22:11 -06003480 "Neither/both cpu name/of_node are set for %s\n",
3481 link->name);
3482 return -EINVAL;
3483 }
3484 /*
3485 * At least one of CPU DAI name or CPU device name/node must be
3486 * specified
3487 */
3488 if (!link->cpu_dai_name &&
3489 !(link->cpu_name || link->cpu_of_node)) {
3490 dev_err(card->dev,
3491 "Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003492 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003493 return -EINVAL;
3494 }
3495 }
3496
Mark Browned77cc12011-05-03 18:25:34 +01003497 dev_set_drvdata(card->dev, card);
3498
Stephen Warren111c6412011-01-28 14:26:35 -07003499 snd_soc_initialize_card_lists(card);
3500
Vinod Koul150dd2f2011-01-13 22:48:32 +05303501 soc_init_card_debugfs(card);
3502
Mark Brown181a6892012-03-14 20:18:49 +00003503 card->rtd = devm_kzalloc(card->dev,
3504 sizeof(struct snd_soc_pcm_runtime) *
3505 (card->num_links + card->num_aux_devs),
3506 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003507 if (card->rtd == NULL)
3508 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003509 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003510 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003511
3512 for (i = 0; i < card->num_links; i++)
3513 card->rtd[i].dai_link = &card->dai_link[i];
3514
Mark Brownc5af3a22008-11-28 13:29:45 +00003515 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003516 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003517 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003518 mutex_init(&card->mutex);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003519 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003520
Mark Brownb19e6e72012-03-14 21:18:39 +00003521 ret = snd_soc_instantiate_card(card);
3522 if (ret != 0)
3523 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003524
Mark Brownb19e6e72012-03-14 21:18:39 +00003525 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003526}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303527EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003528
3529/**
3530 * snd_soc_unregister_card - Unregister a card with the ASoC core
3531 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003532 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003533 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003534 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303535int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003536{
Vinod Koulb0e26482011-01-13 22:48:02 +05303537 if (card->instantiated)
3538 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003539 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3540
3541 return 0;
3542}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303543EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003544
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003545/*
3546 * Simplify DAI link configuration by removing ".-1" from device names
3547 * and sanitizing names.
3548 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003549static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003550{
3551 char *found, name[NAME_SIZE];
3552 int id1, id2;
3553
3554 if (dev_name(dev) == NULL)
3555 return NULL;
3556
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003557 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003558
3559 /* are we a "%s.%d" name (platform and SPI components) */
3560 found = strstr(name, dev->driver->name);
3561 if (found) {
3562 /* get ID */
3563 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3564
3565 /* discard ID from name if ID == -1 */
3566 if (*id == -1)
3567 found[strlen(dev->driver->name)] = '\0';
3568 }
3569
3570 } else {
3571 /* I2C component devices are named "bus-addr" */
3572 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3573 char tmp[NAME_SIZE];
3574
3575 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003576 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003577
3578 /* sanitize component name for DAI link creation */
3579 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003580 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003581 } else
3582 *id = 0;
3583 }
3584
3585 return kstrdup(name, GFP_KERNEL);
3586}
3587
3588/*
3589 * Simplify DAI link naming for single devices with multiple DAIs by removing
3590 * any ".-1" and using the DAI name (instead of device name).
3591 */
3592static inline char *fmt_multiple_name(struct device *dev,
3593 struct snd_soc_dai_driver *dai_drv)
3594{
3595 if (dai_drv->name == NULL) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02003596 pr_err("asoc: error - multiple DAI %s registered with no name\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003597 dev_name(dev));
3598 return NULL;
3599 }
3600
3601 return kstrdup(dai_drv->name, GFP_KERNEL);
3602}
3603
Mark Brown91151712008-11-30 23:31:24 +00003604/**
3605 * snd_soc_register_dai - Register a DAI with the ASoC core
3606 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003607 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003608 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003609int snd_soc_register_dai(struct device *dev,
3610 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003611{
Mark Brown054880f2012-04-09 17:29:19 +01003612 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003613 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003614
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003615 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003616
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003617 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3618 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003619 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003620
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003621 /* create DAI component name */
3622 dai->name = fmt_single_name(dev, &dai->id);
3623 if (dai->name == NULL) {
3624 kfree(dai);
3625 return -ENOMEM;
3626 }
3627
3628 dai->dev = dev;
3629 dai->driver = dai_drv;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003630 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003631 if (!dai->driver->ops)
3632 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003633
3634 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003635
3636 list_for_each_entry(codec, &codec_list, list) {
3637 if (codec->dev == dev) {
3638 dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3639 dai->name, codec->name);
3640 dai->codec = codec;
3641 break;
3642 }
3643 }
3644
Mark Brown91151712008-11-30 23:31:24 +00003645 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003646
Mark Brown91151712008-11-30 23:31:24 +00003647 mutex_unlock(&client_mutex);
3648
3649 pr_debug("Registered DAI '%s'\n", dai->name);
3650
3651 return 0;
3652}
3653EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3654
3655/**
3656 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3657 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003658 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003659 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003660void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003661{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003662 struct snd_soc_dai *dai;
3663
3664 list_for_each_entry(dai, &dai_list, list) {
3665 if (dev == dai->dev)
3666 goto found;
3667 }
3668 return;
3669
3670found:
Mark Brown91151712008-11-30 23:31:24 +00003671 mutex_lock(&client_mutex);
3672 list_del(&dai->list);
3673 mutex_unlock(&client_mutex);
3674
3675 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003676 kfree(dai->name);
3677 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003678}
3679EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3680
3681/**
3682 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3683 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003684 * @dai: Array of DAIs to register
3685 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003686 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003687int snd_soc_register_dais(struct device *dev,
3688 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003689{
Mark Brown054880f2012-04-09 17:29:19 +01003690 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003691 struct snd_soc_dai *dai;
3692 int i, ret = 0;
3693
Liam Girdwood720ffa42010-08-18 00:30:30 +01003694 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003695
3696 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003697
3698 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003699 if (dai == NULL) {
3700 ret = -ENOMEM;
3701 goto err;
3702 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003703
3704 /* create DAI component name */
3705 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3706 if (dai->name == NULL) {
3707 kfree(dai);
3708 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003709 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003710 }
3711
3712 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003713 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003714 if (dai->driver->id)
3715 dai->id = dai->driver->id;
3716 else
3717 dai->id = i;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003718 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003719 if (!dai->driver->ops)
3720 dai->driver->ops = &null_dai_ops;
3721
3722 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003723
3724 list_for_each_entry(codec, &codec_list, list) {
3725 if (codec->dev == dev) {
3726 dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3727 dai->name, codec->name);
3728 dai->codec = codec;
3729 break;
3730 }
3731 }
3732
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003733 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003734
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003735 mutex_unlock(&client_mutex);
3736
3737 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003738 }
3739
3740 return 0;
3741
3742err:
3743 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003744 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003745
3746 return ret;
3747}
3748EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3749
3750/**
3751 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3752 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003753 * @dai: Array of DAIs to unregister
3754 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003755 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003756void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003757{
3758 int i;
3759
3760 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003761 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003762}
3763EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3764
Mark Brown12a48a8c2008-12-03 19:40:30 +00003765/**
3766 * snd_soc_register_platform - Register a platform with the ASoC core
3767 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003768 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003769 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003770int snd_soc_register_platform(struct device *dev,
3771 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003772{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003773 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003774
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003775 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3776
3777 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3778 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003779 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003780
3781 /* create platform component name */
3782 platform->name = fmt_single_name(dev, &platform->id);
3783 if (platform->name == NULL) {
3784 kfree(platform);
3785 return -ENOMEM;
3786 }
3787
3788 platform->dev = dev;
3789 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003790 platform->dapm.dev = dev;
3791 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003792 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00003793 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003794
3795 mutex_lock(&client_mutex);
3796 list_add(&platform->list, &platform_list);
3797 mutex_unlock(&client_mutex);
3798
3799 pr_debug("Registered platform '%s'\n", platform->name);
3800
3801 return 0;
3802}
3803EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3804
3805/**
3806 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3807 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003808 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003809 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003810void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003811{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003812 struct snd_soc_platform *platform;
3813
3814 list_for_each_entry(platform, &platform_list, list) {
3815 if (dev == platform->dev)
3816 goto found;
3817 }
3818 return;
3819
3820found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003821 mutex_lock(&client_mutex);
3822 list_del(&platform->list);
3823 mutex_unlock(&client_mutex);
3824
3825 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003826 kfree(platform->name);
3827 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003828}
3829EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3830
Mark Brown151ab222009-05-09 16:22:58 +01003831static u64 codec_format_map[] = {
3832 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3833 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3834 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3835 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3836 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3837 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3838 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3839 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3840 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3841 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3842 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3843 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3844 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3845 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3846 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3847 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3848};
3849
3850/* Fix up the DAI formats for endianness: codecs don't actually see
3851 * the endianness of the data but we're using the CPU format
3852 * definitions which do need to include endianness so we ensure that
3853 * codec DAIs always have both big and little endian variants set.
3854 */
3855static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3856{
3857 int i;
3858
3859 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3860 if (stream->formats & codec_format_map[i])
3861 stream->formats |= codec_format_map[i];
3862}
3863
Mark Brown0d0cf002008-12-10 14:32:45 +00003864/**
3865 * snd_soc_register_codec - Register a codec with the ASoC core
3866 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003867 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003868 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003869int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003870 const struct snd_soc_codec_driver *codec_drv,
3871 struct snd_soc_dai_driver *dai_drv,
3872 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003873{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003874 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003875 struct snd_soc_codec *codec;
3876 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003877
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003878 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003879
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003880 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3881 if (codec == NULL)
3882 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003883
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003884 /* create CODEC component name */
3885 codec->name = fmt_single_name(dev, &codec->id);
3886 if (codec->name == NULL) {
3887 kfree(codec);
3888 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003889 }
3890
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003891 if (codec_drv->compress_type)
3892 codec->compress_type = codec_drv->compress_type;
3893 else
3894 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3895
Mark Brownc3acec22010-12-02 16:15:29 +00003896 codec->write = codec_drv->write;
3897 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003898 codec->volatile_register = codec_drv->volatile_register;
3899 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003900 codec->writable_register = codec_drv->writable_register;
Mark Brown5124e692012-02-08 13:20:50 +00003901 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003902 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3903 codec->dapm.dev = dev;
3904 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003905 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003906 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003907 codec->dev = dev;
3908 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003909 codec->num_dai = num_dai;
3910 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003911
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003912 /* allocate CODEC register cache */
3913 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003914 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003915 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003916 /* it is necessary to make a copy of the default register cache
3917 * because in the case of using a compression type that requires
3918 * the default register cache to be marked as __devinitconst the
3919 * kernel might have freed the array by the time we initialize
3920 * the cache.
3921 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003922 if (codec_drv->reg_cache_default) {
3923 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3924 reg_size, GFP_KERNEL);
3925 if (!codec->reg_def_copy) {
3926 ret = -ENOMEM;
3927 goto fail;
3928 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003929 }
3930 }
3931
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003932 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3933 if (!codec->volatile_register)
3934 codec->volatile_register = snd_soc_default_volatile_register;
3935 if (!codec->readable_register)
3936 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003937 if (!codec->writable_register)
3938 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003939 }
3940
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003941 for (i = 0; i < num_dai; i++) {
3942 fixup_codec_formats(&dai_drv[i].playback);
3943 fixup_codec_formats(&dai_drv[i].capture);
3944 }
3945
Mark Brown054880f2012-04-09 17:29:19 +01003946 mutex_lock(&client_mutex);
3947 list_add(&codec->list, &codec_list);
3948 mutex_unlock(&client_mutex);
3949
Mark Brown26b01cc2010-08-18 20:20:55 +01003950 /* register any DAIs */
3951 if (num_dai) {
3952 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3953 if (ret < 0)
Mark Brown054880f2012-04-09 17:29:19 +01003954 dev_err(codec->dev, "Failed to regster DAIs: %d\n",
3955 ret);
Mark Brown26b01cc2010-08-18 20:20:55 +01003956 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003957
Mark Brown0d0cf002008-12-10 14:32:45 +00003958 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003959 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003960
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003961fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003962 kfree(codec->reg_def_copy);
3963 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003964 kfree(codec->name);
3965 kfree(codec);
3966 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003967}
3968EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3969
3970/**
3971 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3972 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003973 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003974 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003975void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003976{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003977 struct snd_soc_codec *codec;
3978 int i;
3979
3980 list_for_each_entry(codec, &codec_list, list) {
3981 if (dev == codec->dev)
3982 goto found;
3983 }
3984 return;
3985
3986found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003987 if (codec->num_dai)
3988 for (i = 0; i < codec->num_dai; i++)
3989 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003990
Mark Brown0d0cf002008-12-10 14:32:45 +00003991 mutex_lock(&client_mutex);
3992 list_del(&codec->list);
3993 mutex_unlock(&client_mutex);
3994
3995 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003996
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003997 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003998 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003999 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004000 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004001}
4002EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4003
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004004/* Retrieve a card's name from device tree */
4005int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4006 const char *propname)
4007{
4008 struct device_node *np = card->dev->of_node;
4009 int ret;
4010
4011 ret = of_property_read_string_index(np, propname, 0, &card->name);
4012 /*
4013 * EINVAL means the property does not exist. This is fine providing
4014 * card->name was previously set, which is checked later in
4015 * snd_soc_register_card.
4016 */
4017 if (ret < 0 && ret != -EINVAL) {
4018 dev_err(card->dev,
4019 "Property '%s' could not be read: %d\n",
4020 propname, ret);
4021 return ret;
4022 }
4023
4024 return 0;
4025}
4026EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4027
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004028int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4029 const char *propname)
4030{
4031 struct device_node *np = card->dev->of_node;
4032 int num_routes;
4033 struct snd_soc_dapm_route *routes;
4034 int i, ret;
4035
4036 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004037 if (num_routes < 0 || num_routes & 1) {
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004038 dev_err(card->dev,
Richard Zhaoc34ce322012-04-24 15:24:43 +08004039 "Property '%s' does not exist or its length is not even\n",
4040 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004041 return -EINVAL;
4042 }
4043 num_routes /= 2;
4044 if (!num_routes) {
4045 dev_err(card->dev,
4046 "Property '%s's length is zero\n",
4047 propname);
4048 return -EINVAL;
4049 }
4050
4051 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4052 GFP_KERNEL);
4053 if (!routes) {
4054 dev_err(card->dev,
4055 "Could not allocate DAPM route table\n");
4056 return -EINVAL;
4057 }
4058
4059 for (i = 0; i < num_routes; i++) {
4060 ret = of_property_read_string_index(np, propname,
4061 2 * i, &routes[i].sink);
4062 if (ret) {
4063 dev_err(card->dev,
4064 "Property '%s' index %d could not be read: %d\n",
4065 propname, 2 * i, ret);
4066 return -EINVAL;
4067 }
4068 ret = of_property_read_string_index(np, propname,
4069 (2 * i) + 1, &routes[i].source);
4070 if (ret) {
4071 dev_err(card->dev,
4072 "Property '%s' index %d could not be read: %d\n",
4073 propname, (2 * i) + 1, ret);
4074 return -EINVAL;
4075 }
4076 }
4077
4078 card->num_dapm_routes = num_routes;
4079 card->dapm_routes = routes;
4080
4081 return 0;
4082}
4083EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4084
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004085static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004086{
Mark Brown384c89e2008-12-03 17:34:03 +00004087#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004088 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4089 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004090 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004091 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004092 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004093
Mark Brown8a9dab12011-01-10 22:25:21 +00004094 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004095 &codec_list_fops))
4096 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4097
Mark Brown8a9dab12011-01-10 22:25:21 +00004098 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004099 &dai_list_fops))
4100 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004101
Mark Brown8a9dab12011-01-10 22:25:21 +00004102 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004103 &platform_list_fops))
4104 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004105#endif
4106
Mark Brownfb257892011-04-28 10:57:54 +01004107 snd_soc_util_init();
4108
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004109 return platform_driver_register(&soc_driver);
4110}
Mark Brown4abe8e12010-10-12 17:41:03 +01004111module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004112
Mark Brown7d8c16a2008-11-30 22:11:24 +00004113static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004114{
Mark Brownfb257892011-04-28 10:57:54 +01004115 snd_soc_util_exit();
4116
Mark Brown384c89e2008-12-03 17:34:03 +00004117#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004118 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004119#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004120 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004121}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004122module_exit(snd_soc_exit);
4123
4124/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004125MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004126MODULE_DESCRIPTION("ALSA SoC Core");
4127MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004128MODULE_ALIAS("platform:soc-audio");