blob: 8dc9aba3526d62cadd1fbfbe6c6ca03e52d15289 [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>
Marek Vasut474828a2009-07-22 13:01:03 +020035#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020036#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000037#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020038#include <sound/pcm.h>
39#include <sound/pcm_params.h>
40#include <sound/soc.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020041#include <sound/initval.h>
42
Mark Browna8b1d342010-11-03 18:05:58 -040043#define CREATE_TRACE_POINTS
44#include <trace/events/asoc.h>
45
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000046#define NAME_SIZE 32
47
Frank Mandarinodb2a4162006-10-06 18:31:09 +020048static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
49
Mark Brown384c89e2008-12-03 17:34:03 +000050#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000051struct dentry *snd_soc_debugfs_root;
52EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000053#endif
54
Mark Brownc5af3a22008-11-28 13:29:45 +000055static DEFINE_MUTEX(client_mutex);
56static LIST_HEAD(card_list);
Mark Brown91151712008-11-30 23:31:24 +000057static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000058static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000059static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000060
Liam Girdwoodddee6272011-06-09 14:45:53 +010061int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
Mark Brownc5af3a22008-11-28 13:29:45 +000062
Frank Mandarinodb2a4162006-10-06 18:31:09 +020063/*
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
67 */
68static int pmdown_time = 5000;
69module_param(pmdown_time, int, 0);
70MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
71
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000072/* returns the minimum number of bytes needed to represent
73 * a particular given value */
74static int min_bytes_needed(unsigned long val)
75{
76 int c = 0;
77 int i;
78
79 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
80 if (val & (1UL << i))
81 break;
82 c = (sizeof val * 8) - c;
83 if (!c || (c % 8))
84 c = (c + 8) / 8;
85 else
86 c /= 8;
87 return c;
88}
89
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000090/* fill buf which is 'len' bytes with a formatted
91 * string of the form 'reg: value\n' */
92static int format_register_str(struct snd_soc_codec *codec,
93 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +000094{
Stephen Warren00b317a2011-04-01 14:50:44 -060095 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
96 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000097 int ret;
98 char tmpbuf[len + 1];
99 char regbuf[regsize + 1];
100
101 /* since tmpbuf is allocated on the stack, warn the callers if they
102 * try to abuse this function */
103 WARN_ON(len > 63);
104
105 /* +2 for ': ' and + 1 for '\n' */
106 if (wordsize + regsize + 2 + 1 != len)
107 return -EINVAL;
108
Mark Brown25032c12011-08-01 13:52:48 +0900109 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000110 if (ret < 0) {
111 memset(regbuf, 'X', regsize);
112 regbuf[regsize] = '\0';
113 } else {
114 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
115 }
116
117 /* prepare the buffer */
118 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
119 /* copy it back to the caller without the '\0' */
120 memcpy(buf, tmpbuf, len);
121
122 return 0;
123}
124
125/* codec register dump */
126static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
127 size_t count, loff_t pos)
128{
129 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000130 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000131 int len;
132 size_t total = 0;
133 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000134
Stephen Warren00b317a2011-04-01 14:50:44 -0600135 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
136 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000137
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000138 len = wordsize + regsize + 2 + 1;
139
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000140 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000141 return 0;
142
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000143 if (codec->driver->reg_cache_step)
144 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000145
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000146 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausenb92d1502011-08-27 18:24:14 +0200147 if (!snd_soc_codec_readable_register(codec, i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000148 continue;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000149 if (codec->driver->display_register) {
150 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000151 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100152 } else {
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000153 /* only support larger than PAGE_SIZE bytes debugfs
154 * entries for the default case */
155 if (p >= pos) {
156 if (total + len >= count - 1)
157 break;
158 format_register_str(codec, i, buf + total, len);
159 total += len;
160 }
161 p += len;
Mark Brown5164d742010-07-14 16:14:33 +0100162 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000163 }
164
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000165 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000166
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000167 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000168}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000169
Mark Brown2624d5f2009-11-03 21:56:13 +0000170static ssize_t codec_reg_show(struct device *dev,
171 struct device_attribute *attr, char *buf)
172{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000173 struct snd_soc_pcm_runtime *rtd =
174 container_of(dev, struct snd_soc_pcm_runtime, dev);
175
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000176 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000177}
178
179static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
180
Mark Browndbe21402010-02-12 11:37:24 +0000181static ssize_t pmdown_time_show(struct device *dev,
182 struct device_attribute *attr, char *buf)
183{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000184 struct snd_soc_pcm_runtime *rtd =
185 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Browndbe21402010-02-12 11:37:24 +0000186
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000187 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000188}
189
190static ssize_t pmdown_time_set(struct device *dev,
191 struct device_attribute *attr,
192 const char *buf, size_t count)
193{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000194 struct snd_soc_pcm_runtime *rtd =
195 container_of(dev, struct snd_soc_pcm_runtime, dev);
Mark Brownc593b522010-10-27 20:11:17 -0700196 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000197
Mark Brownc593b522010-10-27 20:11:17 -0700198 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
199 if (ret)
200 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000201
202 return count;
203}
204
205static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
206
Mark Brown2624d5f2009-11-03 21:56:13 +0000207#ifdef CONFIG_DEBUG_FS
208static int codec_reg_open_file(struct inode *inode, struct file *file)
209{
210 file->private_data = inode->i_private;
211 return 0;
212}
213
214static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000215 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000216{
217 ssize_t ret;
218 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000219 char *buf;
220
221 if (*ppos < 0 || !count)
222 return -EINVAL;
223
224 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000225 if (!buf)
226 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000227
228 ret = soc_codec_reg_show(codec, buf, count, *ppos);
229 if (ret >= 0) {
230 if (copy_to_user(user_buf, buf, ret)) {
231 kfree(buf);
232 return -EFAULT;
233 }
234 *ppos += ret;
235 }
236
Mark Brown2624d5f2009-11-03 21:56:13 +0000237 kfree(buf);
238 return ret;
239}
240
241static ssize_t codec_reg_write_file(struct file *file,
242 const char __user *user_buf, size_t count, loff_t *ppos)
243{
244 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700245 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000246 char *start = buf;
247 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000248 struct snd_soc_codec *codec = file->private_data;
249
250 buf_size = min(count, (sizeof(buf)-1));
251 if (copy_from_user(buf, user_buf, buf_size))
252 return -EFAULT;
253 buf[buf_size] = 0;
254
Mark Brown2624d5f2009-11-03 21:56:13 +0000255 while (*start == ' ')
256 start++;
257 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000258 while (*start == ' ')
259 start++;
260 if (strict_strtoul(start, 16, &value))
261 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000262
263 /* Userspace has been fiddling around behind the kernel's back */
264 add_taint(TAINT_USER);
265
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000266 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000267 return buf_size;
268}
269
270static const struct file_operations codec_reg_fops = {
271 .open = codec_reg_open_file,
272 .read = codec_reg_read_file,
273 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200274 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000275};
276
277static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
278{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200279 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
280
281 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
282 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000283 if (!codec->debugfs_codec_root) {
284 printk(KERN_WARNING
285 "ASoC: Failed to create codec debugfs directory\n");
286 return;
287 }
288
Mark Brownaaee8ef2011-01-26 20:53:50 +0000289 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
290 &codec->cache_sync);
291 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
292 &codec->cache_only);
293
Mark Brown2624d5f2009-11-03 21:56:13 +0000294 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
295 codec->debugfs_codec_root,
296 codec, &codec_reg_fops);
297 if (!codec->debugfs_reg)
298 printk(KERN_WARNING
299 "ASoC: Failed to create codec register debugfs file\n");
300
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200301 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000302}
303
304static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
305{
306 debugfs_remove_recursive(codec->debugfs_codec_root);
307}
308
Mark Brownc3c5a192010-09-15 18:15:14 +0100309static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
310 size_t count, loff_t *ppos)
311{
312 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100313 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100314 struct snd_soc_codec *codec;
315
316 if (!buf)
317 return -ENOMEM;
318
Mark Brown2b194f9d2010-10-13 10:52:16 +0100319 list_for_each_entry(codec, &codec_list, list) {
320 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
321 codec->name);
322 if (len >= 0)
323 ret += len;
324 if (ret > PAGE_SIZE) {
325 ret = PAGE_SIZE;
326 break;
327 }
328 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100329
330 if (ret >= 0)
331 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
332
333 kfree(buf);
334
335 return ret;
336}
337
338static const struct file_operations codec_list_fops = {
339 .read = codec_list_read_file,
340 .llseek = default_llseek,/* read accesses f_pos */
341};
342
Mark Brownf3208782010-09-15 18:19:07 +0100343static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
344 size_t count, loff_t *ppos)
345{
346 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100347 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100348 struct snd_soc_dai *dai;
349
350 if (!buf)
351 return -ENOMEM;
352
Mark Brown2b194f9d2010-10-13 10:52:16 +0100353 list_for_each_entry(dai, &dai_list, list) {
354 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
355 if (len >= 0)
356 ret += len;
357 if (ret > PAGE_SIZE) {
358 ret = PAGE_SIZE;
359 break;
360 }
361 }
Mark Brownf3208782010-09-15 18:19:07 +0100362
Mark Brown2b194f9d2010-10-13 10:52:16 +0100363 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100364
365 kfree(buf);
366
367 return ret;
368}
369
370static const struct file_operations dai_list_fops = {
371 .read = dai_list_read_file,
372 .llseek = default_llseek,/* read accesses f_pos */
373};
374
Mark Brown19c7ac22010-09-15 18:22:40 +0100375static ssize_t platform_list_read_file(struct file *file,
376 char __user *user_buf,
377 size_t count, loff_t *ppos)
378{
379 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100380 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100381 struct snd_soc_platform *platform;
382
383 if (!buf)
384 return -ENOMEM;
385
Mark Brown2b194f9d2010-10-13 10:52:16 +0100386 list_for_each_entry(platform, &platform_list, list) {
387 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
388 platform->name);
389 if (len >= 0)
390 ret += len;
391 if (ret > PAGE_SIZE) {
392 ret = PAGE_SIZE;
393 break;
394 }
395 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100396
Mark Brown2b194f9d2010-10-13 10:52:16 +0100397 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100398
399 kfree(buf);
400
401 return ret;
402}
403
404static const struct file_operations platform_list_fops = {
405 .read = platform_list_read_file,
406 .llseek = default_llseek,/* read accesses f_pos */
407};
408
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200409static void soc_init_card_debugfs(struct snd_soc_card *card)
410{
411 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000412 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200413 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200414 dev_warn(card->dev,
415 "ASoC: Failed to create codec debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200416 return;
417 }
418
419 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
420 card->debugfs_card_root,
421 &card->pop_time);
422 if (!card->debugfs_pop_time)
423 dev_warn(card->dev,
424 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200425}
426
427static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
428{
429 debugfs_remove_recursive(card->debugfs_card_root);
430}
431
Mark Brown2624d5f2009-11-03 21:56:13 +0000432#else
433
434static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
435{
436}
437
438static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
439{
440}
Axel Linb95fccb2010-11-09 17:06:44 +0800441
442static inline void soc_init_card_debugfs(struct snd_soc_card *card)
443{
444}
445
446static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
447{
448}
Mark Brown2624d5f2009-11-03 21:56:13 +0000449#endif
450
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200451#ifdef CONFIG_SND_SOC_AC97_BUS
452/* unregister ac97 codec */
453static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
454{
455 if (codec->ac97->dev.bus)
456 device_unregister(&codec->ac97->dev);
457 return 0;
458}
459
460/* stop no dev release warning */
461static void soc_ac97_device_release(struct device *dev){}
462
463/* register ac97 codec to bus */
464static int soc_ac97_dev_register(struct snd_soc_codec *codec)
465{
466 int err;
467
468 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100469 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200470 codec->ac97->dev.release = soc_ac97_device_release;
471
Kay Sieversbb072bf2008-11-02 03:50:35 +0100472 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000473 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200474 err = device_register(&codec->ac97->dev);
475 if (err < 0) {
476 snd_printk(KERN_ERR "Can't register ac97 bus\n");
477 codec->ac97->dev.bus = NULL;
478 return err;
479 }
480 return 0;
481}
482#endif
483
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000484#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200485/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000486int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200487{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000488 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200489 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200490 int i;
491
Daniel Macke3509ff2009-06-03 17:44:49 +0200492 /* If the initialization of this soc device failed, there is no codec
493 * associated with it. Just bail out in this case.
494 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000495 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200496 return 0;
497
Andy Green6ed25972008-06-13 16:24:05 +0100498 /* Due to the resume being scheduled into a workqueue we could
499 * suspend before that's finished - wait for it to complete.
500 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000501 snd_power_lock(card->snd_card);
502 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
503 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100504
505 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000506 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100507
Mark Browna00f90f2010-12-02 16:24:24 +0000508 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000509 for (i = 0; i < card->num_rtd; i++) {
510 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
511 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100512
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000513 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100514 continue;
515
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000516 if (drv->ops->digital_mute && dai->playback_active)
517 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200518 }
519
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100520 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000521 for (i = 0; i < card->num_rtd; i++) {
522 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100523 continue;
524
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000525 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100526 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100527
Mark Brown87506542008-11-18 20:50:34 +0000528 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000529 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200530
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000531 for (i = 0; i < card->num_rtd; i++) {
532 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
533 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100534
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000535 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100536 continue;
537
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000538 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
539 cpu_dai->driver->suspend(cpu_dai);
540 if (platform->driver->suspend && !platform->suspended) {
541 platform->driver->suspend(cpu_dai);
542 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100543 }
544 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200545
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000546 /* close any waiting streams and save state */
547 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +0100548 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200549 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000550 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100551
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000552 for (i = 0; i < card->num_rtd; i++) {
553 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
554
555 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100556 continue;
557
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 if (driver->playback.stream_name != NULL)
559 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
560 SND_SOC_DAPM_STREAM_SUSPEND);
561
562 if (driver->capture.stream_name != NULL)
563 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
564 SND_SOC_DAPM_STREAM_SUSPEND);
565 }
566
567 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200568 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569 /* If there are paths active then the CODEC will be held with
570 * bias _ON and should not be suspended. */
571 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200572 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000573 case SND_SOC_BIAS_STANDBY:
574 case SND_SOC_BIAS_OFF:
575 codec->driver->suspend(codec, PMSG_SUSPEND);
576 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900577 codec->cache_sync = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000578 break;
579 default:
580 dev_dbg(codec->dev, "CODEC is on over suspend\n");
581 break;
582 }
583 }
584 }
585
586 for (i = 0; i < card->num_rtd; i++) {
587 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
588
589 if (card->rtd[i].dai_link->ignore_suspend)
590 continue;
591
592 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
593 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200594 }
595
Mark Brown87506542008-11-18 20:50:34 +0000596 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000597 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200598
599 return 0;
600}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000601EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200602
Andy Green6ed25972008-06-13 16:24:05 +0100603/* deferred resume work, so resume can complete before we finished
604 * setting our codec back up, which can be very slow on I2C
605 */
606static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200607{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608 struct snd_soc_card *card =
609 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200610 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200611 int i;
612
Andy Green6ed25972008-06-13 16:24:05 +0100613 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
614 * so userspace apps are blocked from touching us
615 */
616
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000617 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100618
Mark Brown99497882010-05-07 20:24:05 +0100619 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000620 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100621
Mark Brown87506542008-11-18 20:50:34 +0000622 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000623 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200624
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000625 /* resume AC97 DAIs */
626 for (i = 0; i < card->num_rtd; i++) {
627 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100628
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000629 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100630 continue;
631
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000632 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
633 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200634 }
635
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200636 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000637 /* If the CODEC was idle over suspend then it will have been
638 * left with bias OFF or STANDBY and suspended so we must now
639 * resume. Otherwise the suspend was suppressed.
640 */
641 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200642 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000643 case SND_SOC_BIAS_STANDBY:
644 case SND_SOC_BIAS_OFF:
645 codec->driver->resume(codec);
646 codec->suspended = 0;
647 break;
648 default:
649 dev_dbg(codec->dev, "CODEC was on over suspend\n");
650 break;
651 }
Mark Brown1547aba2010-05-07 21:11:40 +0100652 }
653 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200654
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000655 for (i = 0; i < card->num_rtd; i++) {
656 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100657
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000658 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100659 continue;
660
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000661 if (driver->playback.stream_name != NULL)
662 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200663 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000664
665 if (driver->capture.stream_name != NULL)
666 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200667 SND_SOC_DAPM_STREAM_RESUME);
668 }
669
Mark Brown3ff3f642008-05-19 12:32:25 +0200670 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000671 for (i = 0; i < card->num_rtd; i++) {
672 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
673 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100674
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000675 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100676 continue;
677
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000678 if (drv->ops->digital_mute && dai->playback_active)
679 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200680 }
681
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000682 for (i = 0; i < card->num_rtd; i++) {
683 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
684 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100685
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000686 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100687 continue;
688
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000689 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
690 cpu_dai->driver->resume(cpu_dai);
691 if (platform->driver->resume && platform->suspended) {
692 platform->driver->resume(cpu_dai);
693 platform->suspended = 0;
694 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200695 }
696
Mark Brown87506542008-11-18 20:50:34 +0000697 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000698 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200699
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000700 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100701
702 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000703 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100704}
705
706/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000707int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100708{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000709 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600710 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200711
Mark Brown64ab9ba2009-03-31 11:27:03 +0100712 /* AC97 devices might have other drivers hanging off them so
713 * need to resume immediately. Other drivers don't have that
714 * problem and may take a substantial amount of time to resume
715 * due to I/O costs and anti-pop so handle them out of line.
716 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000717 for (i = 0; i < card->num_rtd; i++) {
718 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600719 ac97_control |= cpu_dai->driver->ac97_control;
720 }
721 if (ac97_control) {
722 dev_dbg(dev, "Resuming AC97 immediately\n");
723 soc_resume_deferred(&card->deferred_resume_work);
724 } else {
725 dev_dbg(dev, "Scheduling resume work\n");
726 if (!schedule_work(&card->deferred_resume_work))
727 dev_err(dev, "resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100728 }
Andy Green6ed25972008-06-13 16:24:05 +0100729
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200730 return 0;
731}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000732EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200733#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000734#define snd_soc_suspend NULL
735#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200736#endif
737
Barry Song02a06d32009-10-16 18:13:38 +0800738static struct snd_soc_dai_ops null_dai_ops = {
739};
740
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000741static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200742{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000743 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
744 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000745 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000746 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000747 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100748 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200749
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000750 if (rtd->complete)
751 return 1;
752 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000753
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000754 /* do we already have the CPU DAI for this link ? */
755 if (rtd->cpu_dai) {
756 goto find_codec;
757 }
758 /* no, then find CPU DAI from registered DAIs*/
759 list_for_each_entry(cpu_dai, &dai_list, list) {
760 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000761 rtd->cpu_dai = cpu_dai;
762 goto find_codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000763 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000764 }
765 dev_dbg(card->dev, "CPU DAI %s not registered\n",
766 dai_link->cpu_dai_name);
767
768find_codec:
769 /* do we already have the CODEC for this link ? */
770 if (rtd->codec) {
771 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +0000772 }
773
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000774 /* no, then find CODEC from registered CODECs*/
775 list_for_each_entry(codec, &codec_list, list) {
776 if (!strcmp(codec->name, dai_link->codec_name)) {
777 rtd->codec = codec;
Mark Brown6b05eda2008-12-08 19:26:48 +0000778
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000779 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
780 list_for_each_entry(codec_dai, &dai_list, list) {
781 if (codec->dev == codec_dai->dev &&
782 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
783 rtd->codec_dai = codec_dai;
784 goto find_platform;
Mark Brown6b05eda2008-12-08 19:26:48 +0000785 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000786 }
787 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
788 dai_link->codec_dai_name);
789
790 goto find_platform;
791 }
792 }
793 dev_dbg(card->dev, "CODEC %s not registered\n",
794 dai_link->codec_name);
795
796find_platform:
Mark Brown848dd8b2011-04-27 18:16:32 +0100797 /* do we need a platform? */
798 if (rtd->platform)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000799 goto out;
Mark Brown848dd8b2011-04-27 18:16:32 +0100800
801 /* if there's no platform we match on the empty platform */
802 platform_name = dai_link->platform_name;
803 if (!platform_name)
804 platform_name = "snd-soc-dummy";
805
806 /* no, then find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000807 list_for_each_entry(platform, &platform_list, list) {
Mark Brown848dd8b2011-04-27 18:16:32 +0100808 if (!strcmp(platform->name, platform_name)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000809 rtd->platform = platform;
810 goto out;
811 }
812 }
813
814 dev_dbg(card->dev, "platform %s not registered\n",
815 dai_link->platform_name);
816 return 0;
817
818out:
819 /* mark rtd as complete if we found all 4 of our client devices */
820 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
821 rtd->complete = 1;
822 card->num_rtd++;
823 }
824 return 1;
825}
826
Jarkko Nikula589c3562010-12-06 16:27:07 +0200827static void soc_remove_codec(struct snd_soc_codec *codec)
828{
829 int err;
830
831 if (codec->driver->remove) {
832 err = codec->driver->remove(codec);
833 if (err < 0)
834 dev_err(codec->dev,
835 "asoc: failed to remove %s: %d\n",
836 codec->name, err);
837 }
838
839 /* Make sure all DAPM widgets are freed */
840 snd_soc_dapm_free(&codec->dapm);
841
842 soc_cleanup_codec_debugfs(codec);
843 codec->probed = 0;
844 list_del(&codec->card_list);
845 module_put(codec->dev->driver->owner);
846}
847
Liam Girdwood0168bf02011-06-07 16:08:05 +0100848static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000849{
850 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
851 struct snd_soc_codec *codec = rtd->codec;
852 struct snd_soc_platform *platform = rtd->platform;
853 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
854 int err;
855
856 /* unregister the rtd device */
857 if (rtd->dev_registered) {
858 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200859 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000860 device_unregister(&rtd->dev);
861 rtd->dev_registered = 0;
862 }
863
864 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100865 if (codec_dai && codec_dai->probed &&
866 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000867 if (codec_dai->driver->remove) {
868 err = codec_dai->driver->remove(codec_dai);
869 if (err < 0)
870 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
871 }
872 codec_dai->probed = 0;
873 list_del(&codec_dai->card_list);
874 }
875
876 /* remove the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100877 if (platform && platform->probed &&
878 platform->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000879 if (platform->driver->remove) {
880 err = platform->driver->remove(platform);
881 if (err < 0)
882 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
883 }
884 platform->probed = 0;
885 list_del(&platform->card_list);
886 module_put(platform->dev->driver->owner);
887 }
888
889 /* remove the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100890 if (codec && codec->probed &&
891 codec->driver->remove_order == order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200892 soc_remove_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000893
894 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100895 if (cpu_dai && cpu_dai->probed &&
896 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000897 if (cpu_dai->driver->remove) {
898 err = cpu_dai->driver->remove(cpu_dai);
899 if (err < 0)
900 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
901 }
902 cpu_dai->probed = 0;
903 list_del(&cpu_dai->card_list);
904 module_put(cpu_dai->dev->driver->owner);
905 }
906}
907
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900908static void soc_remove_dai_links(struct snd_soc_card *card)
909{
Liam Girdwood0168bf02011-06-07 16:08:05 +0100910 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900911
Liam Girdwood0168bf02011-06-07 16:08:05 +0100912 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
913 order++) {
914 for (dai = 0; dai < card->num_rtd; dai++)
915 soc_remove_dai_link(card, dai, order);
916 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900917 card->num_rtd = 0;
918}
919
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200920static void soc_set_name_prefix(struct snd_soc_card *card,
921 struct snd_soc_codec *codec)
922{
923 int i;
924
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000925 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200926 return;
927
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000928 for (i = 0; i < card->num_configs; i++) {
929 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200930 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
931 codec->name_prefix = map->name_prefix;
932 break;
933 }
934 }
935}
936
Jarkko Nikula589c3562010-12-06 16:27:07 +0200937static int soc_probe_codec(struct snd_soc_card *card,
938 struct snd_soc_codec *codec)
939{
940 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +0000941 const struct snd_soc_codec_driver *driver = codec->driver;
Jarkko Nikula589c3562010-12-06 16:27:07 +0200942
943 codec->card = card;
944 codec->dapm.card = card;
945 soc_set_name_prefix(card, codec);
946
Jarkko Nikula70d29332011-01-27 16:24:22 +0200947 if (!try_module_get(codec->dev->driver->owner))
948 return -ENODEV;
949
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +0200950 soc_init_codec_debugfs(codec);
951
Lars-Peter Clausen77530152011-05-05 16:59:09 +0200952 if (driver->dapm_widgets)
953 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
954 driver->num_dapm_widgets);
955
Mark Brown33c5f962011-08-22 18:40:30 +0100956 codec->dapm.idle_bias_off = driver->idle_bias_off;
957
Mark Brown89b95ac02011-03-07 16:38:44 +0000958 if (driver->probe) {
959 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200960 if (ret < 0) {
961 dev_err(codec->dev,
962 "asoc: failed to probe CODEC %s: %d\n",
963 codec->name, ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +0200964 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +0200965 }
966 }
967
Mark Brownb7af1da2011-04-07 19:18:44 +0900968 if (driver->controls)
969 snd_soc_add_controls(codec, driver->controls,
970 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +0000971 if (driver->dapm_routes)
972 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
973 driver->num_dapm_routes);
974
Jarkko Nikula589c3562010-12-06 16:27:07 +0200975 /* mark codec as probed and add to card codec list */
976 codec->probed = 1;
977 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +0200978 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200979
Jarkko Nikula70d29332011-01-27 16:24:22 +0200980 return 0;
981
982err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +0200983 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +0200984 module_put(codec->dev->driver->owner);
985
Jarkko Nikula589c3562010-12-06 16:27:07 +0200986 return ret;
987}
988
Liam Girdwood956245e2011-07-01 16:54:08 +0100989static int soc_probe_platform(struct snd_soc_card *card,
990 struct snd_soc_platform *platform)
991{
992 int ret = 0;
993 const struct snd_soc_platform_driver *driver = platform->driver;
994
995 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +0100996 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +0100997
998 if (!try_module_get(platform->dev->driver->owner))
999 return -ENODEV;
1000
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001001 if (driver->dapm_widgets)
1002 snd_soc_dapm_new_controls(&platform->dapm,
1003 driver->dapm_widgets, driver->num_dapm_widgets);
1004
Liam Girdwood956245e2011-07-01 16:54:08 +01001005 if (driver->probe) {
1006 ret = driver->probe(platform);
1007 if (ret < 0) {
1008 dev_err(platform->dev,
1009 "asoc: failed to probe platform %s: %d\n",
1010 platform->name, ret);
1011 goto err_probe;
1012 }
1013 }
1014
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001015 if (driver->controls)
1016 snd_soc_add_platform_controls(platform, driver->controls,
1017 driver->num_controls);
1018 if (driver->dapm_routes)
1019 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1020 driver->num_dapm_routes);
1021
Liam Girdwood956245e2011-07-01 16:54:08 +01001022 /* mark platform as probed and add to card platform list */
1023 platform->probed = 1;
1024 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001025 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001026
1027 return 0;
1028
1029err_probe:
1030 module_put(platform->dev->driver->owner);
1031
1032 return ret;
1033}
1034
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001035static void rtd_release(struct device *dev) {}
1036
Jarkko Nikula589c3562010-12-06 16:27:07 +02001037static int soc_post_component_init(struct snd_soc_card *card,
1038 struct snd_soc_codec *codec,
1039 int num, int dailess)
1040{
1041 struct snd_soc_dai_link *dai_link = NULL;
1042 struct snd_soc_aux_dev *aux_dev = NULL;
1043 struct snd_soc_pcm_runtime *rtd;
1044 const char *temp, *name;
1045 int ret = 0;
1046
1047 if (!dailess) {
1048 dai_link = &card->dai_link[num];
1049 rtd = &card->rtd[num];
1050 name = dai_link->name;
1051 } else {
1052 aux_dev = &card->aux_dev[num];
1053 rtd = &card->rtd_aux[num];
1054 name = aux_dev->name;
1055 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001056 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001057
1058 /* machine controls, routes and widgets are not prefixed */
1059 temp = codec->name_prefix;
1060 codec->name_prefix = NULL;
1061
Mark Brown0b07ab92011-09-28 20:12:01 +01001062 /* Make sure all DAPM widgets are instantiated */
1063 snd_soc_dapm_new_widgets(&codec->dapm);
1064
Jarkko Nikula589c3562010-12-06 16:27:07 +02001065 /* do machine specific initialization */
1066 if (!dailess && dai_link->init)
1067 ret = dai_link->init(rtd);
1068 else if (dailess && aux_dev->init)
1069 ret = aux_dev->init(&codec->dapm);
1070 if (ret < 0) {
1071 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1072 return ret;
1073 }
1074 codec->name_prefix = temp;
1075
Jarkko Nikula589c3562010-12-06 16:27:07 +02001076 /* register the rtd device */
1077 rtd->codec = codec;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001078 rtd->dev.parent = card->dev;
1079 rtd->dev.release = rtd_release;
1080 rtd->dev.init_name = name;
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001081 mutex_init(&rtd->pcm_mutex);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001082 ret = device_register(&rtd->dev);
1083 if (ret < 0) {
1084 dev_err(card->dev,
1085 "asoc: failed to register runtime device: %d\n", ret);
1086 return ret;
1087 }
1088 rtd->dev_registered = 1;
1089
1090 /* add DAPM sysfs entries for this codec */
1091 ret = snd_soc_dapm_sys_add(&rtd->dev);
1092 if (ret < 0)
1093 dev_err(codec->dev,
1094 "asoc: failed to add codec dapm sysfs entries: %d\n",
1095 ret);
1096
1097 /* add codec sysfs entries */
1098 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1099 if (ret < 0)
1100 dev_err(codec->dev,
1101 "asoc: failed to add codec sysfs files: %d\n", ret);
1102
1103 return 0;
1104}
1105
Liam Girdwood0168bf02011-06-07 16:08:05 +01001106static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001107{
1108 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1109 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1110 struct snd_soc_codec *codec = rtd->codec;
1111 struct snd_soc_platform *platform = rtd->platform;
1112 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1113 int ret;
1114
Liam Girdwood0168bf02011-06-07 16:08:05 +01001115 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1116 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001117
1118 /* config components */
1119 codec_dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001120 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001121 codec_dai->card = card;
1122 cpu_dai->card = card;
1123
1124 /* set default power off timeout */
1125 rtd->pmdown_time = pmdown_time;
1126
1127 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001128 if (!cpu_dai->probed &&
1129 cpu_dai->driver->probe_order == order) {
Liam Girdwood61b61e32011-05-24 13:57:43 +01001130 if (!try_module_get(cpu_dai->dev->driver->owner))
1131 return -ENODEV;
1132
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001133 if (cpu_dai->driver->probe) {
1134 ret = cpu_dai->driver->probe(cpu_dai);
1135 if (ret < 0) {
1136 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1137 cpu_dai->name);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001138 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001139 return ret;
1140 }
1141 }
1142 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001143 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001144 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1145 }
1146
1147 /* probe the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001148 if (!codec->probed &&
1149 codec->driver->probe_order == order) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001150 ret = soc_probe_codec(card, codec);
1151 if (ret < 0)
1152 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001153 }
1154
1155 /* probe the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001156 if (!platform->probed &&
1157 platform->driver->probe_order == order) {
Liam Girdwood956245e2011-07-01 16:54:08 +01001158 ret = soc_probe_platform(card, platform);
1159 if (ret < 0)
1160 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001161 }
1162
1163 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001164 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001165 if (codec_dai->driver->probe) {
1166 ret = codec_dai->driver->probe(codec_dai);
1167 if (ret < 0) {
1168 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1169 codec_dai->name);
1170 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001171 }
1172 }
1173
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001174 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001175 codec_dai->probed = 1;
1176 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001177 }
1178
Liam Girdwood0168bf02011-06-07 16:08:05 +01001179 /* complete DAI probe during last probe */
1180 if (order != SND_SOC_COMP_ORDER_LAST)
1181 return 0;
1182
Jarkko Nikula589c3562010-12-06 16:27:07 +02001183 ret = soc_post_component_init(card, codec, num, 0);
1184 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001185 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001186
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001187 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1188 if (ret < 0)
1189 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1190
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001191 /* create the pcm */
1192 ret = soc_new_pcm(rtd, num);
1193 if (ret < 0) {
1194 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1195 return ret;
1196 }
1197
1198 /* add platform data for AC97 devices */
1199 if (rtd->codec_dai->driver->ac97_control)
1200 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1201
1202 return 0;
1203}
1204
1205#ifdef CONFIG_SND_SOC_AC97_BUS
1206static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1207{
1208 int ret;
1209
1210 /* Only instantiate AC97 if not already done by the adaptor
1211 * for the generic AC97 subsystem.
1212 */
1213 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001214 /*
1215 * It is possible that the AC97 device is already registered to
1216 * the device subsystem. This happens when the device is created
1217 * via snd_ac97_mixer(). Currently only SoC codec that does so
1218 * is the generic AC97 glue but others migh emerge.
1219 *
1220 * In those cases we don't try to register the device again.
1221 */
1222 if (!rtd->codec->ac97_created)
1223 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001224
1225 ret = soc_ac97_dev_register(rtd->codec);
1226 if (ret < 0) {
1227 printk(KERN_ERR "asoc: AC97 device register failed\n");
1228 return ret;
1229 }
1230
1231 rtd->codec->ac97_registered = 1;
1232 }
1233 return 0;
1234}
1235
1236static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1237{
1238 if (codec->ac97_registered) {
1239 soc_ac97_dev_unregister(codec);
1240 codec->ac97_registered = 0;
1241 }
1242}
1243#endif
1244
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001245static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1246{
1247 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001248 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001249 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001250
1251 /* find CODEC from registered CODECs*/
1252 list_for_each_entry(codec, &codec_list, list) {
1253 if (!strcmp(codec->name, aux_dev->codec_name)) {
1254 if (codec->probed) {
1255 dev_err(codec->dev,
1256 "asoc: codec already probed");
1257 ret = -EBUSY;
1258 goto out;
1259 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001260 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001261 }
1262 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001263 /* codec not found */
1264 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1265 goto out;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001266
Jarkko Nikula676ad982010-12-03 09:18:22 +02001267found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001268 ret = soc_probe_codec(card, codec);
1269 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001270 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001271
Jarkko Nikula589c3562010-12-06 16:27:07 +02001272 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001273
1274out:
1275 return ret;
1276}
1277
1278static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1279{
1280 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1281 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001282
1283 /* unregister the rtd device */
1284 if (rtd->dev_registered) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001285 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001286 device_unregister(&rtd->dev);
1287 rtd->dev_registered = 0;
1288 }
1289
Jarkko Nikula589c3562010-12-06 16:27:07 +02001290 if (codec && codec->probed)
1291 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001292}
1293
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001294static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1295 enum snd_soc_compress_type compress_type)
1296{
1297 int ret;
1298
1299 if (codec->cache_init)
1300 return 0;
1301
1302 /* override the compress_type if necessary */
1303 if (compress_type && codec->compress_type != compress_type)
1304 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001305 ret = snd_soc_cache_init(codec);
1306 if (ret < 0) {
1307 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1308 ret);
1309 return ret;
1310 }
1311 codec->cache_init = 1;
1312 return 0;
1313}
1314
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001315static void snd_soc_instantiate_card(struct snd_soc_card *card)
1316{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001317 struct snd_soc_codec *codec;
1318 struct snd_soc_codec_conf *codec_conf;
1319 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001320 struct snd_soc_dai_link *dai_link;
Liam Girdwood0168bf02011-06-07 16:08:05 +01001321 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001322
1323 mutex_lock(&card->mutex);
1324
1325 if (card->instantiated) {
1326 mutex_unlock(&card->mutex);
1327 return;
1328 }
1329
1330 /* bind DAIs */
1331 for (i = 0; i < card->num_links; i++)
1332 soc_bind_dai_link(card, i);
1333
1334 /* bind completed ? */
1335 if (card->num_rtd != card->num_links) {
1336 mutex_unlock(&card->mutex);
1337 return;
1338 }
1339
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001340 /* initialize the register cache for each available codec */
1341 list_for_each_entry(codec, &codec_list, list) {
1342 if (codec->cache_init)
1343 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001344 /* by default we don't override the compress_type */
1345 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001346 /* check to see if we need to override the compress_type */
1347 for (i = 0; i < card->num_configs; ++i) {
1348 codec_conf = &card->codec_conf[i];
1349 if (!strcmp(codec->name, codec_conf->dev_name)) {
1350 compress_type = codec_conf->compress_type;
1351 if (compress_type && compress_type
1352 != codec->compress_type)
1353 break;
1354 }
1355 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001356 ret = snd_soc_init_codec_cache(codec, compress_type);
1357 if (ret < 0) {
1358 mutex_unlock(&card->mutex);
1359 return;
1360 }
1361 }
1362
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001363 /* card bind complete so register a sound card */
1364 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1365 card->owner, 0, &card->snd_card);
1366 if (ret < 0) {
1367 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1368 card->name);
1369 mutex_unlock(&card->mutex);
1370 return;
1371 }
1372 card->snd_card->dev = card->dev;
1373
Mark Browne37a4972011-03-02 18:21:57 +00001374 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1375 card->dapm.dev = card->dev;
1376 card->dapm.card = card;
1377 list_add(&card->dapm.list, &card->dapm_list);
1378
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001379#ifdef CONFIG_DEBUG_FS
1380 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1381#endif
1382
Mark Brown88ee1c62011-02-02 10:43:26 +00001383#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001384 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001385 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001386#endif
Andy Green6ed25972008-06-13 16:24:05 +01001387
Mark Brown9a841eb2011-04-12 17:51:37 -07001388 if (card->dapm_widgets)
1389 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1390 card->num_dapm_widgets);
1391
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001392 /* initialise the sound card only once */
1393 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001394 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001395 if (ret < 0)
1396 goto card_probe_error;
1397 }
1398
Liam Girdwood0168bf02011-06-07 16:08:05 +01001399 /* early DAI link probe */
1400 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1401 order++) {
1402 for (i = 0; i < card->num_links; i++) {
1403 ret = soc_probe_dai_link(card, i, order);
1404 if (ret < 0) {
1405 pr_err("asoc: failed to instantiate card %s: %d\n",
Mark Brown321de0d2010-09-21 15:09:37 +01001406 card->name, ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001407 goto probe_dai_err;
1408 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001409 }
1410 }
1411
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001412 for (i = 0; i < card->num_aux_devs; i++) {
1413 ret = soc_probe_aux_dev(card, i);
1414 if (ret < 0) {
1415 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1416 card->name, ret);
1417 goto probe_aux_dev_err;
1418 }
1419 }
1420
Mark Brownb7af1da2011-04-07 19:18:44 +09001421 /* We should have a non-codec control add function but we don't */
1422 if (card->controls)
1423 snd_soc_add_controls(list_first_entry(&card->codec_dev_list,
1424 struct snd_soc_codec,
1425 card_list),
1426 card->controls,
1427 card->num_controls);
1428
Mark Brownb8ad29d2011-03-02 18:35:51 +00001429 if (card->dapm_routes)
1430 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1431 card->num_dapm_routes);
1432
Mark Brown75d9ac42011-09-27 16:41:01 +01001433 for (i = 0; i < card->num_links; i++) {
1434 dai_link = &card->dai_link[i];
1435
1436 if (dai_link->dai_fmt) {
1437 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1438 dai_link->dai_fmt);
1439 if (ret != 0)
1440 dev_warn(card->rtd[i].codec_dai->dev,
1441 "Failed to set DAI format: %d\n",
1442 ret);
1443
1444 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1445 dai_link->dai_fmt);
1446 if (ret != 0)
1447 dev_warn(card->rtd[i].cpu_dai->dev,
1448 "Failed to set DAI format: %d\n",
1449 ret);
1450 }
1451 }
1452
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001453 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001454 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001455 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1456 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001457 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1458 "%s", card->driver_name ? card->driver_name : card->name);
1459 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1460 switch (card->snd_card->driver[i]) {
1461 case '_':
1462 case '-':
1463 case '\0':
1464 break;
1465 default:
1466 if (!isalnum(card->snd_card->driver[i]))
1467 card->snd_card->driver[i] = '_';
1468 break;
1469 }
1470 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001471
Mark Brown28e9ad92011-03-02 18:36:34 +00001472 if (card->late_probe) {
1473 ret = card->late_probe(card);
1474 if (ret < 0) {
1475 dev_err(card->dev, "%s late_probe() failed: %d\n",
1476 card->name, ret);
1477 goto probe_aux_dev_err;
1478 }
1479 }
1480
Mark Brown2dc00212011-10-08 13:59:44 +01001481 snd_soc_dapm_new_widgets(&card->dapm);
1482
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001483 ret = snd_card_register(card->snd_card);
1484 if (ret < 0) {
1485 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
Axel Lin6b3ed782010-12-07 16:12:29 +08001486 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001487 }
1488
1489#ifdef CONFIG_SND_SOC_AC97_BUS
1490 /* register any AC97 codecs */
1491 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001492 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1493 if (ret < 0) {
1494 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1495 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001496 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001497 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001498 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001499 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001500#endif
1501
Mark Brown435c5e22008-12-04 15:32:53 +00001502 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001503 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001504 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001505 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001506
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001507probe_aux_dev_err:
1508 for (i = 0; i < card->num_aux_devs; i++)
1509 soc_remove_aux_dev(card, i);
1510
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001511probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001512 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001513
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001514card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001515 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001516 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001517
1518 snd_card_free(card->snd_card);
1519
1520 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001521}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001522
Mark Brown435c5e22008-12-04 15:32:53 +00001523/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001524 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001525 * client_mutex.
1526 */
1527static void snd_soc_instantiate_cards(void)
1528{
1529 struct snd_soc_card *card;
1530 list_for_each_entry(card, &card_list, list)
1531 snd_soc_instantiate_card(card);
1532}
1533
1534/* probes a new socdev */
1535static int soc_probe(struct platform_device *pdev)
1536{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001537 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001538 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001539
Vinod Koul70a7ca32011-01-14 19:22:48 +05301540 /*
1541 * no card, so machine driver should be registering card
1542 * we should not be here in that case so ret error
1543 */
1544 if (!card)
1545 return -EINVAL;
1546
Mark Brown435c5e22008-12-04 15:32:53 +00001547 /* Bodge while we unpick instantiation */
1548 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001549
Mark Brown435c5e22008-12-04 15:32:53 +00001550 ret = snd_soc_register_card(card);
1551 if (ret != 0) {
1552 dev_err(&pdev->dev, "Failed to register card\n");
1553 return ret;
1554 }
1555
1556 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001557}
1558
Vinod Koulb0e26482011-01-13 22:48:02 +05301559static int soc_cleanup_card_resources(struct snd_soc_card *card)
1560{
Vinod Koulb0e26482011-01-13 22:48:02 +05301561 int i;
1562
1563 /* make sure any delayed work runs */
1564 for (i = 0; i < card->num_rtd; i++) {
1565 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1566 flush_delayed_work_sync(&rtd->delayed_work);
1567 }
1568
1569 /* remove auxiliary devices */
1570 for (i = 0; i < card->num_aux_devs; i++)
1571 soc_remove_aux_dev(card, i);
1572
1573 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001574 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301575
1576 soc_cleanup_card_debugfs(card);
1577
1578 /* remove the card */
1579 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001580 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301581
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001582 snd_soc_dapm_free(&card->dapm);
1583
Vinod Koulb0e26482011-01-13 22:48:02 +05301584 kfree(card->rtd);
1585 snd_card_free(card->snd_card);
1586 return 0;
1587
1588}
1589
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001590/* removes a socdev */
1591static int soc_remove(struct platform_device *pdev)
1592{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001593 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001594
Mark Brownc5af3a22008-11-28 13:29:45 +00001595 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001596 return 0;
1597}
1598
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001599int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001600{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001601 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001602 int i;
Mark Brown51737472009-06-22 13:16:51 +01001603
1604 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001605 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001606
1607 /* Flush out pmdown_time work - we actually do want to run it
1608 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001609 for (i = 0; i < card->num_rtd; i++) {
1610 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001611 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001612 }
Mark Brown51737472009-06-22 13:16:51 +01001613
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001614 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001615
1616 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001617}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001618EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001619
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001620const struct dev_pm_ops snd_soc_pm_ops = {
1621 .suspend = snd_soc_suspend,
1622 .resume = snd_soc_resume,
1623 .poweroff = snd_soc_poweroff,
Mark Brown416356f2009-06-30 19:05:15 +01001624};
Stephen Warrendeb26072011-04-05 19:35:30 -06001625EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001626
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001627/* ASoC platform driver */
1628static struct platform_driver soc_driver = {
1629 .driver = {
1630 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001631 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001632 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001633 },
1634 .probe = soc_probe,
1635 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001636};
1637
Mark Brown096e49d2009-07-05 15:12:22 +01001638/**
1639 * snd_soc_codec_volatile_register: Report if a register is volatile.
1640 *
1641 * @codec: CODEC to query.
1642 * @reg: Register to query.
1643 *
1644 * Boolean function indiciating if a CODEC register is volatile.
1645 */
Mark Brown181e0552011-01-24 14:05:25 +00001646int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1647 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001648{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001649 if (codec->volatile_register)
1650 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001651 else
1652 return 0;
1653}
1654EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1655
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001656/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001657 * snd_soc_codec_readable_register: Report if a register is readable.
1658 *
1659 * @codec: CODEC to query.
1660 * @reg: Register to query.
1661 *
1662 * Boolean function indicating if a CODEC register is readable.
1663 */
1664int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1665 unsigned int reg)
1666{
1667 if (codec->readable_register)
1668 return codec->readable_register(codec, reg);
1669 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001670 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001671}
1672EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1673
1674/**
1675 * snd_soc_codec_writable_register: Report if a register is writable.
1676 *
1677 * @codec: CODEC to query.
1678 * @reg: Register to query.
1679 *
1680 * Boolean function indicating if a CODEC register is writable.
1681 */
1682int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1683 unsigned int reg)
1684{
1685 if (codec->writable_register)
1686 return codec->writable_register(codec, reg);
1687 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001688 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001689}
1690EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1691
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001692int snd_soc_platform_read(struct snd_soc_platform *platform,
1693 unsigned int reg)
1694{
1695 unsigned int ret;
1696
1697 if (!platform->driver->read) {
1698 dev_err(platform->dev, "platform has no read back\n");
1699 return -1;
1700 }
1701
1702 ret = platform->driver->read(platform, reg);
1703 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001704 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001705
1706 return ret;
1707}
1708EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1709
1710int snd_soc_platform_write(struct snd_soc_platform *platform,
1711 unsigned int reg, unsigned int val)
1712{
1713 if (!platform->driver->write) {
1714 dev_err(platform->dev, "platform has no write back\n");
1715 return -1;
1716 }
1717
1718 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001719 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001720 return platform->driver->write(platform, reg, val);
1721}
1722EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1723
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001724/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001725 * snd_soc_new_ac97_codec - initailise AC97 device
1726 * @codec: audio codec
1727 * @ops: AC97 bus operations
1728 * @num: AC97 codec number
1729 *
1730 * Initialises AC97 codec resources for use by ad-hoc devices only.
1731 */
1732int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1733 struct snd_ac97_bus_ops *ops, int num)
1734{
1735 mutex_lock(&codec->mutex);
1736
1737 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1738 if (codec->ac97 == NULL) {
1739 mutex_unlock(&codec->mutex);
1740 return -ENOMEM;
1741 }
1742
1743 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1744 if (codec->ac97->bus == NULL) {
1745 kfree(codec->ac97);
1746 codec->ac97 = NULL;
1747 mutex_unlock(&codec->mutex);
1748 return -ENOMEM;
1749 }
1750
1751 codec->ac97->bus->ops = ops;
1752 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001753
1754 /*
1755 * Mark the AC97 device to be created by us. This way we ensure that the
1756 * device will be registered with the device subsystem later on.
1757 */
1758 codec->ac97_created = 1;
1759
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001760 mutex_unlock(&codec->mutex);
1761 return 0;
1762}
1763EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1764
1765/**
1766 * snd_soc_free_ac97_codec - free AC97 codec device
1767 * @codec: audio codec
1768 *
1769 * Frees AC97 codec device resources.
1770 */
1771void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1772{
1773 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001774#ifdef CONFIG_SND_SOC_AC97_BUS
1775 soc_unregister_ac97_dai_link(codec);
1776#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001777 kfree(codec->ac97->bus);
1778 kfree(codec->ac97);
1779 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001780 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001781 mutex_unlock(&codec->mutex);
1782}
1783EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1784
Mark Brownc3753702010-11-01 15:41:57 -04001785unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1786{
1787 unsigned int ret;
1788
Mark Brownc3acec22010-12-02 16:15:29 +00001789 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04001790 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04001791 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04001792
1793 return ret;
1794}
1795EXPORT_SYMBOL_GPL(snd_soc_read);
1796
1797unsigned int snd_soc_write(struct snd_soc_codec *codec,
1798 unsigned int reg, unsigned int val)
1799{
1800 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04001801 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00001802 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04001803}
1804EXPORT_SYMBOL_GPL(snd_soc_write);
1805
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00001806unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
1807 unsigned int reg, const void *data, size_t len)
1808{
1809 return codec->bulk_write_raw(codec, reg, data, len);
1810}
1811EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
1812
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001813/**
1814 * snd_soc_update_bits - update codec register bits
1815 * @codec: audio codec
1816 * @reg: codec register
1817 * @mask: register mask
1818 * @value: new value
1819 *
1820 * Writes new register value.
1821 *
Timur Tabi180c3292011-01-10 15:58:13 -06001822 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001823 */
1824int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001825 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001826{
1827 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001828 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06001829 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001830
Timur Tabi180c3292011-01-10 15:58:13 -06001831 ret = snd_soc_read(codec, reg);
1832 if (ret < 0)
1833 return ret;
1834
1835 old = ret;
Mark Brown78bf3c92011-06-03 17:09:14 +01001836 new = (old & ~mask) | (value & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001837 change = old != new;
Timur Tabi180c3292011-01-10 15:58:13 -06001838 if (change) {
1839 ret = snd_soc_write(codec, reg, new);
1840 if (ret < 0)
1841 return ret;
1842 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001843
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001844 return change;
1845}
1846EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1847
1848/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001849 * snd_soc_update_bits_locked - update codec register bits
1850 * @codec: audio codec
1851 * @reg: codec register
1852 * @mask: register mask
1853 * @value: new value
1854 *
1855 * Writes new register value, and takes the codec mutex.
1856 *
1857 * Returns 1 for change else 0.
1858 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001859int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1860 unsigned short reg, unsigned int mask,
1861 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001862{
1863 int change;
1864
1865 mutex_lock(&codec->mutex);
1866 change = snd_soc_update_bits(codec, reg, mask, value);
1867 mutex_unlock(&codec->mutex);
1868
1869 return change;
1870}
Mark Browndd1b3d52009-12-04 14:22:03 +00001871EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001872
1873/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001874 * snd_soc_test_bits - test register for change
1875 * @codec: audio codec
1876 * @reg: codec register
1877 * @mask: register mask
1878 * @value: new value
1879 *
1880 * Tests a register with a new value and checks if the new value is
1881 * different from the old value.
1882 *
1883 * Returns 1 for change else 0.
1884 */
1885int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001886 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001887{
1888 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001889 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001890
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001891 old = snd_soc_read(codec, reg);
1892 new = (old & ~mask) | value;
1893 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001894
1895 return change;
1896}
1897EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1898
1899/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001900 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1901 * @substream: the pcm substream
1902 * @hw: the hardware parameters
1903 *
1904 * Sets the substream runtime hardware parameters.
1905 */
1906int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1907 const struct snd_pcm_hardware *hw)
1908{
1909 struct snd_pcm_runtime *runtime = substream->runtime;
1910 runtime->hw.info = hw->info;
1911 runtime->hw.formats = hw->formats;
1912 runtime->hw.period_bytes_min = hw->period_bytes_min;
1913 runtime->hw.period_bytes_max = hw->period_bytes_max;
1914 runtime->hw.periods_min = hw->periods_min;
1915 runtime->hw.periods_max = hw->periods_max;
1916 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1917 runtime->hw.fifo_size = hw->fifo_size;
1918 return 0;
1919}
1920EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1921
1922/**
1923 * snd_soc_cnew - create new control
1924 * @_template: control template
1925 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00001926 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00001927 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001928 *
1929 * Create a new mixer control from a template control.
1930 *
1931 * Returns 0 for success, else error.
1932 */
1933struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brownefb7ac32011-03-08 17:23:24 +00001934 void *data, char *long_name,
1935 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001936{
1937 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00001938 struct snd_kcontrol *kcontrol;
1939 char *name = NULL;
1940 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001941
1942 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001943 template.index = 0;
1944
Mark Brownefb7ac32011-03-08 17:23:24 +00001945 if (!long_name)
1946 long_name = template.name;
1947
1948 if (prefix) {
1949 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08001950 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00001951 if (!name)
1952 return NULL;
1953
1954 snprintf(name, name_len, "%s %s", prefix, long_name);
1955
1956 template.name = name;
1957 } else {
1958 template.name = long_name;
1959 }
1960
1961 kcontrol = snd_ctl_new1(&template, data);
1962
1963 kfree(name);
1964
1965 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001966}
1967EXPORT_SYMBOL_GPL(snd_soc_cnew);
1968
1969/**
Ian Molton3e8e1952009-01-09 00:23:21 +00001970 * snd_soc_add_controls - add an array of controls to a codec.
1971 * Convienience function to add a list of controls. Many codecs were
1972 * duplicating this code.
1973 *
1974 * @codec: codec to add controls to
1975 * @controls: array of controls to add
1976 * @num_controls: number of elements in the array
1977 *
1978 * Return 0 for success, else error.
1979 */
1980int snd_soc_add_controls(struct snd_soc_codec *codec,
1981 const struct snd_kcontrol_new *controls, int num_controls)
1982{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001983 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00001984 int err, i;
1985
1986 for (i = 0; i < num_controls; i++) {
1987 const struct snd_kcontrol_new *control = &controls[i];
Mark Brownefb7ac32011-03-08 17:23:24 +00001988 err = snd_ctl_add(card, snd_soc_cnew(control, codec,
1989 control->name,
1990 codec->name_prefix));
Ian Molton3e8e1952009-01-09 00:23:21 +00001991 if (err < 0) {
Mark Brown082100d2010-09-20 19:03:28 +01001992 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
Mark Brownefb7ac32011-03-08 17:23:24 +00001993 codec->name, control->name, err);
Ian Molton3e8e1952009-01-09 00:23:21 +00001994 return err;
1995 }
1996 }
1997
1998 return 0;
1999}
2000EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2001
2002/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002003 * snd_soc_add_platform_controls - add an array of controls to a platform.
2004 * Convienience function to add a list of controls.
2005 *
2006 * @platform: platform to add controls to
2007 * @controls: array of controls to add
2008 * @num_controls: number of elements in the array
2009 *
2010 * Return 0 for success, else error.
2011 */
2012int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2013 const struct snd_kcontrol_new *controls, int num_controls)
2014{
2015 struct snd_card *card = platform->card->snd_card;
2016 int err, i;
2017
2018 for (i = 0; i < num_controls; i++) {
2019 const struct snd_kcontrol_new *control = &controls[i];
2020 err = snd_ctl_add(card, snd_soc_cnew(control, platform,
2021 control->name, NULL));
2022 if (err < 0) {
2023 dev_err(platform->dev, "Failed to add %s %d\n",control->name, err);
2024 return err;
2025 }
2026 }
2027
2028 return 0;
2029}
2030EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2031
2032/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002033 * snd_soc_info_enum_double - enumerated double mixer info callback
2034 * @kcontrol: mixer control
2035 * @uinfo: control element information
2036 *
2037 * Callback to provide information about a double enumerated
2038 * mixer control.
2039 *
2040 * Returns 0 for success.
2041 */
2042int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2043 struct snd_ctl_elem_info *uinfo)
2044{
2045 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2046
2047 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2048 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002049 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002050
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002051 if (uinfo->value.enumerated.item > e->max - 1)
2052 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002053 strcpy(uinfo->value.enumerated.name,
2054 e->texts[uinfo->value.enumerated.item]);
2055 return 0;
2056}
2057EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2058
2059/**
2060 * snd_soc_get_enum_double - enumerated double mixer get callback
2061 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002062 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002063 *
2064 * Callback to get the value of a double enumerated mixer.
2065 *
2066 * Returns 0 for success.
2067 */
2068int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2069 struct snd_ctl_elem_value *ucontrol)
2070{
2071 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2072 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002073 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002074
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002075 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002076 ;
2077 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002078 ucontrol->value.enumerated.item[0]
2079 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002080 if (e->shift_l != e->shift_r)
2081 ucontrol->value.enumerated.item[1] =
2082 (val >> e->shift_r) & (bitmask - 1);
2083
2084 return 0;
2085}
2086EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2087
2088/**
2089 * snd_soc_put_enum_double - enumerated double mixer put callback
2090 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002091 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002092 *
2093 * Callback to set the value of a double enumerated mixer.
2094 *
2095 * Returns 0 for success.
2096 */
2097int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2098 struct snd_ctl_elem_value *ucontrol)
2099{
2100 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2101 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002102 unsigned int val;
2103 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002104
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002105 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002106 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002107 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002108 return -EINVAL;
2109 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2110 mask = (bitmask - 1) << e->shift_l;
2111 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002112 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002113 return -EINVAL;
2114 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2115 mask |= (bitmask - 1) << e->shift_r;
2116 }
2117
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002118 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002119}
2120EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2121
2122/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002123 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2124 * @kcontrol: mixer control
2125 * @ucontrol: control element information
2126 *
2127 * Callback to get the value of a double semi enumerated mixer.
2128 *
2129 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2130 * used for handling bitfield coded enumeration for example.
2131 *
2132 * Returns 0 for success.
2133 */
2134int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2135 struct snd_ctl_elem_value *ucontrol)
2136{
2137 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002138 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002139 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002140
2141 reg_val = snd_soc_read(codec, e->reg);
2142 val = (reg_val >> e->shift_l) & e->mask;
2143 for (mux = 0; mux < e->max; mux++) {
2144 if (val == e->values[mux])
2145 break;
2146 }
2147 ucontrol->value.enumerated.item[0] = mux;
2148 if (e->shift_l != e->shift_r) {
2149 val = (reg_val >> e->shift_r) & e->mask;
2150 for (mux = 0; mux < e->max; mux++) {
2151 if (val == e->values[mux])
2152 break;
2153 }
2154 ucontrol->value.enumerated.item[1] = mux;
2155 }
2156
2157 return 0;
2158}
2159EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2160
2161/**
2162 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2163 * @kcontrol: mixer control
2164 * @ucontrol: control element information
2165 *
2166 * Callback to set the value of a double semi enumerated mixer.
2167 *
2168 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2169 * used for handling bitfield coded enumeration for example.
2170 *
2171 * Returns 0 for success.
2172 */
2173int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2174 struct snd_ctl_elem_value *ucontrol)
2175{
2176 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002177 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002178 unsigned int val;
2179 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002180
2181 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2182 return -EINVAL;
2183 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2184 mask = e->mask << e->shift_l;
2185 if (e->shift_l != e->shift_r) {
2186 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2187 return -EINVAL;
2188 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2189 mask |= e->mask << e->shift_r;
2190 }
2191
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002192 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002193}
2194EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2195
2196/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002197 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2198 * @kcontrol: mixer control
2199 * @uinfo: control element information
2200 *
2201 * Callback to provide information about an external enumerated
2202 * single mixer.
2203 *
2204 * Returns 0 for success.
2205 */
2206int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2207 struct snd_ctl_elem_info *uinfo)
2208{
2209 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2210
2211 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2212 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002213 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002214
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002215 if (uinfo->value.enumerated.item > e->max - 1)
2216 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002217 strcpy(uinfo->value.enumerated.name,
2218 e->texts[uinfo->value.enumerated.item]);
2219 return 0;
2220}
2221EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2222
2223/**
2224 * snd_soc_info_volsw_ext - external single mixer info callback
2225 * @kcontrol: mixer control
2226 * @uinfo: control element information
2227 *
2228 * Callback to provide information about a single external mixer control.
2229 *
2230 * Returns 0 for success.
2231 */
2232int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2233 struct snd_ctl_elem_info *uinfo)
2234{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002235 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002236
Mark Brownfd5dfad2009-04-15 21:37:46 +01002237 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002238 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2239 else
2240 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2241
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002242 uinfo->count = 1;
2243 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002244 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002245 return 0;
2246}
2247EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2248
2249/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002250 * snd_soc_info_volsw - single mixer info callback
2251 * @kcontrol: mixer control
2252 * @uinfo: control element information
2253 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002254 * Callback to provide information about a single mixer control, or a double
2255 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002256 *
2257 * Returns 0 for success.
2258 */
2259int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2260 struct snd_ctl_elem_info *uinfo)
2261{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002262 struct soc_mixer_control *mc =
2263 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002264 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002265
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002266 if (!mc->platform_max)
2267 mc->platform_max = mc->max;
2268 platform_max = mc->platform_max;
2269
2270 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002271 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2272 else
2273 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2274
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002275 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002276 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002277 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002278 return 0;
2279}
2280EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2281
2282/**
2283 * snd_soc_get_volsw - single mixer get callback
2284 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002285 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002286 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002287 * Callback to get the value of a single mixer control, or a double mixer
2288 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002289 *
2290 * Returns 0 for success.
2291 */
2292int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2293 struct snd_ctl_elem_value *ucontrol)
2294{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002295 struct soc_mixer_control *mc =
2296 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002297 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002298 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002299 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002300 unsigned int shift = mc->shift;
2301 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002302 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002303 unsigned int mask = (1 << fls(max)) - 1;
2304 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002305
2306 ucontrol->value.integer.value[0] =
2307 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002308 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002309 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002310 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002311
2312 if (snd_soc_volsw_is_stereo(mc)) {
2313 if (reg == reg2)
2314 ucontrol->value.integer.value[1] =
2315 (snd_soc_read(codec, reg) >> rshift) & mask;
2316 else
2317 ucontrol->value.integer.value[1] =
2318 (snd_soc_read(codec, reg2) >> shift) & mask;
2319 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002320 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002321 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002322 }
2323
2324 return 0;
2325}
2326EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2327
2328/**
2329 * snd_soc_put_volsw - single mixer put callback
2330 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002331 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002332 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002333 * Callback to set the value of a single mixer control, or a double mixer
2334 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002335 *
2336 * Returns 0 for success.
2337 */
2338int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2339 struct snd_ctl_elem_value *ucontrol)
2340{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002341 struct soc_mixer_control *mc =
2342 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002343 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002344 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002345 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002346 unsigned int shift = mc->shift;
2347 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002348 int max = mc->max;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002349 unsigned int mask = (1 << fls(max)) - 1;
2350 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002351 int err;
2352 bool type_2r = 0;
2353 unsigned int val2 = 0;
2354 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002355
2356 val = (ucontrol->value.integer.value[0] & mask);
2357 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002358 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002359 val_mask = mask << shift;
2360 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002361 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002362 val2 = (ucontrol->value.integer.value[1] & mask);
2363 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002364 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002365 if (reg == reg2) {
2366 val_mask |= mask << rshift;
2367 val |= val2 << rshift;
2368 } else {
2369 val2 = val2 << shift;
2370 type_2r = 1;
2371 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002372 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002373 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002374 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002375 return err;
2376
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002377 if (type_2r)
2378 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2379
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002380 return err;
2381}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002382EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002383
Mark Browne13ac2e2008-05-28 17:58:05 +01002384/**
2385 * snd_soc_info_volsw_s8 - signed mixer info callback
2386 * @kcontrol: mixer control
2387 * @uinfo: control element information
2388 *
2389 * Callback to provide information about a signed mixer control.
2390 *
2391 * Returns 0 for success.
2392 */
2393int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2394 struct snd_ctl_elem_info *uinfo)
2395{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002396 struct soc_mixer_control *mc =
2397 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002398 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002399 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002400
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002401 if (!mc->platform_max)
2402 mc->platform_max = mc->max;
2403 platform_max = mc->platform_max;
2404
Mark Browne13ac2e2008-05-28 17:58:05 +01002405 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2406 uinfo->count = 2;
2407 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002408 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002409 return 0;
2410}
2411EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2412
2413/**
2414 * snd_soc_get_volsw_s8 - signed mixer get callback
2415 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002416 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002417 *
2418 * Callback to get the value of a signed mixer control.
2419 *
2420 * Returns 0 for success.
2421 */
2422int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2423 struct snd_ctl_elem_value *ucontrol)
2424{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002425 struct soc_mixer_control *mc =
2426 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002427 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002428 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002429 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002430 int val = snd_soc_read(codec, reg);
2431
2432 ucontrol->value.integer.value[0] =
2433 ((signed char)(val & 0xff))-min;
2434 ucontrol->value.integer.value[1] =
2435 ((signed char)((val >> 8) & 0xff))-min;
2436 return 0;
2437}
2438EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2439
2440/**
2441 * snd_soc_put_volsw_sgn - signed mixer put callback
2442 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002443 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002444 *
2445 * Callback to set the value of a signed mixer control.
2446 *
2447 * Returns 0 for success.
2448 */
2449int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2450 struct snd_ctl_elem_value *ucontrol)
2451{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002452 struct soc_mixer_control *mc =
2453 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002454 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002455 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002456 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002457 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002458
2459 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2460 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2461
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002462 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002463}
2464EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2465
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002466/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002467 * snd_soc_limit_volume - Set new limit to an existing volume control.
2468 *
2469 * @codec: where to look for the control
2470 * @name: Name of the control
2471 * @max: new maximum limit
2472 *
2473 * Return 0 for success, else error.
2474 */
2475int snd_soc_limit_volume(struct snd_soc_codec *codec,
2476 const char *name, int max)
2477{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002478 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002479 struct snd_kcontrol *kctl;
2480 struct soc_mixer_control *mc;
2481 int found = 0;
2482 int ret = -EINVAL;
2483
2484 /* Sanity check for name and max */
2485 if (unlikely(!name || max <= 0))
2486 return -EINVAL;
2487
2488 list_for_each_entry(kctl, &card->controls, list) {
2489 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2490 found = 1;
2491 break;
2492 }
2493 }
2494 if (found) {
2495 mc = (struct soc_mixer_control *)kctl->private_value;
2496 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002497 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002498 ret = 0;
2499 }
2500 }
2501 return ret;
2502}
2503EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2504
2505/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002506 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2507 * mixer info callback
2508 * @kcontrol: mixer control
2509 * @uinfo: control element information
2510 *
2511 * Returns 0 for success.
2512 */
2513int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2514 struct snd_ctl_elem_info *uinfo)
2515{
2516 struct soc_mixer_control *mc =
2517 (struct soc_mixer_control *)kcontrol->private_value;
2518 int max = mc->max;
2519 int min = mc->min;
2520
2521 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2522 uinfo->count = 2;
2523 uinfo->value.integer.min = 0;
2524 uinfo->value.integer.max = max-min;
2525
2526 return 0;
2527}
2528EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2529
2530/**
2531 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2532 * mixer get callback
2533 * @kcontrol: mixer control
2534 * @uinfo: control element information
2535 *
2536 * Returns 0 for success.
2537 */
2538int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2539 struct snd_ctl_elem_value *ucontrol)
2540{
2541 struct soc_mixer_control *mc =
2542 (struct soc_mixer_control *)kcontrol->private_value;
2543 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2544 unsigned int mask = (1<<mc->shift)-1;
2545 int min = mc->min;
2546 int val = snd_soc_read(codec, mc->reg) & mask;
2547 int valr = snd_soc_read(codec, mc->rreg) & mask;
2548
Stuart Longland20630c72010-06-18 12:56:10 +10002549 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2550 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002551 return 0;
2552}
2553EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2554
2555/**
2556 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2557 * mixer put callback
2558 * @kcontrol: mixer control
2559 * @uinfo: control element information
2560 *
2561 * Returns 0 for success.
2562 */
2563int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2564 struct snd_ctl_elem_value *ucontrol)
2565{
2566 struct soc_mixer_control *mc =
2567 (struct soc_mixer_control *)kcontrol->private_value;
2568 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2569 unsigned int mask = (1<<mc->shift)-1;
2570 int min = mc->min;
2571 int ret;
2572 unsigned int val, valr, oval, ovalr;
2573
2574 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2575 val &= mask;
2576 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2577 valr &= mask;
2578
2579 oval = snd_soc_read(codec, mc->reg) & mask;
2580 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2581
2582 ret = 0;
2583 if (oval != val) {
2584 ret = snd_soc_write(codec, mc->reg, val);
2585 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002586 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002587 }
2588 if (ovalr != valr) {
2589 ret = snd_soc_write(codec, mc->rreg, valr);
2590 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002591 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002592 }
2593
2594 return 0;
2595}
2596EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2597
2598/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002599 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2600 * @dai: DAI
2601 * @clk_id: DAI specific clock ID
2602 * @freq: new clock frequency in Hz
2603 * @dir: new clock direction - input/output.
2604 *
2605 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2606 */
2607int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2608 unsigned int freq, int dir)
2609{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002610 if (dai->driver && dai->driver->ops->set_sysclk)
2611 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002612 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002613 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00002614 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002615 else
2616 return -EINVAL;
2617}
2618EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2619
2620/**
Mark Brownec4ee522011-03-07 20:58:11 +00002621 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2622 * @codec: CODEC
2623 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01002624 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00002625 * @freq: new clock frequency in Hz
2626 * @dir: new clock direction - input/output.
2627 *
2628 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2629 */
2630int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01002631 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00002632{
2633 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002634 return codec->driver->set_sysclk(codec, clk_id, source,
2635 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002636 else
2637 return -EINVAL;
2638}
2639EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2640
2641/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002642 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2643 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002644 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002645 * @div: new clock divisor.
2646 *
2647 * Configures the clock dividers. This is used to derive the best DAI bit and
2648 * frame clocks from the system or master clock. It's best to set the DAI bit
2649 * and frame clocks as low as possible to save system power.
2650 */
2651int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2652 int div_id, int div)
2653{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002654 if (dai->driver && dai->driver->ops->set_clkdiv)
2655 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002656 else
2657 return -EINVAL;
2658}
2659EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2660
2661/**
2662 * snd_soc_dai_set_pll - configure DAI PLL.
2663 * @dai: DAI
2664 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002665 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002666 * @freq_in: PLL input clock frequency in Hz
2667 * @freq_out: requested PLL output clock frequency in Hz
2668 *
2669 * Configures and enables PLL to generate output clock based on input clock.
2670 */
Mark Brown85488032009-09-05 18:52:16 +01002671int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2672 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002673{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002674 if (dai->driver && dai->driver->ops->set_pll)
2675 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002676 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00002677 else if (dai->codec && dai->codec->driver->set_pll)
2678 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2679 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002680 else
2681 return -EINVAL;
2682}
2683EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2684
Mark Brownec4ee522011-03-07 20:58:11 +00002685/*
2686 * snd_soc_codec_set_pll - configure codec PLL.
2687 * @codec: CODEC
2688 * @pll_id: DAI specific PLL ID
2689 * @source: DAI specific source for the PLL
2690 * @freq_in: PLL input clock frequency in Hz
2691 * @freq_out: requested PLL output clock frequency in Hz
2692 *
2693 * Configures and enables PLL to generate output clock based on input clock.
2694 */
2695int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2696 unsigned int freq_in, unsigned int freq_out)
2697{
2698 if (codec->driver->set_pll)
2699 return codec->driver->set_pll(codec, pll_id, source,
2700 freq_in, freq_out);
2701 else
2702 return -EINVAL;
2703}
2704EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2705
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002706/**
2707 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2708 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002709 * @fmt: SND_SOC_DAIFMT_ format value.
2710 *
2711 * Configures the DAI hardware format and clocking.
2712 */
2713int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2714{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002715 if (dai->driver && dai->driver->ops->set_fmt)
2716 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002717 else
2718 return -EINVAL;
2719}
2720EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2721
2722/**
2723 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2724 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002725 * @tx_mask: bitmask representing active TX slots.
2726 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002727 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002728 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002729 *
2730 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2731 * specific.
2732 */
2733int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002734 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002735{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002736 if (dai->driver && dai->driver->ops->set_tdm_slot)
2737 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002738 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002739 else
2740 return -EINVAL;
2741}
2742EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2743
2744/**
Barry Song472df3c2009-09-12 01:16:29 +08002745 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2746 * @dai: DAI
2747 * @tx_num: how many TX channels
2748 * @tx_slot: pointer to an array which imply the TX slot number channel
2749 * 0~num-1 uses
2750 * @rx_num: how many RX channels
2751 * @rx_slot: pointer to an array which imply the RX slot number channel
2752 * 0~num-1 uses
2753 *
2754 * configure the relationship between channel number and TDM slot number.
2755 */
2756int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2757 unsigned int tx_num, unsigned int *tx_slot,
2758 unsigned int rx_num, unsigned int *rx_slot)
2759{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002760 if (dai->driver && dai->driver->ops->set_channel_map)
2761 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08002762 rx_num, rx_slot);
2763 else
2764 return -EINVAL;
2765}
2766EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2767
2768/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002769 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2770 * @dai: DAI
2771 * @tristate: tristate enable
2772 *
2773 * Tristates the DAI so that others can use it.
2774 */
2775int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2776{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002777 if (dai->driver && dai->driver->ops->set_tristate)
2778 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002779 else
2780 return -EINVAL;
2781}
2782EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2783
2784/**
2785 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2786 * @dai: DAI
2787 * @mute: mute enable
2788 *
2789 * Mutes the DAI DAC.
2790 */
2791int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2792{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002793 if (dai->driver && dai->driver->ops->digital_mute)
2794 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002795 else
2796 return -EINVAL;
2797}
2798EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2799
Mark Brownc5af3a22008-11-28 13:29:45 +00002800/**
2801 * snd_soc_register_card - Register a card with the ASoC core
2802 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002803 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00002804 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002805 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302806int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002807{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002808 int i;
2809
Mark Brownc5af3a22008-11-28 13:29:45 +00002810 if (!card->name || !card->dev)
2811 return -EINVAL;
2812
Mark Browned77cc12011-05-03 18:25:34 +01002813 dev_set_drvdata(card->dev, card);
2814
Stephen Warren111c6412011-01-28 14:26:35 -07002815 snd_soc_initialize_card_lists(card);
2816
Vinod Koul150dd2f2011-01-13 22:48:32 +05302817 soc_init_card_debugfs(card);
2818
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002819 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
2820 (card->num_links + card->num_aux_devs),
2821 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002822 if (card->rtd == NULL)
2823 return -ENOMEM;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02002824 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002825
2826 for (i = 0; i < card->num_links; i++)
2827 card->rtd[i].dai_link = &card->dai_link[i];
2828
Mark Brownc5af3a22008-11-28 13:29:45 +00002829 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01002830 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00002831 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002832 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002833
2834 mutex_lock(&client_mutex);
2835 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002836 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00002837 mutex_unlock(&client_mutex);
2838
2839 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2840
2841 return 0;
2842}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302843EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002844
2845/**
2846 * snd_soc_unregister_card - Unregister a card with the ASoC core
2847 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002848 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00002849 *
Mark Brownc5af3a22008-11-28 13:29:45 +00002850 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05302851int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00002852{
Vinod Koulb0e26482011-01-13 22:48:02 +05302853 if (card->instantiated)
2854 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002855 mutex_lock(&client_mutex);
2856 list_del(&card->list);
2857 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00002858 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2859
2860 return 0;
2861}
Vinod Koul70a7ca32011-01-14 19:22:48 +05302862EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00002863
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002864/*
2865 * Simplify DAI link configuration by removing ".-1" from device names
2866 * and sanitizing names.
2867 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00002868static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002869{
2870 char *found, name[NAME_SIZE];
2871 int id1, id2;
2872
2873 if (dev_name(dev) == NULL)
2874 return NULL;
2875
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002876 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002877
2878 /* are we a "%s.%d" name (platform and SPI components) */
2879 found = strstr(name, dev->driver->name);
2880 if (found) {
2881 /* get ID */
2882 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2883
2884 /* discard ID from name if ID == -1 */
2885 if (*id == -1)
2886 found[strlen(dev->driver->name)] = '\0';
2887 }
2888
2889 } else {
2890 /* I2C component devices are named "bus-addr" */
2891 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2892 char tmp[NAME_SIZE];
2893
2894 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03002895 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002896
2897 /* sanitize component name for DAI link creation */
2898 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00002899 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002900 } else
2901 *id = 0;
2902 }
2903
2904 return kstrdup(name, GFP_KERNEL);
2905}
2906
2907/*
2908 * Simplify DAI link naming for single devices with multiple DAIs by removing
2909 * any ".-1" and using the DAI name (instead of device name).
2910 */
2911static inline char *fmt_multiple_name(struct device *dev,
2912 struct snd_soc_dai_driver *dai_drv)
2913{
2914 if (dai_drv->name == NULL) {
2915 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2916 dev_name(dev));
2917 return NULL;
2918 }
2919
2920 return kstrdup(dai_drv->name, GFP_KERNEL);
2921}
2922
Mark Brown91151712008-11-30 23:31:24 +00002923/**
2924 * snd_soc_register_dai - Register a DAI with the ASoC core
2925 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002926 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00002927 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002928int snd_soc_register_dai(struct device *dev,
2929 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00002930{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002931 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00002932
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002933 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00002934
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002935 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2936 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08002937 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08002938
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002939 /* create DAI component name */
2940 dai->name = fmt_single_name(dev, &dai->id);
2941 if (dai->name == NULL) {
2942 kfree(dai);
2943 return -ENOMEM;
2944 }
2945
2946 dai->dev = dev;
2947 dai->driver = dai_drv;
2948 if (!dai->driver->ops)
2949 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00002950
2951 mutex_lock(&client_mutex);
2952 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00002953 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00002954 mutex_unlock(&client_mutex);
2955
2956 pr_debug("Registered DAI '%s'\n", dai->name);
2957
2958 return 0;
2959}
2960EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2961
2962/**
2963 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2964 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002965 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00002966 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002967void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00002968{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002969 struct snd_soc_dai *dai;
2970
2971 list_for_each_entry(dai, &dai_list, list) {
2972 if (dev == dai->dev)
2973 goto found;
2974 }
2975 return;
2976
2977found:
Mark Brown91151712008-11-30 23:31:24 +00002978 mutex_lock(&client_mutex);
2979 list_del(&dai->list);
2980 mutex_unlock(&client_mutex);
2981
2982 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002983 kfree(dai->name);
2984 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00002985}
2986EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2987
2988/**
2989 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2990 *
Mark Brownac11a2b2009-01-01 12:18:17 +00002991 * @dai: Array of DAIs to register
2992 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00002993 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002994int snd_soc_register_dais(struct device *dev,
2995 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00002996{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002997 struct snd_soc_dai *dai;
2998 int i, ret = 0;
2999
Liam Girdwood720ffa42010-08-18 00:30:30 +01003000 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003001
3002 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003003
3004 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003005 if (dai == NULL) {
3006 ret = -ENOMEM;
3007 goto err;
3008 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003009
3010 /* create DAI component name */
3011 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3012 if (dai->name == NULL) {
3013 kfree(dai);
3014 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003015 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003016 }
3017
3018 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003019 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003020 if (dai->driver->id)
3021 dai->id = dai->driver->id;
3022 else
3023 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003024 if (!dai->driver->ops)
3025 dai->driver->ops = &null_dai_ops;
3026
3027 mutex_lock(&client_mutex);
3028 list_add(&dai->list, &dai_list);
3029 mutex_unlock(&client_mutex);
3030
3031 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003032 }
3033
Axel Lin1dcb4f32010-12-06 16:48:03 +08003034 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003035 snd_soc_instantiate_cards();
Axel Lin1dcb4f32010-12-06 16:48:03 +08003036 mutex_unlock(&client_mutex);
Mark Brown91151712008-11-30 23:31:24 +00003037 return 0;
3038
3039err:
3040 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003041 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003042
3043 return ret;
3044}
3045EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3046
3047/**
3048 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3049 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003050 * @dai: Array of DAIs to unregister
3051 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003052 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003053void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003054{
3055 int i;
3056
3057 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003058 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003059}
3060EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3061
Mark Brown12a48a8c2008-12-03 19:40:30 +00003062/**
3063 * snd_soc_register_platform - Register a platform with the ASoC core
3064 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003065 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003066 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003067int snd_soc_register_platform(struct device *dev,
3068 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003069{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003070 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003071
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003072 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3073
3074 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3075 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003076 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003077
3078 /* create platform component name */
3079 platform->name = fmt_single_name(dev, &platform->id);
3080 if (platform->name == NULL) {
3081 kfree(platform);
3082 return -ENOMEM;
3083 }
3084
3085 platform->dev = dev;
3086 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003087 platform->dapm.dev = dev;
3088 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003089 platform->dapm.stream_event = platform_drv->stream_event;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003090
3091 mutex_lock(&client_mutex);
3092 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003093 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003094 mutex_unlock(&client_mutex);
3095
3096 pr_debug("Registered platform '%s'\n", platform->name);
3097
3098 return 0;
3099}
3100EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3101
3102/**
3103 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3104 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003105 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003106 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003107void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003108{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003109 struct snd_soc_platform *platform;
3110
3111 list_for_each_entry(platform, &platform_list, list) {
3112 if (dev == platform->dev)
3113 goto found;
3114 }
3115 return;
3116
3117found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003118 mutex_lock(&client_mutex);
3119 list_del(&platform->list);
3120 mutex_unlock(&client_mutex);
3121
3122 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003123 kfree(platform->name);
3124 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003125}
3126EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3127
Mark Brown151ab222009-05-09 16:22:58 +01003128static u64 codec_format_map[] = {
3129 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3130 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3131 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3132 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3133 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3134 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3135 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3136 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3137 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3138 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3139 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3140 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3141 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3142 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3143 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3144 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3145};
3146
3147/* Fix up the DAI formats for endianness: codecs don't actually see
3148 * the endianness of the data but we're using the CPU format
3149 * definitions which do need to include endianness so we ensure that
3150 * codec DAIs always have both big and little endian variants set.
3151 */
3152static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3153{
3154 int i;
3155
3156 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3157 if (stream->formats & codec_format_map[i])
3158 stream->formats |= codec_format_map[i];
3159}
3160
Mark Brown0d0cf002008-12-10 14:32:45 +00003161/**
3162 * snd_soc_register_codec - Register a codec with the ASoC core
3163 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003164 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003165 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003166int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003167 const struct snd_soc_codec_driver *codec_drv,
3168 struct snd_soc_dai_driver *dai_drv,
3169 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003170{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003171 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003172 struct snd_soc_codec *codec;
3173 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003174
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003175 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003176
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003177 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3178 if (codec == NULL)
3179 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003180
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003181 /* create CODEC component name */
3182 codec->name = fmt_single_name(dev, &codec->id);
3183 if (codec->name == NULL) {
3184 kfree(codec);
3185 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003186 }
3187
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003188 if (codec_drv->compress_type)
3189 codec->compress_type = codec_drv->compress_type;
3190 else
3191 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3192
Mark Brownc3acec22010-12-02 16:15:29 +00003193 codec->write = codec_drv->write;
3194 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003195 codec->volatile_register = codec_drv->volatile_register;
3196 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003197 codec->writable_register = codec_drv->writable_register;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003198 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3199 codec->dapm.dev = dev;
3200 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003201 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003202 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003203 codec->dev = dev;
3204 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003205 codec->num_dai = num_dai;
3206 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003207
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003208 /* allocate CODEC register cache */
3209 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003210 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003211 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003212 /* it is necessary to make a copy of the default register cache
3213 * because in the case of using a compression type that requires
3214 * the default register cache to be marked as __devinitconst the
3215 * kernel might have freed the array by the time we initialize
3216 * the cache.
3217 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003218 if (codec_drv->reg_cache_default) {
3219 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3220 reg_size, GFP_KERNEL);
3221 if (!codec->reg_def_copy) {
3222 ret = -ENOMEM;
3223 goto fail;
3224 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003225 }
3226 }
3227
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003228 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3229 if (!codec->volatile_register)
3230 codec->volatile_register = snd_soc_default_volatile_register;
3231 if (!codec->readable_register)
3232 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003233 if (!codec->writable_register)
3234 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003235 }
3236
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003237 for (i = 0; i < num_dai; i++) {
3238 fixup_codec_formats(&dai_drv[i].playback);
3239 fixup_codec_formats(&dai_drv[i].capture);
3240 }
3241
Mark Brown26b01cc2010-08-18 20:20:55 +01003242 /* register any DAIs */
3243 if (num_dai) {
3244 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3245 if (ret < 0)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003246 goto fail;
Mark Brown26b01cc2010-08-18 20:20:55 +01003247 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003248
Mark Brown0d0cf002008-12-10 14:32:45 +00003249 mutex_lock(&client_mutex);
3250 list_add(&codec->list, &codec_list);
3251 snd_soc_instantiate_cards();
3252 mutex_unlock(&client_mutex);
3253
3254 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003255 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003256
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003257fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003258 kfree(codec->reg_def_copy);
3259 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003260 kfree(codec->name);
3261 kfree(codec);
3262 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003263}
3264EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3265
3266/**
3267 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3268 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003269 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003270 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003271void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003272{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003273 struct snd_soc_codec *codec;
3274 int i;
3275
3276 list_for_each_entry(codec, &codec_list, list) {
3277 if (dev == codec->dev)
3278 goto found;
3279 }
3280 return;
3281
3282found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003283 if (codec->num_dai)
3284 for (i = 0; i < codec->num_dai; i++)
3285 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003286
Mark Brown0d0cf002008-12-10 14:32:45 +00003287 mutex_lock(&client_mutex);
3288 list_del(&codec->list);
3289 mutex_unlock(&client_mutex);
3290
3291 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003292
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003293 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003294 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003295 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003296 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003297}
3298EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3299
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003300static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003301{
Mark Brown384c89e2008-12-03 17:34:03 +00003302#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003303 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3304 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Mark Brown384c89e2008-12-03 17:34:03 +00003305 printk(KERN_WARNING
3306 "ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00003307 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00003308 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003309
Mark Brown8a9dab12011-01-10 22:25:21 +00003310 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01003311 &codec_list_fops))
3312 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3313
Mark Brown8a9dab12011-01-10 22:25:21 +00003314 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01003315 &dai_list_fops))
3316 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003317
Mark Brown8a9dab12011-01-10 22:25:21 +00003318 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01003319 &platform_list_fops))
3320 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003321#endif
3322
Mark Brownfb257892011-04-28 10:57:54 +01003323 snd_soc_util_init();
3324
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003325 return platform_driver_register(&soc_driver);
3326}
Mark Brown4abe8e12010-10-12 17:41:03 +01003327module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003328
Mark Brown7d8c16a2008-11-30 22:11:24 +00003329static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003330{
Mark Brownfb257892011-04-28 10:57:54 +01003331 snd_soc_util_exit();
3332
Mark Brown384c89e2008-12-03 17:34:03 +00003333#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003334 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00003335#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003336 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003337}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003338module_exit(snd_soc_exit);
3339
3340/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003341MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003342MODULE_DESCRIPTION("ALSA SoC Core");
3343MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003344MODULE_ALIAS("platform:soc-audio");