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