blob: a4592cbee49bad40d3dee93fbf9a7e01727517a2 [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>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020042#include <sound/initval.h>
43
Mark Browna8b1d342010-11-03 18:05:58 -040044#define CREATE_TRACE_POINTS
45#include <trace/events/asoc.h>
46
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000047#define NAME_SIZE 32
48
Frank Mandarinodb2a4162006-10-06 18:31:09 +020049static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
50
Mark Brown384c89e2008-12-03 17:34:03 +000051#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000052struct dentry *snd_soc_debugfs_root;
53EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000054#endif
55
Mark Brownc5af3a22008-11-28 13:29:45 +000056static DEFINE_MUTEX(client_mutex);
57static LIST_HEAD(card_list);
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
Liam Girdwoodddee6272011-06-09 14:45:53 +010062int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
Mark Brownc5af3a22008-11-28 13:29:45 +000063
Frank Mandarinodb2a4162006-10-06 18:31:09 +020064/*
65 * This is a timeout to do a DAPM powerdown after a stream is closed().
66 * It can be used to eliminate pops between different playback streams, e.g.
67 * between two audio tracks.
68 */
69static int pmdown_time = 5000;
70module_param(pmdown_time, int, 0);
71MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
72
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000073/* returns the minimum number of bytes needed to represent
74 * a particular given value */
75static int min_bytes_needed(unsigned long val)
76{
77 int c = 0;
78 int i;
79
80 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
81 if (val & (1UL << i))
82 break;
83 c = (sizeof val * 8) - c;
84 if (!c || (c % 8))
85 c = (c + 8) / 8;
86 else
87 c /= 8;
88 return c;
89}
90
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000091/* fill buf which is 'len' bytes with a formatted
92 * string of the form 'reg: value\n' */
93static int format_register_str(struct snd_soc_codec *codec,
94 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +000095{
Stephen Warren00b317a2011-04-01 14:50:44 -060096 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
97 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000098 int ret;
99 char tmpbuf[len + 1];
100 char regbuf[regsize + 1];
101
102 /* since tmpbuf is allocated on the stack, warn the callers if they
103 * try to abuse this function */
104 WARN_ON(len > 63);
105
106 /* +2 for ': ' and + 1 for '\n' */
107 if (wordsize + regsize + 2 + 1 != len)
108 return -EINVAL;
109
Mark Brown25032c12011-08-01 13:52:48 +0900110 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000111 if (ret < 0) {
112 memset(regbuf, 'X', regsize);
113 regbuf[regsize] = '\0';
114 } else {
115 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
116 }
117
118 /* prepare the buffer */
119 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
120 /* copy it back to the caller without the '\0' */
121 memcpy(buf, tmpbuf, len);
122
123 return 0;
124}
125
126/* codec register dump */
127static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
128 size_t count, loff_t pos)
129{
130 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000131 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000132 int len;
133 size_t total = 0;
134 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000135
Stephen Warren00b317a2011-04-01 14:50:44 -0600136 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
137 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000138
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000139 len = wordsize + regsize + 2 + 1;
140
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000141 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000142 return 0;
143
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000144 if (codec->driver->reg_cache_step)
145 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000146
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000147 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausenb92d1502011-08-27 18:24:14 +0200148 if (!snd_soc_codec_readable_register(codec, i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000149 continue;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000150 if (codec->driver->display_register) {
151 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000152 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100153 } else {
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000154 /* only support larger than PAGE_SIZE bytes debugfs
155 * entries for the default case */
156 if (p >= pos) {
157 if (total + len >= count - 1)
158 break;
159 format_register_str(codec, i, buf + total, len);
160 total += len;
161 }
162 p += len;
Mark Brown5164d742010-07-14 16:14:33 +0100163 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000164 }
165
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000166 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000167
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000168 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000169}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000170
Mark Brown2624d5f2009-11-03 21:56:13 +0000171static ssize_t codec_reg_show(struct device *dev,
172 struct device_attribute *attr, char *buf)
173{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000174 struct snd_soc_pcm_runtime *rtd =
175 container_of(dev, struct snd_soc_pcm_runtime, dev);
176
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000177 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000178}
179
180static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
181
Mark Browndbe21402010-02-12 11:37:24 +0000182static ssize_t pmdown_time_show(struct device *dev,
183 struct device_attribute *attr, char *buf)
184{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000185 struct snd_soc_pcm_runtime *rtd =
186 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Browndbe21402010-02-12 11:37:24 +0000187
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000188 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000189}
190
191static ssize_t pmdown_time_set(struct device *dev,
192 struct device_attribute *attr,
193 const char *buf, size_t count)
194{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000195 struct snd_soc_pcm_runtime *rtd =
196 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Brownc593b522010-10-27 20:11:17 -0700197 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000198
Mark Brownc593b522010-10-27 20:11:17 -0700199 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
200 if (ret)
201 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000202
203 return count;
204}
205
206static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
207
Mark Brown2624d5f2009-11-03 21:56:13 +0000208#ifdef CONFIG_DEBUG_FS
209static int codec_reg_open_file(struct inode *inode, struct file *file)
210{
211 file->private_data = inode->i_private;
212 return 0;
213}
214
215static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000216 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000217{
218 ssize_t ret;
219 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000220 char *buf;
221
222 if (*ppos < 0 || !count)
223 return -EINVAL;
224
225 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000226 if (!buf)
227 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000228
229 ret = soc_codec_reg_show(codec, buf, count, *ppos);
230 if (ret >= 0) {
231 if (copy_to_user(user_buf, buf, ret)) {
232 kfree(buf);
233 return -EFAULT;
234 }
235 *ppos += ret;
236 }
237
Mark Brown2624d5f2009-11-03 21:56:13 +0000238 kfree(buf);
239 return ret;
240}
241
242static ssize_t codec_reg_write_file(struct file *file,
243 const char __user *user_buf, size_t count, loff_t *ppos)
244{
245 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700246 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000247 char *start = buf;
248 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000249 struct snd_soc_codec *codec = file->private_data;
250
251 buf_size = min(count, (sizeof(buf)-1));
252 if (copy_from_user(buf, user_buf, buf_size))
253 return -EFAULT;
254 buf[buf_size] = 0;
255
Mark Brown2624d5f2009-11-03 21:56:13 +0000256 while (*start == ' ')
257 start++;
258 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000259 while (*start == ' ')
260 start++;
261 if (strict_strtoul(start, 16, &value))
262 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000263
264 /* Userspace has been fiddling around behind the kernel's back */
265 add_taint(TAINT_USER);
266
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000267 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000268 return buf_size;
269}
270
271static const struct file_operations codec_reg_fops = {
272 .open = codec_reg_open_file,
273 .read = codec_reg_read_file,
274 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200275 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000276};
277
278static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
279{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200280 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
281
282 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
283 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000284 if (!codec->debugfs_codec_root) {
285 printk(KERN_WARNING
286 "ASoC: Failed to create codec debugfs directory\n");
287 return;
288 }
289
Mark Brownaaee8ef2011-01-26 20:53:50 +0000290 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
291 &codec->cache_sync);
292 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
293 &codec->cache_only);
294
Mark Brown2624d5f2009-11-03 21:56:13 +0000295 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
296 codec->debugfs_codec_root,
297 codec, &codec_reg_fops);
298 if (!codec->debugfs_reg)
299 printk(KERN_WARNING
300 "ASoC: Failed to create codec register debugfs file\n");
301
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200302 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000303}
304
305static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
306{
307 debugfs_remove_recursive(codec->debugfs_codec_root);
308}
309
Mark Brownc3c5a192010-09-15 18:15:14 +0100310static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
311 size_t count, loff_t *ppos)
312{
313 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100314 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100315 struct snd_soc_codec *codec;
316
317 if (!buf)
318 return -ENOMEM;
319
Mark Brown2b194f9d2010-10-13 10:52:16 +0100320 list_for_each_entry(codec, &codec_list, list) {
321 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
322 codec->name);
323 if (len >= 0)
324 ret += len;
325 if (ret > PAGE_SIZE) {
326 ret = PAGE_SIZE;
327 break;
328 }
329 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100330
331 if (ret >= 0)
332 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
333
334 kfree(buf);
335
336 return ret;
337}
338
339static const struct file_operations codec_list_fops = {
340 .read = codec_list_read_file,
341 .llseek = default_llseek,/* read accesses f_pos */
342};
343
Mark Brownf3208782010-09-15 18:19:07 +0100344static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
345 size_t count, loff_t *ppos)
346{
347 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100348 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100349 struct snd_soc_dai *dai;
350
351 if (!buf)
352 return -ENOMEM;
353
Mark Brown2b194f9d2010-10-13 10:52:16 +0100354 list_for_each_entry(dai, &dai_list, list) {
355 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
356 if (len >= 0)
357 ret += len;
358 if (ret > PAGE_SIZE) {
359 ret = PAGE_SIZE;
360 break;
361 }
362 }
Mark Brownf3208782010-09-15 18:19:07 +0100363
Mark Brown2b194f9d2010-10-13 10:52:16 +0100364 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100365
366 kfree(buf);
367
368 return ret;
369}
370
371static const struct file_operations dai_list_fops = {
372 .read = dai_list_read_file,
373 .llseek = default_llseek,/* read accesses f_pos */
374};
375
Mark Brown19c7ac22010-09-15 18:22:40 +0100376static ssize_t platform_list_read_file(struct file *file,
377 char __user *user_buf,
378 size_t count, loff_t *ppos)
379{
380 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100381 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100382 struct snd_soc_platform *platform;
383
384 if (!buf)
385 return -ENOMEM;
386
Mark Brown2b194f9d2010-10-13 10:52:16 +0100387 list_for_each_entry(platform, &platform_list, list) {
388 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
389 platform->name);
390 if (len >= 0)
391 ret += len;
392 if (ret > PAGE_SIZE) {
393 ret = PAGE_SIZE;
394 break;
395 }
396 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100397
Mark Brown2b194f9d2010-10-13 10:52:16 +0100398 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100399
400 kfree(buf);
401
402 return ret;
403}
404
405static const struct file_operations platform_list_fops = {
406 .read = platform_list_read_file,
407 .llseek = default_llseek,/* read accesses f_pos */
408};
409
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200410static void soc_init_card_debugfs(struct snd_soc_card *card)
411{
412 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000413 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200414 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200415 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100416 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200417 return;
418 }
419
420 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
421 card->debugfs_card_root,
422 &card->pop_time);
423 if (!card->debugfs_pop_time)
424 dev_warn(card->dev,
425 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200426}
427
428static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
429{
430 debugfs_remove_recursive(card->debugfs_card_root);
431}
432
Mark Brown2624d5f2009-11-03 21:56:13 +0000433#else
434
435static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
436{
437}
438
439static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
440{
441}
Axel Linb95fccb2010-11-09 17:06:44 +0800442
443static inline void soc_init_card_debugfs(struct snd_soc_card *card)
444{
445}
446
447static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
448{
449}
Mark Brown2624d5f2009-11-03 21:56:13 +0000450#endif
451
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200452#ifdef CONFIG_SND_SOC_AC97_BUS
453/* unregister ac97 codec */
454static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
455{
456 if (codec->ac97->dev.bus)
457 device_unregister(&codec->ac97->dev);
458 return 0;
459}
460
461/* stop no dev release warning */
462static void soc_ac97_device_release(struct device *dev){}
463
464/* register ac97 codec to bus */
465static int soc_ac97_dev_register(struct snd_soc_codec *codec)
466{
467 int err;
468
469 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100470 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200471 codec->ac97->dev.release = soc_ac97_device_release;
472
Kay Sieversbb072bf2008-11-02 03:50:35 +0100473 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000474 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200475 err = device_register(&codec->ac97->dev);
476 if (err < 0) {
477 snd_printk(KERN_ERR "Can't register ac97 bus\n");
478 codec->ac97->dev.bus = NULL;
479 return err;
480 }
481 return 0;
482}
483#endif
484
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000485#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200486/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000487int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200488{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000489 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200490 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200491 int i;
492
Daniel Macke3509ff2009-06-03 17:44:49 +0200493 /* If the initialization of this soc device failed, there is no codec
494 * associated with it. Just bail out in this case.
495 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000496 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200497 return 0;
498
Andy Green6ed25972008-06-13 16:24:05 +0100499 /* Due to the resume being scheduled into a workqueue we could
500 * suspend before that's finished - wait for it to complete.
501 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000502 snd_power_lock(card->snd_card);
503 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
504 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100505
506 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000507 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100508
Mark Browna00f90f2010-12-02 16:24:24 +0000509 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000510 for (i = 0; i < card->num_rtd; i++) {
511 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
512 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100513
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000514 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100515 continue;
516
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000517 if (drv->ops->digital_mute && dai->playback_active)
518 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200519 }
520
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100521 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000522 for (i = 0; i < card->num_rtd; i++) {
523 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100524 continue;
525
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000526 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100527 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100528
Mark Brown87506542008-11-18 20:50:34 +0000529 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000530 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200531
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000532 for (i = 0; i < card->num_rtd; i++) {
533 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
534 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100535
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000536 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100537 continue;
538
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000539 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
540 cpu_dai->driver->suspend(cpu_dai);
541 if (platform->driver->suspend && !platform->suspended) {
542 platform->driver->suspend(cpu_dai);
543 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100544 }
545 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200546
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000547 /* close any waiting streams and save state */
548 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +0100549 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200550 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000551 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100552
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000553 for (i = 0; i < card->num_rtd; i++) {
554 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
555
556 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100557 continue;
558
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000559 if (driver->playback.stream_name != NULL)
560 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
561 SND_SOC_DAPM_STREAM_SUSPEND);
562
563 if (driver->capture.stream_name != NULL)
564 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
565 SND_SOC_DAPM_STREAM_SUSPEND);
566 }
567
568 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200569 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000570 /* If there are paths active then the CODEC will be held with
571 * bias _ON and should not be suspended. */
572 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200573 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000574 case SND_SOC_BIAS_STANDBY:
575 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100576 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000577 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900578 codec->cache_sync = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000579 break;
580 default:
581 dev_dbg(codec->dev, "CODEC is on over suspend\n");
582 break;
583 }
584 }
585 }
586
587 for (i = 0; i < card->num_rtd; i++) {
588 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
589
590 if (card->rtd[i].dai_link->ignore_suspend)
591 continue;
592
593 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
594 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200595 }
596
Mark Brown87506542008-11-18 20:50:34 +0000597 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000598 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200599
600 return 0;
601}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000602EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200603
Andy Green6ed25972008-06-13 16:24:05 +0100604/* deferred resume work, so resume can complete before we finished
605 * setting our codec back up, which can be very slow on I2C
606 */
607static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200608{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000609 struct snd_soc_card *card =
610 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200611 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200612 int i;
613
Andy Green6ed25972008-06-13 16:24:05 +0100614 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
615 * so userspace apps are blocked from touching us
616 */
617
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000618 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100619
Mark Brown99497882010-05-07 20:24:05 +0100620 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000621 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100622
Mark Brown87506542008-11-18 20:50:34 +0000623 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000624 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200625
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000626 /* resume AC97 DAIs */
627 for (i = 0; i < card->num_rtd; i++) {
628 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100629
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000630 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100631 continue;
632
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000633 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
634 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200635 }
636
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200637 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000638 /* If the CODEC was idle over suspend then it will have been
639 * left with bias OFF or STANDBY and suspended so we must now
640 * resume. Otherwise the suspend was suppressed.
641 */
642 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200643 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000644 case SND_SOC_BIAS_STANDBY:
645 case SND_SOC_BIAS_OFF:
646 codec->driver->resume(codec);
647 codec->suspended = 0;
648 break;
649 default:
650 dev_dbg(codec->dev, "CODEC was on over suspend\n");
651 break;
652 }
Mark Brown1547aba2010-05-07 21:11:40 +0100653 }
654 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200655
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000656 for (i = 0; i < card->num_rtd; i++) {
657 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100658
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000659 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100660 continue;
661
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000662 if (driver->playback.stream_name != NULL)
663 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200664 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000665
666 if (driver->capture.stream_name != NULL)
667 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200668 SND_SOC_DAPM_STREAM_RESUME);
669 }
670
Mark Brown3ff3f642008-05-19 12:32:25 +0200671 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000672 for (i = 0; i < card->num_rtd; i++) {
673 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
674 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100675
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000676 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100677 continue;
678
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000679 if (drv->ops->digital_mute && dai->playback_active)
680 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200681 }
682
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000683 for (i = 0; i < card->num_rtd; i++) {
684 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
685 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100686
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000687 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100688 continue;
689
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000690 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
691 cpu_dai->driver->resume(cpu_dai);
692 if (platform->driver->resume && platform->suspended) {
693 platform->driver->resume(cpu_dai);
694 platform->suspended = 0;
695 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200696 }
697
Mark Brown87506542008-11-18 20:50:34 +0000698 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000699 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200700
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000701 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100702
703 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000704 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100705}
706
707/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000708int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100709{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000710 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600711 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200712
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800713 /* If the initialization of this soc device failed, there is no codec
714 * associated with it. Just bail out in this case.
715 */
716 if (list_empty(&card->codec_dev_list))
717 return 0;
718
Mark Brown64ab9ba2009-03-31 11:27:03 +0100719 /* AC97 devices might have other drivers hanging off them so
720 * need to resume immediately. Other drivers don't have that
721 * problem and may take a substantial amount of time to resume
722 * due to I/O costs and anti-pop so handle them out of line.
723 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000724 for (i = 0; i < card->num_rtd; i++) {
725 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600726 ac97_control |= cpu_dai->driver->ac97_control;
727 }
728 if (ac97_control) {
729 dev_dbg(dev, "Resuming AC97 immediately\n");
730 soc_resume_deferred(&card->deferred_resume_work);
731 } else {
732 dev_dbg(dev, "Scheduling resume work\n");
733 if (!schedule_work(&card->deferred_resume_work))
734 dev_err(dev, "resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100735 }
Andy Green6ed25972008-06-13 16:24:05 +0100736
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200737 return 0;
738}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000739EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200740#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000741#define snd_soc_suspend NULL
742#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200743#endif
744
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100745static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800746};
747
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000748static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200749{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000750 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
751 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000752 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000753 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000754 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100755 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200756
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000757 if (rtd->complete)
758 return 1;
759 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000760
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000761 /* do we already have the CPU DAI for this link ? */
762 if (rtd->cpu_dai) {
763 goto find_codec;
764 }
765 /* no, then find CPU DAI from registered DAIs*/
766 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700767 if (dai_link->cpu_dai_of_node) {
768 if (cpu_dai->dev->of_node != dai_link->cpu_dai_of_node)
769 continue;
770 } else {
771 if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
772 continue;
773 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700774
775 rtd->cpu_dai = cpu_dai;
776 goto find_codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000777 }
778 dev_dbg(card->dev, "CPU DAI %s not registered\n",
779 dai_link->cpu_dai_name);
780
781find_codec:
782 /* do we already have the CODEC for this link ? */
783 if (rtd->codec) {
784 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +0000785 }
786
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000787 /* no, then find CODEC from registered CODECs*/
788 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700789 if (dai_link->codec_of_node) {
790 if (codec->dev->of_node != dai_link->codec_of_node)
791 continue;
792 } else {
793 if (strcmp(codec->name, dai_link->codec_name))
794 continue;
795 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000796
Stephen Warren2610ab72011-12-07 13:58:27 -0700797 rtd->codec = codec;
798
799 /*
800 * CODEC found, so find CODEC DAI from registered DAIs from
801 * this CODEC
802 */
803 list_for_each_entry(codec_dai, &dai_list, list) {
804 if (codec->dev == codec_dai->dev &&
805 !strcmp(codec_dai->name,
806 dai_link->codec_dai_name)) {
807
808 rtd->codec_dai = codec_dai;
809 goto find_platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000810 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000811 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700812 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
813 dai_link->codec_dai_name);
814
815 goto find_platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000816 }
817 dev_dbg(card->dev, "CODEC %s not registered\n",
818 dai_link->codec_name);
819
820find_platform:
Mark Brown848dd8b2011-04-27 18:16:32 +0100821 /* do we need a platform? */
822 if (rtd->platform)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000823 goto out;
Mark Brown848dd8b2011-04-27 18:16:32 +0100824
825 /* if there's no platform we match on the empty platform */
826 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700827 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100828 platform_name = "snd-soc-dummy";
829
830 /* no, then find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000831 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700832 if (dai_link->platform_of_node) {
833 if (platform->dev->of_node !=
834 dai_link->platform_of_node)
835 continue;
836 } else {
837 if (strcmp(platform->name, platform_name))
838 continue;
839 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700840
841 rtd->platform = platform;
842 goto out;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000843 }
844
845 dev_dbg(card->dev, "platform %s not registered\n",
846 dai_link->platform_name);
847 return 0;
848
849out:
850 /* mark rtd as complete if we found all 4 of our client devices */
851 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
852 rtd->complete = 1;
853 card->num_rtd++;
854 }
855 return 1;
856}
857
Jarkko Nikula589c3562010-12-06 16:27:07 +0200858static void soc_remove_codec(struct snd_soc_codec *codec)
859{
860 int err;
861
862 if (codec->driver->remove) {
863 err = codec->driver->remove(codec);
864 if (err < 0)
865 dev_err(codec->dev,
866 "asoc: failed to remove %s: %d\n",
867 codec->name, err);
868 }
869
870 /* Make sure all DAPM widgets are freed */
871 snd_soc_dapm_free(&codec->dapm);
872
873 soc_cleanup_codec_debugfs(codec);
874 codec->probed = 0;
875 list_del(&codec->card_list);
876 module_put(codec->dev->driver->owner);
877}
878
Liam Girdwood0168bf02011-06-07 16:08:05 +0100879static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000880{
881 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
882 struct snd_soc_codec *codec = rtd->codec;
883 struct snd_soc_platform *platform = rtd->platform;
884 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
885 int err;
886
887 /* unregister the rtd device */
888 if (rtd->dev_registered) {
889 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200890 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000891 device_unregister(&rtd->dev);
892 rtd->dev_registered = 0;
893 }
894
895 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100896 if (codec_dai && codec_dai->probed &&
897 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000898 if (codec_dai->driver->remove) {
899 err = codec_dai->driver->remove(codec_dai);
900 if (err < 0)
901 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
902 }
903 codec_dai->probed = 0;
904 list_del(&codec_dai->card_list);
905 }
906
907 /* remove the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100908 if (platform && platform->probed &&
909 platform->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000910 if (platform->driver->remove) {
911 err = platform->driver->remove(platform);
912 if (err < 0)
913 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
914 }
915 platform->probed = 0;
916 list_del(&platform->card_list);
917 module_put(platform->dev->driver->owner);
918 }
919
920 /* remove the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100921 if (codec && codec->probed &&
922 codec->driver->remove_order == order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200923 soc_remove_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000924
925 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100926 if (cpu_dai && cpu_dai->probed &&
927 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000928 if (cpu_dai->driver->remove) {
929 err = cpu_dai->driver->remove(cpu_dai);
930 if (err < 0)
931 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
932 }
933 cpu_dai->probed = 0;
934 list_del(&cpu_dai->card_list);
935 module_put(cpu_dai->dev->driver->owner);
936 }
937}
938
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900939static void soc_remove_dai_links(struct snd_soc_card *card)
940{
Liam Girdwood0168bf02011-06-07 16:08:05 +0100941 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900942
Liam Girdwood0168bf02011-06-07 16:08:05 +0100943 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
944 order++) {
945 for (dai = 0; dai < card->num_rtd; dai++)
946 soc_remove_dai_link(card, dai, order);
947 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900948 card->num_rtd = 0;
949}
950
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200951static void soc_set_name_prefix(struct snd_soc_card *card,
952 struct snd_soc_codec *codec)
953{
954 int i;
955
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000956 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200957 return;
958
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000959 for (i = 0; i < card->num_configs; i++) {
960 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200961 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
962 codec->name_prefix = map->name_prefix;
963 break;
964 }
965 }
966}
967
Jarkko Nikula589c3562010-12-06 16:27:07 +0200968static int soc_probe_codec(struct snd_soc_card *card,
969 struct snd_soc_codec *codec)
970{
971 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +0000972 const struct snd_soc_codec_driver *driver = codec->driver;
Jarkko Nikula589c3562010-12-06 16:27:07 +0200973
974 codec->card = card;
975 codec->dapm.card = card;
976 soc_set_name_prefix(card, codec);
977
Jarkko Nikula70d29332011-01-27 16:24:22 +0200978 if (!try_module_get(codec->dev->driver->owner))
979 return -ENODEV;
980
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +0200981 soc_init_codec_debugfs(codec);
982
Lars-Peter Clausen77530152011-05-05 16:59:09 +0200983 if (driver->dapm_widgets)
984 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
985 driver->num_dapm_widgets);
986
Mark Brown33c5f962011-08-22 18:40:30 +0100987 codec->dapm.idle_bias_off = driver->idle_bias_off;
988
Mark Brown89b95ac02011-03-07 16:38:44 +0000989 if (driver->probe) {
990 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200991 if (ret < 0) {
992 dev_err(codec->dev,
993 "asoc: failed to probe CODEC %s: %d\n",
994 codec->name, ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +0200995 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +0200996 }
997 }
998
Mark Brownb7af1da2011-04-07 19:18:44 +0900999 if (driver->controls)
1000 snd_soc_add_controls(codec, driver->controls,
1001 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001002 if (driver->dapm_routes)
1003 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1004 driver->num_dapm_routes);
1005
Jarkko Nikula589c3562010-12-06 16:27:07 +02001006 /* mark codec as probed and add to card codec list */
1007 codec->probed = 1;
1008 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001009 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001010
Jarkko Nikula70d29332011-01-27 16:24:22 +02001011 return 0;
1012
1013err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001014 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001015 module_put(codec->dev->driver->owner);
1016
Jarkko Nikula589c3562010-12-06 16:27:07 +02001017 return ret;
1018}
1019
Liam Girdwood956245e2011-07-01 16:54:08 +01001020static int soc_probe_platform(struct snd_soc_card *card,
1021 struct snd_soc_platform *platform)
1022{
1023 int ret = 0;
1024 const struct snd_soc_platform_driver *driver = platform->driver;
1025
1026 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001027 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001028
1029 if (!try_module_get(platform->dev->driver->owner))
1030 return -ENODEV;
1031
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001032 if (driver->dapm_widgets)
1033 snd_soc_dapm_new_controls(&platform->dapm,
1034 driver->dapm_widgets, driver->num_dapm_widgets);
1035
Liam Girdwood956245e2011-07-01 16:54:08 +01001036 if (driver->probe) {
1037 ret = driver->probe(platform);
1038 if (ret < 0) {
1039 dev_err(platform->dev,
1040 "asoc: failed to probe platform %s: %d\n",
1041 platform->name, ret);
1042 goto err_probe;
1043 }
1044 }
1045
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001046 if (driver->controls)
1047 snd_soc_add_platform_controls(platform, driver->controls,
1048 driver->num_controls);
1049 if (driver->dapm_routes)
1050 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1051 driver->num_dapm_routes);
1052
Liam Girdwood956245e2011-07-01 16:54:08 +01001053 /* mark platform as probed and add to card platform list */
1054 platform->probed = 1;
1055 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001056 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001057
1058 return 0;
1059
1060err_probe:
1061 module_put(platform->dev->driver->owner);
1062
1063 return ret;
1064}
1065
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001066static void rtd_release(struct device *dev) {}
1067
Jarkko Nikula589c3562010-12-06 16:27:07 +02001068static int soc_post_component_init(struct snd_soc_card *card,
1069 struct snd_soc_codec *codec,
1070 int num, int dailess)
1071{
1072 struct snd_soc_dai_link *dai_link = NULL;
1073 struct snd_soc_aux_dev *aux_dev = NULL;
1074 struct snd_soc_pcm_runtime *rtd;
1075 const char *temp, *name;
1076 int ret = 0;
1077
1078 if (!dailess) {
1079 dai_link = &card->dai_link[num];
1080 rtd = &card->rtd[num];
1081 name = dai_link->name;
1082 } else {
1083 aux_dev = &card->aux_dev[num];
1084 rtd = &card->rtd_aux[num];
1085 name = aux_dev->name;
1086 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001087 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001088
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001089 /* Make sure all DAPM widgets are instantiated */
1090 snd_soc_dapm_new_widgets(&codec->dapm);
1091
Jarkko Nikula589c3562010-12-06 16:27:07 +02001092 /* machine controls, routes and widgets are not prefixed */
1093 temp = codec->name_prefix;
1094 codec->name_prefix = NULL;
1095
1096 /* do machine specific initialization */
1097 if (!dailess && dai_link->init)
1098 ret = dai_link->init(rtd);
1099 else if (dailess && aux_dev->init)
1100 ret = aux_dev->init(&codec->dapm);
1101 if (ret < 0) {
1102 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1103 return ret;
1104 }
1105 codec->name_prefix = temp;
1106
Jarkko Nikula589c3562010-12-06 16:27:07 +02001107 /* register the rtd device */
1108 rtd->codec = codec;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001109 rtd->dev.parent = card->dev;
1110 rtd->dev.release = rtd_release;
1111 rtd->dev.init_name = name;
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001112 mutex_init(&rtd->pcm_mutex);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001113 ret = device_register(&rtd->dev);
1114 if (ret < 0) {
1115 dev_err(card->dev,
1116 "asoc: failed to register runtime device: %d\n", ret);
1117 return ret;
1118 }
1119 rtd->dev_registered = 1;
1120
1121 /* add DAPM sysfs entries for this codec */
1122 ret = snd_soc_dapm_sys_add(&rtd->dev);
1123 if (ret < 0)
1124 dev_err(codec->dev,
1125 "asoc: failed to add codec dapm sysfs entries: %d\n",
1126 ret);
1127
1128 /* add codec sysfs entries */
1129 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1130 if (ret < 0)
1131 dev_err(codec->dev,
1132 "asoc: failed to add codec sysfs files: %d\n", ret);
1133
1134 return 0;
1135}
1136
Liam Girdwood0168bf02011-06-07 16:08:05 +01001137static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001138{
1139 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1140 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1141 struct snd_soc_codec *codec = rtd->codec;
1142 struct snd_soc_platform *platform = rtd->platform;
1143 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1144 int ret;
1145
Liam Girdwood0168bf02011-06-07 16:08:05 +01001146 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1147 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001148
1149 /* config components */
1150 codec_dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001151 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001152 codec_dai->card = card;
1153 cpu_dai->card = card;
1154
1155 /* set default power off timeout */
1156 rtd->pmdown_time = pmdown_time;
1157
1158 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001159 if (!cpu_dai->probed &&
1160 cpu_dai->driver->probe_order == order) {
Liam Girdwood61b61e32011-05-24 13:57:43 +01001161 if (!try_module_get(cpu_dai->dev->driver->owner))
1162 return -ENODEV;
1163
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001164 if (cpu_dai->driver->probe) {
1165 ret = cpu_dai->driver->probe(cpu_dai);
1166 if (ret < 0) {
1167 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1168 cpu_dai->name);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001169 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001170 return ret;
1171 }
1172 }
1173 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001174 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001175 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1176 }
1177
1178 /* probe the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001179 if (!codec->probed &&
1180 codec->driver->probe_order == order) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001181 ret = soc_probe_codec(card, codec);
1182 if (ret < 0)
1183 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001184 }
1185
1186 /* probe the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001187 if (!platform->probed &&
1188 platform->driver->probe_order == order) {
Liam Girdwood956245e2011-07-01 16:54:08 +01001189 ret = soc_probe_platform(card, platform);
1190 if (ret < 0)
1191 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001192 }
1193
1194 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001195 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001196 if (codec_dai->driver->probe) {
1197 ret = codec_dai->driver->probe(codec_dai);
1198 if (ret < 0) {
1199 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1200 codec_dai->name);
1201 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001202 }
1203 }
1204
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001205 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001206 codec_dai->probed = 1;
1207 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001208 }
1209
Liam Girdwood0168bf02011-06-07 16:08:05 +01001210 /* complete DAI probe during last probe */
1211 if (order != SND_SOC_COMP_ORDER_LAST)
1212 return 0;
1213
Jarkko Nikula589c3562010-12-06 16:27:07 +02001214 ret = soc_post_component_init(card, codec, num, 0);
1215 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001216 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001217
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001218 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1219 if (ret < 0)
1220 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1221
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001222 /* create the pcm */
1223 ret = soc_new_pcm(rtd, num);
1224 if (ret < 0) {
1225 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1226 return ret;
1227 }
1228
1229 /* add platform data for AC97 devices */
1230 if (rtd->codec_dai->driver->ac97_control)
1231 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1232
1233 return 0;
1234}
1235
1236#ifdef CONFIG_SND_SOC_AC97_BUS
1237static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1238{
1239 int ret;
1240
1241 /* Only instantiate AC97 if not already done by the adaptor
1242 * for the generic AC97 subsystem.
1243 */
1244 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001245 /*
1246 * It is possible that the AC97 device is already registered to
1247 * the device subsystem. This happens when the device is created
1248 * via snd_ac97_mixer(). Currently only SoC codec that does so
1249 * is the generic AC97 glue but others migh emerge.
1250 *
1251 * In those cases we don't try to register the device again.
1252 */
1253 if (!rtd->codec->ac97_created)
1254 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001255
1256 ret = soc_ac97_dev_register(rtd->codec);
1257 if (ret < 0) {
1258 printk(KERN_ERR "asoc: AC97 device register failed\n");
1259 return ret;
1260 }
1261
1262 rtd->codec->ac97_registered = 1;
1263 }
1264 return 0;
1265}
1266
1267static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1268{
1269 if (codec->ac97_registered) {
1270 soc_ac97_dev_unregister(codec);
1271 codec->ac97_registered = 0;
1272 }
1273}
1274#endif
1275
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001276static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1277{
1278 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001279 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001280 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001281
1282 /* find CODEC from registered CODECs*/
1283 list_for_each_entry(codec, &codec_list, list) {
1284 if (!strcmp(codec->name, aux_dev->codec_name)) {
1285 if (codec->probed) {
1286 dev_err(codec->dev,
1287 "asoc: codec already probed");
1288 ret = -EBUSY;
1289 goto out;
1290 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001291 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001292 }
1293 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001294 /* codec not found */
1295 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1296 goto out;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001297
Jarkko Nikula676ad982010-12-03 09:18:22 +02001298found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001299 ret = soc_probe_codec(card, codec);
1300 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001301 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001302
Jarkko Nikula589c3562010-12-06 16:27:07 +02001303 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001304
1305out:
1306 return ret;
1307}
1308
1309static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1310{
1311 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1312 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001313
1314 /* unregister the rtd device */
1315 if (rtd->dev_registered) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001316 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001317 device_unregister(&rtd->dev);
1318 rtd->dev_registered = 0;
1319 }
1320
Jarkko Nikula589c3562010-12-06 16:27:07 +02001321 if (codec && codec->probed)
1322 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001323}
1324
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001325static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1326 enum snd_soc_compress_type compress_type)
1327{
1328 int ret;
1329
1330 if (codec->cache_init)
1331 return 0;
1332
1333 /* override the compress_type if necessary */
1334 if (compress_type && codec->compress_type != compress_type)
1335 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001336 ret = snd_soc_cache_init(codec);
1337 if (ret < 0) {
1338 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1339 ret);
1340 return ret;
1341 }
1342 codec->cache_init = 1;
1343 return 0;
1344}
1345
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001346static void snd_soc_instantiate_card(struct snd_soc_card *card)
1347{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001348 struct snd_soc_codec *codec;
1349 struct snd_soc_codec_conf *codec_conf;
1350 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001351 struct snd_soc_dai_link *dai_link;
Liam Girdwood0168bf02011-06-07 16:08:05 +01001352 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001353
1354 mutex_lock(&card->mutex);
1355
1356 if (card->instantiated) {
1357 mutex_unlock(&card->mutex);
1358 return;
1359 }
1360
1361 /* bind DAIs */
1362 for (i = 0; i < card->num_links; i++)
1363 soc_bind_dai_link(card, i);
1364
1365 /* bind completed ? */
1366 if (card->num_rtd != card->num_links) {
1367 mutex_unlock(&card->mutex);
1368 return;
1369 }
1370
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001371 /* initialize the register cache for each available codec */
1372 list_for_each_entry(codec, &codec_list, list) {
1373 if (codec->cache_init)
1374 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001375 /* by default we don't override the compress_type */
1376 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001377 /* check to see if we need to override the compress_type */
1378 for (i = 0; i < card->num_configs; ++i) {
1379 codec_conf = &card->codec_conf[i];
1380 if (!strcmp(codec->name, codec_conf->dev_name)) {
1381 compress_type = codec_conf->compress_type;
1382 if (compress_type && compress_type
1383 != codec->compress_type)
1384 break;
1385 }
1386 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001387 ret = snd_soc_init_codec_cache(codec, compress_type);
1388 if (ret < 0) {
1389 mutex_unlock(&card->mutex);
1390 return;
1391 }
1392 }
1393
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001394 /* card bind complete so register a sound card */
1395 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1396 card->owner, 0, &card->snd_card);
1397 if (ret < 0) {
1398 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1399 card->name);
1400 mutex_unlock(&card->mutex);
1401 return;
1402 }
1403 card->snd_card->dev = card->dev;
1404
Mark Browne37a4972011-03-02 18:21:57 +00001405 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1406 card->dapm.dev = card->dev;
1407 card->dapm.card = card;
1408 list_add(&card->dapm.list, &card->dapm_list);
1409
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001410#ifdef CONFIG_DEBUG_FS
1411 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1412#endif
1413
Mark Brown88ee1c62011-02-02 10:43:26 +00001414#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001415 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001416 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001417#endif
Andy Green6ed25972008-06-13 16:24:05 +01001418
Mark Brown9a841eb2011-04-12 17:51:37 -07001419 if (card->dapm_widgets)
1420 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1421 card->num_dapm_widgets);
1422
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001423 /* initialise the sound card only once */
1424 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001425 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001426 if (ret < 0)
1427 goto card_probe_error;
1428 }
1429
Liam Girdwood0168bf02011-06-07 16:08:05 +01001430 /* early DAI link probe */
1431 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1432 order++) {
1433 for (i = 0; i < card->num_links; i++) {
1434 ret = soc_probe_dai_link(card, i, order);
1435 if (ret < 0) {
1436 pr_err("asoc: failed to instantiate card %s: %d\n",
Mark Brown321de0d2010-09-21 15:09:37 +01001437 card->name, ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001438 goto probe_dai_err;
1439 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001440 }
1441 }
1442
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001443 for (i = 0; i < card->num_aux_devs; i++) {
1444 ret = soc_probe_aux_dev(card, i);
1445 if (ret < 0) {
1446 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1447 card->name, ret);
1448 goto probe_aux_dev_err;
1449 }
1450 }
1451
Mark Brownb7af1da2011-04-07 19:18:44 +09001452 /* We should have a non-codec control add function but we don't */
1453 if (card->controls)
1454 snd_soc_add_controls(list_first_entry(&card->codec_dev_list,
1455 struct snd_soc_codec,
1456 card_list),
1457 card->controls,
1458 card->num_controls);
1459
Mark Brownb8ad29d2011-03-02 18:35:51 +00001460 if (card->dapm_routes)
1461 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1462 card->num_dapm_routes);
1463
Mark Brownb90d2f92011-10-10 13:38:06 +01001464 snd_soc_dapm_new_widgets(&card->dapm);
1465
Mark Brown75d9ac42011-09-27 16:41:01 +01001466 for (i = 0; i < card->num_links; i++) {
1467 dai_link = &card->dai_link[i];
1468
1469 if (dai_link->dai_fmt) {
1470 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1471 dai_link->dai_fmt);
1472 if (ret != 0)
1473 dev_warn(card->rtd[i].codec_dai->dev,
1474 "Failed to set DAI format: %d\n",
1475 ret);
1476
1477 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1478 dai_link->dai_fmt);
1479 if (ret != 0)
1480 dev_warn(card->rtd[i].cpu_dai->dev,
1481 "Failed to set DAI format: %d\n",
1482 ret);
1483 }
1484 }
1485
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001486 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001487 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001488 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1489 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001490 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1491 "%s", card->driver_name ? card->driver_name : card->name);
1492 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1493 switch (card->snd_card->driver[i]) {
1494 case '_':
1495 case '-':
1496 case '\0':
1497 break;
1498 default:
1499 if (!isalnum(card->snd_card->driver[i]))
1500 card->snd_card->driver[i] = '_';
1501 break;
1502 }
1503 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001504
Mark Brown28e9ad92011-03-02 18:36:34 +00001505 if (card->late_probe) {
1506 ret = card->late_probe(card);
1507 if (ret < 0) {
1508 dev_err(card->dev, "%s late_probe() failed: %d\n",
1509 card->name, ret);
1510 goto probe_aux_dev_err;
1511 }
1512 }
1513
Mark Brown2dc00212011-10-08 13:59:44 +01001514 snd_soc_dapm_new_widgets(&card->dapm);
1515
Stephen Warren16332812011-11-23 12:42:04 -07001516 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001517 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001518 snd_soc_dapm_auto_nc_codec_pins(codec);
1519
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001520 ret = snd_card_register(card->snd_card);
1521 if (ret < 0) {
1522 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
Axel Lin6b3ed782010-12-07 16:12:29 +08001523 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001524 }
1525
1526#ifdef CONFIG_SND_SOC_AC97_BUS
1527 /* register any AC97 codecs */
1528 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001529 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1530 if (ret < 0) {
1531 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1532 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001533 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001534 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001535 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001536 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001537#endif
1538
Mark Brown435c5e22008-12-04 15:32:53 +00001539 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001540 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001541 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001542 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001543
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001544probe_aux_dev_err:
1545 for (i = 0; i < card->num_aux_devs; i++)
1546 soc_remove_aux_dev(card, i);
1547
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001548probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001549 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001550
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001551card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001552 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001553 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001554
1555 snd_card_free(card->snd_card);
1556
1557 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001558}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001559
Mark Brown435c5e22008-12-04 15:32:53 +00001560/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001561 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001562 * client_mutex.
1563 */
1564static void snd_soc_instantiate_cards(void)
1565{
1566 struct snd_soc_card *card;
1567 list_for_each_entry(card, &card_list, list)
1568 snd_soc_instantiate_card(card);
1569}
1570
1571/* probes a new socdev */
1572static int soc_probe(struct platform_device *pdev)
1573{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001574 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001575 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001576
Vinod Koul70a7ca32011-01-14 19:22:48 +05301577 /*
1578 * no card, so machine driver should be registering card
1579 * we should not be here in that case so ret error
1580 */
1581 if (!card)
1582 return -EINVAL;
1583
Mark Brown435c5e22008-12-04 15:32:53 +00001584 /* Bodge while we unpick instantiation */
1585 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001586
Mark Brown435c5e22008-12-04 15:32:53 +00001587 ret = snd_soc_register_card(card);
1588 if (ret != 0) {
1589 dev_err(&pdev->dev, "Failed to register card\n");
1590 return ret;
1591 }
1592
1593 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001594}
1595
Vinod Koulb0e26482011-01-13 22:48:02 +05301596static int soc_cleanup_card_resources(struct snd_soc_card *card)
1597{
Vinod Koulb0e26482011-01-13 22:48:02 +05301598 int i;
1599
1600 /* make sure any delayed work runs */
1601 for (i = 0; i < card->num_rtd; i++) {
1602 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1603 flush_delayed_work_sync(&rtd->delayed_work);
1604 }
1605
1606 /* remove auxiliary devices */
1607 for (i = 0; i < card->num_aux_devs; i++)
1608 soc_remove_aux_dev(card, i);
1609
1610 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001611 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301612
1613 soc_cleanup_card_debugfs(card);
1614
1615 /* remove the card */
1616 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001617 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301618
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001619 snd_soc_dapm_free(&card->dapm);
1620
Vinod Koulb0e26482011-01-13 22:48:02 +05301621 kfree(card->rtd);
1622 snd_card_free(card->snd_card);
1623 return 0;
1624
1625}
1626
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001627/* removes a socdev */
1628static int soc_remove(struct platform_device *pdev)
1629{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001630 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001631
Mark Brownc5af3a22008-11-28 13:29:45 +00001632 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001633 return 0;
1634}
1635
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001636int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001637{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001638 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001639 int i;
Mark Brown51737472009-06-22 13:16:51 +01001640
1641 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001642 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001643
1644 /* Flush out pmdown_time work - we actually do want to run it
1645 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001646 for (i = 0; i < card->num_rtd; i++) {
1647 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001648 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001649 }
Mark Brown51737472009-06-22 13:16:51 +01001650
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001651 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001652
1653 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001654}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001655EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001656
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001657const struct dev_pm_ops snd_soc_pm_ops = {
1658 .suspend = snd_soc_suspend,
1659 .resume = snd_soc_resume,
1660 .poweroff = snd_soc_poweroff,
Mark Brown416356f2009-06-30 19:05:15 +01001661};
Stephen Warrendeb26072011-04-05 19:35:30 -06001662EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001663
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001664/* ASoC platform driver */
1665static struct platform_driver soc_driver = {
1666 .driver = {
1667 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001668 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001669 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001670 },
1671 .probe = soc_probe,
1672 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001673};
1674
Mark Brown096e49d2009-07-05 15:12:22 +01001675/**
1676 * snd_soc_codec_volatile_register: Report if a register is volatile.
1677 *
1678 * @codec: CODEC to query.
1679 * @reg: Register to query.
1680 *
1681 * Boolean function indiciating if a CODEC register is volatile.
1682 */
Mark Brown181e0552011-01-24 14:05:25 +00001683int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1684 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001685{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001686 if (codec->volatile_register)
1687 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001688 else
1689 return 0;
1690}
1691EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1692
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001693/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001694 * snd_soc_codec_readable_register: Report if a register is readable.
1695 *
1696 * @codec: CODEC to query.
1697 * @reg: Register to query.
1698 *
1699 * Boolean function indicating if a CODEC register is readable.
1700 */
1701int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1702 unsigned int reg)
1703{
1704 if (codec->readable_register)
1705 return codec->readable_register(codec, reg);
1706 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001707 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001708}
1709EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1710
1711/**
1712 * snd_soc_codec_writable_register: Report if a register is writable.
1713 *
1714 * @codec: CODEC to query.
1715 * @reg: Register to query.
1716 *
1717 * Boolean function indicating if a CODEC register is writable.
1718 */
1719int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1720 unsigned int reg)
1721{
1722 if (codec->writable_register)
1723 return codec->writable_register(codec, reg);
1724 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001725 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001726}
1727EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1728
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001729int snd_soc_platform_read(struct snd_soc_platform *platform,
1730 unsigned int reg)
1731{
1732 unsigned int ret;
1733
1734 if (!platform->driver->read) {
1735 dev_err(platform->dev, "platform has no read back\n");
1736 return -1;
1737 }
1738
1739 ret = platform->driver->read(platform, reg);
1740 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001741 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001742
1743 return ret;
1744}
1745EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1746
1747int snd_soc_platform_write(struct snd_soc_platform *platform,
1748 unsigned int reg, unsigned int val)
1749{
1750 if (!platform->driver->write) {
1751 dev_err(platform->dev, "platform has no write back\n");
1752 return -1;
1753 }
1754
1755 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001756 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001757 return platform->driver->write(platform, reg, val);
1758}
1759EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1760
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001761/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001762 * snd_soc_new_ac97_codec - initailise AC97 device
1763 * @codec: audio codec
1764 * @ops: AC97 bus operations
1765 * @num: AC97 codec number
1766 *
1767 * Initialises AC97 codec resources for use by ad-hoc devices only.
1768 */
1769int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1770 struct snd_ac97_bus_ops *ops, int num)
1771{
1772 mutex_lock(&codec->mutex);
1773
1774 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1775 if (codec->ac97 == NULL) {
1776 mutex_unlock(&codec->mutex);
1777 return -ENOMEM;
1778 }
1779
1780 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1781 if (codec->ac97->bus == NULL) {
1782 kfree(codec->ac97);
1783 codec->ac97 = NULL;
1784 mutex_unlock(&codec->mutex);
1785 return -ENOMEM;
1786 }
1787
1788 codec->ac97->bus->ops = ops;
1789 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001790
1791 /*
1792 * Mark the AC97 device to be created by us. This way we ensure that the
1793 * device will be registered with the device subsystem later on.
1794 */
1795 codec->ac97_created = 1;
1796
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001797 mutex_unlock(&codec->mutex);
1798 return 0;
1799}
1800EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1801
1802/**
1803 * snd_soc_free_ac97_codec - free AC97 codec device
1804 * @codec: audio codec
1805 *
1806 * Frees AC97 codec device resources.
1807 */
1808void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1809{
1810 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001811#ifdef CONFIG_SND_SOC_AC97_BUS
1812 soc_unregister_ac97_dai_link(codec);
1813#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001814 kfree(codec->ac97->bus);
1815 kfree(codec->ac97);
1816 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001817 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001818 mutex_unlock(&codec->mutex);
1819}
1820EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1821
Mark Brownc3753702010-11-01 15:41:57 -04001822unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1823{
1824 unsigned int ret;
1825
Mark Brownc3acec22010-12-02 16:15:29 +00001826 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04001827 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04001828 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04001829
1830 return ret;
1831}
1832EXPORT_SYMBOL_GPL(snd_soc_read);
1833
1834unsigned int snd_soc_write(struct snd_soc_codec *codec,
1835 unsigned int reg, unsigned int val)
1836{
1837 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04001838 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00001839 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04001840}
1841EXPORT_SYMBOL_GPL(snd_soc_write);
1842
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00001843unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
1844 unsigned int reg, const void *data, size_t len)
1845{
1846 return codec->bulk_write_raw(codec, reg, data, len);
1847}
1848EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
1849
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001850/**
1851 * snd_soc_update_bits - update codec register bits
1852 * @codec: audio codec
1853 * @reg: codec register
1854 * @mask: register mask
1855 * @value: new value
1856 *
1857 * Writes new register value.
1858 *
Timur Tabi180c3292011-01-10 15:58:13 -06001859 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001860 */
1861int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001862 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001863{
1864 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001865 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06001866 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001867
Timur Tabi180c3292011-01-10 15:58:13 -06001868 ret = snd_soc_read(codec, reg);
1869 if (ret < 0)
1870 return ret;
1871
1872 old = ret;
Mark Brown78bf3c92011-06-03 17:09:14 +01001873 new = (old & ~mask) | (value & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001874 change = old != new;
Timur Tabi180c3292011-01-10 15:58:13 -06001875 if (change) {
1876 ret = snd_soc_write(codec, reg, new);
1877 if (ret < 0)
1878 return ret;
1879 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001880
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001881 return change;
1882}
1883EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1884
1885/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001886 * snd_soc_update_bits_locked - update codec register bits
1887 * @codec: audio codec
1888 * @reg: codec register
1889 * @mask: register mask
1890 * @value: new value
1891 *
1892 * Writes new register value, and takes the codec mutex.
1893 *
1894 * Returns 1 for change else 0.
1895 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001896int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1897 unsigned short reg, unsigned int mask,
1898 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001899{
1900 int change;
1901
1902 mutex_lock(&codec->mutex);
1903 change = snd_soc_update_bits(codec, reg, mask, value);
1904 mutex_unlock(&codec->mutex);
1905
1906 return change;
1907}
Mark Browndd1b3d52009-12-04 14:22:03 +00001908EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001909
1910/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001911 * snd_soc_test_bits - test register for change
1912 * @codec: audio codec
1913 * @reg: codec register
1914 * @mask: register mask
1915 * @value: new value
1916 *
1917 * Tests a register with a new value and checks if the new value is
1918 * different from the old value.
1919 *
1920 * Returns 1 for change else 0.
1921 */
1922int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001923 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001924{
1925 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001926 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001927
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001928 old = snd_soc_read(codec, reg);
1929 new = (old & ~mask) | value;
1930 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001931
1932 return change;
1933}
1934EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1935
1936/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001937 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1938 * @substream: the pcm substream
1939 * @hw: the hardware parameters
1940 *
1941 * Sets the substream runtime hardware parameters.
1942 */
1943int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1944 const struct snd_pcm_hardware *hw)
1945{
1946 struct snd_pcm_runtime *runtime = substream->runtime;
1947 runtime->hw.info = hw->info;
1948 runtime->hw.formats = hw->formats;
1949 runtime->hw.period_bytes_min = hw->period_bytes_min;
1950 runtime->hw.period_bytes_max = hw->period_bytes_max;
1951 runtime->hw.periods_min = hw->periods_min;
1952 runtime->hw.periods_max = hw->periods_max;
1953 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1954 runtime->hw.fifo_size = hw->fifo_size;
1955 return 0;
1956}
1957EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1958
1959/**
1960 * snd_soc_cnew - create new control
1961 * @_template: control template
1962 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001963 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00001964 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001965 *
1966 * Create a new mixer control from a template control.
1967 *
1968 * Returns 0 for success, else error.
1969 */
1970struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brownefb7ac32011-03-08 17:23:24 +00001971 void *data, char *long_name,
1972 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001973{
1974 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00001975 struct snd_kcontrol *kcontrol;
1976 char *name = NULL;
1977 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001978
1979 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001980 template.index = 0;
1981
Mark Brownefb7ac32011-03-08 17:23:24 +00001982 if (!long_name)
1983 long_name = template.name;
1984
1985 if (prefix) {
1986 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08001987 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00001988 if (!name)
1989 return NULL;
1990
1991 snprintf(name, name_len, "%s %s", prefix, long_name);
1992
1993 template.name = name;
1994 } else {
1995 template.name = long_name;
1996 }
1997
1998 kcontrol = snd_ctl_new1(&template, data);
1999
2000 kfree(name);
2001
2002 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002003}
2004EXPORT_SYMBOL_GPL(snd_soc_cnew);
2005
2006/**
Ian Molton3e8e1952009-01-09 00:23:21 +00002007 * snd_soc_add_controls - add an array of controls to a codec.
2008 * Convienience function to add a list of controls. Many codecs were
2009 * duplicating this code.
2010 *
2011 * @codec: codec to add controls to
2012 * @controls: array of controls to add
2013 * @num_controls: number of elements in the array
2014 *
2015 * Return 0 for success, else error.
2016 */
2017int snd_soc_add_controls(struct snd_soc_codec *codec,
2018 const struct snd_kcontrol_new *controls, int num_controls)
2019{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002020 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002021 int err, i;
2022
2023 for (i = 0; i < num_controls; i++) {
2024 const struct snd_kcontrol_new *control = &controls[i];
Mark Brownefb7ac32011-03-08 17:23:24 +00002025 err = snd_ctl_add(card, snd_soc_cnew(control, codec,
2026 control->name,
2027 codec->name_prefix));
Ian Molton3e8e1952009-01-09 00:23:21 +00002028 if (err < 0) {
Mark Brown082100d2010-09-20 19:03:28 +01002029 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
Mark Brownefb7ac32011-03-08 17:23:24 +00002030 codec->name, control->name, err);
Ian Molton3e8e1952009-01-09 00:23:21 +00002031 return err;
2032 }
2033 }
2034
2035 return 0;
2036}
2037EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2038
2039/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002040 * snd_soc_add_platform_controls - add an array of controls to a platform.
2041 * Convienience function to add a list of controls.
2042 *
2043 * @platform: platform to add controls to
2044 * @controls: array of controls to add
2045 * @num_controls: number of elements in the array
2046 *
2047 * Return 0 for success, else error.
2048 */
2049int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2050 const struct snd_kcontrol_new *controls, int num_controls)
2051{
2052 struct snd_card *card = platform->card->snd_card;
2053 int err, i;
2054
2055 for (i = 0; i < num_controls; i++) {
2056 const struct snd_kcontrol_new *control = &controls[i];
2057 err = snd_ctl_add(card, snd_soc_cnew(control, platform,
2058 control->name, NULL));
2059 if (err < 0) {
2060 dev_err(platform->dev, "Failed to add %s %d\n",control->name, err);
2061 return err;
2062 }
2063 }
2064
2065 return 0;
2066}
2067EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2068
2069/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002070 * snd_soc_info_enum_double - enumerated double mixer info callback
2071 * @kcontrol: mixer control
2072 * @uinfo: control element information
2073 *
2074 * Callback to provide information about a double enumerated
2075 * mixer control.
2076 *
2077 * Returns 0 for success.
2078 */
2079int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2080 struct snd_ctl_elem_info *uinfo)
2081{
2082 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2083
2084 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2085 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002086 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002087
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002088 if (uinfo->value.enumerated.item > e->max - 1)
2089 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002090 strcpy(uinfo->value.enumerated.name,
2091 e->texts[uinfo->value.enumerated.item]);
2092 return 0;
2093}
2094EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2095
2096/**
2097 * snd_soc_get_enum_double - enumerated double mixer get callback
2098 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002099 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002100 *
2101 * Callback to get the value of a double enumerated mixer.
2102 *
2103 * Returns 0 for success.
2104 */
2105int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2106 struct snd_ctl_elem_value *ucontrol)
2107{
2108 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2109 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002110 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002111
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002112 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002113 ;
2114 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002115 ucontrol->value.enumerated.item[0]
2116 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002117 if (e->shift_l != e->shift_r)
2118 ucontrol->value.enumerated.item[1] =
2119 (val >> e->shift_r) & (bitmask - 1);
2120
2121 return 0;
2122}
2123EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2124
2125/**
2126 * snd_soc_put_enum_double - enumerated double mixer put callback
2127 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002128 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002129 *
2130 * Callback to set the value of a double enumerated mixer.
2131 *
2132 * Returns 0 for success.
2133 */
2134int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2135 struct snd_ctl_elem_value *ucontrol)
2136{
2137 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2138 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002139 unsigned int val;
2140 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002141
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002142 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002143 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002144 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002145 return -EINVAL;
2146 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2147 mask = (bitmask - 1) << e->shift_l;
2148 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002149 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002150 return -EINVAL;
2151 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2152 mask |= (bitmask - 1) << e->shift_r;
2153 }
2154
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002155 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002156}
2157EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2158
2159/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002160 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2161 * @kcontrol: mixer control
2162 * @ucontrol: control element information
2163 *
2164 * Callback to get the value of a double semi enumerated mixer.
2165 *
2166 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2167 * used for handling bitfield coded enumeration for example.
2168 *
2169 * Returns 0 for success.
2170 */
2171int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2172 struct snd_ctl_elem_value *ucontrol)
2173{
2174 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002175 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002176 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002177
2178 reg_val = snd_soc_read(codec, e->reg);
2179 val = (reg_val >> e->shift_l) & e->mask;
2180 for (mux = 0; mux < e->max; mux++) {
2181 if (val == e->values[mux])
2182 break;
2183 }
2184 ucontrol->value.enumerated.item[0] = mux;
2185 if (e->shift_l != e->shift_r) {
2186 val = (reg_val >> e->shift_r) & e->mask;
2187 for (mux = 0; mux < e->max; mux++) {
2188 if (val == e->values[mux])
2189 break;
2190 }
2191 ucontrol->value.enumerated.item[1] = mux;
2192 }
2193
2194 return 0;
2195}
2196EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2197
2198/**
2199 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2200 * @kcontrol: mixer control
2201 * @ucontrol: control element information
2202 *
2203 * Callback to set the value of a double semi enumerated mixer.
2204 *
2205 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2206 * used for handling bitfield coded enumeration for example.
2207 *
2208 * Returns 0 for success.
2209 */
2210int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2211 struct snd_ctl_elem_value *ucontrol)
2212{
2213 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002214 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002215 unsigned int val;
2216 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002217
2218 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2219 return -EINVAL;
2220 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2221 mask = e->mask << e->shift_l;
2222 if (e->shift_l != e->shift_r) {
2223 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2224 return -EINVAL;
2225 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2226 mask |= e->mask << e->shift_r;
2227 }
2228
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002229 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002230}
2231EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2232
2233/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002234 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2235 * @kcontrol: mixer control
2236 * @uinfo: control element information
2237 *
2238 * Callback to provide information about an external enumerated
2239 * single mixer.
2240 *
2241 * Returns 0 for success.
2242 */
2243int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2244 struct snd_ctl_elem_info *uinfo)
2245{
2246 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2247
2248 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2249 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002250 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002251
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002252 if (uinfo->value.enumerated.item > e->max - 1)
2253 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002254 strcpy(uinfo->value.enumerated.name,
2255 e->texts[uinfo->value.enumerated.item]);
2256 return 0;
2257}
2258EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2259
2260/**
2261 * snd_soc_info_volsw_ext - external single mixer info callback
2262 * @kcontrol: mixer control
2263 * @uinfo: control element information
2264 *
2265 * Callback to provide information about a single external mixer control.
2266 *
2267 * Returns 0 for success.
2268 */
2269int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2270 struct snd_ctl_elem_info *uinfo)
2271{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002272 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002273
Mark Brownfd5dfad2009-04-15 21:37:46 +01002274 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002275 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2276 else
2277 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2278
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002279 uinfo->count = 1;
2280 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002281 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002282 return 0;
2283}
2284EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2285
2286/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002287 * snd_soc_info_volsw - single mixer info callback
2288 * @kcontrol: mixer control
2289 * @uinfo: control element information
2290 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002291 * Callback to provide information about a single mixer control, or a double
2292 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002293 *
2294 * Returns 0 for success.
2295 */
2296int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2297 struct snd_ctl_elem_info *uinfo)
2298{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002299 struct soc_mixer_control *mc =
2300 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002301 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002302
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002303 if (!mc->platform_max)
2304 mc->platform_max = mc->max;
2305 platform_max = mc->platform_max;
2306
2307 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002308 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2309 else
2310 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2311
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002312 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002313 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002314 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002315 return 0;
2316}
2317EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2318
2319/**
2320 * snd_soc_get_volsw - single mixer get callback
2321 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002322 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002323 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002324 * Callback to get the value of a single mixer control, or a double mixer
2325 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002326 *
2327 * Returns 0 for success.
2328 */
2329int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2330 struct snd_ctl_elem_value *ucontrol)
2331{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002332 struct soc_mixer_control *mc =
2333 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002334 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002335 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002336 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002337 unsigned int shift = mc->shift;
2338 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002339 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002340 unsigned int mask = (1 << fls(max)) - 1;
2341 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002342
2343 ucontrol->value.integer.value[0] =
2344 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002345 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002346 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002347 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002348
2349 if (snd_soc_volsw_is_stereo(mc)) {
2350 if (reg == reg2)
2351 ucontrol->value.integer.value[1] =
2352 (snd_soc_read(codec, reg) >> rshift) & mask;
2353 else
2354 ucontrol->value.integer.value[1] =
2355 (snd_soc_read(codec, reg2) >> shift) & mask;
2356 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002357 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002358 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002359 }
2360
2361 return 0;
2362}
2363EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2364
2365/**
2366 * snd_soc_put_volsw - single mixer put callback
2367 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002368 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002369 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002370 * Callback to set the value of a single mixer control, or a double mixer
2371 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002372 *
2373 * Returns 0 for success.
2374 */
2375int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2376 struct snd_ctl_elem_value *ucontrol)
2377{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002378 struct soc_mixer_control *mc =
2379 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002380 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002381 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002382 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002383 unsigned int shift = mc->shift;
2384 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002385 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002386 unsigned int mask = (1 << fls(max)) - 1;
2387 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002388 int err;
2389 bool type_2r = 0;
2390 unsigned int val2 = 0;
2391 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002392
2393 val = (ucontrol->value.integer.value[0] & mask);
2394 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002395 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002396 val_mask = mask << shift;
2397 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002398 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002399 val2 = (ucontrol->value.integer.value[1] & mask);
2400 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002401 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002402 if (reg == reg2) {
2403 val_mask |= mask << rshift;
2404 val |= val2 << rshift;
2405 } else {
2406 val2 = val2 << shift;
2407 type_2r = 1;
2408 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002409 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002410 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002411 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002412 return err;
2413
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002414 if (type_2r)
2415 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2416
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002417 return err;
2418}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002419EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002420
Mark Browne13ac2e2008-05-28 17:58:05 +01002421/**
2422 * snd_soc_info_volsw_s8 - signed mixer info callback
2423 * @kcontrol: mixer control
2424 * @uinfo: control element information
2425 *
2426 * Callback to provide information about a signed mixer control.
2427 *
2428 * Returns 0 for success.
2429 */
2430int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2431 struct snd_ctl_elem_info *uinfo)
2432{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002433 struct soc_mixer_control *mc =
2434 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002435 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002436 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002437
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002438 if (!mc->platform_max)
2439 mc->platform_max = mc->max;
2440 platform_max = mc->platform_max;
2441
Mark Browne13ac2e2008-05-28 17:58:05 +01002442 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2443 uinfo->count = 2;
2444 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002445 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002446 return 0;
2447}
2448EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2449
2450/**
2451 * snd_soc_get_volsw_s8 - signed mixer get callback
2452 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002453 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002454 *
2455 * Callback to get the value of a signed mixer control.
2456 *
2457 * Returns 0 for success.
2458 */
2459int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2460 struct snd_ctl_elem_value *ucontrol)
2461{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002462 struct soc_mixer_control *mc =
2463 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002464 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002465 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002466 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002467 int val = snd_soc_read(codec, reg);
2468
2469 ucontrol->value.integer.value[0] =
2470 ((signed char)(val & 0xff))-min;
2471 ucontrol->value.integer.value[1] =
2472 ((signed char)((val >> 8) & 0xff))-min;
2473 return 0;
2474}
2475EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2476
2477/**
2478 * snd_soc_put_volsw_sgn - signed mixer put callback
2479 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002480 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002481 *
2482 * Callback to set the value of a signed mixer control.
2483 *
2484 * Returns 0 for success.
2485 */
2486int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2487 struct snd_ctl_elem_value *ucontrol)
2488{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002489 struct soc_mixer_control *mc =
2490 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002491 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002492 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002493 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002494 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002495
2496 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2497 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2498
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002499 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002500}
2501EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2502
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002503/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002504 * snd_soc_limit_volume - Set new limit to an existing volume control.
2505 *
2506 * @codec: where to look for the control
2507 * @name: Name of the control
2508 * @max: new maximum limit
2509 *
2510 * Return 0 for success, else error.
2511 */
2512int snd_soc_limit_volume(struct snd_soc_codec *codec,
2513 const char *name, int max)
2514{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002515 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002516 struct snd_kcontrol *kctl;
2517 struct soc_mixer_control *mc;
2518 int found = 0;
2519 int ret = -EINVAL;
2520
2521 /* Sanity check for name and max */
2522 if (unlikely(!name || max <= 0))
2523 return -EINVAL;
2524
2525 list_for_each_entry(kctl, &card->controls, list) {
2526 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2527 found = 1;
2528 break;
2529 }
2530 }
2531 if (found) {
2532 mc = (struct soc_mixer_control *)kctl->private_value;
2533 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002534 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002535 ret = 0;
2536 }
2537 }
2538 return ret;
2539}
2540EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2541
2542/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002543 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2544 * mixer info callback
2545 * @kcontrol: mixer control
2546 * @uinfo: control element information
2547 *
2548 * Returns 0 for success.
2549 */
2550int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2551 struct snd_ctl_elem_info *uinfo)
2552{
2553 struct soc_mixer_control *mc =
2554 (struct soc_mixer_control *)kcontrol->private_value;
2555 int max = mc->max;
2556 int min = mc->min;
2557
2558 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2559 uinfo->count = 2;
2560 uinfo->value.integer.min = 0;
2561 uinfo->value.integer.max = max-min;
2562
2563 return 0;
2564}
2565EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2566
2567/**
2568 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2569 * mixer get callback
2570 * @kcontrol: mixer control
2571 * @uinfo: control element information
2572 *
2573 * Returns 0 for success.
2574 */
2575int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2576 struct snd_ctl_elem_value *ucontrol)
2577{
2578 struct soc_mixer_control *mc =
2579 (struct soc_mixer_control *)kcontrol->private_value;
2580 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2581 unsigned int mask = (1<<mc->shift)-1;
2582 int min = mc->min;
2583 int val = snd_soc_read(codec, mc->reg) & mask;
2584 int valr = snd_soc_read(codec, mc->rreg) & mask;
2585
Stuart Longland20630c72010-06-18 12:56:10 +10002586 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2587 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002588 return 0;
2589}
2590EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2591
2592/**
2593 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2594 * mixer put callback
2595 * @kcontrol: mixer control
2596 * @uinfo: control element information
2597 *
2598 * Returns 0 for success.
2599 */
2600int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2601 struct snd_ctl_elem_value *ucontrol)
2602{
2603 struct soc_mixer_control *mc =
2604 (struct soc_mixer_control *)kcontrol->private_value;
2605 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2606 unsigned int mask = (1<<mc->shift)-1;
2607 int min = mc->min;
2608 int ret;
2609 unsigned int val, valr, oval, ovalr;
2610
2611 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2612 val &= mask;
2613 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2614 valr &= mask;
2615
2616 oval = snd_soc_read(codec, mc->reg) & mask;
2617 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2618
2619 ret = 0;
2620 if (oval != val) {
2621 ret = snd_soc_write(codec, mc->reg, val);
2622 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002623 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002624 }
2625 if (ovalr != valr) {
2626 ret = snd_soc_write(codec, mc->rreg, valr);
2627 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002628 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002629 }
2630
2631 return 0;
2632}
2633EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2634
2635/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002636 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2637 * @dai: DAI
2638 * @clk_id: DAI specific clock ID
2639 * @freq: new clock frequency in Hz
2640 * @dir: new clock direction - input/output.
2641 *
2642 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2643 */
2644int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2645 unsigned int freq, int dir)
2646{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002647 if (dai->driver && dai->driver->ops->set_sysclk)
2648 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002649 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002650 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00002651 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002652 else
2653 return -EINVAL;
2654}
2655EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2656
2657/**
Mark Brownec4ee522011-03-07 20:58:11 +00002658 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2659 * @codec: CODEC
2660 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01002661 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00002662 * @freq: new clock frequency in Hz
2663 * @dir: new clock direction - input/output.
2664 *
2665 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2666 */
2667int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01002668 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00002669{
2670 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002671 return codec->driver->set_sysclk(codec, clk_id, source,
2672 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002673 else
2674 return -EINVAL;
2675}
2676EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2677
2678/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002679 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2680 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002681 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002682 * @div: new clock divisor.
2683 *
2684 * Configures the clock dividers. This is used to derive the best DAI bit and
2685 * frame clocks from the system or master clock. It's best to set the DAI bit
2686 * and frame clocks as low as possible to save system power.
2687 */
2688int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2689 int div_id, int div)
2690{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002691 if (dai->driver && dai->driver->ops->set_clkdiv)
2692 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002693 else
2694 return -EINVAL;
2695}
2696EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2697
2698/**
2699 * snd_soc_dai_set_pll - configure DAI PLL.
2700 * @dai: DAI
2701 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002702 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002703 * @freq_in: PLL input clock frequency in Hz
2704 * @freq_out: requested PLL output clock frequency in Hz
2705 *
2706 * Configures and enables PLL to generate output clock based on input clock.
2707 */
Mark Brown85488032009-09-05 18:52:16 +01002708int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2709 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002710{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002711 if (dai->driver && dai->driver->ops->set_pll)
2712 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002713 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00002714 else if (dai->codec && dai->codec->driver->set_pll)
2715 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2716 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002717 else
2718 return -EINVAL;
2719}
2720EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2721
Mark Brownec4ee522011-03-07 20:58:11 +00002722/*
2723 * snd_soc_codec_set_pll - configure codec PLL.
2724 * @codec: CODEC
2725 * @pll_id: DAI specific PLL ID
2726 * @source: DAI specific source for the PLL
2727 * @freq_in: PLL input clock frequency in Hz
2728 * @freq_out: requested PLL output clock frequency in Hz
2729 *
2730 * Configures and enables PLL to generate output clock based on input clock.
2731 */
2732int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2733 unsigned int freq_in, unsigned int freq_out)
2734{
2735 if (codec->driver->set_pll)
2736 return codec->driver->set_pll(codec, pll_id, source,
2737 freq_in, freq_out);
2738 else
2739 return -EINVAL;
2740}
2741EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2742
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002743/**
2744 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2745 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002746 * @fmt: SND_SOC_DAIFMT_ format value.
2747 *
2748 * Configures the DAI hardware format and clocking.
2749 */
2750int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2751{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002752 if (dai->driver && dai->driver->ops->set_fmt)
2753 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002754 else
2755 return -EINVAL;
2756}
2757EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2758
2759/**
2760 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2761 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002762 * @tx_mask: bitmask representing active TX slots.
2763 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002764 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002765 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002766 *
2767 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2768 * specific.
2769 */
2770int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002771 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002772{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002773 if (dai->driver && dai->driver->ops->set_tdm_slot)
2774 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002775 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002776 else
2777 return -EINVAL;
2778}
2779EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2780
2781/**
Barry Song472df3c2009-09-12 01:16:29 +08002782 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2783 * @dai: DAI
2784 * @tx_num: how many TX channels
2785 * @tx_slot: pointer to an array which imply the TX slot number channel
2786 * 0~num-1 uses
2787 * @rx_num: how many RX channels
2788 * @rx_slot: pointer to an array which imply the RX slot number channel
2789 * 0~num-1 uses
2790 *
2791 * configure the relationship between channel number and TDM slot number.
2792 */
2793int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2794 unsigned int tx_num, unsigned int *tx_slot,
2795 unsigned int rx_num, unsigned int *rx_slot)
2796{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002797 if (dai->driver && dai->driver->ops->set_channel_map)
2798 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002799 rx_num, rx_slot);
2800 else
2801 return -EINVAL;
2802}
2803EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2804
2805/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002806 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2807 * @dai: DAI
2808 * @tristate: tristate enable
2809 *
2810 * Tristates the DAI so that others can use it.
2811 */
2812int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2813{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002814 if (dai->driver && dai->driver->ops->set_tristate)
2815 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002816 else
2817 return -EINVAL;
2818}
2819EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2820
2821/**
2822 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2823 * @dai: DAI
2824 * @mute: mute enable
2825 *
2826 * Mutes the DAI DAC.
2827 */
2828int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2829{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002830 if (dai->driver && dai->driver->ops->digital_mute)
2831 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002832 else
2833 return -EINVAL;
2834}
2835EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2836
Mark Brownc5af3a22008-11-28 13:29:45 +00002837/**
2838 * snd_soc_register_card - Register a card with the ASoC core
2839 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002840 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002841 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002842 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302843int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002844{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002845 int i;
2846
Mark Brownc5af3a22008-11-28 13:29:45 +00002847 if (!card->name || !card->dev)
2848 return -EINVAL;
2849
Stephen Warren5a504962011-12-21 10:40:59 -07002850 for (i = 0; i < card->num_links; i++) {
2851 struct snd_soc_dai_link *link = &card->dai_link[i];
2852
2853 /*
2854 * Codec must be specified by 1 of name or OF node,
2855 * not both or neither.
2856 */
2857 if (!!link->codec_name == !!link->codec_of_node) {
2858 dev_err(card->dev,
2859 "Neither/both codec name/of_node are set\n");
2860 return -EINVAL;
2861 }
2862
2863 /*
2864 * Platform may be specified by either name or OF node, but
2865 * can be left unspecified, and a dummy platform will be used.
2866 */
2867 if (link->platform_name && link->platform_of_node) {
2868 dev_err(card->dev,
2869 "Both platform name/of_node are set\n");
2870 return -EINVAL;
2871 }
2872
2873 /*
2874 * CPU DAI must be specified by 1 of name or OF node,
2875 * not both or neither.
2876 */
2877 if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
2878 dev_err(card->dev,
2879 "Neither/both cpu_dai name/of_node are set\n");
2880 return -EINVAL;
2881 }
2882 }
2883
Mark Browned77cc12011-05-03 18:25:34 +01002884 dev_set_drvdata(card->dev, card);
2885
Stephen Warren111c6412011-01-28 14:26:35 -07002886 snd_soc_initialize_card_lists(card);
2887
Vinod Koul150dd2f2011-01-13 22:48:32 +05302888 soc_init_card_debugfs(card);
2889
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002890 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
2891 (card->num_links + card->num_aux_devs),
2892 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002893 if (card->rtd == NULL)
2894 return -ENOMEM;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002895 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002896
2897 for (i = 0; i < card->num_links; i++)
2898 card->rtd[i].dai_link = &card->dai_link[i];
2899
Mark Brownc5af3a22008-11-28 13:29:45 +00002900 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01002901 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00002902 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002903 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002904
2905 mutex_lock(&client_mutex);
2906 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002907 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002908 mutex_unlock(&client_mutex);
2909
2910 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2911
2912 return 0;
2913}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302914EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002915
2916/**
2917 * snd_soc_unregister_card - Unregister a card with the ASoC core
2918 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002919 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002920 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002921 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302922int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002923{
Vinod Koulb0e26482011-01-13 22:48:02 +05302924 if (card->instantiated)
2925 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002926 mutex_lock(&client_mutex);
2927 list_del(&card->list);
2928 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002929 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2930
2931 return 0;
2932}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302933EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002934
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002935/*
2936 * Simplify DAI link configuration by removing ".-1" from device names
2937 * and sanitizing names.
2938 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002939static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002940{
2941 char *found, name[NAME_SIZE];
2942 int id1, id2;
2943
2944 if (dev_name(dev) == NULL)
2945 return NULL;
2946
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002947 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002948
2949 /* are we a "%s.%d" name (platform and SPI components) */
2950 found = strstr(name, dev->driver->name);
2951 if (found) {
2952 /* get ID */
2953 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2954
2955 /* discard ID from name if ID == -1 */
2956 if (*id == -1)
2957 found[strlen(dev->driver->name)] = '\0';
2958 }
2959
2960 } else {
2961 /* I2C component devices are named "bus-addr" */
2962 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2963 char tmp[NAME_SIZE];
2964
2965 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03002966 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002967
2968 /* sanitize component name for DAI link creation */
2969 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002970 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002971 } else
2972 *id = 0;
2973 }
2974
2975 return kstrdup(name, GFP_KERNEL);
2976}
2977
2978/*
2979 * Simplify DAI link naming for single devices with multiple DAIs by removing
2980 * any ".-1" and using the DAI name (instead of device name).
2981 */
2982static inline char *fmt_multiple_name(struct device *dev,
2983 struct snd_soc_dai_driver *dai_drv)
2984{
2985 if (dai_drv->name == NULL) {
2986 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2987 dev_name(dev));
2988 return NULL;
2989 }
2990
2991 return kstrdup(dai_drv->name, GFP_KERNEL);
2992}
2993
Mark Brown91151712008-11-30 23:31:24 +00002994/**
2995 * snd_soc_register_dai - Register a DAI with the ASoC core
2996 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002997 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002998 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002999int snd_soc_register_dai(struct device *dev,
3000 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003001{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003002 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003003
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003004 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003005
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003006 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3007 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003008 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003009
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003010 /* create DAI component name */
3011 dai->name = fmt_single_name(dev, &dai->id);
3012 if (dai->name == NULL) {
3013 kfree(dai);
3014 return -ENOMEM;
3015 }
3016
3017 dai->dev = dev;
3018 dai->driver = dai_drv;
3019 if (!dai->driver->ops)
3020 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003021
3022 mutex_lock(&client_mutex);
3023 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003024 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003025 mutex_unlock(&client_mutex);
3026
3027 pr_debug("Registered DAI '%s'\n", dai->name);
3028
3029 return 0;
3030}
3031EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3032
3033/**
3034 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3035 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003036 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003037 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003038void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003039{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003040 struct snd_soc_dai *dai;
3041
3042 list_for_each_entry(dai, &dai_list, list) {
3043 if (dev == dai->dev)
3044 goto found;
3045 }
3046 return;
3047
3048found:
Mark Brown91151712008-11-30 23:31:24 +00003049 mutex_lock(&client_mutex);
3050 list_del(&dai->list);
3051 mutex_unlock(&client_mutex);
3052
3053 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003054 kfree(dai->name);
3055 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003056}
3057EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3058
3059/**
3060 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3061 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003062 * @dai: Array of DAIs to register
3063 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003064 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003065int snd_soc_register_dais(struct device *dev,
3066 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003067{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003068 struct snd_soc_dai *dai;
3069 int i, ret = 0;
3070
Liam Girdwood720ffa42010-08-18 00:30:30 +01003071 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003072
3073 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003074
3075 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003076 if (dai == NULL) {
3077 ret = -ENOMEM;
3078 goto err;
3079 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003080
3081 /* create DAI component name */
3082 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3083 if (dai->name == NULL) {
3084 kfree(dai);
3085 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003086 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003087 }
3088
3089 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003090 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003091 if (dai->driver->id)
3092 dai->id = dai->driver->id;
3093 else
3094 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003095 if (!dai->driver->ops)
3096 dai->driver->ops = &null_dai_ops;
3097
3098 mutex_lock(&client_mutex);
3099 list_add(&dai->list, &dai_list);
3100 mutex_unlock(&client_mutex);
3101
3102 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003103 }
3104
Axel Lin1dcb4f32010-12-06 16:48:03 +08003105 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003106 snd_soc_instantiate_cards();
Axel Lin1dcb4f32010-12-06 16:48:03 +08003107 mutex_unlock(&client_mutex);
Mark Brown91151712008-11-30 23:31:24 +00003108 return 0;
3109
3110err:
3111 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003112 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003113
3114 return ret;
3115}
3116EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3117
3118/**
3119 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3120 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003121 * @dai: Array of DAIs to unregister
3122 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003123 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003124void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003125{
3126 int i;
3127
3128 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003129 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003130}
3131EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3132
Mark Brown12a48a8c2008-12-03 19:40:30 +00003133/**
3134 * snd_soc_register_platform - Register a platform with the ASoC core
3135 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003136 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003137 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003138int snd_soc_register_platform(struct device *dev,
3139 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003140{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003141 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003142
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003143 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3144
3145 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3146 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003147 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003148
3149 /* create platform component name */
3150 platform->name = fmt_single_name(dev, &platform->id);
3151 if (platform->name == NULL) {
3152 kfree(platform);
3153 return -ENOMEM;
3154 }
3155
3156 platform->dev = dev;
3157 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003158 platform->dapm.dev = dev;
3159 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003160 platform->dapm.stream_event = platform_drv->stream_event;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003161
3162 mutex_lock(&client_mutex);
3163 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003164 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003165 mutex_unlock(&client_mutex);
3166
3167 pr_debug("Registered platform '%s'\n", platform->name);
3168
3169 return 0;
3170}
3171EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3172
3173/**
3174 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3175 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003176 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003177 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003178void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003179{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003180 struct snd_soc_platform *platform;
3181
3182 list_for_each_entry(platform, &platform_list, list) {
3183 if (dev == platform->dev)
3184 goto found;
3185 }
3186 return;
3187
3188found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003189 mutex_lock(&client_mutex);
3190 list_del(&platform->list);
3191 mutex_unlock(&client_mutex);
3192
3193 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003194 kfree(platform->name);
3195 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003196}
3197EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3198
Mark Brown151ab222009-05-09 16:22:58 +01003199static u64 codec_format_map[] = {
3200 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3201 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3202 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3203 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3204 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3205 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3206 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3207 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3208 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3209 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3210 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3211 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3212 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3213 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3214 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3215 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3216};
3217
3218/* Fix up the DAI formats for endianness: codecs don't actually see
3219 * the endianness of the data but we're using the CPU format
3220 * definitions which do need to include endianness so we ensure that
3221 * codec DAIs always have both big and little endian variants set.
3222 */
3223static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3224{
3225 int i;
3226
3227 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3228 if (stream->formats & codec_format_map[i])
3229 stream->formats |= codec_format_map[i];
3230}
3231
Mark Brown0d0cf002008-12-10 14:32:45 +00003232/**
3233 * snd_soc_register_codec - Register a codec with the ASoC core
3234 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003235 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003236 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003237int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003238 const struct snd_soc_codec_driver *codec_drv,
3239 struct snd_soc_dai_driver *dai_drv,
3240 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003241{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003242 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003243 struct snd_soc_codec *codec;
3244 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003245
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003246 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003247
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003248 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3249 if (codec == NULL)
3250 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003251
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003252 /* create CODEC component name */
3253 codec->name = fmt_single_name(dev, &codec->id);
3254 if (codec->name == NULL) {
3255 kfree(codec);
3256 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003257 }
3258
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003259 if (codec_drv->compress_type)
3260 codec->compress_type = codec_drv->compress_type;
3261 else
3262 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3263
Mark Brownc3acec22010-12-02 16:15:29 +00003264 codec->write = codec_drv->write;
3265 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003266 codec->volatile_register = codec_drv->volatile_register;
3267 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003268 codec->writable_register = codec_drv->writable_register;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003269 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3270 codec->dapm.dev = dev;
3271 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003272 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003273 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003274 codec->dev = dev;
3275 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003276 codec->num_dai = num_dai;
3277 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003278
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003279 /* allocate CODEC register cache */
3280 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003281 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003282 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003283 /* it is necessary to make a copy of the default register cache
3284 * because in the case of using a compression type that requires
3285 * the default register cache to be marked as __devinitconst the
3286 * kernel might have freed the array by the time we initialize
3287 * the cache.
3288 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003289 if (codec_drv->reg_cache_default) {
3290 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3291 reg_size, GFP_KERNEL);
3292 if (!codec->reg_def_copy) {
3293 ret = -ENOMEM;
3294 goto fail;
3295 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003296 }
3297 }
3298
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003299 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3300 if (!codec->volatile_register)
3301 codec->volatile_register = snd_soc_default_volatile_register;
3302 if (!codec->readable_register)
3303 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003304 if (!codec->writable_register)
3305 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003306 }
3307
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003308 for (i = 0; i < num_dai; i++) {
3309 fixup_codec_formats(&dai_drv[i].playback);
3310 fixup_codec_formats(&dai_drv[i].capture);
3311 }
3312
Mark Brown26b01cc2010-08-18 20:20:55 +01003313 /* register any DAIs */
3314 if (num_dai) {
3315 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3316 if (ret < 0)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003317 goto fail;
Mark Brown26b01cc2010-08-18 20:20:55 +01003318 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003319
Mark Brown0d0cf002008-12-10 14:32:45 +00003320 mutex_lock(&client_mutex);
3321 list_add(&codec->list, &codec_list);
3322 snd_soc_instantiate_cards();
3323 mutex_unlock(&client_mutex);
3324
3325 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003326 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003327
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003328fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003329 kfree(codec->reg_def_copy);
3330 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003331 kfree(codec->name);
3332 kfree(codec);
3333 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003334}
3335EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3336
3337/**
3338 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3339 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003340 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003341 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003342void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003343{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003344 struct snd_soc_codec *codec;
3345 int i;
3346
3347 list_for_each_entry(codec, &codec_list, list) {
3348 if (dev == codec->dev)
3349 goto found;
3350 }
3351 return;
3352
3353found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003354 if (codec->num_dai)
3355 for (i = 0; i < codec->num_dai; i++)
3356 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003357
Mark Brown0d0cf002008-12-10 14:32:45 +00003358 mutex_lock(&client_mutex);
3359 list_del(&codec->list);
3360 mutex_unlock(&client_mutex);
3361
3362 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003363
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003364 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003365 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003366 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003367 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003368}
3369EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3370
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003371/* Retrieve a card's name from device tree */
3372int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3373 const char *propname)
3374{
3375 struct device_node *np = card->dev->of_node;
3376 int ret;
3377
3378 ret = of_property_read_string_index(np, propname, 0, &card->name);
3379 /*
3380 * EINVAL means the property does not exist. This is fine providing
3381 * card->name was previously set, which is checked later in
3382 * snd_soc_register_card.
3383 */
3384 if (ret < 0 && ret != -EINVAL) {
3385 dev_err(card->dev,
3386 "Property '%s' could not be read: %d\n",
3387 propname, ret);
3388 return ret;
3389 }
3390
3391 return 0;
3392}
3393EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3394
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003395int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3396 const char *propname)
3397{
3398 struct device_node *np = card->dev->of_node;
3399 int num_routes;
3400 struct snd_soc_dapm_route *routes;
3401 int i, ret;
3402
3403 num_routes = of_property_count_strings(np, propname);
3404 if (num_routes & 1) {
3405 dev_err(card->dev,
3406 "Property '%s's length is not even\n",
3407 propname);
3408 return -EINVAL;
3409 }
3410 num_routes /= 2;
3411 if (!num_routes) {
3412 dev_err(card->dev,
3413 "Property '%s's length is zero\n",
3414 propname);
3415 return -EINVAL;
3416 }
3417
3418 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3419 GFP_KERNEL);
3420 if (!routes) {
3421 dev_err(card->dev,
3422 "Could not allocate DAPM route table\n");
3423 return -EINVAL;
3424 }
3425
3426 for (i = 0; i < num_routes; i++) {
3427 ret = of_property_read_string_index(np, propname,
3428 2 * i, &routes[i].sink);
3429 if (ret) {
3430 dev_err(card->dev,
3431 "Property '%s' index %d could not be read: %d\n",
3432 propname, 2 * i, ret);
3433 return -EINVAL;
3434 }
3435 ret = of_property_read_string_index(np, propname,
3436 (2 * i) + 1, &routes[i].source);
3437 if (ret) {
3438 dev_err(card->dev,
3439 "Property '%s' index %d could not be read: %d\n",
3440 propname, (2 * i) + 1, ret);
3441 return -EINVAL;
3442 }
3443 }
3444
3445 card->num_dapm_routes = num_routes;
3446 card->dapm_routes = routes;
3447
3448 return 0;
3449}
3450EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3451
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003452static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003453{
Mark Brown384c89e2008-12-03 17:34:03 +00003454#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003455 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3456 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Mark Brown384c89e2008-12-03 17:34:03 +00003457 printk(KERN_WARNING
3458 "ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00003459 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00003460 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003461
Mark Brown8a9dab12011-01-10 22:25:21 +00003462 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01003463 &codec_list_fops))
3464 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3465
Mark Brown8a9dab12011-01-10 22:25:21 +00003466 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01003467 &dai_list_fops))
3468 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003469
Mark Brown8a9dab12011-01-10 22:25:21 +00003470 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01003471 &platform_list_fops))
3472 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003473#endif
3474
Mark Brownfb257892011-04-28 10:57:54 +01003475 snd_soc_util_init();
3476
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003477 return platform_driver_register(&soc_driver);
3478}
Mark Brown4abe8e12010-10-12 17:41:03 +01003479module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003480
Mark Brown7d8c16a2008-11-30 22:11:24 +00003481static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003482{
Mark Brownfb257892011-04-28 10:57:54 +01003483 snd_soc_util_exit();
3484
Mark Brown384c89e2008-12-03 17:34:03 +00003485#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003486 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00003487#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003488 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003489}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003490module_exit(snd_soc_exit);
3491
3492/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003493MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003494MODULE_DESCRIPTION("ALSA SoC Core");
3495MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003496MODULE_ALIAS("platform:soc-audio");