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