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