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