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