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