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