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