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