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