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