blob: 01220a5cf43e0f2db8584b75840022f32d0db97d [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
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200468#ifdef CONFIG_SND_SOC_AC97_BUS
469/* unregister ac97 codec */
470static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
471{
472 if (codec->ac97->dev.bus)
473 device_unregister(&codec->ac97->dev);
474 return 0;
475}
476
477/* stop no dev release warning */
478static void soc_ac97_device_release(struct device *dev){}
479
480/* register ac97 codec to bus */
481static int soc_ac97_dev_register(struct snd_soc_codec *codec)
482{
483 int err;
484
485 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100486 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200487 codec->ac97->dev.release = soc_ac97_device_release;
488
Kay Sieversbb072bf2008-11-02 03:50:35 +0100489 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000490 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200491 err = device_register(&codec->ac97->dev);
492 if (err < 0) {
493 snd_printk(KERN_ERR "Can't register ac97 bus\n");
494 codec->ac97->dev.bus = NULL;
495 return err;
496 }
497 return 0;
498}
499#endif
500
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000501#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200502/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000503int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200504{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000505 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200506 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200507 int i;
508
Daniel Macke3509ff2009-06-03 17:44:49 +0200509 /* If the initialization of this soc device failed, there is no codec
510 * associated with it. Just bail out in this case.
511 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000512 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200513 return 0;
514
Andy Green6ed25972008-06-13 16:24:05 +0100515 /* Due to the resume being scheduled into a workqueue we could
516 * suspend before that's finished - wait for it to complete.
517 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000518 snd_power_lock(card->snd_card);
519 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
520 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100521
522 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000523 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100524
Mark Browna00f90f2010-12-02 16:24:24 +0000525 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000526 for (i = 0; i < card->num_rtd; i++) {
527 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
528 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100529
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000530 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100531 continue;
532
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000533 if (drv->ops->digital_mute && dai->playback_active)
534 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200535 }
536
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100537 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000538 for (i = 0; i < card->num_rtd; i++) {
539 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100540 continue;
541
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000542 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100543 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100544
Mark Brown87506542008-11-18 20:50:34 +0000545 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000546 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200547
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000548 for (i = 0; i < card->num_rtd; i++) {
549 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
550 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100551
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100553 continue;
554
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000555 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
556 cpu_dai->driver->suspend(cpu_dai);
557 if (platform->driver->suspend && !platform->suspended) {
558 platform->driver->suspend(cpu_dai);
559 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100560 }
561 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200562
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000563 /* close any waiting streams and save state */
564 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +0100565 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200566 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000567 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100568
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000570
571 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100572 continue;
573
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800574 snd_soc_dapm_stream_event(&card->rtd[i],
575 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800576 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000577
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800578 snd_soc_dapm_stream_event(&card->rtd[i],
579 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800580 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000581 }
582
583 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200584 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000585 /* If there are paths active then the CODEC will be held with
586 * bias _ON and should not be suspended. */
587 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200588 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000589 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000590 /*
591 * If the CODEC is capable of idle
592 * bias off then being in STANDBY
593 * means it's doing something,
594 * otherwise fall through.
595 */
596 if (codec->dapm.idle_bias_off) {
597 dev_dbg(codec->dev,
598 "idle_bias_off CODEC on over suspend\n");
599 break;
600 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000601 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100602 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000603 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900604 codec->cache_sync = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000605 break;
606 default:
607 dev_dbg(codec->dev, "CODEC is on over suspend\n");
608 break;
609 }
610 }
611 }
612
613 for (i = 0; i < card->num_rtd; i++) {
614 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
615
616 if (card->rtd[i].dai_link->ignore_suspend)
617 continue;
618
619 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
620 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200621 }
622
Mark Brown87506542008-11-18 20:50:34 +0000623 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000624 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200625
626 return 0;
627}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000628EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200629
Andy Green6ed25972008-06-13 16:24:05 +0100630/* deferred resume work, so resume can complete before we finished
631 * setting our codec back up, which can be very slow on I2C
632 */
633static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200634{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000635 struct snd_soc_card *card =
636 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200637 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200638 int i;
639
Andy Green6ed25972008-06-13 16:24:05 +0100640 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
641 * so userspace apps are blocked from touching us
642 */
643
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000644 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100645
Mark Brown99497882010-05-07 20:24:05 +0100646 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000647 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100648
Mark Brown87506542008-11-18 20:50:34 +0000649 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000650 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200651
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000652 /* resume AC97 DAIs */
653 for (i = 0; i < card->num_rtd; i++) {
654 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100655
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000656 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100657 continue;
658
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000659 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
660 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200661 }
662
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200663 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000664 /* If the CODEC was idle over suspend then it will have been
665 * left with bias OFF or STANDBY and suspended so we must now
666 * resume. Otherwise the suspend was suppressed.
667 */
668 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200669 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000670 case SND_SOC_BIAS_STANDBY:
671 case SND_SOC_BIAS_OFF:
672 codec->driver->resume(codec);
673 codec->suspended = 0;
674 break;
675 default:
676 dev_dbg(codec->dev, "CODEC was on over suspend\n");
677 break;
678 }
Mark Brown1547aba2010-05-07 21:11:40 +0100679 }
680 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200681
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000682 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100683
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000684 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100685 continue;
686
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800687 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000688 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800689 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000690
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800691 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000692 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800693 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200694 }
695
Mark Brown3ff3f642008-05-19 12:32:25 +0200696 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000697 for (i = 0; i < card->num_rtd; i++) {
698 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
699 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100700
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000701 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100702 continue;
703
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000704 if (drv->ops->digital_mute && dai->playback_active)
705 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200706 }
707
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000708 for (i = 0; i < card->num_rtd; i++) {
709 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
710 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100711
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000712 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100713 continue;
714
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000715 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
716 cpu_dai->driver->resume(cpu_dai);
717 if (platform->driver->resume && platform->suspended) {
718 platform->driver->resume(cpu_dai);
719 platform->suspended = 0;
720 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200721 }
722
Mark Brown87506542008-11-18 20:50:34 +0000723 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000724 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200725
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000726 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100727
728 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000729 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100730}
731
732/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000733int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100734{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000735 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600736 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200737
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800738 /* If the initialization of this soc device failed, there is no codec
739 * associated with it. Just bail out in this case.
740 */
741 if (list_empty(&card->codec_dev_list))
742 return 0;
743
Mark Brown64ab9ba2009-03-31 11:27:03 +0100744 /* AC97 devices might have other drivers hanging off them so
745 * need to resume immediately. Other drivers don't have that
746 * problem and may take a substantial amount of time to resume
747 * due to I/O costs and anti-pop so handle them out of line.
748 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000749 for (i = 0; i < card->num_rtd; i++) {
750 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600751 ac97_control |= cpu_dai->driver->ac97_control;
752 }
753 if (ac97_control) {
754 dev_dbg(dev, "Resuming AC97 immediately\n");
755 soc_resume_deferred(&card->deferred_resume_work);
756 } else {
757 dev_dbg(dev, "Scheduling resume work\n");
758 if (!schedule_work(&card->deferred_resume_work))
759 dev_err(dev, "resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100760 }
Andy Green6ed25972008-06-13 16:24:05 +0100761
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200762 return 0;
763}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000764EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200765#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000766#define snd_soc_suspend NULL
767#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200768#endif
769
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100770static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800771};
772
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000773static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200774{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000775 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
776 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000777 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000778 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000779 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100780 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200781
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000782 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000783
Mark Brownb19e6e72012-03-14 21:18:39 +0000784 /* Find CPU DAI from registered DAIs*/
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000785 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700786 if (dai_link->cpu_dai_of_node) {
787 if (cpu_dai->dev->of_node != dai_link->cpu_dai_of_node)
788 continue;
789 } else {
790 if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
791 continue;
792 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700793
794 rtd->cpu_dai = cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000795 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000796
797 if (!rtd->cpu_dai) {
798 dev_dbg(card->dev, "CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000799 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000800 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000801 }
802
Mark Brownb19e6e72012-03-14 21:18:39 +0000803 /* Find CODEC from registered CODECs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000804 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700805 if (dai_link->codec_of_node) {
806 if (codec->dev->of_node != dai_link->codec_of_node)
807 continue;
808 } else {
809 if (strcmp(codec->name, dai_link->codec_name))
810 continue;
811 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000812
Stephen Warren2610ab72011-12-07 13:58:27 -0700813 rtd->codec = codec;
814
815 /*
816 * CODEC found, so find CODEC DAI from registered DAIs from
817 * this CODEC
818 */
819 list_for_each_entry(codec_dai, &dai_list, list) {
820 if (codec->dev == codec_dai->dev &&
821 !strcmp(codec_dai->name,
822 dai_link->codec_dai_name)) {
823
824 rtd->codec_dai = codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000825 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000826 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000827
828 if (!rtd->codec_dai) {
829 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
Stephen Warren2610ab72011-12-07 13:58:27 -0700830 dai_link->codec_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000831 return -EPROBE_DEFER;
832 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000833 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000834
Mark Brownb19e6e72012-03-14 21:18:39 +0000835 if (!rtd->codec) {
836 dev_dbg(card->dev, "CODEC %s not registered\n",
837 dai_link->codec_name);
838 return -EPROBE_DEFER;
839 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100840
841 /* if there's no platform we match on the empty platform */
842 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700843 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100844 platform_name = "snd-soc-dummy";
845
Mark Brownb19e6e72012-03-14 21:18:39 +0000846 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000847 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700848 if (dai_link->platform_of_node) {
849 if (platform->dev->of_node !=
850 dai_link->platform_of_node)
851 continue;
852 } else {
853 if (strcmp(platform->name, platform_name))
854 continue;
855 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700856
857 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000858 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000859 if (!rtd->platform) {
860 dev_dbg(card->dev, "platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000861 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000862 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000863 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000864
865 card->num_rtd++;
866
867 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000868}
869
Jarkko Nikula589c3562010-12-06 16:27:07 +0200870static void soc_remove_codec(struct snd_soc_codec *codec)
871{
872 int err;
873
874 if (codec->driver->remove) {
875 err = codec->driver->remove(codec);
876 if (err < 0)
877 dev_err(codec->dev,
878 "asoc: failed to remove %s: %d\n",
879 codec->name, err);
880 }
881
882 /* Make sure all DAPM widgets are freed */
883 snd_soc_dapm_free(&codec->dapm);
884
885 soc_cleanup_codec_debugfs(codec);
886 codec->probed = 0;
887 list_del(&codec->card_list);
888 module_put(codec->dev->driver->owner);
889}
890
Liam Girdwood0168bf02011-06-07 16:08:05 +0100891static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000892{
893 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
894 struct snd_soc_codec *codec = rtd->codec;
895 struct snd_soc_platform *platform = rtd->platform;
896 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
897 int err;
898
899 /* unregister the rtd device */
900 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800901 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
902 device_remove_file(rtd->dev, &dev_attr_codec_reg);
903 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000904 rtd->dev_registered = 0;
905 }
906
907 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100908 if (codec_dai && codec_dai->probed &&
909 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000910 if (codec_dai->driver->remove) {
911 err = codec_dai->driver->remove(codec_dai);
912 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200913 pr_err("asoc: failed to remove %s: %d\n",
914 codec_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000915 }
916 codec_dai->probed = 0;
917 list_del(&codec_dai->card_list);
918 }
919
920 /* remove the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100921 if (platform && platform->probed &&
922 platform->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000923 if (platform->driver->remove) {
924 err = platform->driver->remove(platform);
925 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200926 pr_err("asoc: failed to remove %s: %d\n",
927 platform->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000928 }
Liam Girdwood675c4962012-01-16 15:25:37 +0000929
930 /* Make sure all DAPM widgets are freed */
931 snd_soc_dapm_free(&platform->dapm);
932
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000933 soc_cleanup_platform_debugfs(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000934 platform->probed = 0;
935 list_del(&platform->card_list);
936 module_put(platform->dev->driver->owner);
937 }
938
939 /* remove the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100940 if (codec && codec->probed &&
941 codec->driver->remove_order == order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200942 soc_remove_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000943
944 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100945 if (cpu_dai && cpu_dai->probed &&
946 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000947 if (cpu_dai->driver->remove) {
948 err = cpu_dai->driver->remove(cpu_dai);
949 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200950 pr_err("asoc: failed to remove %s: %d\n",
951 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000952 }
953 cpu_dai->probed = 0;
954 list_del(&cpu_dai->card_list);
955 module_put(cpu_dai->dev->driver->owner);
956 }
957}
958
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900959static void soc_remove_dai_links(struct snd_soc_card *card)
960{
Liam Girdwood0168bf02011-06-07 16:08:05 +0100961 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900962
Liam Girdwood0168bf02011-06-07 16:08:05 +0100963 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
964 order++) {
965 for (dai = 0; dai < card->num_rtd; dai++)
966 soc_remove_dai_link(card, dai, order);
967 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900968 card->num_rtd = 0;
969}
970
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200971static void soc_set_name_prefix(struct snd_soc_card *card,
972 struct snd_soc_codec *codec)
973{
974 int i;
975
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000976 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200977 return;
978
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000979 for (i = 0; i < card->num_configs; i++) {
980 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200981 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
982 codec->name_prefix = map->name_prefix;
983 break;
984 }
985 }
986}
987
Jarkko Nikula589c3562010-12-06 16:27:07 +0200988static int soc_probe_codec(struct snd_soc_card *card,
989 struct snd_soc_codec *codec)
990{
991 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +0000992 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -0800993 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +0200994
995 codec->card = card;
996 codec->dapm.card = card;
997 soc_set_name_prefix(card, codec);
998
Jarkko Nikula70d29332011-01-27 16:24:22 +0200999 if (!try_module_get(codec->dev->driver->owner))
1000 return -ENODEV;
1001
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001002 soc_init_codec_debugfs(codec);
1003
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001004 if (driver->dapm_widgets)
1005 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1006 driver->num_dapm_widgets);
1007
Mark Brown888df392012-02-16 19:37:51 -08001008 /* Create DAPM widgets for each DAI stream */
1009 list_for_each_entry(dai, &dai_list, list) {
1010 if (dai->dev != codec->dev)
1011 continue;
1012
1013 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1014 }
1015
Mark Brown33c5f962011-08-22 18:40:30 +01001016 codec->dapm.idle_bias_off = driver->idle_bias_off;
1017
Mark Brown89b95ac02011-03-07 16:38:44 +00001018 if (driver->probe) {
1019 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001020 if (ret < 0) {
1021 dev_err(codec->dev,
1022 "asoc: failed to probe CODEC %s: %d\n",
1023 codec->name, ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001024 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001025 }
1026 }
1027
Mark Brownb7af1da2011-04-07 19:18:44 +09001028 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001029 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001030 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001031 if (driver->dapm_routes)
1032 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1033 driver->num_dapm_routes);
1034
Jarkko Nikula589c3562010-12-06 16:27:07 +02001035 /* mark codec as probed and add to card codec list */
1036 codec->probed = 1;
1037 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001038 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001039
Jarkko Nikula70d29332011-01-27 16:24:22 +02001040 return 0;
1041
1042err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001043 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001044 module_put(codec->dev->driver->owner);
1045
Jarkko Nikula589c3562010-12-06 16:27:07 +02001046 return ret;
1047}
1048
Liam Girdwood956245e2011-07-01 16:54:08 +01001049static int soc_probe_platform(struct snd_soc_card *card,
1050 struct snd_soc_platform *platform)
1051{
1052 int ret = 0;
1053 const struct snd_soc_platform_driver *driver = platform->driver;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001054 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001055
1056 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001057 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001058
1059 if (!try_module_get(platform->dev->driver->owner))
1060 return -ENODEV;
1061
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001062 soc_init_platform_debugfs(platform);
1063
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001064 if (driver->dapm_widgets)
1065 snd_soc_dapm_new_controls(&platform->dapm,
1066 driver->dapm_widgets, driver->num_dapm_widgets);
1067
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001068 /* Create DAPM widgets for each DAI stream */
1069 list_for_each_entry(dai, &dai_list, list) {
1070 if (dai->dev != platform->dev)
1071 continue;
1072
1073 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1074 }
1075
Stephen Warren3fec6b62012-04-05 12:28:01 -06001076 platform->dapm.idle_bias_off = 1;
1077
Liam Girdwood956245e2011-07-01 16:54:08 +01001078 if (driver->probe) {
1079 ret = driver->probe(platform);
1080 if (ret < 0) {
1081 dev_err(platform->dev,
1082 "asoc: failed to probe platform %s: %d\n",
1083 platform->name, ret);
1084 goto err_probe;
1085 }
1086 }
1087
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001088 if (driver->controls)
1089 snd_soc_add_platform_controls(platform, driver->controls,
1090 driver->num_controls);
1091 if (driver->dapm_routes)
1092 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1093 driver->num_dapm_routes);
1094
Liam Girdwood956245e2011-07-01 16:54:08 +01001095 /* mark platform as probed and add to card platform list */
1096 platform->probed = 1;
1097 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001098 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001099
1100 return 0;
1101
1102err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001103 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001104 module_put(platform->dev->driver->owner);
1105
1106 return ret;
1107}
1108
Mark Brown36ae1a92012-01-06 17:12:45 -08001109static void rtd_release(struct device *dev)
1110{
1111 kfree(dev);
1112}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001113
Jarkko Nikula589c3562010-12-06 16:27:07 +02001114static int soc_post_component_init(struct snd_soc_card *card,
1115 struct snd_soc_codec *codec,
1116 int num, int dailess)
1117{
1118 struct snd_soc_dai_link *dai_link = NULL;
1119 struct snd_soc_aux_dev *aux_dev = NULL;
1120 struct snd_soc_pcm_runtime *rtd;
1121 const char *temp, *name;
1122 int ret = 0;
1123
1124 if (!dailess) {
1125 dai_link = &card->dai_link[num];
1126 rtd = &card->rtd[num];
1127 name = dai_link->name;
1128 } else {
1129 aux_dev = &card->aux_dev[num];
1130 rtd = &card->rtd_aux[num];
1131 name = aux_dev->name;
1132 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001133 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001134
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001135 /* Make sure all DAPM widgets are instantiated */
1136 snd_soc_dapm_new_widgets(&codec->dapm);
1137
Jarkko Nikula589c3562010-12-06 16:27:07 +02001138 /* machine controls, routes and widgets are not prefixed */
1139 temp = codec->name_prefix;
1140 codec->name_prefix = NULL;
1141
1142 /* do machine specific initialization */
1143 if (!dailess && dai_link->init)
1144 ret = dai_link->init(rtd);
1145 else if (dailess && aux_dev->init)
1146 ret = aux_dev->init(&codec->dapm);
1147 if (ret < 0) {
1148 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1149 return ret;
1150 }
1151 codec->name_prefix = temp;
1152
Jarkko Nikula589c3562010-12-06 16:27:07 +02001153 /* register the rtd device */
1154 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001155
1156 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1157 if (!rtd->dev)
1158 return -ENOMEM;
1159 device_initialize(rtd->dev);
1160 rtd->dev->parent = card->dev;
1161 rtd->dev->release = rtd_release;
1162 rtd->dev->init_name = name;
1163 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001164 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001165 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1166 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1167 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1168 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001169 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001170 if (ret < 0) {
1171 dev_err(card->dev,
1172 "asoc: failed to register runtime device: %d\n", ret);
1173 return ret;
1174 }
1175 rtd->dev_registered = 1;
1176
1177 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001178 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001179 if (ret < 0)
1180 dev_err(codec->dev,
1181 "asoc: failed to add codec dapm sysfs entries: %d\n",
1182 ret);
1183
1184 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001185 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001186 if (ret < 0)
1187 dev_err(codec->dev,
1188 "asoc: failed to add codec sysfs files: %d\n", ret);
1189
1190 return 0;
1191}
1192
Liam Girdwood0168bf02011-06-07 16:08:05 +01001193static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001194{
1195 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1196 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1197 struct snd_soc_codec *codec = rtd->codec;
1198 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001199 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1200 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1201 struct snd_soc_dapm_widget *play_w, *capture_w;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001202 int ret;
1203
Liam Girdwood0168bf02011-06-07 16:08:05 +01001204 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1205 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001206
1207 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001208 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001209 codec_dai->card = card;
1210 cpu_dai->card = card;
1211
1212 /* set default power off timeout */
1213 rtd->pmdown_time = pmdown_time;
1214
1215 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001216 if (!cpu_dai->probed &&
1217 cpu_dai->driver->probe_order == order) {
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001218 cpu_dai->dapm.card = card;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001219 if (!try_module_get(cpu_dai->dev->driver->owner))
1220 return -ENODEV;
1221
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001222 snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1223
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001224 if (cpu_dai->driver->probe) {
1225 ret = cpu_dai->driver->probe(cpu_dai);
1226 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001227 pr_err("asoc: failed to probe CPU DAI %s: %d\n",
1228 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001229 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001230 return ret;
1231 }
1232 }
1233 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001234 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001235 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1236 }
1237
1238 /* probe the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001239 if (!codec->probed &&
1240 codec->driver->probe_order == order) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001241 ret = soc_probe_codec(card, codec);
1242 if (ret < 0)
1243 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001244 }
1245
1246 /* probe the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001247 if (!platform->probed &&
1248 platform->driver->probe_order == order) {
Liam Girdwood956245e2011-07-01 16:54:08 +01001249 ret = soc_probe_platform(card, platform);
1250 if (ret < 0)
1251 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001252 }
1253
1254 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001255 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001256 if (codec_dai->driver->probe) {
1257 ret = codec_dai->driver->probe(codec_dai);
1258 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001259 pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
1260 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001261 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001262 }
1263 }
1264
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001265 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001266 codec_dai->probed = 1;
1267 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001268 }
1269
Liam Girdwood0168bf02011-06-07 16:08:05 +01001270 /* complete DAI probe during last probe */
1271 if (order != SND_SOC_COMP_ORDER_LAST)
1272 return 0;
1273
Jarkko Nikula589c3562010-12-06 16:27:07 +02001274 ret = soc_post_component_init(card, codec, num, 0);
1275 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001276 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001277
Mark Brown36ae1a92012-01-06 17:12:45 -08001278 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001279 if (ret < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -02001280 pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001281
Mark Brownc74184e2012-04-04 22:12:09 +01001282 if (!dai_link->params) {
1283 /* create the pcm */
1284 ret = soc_new_pcm(rtd, num);
1285 if (ret < 0) {
1286 pr_err("asoc: can't create pcm %s :%d\n",
1287 dai_link->stream_name, ret);
1288 return ret;
1289 }
1290 } else {
1291 /* link the DAI widgets */
1292 play_w = codec_dai->playback_widget;
1293 capture_w = cpu_dai->capture_widget;
1294 if (play_w && capture_w) {
1295 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1296 capture_w, play_w);
1297 if (ret != 0) {
1298 dev_err(card->dev, "Can't link %s to %s: %d\n",
1299 play_w->name, capture_w->name, ret);
1300 return ret;
1301 }
1302 }
1303
1304 play_w = cpu_dai->playback_widget;
1305 capture_w = codec_dai->capture_widget;
1306 if (play_w && capture_w) {
1307 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1308 capture_w, play_w);
1309 if (ret != 0) {
1310 dev_err(card->dev, "Can't link %s to %s: %d\n",
1311 play_w->name, capture_w->name, ret);
1312 return ret;
1313 }
1314 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001315 }
1316
1317 /* add platform data for AC97 devices */
1318 if (rtd->codec_dai->driver->ac97_control)
1319 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1320
1321 return 0;
1322}
1323
1324#ifdef CONFIG_SND_SOC_AC97_BUS
1325static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1326{
1327 int ret;
1328
1329 /* Only instantiate AC97 if not already done by the adaptor
1330 * for the generic AC97 subsystem.
1331 */
1332 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001333 /*
1334 * It is possible that the AC97 device is already registered to
1335 * the device subsystem. This happens when the device is created
1336 * via snd_ac97_mixer(). Currently only SoC codec that does so
1337 * is the generic AC97 glue but others migh emerge.
1338 *
1339 * In those cases we don't try to register the device again.
1340 */
1341 if (!rtd->codec->ac97_created)
1342 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001343
1344 ret = soc_ac97_dev_register(rtd->codec);
1345 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001346 pr_err("asoc: AC97 device register failed:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001347 return ret;
1348 }
1349
1350 rtd->codec->ac97_registered = 1;
1351 }
1352 return 0;
1353}
1354
1355static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1356{
1357 if (codec->ac97_registered) {
1358 soc_ac97_dev_unregister(codec);
1359 codec->ac97_registered = 0;
1360 }
1361}
1362#endif
1363
Mark Brownb19e6e72012-03-14 21:18:39 +00001364static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1365{
1366 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1367 struct snd_soc_codec *codec;
1368
1369 /* find CODEC from registered CODECs*/
1370 list_for_each_entry(codec, &codec_list, list) {
1371 if (!strcmp(codec->name, aux_dev->codec_name))
1372 return 0;
1373 }
1374
1375 return -EPROBE_DEFER;
1376}
1377
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001378static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1379{
1380 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001381 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001382 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001383
1384 /* find CODEC from registered CODECs*/
1385 list_for_each_entry(codec, &codec_list, list) {
1386 if (!strcmp(codec->name, aux_dev->codec_name)) {
1387 if (codec->probed) {
1388 dev_err(codec->dev,
1389 "asoc: codec already probed");
1390 ret = -EBUSY;
1391 goto out;
1392 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001393 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001394 }
1395 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001396 /* codec not found */
1397 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001398 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001399
Jarkko Nikula676ad982010-12-03 09:18:22 +02001400found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001401 ret = soc_probe_codec(card, codec);
1402 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001403 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001404
Jarkko Nikula589c3562010-12-06 16:27:07 +02001405 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001406
1407out:
1408 return ret;
1409}
1410
1411static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1412{
1413 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1414 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001415
1416 /* unregister the rtd device */
1417 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001418 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1419 device_del(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001420 rtd->dev_registered = 0;
1421 }
1422
Jarkko Nikula589c3562010-12-06 16:27:07 +02001423 if (codec && codec->probed)
1424 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001425}
1426
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001427static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1428 enum snd_soc_compress_type compress_type)
1429{
1430 int ret;
1431
1432 if (codec->cache_init)
1433 return 0;
1434
1435 /* override the compress_type if necessary */
1436 if (compress_type && codec->compress_type != compress_type)
1437 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001438 ret = snd_soc_cache_init(codec);
1439 if (ret < 0) {
1440 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1441 ret);
1442 return ret;
1443 }
1444 codec->cache_init = 1;
1445 return 0;
1446}
1447
Mark Brownb19e6e72012-03-14 21:18:39 +00001448static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001449{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001450 struct snd_soc_codec *codec;
1451 struct snd_soc_codec_conf *codec_conf;
1452 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001453 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001454 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001455
Liam Girdwood01b9d992012-03-07 10:38:25 +00001456 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001457
Mark Brownb19e6e72012-03-14 21:18:39 +00001458 /* bind DAIs */
1459 for (i = 0; i < card->num_links; i++) {
1460 ret = soc_bind_dai_link(card, i);
1461 if (ret != 0)
1462 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001463 }
1464
Mark Brownb19e6e72012-03-14 21:18:39 +00001465 /* check aux_devs too */
1466 for (i = 0; i < card->num_aux_devs; i++) {
1467 ret = soc_check_aux_dev(card, i);
1468 if (ret != 0)
1469 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001470 }
1471
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001472 /* initialize the register cache for each available codec */
1473 list_for_each_entry(codec, &codec_list, list) {
1474 if (codec->cache_init)
1475 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001476 /* by default we don't override the compress_type */
1477 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001478 /* check to see if we need to override the compress_type */
1479 for (i = 0; i < card->num_configs; ++i) {
1480 codec_conf = &card->codec_conf[i];
1481 if (!strcmp(codec->name, codec_conf->dev_name)) {
1482 compress_type = codec_conf->compress_type;
1483 if (compress_type && compress_type
1484 != codec->compress_type)
1485 break;
1486 }
1487 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001488 ret = snd_soc_init_codec_cache(codec, compress_type);
Mark Brownb19e6e72012-03-14 21:18:39 +00001489 if (ret < 0)
1490 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001491 }
1492
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001493 /* card bind complete so register a sound card */
1494 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1495 card->owner, 0, &card->snd_card);
1496 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001497 pr_err("asoc: can't create sound card for card %s: %d\n",
1498 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001499 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001500 }
1501 card->snd_card->dev = card->dev;
1502
Mark Browne37a4972011-03-02 18:21:57 +00001503 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1504 card->dapm.dev = card->dev;
1505 card->dapm.card = card;
1506 list_add(&card->dapm.list, &card->dapm_list);
1507
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001508#ifdef CONFIG_DEBUG_FS
1509 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1510#endif
1511
Mark Brown88ee1c62011-02-02 10:43:26 +00001512#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001513 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001514 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001515#endif
Andy Green6ed25972008-06-13 16:24:05 +01001516
Mark Brown9a841eb2011-04-12 17:51:37 -07001517 if (card->dapm_widgets)
1518 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1519 card->num_dapm_widgets);
1520
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001521 /* initialise the sound card only once */
1522 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001523 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001524 if (ret < 0)
1525 goto card_probe_error;
1526 }
1527
Liam Girdwood0168bf02011-06-07 16:08:05 +01001528 /* early DAI link probe */
1529 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1530 order++) {
1531 for (i = 0; i < card->num_links; i++) {
1532 ret = soc_probe_dai_link(card, i, order);
1533 if (ret < 0) {
1534 pr_err("asoc: failed to instantiate card %s: %d\n",
Mark Brown321de0d2010-09-21 15:09:37 +01001535 card->name, ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001536 goto probe_dai_err;
1537 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001538 }
1539 }
1540
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001541 for (i = 0; i < card->num_aux_devs; i++) {
1542 ret = soc_probe_aux_dev(card, i);
1543 if (ret < 0) {
1544 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1545 card->name, ret);
1546 goto probe_aux_dev_err;
1547 }
1548 }
1549
Mark Brown888df392012-02-16 19:37:51 -08001550 snd_soc_dapm_link_dai_widgets(card);
1551
Mark Brownb7af1da2011-04-07 19:18:44 +09001552 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001553 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001554
Mark Brownb8ad29d2011-03-02 18:35:51 +00001555 if (card->dapm_routes)
1556 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1557 card->num_dapm_routes);
1558
Mark Brownb90d2f92011-10-10 13:38:06 +01001559 snd_soc_dapm_new_widgets(&card->dapm);
1560
Mark Brown75d9ac42011-09-27 16:41:01 +01001561 for (i = 0; i < card->num_links; i++) {
1562 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001563 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001564
Mark Brownf04209a2012-04-09 16:29:21 +01001565 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001566 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001567 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001568 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001569 dev_warn(card->rtd[i].codec_dai->dev,
1570 "Failed to set DAI format: %d\n",
1571 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001572 }
1573
1574 /* If this is a regular CPU link there will be a platform */
1575 if (dai_fmt && dai_link->platform_name) {
1576 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1577 dai_fmt);
1578 if (ret != 0 && ret != -ENOTSUPP)
1579 dev_warn(card->rtd[i].cpu_dai->dev,
1580 "Failed to set DAI format: %d\n",
1581 ret);
1582 } else if (dai_fmt) {
1583 /* Flip the polarity for the "CPU" end */
1584 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1585 switch (dai_link->dai_fmt &
1586 SND_SOC_DAIFMT_MASTER_MASK) {
1587 case SND_SOC_DAIFMT_CBM_CFM:
1588 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1589 break;
1590 case SND_SOC_DAIFMT_CBM_CFS:
1591 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1592 break;
1593 case SND_SOC_DAIFMT_CBS_CFM:
1594 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1595 break;
1596 case SND_SOC_DAIFMT_CBS_CFS:
1597 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1598 break;
1599 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001600
1601 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001602 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001603 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001604 dev_warn(card->rtd[i].cpu_dai->dev,
1605 "Failed to set DAI format: %d\n",
1606 ret);
1607 }
1608 }
1609
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001610 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001611 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001612 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1613 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001614 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1615 "%s", card->driver_name ? card->driver_name : card->name);
1616 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1617 switch (card->snd_card->driver[i]) {
1618 case '_':
1619 case '-':
1620 case '\0':
1621 break;
1622 default:
1623 if (!isalnum(card->snd_card->driver[i]))
1624 card->snd_card->driver[i] = '_';
1625 break;
1626 }
1627 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001628
Mark Brown28e9ad92011-03-02 18:36:34 +00001629 if (card->late_probe) {
1630 ret = card->late_probe(card);
1631 if (ret < 0) {
1632 dev_err(card->dev, "%s late_probe() failed: %d\n",
1633 card->name, ret);
1634 goto probe_aux_dev_err;
1635 }
1636 }
1637
Mark Brown2dc00212011-10-08 13:59:44 +01001638 snd_soc_dapm_new_widgets(&card->dapm);
1639
Stephen Warren16332812011-11-23 12:42:04 -07001640 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001641 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001642 snd_soc_dapm_auto_nc_codec_pins(codec);
1643
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001644 ret = snd_card_register(card->snd_card);
1645 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001646 pr_err("asoc: failed to register soundcard for %s: %d\n",
1647 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001648 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001649 }
1650
1651#ifdef CONFIG_SND_SOC_AC97_BUS
1652 /* register any AC97 codecs */
1653 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001654 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1655 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001656 pr_err("asoc: failed to register AC97 %s: %d\n",
1657 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001658 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001659 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001660 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001661 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001662 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001663#endif
1664
Mark Brown435c5e22008-12-04 15:32:53 +00001665 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001666 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001667 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001668
1669 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001670
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001671probe_aux_dev_err:
1672 for (i = 0; i < card->num_aux_devs; i++)
1673 soc_remove_aux_dev(card, i);
1674
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001675probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001676 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001677
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001678card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001679 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001680 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001681
1682 snd_card_free(card->snd_card);
1683
Mark Brownb19e6e72012-03-14 21:18:39 +00001684base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001685 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001686
Mark Brownb19e6e72012-03-14 21:18:39 +00001687 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001688}
1689
1690/* probes a new socdev */
1691static int soc_probe(struct platform_device *pdev)
1692{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001693 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001694 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001695
Vinod Koul70a7ca32011-01-14 19:22:48 +05301696 /*
1697 * no card, so machine driver should be registering card
1698 * we should not be here in that case so ret error
1699 */
1700 if (!card)
1701 return -EINVAL;
1702
Mark Brownfe4085e2012-03-02 13:07:41 +00001703 dev_warn(&pdev->dev,
1704 "ASoC machine %s should use snd_soc_register_card()\n",
1705 card->name);
1706
Mark Brown435c5e22008-12-04 15:32:53 +00001707 /* Bodge while we unpick instantiation */
1708 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001709
Mark Brown435c5e22008-12-04 15:32:53 +00001710 ret = snd_soc_register_card(card);
1711 if (ret != 0) {
1712 dev_err(&pdev->dev, "Failed to register card\n");
1713 return ret;
1714 }
1715
1716 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001717}
1718
Vinod Koulb0e26482011-01-13 22:48:02 +05301719static int soc_cleanup_card_resources(struct snd_soc_card *card)
1720{
Vinod Koulb0e26482011-01-13 22:48:02 +05301721 int i;
1722
1723 /* make sure any delayed work runs */
1724 for (i = 0; i < card->num_rtd; i++) {
1725 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1726 flush_delayed_work_sync(&rtd->delayed_work);
1727 }
1728
1729 /* remove auxiliary devices */
1730 for (i = 0; i < card->num_aux_devs; i++)
1731 soc_remove_aux_dev(card, i);
1732
1733 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001734 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301735
1736 soc_cleanup_card_debugfs(card);
1737
1738 /* remove the card */
1739 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001740 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301741
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001742 snd_soc_dapm_free(&card->dapm);
1743
Vinod Koulb0e26482011-01-13 22:48:02 +05301744 snd_card_free(card->snd_card);
1745 return 0;
1746
1747}
1748
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001749/* removes a socdev */
1750static int soc_remove(struct platform_device *pdev)
1751{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001752 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001753
Mark Brownc5af3a22008-11-28 13:29:45 +00001754 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001755 return 0;
1756}
1757
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001758int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001759{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001760 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001761 int i;
Mark Brown51737472009-06-22 13:16:51 +01001762
1763 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001764 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001765
1766 /* Flush out pmdown_time work - we actually do want to run it
1767 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001768 for (i = 0; i < card->num_rtd; i++) {
1769 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001770 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001771 }
Mark Brown51737472009-06-22 13:16:51 +01001772
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001773 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001774
1775 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001776}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001777EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001778
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001779const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301780 .suspend = snd_soc_suspend,
1781 .resume = snd_soc_resume,
1782 .freeze = snd_soc_suspend,
1783 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001784 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301785 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001786};
Stephen Warrendeb26072011-04-05 19:35:30 -06001787EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001788
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001789/* ASoC platform driver */
1790static struct platform_driver soc_driver = {
1791 .driver = {
1792 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001793 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001794 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001795 },
1796 .probe = soc_probe,
1797 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001798};
1799
Mark Brown096e49d2009-07-05 15:12:22 +01001800/**
1801 * snd_soc_codec_volatile_register: Report if a register is volatile.
1802 *
1803 * @codec: CODEC to query.
1804 * @reg: Register to query.
1805 *
1806 * Boolean function indiciating if a CODEC register is volatile.
1807 */
Mark Brown181e0552011-01-24 14:05:25 +00001808int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1809 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001810{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001811 if (codec->volatile_register)
1812 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001813 else
1814 return 0;
1815}
1816EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1817
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001818/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001819 * snd_soc_codec_readable_register: Report if a register is readable.
1820 *
1821 * @codec: CODEC to query.
1822 * @reg: Register to query.
1823 *
1824 * Boolean function indicating if a CODEC register is readable.
1825 */
1826int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1827 unsigned int reg)
1828{
1829 if (codec->readable_register)
1830 return codec->readable_register(codec, reg);
1831 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001832 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001833}
1834EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1835
1836/**
1837 * snd_soc_codec_writable_register: Report if a register is writable.
1838 *
1839 * @codec: CODEC to query.
1840 * @reg: Register to query.
1841 *
1842 * Boolean function indicating if a CODEC register is writable.
1843 */
1844int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1845 unsigned int reg)
1846{
1847 if (codec->writable_register)
1848 return codec->writable_register(codec, reg);
1849 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001850 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001851}
1852EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1853
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001854int snd_soc_platform_read(struct snd_soc_platform *platform,
1855 unsigned int reg)
1856{
1857 unsigned int ret;
1858
1859 if (!platform->driver->read) {
1860 dev_err(platform->dev, "platform has no read back\n");
1861 return -1;
1862 }
1863
1864 ret = platform->driver->read(platform, reg);
1865 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001866 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001867
1868 return ret;
1869}
1870EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1871
1872int snd_soc_platform_write(struct snd_soc_platform *platform,
1873 unsigned int reg, unsigned int val)
1874{
1875 if (!platform->driver->write) {
1876 dev_err(platform->dev, "platform has no write back\n");
1877 return -1;
1878 }
1879
1880 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001881 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001882 return platform->driver->write(platform, reg, val);
1883}
1884EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1885
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001886/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001887 * snd_soc_new_ac97_codec - initailise AC97 device
1888 * @codec: audio codec
1889 * @ops: AC97 bus operations
1890 * @num: AC97 codec number
1891 *
1892 * Initialises AC97 codec resources for use by ad-hoc devices only.
1893 */
1894int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1895 struct snd_ac97_bus_ops *ops, int num)
1896{
1897 mutex_lock(&codec->mutex);
1898
1899 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1900 if (codec->ac97 == NULL) {
1901 mutex_unlock(&codec->mutex);
1902 return -ENOMEM;
1903 }
1904
1905 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1906 if (codec->ac97->bus == NULL) {
1907 kfree(codec->ac97);
1908 codec->ac97 = NULL;
1909 mutex_unlock(&codec->mutex);
1910 return -ENOMEM;
1911 }
1912
1913 codec->ac97->bus->ops = ops;
1914 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001915
1916 /*
1917 * Mark the AC97 device to be created by us. This way we ensure that the
1918 * device will be registered with the device subsystem later on.
1919 */
1920 codec->ac97_created = 1;
1921
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001922 mutex_unlock(&codec->mutex);
1923 return 0;
1924}
1925EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1926
1927/**
1928 * snd_soc_free_ac97_codec - free AC97 codec device
1929 * @codec: audio codec
1930 *
1931 * Frees AC97 codec device resources.
1932 */
1933void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1934{
1935 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001936#ifdef CONFIG_SND_SOC_AC97_BUS
1937 soc_unregister_ac97_dai_link(codec);
1938#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001939 kfree(codec->ac97->bus);
1940 kfree(codec->ac97);
1941 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001942 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001943 mutex_unlock(&codec->mutex);
1944}
1945EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1946
Mark Brownc3753702010-11-01 15:41:57 -04001947unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1948{
1949 unsigned int ret;
1950
Mark Brownc3acec22010-12-02 16:15:29 +00001951 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04001952 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04001953 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04001954
1955 return ret;
1956}
1957EXPORT_SYMBOL_GPL(snd_soc_read);
1958
1959unsigned int snd_soc_write(struct snd_soc_codec *codec,
1960 unsigned int reg, unsigned int val)
1961{
1962 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04001963 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00001964 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04001965}
1966EXPORT_SYMBOL_GPL(snd_soc_write);
1967
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00001968unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
1969 unsigned int reg, const void *data, size_t len)
1970{
1971 return codec->bulk_write_raw(codec, reg, data, len);
1972}
1973EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
1974
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001975/**
1976 * snd_soc_update_bits - update codec register bits
1977 * @codec: audio codec
1978 * @reg: codec register
1979 * @mask: register mask
1980 * @value: new value
1981 *
1982 * Writes new register value.
1983 *
Timur Tabi180c3292011-01-10 15:58:13 -06001984 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001985 */
1986int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001987 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001988{
Mark Brown8a713da2011-12-03 12:33:55 +00001989 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001990 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06001991 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001992
Mark Brown8a713da2011-12-03 12:33:55 +00001993 if (codec->using_regmap) {
1994 ret = regmap_update_bits_check(codec->control_data, reg,
1995 mask, value, &change);
1996 } else {
1997 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06001998 if (ret < 0)
1999 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00002000
2001 old = ret;
2002 new = (old & ~mask) | (value & mask);
2003 change = old != new;
2004 if (change)
2005 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06002006 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002007
Mark Brown8a713da2011-12-03 12:33:55 +00002008 if (ret < 0)
2009 return ret;
2010
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002011 return change;
2012}
2013EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2014
2015/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002016 * snd_soc_update_bits_locked - update codec register bits
2017 * @codec: audio codec
2018 * @reg: codec register
2019 * @mask: register mask
2020 * @value: new value
2021 *
2022 * Writes new register value, and takes the codec mutex.
2023 *
2024 * Returns 1 for change else 0.
2025 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002026int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2027 unsigned short reg, unsigned int mask,
2028 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002029{
2030 int change;
2031
2032 mutex_lock(&codec->mutex);
2033 change = snd_soc_update_bits(codec, reg, mask, value);
2034 mutex_unlock(&codec->mutex);
2035
2036 return change;
2037}
Mark Browndd1b3d52009-12-04 14:22:03 +00002038EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002039
2040/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002041 * snd_soc_test_bits - test register for change
2042 * @codec: audio codec
2043 * @reg: codec register
2044 * @mask: register mask
2045 * @value: new value
2046 *
2047 * Tests a register with a new value and checks if the new value is
2048 * different from the old value.
2049 *
2050 * Returns 1 for change else 0.
2051 */
2052int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002053 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002054{
2055 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002056 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002057
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002058 old = snd_soc_read(codec, reg);
2059 new = (old & ~mask) | value;
2060 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002061
2062 return change;
2063}
2064EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2065
2066/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002067 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2068 * @substream: the pcm substream
2069 * @hw: the hardware parameters
2070 *
2071 * Sets the substream runtime hardware parameters.
2072 */
2073int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2074 const struct snd_pcm_hardware *hw)
2075{
2076 struct snd_pcm_runtime *runtime = substream->runtime;
2077 runtime->hw.info = hw->info;
2078 runtime->hw.formats = hw->formats;
2079 runtime->hw.period_bytes_min = hw->period_bytes_min;
2080 runtime->hw.period_bytes_max = hw->period_bytes_max;
2081 runtime->hw.periods_min = hw->periods_min;
2082 runtime->hw.periods_max = hw->periods_max;
2083 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2084 runtime->hw.fifo_size = hw->fifo_size;
2085 return 0;
2086}
2087EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2088
2089/**
2090 * snd_soc_cnew - create new control
2091 * @_template: control template
2092 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002093 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002094 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002095 *
2096 * Create a new mixer control from a template control.
2097 *
2098 * Returns 0 for success, else error.
2099 */
2100struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002101 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002102 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002103{
2104 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002105 struct snd_kcontrol *kcontrol;
2106 char *name = NULL;
2107 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002108
2109 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002110 template.index = 0;
2111
Mark Brownefb7ac32011-03-08 17:23:24 +00002112 if (!long_name)
2113 long_name = template.name;
2114
2115 if (prefix) {
2116 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08002117 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00002118 if (!name)
2119 return NULL;
2120
2121 snprintf(name, name_len, "%s %s", prefix, long_name);
2122
2123 template.name = name;
2124 } else {
2125 template.name = long_name;
2126 }
2127
2128 kcontrol = snd_ctl_new1(&template, data);
2129
2130 kfree(name);
2131
2132 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002133}
2134EXPORT_SYMBOL_GPL(snd_soc_cnew);
2135
Liam Girdwood022658b2012-02-03 17:43:09 +00002136static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2137 const struct snd_kcontrol_new *controls, int num_controls,
2138 const char *prefix, void *data)
2139{
2140 int err, i;
2141
2142 for (i = 0; i < num_controls; i++) {
2143 const struct snd_kcontrol_new *control = &controls[i];
2144 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2145 control->name, prefix));
2146 if (err < 0) {
2147 dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2148 return err;
2149 }
2150 }
2151
2152 return 0;
2153}
2154
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002155/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002156 * snd_soc_add_codec_controls - add an array of controls to a codec.
2157 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002158 * duplicating this code.
2159 *
2160 * @codec: codec to add controls to
2161 * @controls: array of controls to add
2162 * @num_controls: number of elements in the array
2163 *
2164 * Return 0 for success, else error.
2165 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002166int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002167 const struct snd_kcontrol_new *controls, int num_controls)
2168{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002169 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002170
Liam Girdwood022658b2012-02-03 17:43:09 +00002171 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2172 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002173}
Liam Girdwood022658b2012-02-03 17:43:09 +00002174EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002175
2176/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002177 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002178 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002179 *
2180 * @platform: platform to add controls to
2181 * @controls: array of controls to add
2182 * @num_controls: number of elements in the array
2183 *
2184 * Return 0 for success, else error.
2185 */
2186int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2187 const struct snd_kcontrol_new *controls, int num_controls)
2188{
2189 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002190
Liam Girdwood022658b2012-02-03 17:43:09 +00002191 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2192 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002193}
2194EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2195
2196/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002197 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2198 * Convenience function to add a list of controls.
2199 *
2200 * @soc_card: SoC card to add controls to
2201 * @controls: array of controls to add
2202 * @num_controls: number of elements in the array
2203 *
2204 * Return 0 for success, else error.
2205 */
2206int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2207 const struct snd_kcontrol_new *controls, int num_controls)
2208{
2209 struct snd_card *card = soc_card->snd_card;
2210
2211 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2212 NULL, soc_card);
2213}
2214EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2215
2216/**
2217 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2218 * Convienience function to add a list of controls.
2219 *
2220 * @dai: DAI to add controls to
2221 * @controls: array of controls to add
2222 * @num_controls: number of elements in the array
2223 *
2224 * Return 0 for success, else error.
2225 */
2226int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2227 const struct snd_kcontrol_new *controls, int num_controls)
2228{
2229 struct snd_card *card = dai->card->snd_card;
2230
2231 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2232 NULL, dai);
2233}
2234EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2235
2236/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002237 * snd_soc_info_enum_double - enumerated double mixer info callback
2238 * @kcontrol: mixer control
2239 * @uinfo: control element information
2240 *
2241 * Callback to provide information about a double enumerated
2242 * mixer control.
2243 *
2244 * Returns 0 for success.
2245 */
2246int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2247 struct snd_ctl_elem_info *uinfo)
2248{
2249 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2250
2251 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2252 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002253 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002254
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002255 if (uinfo->value.enumerated.item > e->max - 1)
2256 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002257 strcpy(uinfo->value.enumerated.name,
2258 e->texts[uinfo->value.enumerated.item]);
2259 return 0;
2260}
2261EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2262
2263/**
2264 * snd_soc_get_enum_double - enumerated double mixer get callback
2265 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002266 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002267 *
2268 * Callback to get the value of a double enumerated mixer.
2269 *
2270 * Returns 0 for success.
2271 */
2272int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2273 struct snd_ctl_elem_value *ucontrol)
2274{
2275 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2276 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002277 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002278
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002279 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002280 ;
2281 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002282 ucontrol->value.enumerated.item[0]
2283 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002284 if (e->shift_l != e->shift_r)
2285 ucontrol->value.enumerated.item[1] =
2286 (val >> e->shift_r) & (bitmask - 1);
2287
2288 return 0;
2289}
2290EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2291
2292/**
2293 * snd_soc_put_enum_double - enumerated double mixer put callback
2294 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002295 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002296 *
2297 * Callback to set the value of a double enumerated mixer.
2298 *
2299 * Returns 0 for success.
2300 */
2301int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2302 struct snd_ctl_elem_value *ucontrol)
2303{
2304 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2305 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002306 unsigned int val;
2307 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002308
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002309 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002310 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002311 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002312 return -EINVAL;
2313 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2314 mask = (bitmask - 1) << e->shift_l;
2315 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002316 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002317 return -EINVAL;
2318 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2319 mask |= (bitmask - 1) << e->shift_r;
2320 }
2321
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002322 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002323}
2324EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2325
2326/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002327 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2328 * @kcontrol: mixer control
2329 * @ucontrol: control element information
2330 *
2331 * Callback to get the value of a double semi enumerated mixer.
2332 *
2333 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2334 * used for handling bitfield coded enumeration for example.
2335 *
2336 * Returns 0 for success.
2337 */
2338int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2339 struct snd_ctl_elem_value *ucontrol)
2340{
2341 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002342 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002343 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002344
2345 reg_val = snd_soc_read(codec, e->reg);
2346 val = (reg_val >> e->shift_l) & e->mask;
2347 for (mux = 0; mux < e->max; mux++) {
2348 if (val == e->values[mux])
2349 break;
2350 }
2351 ucontrol->value.enumerated.item[0] = mux;
2352 if (e->shift_l != e->shift_r) {
2353 val = (reg_val >> e->shift_r) & e->mask;
2354 for (mux = 0; mux < e->max; mux++) {
2355 if (val == e->values[mux])
2356 break;
2357 }
2358 ucontrol->value.enumerated.item[1] = mux;
2359 }
2360
2361 return 0;
2362}
2363EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2364
2365/**
2366 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2367 * @kcontrol: mixer control
2368 * @ucontrol: control element information
2369 *
2370 * Callback to set the value of a double semi enumerated mixer.
2371 *
2372 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2373 * used for handling bitfield coded enumeration for example.
2374 *
2375 * Returns 0 for success.
2376 */
2377int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2378 struct snd_ctl_elem_value *ucontrol)
2379{
2380 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002381 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002382 unsigned int val;
2383 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002384
2385 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2386 return -EINVAL;
2387 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2388 mask = e->mask << e->shift_l;
2389 if (e->shift_l != e->shift_r) {
2390 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2391 return -EINVAL;
2392 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2393 mask |= e->mask << e->shift_r;
2394 }
2395
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002396 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002397}
2398EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2399
2400/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002401 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2402 * @kcontrol: mixer control
2403 * @uinfo: control element information
2404 *
2405 * Callback to provide information about an external enumerated
2406 * single mixer.
2407 *
2408 * Returns 0 for success.
2409 */
2410int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2411 struct snd_ctl_elem_info *uinfo)
2412{
2413 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2414
2415 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2416 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002417 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002418
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002419 if (uinfo->value.enumerated.item > e->max - 1)
2420 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002421 strcpy(uinfo->value.enumerated.name,
2422 e->texts[uinfo->value.enumerated.item]);
2423 return 0;
2424}
2425EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2426
2427/**
2428 * snd_soc_info_volsw_ext - external single mixer info callback
2429 * @kcontrol: mixer control
2430 * @uinfo: control element information
2431 *
2432 * Callback to provide information about a single external mixer control.
2433 *
2434 * Returns 0 for success.
2435 */
2436int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2437 struct snd_ctl_elem_info *uinfo)
2438{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002439 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002440
Mark Brownfd5dfad2009-04-15 21:37:46 +01002441 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002442 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2443 else
2444 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2445
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002446 uinfo->count = 1;
2447 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002448 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002449 return 0;
2450}
2451EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2452
2453/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002454 * snd_soc_info_volsw - single mixer info callback
2455 * @kcontrol: mixer control
2456 * @uinfo: control element information
2457 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002458 * Callback to provide information about a single mixer control, or a double
2459 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002460 *
2461 * Returns 0 for success.
2462 */
2463int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2464 struct snd_ctl_elem_info *uinfo)
2465{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002466 struct soc_mixer_control *mc =
2467 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002468 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002469
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002470 if (!mc->platform_max)
2471 mc->platform_max = mc->max;
2472 platform_max = mc->platform_max;
2473
2474 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002475 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2476 else
2477 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2478
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002479 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002480 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002481 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002482 return 0;
2483}
2484EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2485
2486/**
2487 * snd_soc_get_volsw - single mixer get callback
2488 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002489 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002490 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002491 * Callback to get the value of a single mixer control, or a double mixer
2492 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002493 *
2494 * Returns 0 for success.
2495 */
2496int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2497 struct snd_ctl_elem_value *ucontrol)
2498{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002499 struct soc_mixer_control *mc =
2500 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002501 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002502 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002503 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002504 unsigned int shift = mc->shift;
2505 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002506 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002507 unsigned int mask = (1 << fls(max)) - 1;
2508 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002509
2510 ucontrol->value.integer.value[0] =
2511 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002512 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002513 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002514 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002515
2516 if (snd_soc_volsw_is_stereo(mc)) {
2517 if (reg == reg2)
2518 ucontrol->value.integer.value[1] =
2519 (snd_soc_read(codec, reg) >> rshift) & mask;
2520 else
2521 ucontrol->value.integer.value[1] =
2522 (snd_soc_read(codec, reg2) >> shift) & mask;
2523 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002524 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002525 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002526 }
2527
2528 return 0;
2529}
2530EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2531
2532/**
2533 * snd_soc_put_volsw - single mixer put callback
2534 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002535 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002536 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002537 * Callback to set the value of a single mixer control, or a double mixer
2538 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002539 *
2540 * Returns 0 for success.
2541 */
2542int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2543 struct snd_ctl_elem_value *ucontrol)
2544{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002545 struct soc_mixer_control *mc =
2546 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002547 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002548 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002549 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002550 unsigned int shift = mc->shift;
2551 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002552 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002553 unsigned int mask = (1 << fls(max)) - 1;
2554 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002555 int err;
2556 bool type_2r = 0;
2557 unsigned int val2 = 0;
2558 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002559
2560 val = (ucontrol->value.integer.value[0] & mask);
2561 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002562 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002563 val_mask = mask << shift;
2564 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002565 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002566 val2 = (ucontrol->value.integer.value[1] & mask);
2567 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002568 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002569 if (reg == reg2) {
2570 val_mask |= mask << rshift;
2571 val |= val2 << rshift;
2572 } else {
2573 val2 = val2 << shift;
2574 type_2r = 1;
2575 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002576 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002577 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002578 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002579 return err;
2580
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002581 if (type_2r)
2582 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2583
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002584 return err;
2585}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002586EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002587
Mark Browne13ac2e2008-05-28 17:58:05 +01002588/**
Brian Austin1d99f242012-03-30 10:43:55 -05002589 * snd_soc_get_volsw_sx - single mixer get callback
2590 * @kcontrol: mixer control
2591 * @ucontrol: control element information
2592 *
2593 * Callback to get the value of a single mixer control, or a double mixer
2594 * control that spans 2 registers.
2595 *
2596 * Returns 0 for success.
2597 */
2598int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2599 struct snd_ctl_elem_value *ucontrol)
2600{
2601 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2602 struct soc_mixer_control *mc =
2603 (struct soc_mixer_control *)kcontrol->private_value;
2604
2605 unsigned int reg = mc->reg;
2606 unsigned int reg2 = mc->rreg;
2607 unsigned int shift = mc->shift;
2608 unsigned int rshift = mc->rshift;
2609 int max = mc->max;
2610 int min = mc->min;
2611 int mask = (1 << (fls(min + max) - 1)) - 1;
2612
2613 ucontrol->value.integer.value[0] =
2614 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2615
2616 if (snd_soc_volsw_is_stereo(mc))
2617 ucontrol->value.integer.value[1] =
2618 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2619
2620 return 0;
2621}
2622EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2623
2624/**
2625 * snd_soc_put_volsw_sx - double mixer set callback
2626 * @kcontrol: mixer control
2627 * @uinfo: control element information
2628 *
2629 * Callback to set the value of a double mixer control that spans 2 registers.
2630 *
2631 * Returns 0 for success.
2632 */
2633int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2634 struct snd_ctl_elem_value *ucontrol)
2635{
2636 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2637 struct soc_mixer_control *mc =
2638 (struct soc_mixer_control *)kcontrol->private_value;
2639
2640 unsigned int reg = mc->reg;
2641 unsigned int reg2 = mc->rreg;
2642 unsigned int shift = mc->shift;
2643 unsigned int rshift = mc->rshift;
2644 int max = mc->max;
2645 int min = mc->min;
2646 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002647 int err = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002648 unsigned short val, val_mask, val2 = 0;
2649
2650 val_mask = mask << shift;
2651 val = (ucontrol->value.integer.value[0] + min) & mask;
2652 val = val << shift;
2653
2654 if (snd_soc_update_bits_locked(codec, reg, val_mask, val))
2655 return err;
2656
2657 if (snd_soc_volsw_is_stereo(mc)) {
2658 val_mask = mask << rshift;
2659 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2660 val2 = val2 << rshift;
2661
2662 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2663 return err;
2664 }
2665 return 0;
2666}
2667EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2668
2669/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002670 * snd_soc_info_volsw_s8 - signed mixer info callback
2671 * @kcontrol: mixer control
2672 * @uinfo: control element information
2673 *
2674 * Callback to provide information about a signed mixer control.
2675 *
2676 * Returns 0 for success.
2677 */
2678int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2679 struct snd_ctl_elem_info *uinfo)
2680{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002681 struct soc_mixer_control *mc =
2682 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002683 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002684 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002685
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002686 if (!mc->platform_max)
2687 mc->platform_max = mc->max;
2688 platform_max = mc->platform_max;
2689
Mark Browne13ac2e2008-05-28 17:58:05 +01002690 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2691 uinfo->count = 2;
2692 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002693 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002694 return 0;
2695}
2696EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2697
2698/**
2699 * snd_soc_get_volsw_s8 - signed mixer get callback
2700 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002701 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002702 *
2703 * Callback to get the value of a signed mixer control.
2704 *
2705 * Returns 0 for success.
2706 */
2707int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2708 struct snd_ctl_elem_value *ucontrol)
2709{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002710 struct soc_mixer_control *mc =
2711 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002712 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002713 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002714 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002715 int val = snd_soc_read(codec, reg);
2716
2717 ucontrol->value.integer.value[0] =
2718 ((signed char)(val & 0xff))-min;
2719 ucontrol->value.integer.value[1] =
2720 ((signed char)((val >> 8) & 0xff))-min;
2721 return 0;
2722}
2723EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2724
2725/**
2726 * snd_soc_put_volsw_sgn - signed mixer put callback
2727 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002728 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002729 *
2730 * Callback to set the value of a signed mixer control.
2731 *
2732 * Returns 0 for success.
2733 */
2734int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2735 struct snd_ctl_elem_value *ucontrol)
2736{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002737 struct soc_mixer_control *mc =
2738 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002739 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002740 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002741 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002742 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002743
2744 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2745 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2746
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002747 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002748}
2749EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2750
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002751/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002752 * snd_soc_limit_volume - Set new limit to an existing volume control.
2753 *
2754 * @codec: where to look for the control
2755 * @name: Name of the control
2756 * @max: new maximum limit
2757 *
2758 * Return 0 for success, else error.
2759 */
2760int snd_soc_limit_volume(struct snd_soc_codec *codec,
2761 const char *name, int max)
2762{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002763 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002764 struct snd_kcontrol *kctl;
2765 struct soc_mixer_control *mc;
2766 int found = 0;
2767 int ret = -EINVAL;
2768
2769 /* Sanity check for name and max */
2770 if (unlikely(!name || max <= 0))
2771 return -EINVAL;
2772
2773 list_for_each_entry(kctl, &card->controls, list) {
2774 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2775 found = 1;
2776 break;
2777 }
2778 }
2779 if (found) {
2780 mc = (struct soc_mixer_control *)kctl->private_value;
2781 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002782 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002783 ret = 0;
2784 }
2785 }
2786 return ret;
2787}
2788EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2789
Mark Brown71d08512011-10-10 18:31:26 +01002790int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2791 struct snd_ctl_elem_info *uinfo)
2792{
2793 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2794 struct soc_bytes *params = (void *)kcontrol->private_value;
2795
2796 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2797 uinfo->count = params->num_regs * codec->val_bytes;
2798
2799 return 0;
2800}
2801EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
2802
2803int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
2804 struct snd_ctl_elem_value *ucontrol)
2805{
2806 struct soc_bytes *params = (void *)kcontrol->private_value;
2807 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2808 int ret;
2809
2810 if (codec->using_regmap)
2811 ret = regmap_raw_read(codec->control_data, params->base,
2812 ucontrol->value.bytes.data,
2813 params->num_regs * codec->val_bytes);
2814 else
2815 ret = -EINVAL;
2816
Mark Brownf831b052012-02-17 16:20:33 -08002817 /* Hide any masked bytes to ensure consistent data reporting */
2818 if (ret == 0 && params->mask) {
2819 switch (codec->val_bytes) {
2820 case 1:
2821 ucontrol->value.bytes.data[0] &= ~params->mask;
2822 break;
2823 case 2:
2824 ((u16 *)(&ucontrol->value.bytes.data))[0]
2825 &= ~params->mask;
2826 break;
2827 case 4:
2828 ((u32 *)(&ucontrol->value.bytes.data))[0]
2829 &= ~params->mask;
2830 break;
2831 default:
2832 return -EINVAL;
2833 }
2834 }
2835
Mark Brown71d08512011-10-10 18:31:26 +01002836 return ret;
2837}
2838EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
2839
2840int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
2841 struct snd_ctl_elem_value *ucontrol)
2842{
2843 struct soc_bytes *params = (void *)kcontrol->private_value;
2844 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08002845 int ret, len;
2846 unsigned int val;
2847 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01002848
Mark Brownf831b052012-02-17 16:20:33 -08002849 if (!codec->using_regmap)
2850 return -EINVAL;
2851
2852 data = ucontrol->value.bytes.data;
2853 len = params->num_regs * codec->val_bytes;
2854
2855 /*
2856 * If we've got a mask then we need to preserve the register
2857 * bits. We shouldn't modify the incoming data so take a
2858 * copy.
2859 */
2860 if (params->mask) {
2861 ret = regmap_read(codec->control_data, params->base, &val);
2862 if (ret != 0)
2863 return ret;
2864
2865 val &= params->mask;
2866
2867 data = kmemdup(data, len, GFP_KERNEL);
2868 if (!data)
2869 return -ENOMEM;
2870
2871 switch (codec->val_bytes) {
2872 case 1:
2873 ((u8 *)data)[0] &= ~params->mask;
2874 ((u8 *)data)[0] |= val;
2875 break;
2876 case 2:
2877 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
2878 ((u16 *)data)[0] |= cpu_to_be16(val);
2879 break;
2880 case 4:
2881 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
2882 ((u32 *)data)[0] |= cpu_to_be32(val);
2883 break;
2884 default:
2885 return -EINVAL;
2886 }
2887 }
2888
2889 ret = regmap_raw_write(codec->control_data, params->base,
2890 data, len);
2891
2892 if (params->mask)
2893 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01002894
2895 return ret;
2896}
2897EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
2898
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002899/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002900 * snd_soc_info_xr_sx - signed multi register info callback
2901 * @kcontrol: mreg control
2902 * @uinfo: control element information
2903 *
2904 * Callback to provide information of a control that can
2905 * span multiple codec registers which together
2906 * forms a single signed value in a MSB/LSB manner.
2907 *
2908 * Returns 0 for success.
2909 */
2910int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
2911 struct snd_ctl_elem_info *uinfo)
2912{
2913 struct soc_mreg_control *mc =
2914 (struct soc_mreg_control *)kcontrol->private_value;
2915 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2916 uinfo->count = 1;
2917 uinfo->value.integer.min = mc->min;
2918 uinfo->value.integer.max = mc->max;
2919
2920 return 0;
2921}
2922EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
2923
2924/**
2925 * snd_soc_get_xr_sx - signed multi register get callback
2926 * @kcontrol: mreg control
2927 * @ucontrol: control element information
2928 *
2929 * Callback to get the value of a control that can span
2930 * multiple codec registers which together forms a single
2931 * signed value in a MSB/LSB manner. The control supports
2932 * specifying total no of bits used to allow for bitfields
2933 * across the multiple codec registers.
2934 *
2935 * Returns 0 for success.
2936 */
2937int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
2938 struct snd_ctl_elem_value *ucontrol)
2939{
2940 struct soc_mreg_control *mc =
2941 (struct soc_mreg_control *)kcontrol->private_value;
2942 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2943 unsigned int regbase = mc->regbase;
2944 unsigned int regcount = mc->regcount;
2945 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
2946 unsigned int regwmask = (1<<regwshift)-1;
2947 unsigned int invert = mc->invert;
2948 unsigned long mask = (1UL<<mc->nbits)-1;
2949 long min = mc->min;
2950 long max = mc->max;
2951 long val = 0;
2952 unsigned long regval;
2953 unsigned int i;
2954
2955 for (i = 0; i < regcount; i++) {
2956 regval = snd_soc_read(codec, regbase+i) & regwmask;
2957 val |= regval << (regwshift*(regcount-i-1));
2958 }
2959 val &= mask;
2960 if (min < 0 && val > max)
2961 val |= ~mask;
2962 if (invert)
2963 val = max - val;
2964 ucontrol->value.integer.value[0] = val;
2965
2966 return 0;
2967}
2968EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
2969
2970/**
2971 * snd_soc_put_xr_sx - signed multi register get callback
2972 * @kcontrol: mreg control
2973 * @ucontrol: control element information
2974 *
2975 * Callback to set the value of a control that can span
2976 * multiple codec registers which together forms a single
2977 * signed value in a MSB/LSB manner. The control supports
2978 * specifying total no of bits used to allow for bitfields
2979 * across the multiple codec registers.
2980 *
2981 * Returns 0 for success.
2982 */
2983int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
2984 struct snd_ctl_elem_value *ucontrol)
2985{
2986 struct soc_mreg_control *mc =
2987 (struct soc_mreg_control *)kcontrol->private_value;
2988 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2989 unsigned int regbase = mc->regbase;
2990 unsigned int regcount = mc->regcount;
2991 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
2992 unsigned int regwmask = (1<<regwshift)-1;
2993 unsigned int invert = mc->invert;
2994 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02002995 long max = mc->max;
2996 long val = ucontrol->value.integer.value[0];
2997 unsigned int i, regval, regmask;
2998 int err;
2999
3000 if (invert)
3001 val = max - val;
3002 val &= mask;
3003 for (i = 0; i < regcount; i++) {
3004 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3005 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3006 err = snd_soc_update_bits_locked(codec, regbase+i,
3007 regmask, regval);
3008 if (err < 0)
3009 return err;
3010 }
3011
3012 return 0;
3013}
3014EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3015
3016/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003017 * snd_soc_get_strobe - strobe get callback
3018 * @kcontrol: mixer control
3019 * @ucontrol: control element information
3020 *
3021 * Callback get the value of a strobe mixer control.
3022 *
3023 * Returns 0 for success.
3024 */
3025int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3026 struct snd_ctl_elem_value *ucontrol)
3027{
3028 struct soc_mixer_control *mc =
3029 (struct soc_mixer_control *)kcontrol->private_value;
3030 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3031 unsigned int reg = mc->reg;
3032 unsigned int shift = mc->shift;
3033 unsigned int mask = 1 << shift;
3034 unsigned int invert = mc->invert != 0;
3035 unsigned int val = snd_soc_read(codec, reg) & mask;
3036
3037 if (shift != 0 && val != 0)
3038 val = val >> shift;
3039 ucontrol->value.enumerated.item[0] = val ^ invert;
3040
3041 return 0;
3042}
3043EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3044
3045/**
3046 * snd_soc_put_strobe - strobe put callback
3047 * @kcontrol: mixer control
3048 * @ucontrol: control element information
3049 *
3050 * Callback strobe a register bit to high then low (or the inverse)
3051 * in one pass of a single mixer enum control.
3052 *
3053 * Returns 1 for success.
3054 */
3055int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3056 struct snd_ctl_elem_value *ucontrol)
3057{
3058 struct soc_mixer_control *mc =
3059 (struct soc_mixer_control *)kcontrol->private_value;
3060 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3061 unsigned int reg = mc->reg;
3062 unsigned int shift = mc->shift;
3063 unsigned int mask = 1 << shift;
3064 unsigned int invert = mc->invert != 0;
3065 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3066 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3067 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3068 int err;
3069
3070 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3071 if (err < 0)
3072 return err;
3073
3074 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3075 return err;
3076}
3077EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3078
3079/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003080 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3081 * @dai: DAI
3082 * @clk_id: DAI specific clock ID
3083 * @freq: new clock frequency in Hz
3084 * @dir: new clock direction - input/output.
3085 *
3086 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3087 */
3088int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3089 unsigned int freq, int dir)
3090{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003091 if (dai->driver && dai->driver->ops->set_sysclk)
3092 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003093 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003094 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003095 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003096 else
3097 return -EINVAL;
3098}
3099EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3100
3101/**
Mark Brownec4ee522011-03-07 20:58:11 +00003102 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3103 * @codec: CODEC
3104 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003105 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003106 * @freq: new clock frequency in Hz
3107 * @dir: new clock direction - input/output.
3108 *
3109 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3110 */
3111int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003112 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003113{
3114 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003115 return codec->driver->set_sysclk(codec, clk_id, source,
3116 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003117 else
3118 return -EINVAL;
3119}
3120EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3121
3122/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003123 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3124 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003125 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003126 * @div: new clock divisor.
3127 *
3128 * Configures the clock dividers. This is used to derive the best DAI bit and
3129 * frame clocks from the system or master clock. It's best to set the DAI bit
3130 * and frame clocks as low as possible to save system power.
3131 */
3132int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3133 int div_id, int div)
3134{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003135 if (dai->driver && dai->driver->ops->set_clkdiv)
3136 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003137 else
3138 return -EINVAL;
3139}
3140EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3141
3142/**
3143 * snd_soc_dai_set_pll - configure DAI PLL.
3144 * @dai: DAI
3145 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003146 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003147 * @freq_in: PLL input clock frequency in Hz
3148 * @freq_out: requested PLL output clock frequency in Hz
3149 *
3150 * Configures and enables PLL to generate output clock based on input clock.
3151 */
Mark Brown85488032009-09-05 18:52:16 +01003152int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3153 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003154{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003155 if (dai->driver && dai->driver->ops->set_pll)
3156 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003157 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003158 else if (dai->codec && dai->codec->driver->set_pll)
3159 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3160 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003161 else
3162 return -EINVAL;
3163}
3164EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3165
Mark Brownec4ee522011-03-07 20:58:11 +00003166/*
3167 * snd_soc_codec_set_pll - configure codec PLL.
3168 * @codec: CODEC
3169 * @pll_id: DAI specific PLL ID
3170 * @source: DAI specific source for the PLL
3171 * @freq_in: PLL input clock frequency in Hz
3172 * @freq_out: requested PLL output clock frequency in Hz
3173 *
3174 * Configures and enables PLL to generate output clock based on input clock.
3175 */
3176int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3177 unsigned int freq_in, unsigned int freq_out)
3178{
3179 if (codec->driver->set_pll)
3180 return codec->driver->set_pll(codec, pll_id, source,
3181 freq_in, freq_out);
3182 else
3183 return -EINVAL;
3184}
3185EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3186
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003187/**
3188 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3189 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003190 * @fmt: SND_SOC_DAIFMT_ format value.
3191 *
3192 * Configures the DAI hardware format and clocking.
3193 */
3194int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3195{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003196 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003197 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003198 if (dai->driver->ops->set_fmt == NULL)
3199 return -ENOTSUPP;
3200 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003201}
3202EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3203
3204/**
3205 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3206 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003207 * @tx_mask: bitmask representing active TX slots.
3208 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003209 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003210 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003211 *
3212 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3213 * specific.
3214 */
3215int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003216 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003217{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003218 if (dai->driver && dai->driver->ops->set_tdm_slot)
3219 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003220 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003221 else
3222 return -EINVAL;
3223}
3224EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3225
3226/**
Barry Song472df3c2009-09-12 01:16:29 +08003227 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3228 * @dai: DAI
3229 * @tx_num: how many TX channels
3230 * @tx_slot: pointer to an array which imply the TX slot number channel
3231 * 0~num-1 uses
3232 * @rx_num: how many RX channels
3233 * @rx_slot: pointer to an array which imply the RX slot number channel
3234 * 0~num-1 uses
3235 *
3236 * configure the relationship between channel number and TDM slot number.
3237 */
3238int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3239 unsigned int tx_num, unsigned int *tx_slot,
3240 unsigned int rx_num, unsigned int *rx_slot)
3241{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003242 if (dai->driver && dai->driver->ops->set_channel_map)
3243 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003244 rx_num, rx_slot);
3245 else
3246 return -EINVAL;
3247}
3248EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3249
3250/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003251 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3252 * @dai: DAI
3253 * @tristate: tristate enable
3254 *
3255 * Tristates the DAI so that others can use it.
3256 */
3257int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3258{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003259 if (dai->driver && dai->driver->ops->set_tristate)
3260 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003261 else
3262 return -EINVAL;
3263}
3264EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3265
3266/**
3267 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3268 * @dai: DAI
3269 * @mute: mute enable
3270 *
3271 * Mutes the DAI DAC.
3272 */
3273int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3274{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003275 if (dai->driver && dai->driver->ops->digital_mute)
3276 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003277 else
Mark Brown04570c62012-04-13 19:16:03 +01003278 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003279}
3280EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3281
Mark Brownc5af3a22008-11-28 13:29:45 +00003282/**
3283 * snd_soc_register_card - Register a card with the ASoC core
3284 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003285 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003286 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003287 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303288int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003289{
Mark Brownb19e6e72012-03-14 21:18:39 +00003290 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003291
Mark Brownc5af3a22008-11-28 13:29:45 +00003292 if (!card->name || !card->dev)
3293 return -EINVAL;
3294
Stephen Warren5a504962011-12-21 10:40:59 -07003295 for (i = 0; i < card->num_links; i++) {
3296 struct snd_soc_dai_link *link = &card->dai_link[i];
3297
3298 /*
3299 * Codec must be specified by 1 of name or OF node,
3300 * not both or neither.
3301 */
3302 if (!!link->codec_name == !!link->codec_of_node) {
3303 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003304 "Neither/both codec name/of_node are set for %s\n",
3305 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003306 return -EINVAL;
3307 }
3308
3309 /*
3310 * Platform may be specified by either name or OF node, but
3311 * can be left unspecified, and a dummy platform will be used.
3312 */
3313 if (link->platform_name && link->platform_of_node) {
3314 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003315 "Both platform name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003316 return -EINVAL;
3317 }
3318
3319 /*
3320 * CPU DAI must be specified by 1 of name or OF node,
3321 * not both or neither.
3322 */
3323 if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
3324 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003325 "Neither/both cpu_dai name/of_node are set for %s\n",
3326 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003327 return -EINVAL;
3328 }
3329 }
3330
Mark Browned77cc12011-05-03 18:25:34 +01003331 dev_set_drvdata(card->dev, card);
3332
Stephen Warren111c6412011-01-28 14:26:35 -07003333 snd_soc_initialize_card_lists(card);
3334
Vinod Koul150dd2f2011-01-13 22:48:32 +05303335 soc_init_card_debugfs(card);
3336
Mark Brown181a6892012-03-14 20:18:49 +00003337 card->rtd = devm_kzalloc(card->dev,
3338 sizeof(struct snd_soc_pcm_runtime) *
3339 (card->num_links + card->num_aux_devs),
3340 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003341 if (card->rtd == NULL)
3342 return -ENOMEM;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003343 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003344
3345 for (i = 0; i < card->num_links; i++)
3346 card->rtd[i].dai_link = &card->dai_link[i];
3347
Mark Brownc5af3a22008-11-28 13:29:45 +00003348 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003349 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003350 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003351 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003352 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003353
Mark Brownb19e6e72012-03-14 21:18:39 +00003354 ret = snd_soc_instantiate_card(card);
3355 if (ret != 0)
3356 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003357
Mark Brownb19e6e72012-03-14 21:18:39 +00003358 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003359}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303360EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003361
3362/**
3363 * snd_soc_unregister_card - Unregister a card with the ASoC core
3364 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003365 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003366 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003367 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303368int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003369{
Vinod Koulb0e26482011-01-13 22:48:02 +05303370 if (card->instantiated)
3371 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003372 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3373
3374 return 0;
3375}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303376EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003377
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003378/*
3379 * Simplify DAI link configuration by removing ".-1" from device names
3380 * and sanitizing names.
3381 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003382static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003383{
3384 char *found, name[NAME_SIZE];
3385 int id1, id2;
3386
3387 if (dev_name(dev) == NULL)
3388 return NULL;
3389
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003390 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003391
3392 /* are we a "%s.%d" name (platform and SPI components) */
3393 found = strstr(name, dev->driver->name);
3394 if (found) {
3395 /* get ID */
3396 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3397
3398 /* discard ID from name if ID == -1 */
3399 if (*id == -1)
3400 found[strlen(dev->driver->name)] = '\0';
3401 }
3402
3403 } else {
3404 /* I2C component devices are named "bus-addr" */
3405 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3406 char tmp[NAME_SIZE];
3407
3408 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003409 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003410
3411 /* sanitize component name for DAI link creation */
3412 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003413 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003414 } else
3415 *id = 0;
3416 }
3417
3418 return kstrdup(name, GFP_KERNEL);
3419}
3420
3421/*
3422 * Simplify DAI link naming for single devices with multiple DAIs by removing
3423 * any ".-1" and using the DAI name (instead of device name).
3424 */
3425static inline char *fmt_multiple_name(struct device *dev,
3426 struct snd_soc_dai_driver *dai_drv)
3427{
3428 if (dai_drv->name == NULL) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02003429 pr_err("asoc: error - multiple DAI %s registered with no name\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003430 dev_name(dev));
3431 return NULL;
3432 }
3433
3434 return kstrdup(dai_drv->name, GFP_KERNEL);
3435}
3436
Mark Brown91151712008-11-30 23:31:24 +00003437/**
3438 * snd_soc_register_dai - Register a DAI with the ASoC core
3439 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003440 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003441 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003442int snd_soc_register_dai(struct device *dev,
3443 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003444{
Mark Brown054880f2012-04-09 17:29:19 +01003445 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003446 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003447
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003448 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003449
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003450 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3451 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003452 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003453
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003454 /* create DAI component name */
3455 dai->name = fmt_single_name(dev, &dai->id);
3456 if (dai->name == NULL) {
3457 kfree(dai);
3458 return -ENOMEM;
3459 }
3460
3461 dai->dev = dev;
3462 dai->driver = dai_drv;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003463 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003464 if (!dai->driver->ops)
3465 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003466
3467 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003468
3469 list_for_each_entry(codec, &codec_list, list) {
3470 if (codec->dev == dev) {
3471 dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3472 dai->name, codec->name);
3473 dai->codec = codec;
3474 break;
3475 }
3476 }
3477
Mark Brown91151712008-11-30 23:31:24 +00003478 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003479
Mark Brown91151712008-11-30 23:31:24 +00003480 mutex_unlock(&client_mutex);
3481
3482 pr_debug("Registered DAI '%s'\n", dai->name);
3483
3484 return 0;
3485}
3486EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3487
3488/**
3489 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3490 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003491 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003492 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003493void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003494{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003495 struct snd_soc_dai *dai;
3496
3497 list_for_each_entry(dai, &dai_list, list) {
3498 if (dev == dai->dev)
3499 goto found;
3500 }
3501 return;
3502
3503found:
Mark Brown91151712008-11-30 23:31:24 +00003504 mutex_lock(&client_mutex);
3505 list_del(&dai->list);
3506 mutex_unlock(&client_mutex);
3507
3508 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003509 kfree(dai->name);
3510 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003511}
3512EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3513
3514/**
3515 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3516 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003517 * @dai: Array of DAIs to register
3518 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003519 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003520int snd_soc_register_dais(struct device *dev,
3521 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003522{
Mark Brown054880f2012-04-09 17:29:19 +01003523 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003524 struct snd_soc_dai *dai;
3525 int i, ret = 0;
3526
Liam Girdwood720ffa42010-08-18 00:30:30 +01003527 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003528
3529 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003530
3531 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003532 if (dai == NULL) {
3533 ret = -ENOMEM;
3534 goto err;
3535 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003536
3537 /* create DAI component name */
3538 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3539 if (dai->name == NULL) {
3540 kfree(dai);
3541 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003542 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003543 }
3544
3545 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003546 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003547 if (dai->driver->id)
3548 dai->id = dai->driver->id;
3549 else
3550 dai->id = i;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003551 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003552 if (!dai->driver->ops)
3553 dai->driver->ops = &null_dai_ops;
3554
3555 mutex_lock(&client_mutex);
Mark Brown054880f2012-04-09 17:29:19 +01003556
3557 list_for_each_entry(codec, &codec_list, list) {
3558 if (codec->dev == dev) {
3559 dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3560 dai->name, codec->name);
3561 dai->codec = codec;
3562 break;
3563 }
3564 }
3565
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003566 list_add(&dai->list, &dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003567
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003568 mutex_unlock(&client_mutex);
3569
3570 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003571 }
3572
3573 return 0;
3574
3575err:
3576 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003577 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003578
3579 return ret;
3580}
3581EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3582
3583/**
3584 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3585 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003586 * @dai: Array of DAIs to unregister
3587 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003588 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003589void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003590{
3591 int i;
3592
3593 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003594 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003595}
3596EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3597
Mark Brown12a48a8c2008-12-03 19:40:30 +00003598/**
3599 * snd_soc_register_platform - Register a platform with the ASoC core
3600 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003601 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003602 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003603int snd_soc_register_platform(struct device *dev,
3604 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003605{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003606 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003607
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003608 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3609
3610 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3611 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003612 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003613
3614 /* create platform component name */
3615 platform->name = fmt_single_name(dev, &platform->id);
3616 if (platform->name == NULL) {
3617 kfree(platform);
3618 return -ENOMEM;
3619 }
3620
3621 platform->dev = dev;
3622 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003623 platform->dapm.dev = dev;
3624 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003625 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00003626 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003627
3628 mutex_lock(&client_mutex);
3629 list_add(&platform->list, &platform_list);
3630 mutex_unlock(&client_mutex);
3631
3632 pr_debug("Registered platform '%s'\n", platform->name);
3633
3634 return 0;
3635}
3636EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3637
3638/**
3639 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3640 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003641 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003642 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003643void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003644{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003645 struct snd_soc_platform *platform;
3646
3647 list_for_each_entry(platform, &platform_list, list) {
3648 if (dev == platform->dev)
3649 goto found;
3650 }
3651 return;
3652
3653found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003654 mutex_lock(&client_mutex);
3655 list_del(&platform->list);
3656 mutex_unlock(&client_mutex);
3657
3658 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003659 kfree(platform->name);
3660 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003661}
3662EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3663
Mark Brown151ab222009-05-09 16:22:58 +01003664static u64 codec_format_map[] = {
3665 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3666 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3667 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3668 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3669 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3670 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3671 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3672 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3673 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3674 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3675 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3676 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3677 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3678 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3679 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3680 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3681};
3682
3683/* Fix up the DAI formats for endianness: codecs don't actually see
3684 * the endianness of the data but we're using the CPU format
3685 * definitions which do need to include endianness so we ensure that
3686 * codec DAIs always have both big and little endian variants set.
3687 */
3688static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3689{
3690 int i;
3691
3692 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3693 if (stream->formats & codec_format_map[i])
3694 stream->formats |= codec_format_map[i];
3695}
3696
Mark Brown0d0cf002008-12-10 14:32:45 +00003697/**
3698 * snd_soc_register_codec - Register a codec with the ASoC core
3699 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003700 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003701 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003702int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003703 const struct snd_soc_codec_driver *codec_drv,
3704 struct snd_soc_dai_driver *dai_drv,
3705 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003706{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003707 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003708 struct snd_soc_codec *codec;
3709 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003710
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003711 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003712
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003713 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3714 if (codec == NULL)
3715 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003716
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003717 /* create CODEC component name */
3718 codec->name = fmt_single_name(dev, &codec->id);
3719 if (codec->name == NULL) {
3720 kfree(codec);
3721 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003722 }
3723
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003724 if (codec_drv->compress_type)
3725 codec->compress_type = codec_drv->compress_type;
3726 else
3727 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3728
Mark Brownc3acec22010-12-02 16:15:29 +00003729 codec->write = codec_drv->write;
3730 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003731 codec->volatile_register = codec_drv->volatile_register;
3732 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003733 codec->writable_register = codec_drv->writable_register;
Mark Brown5124e692012-02-08 13:20:50 +00003734 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003735 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3736 codec->dapm.dev = dev;
3737 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003738 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003739 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003740 codec->dev = dev;
3741 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003742 codec->num_dai = num_dai;
3743 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003744
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003745 /* allocate CODEC register cache */
3746 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003747 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003748 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003749 /* it is necessary to make a copy of the default register cache
3750 * because in the case of using a compression type that requires
3751 * the default register cache to be marked as __devinitconst the
3752 * kernel might have freed the array by the time we initialize
3753 * the cache.
3754 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003755 if (codec_drv->reg_cache_default) {
3756 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3757 reg_size, GFP_KERNEL);
3758 if (!codec->reg_def_copy) {
3759 ret = -ENOMEM;
3760 goto fail;
3761 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003762 }
3763 }
3764
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003765 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3766 if (!codec->volatile_register)
3767 codec->volatile_register = snd_soc_default_volatile_register;
3768 if (!codec->readable_register)
3769 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003770 if (!codec->writable_register)
3771 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003772 }
3773
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003774 for (i = 0; i < num_dai; i++) {
3775 fixup_codec_formats(&dai_drv[i].playback);
3776 fixup_codec_formats(&dai_drv[i].capture);
3777 }
3778
Mark Brown054880f2012-04-09 17:29:19 +01003779 mutex_lock(&client_mutex);
3780 list_add(&codec->list, &codec_list);
3781 mutex_unlock(&client_mutex);
3782
Mark Brown26b01cc2010-08-18 20:20:55 +01003783 /* register any DAIs */
3784 if (num_dai) {
3785 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3786 if (ret < 0)
Mark Brown054880f2012-04-09 17:29:19 +01003787 dev_err(codec->dev, "Failed to regster DAIs: %d\n",
3788 ret);
Mark Brown26b01cc2010-08-18 20:20:55 +01003789 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003790
Mark Brown0d0cf002008-12-10 14:32:45 +00003791 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003792 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003793
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003794fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003795 kfree(codec->reg_def_copy);
3796 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003797 kfree(codec->name);
3798 kfree(codec);
3799 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003800}
3801EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3802
3803/**
3804 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3805 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003806 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003807 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003808void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003809{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003810 struct snd_soc_codec *codec;
3811 int i;
3812
3813 list_for_each_entry(codec, &codec_list, list) {
3814 if (dev == codec->dev)
3815 goto found;
3816 }
3817 return;
3818
3819found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003820 if (codec->num_dai)
3821 for (i = 0; i < codec->num_dai; i++)
3822 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003823
Mark Brown0d0cf002008-12-10 14:32:45 +00003824 mutex_lock(&client_mutex);
3825 list_del(&codec->list);
3826 mutex_unlock(&client_mutex);
3827
3828 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003829
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003830 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003831 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003832 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003833 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003834}
3835EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3836
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003837/* Retrieve a card's name from device tree */
3838int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3839 const char *propname)
3840{
3841 struct device_node *np = card->dev->of_node;
3842 int ret;
3843
3844 ret = of_property_read_string_index(np, propname, 0, &card->name);
3845 /*
3846 * EINVAL means the property does not exist. This is fine providing
3847 * card->name was previously set, which is checked later in
3848 * snd_soc_register_card.
3849 */
3850 if (ret < 0 && ret != -EINVAL) {
3851 dev_err(card->dev,
3852 "Property '%s' could not be read: %d\n",
3853 propname, ret);
3854 return ret;
3855 }
3856
3857 return 0;
3858}
3859EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3860
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003861int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3862 const char *propname)
3863{
3864 struct device_node *np = card->dev->of_node;
3865 int num_routes;
3866 struct snd_soc_dapm_route *routes;
3867 int i, ret;
3868
3869 num_routes = of_property_count_strings(np, propname);
3870 if (num_routes & 1) {
3871 dev_err(card->dev,
3872 "Property '%s's length is not even\n",
3873 propname);
3874 return -EINVAL;
3875 }
3876 num_routes /= 2;
3877 if (!num_routes) {
3878 dev_err(card->dev,
3879 "Property '%s's length is zero\n",
3880 propname);
3881 return -EINVAL;
3882 }
3883
3884 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3885 GFP_KERNEL);
3886 if (!routes) {
3887 dev_err(card->dev,
3888 "Could not allocate DAPM route table\n");
3889 return -EINVAL;
3890 }
3891
3892 for (i = 0; i < num_routes; i++) {
3893 ret = of_property_read_string_index(np, propname,
3894 2 * i, &routes[i].sink);
3895 if (ret) {
3896 dev_err(card->dev,
3897 "Property '%s' index %d could not be read: %d\n",
3898 propname, 2 * i, ret);
3899 return -EINVAL;
3900 }
3901 ret = of_property_read_string_index(np, propname,
3902 (2 * i) + 1, &routes[i].source);
3903 if (ret) {
3904 dev_err(card->dev,
3905 "Property '%s' index %d could not be read: %d\n",
3906 propname, (2 * i) + 1, ret);
3907 return -EINVAL;
3908 }
3909 }
3910
3911 card->num_dapm_routes = num_routes;
3912 card->dapm_routes = routes;
3913
3914 return 0;
3915}
3916EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3917
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003918static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003919{
Mark Brown384c89e2008-12-03 17:34:03 +00003920#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003921 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3922 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02003923 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00003924 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00003925 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003926
Mark Brown8a9dab12011-01-10 22:25:21 +00003927 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01003928 &codec_list_fops))
3929 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3930
Mark Brown8a9dab12011-01-10 22:25:21 +00003931 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01003932 &dai_list_fops))
3933 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003934
Mark Brown8a9dab12011-01-10 22:25:21 +00003935 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01003936 &platform_list_fops))
3937 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003938#endif
3939
Mark Brownfb257892011-04-28 10:57:54 +01003940 snd_soc_util_init();
3941
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003942 return platform_driver_register(&soc_driver);
3943}
Mark Brown4abe8e12010-10-12 17:41:03 +01003944module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003945
Mark Brown7d8c16a2008-11-30 22:11:24 +00003946static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003947{
Mark Brownfb257892011-04-28 10:57:54 +01003948 snd_soc_util_exit();
3949
Mark Brown384c89e2008-12-03 17:34:03 +00003950#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003951 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00003952#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003953 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003954}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003955module_exit(snd_soc_exit);
3956
3957/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003958MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003959MODULE_DESCRIPTION("ALSA SoC Core");
3960MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003961MODULE_ALIAS("platform:soc-audio");