ASoC: Update PM ifdefs for exported suspend/resume
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / sound / soc / soc-core.c
CommitLineData
db2a4165
FM
1/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
0664d888 5 * Copyright 2005 Openedhand Ltd.
f0fba2ad
LG
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
0664d888 8 *
d331124d 9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
0664d888
LG
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
db2a4165
FM
12 *
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 *
db2a4165
FM
18 * 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>
12ef193d 31#include <linux/debugfs.h>
db2a4165 32#include <linux/platform_device.h>
5a0e3ad6 33#include <linux/slab.h>
474828a4 34#include <sound/ac97_codec.h>
db2a4165 35#include <sound/core.h>
3028eb8c 36#include <sound/jack.h>
db2a4165
FM
37#include <sound/pcm.h>
38#include <sound/pcm_params.h>
39#include <sound/soc.h>
db2a4165
FM
40#include <sound/initval.h>
41
a8b1d34f
MB
42#define CREATE_TRACE_POINTS
43#include <trace/events/asoc.h>
44
f0fba2ad
LG
45#define NAME_SIZE 32
46
db2a4165 47static DEFINE_MUTEX(pcm_mutex);
db2a4165
FM
48static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
49
384c89e2 50#ifdef CONFIG_DEBUG_FS
8a9dab1a
MB
51struct dentry *snd_soc_debugfs_root;
52EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
384c89e2
MB
53#endif
54
c5af3a2e
MB
55static DEFINE_MUTEX(client_mutex);
56static LIST_HEAD(card_list);
9115171a 57static LIST_HEAD(dai_list);
12a48a8c 58static LIST_HEAD(platform_list);
0d0cf00a 59static LIST_HEAD(codec_list);
c5af3a2e 60
f0fba2ad 61static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
c5af3a2e 62
db2a4165
FM
63/*
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
2bc9a81e
DP
72/* 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
2624d5fa
MB
90/* codec register dump */
91static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
92{
5164d74d 93 int ret, i, step = 1, count = 0;
2bc9a81e
DP
94 int wordsize, regsize;
95
96 wordsize = codec->driver->reg_word_size * 2;
97 regsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
2624d5fa 98
f0fba2ad 99 if (!codec->driver->reg_cache_size)
2624d5fa
MB
100 return 0;
101
f0fba2ad
LG
102 if (codec->driver->reg_cache_step)
103 step = codec->driver->reg_cache_step;
2624d5fa 104
f0fba2ad 105 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
1500b7b5 106 if (codec->readable_register && !codec->readable_register(codec, i))
2624d5fa
MB
107 continue;
108
2bc9a81e 109 count += sprintf(buf + count, "%.*x: ", regsize, i);
2624d5fa
MB
110 if (count >= PAGE_SIZE - 1)
111 break;
112
f0fba2ad
LG
113 if (codec->driver->display_register) {
114 count += codec->driver->display_register(codec, buf + count,
2624d5fa 115 PAGE_SIZE - count, i);
5164d74d
MB
116 } else {
117 /* If the read fails it's almost certainly due to
118 * the register being volatile and the device being
119 * powered off.
120 */
e4f078d8 121 ret = snd_soc_read(codec, i);
5164d74d
MB
122 if (ret >= 0)
123 count += snprintf(buf + count,
124 PAGE_SIZE - count,
2bc9a81e 125 "%.*x", wordsize, ret);
5164d74d
MB
126 else
127 count += snprintf(buf + count,
128 PAGE_SIZE - count,
129 "<no data: %d>", ret);
130 }
2624d5fa
MB
131
132 if (count >= PAGE_SIZE - 1)
133 break;
134
135 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
136 if (count >= PAGE_SIZE - 1)
137 break;
138 }
139
140 /* Truncate count; min() would cause a warning */
141 if (count >= PAGE_SIZE)
142 count = PAGE_SIZE - 1;
143
144 return count;
145}
146static ssize_t codec_reg_show(struct device *dev,
147 struct device_attribute *attr, char *buf)
148{
f0fba2ad
LG
149 struct snd_soc_pcm_runtime *rtd =
150 container_of(dev, struct snd_soc_pcm_runtime, dev);
151
152 return soc_codec_reg_show(rtd->codec, buf);
2624d5fa
MB
153}
154
155static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
156
dbe21408
MB
157static ssize_t pmdown_time_show(struct device *dev,
158 struct device_attribute *attr, char *buf)
159{
f0fba2ad
LG
160 struct snd_soc_pcm_runtime *rtd =
161 container_of(dev, struct snd_soc_pcm_runtime, dev);
dbe21408 162
f0fba2ad 163 return sprintf(buf, "%ld\n", rtd->pmdown_time);
dbe21408
MB
164}
165
166static ssize_t pmdown_time_set(struct device *dev,
167 struct device_attribute *attr,
168 const char *buf, size_t count)
169{
f0fba2ad
LG
170 struct snd_soc_pcm_runtime *rtd =
171 container_of(dev, struct snd_soc_pcm_runtime, dev);
c593b520 172 int ret;
dbe21408 173
c593b520
MB
174 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
175 if (ret)
176 return ret;
dbe21408
MB
177
178 return count;
179}
180
181static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
182
2624d5fa
MB
183#ifdef CONFIG_DEBUG_FS
184static int codec_reg_open_file(struct inode *inode, struct file *file)
185{
186 file->private_data = inode->i_private;
187 return 0;
188}
189
190static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
191 size_t count, loff_t *ppos)
192{
193 ssize_t ret;
194 struct snd_soc_codec *codec = file->private_data;
195 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
196 if (!buf)
197 return -ENOMEM;
198 ret = soc_codec_reg_show(codec, buf);
199 if (ret >= 0)
200 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
201 kfree(buf);
202 return ret;
203}
204
205static ssize_t codec_reg_write_file(struct file *file,
206 const char __user *user_buf, size_t count, loff_t *ppos)
207{
208 char buf[32];
209 int buf_size;
210 char *start = buf;
211 unsigned long reg, value;
212 int step = 1;
213 struct snd_soc_codec *codec = file->private_data;
214
215 buf_size = min(count, (sizeof(buf)-1));
216 if (copy_from_user(buf, user_buf, buf_size))
217 return -EFAULT;
218 buf[buf_size] = 0;
219
f0fba2ad
LG
220 if (codec->driver->reg_cache_step)
221 step = codec->driver->reg_cache_step;
2624d5fa
MB
222
223 while (*start == ' ')
224 start++;
225 reg = simple_strtoul(start, &start, 16);
f0fba2ad 226 if ((reg >= codec->driver->reg_cache_size) || (reg % step))
2624d5fa
MB
227 return -EINVAL;
228 while (*start == ' ')
229 start++;
230 if (strict_strtoul(start, 16, &value))
231 return -EINVAL;
0d51a9cb
MB
232
233 /* Userspace has been fiddling around behind the kernel's back */
234 add_taint(TAINT_USER);
235
e4f078d8 236 snd_soc_write(codec, reg, value);
2624d5fa
MB
237 return buf_size;
238}
239
240static const struct file_operations codec_reg_fops = {
241 .open = codec_reg_open_file,
242 .read = codec_reg_read_file,
243 .write = codec_reg_write_file,
6038f373 244 .llseek = default_llseek,
2624d5fa
MB
245};
246
247static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
248{
d6ce4cf3
JN
249 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
250
251 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
252 debugfs_card_root);
2624d5fa
MB
253 if (!codec->debugfs_codec_root) {
254 printk(KERN_WARNING
255 "ASoC: Failed to create codec debugfs directory\n");
256 return;
257 }
258
aaee8ef1
MB
259 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
260 &codec->cache_sync);
261 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
262 &codec->cache_only);
263
2624d5fa
MB
264 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
265 codec->debugfs_codec_root,
266 codec, &codec_reg_fops);
267 if (!codec->debugfs_reg)
268 printk(KERN_WARNING
269 "ASoC: Failed to create codec register debugfs file\n");
270
ce6120cc 271 codec->dapm.debugfs_dapm = debugfs_create_dir("dapm",
2624d5fa 272 codec->debugfs_codec_root);
ce6120cc 273 if (!codec->dapm.debugfs_dapm)
2624d5fa
MB
274 printk(KERN_WARNING
275 "Failed to create DAPM debugfs directory\n");
276
ce6120cc 277 snd_soc_dapm_debugfs_init(&codec->dapm);
2624d5fa
MB
278}
279
280static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
281{
282 debugfs_remove_recursive(codec->debugfs_codec_root);
283}
284
c3c5a19a
MB
285static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
286 size_t count, loff_t *ppos)
287{
288 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2b194f9d 289 ssize_t len, ret = 0;
c3c5a19a
MB
290 struct snd_soc_codec *codec;
291
292 if (!buf)
293 return -ENOMEM;
294
2b194f9d
MB
295 list_for_each_entry(codec, &codec_list, list) {
296 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
297 codec->name);
298 if (len >= 0)
299 ret += len;
300 if (ret > PAGE_SIZE) {
301 ret = PAGE_SIZE;
302 break;
303 }
304 }
c3c5a19a
MB
305
306 if (ret >= 0)
307 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
308
309 kfree(buf);
310
311 return ret;
312}
313
314static const struct file_operations codec_list_fops = {
315 .read = codec_list_read_file,
316 .llseek = default_llseek,/* read accesses f_pos */
317};
318
f3208780
MB
319static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
320 size_t count, loff_t *ppos)
321{
322 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2b194f9d 323 ssize_t len, ret = 0;
f3208780
MB
324 struct snd_soc_dai *dai;
325
326 if (!buf)
327 return -ENOMEM;
328
2b194f9d
MB
329 list_for_each_entry(dai, &dai_list, list) {
330 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
331 if (len >= 0)
332 ret += len;
333 if (ret > PAGE_SIZE) {
334 ret = PAGE_SIZE;
335 break;
336 }
337 }
f3208780 338
2b194f9d 339 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
f3208780
MB
340
341 kfree(buf);
342
343 return ret;
344}
345
346static const struct file_operations dai_list_fops = {
347 .read = dai_list_read_file,
348 .llseek = default_llseek,/* read accesses f_pos */
349};
350
19c7ac27
MB
351static ssize_t platform_list_read_file(struct file *file,
352 char __user *user_buf,
353 size_t count, loff_t *ppos)
354{
355 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2b194f9d 356 ssize_t len, ret = 0;
19c7ac27
MB
357 struct snd_soc_platform *platform;
358
359 if (!buf)
360 return -ENOMEM;
361
2b194f9d
MB
362 list_for_each_entry(platform, &platform_list, list) {
363 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
364 platform->name);
365 if (len >= 0)
366 ret += len;
367 if (ret > PAGE_SIZE) {
368 ret = PAGE_SIZE;
369 break;
370 }
371 }
19c7ac27 372
2b194f9d 373 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
19c7ac27
MB
374
375 kfree(buf);
376
377 return ret;
378}
379
380static const struct file_operations platform_list_fops = {
381 .read = platform_list_read_file,
382 .llseek = default_llseek,/* read accesses f_pos */
383};
384
a6052154
JN
385static void soc_init_card_debugfs(struct snd_soc_card *card)
386{
387 card->debugfs_card_root = debugfs_create_dir(card->name,
8a9dab1a 388 snd_soc_debugfs_root);
3a45b867 389 if (!card->debugfs_card_root) {
a6052154
JN
390 dev_warn(card->dev,
391 "ASoC: Failed to create codec debugfs directory\n");
3a45b867
JN
392 return;
393 }
394
395 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
396 card->debugfs_card_root,
397 &card->pop_time);
398 if (!card->debugfs_pop_time)
399 dev_warn(card->dev,
400 "Failed to create pop time debugfs file\n");
a6052154
JN
401}
402
403static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
404{
405 debugfs_remove_recursive(card->debugfs_card_root);
406}
407
2624d5fa
MB
408#else
409
410static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
411{
412}
413
414static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
415{
416}
b95fccbc
AL
417
418static inline void soc_init_card_debugfs(struct snd_soc_card *card)
419{
420}
421
422static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
423{
424}
2624d5fa
MB
425#endif
426
db2a4165
FM
427#ifdef CONFIG_SND_SOC_AC97_BUS
428/* unregister ac97 codec */
429static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
430{
431 if (codec->ac97->dev.bus)
432 device_unregister(&codec->ac97->dev);
433 return 0;
434}
435
436/* stop no dev release warning */
437static void soc_ac97_device_release(struct device *dev){}
438
439/* register ac97 codec to bus */
440static int soc_ac97_dev_register(struct snd_soc_codec *codec)
441{
442 int err;
443
444 codec->ac97->dev.bus = &ac97_bus_type;
4ac5c61f 445 codec->ac97->dev.parent = codec->card->dev;
db2a4165
FM
446 codec->ac97->dev.release = soc_ac97_device_release;
447
bb072bf0 448 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
f0fba2ad 449 codec->card->snd_card->number, 0, codec->name);
db2a4165
FM
450 err = device_register(&codec->ac97->dev);
451 if (err < 0) {
452 snd_printk(KERN_ERR "Can't register ac97 bus\n");
453 codec->ac97->dev.bus = NULL;
454 return err;
455 }
456 return 0;
457}
458#endif
459
06f409d7
MB
460static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
461{
462 struct snd_soc_pcm_runtime *rtd = substream->private_data;
f0fba2ad
LG
463 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
464 struct snd_soc_dai *codec_dai = rtd->codec_dai;
06f409d7
MB
465 int ret;
466
f0fba2ad
LG
467 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
468 rtd->dai_link->symmetric_rates) {
469 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
470 rtd->rate);
06f409d7
MB
471
472 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
473 SNDRV_PCM_HW_PARAM_RATE,
f0fba2ad
LG
474 rtd->rate,
475 rtd->rate);
06f409d7 476 if (ret < 0) {
f0fba2ad 477 dev_err(&rtd->dev,
06f409d7
MB
478 "Unable to apply rate symmetry constraint: %d\n", ret);
479 return ret;
480 }
481 }
482
483 return 0;
484}
485
db2a4165
FM
486/*
487 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
488 * then initialized and any private data can be allocated. This also calls
489 * startup for the cpu DAI, platform, machine and codec DAI.
490 */
491static int soc_pcm_open(struct snd_pcm_substream *substream)
492{
493 struct snd_soc_pcm_runtime *rtd = substream->private_data;
db2a4165 494 struct snd_pcm_runtime *runtime = substream->runtime;
f0fba2ad
LG
495 struct snd_soc_platform *platform = rtd->platform;
496 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
497 struct snd_soc_dai *codec_dai = rtd->codec_dai;
498 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
499 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
db2a4165
FM
500 int ret = 0;
501
502 mutex_lock(&pcm_mutex);
503
504 /* startup the audio subsystem */
f0fba2ad
LG
505 if (cpu_dai->driver->ops->startup) {
506 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
db2a4165
FM
507 if (ret < 0) {
508 printk(KERN_ERR "asoc: can't open interface %s\n",
cb666e5b 509 cpu_dai->name);
db2a4165
FM
510 goto out;
511 }
512 }
513
f0fba2ad
LG
514 if (platform->driver->ops->open) {
515 ret = platform->driver->ops->open(substream);
db2a4165
FM
516 if (ret < 0) {
517 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
518 goto platform_err;
519 }
520 }
521
f0fba2ad
LG
522 if (codec_dai->driver->ops->startup) {
523 ret = codec_dai->driver->ops->startup(substream, codec_dai);
db2a4165 524 if (ret < 0) {
cb666e5b
LG
525 printk(KERN_ERR "asoc: can't open codec %s\n",
526 codec_dai->name);
527 goto codec_dai_err;
db2a4165
FM
528 }
529 }
530
f0fba2ad
LG
531 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
532 ret = rtd->dai_link->ops->startup(substream);
db2a4165 533 if (ret < 0) {
f0fba2ad 534 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
cb666e5b 535 goto machine_err;
db2a4165
FM
536 }
537 }
538
a00f90f9 539 /* Check that the codec and cpu DAIs are compatible */
db2a4165
FM
540 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
541 runtime->hw.rate_min =
f0fba2ad
LG
542 max(codec_dai_drv->playback.rate_min,
543 cpu_dai_drv->playback.rate_min);
db2a4165 544 runtime->hw.rate_max =
f0fba2ad
LG
545 min(codec_dai_drv->playback.rate_max,
546 cpu_dai_drv->playback.rate_max);
db2a4165 547 runtime->hw.channels_min =
f0fba2ad
LG
548 max(codec_dai_drv->playback.channels_min,
549 cpu_dai_drv->playback.channels_min);
db2a4165 550 runtime->hw.channels_max =
f0fba2ad
LG
551 min(codec_dai_drv->playback.channels_max,
552 cpu_dai_drv->playback.channels_max);
cb666e5b 553 runtime->hw.formats =
f0fba2ad 554 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
cb666e5b 555 runtime->hw.rates =
f0fba2ad
LG
556 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
557 if (codec_dai_drv->playback.rates
d9ad6296 558 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
f0fba2ad
LG
559 runtime->hw.rates |= cpu_dai_drv->playback.rates;
560 if (cpu_dai_drv->playback.rates
d9ad6296 561 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
f0fba2ad 562 runtime->hw.rates |= codec_dai_drv->playback.rates;
db2a4165
FM
563 } else {
564 runtime->hw.rate_min =
f0fba2ad
LG
565 max(codec_dai_drv->capture.rate_min,
566 cpu_dai_drv->capture.rate_min);
db2a4165 567 runtime->hw.rate_max =
f0fba2ad
LG
568 min(codec_dai_drv->capture.rate_max,
569 cpu_dai_drv->capture.rate_max);
db2a4165 570 runtime->hw.channels_min =
f0fba2ad
LG
571 max(codec_dai_drv->capture.channels_min,
572 cpu_dai_drv->capture.channels_min);
db2a4165 573 runtime->hw.channels_max =
f0fba2ad
LG
574 min(codec_dai_drv->capture.channels_max,
575 cpu_dai_drv->capture.channels_max);
cb666e5b 576 runtime->hw.formats =
f0fba2ad 577 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
cb666e5b 578 runtime->hw.rates =
f0fba2ad
LG
579 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
580 if (codec_dai_drv->capture.rates
d9ad6296 581 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
f0fba2ad
LG
582 runtime->hw.rates |= cpu_dai_drv->capture.rates;
583 if (cpu_dai_drv->capture.rates
d9ad6296 584 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
f0fba2ad 585 runtime->hw.rates |= codec_dai_drv->capture.rates;
db2a4165
FM
586 }
587
588 snd_pcm_limit_hw_rates(runtime);
589 if (!runtime->hw.rates) {
590 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
cb666e5b 591 codec_dai->name, cpu_dai->name);
bb1c0478 592 goto config_err;
db2a4165
FM
593 }
594 if (!runtime->hw.formats) {
595 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
cb666e5b 596 codec_dai->name, cpu_dai->name);
bb1c0478 597 goto config_err;
db2a4165
FM
598 }
599 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
600 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
f0fba2ad 601 codec_dai->name, cpu_dai->name);
bb1c0478 602 goto config_err;
db2a4165
FM
603 }
604
06f409d7
MB
605 /* Symmetry only applies if we've already got an active stream. */
606 if (cpu_dai->active || codec_dai->active) {
607 ret = soc_pcm_apply_symmetry(substream);
608 if (ret != 0)
bb1c0478 609 goto config_err;
06f409d7
MB
610 }
611
f0fba2ad
LG
612 pr_debug("asoc: %s <-> %s info:\n",
613 codec_dai->name, cpu_dai->name);
f24368c2
MB
614 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
615 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
616 runtime->hw.channels_max);
617 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
618 runtime->hw.rate_max);
db2a4165 619
14dc5734 620 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
f0fba2ad
LG
621 cpu_dai->playback_active++;
622 codec_dai->playback_active++;
14dc5734 623 } else {
f0fba2ad
LG
624 cpu_dai->capture_active++;
625 codec_dai->capture_active++;
14dc5734
JB
626 }
627 cpu_dai->active++;
628 codec_dai->active++;
f0fba2ad 629 rtd->codec->active++;
db2a4165
FM
630 mutex_unlock(&pcm_mutex);
631 return 0;
632
bb1c0478 633config_err:
f0fba2ad
LG
634 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
635 rtd->dai_link->ops->shutdown(substream);
db2a4165 636
bb1c0478 637machine_err:
f0fba2ad
LG
638 if (codec_dai->driver->ops->shutdown)
639 codec_dai->driver->ops->shutdown(substream, codec_dai);
bb1c0478 640
cb666e5b 641codec_dai_err:
f0fba2ad
LG
642 if (platform->driver->ops->close)
643 platform->driver->ops->close(substream);
db2a4165
FM
644
645platform_err:
f0fba2ad
LG
646 if (cpu_dai->driver->ops->shutdown)
647 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
db2a4165
FM
648out:
649 mutex_unlock(&pcm_mutex);
650 return ret;
651}
652
653/*
3a4fa0a2 654 * Power down the audio subsystem pmdown_time msecs after close is called.
db2a4165
FM
655 * This is to ensure there are no pops or clicks in between any music tracks
656 * due to DAPM power cycling.
657 */
4484bb2e 658static void close_delayed_work(struct work_struct *work)
db2a4165 659{
f0fba2ad
LG
660 struct snd_soc_pcm_runtime *rtd =
661 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
662 struct snd_soc_dai *codec_dai = rtd->codec_dai;
db2a4165
FM
663
664 mutex_lock(&pcm_mutex);
f0fba2ad
LG
665
666 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
667 codec_dai->driver->playback.stream_name,
668 codec_dai->playback_active ? "active" : "inactive",
669 codec_dai->pop_wait ? "yes" : "no");
670
671 /* are we waiting on this codec DAI stream */
672 if (codec_dai->pop_wait == 1) {
673 codec_dai->pop_wait = 0;
674 snd_soc_dapm_stream_event(rtd,
675 codec_dai->driver->playback.stream_name,
676 SND_SOC_DAPM_STREAM_STOP);
db2a4165 677 }
f0fba2ad 678
db2a4165
FM
679 mutex_unlock(&pcm_mutex);
680}
681
682/*
683 * Called by ALSA when a PCM substream is closed. Private data can be
684 * freed here. The cpu DAI, codec DAI, machine and platform are also
685 * shutdown.
686 */
687static int soc_codec_close(struct snd_pcm_substream *substream)
688{
689 struct snd_soc_pcm_runtime *rtd = substream->private_data;
f0fba2ad
LG
690 struct snd_soc_platform *platform = rtd->platform;
691 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
692 struct snd_soc_dai *codec_dai = rtd->codec_dai;
693 struct snd_soc_codec *codec = rtd->codec;
db2a4165
FM
694
695 mutex_lock(&pcm_mutex);
696
14dc5734 697 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
f0fba2ad
LG
698 cpu_dai->playback_active--;
699 codec_dai->playback_active--;
14dc5734 700 } else {
f0fba2ad
LG
701 cpu_dai->capture_active--;
702 codec_dai->capture_active--;
db2a4165 703 }
14dc5734
JB
704
705 cpu_dai->active--;
706 codec_dai->active--;
db2a4165
FM
707 codec->active--;
708
6010b2da
MB
709 /* Muting the DAC suppresses artifacts caused during digital
710 * shutdown, for example from stopping clocks.
711 */
712 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
713 snd_soc_dai_digital_mute(codec_dai, 1);
714
f0fba2ad
LG
715 if (cpu_dai->driver->ops->shutdown)
716 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
db2a4165 717
f0fba2ad
LG
718 if (codec_dai->driver->ops->shutdown)
719 codec_dai->driver->ops->shutdown(substream, codec_dai);
db2a4165 720
f0fba2ad
LG
721 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
722 rtd->dai_link->ops->shutdown(substream);
db2a4165 723
f0fba2ad
LG
724 if (platform->driver->ops->close)
725 platform->driver->ops->close(substream);
726 cpu_dai->runtime = NULL;
db2a4165
FM
727
728 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
729 /* start delayed pop wq here for playback streams */
cb666e5b 730 codec_dai->pop_wait = 1;
f0fba2ad
LG
731 schedule_delayed_work(&rtd->delayed_work,
732 msecs_to_jiffies(rtd->pmdown_time));
db2a4165
FM
733 } else {
734 /* capture streams can be powered down now */
f0fba2ad
LG
735 snd_soc_dapm_stream_event(rtd,
736 codec_dai->driver->capture.stream_name,
0b4d221b 737 SND_SOC_DAPM_STREAM_STOP);
db2a4165
FM
738 }
739
740 mutex_unlock(&pcm_mutex);
741 return 0;
742}
743
744/*
745 * Called by ALSA when the PCM substream is prepared, can set format, sample
746 * rate, etc. This function is non atomic and can be called multiple times,
747 * it can refer to the runtime info.
748 */
749static int soc_pcm_prepare(struct snd_pcm_substream *substream)
750{
751 struct snd_soc_pcm_runtime *rtd = substream->private_data;
f0fba2ad
LG
752 struct snd_soc_platform *platform = rtd->platform;
753 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
754 struct snd_soc_dai *codec_dai = rtd->codec_dai;
db2a4165
FM
755 int ret = 0;
756
757 mutex_lock(&pcm_mutex);
cb666e5b 758
f0fba2ad
LG
759 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
760 ret = rtd->dai_link->ops->prepare(substream);
cb666e5b
LG
761 if (ret < 0) {
762 printk(KERN_ERR "asoc: machine prepare error\n");
763 goto out;
764 }
765 }
766
f0fba2ad
LG
767 if (platform->driver->ops->prepare) {
768 ret = platform->driver->ops->prepare(substream);
a71a468a
LG
769 if (ret < 0) {
770 printk(KERN_ERR "asoc: platform prepare error\n");
db2a4165 771 goto out;
a71a468a 772 }
db2a4165
FM
773 }
774
f0fba2ad
LG
775 if (codec_dai->driver->ops->prepare) {
776 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
a71a468a
LG
777 if (ret < 0) {
778 printk(KERN_ERR "asoc: codec DAI prepare error\n");
db2a4165 779 goto out;
a71a468a 780 }
db2a4165
FM
781 }
782
f0fba2ad
LG
783 if (cpu_dai->driver->ops->prepare) {
784 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
cb666e5b
LG
785 if (ret < 0) {
786 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
787 goto out;
788 }
789 }
db2a4165 790
d45f6219
MB
791 /* cancel any delayed stream shutdown that is pending */
792 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
793 codec_dai->pop_wait) {
794 codec_dai->pop_wait = 0;
f0fba2ad 795 cancel_delayed_work(&rtd->delayed_work);
d45f6219 796 }
db2a4165 797
452c5eaa 798 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
f0fba2ad
LG
799 snd_soc_dapm_stream_event(rtd,
800 codec_dai->driver->playback.stream_name,
452c5eaa
MB
801 SND_SOC_DAPM_STREAM_START);
802 else
f0fba2ad
LG
803 snd_soc_dapm_stream_event(rtd,
804 codec_dai->driver->capture.stream_name,
452c5eaa 805 SND_SOC_DAPM_STREAM_START);
8c6529db 806
452c5eaa 807 snd_soc_dai_digital_mute(codec_dai, 0);
db2a4165
FM
808
809out:
810 mutex_unlock(&pcm_mutex);
811 return ret;
812}
813
814/*
815 * Called by ALSA when the hardware params are set by application. This
816 * function can also be called multiple times and can allocate buffers
817 * (using snd_pcm_lib_* ). It's non-atomic.
818 */
819static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
820 struct snd_pcm_hw_params *params)
821{
822 struct snd_soc_pcm_runtime *rtd = substream->private_data;
f0fba2ad
LG
823 struct snd_soc_platform *platform = rtd->platform;
824 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
825 struct snd_soc_dai *codec_dai = rtd->codec_dai;
db2a4165
FM
826 int ret = 0;
827
828 mutex_lock(&pcm_mutex);
829
f0fba2ad
LG
830 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
831 ret = rtd->dai_link->ops->hw_params(substream, params);
cb666e5b
LG
832 if (ret < 0) {
833 printk(KERN_ERR "asoc: machine hw_params failed\n");
db2a4165 834 goto out;
cb666e5b 835 }
db2a4165
FM
836 }
837
f0fba2ad
LG
838 if (codec_dai->driver->ops->hw_params) {
839 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
db2a4165
FM
840 if (ret < 0) {
841 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
cb666e5b
LG
842 codec_dai->name);
843 goto codec_err;
db2a4165
FM
844 }
845 }
846
f0fba2ad
LG
847 if (cpu_dai->driver->ops->hw_params) {
848 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
db2a4165 849 if (ret < 0) {
3ff3f64b 850 printk(KERN_ERR "asoc: interface %s hw params failed\n",
cb666e5b 851 cpu_dai->name);
db2a4165
FM
852 goto interface_err;
853 }
854 }
855
f0fba2ad
LG
856 if (platform->driver->ops->hw_params) {
857 ret = platform->driver->ops->hw_params(substream, params);
db2a4165 858 if (ret < 0) {
3ff3f64b 859 printk(KERN_ERR "asoc: platform %s hw params failed\n",
db2a4165
FM
860 platform->name);
861 goto platform_err;
862 }
863 }
864
f0fba2ad 865 rtd->rate = params_rate(params);
06f409d7 866
db2a4165
FM
867out:
868 mutex_unlock(&pcm_mutex);
869 return ret;
870
db2a4165 871platform_err:
f0fba2ad
LG
872 if (cpu_dai->driver->ops->hw_free)
873 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
db2a4165
FM
874
875interface_err:
f0fba2ad
LG
876 if (codec_dai->driver->ops->hw_free)
877 codec_dai->driver->ops->hw_free(substream, codec_dai);
cb666e5b
LG
878
879codec_err:
f0fba2ad
LG
880 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
881 rtd->dai_link->ops->hw_free(substream);
db2a4165
FM
882
883 mutex_unlock(&pcm_mutex);
884 return ret;
885}
886
887/*
a00f90f9 888 * Frees resources allocated by hw_params, can be called multiple times
db2a4165
FM
889 */
890static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
891{
892 struct snd_soc_pcm_runtime *rtd = substream->private_data;
f0fba2ad
LG
893 struct snd_soc_platform *platform = rtd->platform;
894 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
895 struct snd_soc_dai *codec_dai = rtd->codec_dai;
896 struct snd_soc_codec *codec = rtd->codec;
db2a4165
FM
897
898 mutex_lock(&pcm_mutex);
899
900 /* apply codec digital mute */
8c6529db
LG
901 if (!codec->active)
902 snd_soc_dai_digital_mute(codec_dai, 1);
db2a4165
FM
903
904 /* free any machine hw params */
f0fba2ad
LG
905 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
906 rtd->dai_link->ops->hw_free(substream);
db2a4165
FM
907
908 /* free any DMA resources */
f0fba2ad
LG
909 if (platform->driver->ops->hw_free)
910 platform->driver->ops->hw_free(substream);
db2a4165 911
a00f90f9 912 /* now free hw params for the DAIs */
f0fba2ad
LG
913 if (codec_dai->driver->ops->hw_free)
914 codec_dai->driver->ops->hw_free(substream, codec_dai);
db2a4165 915
f0fba2ad
LG
916 if (cpu_dai->driver->ops->hw_free)
917 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
db2a4165
FM
918
919 mutex_unlock(&pcm_mutex);
920 return 0;
921}
922
923static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
924{
925 struct snd_soc_pcm_runtime *rtd = substream->private_data;
f0fba2ad
LG
926 struct snd_soc_platform *platform = rtd->platform;
927 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
928 struct snd_soc_dai *codec_dai = rtd->codec_dai;
db2a4165
FM
929 int ret;
930
f0fba2ad
LG
931 if (codec_dai->driver->ops->trigger) {
932 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
db2a4165
FM
933 if (ret < 0)
934 return ret;
935 }
936
f0fba2ad
LG
937 if (platform->driver->ops->trigger) {
938 ret = platform->driver->ops->trigger(substream, cmd);
db2a4165
FM
939 if (ret < 0)
940 return ret;
941 }
942
f0fba2ad
LG
943 if (cpu_dai->driver->ops->trigger) {
944 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
db2a4165
FM
945 if (ret < 0)
946 return ret;
947 }
948 return 0;
949}
950
377b6f62
PU
951/*
952 * soc level wrapper for pointer callback
258020d0
PU
953 * If cpu_dai, codec_dai, platform driver has the delay callback, than
954 * the runtime->delay will be updated accordingly.
377b6f62
PU
955 */
956static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
957{
958 struct snd_soc_pcm_runtime *rtd = substream->private_data;
f0fba2ad
LG
959 struct snd_soc_platform *platform = rtd->platform;
960 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
961 struct snd_soc_dai *codec_dai = rtd->codec_dai;
258020d0 962 struct snd_pcm_runtime *runtime = substream->runtime;
377b6f62 963 snd_pcm_uframes_t offset = 0;
258020d0 964 snd_pcm_sframes_t delay = 0;
377b6f62 965
f0fba2ad
LG
966 if (platform->driver->ops->pointer)
967 offset = platform->driver->ops->pointer(substream);
377b6f62 968
f0fba2ad
LG
969 if (cpu_dai->driver->ops->delay)
970 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
258020d0 971
f0fba2ad
LG
972 if (codec_dai->driver->ops->delay)
973 delay += codec_dai->driver->ops->delay(substream, codec_dai);
258020d0 974
f0fba2ad
LG
975 if (platform->driver->delay)
976 delay += platform->driver->delay(substream, codec_dai);
258020d0
PU
977
978 runtime->delay = delay;
979
377b6f62
PU
980 return offset;
981}
982
db2a4165
FM
983/* ASoC PCM operations */
984static struct snd_pcm_ops soc_pcm_ops = {
985 .open = soc_pcm_open,
986 .close = soc_codec_close,
987 .hw_params = soc_pcm_hw_params,
988 .hw_free = soc_pcm_hw_free,
989 .prepare = soc_pcm_prepare,
990 .trigger = soc_pcm_trigger,
377b6f62 991 .pointer = soc_pcm_pointer,
db2a4165
FM
992};
993
6f8ab4ac 994#ifdef CONFIG_PM_SLEEP
db2a4165 995/* powers down audio subsystem for suspend */
6f8ab4ac 996int snd_soc_suspend(struct device *dev)
db2a4165 997{
6f8ab4ac 998 struct snd_soc_card *card = dev_get_drvdata(dev);
2eea392d 999 struct snd_soc_codec *codec;
db2a4165
FM
1000 int i;
1001
e3509ff0
DM
1002 /* If the initialization of this soc device failed, there is no codec
1003 * associated with it. Just bail out in this case.
1004 */
f0fba2ad 1005 if (list_empty(&card->codec_dev_list))
e3509ff0
DM
1006 return 0;
1007
6ed25978
AG
1008 /* Due to the resume being scheduled into a workqueue we could
1009 * suspend before that's finished - wait for it to complete.
1010 */
f0fba2ad
LG
1011 snd_power_lock(card->snd_card);
1012 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
1013 snd_power_unlock(card->snd_card);
6ed25978
AG
1014
1015 /* we're going to block userspace touching us until resume completes */
f0fba2ad 1016 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
6ed25978 1017
a00f90f9 1018 /* mute any active DACs */
f0fba2ad
LG
1019 for (i = 0; i < card->num_rtd; i++) {
1020 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1021 struct snd_soc_dai_driver *drv = dai->driver;
3efab7dc 1022
f0fba2ad 1023 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
1024 continue;
1025
f0fba2ad
LG
1026 if (drv->ops->digital_mute && dai->playback_active)
1027 drv->ops->digital_mute(dai, 1);
db2a4165
FM
1028 }
1029
4ccab3e7 1030 /* suspend all pcms */
f0fba2ad
LG
1031 for (i = 0; i < card->num_rtd; i++) {
1032 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
1033 continue;
1034
f0fba2ad 1035 snd_pcm_suspend_all(card->rtd[i].pcm);
3efab7dc 1036 }
4ccab3e7 1037
87506549 1038 if (card->suspend_pre)
70b2ac12 1039 card->suspend_pre(card);
db2a4165 1040
f0fba2ad
LG
1041 for (i = 0; i < card->num_rtd; i++) {
1042 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1043 struct snd_soc_platform *platform = card->rtd[i].platform;
3efab7dc 1044
f0fba2ad 1045 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
1046 continue;
1047
f0fba2ad
LG
1048 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
1049 cpu_dai->driver->suspend(cpu_dai);
1050 if (platform->driver->suspend && !platform->suspended) {
1051 platform->driver->suspend(cpu_dai);
1052 platform->suspended = 1;
1053 }
db2a4165
FM
1054 }
1055
1056 /* close any waiting streams and save state */
f0fba2ad 1057 for (i = 0; i < card->num_rtd; i++) {
5b84ba26 1058 flush_delayed_work_sync(&card->rtd[i].delayed_work);
ce6120cc 1059 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
f0fba2ad 1060 }
db2a4165 1061
f0fba2ad
LG
1062 for (i = 0; i < card->num_rtd; i++) {
1063 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
3efab7dc 1064
f0fba2ad 1065 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
1066 continue;
1067
f0fba2ad
LG
1068 if (driver->playback.stream_name != NULL)
1069 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
db2a4165 1070 SND_SOC_DAPM_STREAM_SUSPEND);
f0fba2ad
LG
1071
1072 if (driver->capture.stream_name != NULL)
1073 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
db2a4165
FM
1074 SND_SOC_DAPM_STREAM_SUSPEND);
1075 }
1076
f0fba2ad 1077 /* suspend all CODECs */
2eea392d 1078 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
f0fba2ad
LG
1079 /* If there are paths active then the CODEC will be held with
1080 * bias _ON and should not be suspended. */
1081 if (!codec->suspended && codec->driver->suspend) {
ce6120cc 1082 switch (codec->dapm.bias_level) {
f0fba2ad
LG
1083 case SND_SOC_BIAS_STANDBY:
1084 case SND_SOC_BIAS_OFF:
1085 codec->driver->suspend(codec, PMSG_SUSPEND);
1086 codec->suspended = 1;
1087 break;
1088 default:
1089 dev_dbg(codec->dev, "CODEC is on over suspend\n");
1090 break;
1091 }
1547aba9
MB
1092 }
1093 }
db2a4165 1094
f0fba2ad
LG
1095 for (i = 0; i < card->num_rtd; i++) {
1096 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3efab7dc 1097
f0fba2ad 1098 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
1099 continue;
1100
f0fba2ad
LG
1101 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
1102 cpu_dai->driver->suspend(cpu_dai);
db2a4165
FM
1103 }
1104
87506549 1105 if (card->suspend_post)
70b2ac12 1106 card->suspend_post(card);
db2a4165
FM
1107
1108 return 0;
1109}
6f8ab4ac 1110EXPORT_SYMBOL_GPL(snd_soc_suspend);
db2a4165 1111
6ed25978
AG
1112/* deferred resume work, so resume can complete before we finished
1113 * setting our codec back up, which can be very slow on I2C
1114 */
1115static void soc_resume_deferred(struct work_struct *work)
db2a4165 1116{
f0fba2ad
LG
1117 struct snd_soc_card *card =
1118 container_of(work, struct snd_soc_card, deferred_resume_work);
2eea392d 1119 struct snd_soc_codec *codec;
db2a4165
FM
1120 int i;
1121
6ed25978
AG
1122 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1123 * so userspace apps are blocked from touching us
1124 */
1125
f0fba2ad 1126 dev_dbg(card->dev, "starting resume work\n");
6ed25978 1127
9949788b 1128 /* Bring us up into D2 so that DAPM starts enabling things */
f0fba2ad 1129 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
9949788b 1130
87506549 1131 if (card->resume_pre)
70b2ac12 1132 card->resume_pre(card);
db2a4165 1133
f0fba2ad
LG
1134 /* resume AC97 DAIs */
1135 for (i = 0; i < card->num_rtd; i++) {
1136 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3efab7dc 1137
f0fba2ad 1138 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
1139 continue;
1140
f0fba2ad
LG
1141 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
1142 cpu_dai->driver->resume(cpu_dai);
1143 }
1144
2eea392d 1145 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
f0fba2ad
LG
1146 /* If the CODEC was idle over suspend then it will have been
1147 * left with bias OFF or STANDBY and suspended so we must now
1148 * resume. Otherwise the suspend was suppressed.
1149 */
1150 if (codec->driver->resume && codec->suspended) {
ce6120cc 1151 switch (codec->dapm.bias_level) {
f0fba2ad
LG
1152 case SND_SOC_BIAS_STANDBY:
1153 case SND_SOC_BIAS_OFF:
1154 codec->driver->resume(codec);
1155 codec->suspended = 0;
1156 break;
1157 default:
1158 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1159 break;
1160 }
1547aba9
MB
1161 }
1162 }
db2a4165 1163
f0fba2ad
LG
1164 for (i = 0; i < card->num_rtd; i++) {
1165 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
3efab7dc 1166
f0fba2ad 1167 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
1168 continue;
1169
f0fba2ad
LG
1170 if (driver->playback.stream_name != NULL)
1171 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
db2a4165 1172 SND_SOC_DAPM_STREAM_RESUME);
f0fba2ad
LG
1173
1174 if (driver->capture.stream_name != NULL)
1175 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
db2a4165
FM
1176 SND_SOC_DAPM_STREAM_RESUME);
1177 }
1178
3ff3f64b 1179 /* unmute any active DACs */
f0fba2ad
LG
1180 for (i = 0; i < card->num_rtd; i++) {
1181 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1182 struct snd_soc_dai_driver *drv = dai->driver;
3efab7dc 1183
f0fba2ad 1184 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
1185 continue;
1186
f0fba2ad
LG
1187 if (drv->ops->digital_mute && dai->playback_active)
1188 drv->ops->digital_mute(dai, 0);
db2a4165
FM
1189 }
1190
f0fba2ad
LG
1191 for (i = 0; i < card->num_rtd; i++) {
1192 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1193 struct snd_soc_platform *platform = card->rtd[i].platform;
3efab7dc 1194
f0fba2ad 1195 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
1196 continue;
1197
f0fba2ad
LG
1198 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1199 cpu_dai->driver->resume(cpu_dai);
1200 if (platform->driver->resume && platform->suspended) {
1201 platform->driver->resume(cpu_dai);
1202 platform->suspended = 0;
1203 }
db2a4165
FM
1204 }
1205
87506549 1206 if (card->resume_post)
70b2ac12 1207 card->resume_post(card);
db2a4165 1208
f0fba2ad 1209 dev_dbg(card->dev, "resume work completed\n");
6ed25978
AG
1210
1211 /* userspace can access us now we are back as we were before */
f0fba2ad 1212 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
6ed25978
AG
1213}
1214
1215/* powers up audio subsystem after a suspend */
6f8ab4ac 1216int snd_soc_resume(struct device *dev)
6ed25978 1217{
6f8ab4ac 1218 struct snd_soc_card *card = dev_get_drvdata(dev);
f0fba2ad 1219 int i;
b9dd94a8 1220
64ab9baa
MB
1221 /* AC97 devices might have other drivers hanging off them so
1222 * need to resume immediately. Other drivers don't have that
1223 * problem and may take a substantial amount of time to resume
1224 * due to I/O costs and anti-pop so handle them out of line.
1225 */
f0fba2ad
LG
1226 for (i = 0; i < card->num_rtd; i++) {
1227 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1228 if (cpu_dai->driver->ac97_control) {
1229 dev_dbg(dev, "Resuming AC97 immediately\n");
1230 soc_resume_deferred(&card->deferred_resume_work);
1231 } else {
1232 dev_dbg(dev, "Scheduling resume work\n");
1233 if (!schedule_work(&card->deferred_resume_work))
1234 dev_err(dev, "resume work item may be lost\n");
1235 }
64ab9baa 1236 }
6ed25978 1237
db2a4165
FM
1238 return 0;
1239}
6f8ab4ac 1240EXPORT_SYMBOL_GPL(snd_soc_resume);
db2a4165 1241#else
6f8ab4ac
MB
1242#define snd_soc_suspend NULL
1243#define snd_soc_resume NULL
db2a4165
FM
1244#endif
1245
02a06d30
BS
1246static struct snd_soc_dai_ops null_dai_ops = {
1247};
1248
f0fba2ad 1249static int soc_bind_dai_link(struct snd_soc_card *card, int num)
db2a4165 1250{
f0fba2ad
LG
1251 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1252 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
fe3e78e0 1253 struct snd_soc_codec *codec;
435c5e25 1254 struct snd_soc_platform *platform;
f0fba2ad 1255 struct snd_soc_dai *codec_dai, *cpu_dai;
435c5e25 1256
f0fba2ad
LG
1257 if (rtd->complete)
1258 return 1;
1259 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
435c5e25 1260
f0fba2ad
LG
1261 /* do we already have the CPU DAI for this link ? */
1262 if (rtd->cpu_dai) {
1263 goto find_codec;
1264 }
1265 /* no, then find CPU DAI from registered DAIs*/
1266 list_for_each_entry(cpu_dai, &dai_list, list) {
1267 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1268
1269 if (!try_module_get(cpu_dai->dev->driver->owner))
1270 return -ENODEV;
1271
1272 rtd->cpu_dai = cpu_dai;
1273 goto find_codec;
435c5e25 1274 }
435c5e25 1275 }
f0fba2ad
LG
1276 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1277 dai_link->cpu_dai_name);
6308419a 1278
f0fba2ad
LG
1279find_codec:
1280 /* do we already have the CODEC for this link ? */
1281 if (rtd->codec) {
1282 goto find_platform;
1283 }
1284
1285 /* no, then find CODEC from registered CODECs*/
1286 list_for_each_entry(codec, &codec_list, list) {
1287 if (!strcmp(codec->name, dai_link->codec_name)) {
1288 rtd->codec = codec;
1289
f0fba2ad
LG
1290 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1291 list_for_each_entry(codec_dai, &dai_list, list) {
1292 if (codec->dev == codec_dai->dev &&
1293 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1294 rtd->codec_dai = codec_dai;
1295 goto find_platform;
1296 }
435c5e25 1297 }
f0fba2ad
LG
1298 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1299 dai_link->codec_dai_name);
1300
1301 goto find_platform;
435c5e25 1302 }
f0fba2ad
LG
1303 }
1304 dev_dbg(card->dev, "CODEC %s not registered\n",
1305 dai_link->codec_name);
6b05eda6 1306
f0fba2ad
LG
1307find_platform:
1308 /* do we already have the CODEC DAI for this link ? */
1309 if (rtd->platform) {
1310 goto out;
c5af3a2e 1311 }
f0fba2ad
LG
1312 /* no, then find CPU DAI from registered DAIs*/
1313 list_for_each_entry(platform, &platform_list, list) {
1314 if (!strcmp(platform->name, dai_link->platform_name)) {
f0fba2ad
LG
1315 rtd->platform = platform;
1316 goto out;
1317 }
02a06d30
BS
1318 }
1319
f0fba2ad
LG
1320 dev_dbg(card->dev, "platform %s not registered\n",
1321 dai_link->platform_name);
1322 return 0;
1323
1324out:
1325 /* mark rtd as complete if we found all 4 of our client devices */
1326 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1327 rtd->complete = 1;
1328 card->num_rtd++;
1329 }
1330 return 1;
1331}
1332
589c3563
JN
1333static void soc_remove_codec(struct snd_soc_codec *codec)
1334{
1335 int err;
1336
1337 if (codec->driver->remove) {
1338 err = codec->driver->remove(codec);
1339 if (err < 0)
1340 dev_err(codec->dev,
1341 "asoc: failed to remove %s: %d\n",
1342 codec->name, err);
1343 }
1344
1345 /* Make sure all DAPM widgets are freed */
1346 snd_soc_dapm_free(&codec->dapm);
1347
1348 soc_cleanup_codec_debugfs(codec);
1349 codec->probed = 0;
1350 list_del(&codec->card_list);
1351 module_put(codec->dev->driver->owner);
1352}
1353
f0fba2ad
LG
1354static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1355{
1356 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1357 struct snd_soc_codec *codec = rtd->codec;
1358 struct snd_soc_platform *platform = rtd->platform;
1359 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1360 int err;
1361
1362 /* unregister the rtd device */
1363 if (rtd->dev_registered) {
1364 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
589c3563 1365 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
f0fba2ad
LG
1366 device_unregister(&rtd->dev);
1367 rtd->dev_registered = 0;
1368 }
1369
1370 /* remove the CODEC DAI */
1371 if (codec_dai && codec_dai->probed) {
1372 if (codec_dai->driver->remove) {
1373 err = codec_dai->driver->remove(codec_dai);
1374 if (err < 0)
1375 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
6b05eda6 1376 }
f0fba2ad
LG
1377 codec_dai->probed = 0;
1378 list_del(&codec_dai->card_list);
1379 }
6b05eda6 1380
f0fba2ad
LG
1381 /* remove the platform */
1382 if (platform && platform->probed) {
1383 if (platform->driver->remove) {
1384 err = platform->driver->remove(platform);
1385 if (err < 0)
1386 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1387 }
1388 platform->probed = 0;
1389 list_del(&platform->card_list);
1390 module_put(platform->dev->driver->owner);
1391 }
435c5e25 1392
f0fba2ad 1393 /* remove the CODEC */
589c3563
JN
1394 if (codec && codec->probed)
1395 soc_remove_codec(codec);
db2a4165 1396
f0fba2ad
LG
1397 /* remove the cpu_dai */
1398 if (cpu_dai && cpu_dai->probed) {
1399 if (cpu_dai->driver->remove) {
1400 err = cpu_dai->driver->remove(cpu_dai);
1401 if (err < 0)
1402 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
db2a4165 1403 }
f0fba2ad
LG
1404 cpu_dai->probed = 0;
1405 list_del(&cpu_dai->card_list);
1406 module_put(cpu_dai->dev->driver->owner);
db2a4165 1407 }
f0fba2ad 1408}
db2a4165 1409
ead9b919
JN
1410static void soc_set_name_prefix(struct snd_soc_card *card,
1411 struct snd_soc_codec *codec)
1412{
1413 int i;
1414
ff819b83 1415 if (card->codec_conf == NULL)
ead9b919
JN
1416 return;
1417
ff819b83
DP
1418 for (i = 0; i < card->num_configs; i++) {
1419 struct snd_soc_codec_conf *map = &card->codec_conf[i];
ead9b919
JN
1420 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1421 codec->name_prefix = map->name_prefix;
1422 break;
1423 }
1424 }
1425}
1426
589c3563
JN
1427static int soc_probe_codec(struct snd_soc_card *card,
1428 struct snd_soc_codec *codec)
1429{
1430 int ret = 0;
1431
1432 codec->card = card;
1433 codec->dapm.card = card;
1434 soc_set_name_prefix(card, codec);
1435
70d29331
JN
1436 if (!try_module_get(codec->dev->driver->owner))
1437 return -ENODEV;
1438
589c3563
JN
1439 if (codec->driver->probe) {
1440 ret = codec->driver->probe(codec);
1441 if (ret < 0) {
1442 dev_err(codec->dev,
1443 "asoc: failed to probe CODEC %s: %d\n",
1444 codec->name, ret);
70d29331 1445 goto err_probe;
589c3563
JN
1446 }
1447 }
1448
1449 soc_init_codec_debugfs(codec);
1450
1451 /* mark codec as probed and add to card codec list */
1452 codec->probed = 1;
1453 list_add(&codec->card_list, &card->codec_dev_list);
7be31be8 1454 list_add(&codec->dapm.list, &card->dapm_list);
589c3563 1455
70d29331
JN
1456 return 0;
1457
1458err_probe:
1459 module_put(codec->dev->driver->owner);
1460
589c3563
JN
1461 return ret;
1462}
1463
f0fba2ad
LG
1464static void rtd_release(struct device *dev) {}
1465
589c3563
JN
1466static int soc_post_component_init(struct snd_soc_card *card,
1467 struct snd_soc_codec *codec,
1468 int num, int dailess)
1469{
1470 struct snd_soc_dai_link *dai_link = NULL;
1471 struct snd_soc_aux_dev *aux_dev = NULL;
1472 struct snd_soc_pcm_runtime *rtd;
1473 const char *temp, *name;
1474 int ret = 0;
1475
1476 if (!dailess) {
1477 dai_link = &card->dai_link[num];
1478 rtd = &card->rtd[num];
1479 name = dai_link->name;
1480 } else {
1481 aux_dev = &card->aux_dev[num];
1482 rtd = &card->rtd_aux[num];
1483 name = aux_dev->name;
1484 }
1485
1486 /* machine controls, routes and widgets are not prefixed */
1487 temp = codec->name_prefix;
1488 codec->name_prefix = NULL;
1489
1490 /* do machine specific initialization */
1491 if (!dailess && dai_link->init)
1492 ret = dai_link->init(rtd);
1493 else if (dailess && aux_dev->init)
1494 ret = aux_dev->init(&codec->dapm);
1495 if (ret < 0) {
1496 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1497 return ret;
1498 }
1499 codec->name_prefix = temp;
1500
1501 /* Make sure all DAPM widgets are instantiated */
1502 snd_soc_dapm_new_widgets(&codec->dapm);
589c3563
JN
1503
1504 /* register the rtd device */
1505 rtd->codec = codec;
1506 rtd->card = card;
1507 rtd->dev.parent = card->dev;
1508 rtd->dev.release = rtd_release;
1509 rtd->dev.init_name = name;
1510 ret = device_register(&rtd->dev);
1511 if (ret < 0) {
1512 dev_err(card->dev,
1513 "asoc: failed to register runtime device: %d\n", ret);
1514 return ret;
1515 }
1516 rtd->dev_registered = 1;
1517
1518 /* add DAPM sysfs entries for this codec */
1519 ret = snd_soc_dapm_sys_add(&rtd->dev);
1520 if (ret < 0)
1521 dev_err(codec->dev,
1522 "asoc: failed to add codec dapm sysfs entries: %d\n",
1523 ret);
1524
1525 /* add codec sysfs entries */
1526 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1527 if (ret < 0)
1528 dev_err(codec->dev,
1529 "asoc: failed to add codec sysfs files: %d\n", ret);
1530
1531 return 0;
1532}
1533
f0fba2ad
LG
1534static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1535{
1536 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1537 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1538 struct snd_soc_codec *codec = rtd->codec;
1539 struct snd_soc_platform *platform = rtd->platform;
1540 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1541 int ret;
1542
1543 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1544
1545 /* config components */
1546 codec_dai->codec = codec;
f0fba2ad 1547 cpu_dai->platform = platform;
f0fba2ad
LG
1548 codec_dai->card = card;
1549 cpu_dai->card = card;
1550
1551 /* set default power off timeout */
1552 rtd->pmdown_time = pmdown_time;
1553
1554 /* probe the cpu_dai */
1555 if (!cpu_dai->probed) {
1556 if (cpu_dai->driver->probe) {
1557 ret = cpu_dai->driver->probe(cpu_dai);
1558 if (ret < 0) {
1559 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1560 cpu_dai->name);
1561 return ret;
1562 }
1563 }
1564 cpu_dai->probed = 1;
1565 /* mark cpu_dai as probed and add to card cpu_dai list */
1566 list_add(&cpu_dai->card_list, &card->dai_dev_list);
db2a4165
FM
1567 }
1568
f0fba2ad
LG
1569 /* probe the CODEC */
1570 if (!codec->probed) {
589c3563
JN
1571 ret = soc_probe_codec(card, codec);
1572 if (ret < 0)
1573 return ret;
db2a4165
FM
1574 }
1575
f0fba2ad
LG
1576 /* probe the platform */
1577 if (!platform->probed) {
70d29331
JN
1578 if (!try_module_get(platform->dev->driver->owner))
1579 return -ENODEV;
1580
f0fba2ad
LG
1581 if (platform->driver->probe) {
1582 ret = platform->driver->probe(platform);
1583 if (ret < 0) {
1584 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1585 platform->name);
70d29331 1586 module_put(platform->dev->driver->owner);
f0fba2ad
LG
1587 return ret;
1588 }
1589 }
1590 /* mark platform as probed and add to card platform list */
1591 platform->probed = 1;
1592 list_add(&platform->card_list, &card->platform_dev_list);
1593 }
6ed25978 1594
f0fba2ad
LG
1595 /* probe the CODEC DAI */
1596 if (!codec_dai->probed) {
1597 if (codec_dai->driver->probe) {
1598 ret = codec_dai->driver->probe(codec_dai);
fe3e78e0 1599 if (ret < 0) {
f0fba2ad
LG
1600 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1601 codec_dai->name);
1602 return ret;
fe3e78e0
MB
1603 }
1604 }
f0fba2ad
LG
1605
1606 /* mark cpu_dai as probed and add to card cpu_dai list */
1607 codec_dai->probed = 1;
1608 list_add(&codec_dai->card_list, &card->dai_dev_list);
fe3e78e0
MB
1609 }
1610
f0fba2ad
LG
1611 /* DAPM dai link stream work */
1612 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1613
589c3563
JN
1614 ret = soc_post_component_init(card, codec, num, 0);
1615 if (ret)
f0fba2ad 1616 return ret;
fe3e78e0 1617
f0fba2ad
LG
1618 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1619 if (ret < 0)
1620 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1621
f0fba2ad
LG
1622 /* create the pcm */
1623 ret = soc_new_pcm(rtd, num);
1624 if (ret < 0) {
1625 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1626 return ret;
1627 }
1628
1629 /* add platform data for AC97 devices */
1630 if (rtd->codec_dai->driver->ac97_control)
1631 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1632
1633 return 0;
1634}
1635
fe3e78e0 1636#ifdef CONFIG_SND_SOC_AC97_BUS
f0fba2ad
LG
1637static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1638{
1639 int ret;
1640
fe3e78e0
MB
1641 /* Only instantiate AC97 if not already done by the adaptor
1642 * for the generic AC97 subsystem.
1643 */
f0fba2ad 1644 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
0562f788
MW
1645 /*
1646 * It is possible that the AC97 device is already registered to
1647 * the device subsystem. This happens when the device is created
1648 * via snd_ac97_mixer(). Currently only SoC codec that does so
1649 * is the generic AC97 glue but others migh emerge.
1650 *
1651 * In those cases we don't try to register the device again.
1652 */
1653 if (!rtd->codec->ac97_created)
1654 return 0;
f0fba2ad
LG
1655
1656 ret = soc_ac97_dev_register(rtd->codec);
fe3e78e0
MB
1657 if (ret < 0) {
1658 printk(KERN_ERR "asoc: AC97 device register failed\n");
f0fba2ad 1659 return ret;
fe3e78e0 1660 }
f0fba2ad
LG
1661
1662 rtd->codec->ac97_registered = 1;
fe3e78e0 1663 }
f0fba2ad
LG
1664 return 0;
1665}
1666
1667static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1668{
1669 if (codec->ac97_registered) {
1670 soc_ac97_dev_unregister(codec);
1671 codec->ac97_registered = 0;
1672 }
1673}
fe3e78e0
MB
1674#endif
1675
2eea392d
JN
1676static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1677{
1678 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
2eea392d 1679 struct snd_soc_codec *codec;
676ad98a 1680 int ret = -ENODEV;
2eea392d
JN
1681
1682 /* find CODEC from registered CODECs*/
1683 list_for_each_entry(codec, &codec_list, list) {
1684 if (!strcmp(codec->name, aux_dev->codec_name)) {
1685 if (codec->probed) {
1686 dev_err(codec->dev,
1687 "asoc: codec already probed");
1688 ret = -EBUSY;
1689 goto out;
1690 }
676ad98a 1691 goto found;
2eea392d
JN
1692 }
1693 }
676ad98a
JN
1694 /* codec not found */
1695 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1696 goto out;
2eea392d 1697
676ad98a 1698found:
589c3563 1699 ret = soc_probe_codec(card, codec);
2eea392d 1700 if (ret < 0)
589c3563 1701 return ret;
2eea392d 1702
589c3563 1703 ret = soc_post_component_init(card, codec, num, 1);
2eea392d
JN
1704
1705out:
1706 return ret;
1707}
1708
1709static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1710{
1711 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1712 struct snd_soc_codec *codec = rtd->codec;
2eea392d
JN
1713
1714 /* unregister the rtd device */
1715 if (rtd->dev_registered) {
589c3563 1716 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
2eea392d
JN
1717 device_unregister(&rtd->dev);
1718 rtd->dev_registered = 0;
1719 }
1720
589c3563
JN
1721 if (codec && codec->probed)
1722 soc_remove_codec(codec);
2eea392d
JN
1723}
1724
fdf0f54d
DP
1725static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1726 enum snd_soc_compress_type compress_type)
1727{
1728 int ret;
1729
1730 if (codec->cache_init)
1731 return 0;
1732
1733 /* override the compress_type if necessary */
1734 if (compress_type && codec->compress_type != compress_type)
1735 codec->compress_type = compress_type;
fdf0f54d
DP
1736 ret = snd_soc_cache_init(codec);
1737 if (ret < 0) {
1738 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1739 ret);
1740 return ret;
1741 }
1742 codec->cache_init = 1;
1743 return 0;
1744}
1745
f0fba2ad
LG
1746static void snd_soc_instantiate_card(struct snd_soc_card *card)
1747{
fdf0f54d
DP
1748 struct snd_soc_codec *codec;
1749 struct snd_soc_codec_conf *codec_conf;
1750 enum snd_soc_compress_type compress_type;
f0fba2ad 1751 int ret, i;
fe3e78e0 1752
f0fba2ad 1753 mutex_lock(&card->mutex);
dbe21408 1754
f0fba2ad
LG
1755 if (card->instantiated) {
1756 mutex_unlock(&card->mutex);
1757 return;
1758 }
fe3e78e0 1759
f0fba2ad
LG
1760 /* bind DAIs */
1761 for (i = 0; i < card->num_links; i++)
1762 soc_bind_dai_link(card, i);
fe3e78e0 1763
f0fba2ad
LG
1764 /* bind completed ? */
1765 if (card->num_rtd != card->num_links) {
1766 mutex_unlock(&card->mutex);
1767 return;
1768 }
435c5e25 1769
fdf0f54d
DP
1770 /* initialize the register cache for each available codec */
1771 list_for_each_entry(codec, &codec_list, list) {
1772 if (codec->cache_init)
1773 continue;
861f2faf
DP
1774 /* by default we don't override the compress_type */
1775 compress_type = 0;
fdf0f54d
DP
1776 /* check to see if we need to override the compress_type */
1777 for (i = 0; i < card->num_configs; ++i) {
1778 codec_conf = &card->codec_conf[i];
1779 if (!strcmp(codec->name, codec_conf->dev_name)) {
1780 compress_type = codec_conf->compress_type;
1781 if (compress_type && compress_type
1782 != codec->compress_type)
1783 break;
1784 }
1785 }
fdf0f54d
DP
1786 ret = snd_soc_init_codec_cache(codec, compress_type);
1787 if (ret < 0) {
1788 mutex_unlock(&card->mutex);
1789 return;
1790 }
1791 }
1792
f0fba2ad
LG
1793 /* card bind complete so register a sound card */
1794 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1795 card->owner, 0, &card->snd_card);
1796 if (ret < 0) {
1797 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1798 card->name);
1799 mutex_unlock(&card->mutex);
1800 return;
1801 }
1802 card->snd_card->dev = card->dev;
1803
88ee1c61 1804#ifdef CONFIG_PM_SLEEP
f0fba2ad
LG
1805 /* deferred resume work */
1806 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1807#endif
db2a4165 1808
f0fba2ad
LG
1809 /* initialise the sound card only once */
1810 if (card->probe) {
e7361ec4 1811 ret = card->probe(card);
f0fba2ad
LG
1812 if (ret < 0)
1813 goto card_probe_error;
1814 }
fe3e78e0 1815
f0fba2ad
LG
1816 for (i = 0; i < card->num_links; i++) {
1817 ret = soc_probe_dai_link(card, i);
1818 if (ret < 0) {
321de0d0
MB
1819 pr_err("asoc: failed to instantiate card %s: %d\n",
1820 card->name, ret);
f0fba2ad
LG
1821 goto probe_dai_err;
1822 }
1823 }
db2a4165 1824
2eea392d
JN
1825 for (i = 0; i < card->num_aux_devs; i++) {
1826 ret = soc_probe_aux_dev(card, i);
1827 if (ret < 0) {
1828 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1829 card->name, ret);
1830 goto probe_aux_dev_err;
1831 }
1832 }
1833
f0fba2ad
LG
1834 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1835 "%s", card->name);
1836 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1837 "%s", card->name);
1838
1839 ret = snd_card_register(card->snd_card);
1840 if (ret < 0) {
1841 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
6b3ed785 1842 goto probe_aux_dev_err;
db2a4165
FM
1843 }
1844
f0fba2ad
LG
1845#ifdef CONFIG_SND_SOC_AC97_BUS
1846 /* register any AC97 codecs */
1847 for (i = 0; i < card->num_rtd; i++) {
6b3ed785
AL
1848 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1849 if (ret < 0) {
1850 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1851 while (--i >= 0)
7d8316df 1852 soc_unregister_ac97_dai_link(card->rtd[i].codec);
6b3ed785 1853 goto probe_aux_dev_err;
f0fba2ad 1854 }
6b3ed785 1855 }
f0fba2ad
LG
1856#endif
1857
1858 card->instantiated = 1;
1859 mutex_unlock(&card->mutex);
1860 return;
1861
2eea392d
JN
1862probe_aux_dev_err:
1863 for (i = 0; i < card->num_aux_devs; i++)
1864 soc_remove_aux_dev(card, i);
1865
f0fba2ad
LG
1866probe_dai_err:
1867 for (i = 0; i < card->num_links; i++)
1868 soc_remove_dai_link(card, i);
1869
1870card_probe_error:
87506549 1871 if (card->remove)
e7361ec4 1872 card->remove(card);
f0fba2ad
LG
1873
1874 snd_card_free(card->snd_card);
1875
1876 mutex_unlock(&card->mutex);
435c5e25 1877}
db2a4165 1878
435c5e25 1879/*
421f91d2 1880 * Attempt to initialise any uninitialised cards. Must be called with
435c5e25
MB
1881 * client_mutex.
1882 */
1883static void snd_soc_instantiate_cards(void)
1884{
1885 struct snd_soc_card *card;
1886 list_for_each_entry(card, &card_list, list)
1887 snd_soc_instantiate_card(card);
1888}
1889
1890/* probes a new socdev */
1891static int soc_probe(struct platform_device *pdev)
1892{
f0fba2ad 1893 struct snd_soc_card *card = platform_get_drvdata(pdev);
435c5e25 1894 int ret = 0;
435c5e25 1895
70a7ca34
VK
1896 /*
1897 * no card, so machine driver should be registering card
1898 * we should not be here in that case so ret error
1899 */
1900 if (!card)
1901 return -EINVAL;
1902
435c5e25
MB
1903 /* Bodge while we unpick instantiation */
1904 card->dev = &pdev->dev;
f0fba2ad 1905
435c5e25
MB
1906 ret = snd_soc_register_card(card);
1907 if (ret != 0) {
1908 dev_err(&pdev->dev, "Failed to register card\n");
1909 return ret;
1910 }
1911
1912 return 0;
db2a4165
FM
1913}
1914
b0e26485 1915static int soc_cleanup_card_resources(struct snd_soc_card *card)
db2a4165
FM
1916{
1917 int i;
db2a4165 1918
b0e26485
VK
1919 /* make sure any delayed work runs */
1920 for (i = 0; i < card->num_rtd; i++) {
1921 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1922 flush_delayed_work_sync(&rtd->delayed_work);
1923 }
914dc182 1924
b0e26485
VK
1925 /* remove auxiliary devices */
1926 for (i = 0; i < card->num_aux_devs; i++)
1927 soc_remove_aux_dev(card, i);
db2a4165 1928
b0e26485
VK
1929 /* remove and free each DAI */
1930 for (i = 0; i < card->num_rtd; i++)
1931 soc_remove_dai_link(card, i);
2eea392d 1932
b0e26485 1933 soc_cleanup_card_debugfs(card);
f0fba2ad 1934
b0e26485
VK
1935 /* remove the card */
1936 if (card->remove)
e7361ec4 1937 card->remove(card);
a6052154 1938
b0e26485
VK
1939 kfree(card->rtd);
1940 snd_card_free(card->snd_card);
1941 return 0;
1942
1943}
1944
1945/* removes a socdev */
1946static int soc_remove(struct platform_device *pdev)
1947{
1948 struct snd_soc_card *card = platform_get_drvdata(pdev);
db2a4165 1949
c5af3a2e 1950 snd_soc_unregister_card(card);
db2a4165
FM
1951 return 0;
1952}
1953
6f8ab4ac 1954int snd_soc_poweroff(struct device *dev)
51737470 1955{
6f8ab4ac 1956 struct snd_soc_card *card = dev_get_drvdata(dev);
f0fba2ad 1957 int i;
51737470
MB
1958
1959 if (!card->instantiated)
416356fc 1960 return 0;
51737470
MB
1961
1962 /* Flush out pmdown_time work - we actually do want to run it
1963 * now, we're shutting down so no imminent restart. */
f0fba2ad
LG
1964 for (i = 0; i < card->num_rtd; i++) {
1965 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
5b84ba26 1966 flush_delayed_work_sync(&rtd->delayed_work);
f0fba2ad 1967 }
51737470 1968
f0fba2ad 1969 snd_soc_dapm_shutdown(card);
416356fc
MB
1970
1971 return 0;
51737470 1972}
6f8ab4ac 1973EXPORT_SYMBOL_GPL(snd_soc_poweroff);
51737470 1974
6f8ab4ac
MB
1975const struct dev_pm_ops snd_soc_pm_ops = {
1976 .suspend = snd_soc_suspend,
1977 .resume = snd_soc_resume,
1978 .poweroff = snd_soc_poweroff,
416356fc
MB
1979};
1980
db2a4165
FM
1981/* ASoC platform driver */
1982static struct platform_driver soc_driver = {
1983 .driver = {
1984 .name = "soc-audio",
8b45a209 1985 .owner = THIS_MODULE,
6f8ab4ac 1986 .pm = &snd_soc_pm_ops,
db2a4165
FM
1987 },
1988 .probe = soc_probe,
1989 .remove = soc_remove,
db2a4165
FM
1990};
1991
1992/* create a new pcm */
f0fba2ad
LG
1993static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
1994{
1995 struct snd_soc_codec *codec = rtd->codec;
1996 struct snd_soc_platform *platform = rtd->platform;
1997 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1998 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
db2a4165
FM
1999 struct snd_pcm *pcm;
2000 char new_name[64];
2001 int ret = 0, playback = 0, capture = 0;
2002
db2a4165 2003 /* check client and interface hw capabilities */
40ca1142 2004 snprintf(new_name, sizeof(new_name), "%s %s-%d",
f0fba2ad 2005 rtd->dai_link->stream_name, codec_dai->name, num);
db2a4165 2006
f0fba2ad 2007 if (codec_dai->driver->playback.channels_min)
db2a4165 2008 playback = 1;
f0fba2ad 2009 if (codec_dai->driver->capture.channels_min)
db2a4165
FM
2010 capture = 1;
2011
f0fba2ad
LG
2012 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
2013 ret = snd_pcm_new(rtd->card->snd_card, new_name,
2014 num, playback, capture, &pcm);
db2a4165 2015 if (ret < 0) {
f0fba2ad 2016 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
db2a4165
FM
2017 return ret;
2018 }
2019
f0fba2ad 2020 rtd->pcm = pcm;
db2a4165 2021 pcm->private_data = rtd;
f0fba2ad
LG
2022 soc_pcm_ops.mmap = platform->driver->ops->mmap;
2023 soc_pcm_ops.pointer = platform->driver->ops->pointer;
2024 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
2025 soc_pcm_ops.copy = platform->driver->ops->copy;
2026 soc_pcm_ops.silence = platform->driver->ops->silence;
2027 soc_pcm_ops.ack = platform->driver->ops->ack;
2028 soc_pcm_ops.page = platform->driver->ops->page;
db2a4165
FM
2029
2030 if (playback)
2031 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
2032
2033 if (capture)
2034 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
2035
f0fba2ad 2036 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
db2a4165
FM
2037 if (ret < 0) {
2038 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
db2a4165
FM
2039 return ret;
2040 }
2041
f0fba2ad 2042 pcm->private_free = platform->driver->pcm_free;
db2a4165
FM
2043 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
2044 cpu_dai->name);
2045 return ret;
2046}
2047
096e49d5
MB
2048/**
2049 * snd_soc_codec_volatile_register: Report if a register is volatile.
2050 *
2051 * @codec: CODEC to query.
2052 * @reg: Register to query.
2053 *
2054 * Boolean function indiciating if a CODEC register is volatile.
2055 */
181e055e
MB
2056int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
2057 unsigned int reg)
096e49d5 2058{
1500b7b5
DP
2059 if (codec->volatile_register)
2060 return codec->volatile_register(codec, reg);
096e49d5
MB
2061 else
2062 return 0;
2063}
2064EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
2065
db2a4165
FM
2066/**
2067 * snd_soc_new_ac97_codec - initailise AC97 device
2068 * @codec: audio codec
2069 * @ops: AC97 bus operations
2070 * @num: AC97 codec number
2071 *
2072 * Initialises AC97 codec resources for use by ad-hoc devices only.
2073 */
2074int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2075 struct snd_ac97_bus_ops *ops, int num)
2076{
2077 mutex_lock(&codec->mutex);
2078
2079 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2080 if (codec->ac97 == NULL) {
2081 mutex_unlock(&codec->mutex);
2082 return -ENOMEM;
2083 }
2084
2085 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2086 if (codec->ac97->bus == NULL) {
2087 kfree(codec->ac97);
2088 codec->ac97 = NULL;
2089 mutex_unlock(&codec->mutex);
2090 return -ENOMEM;
2091 }
2092
2093 codec->ac97->bus->ops = ops;
2094 codec->ac97->num = num;
0562f788
MW
2095
2096 /*
2097 * Mark the AC97 device to be created by us. This way we ensure that the
2098 * device will be registered with the device subsystem later on.
2099 */
2100 codec->ac97_created = 1;
2101
db2a4165
FM
2102 mutex_unlock(&codec->mutex);
2103 return 0;
2104}
2105EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2106
2107/**
2108 * snd_soc_free_ac97_codec - free AC97 codec device
2109 * @codec: audio codec
2110 *
2111 * Frees AC97 codec device resources.
2112 */
2113void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2114{
2115 mutex_lock(&codec->mutex);
f0fba2ad
LG
2116#ifdef CONFIG_SND_SOC_AC97_BUS
2117 soc_unregister_ac97_dai_link(codec);
2118#endif
db2a4165
FM
2119 kfree(codec->ac97->bus);
2120 kfree(codec->ac97);
2121 codec->ac97 = NULL;
0562f788 2122 codec->ac97_created = 0;
db2a4165
FM
2123 mutex_unlock(&codec->mutex);
2124}
2125EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2126
c3753707
MB
2127unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2128{
2129 unsigned int ret;
2130
c3acec26 2131 ret = codec->read(codec, reg);
c3753707 2132 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
a8b1d34f 2133 trace_snd_soc_reg_read(codec, reg, ret);
c3753707
MB
2134
2135 return ret;
2136}
2137EXPORT_SYMBOL_GPL(snd_soc_read);
2138
2139unsigned int snd_soc_write(struct snd_soc_codec *codec,
2140 unsigned int reg, unsigned int val)
2141{
2142 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
a8b1d34f 2143 trace_snd_soc_reg_write(codec, reg, val);
c3acec26 2144 return codec->write(codec, reg, val);
c3753707
MB
2145}
2146EXPORT_SYMBOL_GPL(snd_soc_write);
2147
db2a4165
FM
2148/**
2149 * snd_soc_update_bits - update codec register bits
2150 * @codec: audio codec
2151 * @reg: codec register
2152 * @mask: register mask
2153 * @value: new value
2154 *
2155 * Writes new register value.
2156 *
180c329d 2157 * Returns 1 for change, 0 for no change, or negative error code.
db2a4165
FM
2158 */
2159int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
46f5822f 2160 unsigned int mask, unsigned int value)
db2a4165
FM
2161{
2162 int change;
46f5822f 2163 unsigned int old, new;
180c329d
TT
2164 int ret;
2165
2166 ret = snd_soc_read(codec, reg);
2167 if (ret < 0)
2168 return ret;
db2a4165 2169
180c329d 2170 old = ret;
db2a4165
FM
2171 new = (old & ~mask) | value;
2172 change = old != new;
180c329d
TT
2173 if (change) {
2174 ret = snd_soc_write(codec, reg, new);
2175 if (ret < 0)
2176 return ret;
2177 }
db2a4165 2178
db2a4165
FM
2179 return change;
2180}
2181EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2182
6c508c62
EN
2183/**
2184 * snd_soc_update_bits_locked - update codec register bits
2185 * @codec: audio codec
2186 * @reg: codec register
2187 * @mask: register mask
2188 * @value: new value
2189 *
2190 * Writes new register value, and takes the codec mutex.
2191 *
2192 * Returns 1 for change else 0.
2193 */
dd1b3d53
MB
2194int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2195 unsigned short reg, unsigned int mask,
2196 unsigned int value)
6c508c62
EN
2197{
2198 int change;
2199
2200 mutex_lock(&codec->mutex);
2201 change = snd_soc_update_bits(codec, reg, mask, value);
2202 mutex_unlock(&codec->mutex);
2203
2204 return change;
2205}
dd1b3d53 2206EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
6c508c62 2207
db2a4165
FM
2208/**
2209 * snd_soc_test_bits - test register for change
2210 * @codec: audio codec
2211 * @reg: codec register
2212 * @mask: register mask
2213 * @value: new value
2214 *
2215 * Tests a register with a new value and checks if the new value is
2216 * different from the old value.
2217 *
2218 * Returns 1 for change else 0.
2219 */
2220int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
46f5822f 2221 unsigned int mask, unsigned int value)
db2a4165
FM
2222{
2223 int change;
46f5822f 2224 unsigned int old, new;
db2a4165 2225
db2a4165
FM
2226 old = snd_soc_read(codec, reg);
2227 new = (old & ~mask) | value;
2228 change = old != new;
db2a4165
FM
2229
2230 return change;
2231}
2232EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2233
db2a4165
FM
2234/**
2235 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2236 * @substream: the pcm substream
2237 * @hw: the hardware parameters
2238 *
2239 * Sets the substream runtime hardware parameters.
2240 */
2241int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2242 const struct snd_pcm_hardware *hw)
2243{
2244 struct snd_pcm_runtime *runtime = substream->runtime;
2245 runtime->hw.info = hw->info;
2246 runtime->hw.formats = hw->formats;
2247 runtime->hw.period_bytes_min = hw->period_bytes_min;
2248 runtime->hw.period_bytes_max = hw->period_bytes_max;
2249 runtime->hw.periods_min = hw->periods_min;
2250 runtime->hw.periods_max = hw->periods_max;
2251 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2252 runtime->hw.fifo_size = hw->fifo_size;
2253 return 0;
2254}
2255EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2256
2257/**
2258 * snd_soc_cnew - create new control
2259 * @_template: control template
2260 * @data: control private data
ac11a2b3 2261 * @long_name: control long name
db2a4165
FM
2262 *
2263 * Create a new mixer control from a template control.
2264 *
2265 * Returns 0 for success, else error.
2266 */
2267struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2268 void *data, char *long_name)
2269{
2270 struct snd_kcontrol_new template;
2271
2272 memcpy(&template, _template, sizeof(template));
2273 if (long_name)
2274 template.name = long_name;
db2a4165
FM
2275 template.index = 0;
2276
2277 return snd_ctl_new1(&template, data);
2278}
2279EXPORT_SYMBOL_GPL(snd_soc_cnew);
2280
3e8e1952
IM
2281/**
2282 * snd_soc_add_controls - add an array of controls to a codec.
2283 * Convienience function to add a list of controls. Many codecs were
2284 * duplicating this code.
2285 *
2286 * @codec: codec to add controls to
2287 * @controls: array of controls to add
2288 * @num_controls: number of elements in the array
2289 *
2290 * Return 0 for success, else error.
2291 */
2292int snd_soc_add_controls(struct snd_soc_codec *codec,
2293 const struct snd_kcontrol_new *controls, int num_controls)
2294{
f0fba2ad 2295 struct snd_card *card = codec->card->snd_card;
ead9b919 2296 char prefixed_name[44], *name;
3e8e1952
IM
2297 int err, i;
2298
2299 for (i = 0; i < num_controls; i++) {
2300 const struct snd_kcontrol_new *control = &controls[i];
ead9b919
JN
2301 if (codec->name_prefix) {
2302 snprintf(prefixed_name, sizeof(prefixed_name), "%s %s",
2303 codec->name_prefix, control->name);
2304 name = prefixed_name;
2305 } else {
2306 name = control->name;
2307 }
2308 err = snd_ctl_add(card, snd_soc_cnew(control, codec, name));
3e8e1952 2309 if (err < 0) {
082100dc 2310 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
ead9b919 2311 codec->name, name, err);
3e8e1952
IM
2312 return err;
2313 }
2314 }
2315
2316 return 0;
2317}
2318EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2319
db2a4165
FM
2320/**
2321 * snd_soc_info_enum_double - enumerated double mixer info callback
2322 * @kcontrol: mixer control
2323 * @uinfo: control element information
2324 *
2325 * Callback to provide information about a double enumerated
2326 * mixer control.
2327 *
2328 * Returns 0 for success.
2329 */
2330int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2331 struct snd_ctl_elem_info *uinfo)
2332{
2333 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2334
2335 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2336 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
f8ba0b7b 2337 uinfo->value.enumerated.items = e->max;
db2a4165 2338
f8ba0b7b
JS
2339 if (uinfo->value.enumerated.item > e->max - 1)
2340 uinfo->value.enumerated.item = e->max - 1;
db2a4165
FM
2341 strcpy(uinfo->value.enumerated.name,
2342 e->texts[uinfo->value.enumerated.item]);
2343 return 0;
2344}
2345EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2346
2347/**
2348 * snd_soc_get_enum_double - enumerated double mixer get callback
2349 * @kcontrol: mixer control
ac11a2b3 2350 * @ucontrol: control element information
db2a4165
FM
2351 *
2352 * Callback to get the value of a double enumerated mixer.
2353 *
2354 * Returns 0 for success.
2355 */
2356int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2357 struct snd_ctl_elem_value *ucontrol)
2358{
2359 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2360 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
46f5822f 2361 unsigned int val, bitmask;
db2a4165 2362
f8ba0b7b 2363 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
db2a4165
FM
2364 ;
2365 val = snd_soc_read(codec, e->reg);
3ff3f64b
MB
2366 ucontrol->value.enumerated.item[0]
2367 = (val >> e->shift_l) & (bitmask - 1);
db2a4165
FM
2368 if (e->shift_l != e->shift_r)
2369 ucontrol->value.enumerated.item[1] =
2370 (val >> e->shift_r) & (bitmask - 1);
2371
2372 return 0;
2373}
2374EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2375
2376/**
2377 * snd_soc_put_enum_double - enumerated double mixer put callback
2378 * @kcontrol: mixer control
ac11a2b3 2379 * @ucontrol: control element information
db2a4165
FM
2380 *
2381 * Callback to set the value of a double enumerated mixer.
2382 *
2383 * Returns 0 for success.
2384 */
2385int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2386 struct snd_ctl_elem_value *ucontrol)
2387{
2388 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2389 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
46f5822f
DR
2390 unsigned int val;
2391 unsigned int mask, bitmask;
db2a4165 2392
f8ba0b7b 2393 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
db2a4165 2394 ;
f8ba0b7b 2395 if (ucontrol->value.enumerated.item[0] > e->max - 1)
db2a4165
FM
2396 return -EINVAL;
2397 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2398 mask = (bitmask - 1) << e->shift_l;
2399 if (e->shift_l != e->shift_r) {
f8ba0b7b 2400 if (ucontrol->value.enumerated.item[1] > e->max - 1)
db2a4165
FM
2401 return -EINVAL;
2402 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2403 mask |= (bitmask - 1) << e->shift_r;
2404 }
2405
6c508c62 2406 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
db2a4165
FM
2407}
2408EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2409
2e72f8e3
PU
2410/**
2411 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2412 * @kcontrol: mixer control
2413 * @ucontrol: control element information
2414 *
2415 * Callback to get the value of a double semi enumerated mixer.
2416 *
2417 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2418 * used for handling bitfield coded enumeration for example.
2419 *
2420 * Returns 0 for success.
2421 */
2422int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2423 struct snd_ctl_elem_value *ucontrol)
2424{
2425 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
74155556 2426 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
46f5822f 2427 unsigned int reg_val, val, mux;
2e72f8e3
PU
2428
2429 reg_val = snd_soc_read(codec, e->reg);
2430 val = (reg_val >> e->shift_l) & e->mask;
2431 for (mux = 0; mux < e->max; mux++) {
2432 if (val == e->values[mux])
2433 break;
2434 }
2435 ucontrol->value.enumerated.item[0] = mux;
2436 if (e->shift_l != e->shift_r) {
2437 val = (reg_val >> e->shift_r) & e->mask;
2438 for (mux = 0; mux < e->max; mux++) {
2439 if (val == e->values[mux])
2440 break;
2441 }
2442 ucontrol->value.enumerated.item[1] = mux;
2443 }
2444
2445 return 0;
2446}
2447EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2448
2449/**
2450 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2451 * @kcontrol: mixer control
2452 * @ucontrol: control element information
2453 *
2454 * Callback to set the value of a double semi enumerated mixer.
2455 *
2456 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2457 * used for handling bitfield coded enumeration for example.
2458 *
2459 * Returns 0 for success.
2460 */
2461int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2462 struct snd_ctl_elem_value *ucontrol)
2463{
2464 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
74155556 2465 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
46f5822f
DR
2466 unsigned int val;
2467 unsigned int mask;
2e72f8e3
PU
2468
2469 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2470 return -EINVAL;
2471 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2472 mask = e->mask << e->shift_l;
2473 if (e->shift_l != e->shift_r) {
2474 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2475 return -EINVAL;
2476 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2477 mask |= e->mask << e->shift_r;
2478 }
2479
6c508c62 2480 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2e72f8e3
PU
2481}
2482EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2483
db2a4165
FM
2484/**
2485 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2486 * @kcontrol: mixer control
2487 * @uinfo: control element information
2488 *
2489 * Callback to provide information about an external enumerated
2490 * single mixer.
2491 *
2492 * Returns 0 for success.
2493 */
2494int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2495 struct snd_ctl_elem_info *uinfo)
2496{
2497 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2498
2499 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2500 uinfo->count = 1;
f8ba0b7b 2501 uinfo->value.enumerated.items = e->max;
db2a4165 2502
f8ba0b7b
JS
2503 if (uinfo->value.enumerated.item > e->max - 1)
2504 uinfo->value.enumerated.item = e->max - 1;
db2a4165
FM
2505 strcpy(uinfo->value.enumerated.name,
2506 e->texts[uinfo->value.enumerated.item]);
2507 return 0;
2508}
2509EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2510
2511/**
2512 * snd_soc_info_volsw_ext - external single mixer info callback
2513 * @kcontrol: mixer control
2514 * @uinfo: control element information
2515 *
2516 * Callback to provide information about a single external mixer control.
2517 *
2518 * Returns 0 for success.
2519 */
2520int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2521 struct snd_ctl_elem_info *uinfo)
2522{
a7a4ac86
PZ
2523 int max = kcontrol->private_value;
2524
fd5dfad9 2525 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
a7a4ac86
PZ
2526 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2527 else
2528 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
db2a4165 2529
db2a4165
FM
2530 uinfo->count = 1;
2531 uinfo->value.integer.min = 0;
a7a4ac86 2532 uinfo->value.integer.max = max;
db2a4165
FM
2533 return 0;
2534}
2535EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2536
db2a4165
FM
2537/**
2538 * snd_soc_info_volsw - single mixer info callback
2539 * @kcontrol: mixer control
2540 * @uinfo: control element information
2541 *
2542 * Callback to provide information about a single mixer control.
2543 *
2544 * Returns 0 for success.
2545 */
2546int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2547 struct snd_ctl_elem_info *uinfo)
2548{
4eaa9819
JS
2549 struct soc_mixer_control *mc =
2550 (struct soc_mixer_control *)kcontrol->private_value;
d11bb4a9 2551 int platform_max;
762b8df7 2552 unsigned int shift = mc->shift;
815ecf8d 2553 unsigned int rshift = mc->rshift;
db2a4165 2554
d11bb4a9
PU
2555 if (!mc->platform_max)
2556 mc->platform_max = mc->max;
2557 platform_max = mc->platform_max;
2558
2559 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
a7a4ac86
PZ
2560 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2561 else
2562 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2563
db2a4165
FM
2564 uinfo->count = shift == rshift ? 1 : 2;
2565 uinfo->value.integer.min = 0;
d11bb4a9 2566 uinfo->value.integer.max = platform_max;
db2a4165
FM
2567 return 0;
2568}
2569EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2570
2571/**
2572 * snd_soc_get_volsw - single mixer get callback
2573 * @kcontrol: mixer control
ac11a2b3 2574 * @ucontrol: control element information
db2a4165
FM
2575 *
2576 * Callback to get the value of a single mixer control.
2577 *
2578 * Returns 0 for success.
2579 */
2580int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2581 struct snd_ctl_elem_value *ucontrol)
2582{
4eaa9819
JS
2583 struct soc_mixer_control *mc =
2584 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 2585 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
2586 unsigned int reg = mc->reg;
2587 unsigned int shift = mc->shift;
2588 unsigned int rshift = mc->rshift;
4eaa9819 2589 int max = mc->max;
815ecf8d
JS
2590 unsigned int mask = (1 << fls(max)) - 1;
2591 unsigned int invert = mc->invert;
db2a4165
FM
2592
2593 ucontrol->value.integer.value[0] =
2594 (snd_soc_read(codec, reg) >> shift) & mask;
2595 if (shift != rshift)
2596 ucontrol->value.integer.value[1] =
2597 (snd_soc_read(codec, reg) >> rshift) & mask;
2598 if (invert) {
2599 ucontrol->value.integer.value[0] =
a7a4ac86 2600 max - ucontrol->value.integer.value[0];
db2a4165
FM
2601 if (shift != rshift)
2602 ucontrol->value.integer.value[1] =
a7a4ac86 2603 max - ucontrol->value.integer.value[1];
db2a4165
FM
2604 }
2605
2606 return 0;
2607}
2608EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2609
2610/**
2611 * snd_soc_put_volsw - single mixer put callback
2612 * @kcontrol: mixer control
ac11a2b3 2613 * @ucontrol: control element information
db2a4165
FM
2614 *
2615 * Callback to set the value of a single mixer control.
2616 *
2617 * Returns 0 for success.
2618 */
2619int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2620 struct snd_ctl_elem_value *ucontrol)
2621{
4eaa9819
JS
2622 struct soc_mixer_control *mc =
2623 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 2624 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
2625 unsigned int reg = mc->reg;
2626 unsigned int shift = mc->shift;
2627 unsigned int rshift = mc->rshift;
4eaa9819 2628 int max = mc->max;
815ecf8d
JS
2629 unsigned int mask = (1 << fls(max)) - 1;
2630 unsigned int invert = mc->invert;
46f5822f 2631 unsigned int val, val2, val_mask;
db2a4165
FM
2632
2633 val = (ucontrol->value.integer.value[0] & mask);
2634 if (invert)
a7a4ac86 2635 val = max - val;
db2a4165
FM
2636 val_mask = mask << shift;
2637 val = val << shift;
2638 if (shift != rshift) {
2639 val2 = (ucontrol->value.integer.value[1] & mask);
2640 if (invert)
a7a4ac86 2641 val2 = max - val2;
db2a4165
FM
2642 val_mask |= mask << rshift;
2643 val |= val2 << rshift;
2644 }
6c508c62 2645 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
db2a4165
FM
2646}
2647EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2648
2649/**
2650 * snd_soc_info_volsw_2r - double mixer info callback
2651 * @kcontrol: mixer control
2652 * @uinfo: control element information
2653 *
2654 * Callback to provide information about a double mixer control that
2655 * spans 2 codec registers.
2656 *
2657 * Returns 0 for success.
2658 */
2659int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2660 struct snd_ctl_elem_info *uinfo)
2661{
4eaa9819
JS
2662 struct soc_mixer_control *mc =
2663 (struct soc_mixer_control *)kcontrol->private_value;
d11bb4a9 2664 int platform_max;
a7a4ac86 2665
d11bb4a9
PU
2666 if (!mc->platform_max)
2667 mc->platform_max = mc->max;
2668 platform_max = mc->platform_max;
2669
2670 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
a7a4ac86
PZ
2671 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2672 else
2673 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
db2a4165 2674
db2a4165
FM
2675 uinfo->count = 2;
2676 uinfo->value.integer.min = 0;
d11bb4a9 2677 uinfo->value.integer.max = platform_max;
db2a4165
FM
2678 return 0;
2679}
2680EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2681
2682/**
2683 * snd_soc_get_volsw_2r - double mixer get callback
2684 * @kcontrol: mixer control
ac11a2b3 2685 * @ucontrol: control element information
db2a4165
FM
2686 *
2687 * Callback to get the value of a double mixer control that spans 2 registers.
2688 *
2689 * Returns 0 for success.
2690 */
2691int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2692 struct snd_ctl_elem_value *ucontrol)
2693{
4eaa9819
JS
2694 struct soc_mixer_control *mc =
2695 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 2696 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
2697 unsigned int reg = mc->reg;
2698 unsigned int reg2 = mc->rreg;
2699 unsigned int shift = mc->shift;
4eaa9819 2700 int max = mc->max;
46f5822f 2701 unsigned int mask = (1 << fls(max)) - 1;
815ecf8d 2702 unsigned int invert = mc->invert;
db2a4165
FM
2703
2704 ucontrol->value.integer.value[0] =
2705 (snd_soc_read(codec, reg) >> shift) & mask;
2706 ucontrol->value.integer.value[1] =
2707 (snd_soc_read(codec, reg2) >> shift) & mask;
2708 if (invert) {
2709 ucontrol->value.integer.value[0] =
a7a4ac86 2710 max - ucontrol->value.integer.value[0];
db2a4165 2711 ucontrol->value.integer.value[1] =
a7a4ac86 2712 max - ucontrol->value.integer.value[1];
db2a4165
FM
2713 }
2714
2715 return 0;
2716}
2717EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2718
2719/**
2720 * snd_soc_put_volsw_2r - double mixer set callback
2721 * @kcontrol: mixer control
ac11a2b3 2722 * @ucontrol: control element information
db2a4165
FM
2723 *
2724 * Callback to set the value of a double mixer control that spans 2 registers.
2725 *
2726 * Returns 0 for success.
2727 */
2728int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2729 struct snd_ctl_elem_value *ucontrol)
2730{
4eaa9819
JS
2731 struct soc_mixer_control *mc =
2732 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 2733 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d
JS
2734 unsigned int reg = mc->reg;
2735 unsigned int reg2 = mc->rreg;
2736 unsigned int shift = mc->shift;
4eaa9819 2737 int max = mc->max;
815ecf8d
JS
2738 unsigned int mask = (1 << fls(max)) - 1;
2739 unsigned int invert = mc->invert;
db2a4165 2740 int err;
46f5822f 2741 unsigned int val, val2, val_mask;
db2a4165
FM
2742
2743 val_mask = mask << shift;
2744 val = (ucontrol->value.integer.value[0] & mask);
2745 val2 = (ucontrol->value.integer.value[1] & mask);
2746
2747 if (invert) {
a7a4ac86
PZ
2748 val = max - val;
2749 val2 = max - val2;
db2a4165
FM
2750 }
2751
2752 val = val << shift;
2753 val2 = val2 << shift;
2754
6c508c62 2755 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
3ff3f64b 2756 if (err < 0)
db2a4165
FM
2757 return err;
2758
6c508c62 2759 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
db2a4165
FM
2760 return err;
2761}
2762EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2763
e13ac2e9
MB
2764/**
2765 * snd_soc_info_volsw_s8 - signed mixer info callback
2766 * @kcontrol: mixer control
2767 * @uinfo: control element information
2768 *
2769 * Callback to provide information about a signed mixer control.
2770 *
2771 * Returns 0 for success.
2772 */
2773int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2774 struct snd_ctl_elem_info *uinfo)
2775{
4eaa9819
JS
2776 struct soc_mixer_control *mc =
2777 (struct soc_mixer_control *)kcontrol->private_value;
d11bb4a9 2778 int platform_max;
4eaa9819 2779 int min = mc->min;
e13ac2e9 2780
d11bb4a9
PU
2781 if (!mc->platform_max)
2782 mc->platform_max = mc->max;
2783 platform_max = mc->platform_max;
2784
e13ac2e9
MB
2785 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2786 uinfo->count = 2;
2787 uinfo->value.integer.min = 0;
d11bb4a9 2788 uinfo->value.integer.max = platform_max - min;
e13ac2e9
MB
2789 return 0;
2790}
2791EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2792
2793/**
2794 * snd_soc_get_volsw_s8 - signed mixer get callback
2795 * @kcontrol: mixer control
ac11a2b3 2796 * @ucontrol: control element information
e13ac2e9
MB
2797 *
2798 * Callback to get the value of a signed mixer control.
2799 *
2800 * Returns 0 for success.
2801 */
2802int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2803 struct snd_ctl_elem_value *ucontrol)
2804{
4eaa9819
JS
2805 struct soc_mixer_control *mc =
2806 (struct soc_mixer_control *)kcontrol->private_value;
e13ac2e9 2807 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d 2808 unsigned int reg = mc->reg;
4eaa9819 2809 int min = mc->min;
e13ac2e9
MB
2810 int val = snd_soc_read(codec, reg);
2811
2812 ucontrol->value.integer.value[0] =
2813 ((signed char)(val & 0xff))-min;
2814 ucontrol->value.integer.value[1] =
2815 ((signed char)((val >> 8) & 0xff))-min;
2816 return 0;
2817}
2818EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2819
2820/**
2821 * snd_soc_put_volsw_sgn - signed mixer put callback
2822 * @kcontrol: mixer control
ac11a2b3 2823 * @ucontrol: control element information
e13ac2e9
MB
2824 *
2825 * Callback to set the value of a signed mixer control.
2826 *
2827 * Returns 0 for success.
2828 */
2829int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2830 struct snd_ctl_elem_value *ucontrol)
2831{
4eaa9819
JS
2832 struct soc_mixer_control *mc =
2833 (struct soc_mixer_control *)kcontrol->private_value;
e13ac2e9 2834 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d 2835 unsigned int reg = mc->reg;
4eaa9819 2836 int min = mc->min;
46f5822f 2837 unsigned int val;
e13ac2e9
MB
2838
2839 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2840 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2841
6c508c62 2842 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
e13ac2e9
MB
2843}
2844EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2845
637d3847
PU
2846/**
2847 * snd_soc_limit_volume - Set new limit to an existing volume control.
2848 *
2849 * @codec: where to look for the control
2850 * @name: Name of the control
2851 * @max: new maximum limit
2852 *
2853 * Return 0 for success, else error.
2854 */
2855int snd_soc_limit_volume(struct snd_soc_codec *codec,
2856 const char *name, int max)
2857{
f0fba2ad 2858 struct snd_card *card = codec->card->snd_card;
637d3847
PU
2859 struct snd_kcontrol *kctl;
2860 struct soc_mixer_control *mc;
2861 int found = 0;
2862 int ret = -EINVAL;
2863
2864 /* Sanity check for name and max */
2865 if (unlikely(!name || max <= 0))
2866 return -EINVAL;
2867
2868 list_for_each_entry(kctl, &card->controls, list) {
2869 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2870 found = 1;
2871 break;
2872 }
2873 }
2874 if (found) {
2875 mc = (struct soc_mixer_control *)kctl->private_value;
2876 if (max <= mc->max) {
d11bb4a9 2877 mc->platform_max = max;
637d3847
PU
2878 ret = 0;
2879 }
2880 }
2881 return ret;
2882}
2883EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2884
b6f4bb38 2885/**
2886 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2887 * mixer info callback
2888 * @kcontrol: mixer control
2889 * @uinfo: control element information
2890 *
2891 * Returns 0 for success.
2892 */
2893int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2894 struct snd_ctl_elem_info *uinfo)
2895{
2896 struct soc_mixer_control *mc =
2897 (struct soc_mixer_control *)kcontrol->private_value;
2898 int max = mc->max;
2899 int min = mc->min;
2900
2901 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2902 uinfo->count = 2;
2903 uinfo->value.integer.min = 0;
2904 uinfo->value.integer.max = max-min;
2905
2906 return 0;
2907}
2908EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2909
2910/**
2911 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2912 * mixer get callback
2913 * @kcontrol: mixer control
2914 * @uinfo: control element information
2915 *
2916 * Returns 0 for success.
2917 */
2918int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2919 struct snd_ctl_elem_value *ucontrol)
2920{
2921 struct soc_mixer_control *mc =
2922 (struct soc_mixer_control *)kcontrol->private_value;
2923 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2924 unsigned int mask = (1<<mc->shift)-1;
2925 int min = mc->min;
2926 int val = snd_soc_read(codec, mc->reg) & mask;
2927 int valr = snd_soc_read(codec, mc->rreg) & mask;
2928
20630c7f
SL
2929 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2930 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
b6f4bb38 2931 return 0;
2932}
2933EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2934
2935/**
2936 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2937 * mixer put callback
2938 * @kcontrol: mixer control
2939 * @uinfo: control element information
2940 *
2941 * Returns 0 for success.
2942 */
2943int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2944 struct snd_ctl_elem_value *ucontrol)
2945{
2946 struct soc_mixer_control *mc =
2947 (struct soc_mixer_control *)kcontrol->private_value;
2948 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2949 unsigned int mask = (1<<mc->shift)-1;
2950 int min = mc->min;
2951 int ret;
2952 unsigned int val, valr, oval, ovalr;
2953
2954 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2955 val &= mask;
2956 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2957 valr &= mask;
2958
2959 oval = snd_soc_read(codec, mc->reg) & mask;
2960 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2961
2962 ret = 0;
2963 if (oval != val) {
2964 ret = snd_soc_write(codec, mc->reg, val);
2965 if (ret < 0)
f1df5aec 2966 return ret;
b6f4bb38 2967 }
2968 if (ovalr != valr) {
2969 ret = snd_soc_write(codec, mc->rreg, valr);
2970 if (ret < 0)
f1df5aec 2971 return ret;
b6f4bb38 2972 }
2973
2974 return 0;
2975}
2976EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2977
8c6529db
LG
2978/**
2979 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2980 * @dai: DAI
2981 * @clk_id: DAI specific clock ID
2982 * @freq: new clock frequency in Hz
2983 * @dir: new clock direction - input/output.
2984 *
2985 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2986 */
2987int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2988 unsigned int freq, int dir)
2989{
f0fba2ad
LG
2990 if (dai->driver && dai->driver->ops->set_sysclk)
2991 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
8c6529db
LG
2992 else
2993 return -EINVAL;
2994}
2995EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2996
2997/**
2998 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2999 * @dai: DAI
ac11a2b3 3000 * @div_id: DAI specific clock divider ID
8c6529db
LG
3001 * @div: new clock divisor.
3002 *
3003 * Configures the clock dividers. This is used to derive the best DAI bit and
3004 * frame clocks from the system or master clock. It's best to set the DAI bit
3005 * and frame clocks as low as possible to save system power.
3006 */
3007int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3008 int div_id, int div)
3009{
f0fba2ad
LG
3010 if (dai->driver && dai->driver->ops->set_clkdiv)
3011 return dai->driver->ops->set_clkdiv(dai, div_id, div);
8c6529db
LG
3012 else
3013 return -EINVAL;
3014}
3015EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3016
3017/**
3018 * snd_soc_dai_set_pll - configure DAI PLL.
3019 * @dai: DAI
3020 * @pll_id: DAI specific PLL ID
85488037 3021 * @source: DAI specific source for the PLL
8c6529db
LG
3022 * @freq_in: PLL input clock frequency in Hz
3023 * @freq_out: requested PLL output clock frequency in Hz
3024 *
3025 * Configures and enables PLL to generate output clock based on input clock.
3026 */
85488037
MB
3027int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3028 unsigned int freq_in, unsigned int freq_out)
8c6529db 3029{
f0fba2ad
LG
3030 if (dai->driver && dai->driver->ops->set_pll)
3031 return dai->driver->ops->set_pll(dai, pll_id, source,
85488037 3032 freq_in, freq_out);
8c6529db
LG
3033 else
3034 return -EINVAL;
3035}
3036EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3037
3038/**
3039 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3040 * @dai: DAI
8c6529db
LG
3041 * @fmt: SND_SOC_DAIFMT_ format value.
3042 *
3043 * Configures the DAI hardware format and clocking.
3044 */
3045int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3046{
f0fba2ad
LG
3047 if (dai->driver && dai->driver->ops->set_fmt)
3048 return dai->driver->ops->set_fmt(dai, fmt);
8c6529db
LG
3049 else
3050 return -EINVAL;
3051}
3052EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3053
3054/**
3055 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3056 * @dai: DAI
a5479e38
DR
3057 * @tx_mask: bitmask representing active TX slots.
3058 * @rx_mask: bitmask representing active RX slots.
8c6529db 3059 * @slots: Number of slots in use.
a5479e38 3060 * @slot_width: Width in bits for each slot.
8c6529db
LG
3061 *
3062 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3063 * specific.
3064 */
3065int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
a5479e38 3066 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
8c6529db 3067{
f0fba2ad
LG
3068 if (dai->driver && dai->driver->ops->set_tdm_slot)
3069 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
a5479e38 3070 slots, slot_width);
8c6529db
LG
3071 else
3072 return -EINVAL;
3073}
3074EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3075
472df3cb
BS
3076/**
3077 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3078 * @dai: DAI
3079 * @tx_num: how many TX channels
3080 * @tx_slot: pointer to an array which imply the TX slot number channel
3081 * 0~num-1 uses
3082 * @rx_num: how many RX channels
3083 * @rx_slot: pointer to an array which imply the RX slot number channel
3084 * 0~num-1 uses
3085 *
3086 * configure the relationship between channel number and TDM slot number.
3087 */
3088int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3089 unsigned int tx_num, unsigned int *tx_slot,
3090 unsigned int rx_num, unsigned int *rx_slot)
3091{
f0fba2ad
LG
3092 if (dai->driver && dai->driver->ops->set_channel_map)
3093 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
472df3cb
BS
3094 rx_num, rx_slot);
3095 else
3096 return -EINVAL;
3097}
3098EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3099
8c6529db
LG
3100/**
3101 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3102 * @dai: DAI
3103 * @tristate: tristate enable
3104 *
3105 * Tristates the DAI so that others can use it.
3106 */
3107int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3108{
f0fba2ad
LG
3109 if (dai->driver && dai->driver->ops->set_tristate)
3110 return dai->driver->ops->set_tristate(dai, tristate);
8c6529db
LG
3111 else
3112 return -EINVAL;
3113}
3114EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3115
3116/**
3117 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3118 * @dai: DAI
3119 * @mute: mute enable
3120 *
3121 * Mutes the DAI DAC.
3122 */
3123int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3124{
f0fba2ad
LG
3125 if (dai->driver && dai->driver->ops->digital_mute)
3126 return dai->driver->ops->digital_mute(dai, mute);
8c6529db
LG
3127 else
3128 return -EINVAL;
3129}
3130EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3131
c5af3a2e
MB
3132/**
3133 * snd_soc_register_card - Register a card with the ASoC core
3134 *
ac11a2b3 3135 * @card: Card to register
c5af3a2e 3136 *
c5af3a2e 3137 */
70a7ca34 3138int snd_soc_register_card(struct snd_soc_card *card)
c5af3a2e 3139{
f0fba2ad
LG
3140 int i;
3141
c5af3a2e
MB
3142 if (!card->name || !card->dev)
3143 return -EINVAL;
3144
111c6419
SW
3145 snd_soc_initialize_card_lists(card);
3146
150dd2f8
VK
3147 soc_init_card_debugfs(card);
3148
2eea392d
JN
3149 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
3150 (card->num_links + card->num_aux_devs),
3151 GFP_KERNEL);
f0fba2ad
LG
3152 if (card->rtd == NULL)
3153 return -ENOMEM;
2eea392d 3154 card->rtd_aux = &card->rtd[card->num_links];
f0fba2ad
LG
3155
3156 for (i = 0; i < card->num_links; i++)
3157 card->rtd[i].dai_link = &card->dai_link[i];
3158
c5af3a2e
MB
3159 INIT_LIST_HEAD(&card->list);
3160 card->instantiated = 0;
f0fba2ad 3161 mutex_init(&card->mutex);
c5af3a2e
MB
3162
3163 mutex_lock(&client_mutex);
3164 list_add(&card->list, &card_list);
435c5e25 3165 snd_soc_instantiate_cards();
c5af3a2e
MB
3166 mutex_unlock(&client_mutex);
3167
3168 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
3169
3170 return 0;
3171}
70a7ca34 3172EXPORT_SYMBOL_GPL(snd_soc_register_card);
c5af3a2e
MB
3173
3174/**
3175 * snd_soc_unregister_card - Unregister a card with the ASoC core
3176 *
ac11a2b3 3177 * @card: Card to unregister
c5af3a2e 3178 *
c5af3a2e 3179 */
70a7ca34 3180int snd_soc_unregister_card(struct snd_soc_card *card)
c5af3a2e 3181{
b0e26485
VK
3182 if (card->instantiated)
3183 soc_cleanup_card_resources(card);
c5af3a2e
MB
3184 mutex_lock(&client_mutex);
3185 list_del(&card->list);
3186 mutex_unlock(&client_mutex);
c5af3a2e
MB
3187 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3188
3189 return 0;
3190}
70a7ca34 3191EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
c5af3a2e 3192
f0fba2ad
LG
3193/*
3194 * Simplify DAI link configuration by removing ".-1" from device names
3195 * and sanitizing names.
3196 */
0b9a214a 3197static char *fmt_single_name(struct device *dev, int *id)
f0fba2ad
LG
3198{
3199 char *found, name[NAME_SIZE];
3200 int id1, id2;
3201
3202 if (dev_name(dev) == NULL)
3203 return NULL;
3204
58818a77 3205 strlcpy(name, dev_name(dev), NAME_SIZE);
f0fba2ad
LG
3206
3207 /* are we a "%s.%d" name (platform and SPI components) */
3208 found = strstr(name, dev->driver->name);
3209 if (found) {
3210 /* get ID */
3211 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3212
3213 /* discard ID from name if ID == -1 */
3214 if (*id == -1)
3215 found[strlen(dev->driver->name)] = '\0';
3216 }
3217
3218 } else {
3219 /* I2C component devices are named "bus-addr" */
3220 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3221 char tmp[NAME_SIZE];
3222
3223 /* create unique ID number from I2C addr and bus */
05899446 3224 *id = ((id1 & 0xffff) << 16) + id2;
f0fba2ad
LG
3225
3226 /* sanitize component name for DAI link creation */
3227 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
58818a77 3228 strlcpy(name, tmp, NAME_SIZE);
f0fba2ad
LG
3229 } else
3230 *id = 0;
3231 }
3232
3233 return kstrdup(name, GFP_KERNEL);
3234}
3235
3236/*
3237 * Simplify DAI link naming for single devices with multiple DAIs by removing
3238 * any ".-1" and using the DAI name (instead of device name).
3239 */
3240static inline char *fmt_multiple_name(struct device *dev,
3241 struct snd_soc_dai_driver *dai_drv)
3242{
3243 if (dai_drv->name == NULL) {
3244 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3245 dev_name(dev));
3246 return NULL;
3247 }
3248
3249 return kstrdup(dai_drv->name, GFP_KERNEL);
3250}
3251
9115171a
MB
3252/**
3253 * snd_soc_register_dai - Register a DAI with the ASoC core
3254 *
ac11a2b3 3255 * @dai: DAI to register
9115171a 3256 */
f0fba2ad
LG
3257int snd_soc_register_dai(struct device *dev,
3258 struct snd_soc_dai_driver *dai_drv)
9115171a 3259{
f0fba2ad
LG
3260 struct snd_soc_dai *dai;
3261
3262 dev_dbg(dev, "dai register %s\n", dev_name(dev));
9115171a 3263
f0fba2ad
LG
3264 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3265 if (dai == NULL)
3266 return -ENOMEM;
9115171a 3267
f0fba2ad
LG
3268 /* create DAI component name */
3269 dai->name = fmt_single_name(dev, &dai->id);
3270 if (dai->name == NULL) {
3271 kfree(dai);
3272 return -ENOMEM;
3273 }
6335d055 3274
f0fba2ad
LG
3275 dai->dev = dev;
3276 dai->driver = dai_drv;
3277 if (!dai->driver->ops)
3278 dai->driver->ops = &null_dai_ops;
9115171a
MB
3279
3280 mutex_lock(&client_mutex);
3281 list_add(&dai->list, &dai_list);
435c5e25 3282 snd_soc_instantiate_cards();
9115171a
MB
3283 mutex_unlock(&client_mutex);
3284
3285 pr_debug("Registered DAI '%s'\n", dai->name);
3286
3287 return 0;
3288}
3289EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3290
3291/**
3292 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3293 *
ac11a2b3 3294 * @dai: DAI to unregister
9115171a 3295 */
f0fba2ad 3296void snd_soc_unregister_dai(struct device *dev)
9115171a 3297{
f0fba2ad
LG
3298 struct snd_soc_dai *dai;
3299
3300 list_for_each_entry(dai, &dai_list, list) {
3301 if (dev == dai->dev)
3302 goto found;
3303 }
3304 return;
3305
3306found:
9115171a
MB
3307 mutex_lock(&client_mutex);
3308 list_del(&dai->list);
3309 mutex_unlock(&client_mutex);
3310
3311 pr_debug("Unregistered DAI '%s'\n", dai->name);
f0fba2ad
LG
3312 kfree(dai->name);
3313 kfree(dai);
9115171a
MB
3314}
3315EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3316
3317/**
3318 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3319 *
ac11a2b3
MB
3320 * @dai: Array of DAIs to register
3321 * @count: Number of DAIs
9115171a 3322 */
f0fba2ad
LG
3323int snd_soc_register_dais(struct device *dev,
3324 struct snd_soc_dai_driver *dai_drv, size_t count)
9115171a 3325{
f0fba2ad
LG
3326 struct snd_soc_dai *dai;
3327 int i, ret = 0;
3328
720ffa4c 3329 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
9115171a
MB
3330
3331 for (i = 0; i < count; i++) {
f0fba2ad
LG
3332
3333 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
c46e0079
AL
3334 if (dai == NULL) {
3335 ret = -ENOMEM;
3336 goto err;
3337 }
f0fba2ad
LG
3338
3339 /* create DAI component name */
3340 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3341 if (dai->name == NULL) {
3342 kfree(dai);
3343 ret = -EINVAL;
9115171a 3344 goto err;
f0fba2ad
LG
3345 }
3346
3347 dai->dev = dev;
f0fba2ad 3348 dai->driver = &dai_drv[i];
0f9141c9
MB
3349 if (dai->driver->id)
3350 dai->id = dai->driver->id;
3351 else
3352 dai->id = i;
f0fba2ad
LG
3353 if (!dai->driver->ops)
3354 dai->driver->ops = &null_dai_ops;
3355
3356 mutex_lock(&client_mutex);
3357 list_add(&dai->list, &dai_list);
3358 mutex_unlock(&client_mutex);
3359
3360 pr_debug("Registered DAI '%s'\n", dai->name);
9115171a
MB
3361 }
3362
1dcb4f38 3363 mutex_lock(&client_mutex);
f0fba2ad 3364 snd_soc_instantiate_cards();
1dcb4f38 3365 mutex_unlock(&client_mutex);
9115171a
MB
3366 return 0;
3367
3368err:
3369 for (i--; i >= 0; i--)
f0fba2ad 3370 snd_soc_unregister_dai(dev);
9115171a
MB
3371
3372 return ret;
3373}
3374EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3375
3376/**
3377 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3378 *
ac11a2b3
MB
3379 * @dai: Array of DAIs to unregister
3380 * @count: Number of DAIs
9115171a 3381 */
f0fba2ad 3382void snd_soc_unregister_dais(struct device *dev, size_t count)
9115171a
MB
3383{
3384 int i;
3385
3386 for (i = 0; i < count; i++)
f0fba2ad 3387 snd_soc_unregister_dai(dev);
9115171a
MB
3388}
3389EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3390
12a48a8c
MB
3391/**
3392 * snd_soc_register_platform - Register a platform with the ASoC core
3393 *
ac11a2b3 3394 * @platform: platform to register
12a48a8c 3395 */
f0fba2ad
LG
3396int snd_soc_register_platform(struct device *dev,
3397 struct snd_soc_platform_driver *platform_drv)
12a48a8c 3398{
f0fba2ad
LG
3399 struct snd_soc_platform *platform;
3400
3401 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3402
3403 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3404 if (platform == NULL)
3405 return -ENOMEM;
3406
3407 /* create platform component name */
3408 platform->name = fmt_single_name(dev, &platform->id);
3409 if (platform->name == NULL) {
3410 kfree(platform);
3411 return -ENOMEM;
3412 }
12a48a8c 3413
f0fba2ad
LG
3414 platform->dev = dev;
3415 platform->driver = platform_drv;
12a48a8c
MB
3416
3417 mutex_lock(&client_mutex);
3418 list_add(&platform->list, &platform_list);
435c5e25 3419 snd_soc_instantiate_cards();
12a48a8c
MB
3420 mutex_unlock(&client_mutex);
3421
3422 pr_debug("Registered platform '%s'\n", platform->name);
3423
3424 return 0;
3425}
3426EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3427
3428/**
3429 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3430 *
ac11a2b3 3431 * @platform: platform to unregister
12a48a8c 3432 */
f0fba2ad 3433void snd_soc_unregister_platform(struct device *dev)
12a48a8c 3434{
f0fba2ad
LG
3435 struct snd_soc_platform *platform;
3436
3437 list_for_each_entry(platform, &platform_list, list) {
3438 if (dev == platform->dev)
3439 goto found;
3440 }
3441 return;
3442
3443found:
12a48a8c
MB
3444 mutex_lock(&client_mutex);
3445 list_del(&platform->list);
3446 mutex_unlock(&client_mutex);
3447
3448 pr_debug("Unregistered platform '%s'\n", platform->name);
f0fba2ad
LG
3449 kfree(platform->name);
3450 kfree(platform);
12a48a8c
MB
3451}
3452EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3453
151ab22c
MB
3454static u64 codec_format_map[] = {
3455 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3456 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3457 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3458 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3459 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3460 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3461 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3462 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3463 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3464 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3465 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3466 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3467 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3468 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3469 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3470 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3471};
3472
3473/* Fix up the DAI formats for endianness: codecs don't actually see
3474 * the endianness of the data but we're using the CPU format
3475 * definitions which do need to include endianness so we ensure that
3476 * codec DAIs always have both big and little endian variants set.
3477 */
3478static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3479{
3480 int i;
3481
3482 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3483 if (stream->formats & codec_format_map[i])
3484 stream->formats |= codec_format_map[i];
3485}
3486
0d0cf00a
MB
3487/**
3488 * snd_soc_register_codec - Register a codec with the ASoC core
3489 *
ac11a2b3 3490 * @codec: codec to register
0d0cf00a 3491 */
f0fba2ad 3492int snd_soc_register_codec(struct device *dev,
001ae4c0
MB
3493 const struct snd_soc_codec_driver *codec_drv,
3494 struct snd_soc_dai_driver *dai_drv,
3495 int num_dai)
0d0cf00a 3496{
3335ddca 3497 size_t reg_size;
f0fba2ad
LG
3498 struct snd_soc_codec *codec;
3499 int ret, i;
151ab22c 3500
f0fba2ad
LG
3501 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3502
3503 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3504 if (codec == NULL)
3505 return -ENOMEM;
0d0cf00a 3506
f0fba2ad
LG
3507 /* create CODEC component name */
3508 codec->name = fmt_single_name(dev, &codec->id);
3509 if (codec->name == NULL) {
3510 kfree(codec);
3511 return -ENOMEM;
3512 }
3513
23bbce34
DP
3514 if (codec_drv->compress_type)
3515 codec->compress_type = codec_drv->compress_type;
3516 else
3517 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3518
c3acec26
MB
3519 codec->write = codec_drv->write;
3520 codec->read = codec_drv->read;
1500b7b5
DP
3521 codec->volatile_register = codec_drv->volatile_register;
3522 codec->readable_register = codec_drv->readable_register;
ce6120cc
LG
3523 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3524 codec->dapm.dev = dev;
3525 codec->dapm.codec = codec;
474b62d6 3526 codec->dapm.seq_notifier = codec_drv->seq_notifier;
7a30a3db
DP
3527 codec->dev = dev;
3528 codec->driver = codec_drv;
3529 codec->num_dai = num_dai;
3530 mutex_init(&codec->mutex);
ce6120cc 3531
f0fba2ad
LG
3532 /* allocate CODEC register cache */
3533 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3335ddca 3534 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
aea170a0 3535 codec->reg_size = reg_size;
3335ddca
DP
3536 /* it is necessary to make a copy of the default register cache
3537 * because in the case of using a compression type that requires
3538 * the default register cache to be marked as __devinitconst the
3539 * kernel might have freed the array by the time we initialize
3540 * the cache.
3541 */
2aa86323
DP
3542 if (codec_drv->reg_cache_default) {
3543 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3544 reg_size, GFP_KERNEL);
3545 if (!codec->reg_def_copy) {
3546 ret = -ENOMEM;
3547 goto fail;
3548 }
f0fba2ad 3549 }
151ab22c
MB
3550 }
3551
1500b7b5
DP
3552 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3553 if (!codec->volatile_register)
3554 codec->volatile_register = snd_soc_default_volatile_register;
3555 if (!codec->readable_register)
3556 codec->readable_register = snd_soc_default_readable_register;
3557 }
3558
f0fba2ad
LG
3559 for (i = 0; i < num_dai; i++) {
3560 fixup_codec_formats(&dai_drv[i].playback);
3561 fixup_codec_formats(&dai_drv[i].capture);
3562 }
3563
26b01ccd
MB
3564 /* register any DAIs */
3565 if (num_dai) {
3566 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3567 if (ret < 0)
fdf0f54d 3568 goto fail;
26b01ccd 3569 }
f0fba2ad 3570
0d0cf00a
MB
3571 mutex_lock(&client_mutex);
3572 list_add(&codec->list, &codec_list);
3573 snd_soc_instantiate_cards();
3574 mutex_unlock(&client_mutex);
3575
3576 pr_debug("Registered codec '%s'\n", codec->name);
0d0cf00a 3577 return 0;
f0fba2ad 3578
fdf0f54d 3579fail:
3335ddca
DP
3580 kfree(codec->reg_def_copy);
3581 codec->reg_def_copy = NULL;
f0fba2ad
LG
3582 kfree(codec->name);
3583 kfree(codec);
3584 return ret;
0d0cf00a
MB
3585}
3586EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3587
3588/**
3589 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3590 *
ac11a2b3 3591 * @codec: codec to unregister
0d0cf00a 3592 */
f0fba2ad 3593void snd_soc_unregister_codec(struct device *dev)
0d0cf00a 3594{
f0fba2ad
LG
3595 struct snd_soc_codec *codec;
3596 int i;
3597
3598 list_for_each_entry(codec, &codec_list, list) {
3599 if (dev == codec->dev)
3600 goto found;
3601 }
3602 return;
3603
3604found:
26b01ccd
MB
3605 if (codec->num_dai)
3606 for (i = 0; i < codec->num_dai; i++)
3607 snd_soc_unregister_dai(dev);
f0fba2ad 3608
0d0cf00a
MB
3609 mutex_lock(&client_mutex);
3610 list_del(&codec->list);
3611 mutex_unlock(&client_mutex);
3612
3613 pr_debug("Unregistered codec '%s'\n", codec->name);
f0fba2ad 3614
7a30a3db 3615 snd_soc_cache_exit(codec);
3335ddca 3616 kfree(codec->reg_def_copy);
1aafcd4d 3617 kfree(codec->name);
f0fba2ad 3618 kfree(codec);
0d0cf00a
MB
3619}
3620EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3621
c9b3a40f 3622static int __init snd_soc_init(void)
db2a4165 3623{
384c89e2 3624#ifdef CONFIG_DEBUG_FS
8a9dab1a
MB
3625 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3626 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
384c89e2
MB
3627 printk(KERN_WARNING
3628 "ASoC: Failed to create debugfs directory\n");
8a9dab1a 3629 snd_soc_debugfs_root = NULL;
384c89e2 3630 }
c3c5a19a 3631
8a9dab1a 3632 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
c3c5a19a
MB
3633 &codec_list_fops))
3634 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3635
8a9dab1a 3636 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
f3208780
MB
3637 &dai_list_fops))
3638 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
19c7ac27 3639
8a9dab1a 3640 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
19c7ac27
MB
3641 &platform_list_fops))
3642 pr_warn("ASoC: Failed to create platform list debugfs file\n");
384c89e2
MB
3643#endif
3644
db2a4165
FM
3645 return platform_driver_register(&soc_driver);
3646}
4abe8e16 3647module_init(snd_soc_init);
db2a4165 3648
7d8c16a6 3649static void __exit snd_soc_exit(void)
db2a4165 3650{
384c89e2 3651#ifdef CONFIG_DEBUG_FS
8a9dab1a 3652 debugfs_remove_recursive(snd_soc_debugfs_root);
384c89e2 3653#endif
3ff3f64b 3654 platform_driver_unregister(&soc_driver);
db2a4165 3655}
db2a4165
FM
3656module_exit(snd_soc_exit);
3657
3658/* Module information */
d331124d 3659MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
db2a4165
FM
3660MODULE_DESCRIPTION("ALSA SoC Core");
3661MODULE_LICENSE("GPL");
8b45a209 3662MODULE_ALIAS("platform:soc-audio");