blob: 2b418398ec18c7346253da15978dbd7b9ccd291c [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
Mark Browne2d32ff2012-08-31 17:38:32 -0700612 /* Recheck all analogue paths too */
613 dapm_mark_io_dirty(&card->dapm);
614 snd_soc_dapm_sync(&card->dapm);
615
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000616 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200617 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000618 /* If there are paths active then the CODEC will be held with
619 * bias _ON and should not be suspended. */
620 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200621 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000622 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000623 /*
624 * If the CODEC is capable of idle
625 * bias off then being in STANDBY
626 * means it's doing something,
627 * otherwise fall through.
628 */
629 if (codec->dapm.idle_bias_off) {
630 dev_dbg(codec->dev,
631 "idle_bias_off CODEC on over suspend\n");
632 break;
633 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000634 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100635 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000636 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900637 codec->cache_sync = 1;
Mark Brownda8b8e02012-09-12 12:21:52 +0800638 if (codec->using_regmap)
639 regcache_mark_dirty(codec->control_data);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000640 break;
641 default:
642 dev_dbg(codec->dev, "CODEC is on over suspend\n");
643 break;
644 }
645 }
646 }
647
648 for (i = 0; i < card->num_rtd; i++) {
649 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
650
651 if (card->rtd[i].dai_link->ignore_suspend)
652 continue;
653
654 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
655 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200656 }
657
Mark Brown87506542008-11-18 20:50:34 +0000658 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000659 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200660
661 return 0;
662}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000663EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200664
Andy Green6ed25972008-06-13 16:24:05 +0100665/* deferred resume work, so resume can complete before we finished
666 * setting our codec back up, which can be very slow on I2C
667 */
668static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200669{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000670 struct snd_soc_card *card =
671 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200672 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200673 int i;
674
Andy Green6ed25972008-06-13 16:24:05 +0100675 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
676 * so userspace apps are blocked from touching us
677 */
678
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000679 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100680
Mark Brown9949788b2010-05-07 20:24:05 +0100681 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000682 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown9949788b2010-05-07 20:24:05 +0100683
Mark Brown87506542008-11-18 20:50:34 +0000684 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000685 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200686
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000687 /* resume AC97 DAIs */
688 for (i = 0; i < card->num_rtd; i++) {
689 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100690
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000691 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100692 continue;
693
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000694 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
695 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200696 }
697
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200698 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000699 /* If the CODEC was idle over suspend then it will have been
700 * left with bias OFF or STANDBY and suspended so we must now
701 * resume. Otherwise the suspend was suppressed.
702 */
703 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200704 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000705 case SND_SOC_BIAS_STANDBY:
706 case SND_SOC_BIAS_OFF:
707 codec->driver->resume(codec);
708 codec->suspended = 0;
709 break;
710 default:
711 dev_dbg(codec->dev, "CODEC was on over suspend\n");
712 break;
713 }
Mark Brown1547aba2010-05-07 21:11:40 +0100714 }
715 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200716
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000717 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100718
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000719 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100720 continue;
721
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800722 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000723 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800724 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000725
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800726 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000727 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800728 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200729 }
730
Mark Brown3ff3f642008-05-19 12:32:25 +0200731 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000732 for (i = 0; i < card->num_rtd; i++) {
733 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
734 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100735
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000736 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100737 continue;
738
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000739 if (drv->ops->digital_mute && dai->playback_active)
740 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200741 }
742
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000743 for (i = 0; i < card->num_rtd; i++) {
744 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
745 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100746
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000747 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100748 continue;
749
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000750 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
751 cpu_dai->driver->resume(cpu_dai);
752 if (platform->driver->resume && platform->suspended) {
753 platform->driver->resume(cpu_dai);
754 platform->suspended = 0;
755 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200756 }
757
Mark Brown87506542008-11-18 20:50:34 +0000758 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000759 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200760
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000761 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100762
763 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000764 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700765
766 /* Recheck all analogue paths too */
767 dapm_mark_io_dirty(&card->dapm);
768 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100769}
770
771/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000772int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100773{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000774 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600775 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200776
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800777 /* If the initialization of this soc device failed, there is no codec
778 * associated with it. Just bail out in this case.
779 */
780 if (list_empty(&card->codec_dev_list))
781 return 0;
782
Mark Brown64ab9ba2009-03-31 11:27:03 +0100783 /* AC97 devices might have other drivers hanging off them so
784 * need to resume immediately. Other drivers don't have that
785 * problem and may take a substantial amount of time to resume
786 * due to I/O costs and anti-pop so handle them out of line.
787 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000788 for (i = 0; i < card->num_rtd; i++) {
789 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600790 ac97_control |= cpu_dai->driver->ac97_control;
791 }
792 if (ac97_control) {
793 dev_dbg(dev, "Resuming AC97 immediately\n");
794 soc_resume_deferred(&card->deferred_resume_work);
795 } else {
796 dev_dbg(dev, "Scheduling resume work\n");
797 if (!schedule_work(&card->deferred_resume_work))
798 dev_err(dev, "resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100799 }
Andy Green6ed25972008-06-13 16:24:05 +0100800
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200801 return 0;
802}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000803EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200804#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000805#define snd_soc_suspend NULL
806#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200807#endif
808
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100809static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800810};
811
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000812static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200813{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000814 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
815 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000816 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000817 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000818 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100819 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200820
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000821 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000822
Mark Brownb19e6e72012-03-14 21:18:39 +0000823 /* Find CPU DAI from registered DAIs*/
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000824 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600825 if (dai_link->cpu_of_node &&
826 (cpu_dai->dev->of_node != dai_link->cpu_of_node))
827 continue;
828 if (dai_link->cpu_name &&
829 strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
830 continue;
831 if (dai_link->cpu_dai_name &&
832 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
833 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700834
835 rtd->cpu_dai = cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000836 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000837
838 if (!rtd->cpu_dai) {
Mark Brownfb099cb2012-08-09 18:44:37 +0100839 dev_err(card->dev, "CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000840 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000841 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000842 }
843
Mark Brownb19e6e72012-03-14 21:18:39 +0000844 /* Find CODEC from registered CODECs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000845 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700846 if (dai_link->codec_of_node) {
847 if (codec->dev->of_node != dai_link->codec_of_node)
848 continue;
849 } else {
850 if (strcmp(codec->name, dai_link->codec_name))
851 continue;
852 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000853
Stephen Warren2610ab72011-12-07 13:58:27 -0700854 rtd->codec = codec;
855
856 /*
857 * CODEC found, so find CODEC DAI from registered DAIs from
858 * this CODEC
859 */
860 list_for_each_entry(codec_dai, &dai_list, list) {
861 if (codec->dev == codec_dai->dev &&
862 !strcmp(codec_dai->name,
863 dai_link->codec_dai_name)) {
864
865 rtd->codec_dai = codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000866 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000867 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000868
869 if (!rtd->codec_dai) {
Mark Brownfb099cb2012-08-09 18:44:37 +0100870 dev_err(card->dev, "CODEC DAI %s not registered\n",
Stephen Warren2610ab72011-12-07 13:58:27 -0700871 dai_link->codec_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000872 return -EPROBE_DEFER;
873 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000874 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000875
Mark Brownb19e6e72012-03-14 21:18:39 +0000876 if (!rtd->codec) {
Mark Brownfb099cb2012-08-09 18:44:37 +0100877 dev_err(card->dev, "CODEC %s not registered\n",
Mark Brownb19e6e72012-03-14 21:18:39 +0000878 dai_link->codec_name);
879 return -EPROBE_DEFER;
880 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100881
882 /* if there's no platform we match on the empty platform */
883 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700884 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100885 platform_name = "snd-soc-dummy";
886
Mark Brownb19e6e72012-03-14 21:18:39 +0000887 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000888 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700889 if (dai_link->platform_of_node) {
890 if (platform->dev->of_node !=
891 dai_link->platform_of_node)
892 continue;
893 } else {
894 if (strcmp(platform->name, platform_name))
895 continue;
896 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700897
898 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000899 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000900 if (!rtd->platform) {
Mark Brownfb099cb2012-08-09 18:44:37 +0100901 dev_err(card->dev, "platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000902 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000903 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000904 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000905
906 card->num_rtd++;
907
908 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000909}
910
Stephen Warrend12cd192012-06-08 12:34:22 -0600911static int soc_remove_platform(struct snd_soc_platform *platform)
912{
913 int ret;
914
915 if (platform->driver->remove) {
916 ret = platform->driver->remove(platform);
917 if (ret < 0)
918 pr_err("asoc: failed to remove %s: %d\n",
919 platform->name, ret);
920 }
921
922 /* Make sure all DAPM widgets are freed */
923 snd_soc_dapm_free(&platform->dapm);
924
925 soc_cleanup_platform_debugfs(platform);
926 platform->probed = 0;
927 list_del(&platform->card_list);
928 module_put(platform->dev->driver->owner);
929
930 return 0;
931}
932
Jarkko Nikula589c3562010-12-06 16:27:07 +0200933static void soc_remove_codec(struct snd_soc_codec *codec)
934{
935 int err;
936
937 if (codec->driver->remove) {
938 err = codec->driver->remove(codec);
939 if (err < 0)
940 dev_err(codec->dev,
941 "asoc: failed to remove %s: %d\n",
942 codec->name, err);
943 }
944
945 /* Make sure all DAPM widgets are freed */
946 snd_soc_dapm_free(&codec->dapm);
947
948 soc_cleanup_codec_debugfs(codec);
949 codec->probed = 0;
950 list_del(&codec->card_list);
951 module_put(codec->dev->driver->owner);
952}
953
Stephen Warren62ae68f2012-06-08 12:34:23 -0600954static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000955{
956 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000957 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
958 int err;
959
960 /* unregister the rtd device */
961 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800962 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
963 device_remove_file(rtd->dev, &dev_attr_codec_reg);
964 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000965 rtd->dev_registered = 0;
966 }
967
968 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100969 if (codec_dai && codec_dai->probed &&
970 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000971 if (codec_dai->driver->remove) {
972 err = codec_dai->driver->remove(codec_dai);
973 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200974 pr_err("asoc: failed to remove %s: %d\n",
975 codec_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000976 }
977 codec_dai->probed = 0;
978 list_del(&codec_dai->card_list);
979 }
980
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000981 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100982 if (cpu_dai && cpu_dai->probed &&
983 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000984 if (cpu_dai->driver->remove) {
985 err = cpu_dai->driver->remove(cpu_dai);
986 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200987 pr_err("asoc: failed to remove %s: %d\n",
988 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000989 }
990 cpu_dai->probed = 0;
991 list_del(&cpu_dai->card_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -0600992
Stephen Warren18d75642012-06-08 12:34:21 -0600993 if (!cpu_dai->codec) {
994 snd_soc_dapm_free(&cpu_dai->dapm);
Stephen Warrena9db7db2012-06-08 12:34:20 -0600995 module_put(cpu_dai->dev->driver->owner);
Stephen Warren18d75642012-06-08 12:34:21 -0600996 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000997 }
998}
999
Stephen Warren62ae68f2012-06-08 12:34:23 -06001000static void soc_remove_link_components(struct snd_soc_card *card, int num,
1001 int order)
1002{
1003 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1004 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1005 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1006 struct snd_soc_platform *platform = rtd->platform;
1007 struct snd_soc_codec *codec;
1008
1009 /* remove the platform */
1010 if (platform && platform->probed &&
1011 platform->driver->remove_order == order) {
1012 soc_remove_platform(platform);
1013 }
1014
1015 /* remove the CODEC-side CODEC */
1016 if (codec_dai) {
1017 codec = codec_dai->codec;
1018 if (codec && codec->probed &&
1019 codec->driver->remove_order == order)
1020 soc_remove_codec(codec);
1021 }
1022
1023 /* remove any CPU-side CODEC */
1024 if (cpu_dai) {
1025 codec = cpu_dai->codec;
1026 if (codec && codec->probed &&
1027 codec->driver->remove_order == order)
1028 soc_remove_codec(codec);
1029 }
1030}
1031
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001032static void soc_remove_dai_links(struct snd_soc_card *card)
1033{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001034 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001035
Liam Girdwood0168bf02011-06-07 16:08:05 +01001036 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1037 order++) {
1038 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001039 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001040 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001041
1042 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1043 order++) {
1044 for (dai = 0; dai < card->num_rtd; dai++)
1045 soc_remove_link_components(card, dai, order);
1046 }
1047
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001048 card->num_rtd = 0;
1049}
1050
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001051static void soc_set_name_prefix(struct snd_soc_card *card,
1052 struct snd_soc_codec *codec)
1053{
1054 int i;
1055
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001056 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001057 return;
1058
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001059 for (i = 0; i < card->num_configs; i++) {
1060 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001061 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1062 codec->name_prefix = map->name_prefix;
1063 break;
1064 }
1065 }
1066}
1067
Jarkko Nikula589c3562010-12-06 16:27:07 +02001068static int soc_probe_codec(struct snd_soc_card *card,
1069 struct snd_soc_codec *codec)
1070{
1071 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001072 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001073 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001074
1075 codec->card = card;
1076 codec->dapm.card = card;
1077 soc_set_name_prefix(card, codec);
1078
Jarkko Nikula70d293312011-01-27 16:24:22 +02001079 if (!try_module_get(codec->dev->driver->owner))
1080 return -ENODEV;
1081
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001082 soc_init_codec_debugfs(codec);
1083
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001084 if (driver->dapm_widgets)
1085 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1086 driver->num_dapm_widgets);
1087
Mark Brown888df392012-02-16 19:37:51 -08001088 /* Create DAPM widgets for each DAI stream */
1089 list_for_each_entry(dai, &dai_list, list) {
1090 if (dai->dev != codec->dev)
1091 continue;
1092
1093 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1094 }
1095
Mark Brown33c5f962011-08-22 18:40:30 +01001096 codec->dapm.idle_bias_off = driver->idle_bias_off;
1097
Mark Brown89b95ac02011-03-07 16:38:44 +00001098 if (driver->probe) {
1099 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001100 if (ret < 0) {
1101 dev_err(codec->dev,
1102 "asoc: failed to probe CODEC %s: %d\n",
1103 codec->name, ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001104 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001105 }
1106 }
1107
Mark Brown38cbf952012-06-22 13:04:02 +01001108 /* If the driver didn't set I/O up try regmap */
Mark Brown98d30882012-08-01 20:05:47 +01001109 if (!codec->write && dev_get_regmap(codec->dev, NULL))
Mark Brown38cbf952012-06-22 13:04:02 +01001110 snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
1111
Mark Brownb7af1da2011-04-07 19:18:44 +09001112 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001113 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001114 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001115 if (driver->dapm_routes)
1116 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1117 driver->num_dapm_routes);
1118
Jarkko Nikula589c3562010-12-06 16:27:07 +02001119 /* mark codec as probed and add to card codec list */
1120 codec->probed = 1;
1121 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001122 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001123
Jarkko Nikula70d293312011-01-27 16:24:22 +02001124 return 0;
1125
1126err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001127 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001128 module_put(codec->dev->driver->owner);
1129
Jarkko Nikula589c3562010-12-06 16:27:07 +02001130 return ret;
1131}
1132
Liam Girdwood956245e2011-07-01 16:54:08 +01001133static int soc_probe_platform(struct snd_soc_card *card,
1134 struct snd_soc_platform *platform)
1135{
1136 int ret = 0;
1137 const struct snd_soc_platform_driver *driver = platform->driver;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001138 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001139
1140 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001141 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001142
1143 if (!try_module_get(platform->dev->driver->owner))
1144 return -ENODEV;
1145
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001146 soc_init_platform_debugfs(platform);
1147
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001148 if (driver->dapm_widgets)
1149 snd_soc_dapm_new_controls(&platform->dapm,
1150 driver->dapm_widgets, driver->num_dapm_widgets);
1151
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001152 /* Create DAPM widgets for each DAI stream */
1153 list_for_each_entry(dai, &dai_list, list) {
1154 if (dai->dev != platform->dev)
1155 continue;
1156
1157 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1158 }
1159
Stephen Warren3fec6b62012-04-05 12:28:01 -06001160 platform->dapm.idle_bias_off = 1;
1161
Liam Girdwood956245e2011-07-01 16:54:08 +01001162 if (driver->probe) {
1163 ret = driver->probe(platform);
1164 if (ret < 0) {
1165 dev_err(platform->dev,
1166 "asoc: failed to probe platform %s: %d\n",
1167 platform->name, ret);
1168 goto err_probe;
1169 }
1170 }
1171
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001172 if (driver->controls)
1173 snd_soc_add_platform_controls(platform, driver->controls,
1174 driver->num_controls);
1175 if (driver->dapm_routes)
1176 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1177 driver->num_dapm_routes);
1178
Liam Girdwood956245e2011-07-01 16:54:08 +01001179 /* mark platform as probed and add to card platform list */
1180 platform->probed = 1;
1181 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001182 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001183
1184 return 0;
1185
1186err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001187 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001188 module_put(platform->dev->driver->owner);
1189
1190 return ret;
1191}
1192
Mark Brown36ae1a92012-01-06 17:12:45 -08001193static void rtd_release(struct device *dev)
1194{
1195 kfree(dev);
1196}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001197
Jarkko Nikula589c3562010-12-06 16:27:07 +02001198static int soc_post_component_init(struct snd_soc_card *card,
1199 struct snd_soc_codec *codec,
1200 int num, int dailess)
1201{
1202 struct snd_soc_dai_link *dai_link = NULL;
1203 struct snd_soc_aux_dev *aux_dev = NULL;
1204 struct snd_soc_pcm_runtime *rtd;
1205 const char *temp, *name;
1206 int ret = 0;
1207
1208 if (!dailess) {
1209 dai_link = &card->dai_link[num];
1210 rtd = &card->rtd[num];
1211 name = dai_link->name;
1212 } else {
1213 aux_dev = &card->aux_dev[num];
1214 rtd = &card->rtd_aux[num];
1215 name = aux_dev->name;
1216 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001217 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001218
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001219 /* Make sure all DAPM widgets are instantiated */
1220 snd_soc_dapm_new_widgets(&codec->dapm);
1221
Jarkko Nikula589c3562010-12-06 16:27:07 +02001222 /* machine controls, routes and widgets are not prefixed */
1223 temp = codec->name_prefix;
1224 codec->name_prefix = NULL;
1225
1226 /* do machine specific initialization */
1227 if (!dailess && dai_link->init)
1228 ret = dai_link->init(rtd);
1229 else if (dailess && aux_dev->init)
1230 ret = aux_dev->init(&codec->dapm);
1231 if (ret < 0) {
1232 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1233 return ret;
1234 }
1235 codec->name_prefix = temp;
1236
Jarkko Nikula589c3562010-12-06 16:27:07 +02001237 /* register the rtd device */
1238 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001239
1240 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1241 if (!rtd->dev)
1242 return -ENOMEM;
1243 device_initialize(rtd->dev);
1244 rtd->dev->parent = card->dev;
1245 rtd->dev->release = rtd_release;
1246 rtd->dev->init_name = name;
1247 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001248 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001249 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1250 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1251 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1252 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001253 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001254 if (ret < 0) {
1255 dev_err(card->dev,
1256 "asoc: failed to register runtime device: %d\n", ret);
1257 return ret;
1258 }
1259 rtd->dev_registered = 1;
1260
1261 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001262 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001263 if (ret < 0)
1264 dev_err(codec->dev,
1265 "asoc: failed to add codec dapm sysfs entries: %d\n",
1266 ret);
1267
1268 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001269 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001270 if (ret < 0)
1271 dev_err(codec->dev,
1272 "asoc: failed to add codec sysfs files: %d\n", ret);
1273
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001274#ifdef CONFIG_DEBUG_FS
1275 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001276 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001277 goto out;
1278
1279 ret = soc_dpcm_debugfs_add(rtd);
1280 if (ret < 0)
1281 dev_err(rtd->dev, "asoc: failed to add dpcm sysfs entries: %d\n", ret);
1282
1283out:
1284#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001285 return 0;
1286}
1287
Stephen Warren62ae68f2012-06-08 12:34:23 -06001288static int soc_probe_link_components(struct snd_soc_card *card, int num,
1289 int order)
1290{
1291 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1292 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1293 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1294 struct snd_soc_platform *platform = rtd->platform;
1295 int ret;
1296
1297 /* probe the CPU-side component, if it is a CODEC */
1298 if (cpu_dai->codec &&
1299 !cpu_dai->codec->probed &&
1300 cpu_dai->codec->driver->probe_order == order) {
1301 ret = soc_probe_codec(card, cpu_dai->codec);
1302 if (ret < 0)
1303 return ret;
1304 }
1305
1306 /* probe the CODEC-side component */
1307 if (!codec_dai->codec->probed &&
1308 codec_dai->codec->driver->probe_order == order) {
1309 ret = soc_probe_codec(card, codec_dai->codec);
1310 if (ret < 0)
1311 return ret;
1312 }
1313
1314 /* probe the platform */
1315 if (!platform->probed &&
1316 platform->driver->probe_order == order) {
1317 ret = soc_probe_platform(card, platform);
1318 if (ret < 0)
1319 return ret;
1320 }
1321
1322 return 0;
1323}
1324
1325static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001326{
1327 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1328 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1329 struct snd_soc_codec *codec = rtd->codec;
1330 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001331 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1332 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1333 struct snd_soc_dapm_widget *play_w, *capture_w;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001334 int ret;
1335
Liam Girdwood0168bf02011-06-07 16:08:05 +01001336 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1337 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001338
1339 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001340 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001341 codec_dai->card = card;
1342 cpu_dai->card = card;
1343
1344 /* set default power off timeout */
1345 rtd->pmdown_time = pmdown_time;
1346
1347 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001348 if (!cpu_dai->probed &&
1349 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001350 if (!cpu_dai->codec) {
1351 cpu_dai->dapm.card = card;
1352 if (!try_module_get(cpu_dai->dev->driver->owner))
1353 return -ENODEV;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001354
Stephen Warren18d75642012-06-08 12:34:21 -06001355 list_add(&cpu_dai->dapm.list, &card->dapm_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001356 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1357 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001358
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001359 if (cpu_dai->driver->probe) {
1360 ret = cpu_dai->driver->probe(cpu_dai);
1361 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001362 pr_err("asoc: failed to probe CPU DAI %s: %d\n",
1363 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001364 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001365 return ret;
1366 }
1367 }
1368 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001369 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001370 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1371 }
1372
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001373 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001374 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001375 if (codec_dai->driver->probe) {
1376 ret = codec_dai->driver->probe(codec_dai);
1377 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001378 pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
1379 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001380 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001381 }
1382 }
1383
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001384 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001385 codec_dai->probed = 1;
1386 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001387 }
1388
Liam Girdwood0168bf02011-06-07 16:08:05 +01001389 /* complete DAI probe during last probe */
1390 if (order != SND_SOC_COMP_ORDER_LAST)
1391 return 0;
1392
Jarkko Nikula589c3562010-12-06 16:27:07 +02001393 ret = soc_post_component_init(card, codec, num, 0);
1394 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001395 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001396
Mark Brown36ae1a92012-01-06 17:12:45 -08001397 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001398 if (ret < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -02001399 pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001400
Namarta Kohli1245b702012-08-16 17:10:41 +05301401 if (cpu_dai->driver->compress_dai) {
1402 /*create compress_device"*/
1403 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001404 if (ret < 0) {
Namarta Kohli1245b702012-08-16 17:10:41 +05301405 pr_err("asoc: can't create compress %s\n",
1406 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001407 return ret;
1408 }
1409 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301410
1411 if (!dai_link->params) {
1412 /* create the pcm */
1413 ret = soc_new_pcm(rtd, num);
1414 if (ret < 0) {
1415 pr_err("asoc: can't create pcm %s :%d\n",
1416 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001417 return ret;
1418 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301419 } else {
1420 /* link the DAI widgets */
1421 play_w = codec_dai->playback_widget;
1422 capture_w = cpu_dai->capture_widget;
1423 if (play_w && capture_w) {
1424 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Mark Brownc74184e2012-04-04 22:12:09 +01001425 capture_w, play_w);
Namarta Kohli1245b702012-08-16 17:10:41 +05301426 if (ret != 0) {
1427 dev_err(card->dev, "Can't link %s to %s: %d\n",
1428 play_w->name, capture_w->name, ret);
1429 return ret;
1430 }
1431 }
1432
1433 play_w = cpu_dai->playback_widget;
1434 capture_w = codec_dai->capture_widget;
1435 if (play_w && capture_w) {
1436 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1437 capture_w, play_w);
1438 if (ret != 0) {
1439 dev_err(card->dev, "Can't link %s to %s: %d\n",
1440 play_w->name, capture_w->name, ret);
1441 return ret;
1442 }
Mark Brownc74184e2012-04-04 22:12:09 +01001443 }
1444 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001445 }
1446
1447 /* add platform data for AC97 devices */
1448 if (rtd->codec_dai->driver->ac97_control)
1449 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1450
1451 return 0;
1452}
1453
1454#ifdef CONFIG_SND_SOC_AC97_BUS
1455static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1456{
1457 int ret;
1458
1459 /* Only instantiate AC97 if not already done by the adaptor
1460 * for the generic AC97 subsystem.
1461 */
1462 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001463 /*
1464 * It is possible that the AC97 device is already registered to
1465 * the device subsystem. This happens when the device is created
1466 * via snd_ac97_mixer(). Currently only SoC codec that does so
1467 * is the generic AC97 glue but others migh emerge.
1468 *
1469 * In those cases we don't try to register the device again.
1470 */
1471 if (!rtd->codec->ac97_created)
1472 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001473
1474 ret = soc_ac97_dev_register(rtd->codec);
1475 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001476 pr_err("asoc: AC97 device register failed:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001477 return ret;
1478 }
1479
1480 rtd->codec->ac97_registered = 1;
1481 }
1482 return 0;
1483}
1484
1485static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1486{
1487 if (codec->ac97_registered) {
1488 soc_ac97_dev_unregister(codec);
1489 codec->ac97_registered = 0;
1490 }
1491}
1492#endif
1493
Mark Brownb19e6e72012-03-14 21:18:39 +00001494static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1495{
1496 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1497 struct snd_soc_codec *codec;
1498
1499 /* find CODEC from registered CODECs*/
1500 list_for_each_entry(codec, &codec_list, list) {
1501 if (!strcmp(codec->name, aux_dev->codec_name))
1502 return 0;
1503 }
1504
Mark Brownfb099cb2012-08-09 18:44:37 +01001505 dev_err(card->dev, "%s not registered\n", aux_dev->codec_name);
1506
Mark Brownb19e6e72012-03-14 21:18:39 +00001507 return -EPROBE_DEFER;
1508}
1509
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001510static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1511{
1512 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001513 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001514 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001515
1516 /* find CODEC from registered CODECs*/
1517 list_for_each_entry(codec, &codec_list, list) {
1518 if (!strcmp(codec->name, aux_dev->codec_name)) {
1519 if (codec->probed) {
1520 dev_err(codec->dev,
1521 "asoc: codec already probed");
1522 ret = -EBUSY;
1523 goto out;
1524 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001525 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001526 }
1527 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001528 /* codec not found */
1529 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001530 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001531
Jarkko Nikula676ad982010-12-03 09:18:22 +02001532found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001533 ret = soc_probe_codec(card, codec);
1534 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001535 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001536
Jarkko Nikula589c3562010-12-06 16:27:07 +02001537 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001538
1539out:
1540 return ret;
1541}
1542
1543static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1544{
1545 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1546 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001547
1548 /* unregister the rtd device */
1549 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001550 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1551 device_del(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001552 rtd->dev_registered = 0;
1553 }
1554
Jarkko Nikula589c3562010-12-06 16:27:07 +02001555 if (codec && codec->probed)
1556 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001557}
1558
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001559static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1560 enum snd_soc_compress_type compress_type)
1561{
1562 int ret;
1563
1564 if (codec->cache_init)
1565 return 0;
1566
1567 /* override the compress_type if necessary */
1568 if (compress_type && codec->compress_type != compress_type)
1569 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001570 ret = snd_soc_cache_init(codec);
1571 if (ret < 0) {
1572 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1573 ret);
1574 return ret;
1575 }
1576 codec->cache_init = 1;
1577 return 0;
1578}
1579
Mark Brownb19e6e72012-03-14 21:18:39 +00001580static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001581{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001582 struct snd_soc_codec *codec;
1583 struct snd_soc_codec_conf *codec_conf;
1584 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001585 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001586 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001587
Liam Girdwood01b9d992012-03-07 10:38:25 +00001588 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001589
Mark Brownb19e6e72012-03-14 21:18:39 +00001590 /* bind DAIs */
1591 for (i = 0; i < card->num_links; i++) {
1592 ret = soc_bind_dai_link(card, i);
1593 if (ret != 0)
1594 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001595 }
1596
Mark Brownb19e6e72012-03-14 21:18:39 +00001597 /* check aux_devs too */
1598 for (i = 0; i < card->num_aux_devs; i++) {
1599 ret = soc_check_aux_dev(card, i);
1600 if (ret != 0)
1601 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001602 }
1603
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001604 /* initialize the register cache for each available codec */
1605 list_for_each_entry(codec, &codec_list, list) {
1606 if (codec->cache_init)
1607 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001608 /* by default we don't override the compress_type */
1609 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001610 /* check to see if we need to override the compress_type */
1611 for (i = 0; i < card->num_configs; ++i) {
1612 codec_conf = &card->codec_conf[i];
1613 if (!strcmp(codec->name, codec_conf->dev_name)) {
1614 compress_type = codec_conf->compress_type;
1615 if (compress_type && compress_type
1616 != codec->compress_type)
1617 break;
1618 }
1619 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001620 ret = snd_soc_init_codec_cache(codec, compress_type);
Mark Brownb19e6e72012-03-14 21:18:39 +00001621 if (ret < 0)
1622 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001623 }
1624
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001625 /* card bind complete so register a sound card */
1626 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1627 card->owner, 0, &card->snd_card);
1628 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001629 pr_err("asoc: can't create sound card for card %s: %d\n",
1630 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001631 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001632 }
1633 card->snd_card->dev = card->dev;
1634
Mark Browne37a4972011-03-02 18:21:57 +00001635 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1636 card->dapm.dev = card->dev;
1637 card->dapm.card = card;
1638 list_add(&card->dapm.list, &card->dapm_list);
1639
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001640#ifdef CONFIG_DEBUG_FS
1641 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1642#endif
1643
Mark Brown88ee1c62011-02-02 10:43:26 +00001644#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001645 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001646 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001647#endif
Andy Green6ed25972008-06-13 16:24:05 +01001648
Mark Brown9a841eb2011-04-12 17:51:37 -07001649 if (card->dapm_widgets)
1650 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1651 card->num_dapm_widgets);
1652
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001653 /* initialise the sound card only once */
1654 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001655 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001656 if (ret < 0)
1657 goto card_probe_error;
1658 }
1659
Stephen Warren62ae68f2012-06-08 12:34:23 -06001660 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001661 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1662 order++) {
1663 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001664 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001665 if (ret < 0) {
1666 pr_err("asoc: failed to instantiate card %s: %d\n",
Stephen Warren62ae68f2012-06-08 12:34:23 -06001667 card->name, ret);
1668 goto probe_dai_err;
1669 }
1670 }
1671 }
1672
1673 /* probe all DAI links on this card */
1674 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1675 order++) {
1676 for (i = 0; i < card->num_links; i++) {
1677 ret = soc_probe_link_dais(card, i, order);
1678 if (ret < 0) {
1679 pr_err("asoc: failed to instantiate card %s: %d\n",
1680 card->name, ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001681 goto probe_dai_err;
1682 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001683 }
1684 }
1685
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001686 for (i = 0; i < card->num_aux_devs; i++) {
1687 ret = soc_probe_aux_dev(card, i);
1688 if (ret < 0) {
1689 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1690 card->name, ret);
1691 goto probe_aux_dev_err;
1692 }
1693 }
1694
Mark Brown888df392012-02-16 19:37:51 -08001695 snd_soc_dapm_link_dai_widgets(card);
1696
Mark Brownb7af1da2011-04-07 19:18:44 +09001697 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001698 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001699
Mark Brownb8ad29d2011-03-02 18:35:51 +00001700 if (card->dapm_routes)
1701 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1702 card->num_dapm_routes);
1703
Mark Brownb90d2f92011-10-10 13:38:06 +01001704 snd_soc_dapm_new_widgets(&card->dapm);
1705
Mark Brown75d9ac42011-09-27 16:41:01 +01001706 for (i = 0; i < card->num_links; i++) {
1707 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001708 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001709
Mark Brownf04209a2012-04-09 16:29:21 +01001710 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001711 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001712 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001713 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001714 dev_warn(card->rtd[i].codec_dai->dev,
1715 "Failed to set DAI format: %d\n",
1716 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001717 }
1718
1719 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001720 if (dai_fmt &&
1721 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001722 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1723 dai_fmt);
1724 if (ret != 0 && ret != -ENOTSUPP)
1725 dev_warn(card->rtd[i].cpu_dai->dev,
1726 "Failed to set DAI format: %d\n",
1727 ret);
1728 } else if (dai_fmt) {
1729 /* Flip the polarity for the "CPU" end */
1730 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1731 switch (dai_link->dai_fmt &
1732 SND_SOC_DAIFMT_MASTER_MASK) {
1733 case SND_SOC_DAIFMT_CBM_CFM:
1734 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1735 break;
1736 case SND_SOC_DAIFMT_CBM_CFS:
1737 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1738 break;
1739 case SND_SOC_DAIFMT_CBS_CFM:
1740 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1741 break;
1742 case SND_SOC_DAIFMT_CBS_CFS:
1743 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1744 break;
1745 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001746
1747 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001748 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001749 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001750 dev_warn(card->rtd[i].cpu_dai->dev,
1751 "Failed to set DAI format: %d\n",
1752 ret);
1753 }
1754 }
1755
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001756 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001757 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001758 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1759 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001760 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1761 "%s", card->driver_name ? card->driver_name : card->name);
1762 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1763 switch (card->snd_card->driver[i]) {
1764 case '_':
1765 case '-':
1766 case '\0':
1767 break;
1768 default:
1769 if (!isalnum(card->snd_card->driver[i]))
1770 card->snd_card->driver[i] = '_';
1771 break;
1772 }
1773 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001774
Mark Brown28e9ad92011-03-02 18:36:34 +00001775 if (card->late_probe) {
1776 ret = card->late_probe(card);
1777 if (ret < 0) {
1778 dev_err(card->dev, "%s late_probe() failed: %d\n",
1779 card->name, ret);
1780 goto probe_aux_dev_err;
1781 }
1782 }
1783
Mark Brown2dc00212011-10-08 13:59:44 +01001784 snd_soc_dapm_new_widgets(&card->dapm);
1785
Stephen Warren16332812011-11-23 12:42:04 -07001786 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001787 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001788 snd_soc_dapm_auto_nc_codec_pins(codec);
1789
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001790 ret = snd_card_register(card->snd_card);
1791 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001792 pr_err("asoc: failed to register soundcard for %s: %d\n",
1793 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001794 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001795 }
1796
1797#ifdef CONFIG_SND_SOC_AC97_BUS
1798 /* register any AC97 codecs */
1799 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001800 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1801 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001802 pr_err("asoc: failed to register AC97 %s: %d\n",
1803 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001804 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001805 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001806 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001807 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001808 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001809#endif
1810
Mark Brown435c5e22008-12-04 15:32:53 +00001811 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001812 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001813 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001814
1815 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001816
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001817probe_aux_dev_err:
1818 for (i = 0; i < card->num_aux_devs; i++)
1819 soc_remove_aux_dev(card, i);
1820
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001821probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001822 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001823
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001824card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001825 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001826 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001827
1828 snd_card_free(card->snd_card);
1829
Mark Brownb19e6e72012-03-14 21:18:39 +00001830base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001831 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001832
Mark Brownb19e6e72012-03-14 21:18:39 +00001833 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001834}
1835
1836/* probes a new socdev */
1837static int soc_probe(struct platform_device *pdev)
1838{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001839 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001840
Vinod Koul70a7ca32011-01-14 19:22:48 +05301841 /*
1842 * no card, so machine driver should be registering card
1843 * we should not be here in that case so ret error
1844 */
1845 if (!card)
1846 return -EINVAL;
1847
Mark Brownfe4085e2012-03-02 13:07:41 +00001848 dev_warn(&pdev->dev,
1849 "ASoC machine %s should use snd_soc_register_card()\n",
1850 card->name);
1851
Mark Brown435c5e22008-12-04 15:32:53 +00001852 /* Bodge while we unpick instantiation */
1853 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001854
Mark Brown28d528c2012-08-09 18:45:23 +01001855 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001856}
1857
Vinod Koulb0e26482011-01-13 22:48:02 +05301858static int soc_cleanup_card_resources(struct snd_soc_card *card)
1859{
Vinod Koulb0e26482011-01-13 22:48:02 +05301860 int i;
1861
1862 /* make sure any delayed work runs */
1863 for (i = 0; i < card->num_rtd; i++) {
1864 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1865 flush_delayed_work_sync(&rtd->delayed_work);
1866 }
1867
1868 /* remove auxiliary devices */
1869 for (i = 0; i < card->num_aux_devs; i++)
1870 soc_remove_aux_dev(card, i);
1871
1872 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001873 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301874
1875 soc_cleanup_card_debugfs(card);
1876
1877 /* remove the card */
1878 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001879 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301880
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001881 snd_soc_dapm_free(&card->dapm);
1882
Vinod Koulb0e26482011-01-13 22:48:02 +05301883 snd_card_free(card->snd_card);
1884 return 0;
1885
1886}
1887
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001888/* removes a socdev */
1889static int soc_remove(struct platform_device *pdev)
1890{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001891 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001892
Mark Brownc5af3a22008-11-28 13:29:45 +00001893 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001894 return 0;
1895}
1896
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001897int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001898{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001899 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001900 int i;
Mark Brown51737472009-06-22 13:16:51 +01001901
1902 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001903 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001904
1905 /* Flush out pmdown_time work - we actually do want to run it
1906 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001907 for (i = 0; i < card->num_rtd; i++) {
1908 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001909 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001910 }
Mark Brown51737472009-06-22 13:16:51 +01001911
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001912 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001913
1914 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001915}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001916EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001917
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001918const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301919 .suspend = snd_soc_suspend,
1920 .resume = snd_soc_resume,
1921 .freeze = snd_soc_suspend,
1922 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001923 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301924 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001925};
Stephen Warrendeb26072011-04-05 19:35:30 -06001926EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001927
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001928/* ASoC platform driver */
1929static struct platform_driver soc_driver = {
1930 .driver = {
1931 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001932 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001933 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001934 },
1935 .probe = soc_probe,
1936 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001937};
1938
Mark Brown096e49d2009-07-05 15:12:22 +01001939/**
1940 * snd_soc_codec_volatile_register: Report if a register is volatile.
1941 *
1942 * @codec: CODEC to query.
1943 * @reg: Register to query.
1944 *
1945 * Boolean function indiciating if a CODEC register is volatile.
1946 */
Mark Brown181e0552011-01-24 14:05:25 +00001947int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1948 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001949{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001950 if (codec->volatile_register)
1951 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001952 else
1953 return 0;
1954}
1955EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1956
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001957/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001958 * snd_soc_codec_readable_register: Report if a register is readable.
1959 *
1960 * @codec: CODEC to query.
1961 * @reg: Register to query.
1962 *
1963 * Boolean function indicating if a CODEC register is readable.
1964 */
1965int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1966 unsigned int reg)
1967{
1968 if (codec->readable_register)
1969 return codec->readable_register(codec, reg);
1970 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001971 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001972}
1973EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1974
1975/**
1976 * snd_soc_codec_writable_register: Report if a register is writable.
1977 *
1978 * @codec: CODEC to query.
1979 * @reg: Register to query.
1980 *
1981 * Boolean function indicating if a CODEC register is writable.
1982 */
1983int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1984 unsigned int reg)
1985{
1986 if (codec->writable_register)
1987 return codec->writable_register(codec, reg);
1988 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001989 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001990}
1991EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1992
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001993int snd_soc_platform_read(struct snd_soc_platform *platform,
1994 unsigned int reg)
1995{
1996 unsigned int ret;
1997
1998 if (!platform->driver->read) {
1999 dev_err(platform->dev, "platform has no read back\n");
2000 return -1;
2001 }
2002
2003 ret = platform->driver->read(platform, reg);
2004 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002005 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002006
2007 return ret;
2008}
2009EXPORT_SYMBOL_GPL(snd_soc_platform_read);
2010
2011int snd_soc_platform_write(struct snd_soc_platform *platform,
2012 unsigned int reg, unsigned int val)
2013{
2014 if (!platform->driver->write) {
2015 dev_err(platform->dev, "platform has no write back\n");
2016 return -1;
2017 }
2018
2019 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002020 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002021 return platform->driver->write(platform, reg, val);
2022}
2023EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2024
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002025/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002026 * snd_soc_new_ac97_codec - initailise AC97 device
2027 * @codec: audio codec
2028 * @ops: AC97 bus operations
2029 * @num: AC97 codec number
2030 *
2031 * Initialises AC97 codec resources for use by ad-hoc devices only.
2032 */
2033int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2034 struct snd_ac97_bus_ops *ops, int num)
2035{
2036 mutex_lock(&codec->mutex);
2037
2038 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2039 if (codec->ac97 == NULL) {
2040 mutex_unlock(&codec->mutex);
2041 return -ENOMEM;
2042 }
2043
2044 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2045 if (codec->ac97->bus == NULL) {
2046 kfree(codec->ac97);
2047 codec->ac97 = NULL;
2048 mutex_unlock(&codec->mutex);
2049 return -ENOMEM;
2050 }
2051
2052 codec->ac97->bus->ops = ops;
2053 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002054
2055 /*
2056 * Mark the AC97 device to be created by us. This way we ensure that the
2057 * device will be registered with the device subsystem later on.
2058 */
2059 codec->ac97_created = 1;
2060
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002061 mutex_unlock(&codec->mutex);
2062 return 0;
2063}
2064EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2065
2066/**
2067 * snd_soc_free_ac97_codec - free AC97 codec device
2068 * @codec: audio codec
2069 *
2070 * Frees AC97 codec device resources.
2071 */
2072void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2073{
2074 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002075#ifdef CONFIG_SND_SOC_AC97_BUS
2076 soc_unregister_ac97_dai_link(codec);
2077#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002078 kfree(codec->ac97->bus);
2079 kfree(codec->ac97);
2080 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002081 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002082 mutex_unlock(&codec->mutex);
2083}
2084EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2085
Mark Brownc3753702010-11-01 15:41:57 -04002086unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2087{
2088 unsigned int ret;
2089
Mark Brownc3acec22010-12-02 16:15:29 +00002090 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002091 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002092 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002093
2094 return ret;
2095}
2096EXPORT_SYMBOL_GPL(snd_soc_read);
2097
2098unsigned int snd_soc_write(struct snd_soc_codec *codec,
2099 unsigned int reg, unsigned int val)
2100{
2101 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002102 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002103 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002104}
2105EXPORT_SYMBOL_GPL(snd_soc_write);
2106
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00002107unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2108 unsigned int reg, const void *data, size_t len)
2109{
2110 return codec->bulk_write_raw(codec, reg, data, len);
2111}
2112EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2113
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002114/**
2115 * snd_soc_update_bits - update codec register bits
2116 * @codec: audio codec
2117 * @reg: codec register
2118 * @mask: register mask
2119 * @value: new value
2120 *
2121 * Writes new register value.
2122 *
Timur Tabi180c3292011-01-10 15:58:13 -06002123 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002124 */
2125int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002126 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002127{
Mark Brown8a713da2011-12-03 12:33:55 +00002128 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002129 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002130 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002131
Mark Brown8a713da2011-12-03 12:33:55 +00002132 if (codec->using_regmap) {
2133 ret = regmap_update_bits_check(codec->control_data, reg,
2134 mask, value, &change);
2135 } else {
2136 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06002137 if (ret < 0)
2138 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00002139
2140 old = ret;
2141 new = (old & ~mask) | (value & mask);
2142 change = old != new;
2143 if (change)
2144 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06002145 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002146
Mark Brown8a713da2011-12-03 12:33:55 +00002147 if (ret < 0)
2148 return ret;
2149
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002150 return change;
2151}
2152EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2153
2154/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002155 * snd_soc_update_bits_locked - update codec register bits
2156 * @codec: audio codec
2157 * @reg: codec register
2158 * @mask: register mask
2159 * @value: new value
2160 *
2161 * Writes new register value, and takes the codec mutex.
2162 *
2163 * Returns 1 for change else 0.
2164 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002165int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2166 unsigned short reg, unsigned int mask,
2167 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002168{
2169 int change;
2170
2171 mutex_lock(&codec->mutex);
2172 change = snd_soc_update_bits(codec, reg, mask, value);
2173 mutex_unlock(&codec->mutex);
2174
2175 return change;
2176}
Mark Browndd1b3d52009-12-04 14:22:03 +00002177EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002178
2179/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002180 * snd_soc_test_bits - test register for change
2181 * @codec: audio codec
2182 * @reg: codec register
2183 * @mask: register mask
2184 * @value: new value
2185 *
2186 * Tests a register with a new value and checks if the new value is
2187 * different from the old value.
2188 *
2189 * Returns 1 for change else 0.
2190 */
2191int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002192 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002193{
2194 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002195 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002196
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002197 old = snd_soc_read(codec, reg);
2198 new = (old & ~mask) | value;
2199 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002200
2201 return change;
2202}
2203EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2204
2205/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002206 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2207 * @substream: the pcm substream
2208 * @hw: the hardware parameters
2209 *
2210 * Sets the substream runtime hardware parameters.
2211 */
2212int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2213 const struct snd_pcm_hardware *hw)
2214{
2215 struct snd_pcm_runtime *runtime = substream->runtime;
2216 runtime->hw.info = hw->info;
2217 runtime->hw.formats = hw->formats;
2218 runtime->hw.period_bytes_min = hw->period_bytes_min;
2219 runtime->hw.period_bytes_max = hw->period_bytes_max;
2220 runtime->hw.periods_min = hw->periods_min;
2221 runtime->hw.periods_max = hw->periods_max;
2222 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2223 runtime->hw.fifo_size = hw->fifo_size;
2224 return 0;
2225}
2226EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2227
2228/**
2229 * snd_soc_cnew - create new control
2230 * @_template: control template
2231 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002232 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002233 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002234 *
2235 * Create a new mixer control from a template control.
2236 *
2237 * Returns 0 for success, else error.
2238 */
2239struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002240 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002241 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002242{
2243 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002244 struct snd_kcontrol *kcontrol;
2245 char *name = NULL;
2246 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002247
2248 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002249 template.index = 0;
2250
Mark Brownefb7ac32011-03-08 17:23:24 +00002251 if (!long_name)
2252 long_name = template.name;
2253
2254 if (prefix) {
2255 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08002256 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00002257 if (!name)
2258 return NULL;
2259
2260 snprintf(name, name_len, "%s %s", prefix, long_name);
2261
2262 template.name = name;
2263 } else {
2264 template.name = long_name;
2265 }
2266
2267 kcontrol = snd_ctl_new1(&template, data);
2268
2269 kfree(name);
2270
2271 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002272}
2273EXPORT_SYMBOL_GPL(snd_soc_cnew);
2274
Liam Girdwood022658b2012-02-03 17:43:09 +00002275static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2276 const struct snd_kcontrol_new *controls, int num_controls,
2277 const char *prefix, void *data)
2278{
2279 int err, i;
2280
2281 for (i = 0; i < num_controls; i++) {
2282 const struct snd_kcontrol_new *control = &controls[i];
2283 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2284 control->name, prefix));
2285 if (err < 0) {
2286 dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2287 return err;
2288 }
2289 }
2290
2291 return 0;
2292}
2293
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002294/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002295 * snd_soc_add_codec_controls - add an array of controls to a codec.
2296 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002297 * duplicating this code.
2298 *
2299 * @codec: codec to add controls to
2300 * @controls: array of controls to add
2301 * @num_controls: number of elements in the array
2302 *
2303 * Return 0 for success, else error.
2304 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002305int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002306 const struct snd_kcontrol_new *controls, int num_controls)
2307{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002308 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002309
Liam Girdwood022658b2012-02-03 17:43:09 +00002310 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2311 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002312}
Liam Girdwood022658b2012-02-03 17:43:09 +00002313EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002314
2315/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002316 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002317 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002318 *
2319 * @platform: platform to add controls to
2320 * @controls: array of controls to add
2321 * @num_controls: number of elements in the array
2322 *
2323 * Return 0 for success, else error.
2324 */
2325int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2326 const struct snd_kcontrol_new *controls, int num_controls)
2327{
2328 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002329
Liam Girdwood022658b2012-02-03 17:43:09 +00002330 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2331 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002332}
2333EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2334
2335/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002336 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2337 * Convenience function to add a list of controls.
2338 *
2339 * @soc_card: SoC card to add controls to
2340 * @controls: array of controls to add
2341 * @num_controls: number of elements in the array
2342 *
2343 * Return 0 for success, else error.
2344 */
2345int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2346 const struct snd_kcontrol_new *controls, int num_controls)
2347{
2348 struct snd_card *card = soc_card->snd_card;
2349
2350 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2351 NULL, soc_card);
2352}
2353EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2354
2355/**
2356 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2357 * Convienience function to add a list of controls.
2358 *
2359 * @dai: DAI to add controls to
2360 * @controls: array of controls to add
2361 * @num_controls: number of elements in the array
2362 *
2363 * Return 0 for success, else error.
2364 */
2365int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2366 const struct snd_kcontrol_new *controls, int num_controls)
2367{
2368 struct snd_card *card = dai->card->snd_card;
2369
2370 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2371 NULL, dai);
2372}
2373EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2374
2375/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002376 * snd_soc_info_enum_double - enumerated double mixer info callback
2377 * @kcontrol: mixer control
2378 * @uinfo: control element information
2379 *
2380 * Callback to provide information about a double enumerated
2381 * mixer control.
2382 *
2383 * Returns 0 for success.
2384 */
2385int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2386 struct snd_ctl_elem_info *uinfo)
2387{
2388 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2389
2390 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2391 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002392 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002393
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002394 if (uinfo->value.enumerated.item > e->max - 1)
2395 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002396 strcpy(uinfo->value.enumerated.name,
2397 e->texts[uinfo->value.enumerated.item]);
2398 return 0;
2399}
2400EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2401
2402/**
2403 * snd_soc_get_enum_double - enumerated double mixer get callback
2404 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002405 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002406 *
2407 * Callback to get the value of a double enumerated mixer.
2408 *
2409 * Returns 0 for success.
2410 */
2411int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2412 struct snd_ctl_elem_value *ucontrol)
2413{
2414 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2415 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002416 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002417
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002418 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002419 ;
2420 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002421 ucontrol->value.enumerated.item[0]
2422 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002423 if (e->shift_l != e->shift_r)
2424 ucontrol->value.enumerated.item[1] =
2425 (val >> e->shift_r) & (bitmask - 1);
2426
2427 return 0;
2428}
2429EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2430
2431/**
2432 * snd_soc_put_enum_double - enumerated double mixer put callback
2433 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002434 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002435 *
2436 * Callback to set the value of a double enumerated mixer.
2437 *
2438 * Returns 0 for success.
2439 */
2440int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2441 struct snd_ctl_elem_value *ucontrol)
2442{
2443 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2444 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002445 unsigned int val;
2446 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002447
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002448 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002449 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002450 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002451 return -EINVAL;
2452 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2453 mask = (bitmask - 1) << e->shift_l;
2454 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002455 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002456 return -EINVAL;
2457 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2458 mask |= (bitmask - 1) << e->shift_r;
2459 }
2460
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002461 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002462}
2463EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2464
2465/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002466 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2467 * @kcontrol: mixer control
2468 * @ucontrol: control element information
2469 *
2470 * Callback to get the value of a double semi enumerated mixer.
2471 *
2472 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2473 * used for handling bitfield coded enumeration for example.
2474 *
2475 * Returns 0 for success.
2476 */
2477int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2478 struct snd_ctl_elem_value *ucontrol)
2479{
2480 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002481 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002482 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002483
2484 reg_val = snd_soc_read(codec, e->reg);
2485 val = (reg_val >> e->shift_l) & e->mask;
2486 for (mux = 0; mux < e->max; mux++) {
2487 if (val == e->values[mux])
2488 break;
2489 }
2490 ucontrol->value.enumerated.item[0] = mux;
2491 if (e->shift_l != e->shift_r) {
2492 val = (reg_val >> e->shift_r) & e->mask;
2493 for (mux = 0; mux < e->max; mux++) {
2494 if (val == e->values[mux])
2495 break;
2496 }
2497 ucontrol->value.enumerated.item[1] = mux;
2498 }
2499
2500 return 0;
2501}
2502EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2503
2504/**
2505 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2506 * @kcontrol: mixer control
2507 * @ucontrol: control element information
2508 *
2509 * Callback to set the value of a double semi enumerated mixer.
2510 *
2511 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2512 * used for handling bitfield coded enumeration for example.
2513 *
2514 * Returns 0 for success.
2515 */
2516int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2517 struct snd_ctl_elem_value *ucontrol)
2518{
2519 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002520 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002521 unsigned int val;
2522 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002523
2524 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2525 return -EINVAL;
2526 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2527 mask = e->mask << e->shift_l;
2528 if (e->shift_l != e->shift_r) {
2529 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2530 return -EINVAL;
2531 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2532 mask |= e->mask << e->shift_r;
2533 }
2534
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002535 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002536}
2537EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2538
2539/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002540 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2541 * @kcontrol: mixer control
2542 * @uinfo: control element information
2543 *
2544 * Callback to provide information about an external enumerated
2545 * single mixer.
2546 *
2547 * Returns 0 for success.
2548 */
2549int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2550 struct snd_ctl_elem_info *uinfo)
2551{
2552 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2553
2554 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2555 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002556 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002557
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002558 if (uinfo->value.enumerated.item > e->max - 1)
2559 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002560 strcpy(uinfo->value.enumerated.name,
2561 e->texts[uinfo->value.enumerated.item]);
2562 return 0;
2563}
2564EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2565
2566/**
2567 * snd_soc_info_volsw_ext - external single mixer info callback
2568 * @kcontrol: mixer control
2569 * @uinfo: control element information
2570 *
2571 * Callback to provide information about a single external mixer control.
2572 *
2573 * Returns 0 for success.
2574 */
2575int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2576 struct snd_ctl_elem_info *uinfo)
2577{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002578 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002579
Mark Brownfd5dfad2009-04-15 21:37:46 +01002580 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002581 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2582 else
2583 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2584
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002585 uinfo->count = 1;
2586 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002587 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002588 return 0;
2589}
2590EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2591
2592/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002593 * snd_soc_info_volsw - single mixer info callback
2594 * @kcontrol: mixer control
2595 * @uinfo: control element information
2596 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002597 * Callback to provide information about a single mixer control, or a double
2598 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002599 *
2600 * Returns 0 for success.
2601 */
2602int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2603 struct snd_ctl_elem_info *uinfo)
2604{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002605 struct soc_mixer_control *mc =
2606 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002607 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002608
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002609 if (!mc->platform_max)
2610 mc->platform_max = mc->max;
2611 platform_max = mc->platform_max;
2612
2613 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002614 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2615 else
2616 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2617
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002618 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002619 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002620 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002621 return 0;
2622}
2623EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2624
2625/**
2626 * snd_soc_get_volsw - single mixer get callback
2627 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002628 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002629 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002630 * Callback to get the value of a single mixer control, or a double mixer
2631 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002632 *
2633 * Returns 0 for success.
2634 */
2635int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2636 struct snd_ctl_elem_value *ucontrol)
2637{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002638 struct soc_mixer_control *mc =
2639 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002640 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002641 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002642 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002643 unsigned int shift = mc->shift;
2644 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002645 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002646 unsigned int mask = (1 << fls(max)) - 1;
2647 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002648
2649 ucontrol->value.integer.value[0] =
2650 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002651 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002652 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002653 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002654
2655 if (snd_soc_volsw_is_stereo(mc)) {
2656 if (reg == reg2)
2657 ucontrol->value.integer.value[1] =
2658 (snd_soc_read(codec, reg) >> rshift) & mask;
2659 else
2660 ucontrol->value.integer.value[1] =
2661 (snd_soc_read(codec, reg2) >> shift) & mask;
2662 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002663 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002664 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002665 }
2666
2667 return 0;
2668}
2669EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2670
2671/**
2672 * snd_soc_put_volsw - single mixer put callback
2673 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002674 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002675 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002676 * Callback to set the value of a single mixer control, or a double mixer
2677 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002678 *
2679 * Returns 0 for success.
2680 */
2681int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2682 struct snd_ctl_elem_value *ucontrol)
2683{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002684 struct soc_mixer_control *mc =
2685 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002686 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002687 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002688 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002689 unsigned int shift = mc->shift;
2690 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002691 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002692 unsigned int mask = (1 << fls(max)) - 1;
2693 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002694 int err;
2695 bool type_2r = 0;
2696 unsigned int val2 = 0;
2697 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002698
2699 val = (ucontrol->value.integer.value[0] & mask);
2700 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002701 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002702 val_mask = mask << shift;
2703 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002704 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002705 val2 = (ucontrol->value.integer.value[1] & mask);
2706 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002707 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002708 if (reg == reg2) {
2709 val_mask |= mask << rshift;
2710 val |= val2 << rshift;
2711 } else {
2712 val2 = val2 << shift;
2713 type_2r = 1;
2714 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002715 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002716 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002717 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002718 return err;
2719
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002720 if (type_2r)
2721 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2722
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002723 return err;
2724}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002725EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002726
Mark Browne13ac2e2008-05-28 17:58:05 +01002727/**
Brian Austin1d99f242012-03-30 10:43:55 -05002728 * snd_soc_get_volsw_sx - single mixer get callback
2729 * @kcontrol: mixer control
2730 * @ucontrol: control element information
2731 *
2732 * Callback to get the value of a single mixer control, or a double mixer
2733 * control that spans 2 registers.
2734 *
2735 * Returns 0 for success.
2736 */
2737int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2738 struct snd_ctl_elem_value *ucontrol)
2739{
2740 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2741 struct soc_mixer_control *mc =
2742 (struct soc_mixer_control *)kcontrol->private_value;
2743
2744 unsigned int reg = mc->reg;
2745 unsigned int reg2 = mc->rreg;
2746 unsigned int shift = mc->shift;
2747 unsigned int rshift = mc->rshift;
2748 int max = mc->max;
2749 int min = mc->min;
2750 int mask = (1 << (fls(min + max) - 1)) - 1;
2751
2752 ucontrol->value.integer.value[0] =
2753 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2754
2755 if (snd_soc_volsw_is_stereo(mc))
2756 ucontrol->value.integer.value[1] =
2757 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2758
2759 return 0;
2760}
2761EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2762
2763/**
2764 * snd_soc_put_volsw_sx - double mixer set callback
2765 * @kcontrol: mixer control
2766 * @uinfo: control element information
2767 *
2768 * Callback to set the value of a double mixer control that spans 2 registers.
2769 *
2770 * Returns 0 for success.
2771 */
2772int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2773 struct snd_ctl_elem_value *ucontrol)
2774{
2775 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2776 struct soc_mixer_control *mc =
2777 (struct soc_mixer_control *)kcontrol->private_value;
2778
2779 unsigned int reg = mc->reg;
2780 unsigned int reg2 = mc->rreg;
2781 unsigned int shift = mc->shift;
2782 unsigned int rshift = mc->rshift;
2783 int max = mc->max;
2784 int min = mc->min;
2785 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002786 int err = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002787 unsigned short val, val_mask, val2 = 0;
2788
2789 val_mask = mask << shift;
2790 val = (ucontrol->value.integer.value[0] + min) & mask;
2791 val = val << shift;
2792
2793 if (snd_soc_update_bits_locked(codec, reg, val_mask, val))
2794 return err;
2795
2796 if (snd_soc_volsw_is_stereo(mc)) {
2797 val_mask = mask << rshift;
2798 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2799 val2 = val2 << rshift;
2800
2801 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2802 return err;
2803 }
2804 return 0;
2805}
2806EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2807
2808/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002809 * snd_soc_info_volsw_s8 - signed mixer info callback
2810 * @kcontrol: mixer control
2811 * @uinfo: control element information
2812 *
2813 * Callback to provide information about a signed mixer control.
2814 *
2815 * Returns 0 for success.
2816 */
2817int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2818 struct snd_ctl_elem_info *uinfo)
2819{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002820 struct soc_mixer_control *mc =
2821 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002822 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002823 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002824
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002825 if (!mc->platform_max)
2826 mc->platform_max = mc->max;
2827 platform_max = mc->platform_max;
2828
Mark Browne13ac2e2008-05-28 17:58:05 +01002829 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2830 uinfo->count = 2;
2831 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002832 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002833 return 0;
2834}
2835EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2836
2837/**
2838 * snd_soc_get_volsw_s8 - signed mixer get callback
2839 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002840 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002841 *
2842 * Callback to get the value of a signed mixer control.
2843 *
2844 * Returns 0 for success.
2845 */
2846int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2847 struct snd_ctl_elem_value *ucontrol)
2848{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002849 struct soc_mixer_control *mc =
2850 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002851 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002852 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002853 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002854 int val = snd_soc_read(codec, reg);
2855
2856 ucontrol->value.integer.value[0] =
2857 ((signed char)(val & 0xff))-min;
2858 ucontrol->value.integer.value[1] =
2859 ((signed char)((val >> 8) & 0xff))-min;
2860 return 0;
2861}
2862EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2863
2864/**
2865 * snd_soc_put_volsw_sgn - signed mixer put callback
2866 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002867 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002868 *
2869 * Callback to set the value of a signed mixer control.
2870 *
2871 * Returns 0 for success.
2872 */
2873int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2874 struct snd_ctl_elem_value *ucontrol)
2875{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002876 struct soc_mixer_control *mc =
2877 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002878 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002879 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002880 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002881 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002882
2883 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2884 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2885
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002886 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002887}
2888EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2889
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002890/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002891 * snd_soc_info_volsw_range - single mixer info callback with range.
2892 * @kcontrol: mixer control
2893 * @uinfo: control element information
2894 *
2895 * Callback to provide information, within a range, about a single
2896 * mixer control.
2897 *
2898 * returns 0 for success.
2899 */
2900int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2901 struct snd_ctl_elem_info *uinfo)
2902{
2903 struct soc_mixer_control *mc =
2904 (struct soc_mixer_control *)kcontrol->private_value;
2905 int platform_max;
2906 int min = mc->min;
2907
2908 if (!mc->platform_max)
2909 mc->platform_max = mc->max;
2910 platform_max = mc->platform_max;
2911
2912 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2913 uinfo->count = 1;
2914 uinfo->value.integer.min = 0;
2915 uinfo->value.integer.max = platform_max - min;
2916
2917 return 0;
2918}
2919EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2920
2921/**
2922 * snd_soc_put_volsw_range - single mixer put value callback with range.
2923 * @kcontrol: mixer control
2924 * @ucontrol: control element information
2925 *
2926 * Callback to set the value, within a range, for a single mixer control.
2927 *
2928 * Returns 0 for success.
2929 */
2930int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2931 struct snd_ctl_elem_value *ucontrol)
2932{
2933 struct soc_mixer_control *mc =
2934 (struct soc_mixer_control *)kcontrol->private_value;
2935 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2936 unsigned int reg = mc->reg;
2937 unsigned int shift = mc->shift;
2938 int min = mc->min;
2939 int max = mc->max;
2940 unsigned int mask = (1 << fls(max)) - 1;
2941 unsigned int invert = mc->invert;
2942 unsigned int val, val_mask;
2943
2944 val = ((ucontrol->value.integer.value[0] + min) & mask);
2945 if (invert)
2946 val = max - val;
2947 val_mask = mask << shift;
2948 val = val << shift;
2949
2950 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2951}
2952EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2953
2954/**
2955 * snd_soc_get_volsw_range - single mixer get callback with range
2956 * @kcontrol: mixer control
2957 * @ucontrol: control element information
2958 *
2959 * Callback to get the value, within a range, of a single mixer control.
2960 *
2961 * Returns 0 for success.
2962 */
2963int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2964 struct snd_ctl_elem_value *ucontrol)
2965{
2966 struct soc_mixer_control *mc =
2967 (struct soc_mixer_control *)kcontrol->private_value;
2968 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2969 unsigned int reg = mc->reg;
2970 unsigned int shift = mc->shift;
2971 int min = mc->min;
2972 int max = mc->max;
2973 unsigned int mask = (1 << fls(max)) - 1;
2974 unsigned int invert = mc->invert;
2975
2976 ucontrol->value.integer.value[0] =
2977 (snd_soc_read(codec, reg) >> shift) & mask;
2978 if (invert)
2979 ucontrol->value.integer.value[0] =
2980 max - ucontrol->value.integer.value[0];
2981 ucontrol->value.integer.value[0] =
2982 ucontrol->value.integer.value[0] - min;
2983
2984 return 0;
2985}
2986EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2987
2988/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002989 * snd_soc_limit_volume - Set new limit to an existing volume control.
2990 *
2991 * @codec: where to look for the control
2992 * @name: Name of the control
2993 * @max: new maximum limit
2994 *
2995 * Return 0 for success, else error.
2996 */
2997int snd_soc_limit_volume(struct snd_soc_codec *codec,
2998 const char *name, int max)
2999{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003000 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003001 struct snd_kcontrol *kctl;
3002 struct soc_mixer_control *mc;
3003 int found = 0;
3004 int ret = -EINVAL;
3005
3006 /* Sanity check for name and max */
3007 if (unlikely(!name || max <= 0))
3008 return -EINVAL;
3009
3010 list_for_each_entry(kctl, &card->controls, list) {
3011 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3012 found = 1;
3013 break;
3014 }
3015 }
3016 if (found) {
3017 mc = (struct soc_mixer_control *)kctl->private_value;
3018 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003019 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003020 ret = 0;
3021 }
3022 }
3023 return ret;
3024}
3025EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3026
Mark Brown71d08512011-10-10 18:31:26 +01003027int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3028 struct snd_ctl_elem_info *uinfo)
3029{
3030 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3031 struct soc_bytes *params = (void *)kcontrol->private_value;
3032
3033 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3034 uinfo->count = params->num_regs * codec->val_bytes;
3035
3036 return 0;
3037}
3038EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3039
3040int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3041 struct snd_ctl_elem_value *ucontrol)
3042{
3043 struct soc_bytes *params = (void *)kcontrol->private_value;
3044 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3045 int ret;
3046
3047 if (codec->using_regmap)
3048 ret = regmap_raw_read(codec->control_data, params->base,
3049 ucontrol->value.bytes.data,
3050 params->num_regs * codec->val_bytes);
3051 else
3052 ret = -EINVAL;
3053
Mark Brownf831b052012-02-17 16:20:33 -08003054 /* Hide any masked bytes to ensure consistent data reporting */
3055 if (ret == 0 && params->mask) {
3056 switch (codec->val_bytes) {
3057 case 1:
3058 ucontrol->value.bytes.data[0] &= ~params->mask;
3059 break;
3060 case 2:
3061 ((u16 *)(&ucontrol->value.bytes.data))[0]
3062 &= ~params->mask;
3063 break;
3064 case 4:
3065 ((u32 *)(&ucontrol->value.bytes.data))[0]
3066 &= ~params->mask;
3067 break;
3068 default:
3069 return -EINVAL;
3070 }
3071 }
3072
Mark Brown71d08512011-10-10 18:31:26 +01003073 return ret;
3074}
3075EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3076
3077int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3078 struct snd_ctl_elem_value *ucontrol)
3079{
3080 struct soc_bytes *params = (void *)kcontrol->private_value;
3081 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08003082 int ret, len;
3083 unsigned int val;
3084 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003085
Mark Brownf831b052012-02-17 16:20:33 -08003086 if (!codec->using_regmap)
3087 return -EINVAL;
3088
3089 data = ucontrol->value.bytes.data;
3090 len = params->num_regs * codec->val_bytes;
3091
3092 /*
3093 * If we've got a mask then we need to preserve the register
3094 * bits. We shouldn't modify the incoming data so take a
3095 * copy.
3096 */
3097 if (params->mask) {
3098 ret = regmap_read(codec->control_data, params->base, &val);
3099 if (ret != 0)
3100 return ret;
3101
3102 val &= params->mask;
3103
3104 data = kmemdup(data, len, GFP_KERNEL);
3105 if (!data)
3106 return -ENOMEM;
3107
3108 switch (codec->val_bytes) {
3109 case 1:
3110 ((u8 *)data)[0] &= ~params->mask;
3111 ((u8 *)data)[0] |= val;
3112 break;
3113 case 2:
3114 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3115 ((u16 *)data)[0] |= cpu_to_be16(val);
3116 break;
3117 case 4:
3118 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3119 ((u32 *)data)[0] |= cpu_to_be32(val);
3120 break;
3121 default:
3122 return -EINVAL;
3123 }
3124 }
3125
3126 ret = regmap_raw_write(codec->control_data, params->base,
3127 data, len);
3128
3129 if (params->mask)
3130 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003131
3132 return ret;
3133}
3134EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3135
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003136/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003137 * snd_soc_info_xr_sx - signed multi register info callback
3138 * @kcontrol: mreg control
3139 * @uinfo: control element information
3140 *
3141 * Callback to provide information of a control that can
3142 * span multiple codec registers which together
3143 * forms a single signed value in a MSB/LSB manner.
3144 *
3145 * Returns 0 for success.
3146 */
3147int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3148 struct snd_ctl_elem_info *uinfo)
3149{
3150 struct soc_mreg_control *mc =
3151 (struct soc_mreg_control *)kcontrol->private_value;
3152 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3153 uinfo->count = 1;
3154 uinfo->value.integer.min = mc->min;
3155 uinfo->value.integer.max = mc->max;
3156
3157 return 0;
3158}
3159EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3160
3161/**
3162 * snd_soc_get_xr_sx - signed multi register get callback
3163 * @kcontrol: mreg control
3164 * @ucontrol: control element information
3165 *
3166 * Callback to get the value of a control that can span
3167 * multiple codec registers which together forms a single
3168 * signed value in a MSB/LSB manner. The control supports
3169 * specifying total no of bits used to allow for bitfields
3170 * across the multiple codec registers.
3171 *
3172 * Returns 0 for success.
3173 */
3174int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3175 struct snd_ctl_elem_value *ucontrol)
3176{
3177 struct soc_mreg_control *mc =
3178 (struct soc_mreg_control *)kcontrol->private_value;
3179 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3180 unsigned int regbase = mc->regbase;
3181 unsigned int regcount = mc->regcount;
3182 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3183 unsigned int regwmask = (1<<regwshift)-1;
3184 unsigned int invert = mc->invert;
3185 unsigned long mask = (1UL<<mc->nbits)-1;
3186 long min = mc->min;
3187 long max = mc->max;
3188 long val = 0;
3189 unsigned long regval;
3190 unsigned int i;
3191
3192 for (i = 0; i < regcount; i++) {
3193 regval = snd_soc_read(codec, regbase+i) & regwmask;
3194 val |= regval << (regwshift*(regcount-i-1));
3195 }
3196 val &= mask;
3197 if (min < 0 && val > max)
3198 val |= ~mask;
3199 if (invert)
3200 val = max - val;
3201 ucontrol->value.integer.value[0] = val;
3202
3203 return 0;
3204}
3205EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3206
3207/**
3208 * snd_soc_put_xr_sx - signed multi register get callback
3209 * @kcontrol: mreg control
3210 * @ucontrol: control element information
3211 *
3212 * Callback to set the value of a control that can span
3213 * multiple codec registers which together forms a single
3214 * signed value in a MSB/LSB manner. The control supports
3215 * specifying total no of bits used to allow for bitfields
3216 * across the multiple codec registers.
3217 *
3218 * Returns 0 for success.
3219 */
3220int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3221 struct snd_ctl_elem_value *ucontrol)
3222{
3223 struct soc_mreg_control *mc =
3224 (struct soc_mreg_control *)kcontrol->private_value;
3225 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3226 unsigned int regbase = mc->regbase;
3227 unsigned int regcount = mc->regcount;
3228 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3229 unsigned int regwmask = (1<<regwshift)-1;
3230 unsigned int invert = mc->invert;
3231 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003232 long max = mc->max;
3233 long val = ucontrol->value.integer.value[0];
3234 unsigned int i, regval, regmask;
3235 int err;
3236
3237 if (invert)
3238 val = max - val;
3239 val &= mask;
3240 for (i = 0; i < regcount; i++) {
3241 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3242 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3243 err = snd_soc_update_bits_locked(codec, regbase+i,
3244 regmask, regval);
3245 if (err < 0)
3246 return err;
3247 }
3248
3249 return 0;
3250}
3251EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3252
3253/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003254 * snd_soc_get_strobe - strobe get callback
3255 * @kcontrol: mixer control
3256 * @ucontrol: control element information
3257 *
3258 * Callback get the value of a strobe mixer control.
3259 *
3260 * Returns 0 for success.
3261 */
3262int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3263 struct snd_ctl_elem_value *ucontrol)
3264{
3265 struct soc_mixer_control *mc =
3266 (struct soc_mixer_control *)kcontrol->private_value;
3267 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3268 unsigned int reg = mc->reg;
3269 unsigned int shift = mc->shift;
3270 unsigned int mask = 1 << shift;
3271 unsigned int invert = mc->invert != 0;
3272 unsigned int val = snd_soc_read(codec, reg) & mask;
3273
3274 if (shift != 0 && val != 0)
3275 val = val >> shift;
3276 ucontrol->value.enumerated.item[0] = val ^ invert;
3277
3278 return 0;
3279}
3280EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3281
3282/**
3283 * snd_soc_put_strobe - strobe put callback
3284 * @kcontrol: mixer control
3285 * @ucontrol: control element information
3286 *
3287 * Callback strobe a register bit to high then low (or the inverse)
3288 * in one pass of a single mixer enum control.
3289 *
3290 * Returns 1 for success.
3291 */
3292int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3293 struct snd_ctl_elem_value *ucontrol)
3294{
3295 struct soc_mixer_control *mc =
3296 (struct soc_mixer_control *)kcontrol->private_value;
3297 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3298 unsigned int reg = mc->reg;
3299 unsigned int shift = mc->shift;
3300 unsigned int mask = 1 << shift;
3301 unsigned int invert = mc->invert != 0;
3302 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3303 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3304 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3305 int err;
3306
3307 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3308 if (err < 0)
3309 return err;
3310
3311 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3312 return err;
3313}
3314EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3315
3316/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003317 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3318 * @dai: DAI
3319 * @clk_id: DAI specific clock ID
3320 * @freq: new clock frequency in Hz
3321 * @dir: new clock direction - input/output.
3322 *
3323 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3324 */
3325int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3326 unsigned int freq, int dir)
3327{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003328 if (dai->driver && dai->driver->ops->set_sysclk)
3329 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003330 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003331 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003332 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003333 else
3334 return -EINVAL;
3335}
3336EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3337
3338/**
Mark Brownec4ee522011-03-07 20:58:11 +00003339 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3340 * @codec: CODEC
3341 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003342 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003343 * @freq: new clock frequency in Hz
3344 * @dir: new clock direction - input/output.
3345 *
3346 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3347 */
3348int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003349 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003350{
3351 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003352 return codec->driver->set_sysclk(codec, clk_id, source,
3353 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003354 else
3355 return -EINVAL;
3356}
3357EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3358
3359/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003360 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3361 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003362 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003363 * @div: new clock divisor.
3364 *
3365 * Configures the clock dividers. This is used to derive the best DAI bit and
3366 * frame clocks from the system or master clock. It's best to set the DAI bit
3367 * and frame clocks as low as possible to save system power.
3368 */
3369int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3370 int div_id, int div)
3371{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003372 if (dai->driver && dai->driver->ops->set_clkdiv)
3373 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003374 else
3375 return -EINVAL;
3376}
3377EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3378
3379/**
3380 * snd_soc_dai_set_pll - configure DAI PLL.
3381 * @dai: DAI
3382 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003383 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003384 * @freq_in: PLL input clock frequency in Hz
3385 * @freq_out: requested PLL output clock frequency in Hz
3386 *
3387 * Configures and enables PLL to generate output clock based on input clock.
3388 */
Mark Brown85488032009-09-05 18:52:16 +01003389int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3390 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003391{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003392 if (dai->driver && dai->driver->ops->set_pll)
3393 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003394 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003395 else if (dai->codec && dai->codec->driver->set_pll)
3396 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3397 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003398 else
3399 return -EINVAL;
3400}
3401EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3402
Mark Brownec4ee522011-03-07 20:58:11 +00003403/*
3404 * snd_soc_codec_set_pll - configure codec PLL.
3405 * @codec: CODEC
3406 * @pll_id: DAI specific PLL ID
3407 * @source: DAI specific source for the PLL
3408 * @freq_in: PLL input clock frequency in Hz
3409 * @freq_out: requested PLL output clock frequency in Hz
3410 *
3411 * Configures and enables PLL to generate output clock based on input clock.
3412 */
3413int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3414 unsigned int freq_in, unsigned int freq_out)
3415{
3416 if (codec->driver->set_pll)
3417 return codec->driver->set_pll(codec, pll_id, source,
3418 freq_in, freq_out);
3419 else
3420 return -EINVAL;
3421}
3422EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3423
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003424/**
3425 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3426 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003427 * @fmt: SND_SOC_DAIFMT_ format value.
3428 *
3429 * Configures the DAI hardware format and clocking.
3430 */
3431int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3432{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003433 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003434 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003435 if (dai->driver->ops->set_fmt == NULL)
3436 return -ENOTSUPP;
3437 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003438}
3439EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3440
3441/**
3442 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3443 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003444 * @tx_mask: bitmask representing active TX slots.
3445 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003446 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003447 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003448 *
3449 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3450 * specific.
3451 */
3452int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003453 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003454{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003455 if (dai->driver && dai->driver->ops->set_tdm_slot)
3456 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003457 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003458 else
3459 return -EINVAL;
3460}
3461EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3462
3463/**
Barry Song472df3c2009-09-12 01:16:29 +08003464 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3465 * @dai: DAI
3466 * @tx_num: how many TX channels
3467 * @tx_slot: pointer to an array which imply the TX slot number channel
3468 * 0~num-1 uses
3469 * @rx_num: how many RX channels
3470 * @rx_slot: pointer to an array which imply the RX slot number channel
3471 * 0~num-1 uses
3472 *
3473 * configure the relationship between channel number and TDM slot number.
3474 */
3475int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3476 unsigned int tx_num, unsigned int *tx_slot,
3477 unsigned int rx_num, unsigned int *rx_slot)
3478{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003479 if (dai->driver && dai->driver->ops->set_channel_map)
3480 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003481 rx_num, rx_slot);
3482 else
3483 return -EINVAL;
3484}
3485EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3486
3487/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003488 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3489 * @dai: DAI
3490 * @tristate: tristate enable
3491 *
3492 * Tristates the DAI so that others can use it.
3493 */
3494int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3495{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003496 if (dai->driver && dai->driver->ops->set_tristate)
3497 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003498 else
3499 return -EINVAL;
3500}
3501EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3502
3503/**
3504 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3505 * @dai: DAI
3506 * @mute: mute enable
3507 *
3508 * Mutes the DAI DAC.
3509 */
3510int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3511{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003512 if (dai->driver && dai->driver->ops->digital_mute)
3513 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003514 else
Mark Brown04570c62012-04-13 19:16:03 +01003515 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003516}
3517EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3518
Mark Brownc5af3a22008-11-28 13:29:45 +00003519/**
3520 * snd_soc_register_card - Register a card with the ASoC core
3521 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003522 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003523 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003524 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303525int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003526{
Mark Brownb19e6e72012-03-14 21:18:39 +00003527 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003528
Mark Brownc5af3a22008-11-28 13:29:45 +00003529 if (!card->name || !card->dev)
3530 return -EINVAL;
3531
Stephen Warren5a504962011-12-21 10:40:59 -07003532 for (i = 0; i < card->num_links; i++) {
3533 struct snd_soc_dai_link *link = &card->dai_link[i];
3534
3535 /*
3536 * Codec must be specified by 1 of name or OF node,
3537 * not both or neither.
3538 */
3539 if (!!link->codec_name == !!link->codec_of_node) {
3540 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003541 "Neither/both codec name/of_node are set for %s\n",
3542 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003543 return -EINVAL;
3544 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003545 /* Codec DAI name must be specified */
3546 if (!link->codec_dai_name) {
3547 dev_err(card->dev, "codec_dai_name not set for %s\n",
3548 link->name);
3549 return -EINVAL;
3550 }
Stephen Warren5a504962011-12-21 10:40:59 -07003551
3552 /*
3553 * Platform may be specified by either name or OF node, but
3554 * can be left unspecified, and a dummy platform will be used.
3555 */
3556 if (link->platform_name && link->platform_of_node) {
3557 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003558 "Both platform name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003559 return -EINVAL;
3560 }
3561
3562 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003563 * CPU device may be specified by either name or OF node, but
3564 * can be left unspecified, and will be matched based on DAI
3565 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003566 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003567 if (link->cpu_name && link->cpu_of_node) {
Stephen Warren5a504962011-12-21 10:40:59 -07003568 dev_err(card->dev,
Stephen Warrenbc926572012-05-25 18:22:11 -06003569 "Neither/both cpu name/of_node are set for %s\n",
3570 link->name);
3571 return -EINVAL;
3572 }
3573 /*
3574 * At least one of CPU DAI name or CPU device name/node must be
3575 * specified
3576 */
3577 if (!link->cpu_dai_name &&
3578 !(link->cpu_name || link->cpu_of_node)) {
3579 dev_err(card->dev,
3580 "Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003581 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003582 return -EINVAL;
3583 }
3584 }
3585
Mark Browned77cc12011-05-03 18:25:34 +01003586 dev_set_drvdata(card->dev, card);
3587
Stephen Warren111c6412011-01-28 14:26:35 -07003588 snd_soc_initialize_card_lists(card);
3589
Vinod Koul150dd2f2011-01-13 22:48:32 +05303590 soc_init_card_debugfs(card);
3591
Mark Brown181a6892012-03-14 20:18:49 +00003592 card->rtd = devm_kzalloc(card->dev,
3593 sizeof(struct snd_soc_pcm_runtime) *
3594 (card->num_links + card->num_aux_devs),
3595 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003596 if (card->rtd == NULL)
3597 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003598 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003599 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003600
3601 for (i = 0; i < card->num_links; i++)
3602 card->rtd[i].dai_link = &card->dai_link[i];
3603
Mark Brownc5af3a22008-11-28 13:29:45 +00003604 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003605 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003606 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003607 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003608 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003609
Mark Brownb19e6e72012-03-14 21:18:39 +00003610 ret = snd_soc_instantiate_card(card);
3611 if (ret != 0)
3612 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003613
Mark Brownb19e6e72012-03-14 21:18:39 +00003614 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003615}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303616EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003617
3618/**
3619 * snd_soc_unregister_card - Unregister a card with the ASoC core
3620 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003621 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003622 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003623 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303624int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003625{
Vinod Koulb0e26482011-01-13 22:48:02 +05303626 if (card->instantiated)
3627 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003628 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3629
3630 return 0;
3631}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303632EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003633
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003634/*
3635 * Simplify DAI link configuration by removing ".-1" from device names
3636 * and sanitizing names.
3637 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003638static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003639{
3640 char *found, name[NAME_SIZE];
3641 int id1, id2;
3642
3643 if (dev_name(dev) == NULL)
3644 return NULL;
3645
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003646 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003647
3648 /* are we a "%s.%d" name (platform and SPI components) */
3649 found = strstr(name, dev->driver->name);
3650 if (found) {
3651 /* get ID */
3652 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3653
3654 /* discard ID from name if ID == -1 */
3655 if (*id == -1)
3656 found[strlen(dev->driver->name)] = '\0';
3657 }
3658
3659 } else {
3660 /* I2C component devices are named "bus-addr" */
3661 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3662 char tmp[NAME_SIZE];
3663
3664 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003665 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003666
3667 /* sanitize component name for DAI link creation */
3668 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003669 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003670 } else
3671 *id = 0;
3672 }
3673
3674 return kstrdup(name, GFP_KERNEL);
3675}
3676
3677/*
3678 * Simplify DAI link naming for single devices with multiple DAIs by removing
3679 * any ".-1" and using the DAI name (instead of device name).
3680 */
3681static inline char *fmt_multiple_name(struct device *dev,
3682 struct snd_soc_dai_driver *dai_drv)
3683{
3684 if (dai_drv->name == NULL) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02003685 pr_err("asoc: error - multiple DAI %s registered with no name\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003686 dev_name(dev));
3687 return NULL;
3688 }
3689
3690 return kstrdup(dai_drv->name, GFP_KERNEL);
3691}
3692
Mark Brown91151712008-11-30 23:31:24 +00003693/**
3694 * snd_soc_register_dai - Register a DAI with the ASoC core
3695 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003696 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003697 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003698int snd_soc_register_dai(struct device *dev,
3699 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003700{
Mark Brown054880f2012-04-09 17:29:19 +01003701 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003702 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003703
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003704 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003705
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003706 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3707 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003708 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003709
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003710 /* create DAI component name */
3711 dai->name = fmt_single_name(dev, &dai->id);
3712 if (dai->name == NULL) {
3713 kfree(dai);
3714 return -ENOMEM;
3715 }
3716
3717 dai->dev = dev;
3718 dai->driver = dai_drv;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003719 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003720 if (!dai->driver->ops)
3721 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003722
3723 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003724
3725 list_for_each_entry(codec, &codec_list, list) {
3726 if (codec->dev == dev) {
3727 dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3728 dai->name, codec->name);
3729 dai->codec = codec;
3730 break;
3731 }
3732 }
3733
Peter Ujfalusi5f800082012-08-07 10:24:13 +03003734 if (!dai->codec)
3735 dai->dapm.idle_bias_off = 1;
3736
Mark Brown91151712008-11-30 23:31:24 +00003737 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003738
Mark Brown91151712008-11-30 23:31:24 +00003739 mutex_unlock(&client_mutex);
3740
3741 pr_debug("Registered DAI '%s'\n", dai->name);
3742
3743 return 0;
3744}
3745EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3746
3747/**
3748 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3749 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003750 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003751 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003752void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003753{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003754 struct snd_soc_dai *dai;
3755
3756 list_for_each_entry(dai, &dai_list, list) {
3757 if (dev == dai->dev)
3758 goto found;
3759 }
3760 return;
3761
3762found:
Mark Brown91151712008-11-30 23:31:24 +00003763 mutex_lock(&client_mutex);
3764 list_del(&dai->list);
3765 mutex_unlock(&client_mutex);
3766
3767 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003768 kfree(dai->name);
3769 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003770}
3771EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3772
3773/**
3774 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3775 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003776 * @dai: Array of DAIs to register
3777 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003778 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003779int snd_soc_register_dais(struct device *dev,
3780 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003781{
Mark Brown054880f2012-04-09 17:29:19 +01003782 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003783 struct snd_soc_dai *dai;
3784 int i, ret = 0;
3785
Liam Girdwood720ffa42010-08-18 00:30:30 +01003786 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003787
3788 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003789
3790 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003791 if (dai == NULL) {
3792 ret = -ENOMEM;
3793 goto err;
3794 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003795
3796 /* create DAI component name */
3797 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3798 if (dai->name == NULL) {
3799 kfree(dai);
3800 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003801 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003802 }
3803
3804 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003805 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003806 if (dai->driver->id)
3807 dai->id = dai->driver->id;
3808 else
3809 dai->id = i;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003810 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003811 if (!dai->driver->ops)
3812 dai->driver->ops = &null_dai_ops;
3813
3814 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003815
3816 list_for_each_entry(codec, &codec_list, list) {
3817 if (codec->dev == dev) {
3818 dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3819 dai->name, codec->name);
3820 dai->codec = codec;
3821 break;
3822 }
3823 }
3824
Peter Ujfalusi5f800082012-08-07 10:24:13 +03003825 if (!dai->codec)
3826 dai->dapm.idle_bias_off = 1;
3827
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003828 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003829
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003830 mutex_unlock(&client_mutex);
3831
3832 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003833 }
3834
3835 return 0;
3836
3837err:
3838 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003839 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003840
3841 return ret;
3842}
3843EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3844
3845/**
3846 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3847 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003848 * @dai: Array of DAIs to unregister
3849 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003850 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003851void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003852{
3853 int i;
3854
3855 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003856 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003857}
3858EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3859
Mark Brown12a48a8c2008-12-03 19:40:30 +00003860/**
3861 * snd_soc_register_platform - Register a platform with the ASoC core
3862 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003863 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003864 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003865int snd_soc_register_platform(struct device *dev,
3866 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003867{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003868 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003869
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003870 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3871
3872 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3873 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003874 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003875
3876 /* create platform component name */
3877 platform->name = fmt_single_name(dev, &platform->id);
3878 if (platform->name == NULL) {
3879 kfree(platform);
3880 return -ENOMEM;
3881 }
3882
3883 platform->dev = dev;
3884 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003885 platform->dapm.dev = dev;
3886 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003887 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00003888 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003889
3890 mutex_lock(&client_mutex);
3891 list_add(&platform->list, &platform_list);
3892 mutex_unlock(&client_mutex);
3893
3894 pr_debug("Registered platform '%s'\n", platform->name);
3895
3896 return 0;
3897}
3898EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3899
3900/**
3901 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3902 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003903 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003904 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003905void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003906{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003907 struct snd_soc_platform *platform;
3908
3909 list_for_each_entry(platform, &platform_list, list) {
3910 if (dev == platform->dev)
3911 goto found;
3912 }
3913 return;
3914
3915found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003916 mutex_lock(&client_mutex);
3917 list_del(&platform->list);
3918 mutex_unlock(&client_mutex);
3919
3920 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003921 kfree(platform->name);
3922 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003923}
3924EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3925
Mark Brown151ab222009-05-09 16:22:58 +01003926static u64 codec_format_map[] = {
3927 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3928 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3929 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3930 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3931 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3932 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3933 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3934 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3935 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3936 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3937 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3938 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3939 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3940 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3941 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3942 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3943};
3944
3945/* Fix up the DAI formats for endianness: codecs don't actually see
3946 * the endianness of the data but we're using the CPU format
3947 * definitions which do need to include endianness so we ensure that
3948 * codec DAIs always have both big and little endian variants set.
3949 */
3950static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3951{
3952 int i;
3953
3954 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3955 if (stream->formats & codec_format_map[i])
3956 stream->formats |= codec_format_map[i];
3957}
3958
Mark Brown0d0cf002008-12-10 14:32:45 +00003959/**
3960 * snd_soc_register_codec - Register a codec with the ASoC core
3961 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003962 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003963 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003964int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003965 const struct snd_soc_codec_driver *codec_drv,
3966 struct snd_soc_dai_driver *dai_drv,
3967 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003968{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003969 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003970 struct snd_soc_codec *codec;
3971 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003972
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003973 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003974
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003975 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3976 if (codec == NULL)
3977 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003978
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003979 /* create CODEC component name */
3980 codec->name = fmt_single_name(dev, &codec->id);
3981 if (codec->name == NULL) {
3982 kfree(codec);
3983 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003984 }
3985
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003986 if (codec_drv->compress_type)
3987 codec->compress_type = codec_drv->compress_type;
3988 else
3989 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3990
Mark Brownc3acec22010-12-02 16:15:29 +00003991 codec->write = codec_drv->write;
3992 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003993 codec->volatile_register = codec_drv->volatile_register;
3994 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003995 codec->writable_register = codec_drv->writable_register;
Mark Brown5124e692012-02-08 13:20:50 +00003996 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003997 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3998 codec->dapm.dev = dev;
3999 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00004000 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01004001 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004002 codec->dev = dev;
4003 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004004 codec->num_dai = num_dai;
4005 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004006
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004007 /* allocate CODEC register cache */
4008 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004009 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00004010 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004011 /* it is necessary to make a copy of the default register cache
4012 * because in the case of using a compression type that requires
4013 * the default register cache to be marked as __devinitconst the
4014 * kernel might have freed the array by the time we initialize
4015 * the cache.
4016 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00004017 if (codec_drv->reg_cache_default) {
4018 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
4019 reg_size, GFP_KERNEL);
4020 if (!codec->reg_def_copy) {
4021 ret = -ENOMEM;
4022 goto fail;
4023 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004024 }
4025 }
4026
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004027 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
4028 if (!codec->volatile_register)
4029 codec->volatile_register = snd_soc_default_volatile_register;
4030 if (!codec->readable_register)
4031 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004032 if (!codec->writable_register)
4033 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004034 }
4035
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004036 for (i = 0; i < num_dai; i++) {
4037 fixup_codec_formats(&dai_drv[i].playback);
4038 fixup_codec_formats(&dai_drv[i].capture);
4039 }
4040
Mark Brown054880f2012-04-09 17:29:19 +01004041 mutex_lock(&client_mutex);
4042 list_add(&codec->list, &codec_list);
4043 mutex_unlock(&client_mutex);
4044
Mark Brown26b01cc2010-08-18 20:20:55 +01004045 /* register any DAIs */
4046 if (num_dai) {
4047 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4048 if (ret < 0)
Mark Brown054880f2012-04-09 17:29:19 +01004049 dev_err(codec->dev, "Failed to regster DAIs: %d\n",
4050 ret);
Mark Brown26b01cc2010-08-18 20:20:55 +01004051 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004052
Mark Brown0d0cf002008-12-10 14:32:45 +00004053 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004054 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004055
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00004056fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004057 kfree(codec->reg_def_copy);
4058 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004059 kfree(codec->name);
4060 kfree(codec);
4061 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004062}
4063EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4064
4065/**
4066 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4067 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004068 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004069 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004070void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004071{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004072 struct snd_soc_codec *codec;
4073 int i;
4074
4075 list_for_each_entry(codec, &codec_list, list) {
4076 if (dev == codec->dev)
4077 goto found;
4078 }
4079 return;
4080
4081found:
Mark Brown26b01cc2010-08-18 20:20:55 +01004082 if (codec->num_dai)
4083 for (i = 0; i < codec->num_dai; i++)
4084 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004085
Mark Brown0d0cf002008-12-10 14:32:45 +00004086 mutex_lock(&client_mutex);
4087 list_del(&codec->list);
4088 mutex_unlock(&client_mutex);
4089
4090 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004091
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004092 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00004093 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01004094 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004095 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004096}
4097EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4098
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004099/* Retrieve a card's name from device tree */
4100int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4101 const char *propname)
4102{
4103 struct device_node *np = card->dev->of_node;
4104 int ret;
4105
4106 ret = of_property_read_string_index(np, propname, 0, &card->name);
4107 /*
4108 * EINVAL means the property does not exist. This is fine providing
4109 * card->name was previously set, which is checked later in
4110 * snd_soc_register_card.
4111 */
4112 if (ret < 0 && ret != -EINVAL) {
4113 dev_err(card->dev,
4114 "Property '%s' could not be read: %d\n",
4115 propname, ret);
4116 return ret;
4117 }
4118
4119 return 0;
4120}
4121EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4122
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004123int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4124 const char *propname)
4125{
4126 struct device_node *np = card->dev->of_node;
4127 int num_routes;
4128 struct snd_soc_dapm_route *routes;
4129 int i, ret;
4130
4131 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004132 if (num_routes < 0 || num_routes & 1) {
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004133 dev_err(card->dev,
Richard Zhaoc34ce322012-04-24 15:24:43 +08004134 "Property '%s' does not exist or its length is not even\n",
4135 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004136 return -EINVAL;
4137 }
4138 num_routes /= 2;
4139 if (!num_routes) {
4140 dev_err(card->dev,
4141 "Property '%s's length is zero\n",
4142 propname);
4143 return -EINVAL;
4144 }
4145
4146 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4147 GFP_KERNEL);
4148 if (!routes) {
4149 dev_err(card->dev,
4150 "Could not allocate DAPM route table\n");
4151 return -EINVAL;
4152 }
4153
4154 for (i = 0; i < num_routes; i++) {
4155 ret = of_property_read_string_index(np, propname,
4156 2 * i, &routes[i].sink);
4157 if (ret) {
4158 dev_err(card->dev,
4159 "Property '%s' index %d could not be read: %d\n",
4160 propname, 2 * i, ret);
Matthias Kaehlckeb761c0c2012-07-11 17:36:34 +02004161 kfree(routes);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004162 return -EINVAL;
4163 }
4164 ret = of_property_read_string_index(np, propname,
4165 (2 * i) + 1, &routes[i].source);
4166 if (ret) {
4167 dev_err(card->dev,
4168 "Property '%s' index %d could not be read: %d\n",
4169 propname, (2 * i) + 1, ret);
Matthias Kaehlckeb761c0c2012-07-11 17:36:34 +02004170 kfree(routes);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004171 return -EINVAL;
4172 }
4173 }
4174
4175 card->num_dapm_routes = num_routes;
4176 card->dapm_routes = routes;
4177
4178 return 0;
4179}
4180EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4181
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004182static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004183{
Mark Brown384c89e2008-12-03 17:34:03 +00004184#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004185 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4186 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004187 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004188 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004189 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004190
Mark Brown8a9dab12011-01-10 22:25:21 +00004191 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004192 &codec_list_fops))
4193 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4194
Mark Brown8a9dab12011-01-10 22:25:21 +00004195 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004196 &dai_list_fops))
4197 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004198
Mark Brown8a9dab12011-01-10 22:25:21 +00004199 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004200 &platform_list_fops))
4201 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004202#endif
4203
Mark Brownfb257892011-04-28 10:57:54 +01004204 snd_soc_util_init();
4205
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004206 return platform_driver_register(&soc_driver);
4207}
Mark Brown4abe8e12010-10-12 17:41:03 +01004208module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004209
Mark Brown7d8c16a2008-11-30 22:11:24 +00004210static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004211{
Mark Brownfb257892011-04-28 10:57:54 +01004212 snd_soc_util_exit();
4213
Mark Brown384c89e2008-12-03 17:34:03 +00004214#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004215 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004216#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004217 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004218}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004219module_exit(snd_soc_exit);
4220
4221/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004222MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004223MODULE_DESCRIPTION("ALSA SoC Core");
4224MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004225MODULE_ALIAS("platform:soc-audio");