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