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