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