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