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