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