]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - sound/soc/soc-core.c
ASoC: dapm - Fix check for codec context in dapm_power_widgets().
[mirror_ubuntu-bionic-kernel.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>
db2a4165
FM
42#include <sound/initval.h>
43
a8b1d34f
MB
44#define CREATE_TRACE_POINTS
45#include <trace/events/asoc.h>
46
f0fba2ad
LG
47#define NAME_SIZE 32
48
db2a4165
FM
49static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
50
384c89e2 51#ifdef CONFIG_DEBUG_FS
8a9dab1a
MB
52struct dentry *snd_soc_debugfs_root;
53EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
384c89e2
MB
54#endif
55
c5af3a2e
MB
56static DEFINE_MUTEX(client_mutex);
57static LIST_HEAD(card_list);
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
MB
203#ifdef CONFIG_DEBUG_FS
204static int codec_reg_open_file(struct inode *inode, struct file *file)
205{
206 file->private_data = inode->i_private;
207 return 0;
208}
209
210static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
13fd179f 211 size_t count, loff_t *ppos)
2624d5fa
MB
212{
213 ssize_t ret;
214 struct snd_soc_codec *codec = file->private_data;
13fd179f
DP
215 char *buf;
216
217 if (*ppos < 0 || !count)
218 return -EINVAL;
219
220 buf = kmalloc(count, GFP_KERNEL);
2624d5fa
MB
221 if (!buf)
222 return -ENOMEM;
13fd179f
DP
223
224 ret = soc_codec_reg_show(codec, buf, count, *ppos);
225 if (ret >= 0) {
226 if (copy_to_user(user_buf, buf, ret)) {
227 kfree(buf);
228 return -EFAULT;
229 }
230 *ppos += ret;
231 }
232
2624d5fa
MB
233 kfree(buf);
234 return ret;
235}
236
237static ssize_t codec_reg_write_file(struct file *file,
238 const char __user *user_buf, size_t count, loff_t *ppos)
239{
240 char buf[32];
34e268d8 241 size_t buf_size;
2624d5fa
MB
242 char *start = buf;
243 unsigned long reg, value;
2624d5fa
MB
244 struct snd_soc_codec *codec = file->private_data;
245
246 buf_size = min(count, (sizeof(buf)-1));
247 if (copy_from_user(buf, user_buf, buf_size))
248 return -EFAULT;
249 buf[buf_size] = 0;
2624d5fa
MB
250
251 while (*start == ' ')
252 start++;
253 reg = simple_strtoul(start, &start, 16);
2624d5fa
MB
254 while (*start == ' ')
255 start++;
256 if (strict_strtoul(start, 16, &value))
257 return -EINVAL;
0d51a9cb
MB
258
259 /* Userspace has been fiddling around behind the kernel's back */
260 add_taint(TAINT_USER);
261
e4f078d8 262 snd_soc_write(codec, reg, value);
2624d5fa
MB
263 return buf_size;
264}
265
266static const struct file_operations codec_reg_fops = {
267 .open = codec_reg_open_file,
268 .read = codec_reg_read_file,
269 .write = codec_reg_write_file,
6038f373 270 .llseek = default_llseek,
2624d5fa
MB
271};
272
273static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
274{
d6ce4cf3
JN
275 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
276
277 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
278 debugfs_card_root);
2624d5fa
MB
279 if (!codec->debugfs_codec_root) {
280 printk(KERN_WARNING
281 "ASoC: Failed to create codec debugfs directory\n");
282 return;
283 }
284
aaee8ef1
MB
285 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
286 &codec->cache_sync);
287 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
288 &codec->cache_only);
289
2624d5fa
MB
290 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
291 codec->debugfs_codec_root,
292 codec, &codec_reg_fops);
293 if (!codec->debugfs_reg)
294 printk(KERN_WARNING
295 "ASoC: Failed to create codec register debugfs file\n");
296
8eecaf62 297 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
2624d5fa
MB
298}
299
300static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
301{
302 debugfs_remove_recursive(codec->debugfs_codec_root);
303}
304
c3c5a19a
MB
305static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
306 size_t count, loff_t *ppos)
307{
308 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2b194f9d 309 ssize_t len, ret = 0;
c3c5a19a
MB
310 struct snd_soc_codec *codec;
311
312 if (!buf)
313 return -ENOMEM;
314
2b194f9d
MB
315 list_for_each_entry(codec, &codec_list, list) {
316 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
317 codec->name);
318 if (len >= 0)
319 ret += len;
320 if (ret > PAGE_SIZE) {
321 ret = PAGE_SIZE;
322 break;
323 }
324 }
c3c5a19a
MB
325
326 if (ret >= 0)
327 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
328
329 kfree(buf);
330
331 return ret;
332}
333
334static const struct file_operations codec_list_fops = {
335 .read = codec_list_read_file,
336 .llseek = default_llseek,/* read accesses f_pos */
337};
338
f3208780
MB
339static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
340 size_t count, loff_t *ppos)
341{
342 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2b194f9d 343 ssize_t len, ret = 0;
f3208780
MB
344 struct snd_soc_dai *dai;
345
346 if (!buf)
347 return -ENOMEM;
348
2b194f9d
MB
349 list_for_each_entry(dai, &dai_list, list) {
350 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
351 if (len >= 0)
352 ret += len;
353 if (ret > PAGE_SIZE) {
354 ret = PAGE_SIZE;
355 break;
356 }
357 }
f3208780 358
2b194f9d 359 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
f3208780
MB
360
361 kfree(buf);
362
363 return ret;
364}
365
366static const struct file_operations dai_list_fops = {
367 .read = dai_list_read_file,
368 .llseek = default_llseek,/* read accesses f_pos */
369};
370
19c7ac27
MB
371static ssize_t platform_list_read_file(struct file *file,
372 char __user *user_buf,
373 size_t count, loff_t *ppos)
374{
375 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2b194f9d 376 ssize_t len, ret = 0;
19c7ac27
MB
377 struct snd_soc_platform *platform;
378
379 if (!buf)
380 return -ENOMEM;
381
2b194f9d
MB
382 list_for_each_entry(platform, &platform_list, list) {
383 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
384 platform->name);
385 if (len >= 0)
386 ret += len;
387 if (ret > PAGE_SIZE) {
388 ret = PAGE_SIZE;
389 break;
390 }
391 }
19c7ac27 392
2b194f9d 393 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
19c7ac27
MB
394
395 kfree(buf);
396
397 return ret;
398}
399
400static const struct file_operations platform_list_fops = {
401 .read = platform_list_read_file,
402 .llseek = default_llseek,/* read accesses f_pos */
403};
404
a6052154
JN
405static void soc_init_card_debugfs(struct snd_soc_card *card)
406{
407 card->debugfs_card_root = debugfs_create_dir(card->name,
8a9dab1a 408 snd_soc_debugfs_root);
3a45b867 409 if (!card->debugfs_card_root) {
a6052154 410 dev_warn(card->dev,
7c08be84 411 "ASoC: Failed to create card debugfs directory\n");
3a45b867
JN
412 return;
413 }
414
415 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
416 card->debugfs_card_root,
417 &card->pop_time);
418 if (!card->debugfs_pop_time)
419 dev_warn(card->dev,
420 "Failed to create pop time debugfs file\n");
a6052154
JN
421}
422
423static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
424{
425 debugfs_remove_recursive(card->debugfs_card_root);
426}
427
2624d5fa
MB
428#else
429
430static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
431{
432}
433
434static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
435{
436}
b95fccbc
AL
437
438static inline void soc_init_card_debugfs(struct snd_soc_card *card)
439{
440}
441
442static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
443{
444}
2624d5fa
MB
445#endif
446
db2a4165
FM
447#ifdef CONFIG_SND_SOC_AC97_BUS
448/* unregister ac97 codec */
449static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
450{
451 if (codec->ac97->dev.bus)
452 device_unregister(&codec->ac97->dev);
453 return 0;
454}
455
456/* stop no dev release warning */
457static void soc_ac97_device_release(struct device *dev){}
458
459/* register ac97 codec to bus */
460static int soc_ac97_dev_register(struct snd_soc_codec *codec)
461{
462 int err;
463
464 codec->ac97->dev.bus = &ac97_bus_type;
4ac5c61f 465 codec->ac97->dev.parent = codec->card->dev;
db2a4165
FM
466 codec->ac97->dev.release = soc_ac97_device_release;
467
bb072bf0 468 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
f0fba2ad 469 codec->card->snd_card->number, 0, codec->name);
db2a4165
FM
470 err = device_register(&codec->ac97->dev);
471 if (err < 0) {
472 snd_printk(KERN_ERR "Can't register ac97 bus\n");
473 codec->ac97->dev.bus = NULL;
474 return err;
475 }
476 return 0;
477}
478#endif
479
6f8ab4ac 480#ifdef CONFIG_PM_SLEEP
db2a4165 481/* powers down audio subsystem for suspend */
6f8ab4ac 482int snd_soc_suspend(struct device *dev)
db2a4165 483{
6f8ab4ac 484 struct snd_soc_card *card = dev_get_drvdata(dev);
2eea392d 485 struct snd_soc_codec *codec;
db2a4165
FM
486 int i;
487
e3509ff0
DM
488 /* If the initialization of this soc device failed, there is no codec
489 * associated with it. Just bail out in this case.
490 */
f0fba2ad 491 if (list_empty(&card->codec_dev_list))
e3509ff0
DM
492 return 0;
493
6ed25978
AG
494 /* Due to the resume being scheduled into a workqueue we could
495 * suspend before that's finished - wait for it to complete.
496 */
f0fba2ad
LG
497 snd_power_lock(card->snd_card);
498 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
499 snd_power_unlock(card->snd_card);
6ed25978
AG
500
501 /* we're going to block userspace touching us until resume completes */
f0fba2ad 502 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
6ed25978 503
a00f90f9 504 /* mute any active DACs */
f0fba2ad
LG
505 for (i = 0; i < card->num_rtd; i++) {
506 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
507 struct snd_soc_dai_driver *drv = dai->driver;
3efab7dc 508
f0fba2ad 509 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
510 continue;
511
f0fba2ad
LG
512 if (drv->ops->digital_mute && dai->playback_active)
513 drv->ops->digital_mute(dai, 1);
db2a4165
FM
514 }
515
4ccab3e7 516 /* suspend all pcms */
f0fba2ad
LG
517 for (i = 0; i < card->num_rtd; i++) {
518 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
519 continue;
520
f0fba2ad 521 snd_pcm_suspend_all(card->rtd[i].pcm);
3efab7dc 522 }
4ccab3e7 523
87506549 524 if (card->suspend_pre)
70b2ac12 525 card->suspend_pre(card);
db2a4165 526
f0fba2ad
LG
527 for (i = 0; i < card->num_rtd; i++) {
528 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
529 struct snd_soc_platform *platform = card->rtd[i].platform;
3efab7dc 530
f0fba2ad 531 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
532 continue;
533
f0fba2ad
LG
534 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
535 cpu_dai->driver->suspend(cpu_dai);
536 if (platform->driver->suspend && !platform->suspended) {
537 platform->driver->suspend(cpu_dai);
538 platform->suspended = 1;
539 }
db2a4165
FM
540 }
541
542 /* close any waiting streams and save state */
f0fba2ad 543 for (i = 0; i < card->num_rtd; i++) {
5b84ba26 544 flush_delayed_work_sync(&card->rtd[i].delayed_work);
ce6120cc 545 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
f0fba2ad 546 }
db2a4165 547
f0fba2ad
LG
548 for (i = 0; i < card->num_rtd; i++) {
549 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
3efab7dc 550
f0fba2ad 551 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
552 continue;
553
f0fba2ad
LG
554 if (driver->playback.stream_name != NULL)
555 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
db2a4165 556 SND_SOC_DAPM_STREAM_SUSPEND);
f0fba2ad
LG
557
558 if (driver->capture.stream_name != NULL)
559 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
db2a4165
FM
560 SND_SOC_DAPM_STREAM_SUSPEND);
561 }
562
f0fba2ad 563 /* suspend all CODECs */
2eea392d 564 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
f0fba2ad
LG
565 /* If there are paths active then the CODEC will be held with
566 * bias _ON and should not be suspended. */
567 if (!codec->suspended && codec->driver->suspend) {
ce6120cc 568 switch (codec->dapm.bias_level) {
f0fba2ad
LG
569 case SND_SOC_BIAS_STANDBY:
570 case SND_SOC_BIAS_OFF:
84b315ee 571 codec->driver->suspend(codec);
f0fba2ad 572 codec->suspended = 1;
7be4ba24 573 codec->cache_sync = 1;
f0fba2ad
LG
574 break;
575 default:
576 dev_dbg(codec->dev, "CODEC is on over suspend\n");
577 break;
578 }
1547aba9
MB
579 }
580 }
db2a4165 581
f0fba2ad
LG
582 for (i = 0; i < card->num_rtd; i++) {
583 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3efab7dc 584
f0fba2ad 585 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
586 continue;
587
f0fba2ad
LG
588 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
589 cpu_dai->driver->suspend(cpu_dai);
db2a4165
FM
590 }
591
87506549 592 if (card->suspend_post)
70b2ac12 593 card->suspend_post(card);
db2a4165
FM
594
595 return 0;
596}
6f8ab4ac 597EXPORT_SYMBOL_GPL(snd_soc_suspend);
db2a4165 598
6ed25978
AG
599/* deferred resume work, so resume can complete before we finished
600 * setting our codec back up, which can be very slow on I2C
601 */
602static void soc_resume_deferred(struct work_struct *work)
db2a4165 603{
f0fba2ad
LG
604 struct snd_soc_card *card =
605 container_of(work, struct snd_soc_card, deferred_resume_work);
2eea392d 606 struct snd_soc_codec *codec;
db2a4165
FM
607 int i;
608
6ed25978
AG
609 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
610 * so userspace apps are blocked from touching us
611 */
612
f0fba2ad 613 dev_dbg(card->dev, "starting resume work\n");
6ed25978 614
9949788b 615 /* Bring us up into D2 so that DAPM starts enabling things */
f0fba2ad 616 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
9949788b 617
87506549 618 if (card->resume_pre)
70b2ac12 619 card->resume_pre(card);
db2a4165 620
f0fba2ad
LG
621 /* resume AC97 DAIs */
622 for (i = 0; i < card->num_rtd; i++) {
623 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3efab7dc 624
f0fba2ad 625 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
626 continue;
627
f0fba2ad
LG
628 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
629 cpu_dai->driver->resume(cpu_dai);
630 }
631
2eea392d 632 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
f0fba2ad
LG
633 /* If the CODEC was idle over suspend then it will have been
634 * left with bias OFF or STANDBY and suspended so we must now
635 * resume. Otherwise the suspend was suppressed.
636 */
637 if (codec->driver->resume && codec->suspended) {
ce6120cc 638 switch (codec->dapm.bias_level) {
f0fba2ad
LG
639 case SND_SOC_BIAS_STANDBY:
640 case SND_SOC_BIAS_OFF:
641 codec->driver->resume(codec);
642 codec->suspended = 0;
643 break;
644 default:
645 dev_dbg(codec->dev, "CODEC was on over suspend\n");
646 break;
647 }
1547aba9
MB
648 }
649 }
db2a4165 650
f0fba2ad
LG
651 for (i = 0; i < card->num_rtd; i++) {
652 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
3efab7dc 653
f0fba2ad 654 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
655 continue;
656
f0fba2ad
LG
657 if (driver->playback.stream_name != NULL)
658 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
db2a4165 659 SND_SOC_DAPM_STREAM_RESUME);
f0fba2ad
LG
660
661 if (driver->capture.stream_name != NULL)
662 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
db2a4165
FM
663 SND_SOC_DAPM_STREAM_RESUME);
664 }
665
3ff3f64b 666 /* unmute any active DACs */
f0fba2ad
LG
667 for (i = 0; i < card->num_rtd; i++) {
668 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
669 struct snd_soc_dai_driver *drv = dai->driver;
3efab7dc 670
f0fba2ad 671 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
672 continue;
673
f0fba2ad
LG
674 if (drv->ops->digital_mute && dai->playback_active)
675 drv->ops->digital_mute(dai, 0);
db2a4165
FM
676 }
677
f0fba2ad
LG
678 for (i = 0; i < card->num_rtd; i++) {
679 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
680 struct snd_soc_platform *platform = card->rtd[i].platform;
3efab7dc 681
f0fba2ad 682 if (card->rtd[i].dai_link->ignore_suspend)
3efab7dc
MB
683 continue;
684
f0fba2ad
LG
685 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
686 cpu_dai->driver->resume(cpu_dai);
687 if (platform->driver->resume && platform->suspended) {
688 platform->driver->resume(cpu_dai);
689 platform->suspended = 0;
690 }
db2a4165
FM
691 }
692
87506549 693 if (card->resume_post)
70b2ac12 694 card->resume_post(card);
db2a4165 695
f0fba2ad 696 dev_dbg(card->dev, "resume work completed\n");
6ed25978
AG
697
698 /* userspace can access us now we are back as we were before */
f0fba2ad 699 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
6ed25978
AG
700}
701
702/* powers up audio subsystem after a suspend */
6f8ab4ac 703int snd_soc_resume(struct device *dev)
6ed25978 704{
6f8ab4ac 705 struct snd_soc_card *card = dev_get_drvdata(dev);
82e14e8b 706 int i, ac97_control = 0;
b9dd94a8 707
5ff1ddf2
EM
708 /* If the initialization of this soc device failed, there is no codec
709 * associated with it. Just bail out in this case.
710 */
711 if (list_empty(&card->codec_dev_list))
712 return 0;
713
64ab9baa
MB
714 /* AC97 devices might have other drivers hanging off them so
715 * need to resume immediately. Other drivers don't have that
716 * problem and may take a substantial amount of time to resume
717 * due to I/O costs and anti-pop so handle them out of line.
718 */
f0fba2ad
LG
719 for (i = 0; i < card->num_rtd; i++) {
720 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
82e14e8b
SW
721 ac97_control |= cpu_dai->driver->ac97_control;
722 }
723 if (ac97_control) {
724 dev_dbg(dev, "Resuming AC97 immediately\n");
725 soc_resume_deferred(&card->deferred_resume_work);
726 } else {
727 dev_dbg(dev, "Scheduling resume work\n");
728 if (!schedule_work(&card->deferred_resume_work))
729 dev_err(dev, "resume work item may be lost\n");
64ab9baa 730 }
6ed25978 731
db2a4165
FM
732 return 0;
733}
6f8ab4ac 734EXPORT_SYMBOL_GPL(snd_soc_resume);
db2a4165 735#else
6f8ab4ac
MB
736#define snd_soc_suspend NULL
737#define snd_soc_resume NULL
db2a4165
FM
738#endif
739
85e7652d 740static const struct snd_soc_dai_ops null_dai_ops = {
02a06d30
BS
741};
742
f0fba2ad 743static int soc_bind_dai_link(struct snd_soc_card *card, int num)
db2a4165 744{
f0fba2ad
LG
745 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
746 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
fe3e78e0 747 struct snd_soc_codec *codec;
435c5e25 748 struct snd_soc_platform *platform;
f0fba2ad 749 struct snd_soc_dai *codec_dai, *cpu_dai;
848dd8be 750 const char *platform_name;
435c5e25 751
f0fba2ad
LG
752 if (rtd->complete)
753 return 1;
754 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
435c5e25 755
f0fba2ad
LG
756 /* do we already have the CPU DAI for this link ? */
757 if (rtd->cpu_dai) {
758 goto find_codec;
759 }
760 /* no, then find CPU DAI from registered DAIs*/
761 list_for_each_entry(cpu_dai, &dai_list, list) {
5a504963
SW
762 if (dai_link->cpu_dai_of_node) {
763 if (cpu_dai->dev->of_node != dai_link->cpu_dai_of_node)
764 continue;
765 } else {
766 if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
767 continue;
768 }
2610ab77
SW
769
770 rtd->cpu_dai = cpu_dai;
771 goto find_codec;
435c5e25 772 }
f0fba2ad
LG
773 dev_dbg(card->dev, "CPU DAI %s not registered\n",
774 dai_link->cpu_dai_name);
6308419a 775
f0fba2ad
LG
776find_codec:
777 /* do we already have the CODEC for this link ? */
778 if (rtd->codec) {
779 goto find_platform;
780 }
781
782 /* no, then find CODEC from registered CODECs*/
783 list_for_each_entry(codec, &codec_list, list) {
5a504963
SW
784 if (dai_link->codec_of_node) {
785 if (codec->dev->of_node != dai_link->codec_of_node)
786 continue;
787 } else {
788 if (strcmp(codec->name, dai_link->codec_name))
789 continue;
790 }
2610ab77
SW
791
792 rtd->codec = codec;
793
794 /*
795 * CODEC found, so find CODEC DAI from registered DAIs from
796 * this CODEC
797 */
798 list_for_each_entry(codec_dai, &dai_list, list) {
799 if (codec->dev == codec_dai->dev &&
800 !strcmp(codec_dai->name,
801 dai_link->codec_dai_name)) {
f0fba2ad 802
2610ab77
SW
803 rtd->codec_dai = codec_dai;
804 goto find_platform;
805 }
435c5e25 806 }
2610ab77
SW
807 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
808 dai_link->codec_dai_name);
809
810 goto find_platform;
f0fba2ad
LG
811 }
812 dev_dbg(card->dev, "CODEC %s not registered\n",
813 dai_link->codec_name);
6b05eda6 814
f0fba2ad 815find_platform:
848dd8be
MB
816 /* do we need a platform? */
817 if (rtd->platform)
f0fba2ad 818 goto out;
848dd8be
MB
819
820 /* if there's no platform we match on the empty platform */
821 platform_name = dai_link->platform_name;
5a504963 822 if (!platform_name && !dai_link->platform_of_node)
848dd8be
MB
823 platform_name = "snd-soc-dummy";
824
825 /* no, then find one from the set of registered platforms */
f0fba2ad 826 list_for_each_entry(platform, &platform_list, list) {
5a504963
SW
827 if (dai_link->platform_of_node) {
828 if (platform->dev->of_node !=
829 dai_link->platform_of_node)
830 continue;
831 } else {
832 if (strcmp(platform->name, platform_name))
833 continue;
834 }
2610ab77
SW
835
836 rtd->platform = platform;
837 goto out;
02a06d30
BS
838 }
839
f0fba2ad
LG
840 dev_dbg(card->dev, "platform %s not registered\n",
841 dai_link->platform_name);
842 return 0;
843
844out:
845 /* mark rtd as complete if we found all 4 of our client devices */
846 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
847 rtd->complete = 1;
848 card->num_rtd++;
849 }
850 return 1;
851}
852
589c3563
JN
853static void soc_remove_codec(struct snd_soc_codec *codec)
854{
855 int err;
856
857 if (codec->driver->remove) {
858 err = codec->driver->remove(codec);
859 if (err < 0)
860 dev_err(codec->dev,
861 "asoc: failed to remove %s: %d\n",
862 codec->name, err);
863 }
864
865 /* Make sure all DAPM widgets are freed */
866 snd_soc_dapm_free(&codec->dapm);
867
868 soc_cleanup_codec_debugfs(codec);
869 codec->probed = 0;
870 list_del(&codec->card_list);
871 module_put(codec->dev->driver->owner);
872}
873
0168bf0d 874static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
f0fba2ad
LG
875{
876 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
877 struct snd_soc_codec *codec = rtd->codec;
878 struct snd_soc_platform *platform = rtd->platform;
879 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
880 int err;
881
882 /* unregister the rtd device */
883 if (rtd->dev_registered) {
36ae1a96
MB
884 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
885 device_remove_file(rtd->dev, &dev_attr_codec_reg);
886 device_unregister(rtd->dev);
f0fba2ad
LG
887 rtd->dev_registered = 0;
888 }
889
890 /* remove the CODEC DAI */
0168bf0d
LG
891 if (codec_dai && codec_dai->probed &&
892 codec_dai->driver->remove_order == order) {
f0fba2ad
LG
893 if (codec_dai->driver->remove) {
894 err = codec_dai->driver->remove(codec_dai);
895 if (err < 0)
896 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
6b05eda6 897 }
f0fba2ad
LG
898 codec_dai->probed = 0;
899 list_del(&codec_dai->card_list);
900 }
6b05eda6 901
f0fba2ad 902 /* remove the platform */
0168bf0d
LG
903 if (platform && platform->probed &&
904 platform->driver->remove_order == order) {
f0fba2ad
LG
905 if (platform->driver->remove) {
906 err = platform->driver->remove(platform);
907 if (err < 0)
908 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
909 }
910 platform->probed = 0;
911 list_del(&platform->card_list);
912 module_put(platform->dev->driver->owner);
913 }
435c5e25 914
f0fba2ad 915 /* remove the CODEC */
0168bf0d
LG
916 if (codec && codec->probed &&
917 codec->driver->remove_order == order)
589c3563 918 soc_remove_codec(codec);
db2a4165 919
f0fba2ad 920 /* remove the cpu_dai */
0168bf0d
LG
921 if (cpu_dai && cpu_dai->probed &&
922 cpu_dai->driver->remove_order == order) {
f0fba2ad
LG
923 if (cpu_dai->driver->remove) {
924 err = cpu_dai->driver->remove(cpu_dai);
925 if (err < 0)
926 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
db2a4165 927 }
f0fba2ad
LG
928 cpu_dai->probed = 0;
929 list_del(&cpu_dai->card_list);
930 module_put(cpu_dai->dev->driver->owner);
db2a4165 931 }
f0fba2ad 932}
db2a4165 933
0671fd8e
KM
934static void soc_remove_dai_links(struct snd_soc_card *card)
935{
0168bf0d 936 int dai, order;
0671fd8e 937
0168bf0d
LG
938 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
939 order++) {
940 for (dai = 0; dai < card->num_rtd; dai++)
941 soc_remove_dai_link(card, dai, order);
942 }
0671fd8e
KM
943 card->num_rtd = 0;
944}
945
ead9b919
JN
946static void soc_set_name_prefix(struct snd_soc_card *card,
947 struct snd_soc_codec *codec)
948{
949 int i;
950
ff819b83 951 if (card->codec_conf == NULL)
ead9b919
JN
952 return;
953
ff819b83
DP
954 for (i = 0; i < card->num_configs; i++) {
955 struct snd_soc_codec_conf *map = &card->codec_conf[i];
ead9b919
JN
956 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
957 codec->name_prefix = map->name_prefix;
958 break;
959 }
960 }
961}
962
589c3563
JN
963static int soc_probe_codec(struct snd_soc_card *card,
964 struct snd_soc_codec *codec)
965{
966 int ret = 0;
89b95ac0 967 const struct snd_soc_codec_driver *driver = codec->driver;
589c3563
JN
968
969 codec->card = card;
970 codec->dapm.card = card;
971 soc_set_name_prefix(card, codec);
972
70d29331
JN
973 if (!try_module_get(codec->dev->driver->owner))
974 return -ENODEV;
975
d5d1e0be
LPC
976 soc_init_codec_debugfs(codec);
977
77530150
LPC
978 if (driver->dapm_widgets)
979 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
980 driver->num_dapm_widgets);
981
33c5f969
MB
982 codec->dapm.idle_bias_off = driver->idle_bias_off;
983
89b95ac0
MB
984 if (driver->probe) {
985 ret = driver->probe(codec);
589c3563
JN
986 if (ret < 0) {
987 dev_err(codec->dev,
988 "asoc: failed to probe CODEC %s: %d\n",
989 codec->name, ret);
70d29331 990 goto err_probe;
589c3563
JN
991 }
992 }
993
b7af1daf
MB
994 if (driver->controls)
995 snd_soc_add_controls(codec, driver->controls,
996 driver->num_controls);
89b95ac0
MB
997 if (driver->dapm_routes)
998 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
999 driver->num_dapm_routes);
1000
589c3563
JN
1001 /* mark codec as probed and add to card codec list */
1002 codec->probed = 1;
1003 list_add(&codec->card_list, &card->codec_dev_list);
7be31be8 1004 list_add(&codec->dapm.list, &card->dapm_list);
589c3563 1005
70d29331
JN
1006 return 0;
1007
1008err_probe:
d5d1e0be 1009 soc_cleanup_codec_debugfs(codec);
70d29331
JN
1010 module_put(codec->dev->driver->owner);
1011
589c3563
JN
1012 return ret;
1013}
1014
956245e9
LG
1015static int soc_probe_platform(struct snd_soc_card *card,
1016 struct snd_soc_platform *platform)
1017{
1018 int ret = 0;
1019 const struct snd_soc_platform_driver *driver = platform->driver;
1020
1021 platform->card = card;
b7950641 1022 platform->dapm.card = card;
956245e9
LG
1023
1024 if (!try_module_get(platform->dev->driver->owner))
1025 return -ENODEV;
1026
cb2cf612
LG
1027 if (driver->dapm_widgets)
1028 snd_soc_dapm_new_controls(&platform->dapm,
1029 driver->dapm_widgets, driver->num_dapm_widgets);
1030
956245e9
LG
1031 if (driver->probe) {
1032 ret = driver->probe(platform);
1033 if (ret < 0) {
1034 dev_err(platform->dev,
1035 "asoc: failed to probe platform %s: %d\n",
1036 platform->name, ret);
1037 goto err_probe;
1038 }
1039 }
1040
cb2cf612
LG
1041 if (driver->controls)
1042 snd_soc_add_platform_controls(platform, driver->controls,
1043 driver->num_controls);
1044 if (driver->dapm_routes)
1045 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1046 driver->num_dapm_routes);
1047
956245e9
LG
1048 /* mark platform as probed and add to card platform list */
1049 platform->probed = 1;
1050 list_add(&platform->card_list, &card->platform_dev_list);
b7950641 1051 list_add(&platform->dapm.list, &card->dapm_list);
956245e9
LG
1052
1053 return 0;
1054
1055err_probe:
1056 module_put(platform->dev->driver->owner);
1057
1058 return ret;
1059}
1060
36ae1a96
MB
1061static void rtd_release(struct device *dev)
1062{
1063 kfree(dev);
1064}
f0fba2ad 1065
589c3563
JN
1066static int soc_post_component_init(struct snd_soc_card *card,
1067 struct snd_soc_codec *codec,
1068 int num, int dailess)
1069{
1070 struct snd_soc_dai_link *dai_link = NULL;
1071 struct snd_soc_aux_dev *aux_dev = NULL;
1072 struct snd_soc_pcm_runtime *rtd;
1073 const char *temp, *name;
1074 int ret = 0;
1075
1076 if (!dailess) {
1077 dai_link = &card->dai_link[num];
1078 rtd = &card->rtd[num];
1079 name = dai_link->name;
1080 } else {
1081 aux_dev = &card->aux_dev[num];
1082 rtd = &card->rtd_aux[num];
1083 name = aux_dev->name;
1084 }
0962bb21 1085 rtd->card = card;
589c3563 1086
4b1cfcb4
MB
1087 /* Make sure all DAPM widgets are instantiated */
1088 snd_soc_dapm_new_widgets(&codec->dapm);
1089
589c3563
JN
1090 /* machine controls, routes and widgets are not prefixed */
1091 temp = codec->name_prefix;
1092 codec->name_prefix = NULL;
1093
1094 /* do machine specific initialization */
1095 if (!dailess && dai_link->init)
1096 ret = dai_link->init(rtd);
1097 else if (dailess && aux_dev->init)
1098 ret = aux_dev->init(&codec->dapm);
1099 if (ret < 0) {
1100 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1101 return ret;
1102 }
1103 codec->name_prefix = temp;
1104
589c3563
JN
1105 /* register the rtd device */
1106 rtd->codec = codec;
36ae1a96
MB
1107
1108 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1109 if (!rtd->dev)
1110 return -ENOMEM;
1111 device_initialize(rtd->dev);
1112 rtd->dev->parent = card->dev;
1113 rtd->dev->release = rtd_release;
1114 rtd->dev->init_name = name;
1115 dev_set_drvdata(rtd->dev, rtd);
b8c0dab9 1116 mutex_init(&rtd->pcm_mutex);
36ae1a96 1117 ret = device_add(rtd->dev);
589c3563
JN
1118 if (ret < 0) {
1119 dev_err(card->dev,
1120 "asoc: failed to register runtime device: %d\n", ret);
1121 return ret;
1122 }
1123 rtd->dev_registered = 1;
1124
1125 /* add DAPM sysfs entries for this codec */
36ae1a96 1126 ret = snd_soc_dapm_sys_add(rtd->dev);
589c3563
JN
1127 if (ret < 0)
1128 dev_err(codec->dev,
1129 "asoc: failed to add codec dapm sysfs entries: %d\n",
1130 ret);
1131
1132 /* add codec sysfs entries */
36ae1a96 1133 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
589c3563
JN
1134 if (ret < 0)
1135 dev_err(codec->dev,
1136 "asoc: failed to add codec sysfs files: %d\n", ret);
1137
1138 return 0;
1139}
1140
0168bf0d 1141static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
f0fba2ad
LG
1142{
1143 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1144 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1145 struct snd_soc_codec *codec = rtd->codec;
1146 struct snd_soc_platform *platform = rtd->platform;
1147 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1148 int ret;
1149
0168bf0d
LG
1150 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1151 card->name, num, order);
f0fba2ad
LG
1152
1153 /* config components */
1154 codec_dai->codec = codec;
f0fba2ad 1155 cpu_dai->platform = platform;
f0fba2ad
LG
1156 codec_dai->card = card;
1157 cpu_dai->card = card;
1158
1159 /* set default power off timeout */
1160 rtd->pmdown_time = pmdown_time;
1161
1162 /* probe the cpu_dai */
0168bf0d
LG
1163 if (!cpu_dai->probed &&
1164 cpu_dai->driver->probe_order == order) {
61b61e3c
LG
1165 if (!try_module_get(cpu_dai->dev->driver->owner))
1166 return -ENODEV;
1167
f0fba2ad
LG
1168 if (cpu_dai->driver->probe) {
1169 ret = cpu_dai->driver->probe(cpu_dai);
1170 if (ret < 0) {
1171 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1172 cpu_dai->name);
61b61e3c 1173 module_put(cpu_dai->dev->driver->owner);
f0fba2ad
LG
1174 return ret;
1175 }
1176 }
1177 cpu_dai->probed = 1;
1c8371d6 1178 /* mark cpu_dai as probed and add to card dai list */
f0fba2ad 1179 list_add(&cpu_dai->card_list, &card->dai_dev_list);
db2a4165
FM
1180 }
1181
f0fba2ad 1182 /* probe the CODEC */
0168bf0d
LG
1183 if (!codec->probed &&
1184 codec->driver->probe_order == order) {
589c3563
JN
1185 ret = soc_probe_codec(card, codec);
1186 if (ret < 0)
1187 return ret;
db2a4165
FM
1188 }
1189
f0fba2ad 1190 /* probe the platform */
0168bf0d
LG
1191 if (!platform->probed &&
1192 platform->driver->probe_order == order) {
956245e9
LG
1193 ret = soc_probe_platform(card, platform);
1194 if (ret < 0)
1195 return ret;
f0fba2ad 1196 }
6ed25978 1197
f0fba2ad 1198 /* probe the CODEC DAI */
0168bf0d 1199 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
f0fba2ad
LG
1200 if (codec_dai->driver->probe) {
1201 ret = codec_dai->driver->probe(codec_dai);
fe3e78e0 1202 if (ret < 0) {
f0fba2ad
LG
1203 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1204 codec_dai->name);
1205 return ret;
fe3e78e0
MB
1206 }
1207 }
f0fba2ad 1208
1c8371d6 1209 /* mark codec_dai as probed and add to card dai list */
f0fba2ad
LG
1210 codec_dai->probed = 1;
1211 list_add(&codec_dai->card_list, &card->dai_dev_list);
fe3e78e0
MB
1212 }
1213
0168bf0d
LG
1214 /* complete DAI probe during last probe */
1215 if (order != SND_SOC_COMP_ORDER_LAST)
1216 return 0;
1217
589c3563
JN
1218 ret = soc_post_component_init(card, codec, num, 0);
1219 if (ret)
f0fba2ad 1220 return ret;
fe3e78e0 1221
36ae1a96 1222 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
f0fba2ad
LG
1223 if (ret < 0)
1224 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1225
f0fba2ad
LG
1226 /* create the pcm */
1227 ret = soc_new_pcm(rtd, num);
1228 if (ret < 0) {
1229 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1230 return ret;
1231 }
1232
1233 /* add platform data for AC97 devices */
1234 if (rtd->codec_dai->driver->ac97_control)
1235 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1236
1237 return 0;
1238}
1239
fe3e78e0 1240#ifdef CONFIG_SND_SOC_AC97_BUS
f0fba2ad
LG
1241static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1242{
1243 int ret;
1244
fe3e78e0
MB
1245 /* Only instantiate AC97 if not already done by the adaptor
1246 * for the generic AC97 subsystem.
1247 */
f0fba2ad 1248 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
0562f788
MW
1249 /*
1250 * It is possible that the AC97 device is already registered to
1251 * the device subsystem. This happens when the device is created
1252 * via snd_ac97_mixer(). Currently only SoC codec that does so
1253 * is the generic AC97 glue but others migh emerge.
1254 *
1255 * In those cases we don't try to register the device again.
1256 */
1257 if (!rtd->codec->ac97_created)
1258 return 0;
f0fba2ad
LG
1259
1260 ret = soc_ac97_dev_register(rtd->codec);
fe3e78e0
MB
1261 if (ret < 0) {
1262 printk(KERN_ERR "asoc: AC97 device register failed\n");
f0fba2ad 1263 return ret;
fe3e78e0 1264 }
f0fba2ad
LG
1265
1266 rtd->codec->ac97_registered = 1;
fe3e78e0 1267 }
f0fba2ad
LG
1268 return 0;
1269}
1270
1271static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1272{
1273 if (codec->ac97_registered) {
1274 soc_ac97_dev_unregister(codec);
1275 codec->ac97_registered = 0;
1276 }
1277}
fe3e78e0
MB
1278#endif
1279
2eea392d
JN
1280static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1281{
1282 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
2eea392d 1283 struct snd_soc_codec *codec;
676ad98a 1284 int ret = -ENODEV;
2eea392d
JN
1285
1286 /* find CODEC from registered CODECs*/
1287 list_for_each_entry(codec, &codec_list, list) {
1288 if (!strcmp(codec->name, aux_dev->codec_name)) {
1289 if (codec->probed) {
1290 dev_err(codec->dev,
1291 "asoc: codec already probed");
1292 ret = -EBUSY;
1293 goto out;
1294 }
676ad98a 1295 goto found;
2eea392d
JN
1296 }
1297 }
676ad98a
JN
1298 /* codec not found */
1299 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1300 goto out;
2eea392d 1301
676ad98a 1302found:
589c3563 1303 ret = soc_probe_codec(card, codec);
2eea392d 1304 if (ret < 0)
589c3563 1305 return ret;
2eea392d 1306
589c3563 1307 ret = soc_post_component_init(card, codec, num, 1);
2eea392d
JN
1308
1309out:
1310 return ret;
1311}
1312
1313static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1314{
1315 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1316 struct snd_soc_codec *codec = rtd->codec;
2eea392d
JN
1317
1318 /* unregister the rtd device */
1319 if (rtd->dev_registered) {
36ae1a96
MB
1320 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1321 device_del(rtd->dev);
2eea392d
JN
1322 rtd->dev_registered = 0;
1323 }
1324
589c3563
JN
1325 if (codec && codec->probed)
1326 soc_remove_codec(codec);
2eea392d
JN
1327}
1328
fdf0f54d
DP
1329static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1330 enum snd_soc_compress_type compress_type)
1331{
1332 int ret;
1333
1334 if (codec->cache_init)
1335 return 0;
1336
1337 /* override the compress_type if necessary */
1338 if (compress_type && codec->compress_type != compress_type)
1339 codec->compress_type = compress_type;
fdf0f54d
DP
1340 ret = snd_soc_cache_init(codec);
1341 if (ret < 0) {
1342 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1343 ret);
1344 return ret;
1345 }
1346 codec->cache_init = 1;
1347 return 0;
1348}
1349
f0fba2ad
LG
1350static void snd_soc_instantiate_card(struct snd_soc_card *card)
1351{
fdf0f54d
DP
1352 struct snd_soc_codec *codec;
1353 struct snd_soc_codec_conf *codec_conf;
1354 enum snd_soc_compress_type compress_type;
75d9ac46 1355 struct snd_soc_dai_link *dai_link;
0168bf0d 1356 int ret, i, order;
fe3e78e0 1357
f0fba2ad 1358 mutex_lock(&card->mutex);
dbe21408 1359
f0fba2ad
LG
1360 if (card->instantiated) {
1361 mutex_unlock(&card->mutex);
1362 return;
1363 }
fe3e78e0 1364
f0fba2ad
LG
1365 /* bind DAIs */
1366 for (i = 0; i < card->num_links; i++)
1367 soc_bind_dai_link(card, i);
fe3e78e0 1368
f0fba2ad
LG
1369 /* bind completed ? */
1370 if (card->num_rtd != card->num_links) {
1371 mutex_unlock(&card->mutex);
1372 return;
1373 }
435c5e25 1374
fdf0f54d
DP
1375 /* initialize the register cache for each available codec */
1376 list_for_each_entry(codec, &codec_list, list) {
1377 if (codec->cache_init)
1378 continue;
861f2faf
DP
1379 /* by default we don't override the compress_type */
1380 compress_type = 0;
fdf0f54d
DP
1381 /* check to see if we need to override the compress_type */
1382 for (i = 0; i < card->num_configs; ++i) {
1383 codec_conf = &card->codec_conf[i];
1384 if (!strcmp(codec->name, codec_conf->dev_name)) {
1385 compress_type = codec_conf->compress_type;
1386 if (compress_type && compress_type
1387 != codec->compress_type)
1388 break;
1389 }
1390 }
fdf0f54d
DP
1391 ret = snd_soc_init_codec_cache(codec, compress_type);
1392 if (ret < 0) {
1393 mutex_unlock(&card->mutex);
1394 return;
1395 }
1396 }
1397
f0fba2ad
LG
1398 /* card bind complete so register a sound card */
1399 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1400 card->owner, 0, &card->snd_card);
1401 if (ret < 0) {
1402 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1403 card->name);
1404 mutex_unlock(&card->mutex);
1405 return;
1406 }
1407 card->snd_card->dev = card->dev;
1408
e37a4970
MB
1409 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1410 card->dapm.dev = card->dev;
1411 card->dapm.card = card;
1412 list_add(&card->dapm.list, &card->dapm_list);
1413
d5d1e0be
LPC
1414#ifdef CONFIG_DEBUG_FS
1415 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1416#endif
1417
88ee1c61 1418#ifdef CONFIG_PM_SLEEP
f0fba2ad
LG
1419 /* deferred resume work */
1420 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1421#endif
db2a4165 1422
9a841ebb
MB
1423 if (card->dapm_widgets)
1424 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1425 card->num_dapm_widgets);
1426
f0fba2ad
LG
1427 /* initialise the sound card only once */
1428 if (card->probe) {
e7361ec4 1429 ret = card->probe(card);
f0fba2ad
LG
1430 if (ret < 0)
1431 goto card_probe_error;
1432 }
fe3e78e0 1433
0168bf0d
LG
1434 /* early DAI link probe */
1435 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1436 order++) {
1437 for (i = 0; i < card->num_links; i++) {
1438 ret = soc_probe_dai_link(card, i, order);
1439 if (ret < 0) {
1440 pr_err("asoc: failed to instantiate card %s: %d\n",
321de0d0 1441 card->name, ret);
0168bf0d
LG
1442 goto probe_dai_err;
1443 }
f0fba2ad
LG
1444 }
1445 }
db2a4165 1446
2eea392d
JN
1447 for (i = 0; i < card->num_aux_devs; i++) {
1448 ret = soc_probe_aux_dev(card, i);
1449 if (ret < 0) {
1450 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1451 card->name, ret);
1452 goto probe_aux_dev_err;
1453 }
1454 }
1455
b7af1daf
MB
1456 /* We should have a non-codec control add function but we don't */
1457 if (card->controls)
1458 snd_soc_add_controls(list_first_entry(&card->codec_dev_list,
1459 struct snd_soc_codec,
1460 card_list),
1461 card->controls,
1462 card->num_controls);
1463
b8ad29de
MB
1464 if (card->dapm_routes)
1465 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1466 card->num_dapm_routes);
1467
b90d2f90
MB
1468 snd_soc_dapm_new_widgets(&card->dapm);
1469
75d9ac46
MB
1470 for (i = 0; i < card->num_links; i++) {
1471 dai_link = &card->dai_link[i];
1472
1473 if (dai_link->dai_fmt) {
1474 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1475 dai_link->dai_fmt);
1476 if (ret != 0)
1477 dev_warn(card->rtd[i].codec_dai->dev,
1478 "Failed to set DAI format: %d\n",
1479 ret);
1480
1481 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1482 dai_link->dai_fmt);
1483 if (ret != 0)
1484 dev_warn(card->rtd[i].cpu_dai->dev,
1485 "Failed to set DAI format: %d\n",
1486 ret);
1487 }
1488 }
1489
f0fba2ad 1490 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
f0fba2ad 1491 "%s", card->name);
22de71ba
LG
1492 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1493 "%s", card->long_name ? card->long_name : card->name);
f0e8ed85
MB
1494 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1495 "%s", card->driver_name ? card->driver_name : card->name);
1496 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1497 switch (card->snd_card->driver[i]) {
1498 case '_':
1499 case '-':
1500 case '\0':
1501 break;
1502 default:
1503 if (!isalnum(card->snd_card->driver[i]))
1504 card->snd_card->driver[i] = '_';
1505 break;
1506 }
1507 }
f0fba2ad 1508
28e9ad92
MB
1509 if (card->late_probe) {
1510 ret = card->late_probe(card);
1511 if (ret < 0) {
1512 dev_err(card->dev, "%s late_probe() failed: %d\n",
1513 card->name, ret);
1514 goto probe_aux_dev_err;
1515 }
1516 }
1517
2dc00213
MB
1518 snd_soc_dapm_new_widgets(&card->dapm);
1519
1633281b 1520 if (card->fully_routed)
b05d8dc1 1521 list_for_each_entry(codec, &card->codec_dev_list, card_list)
1633281b
SW
1522 snd_soc_dapm_auto_nc_codec_pins(codec);
1523
f0fba2ad
LG
1524 ret = snd_card_register(card->snd_card);
1525 if (ret < 0) {
1526 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
6b3ed785 1527 goto probe_aux_dev_err;
db2a4165
FM
1528 }
1529
f0fba2ad
LG
1530#ifdef CONFIG_SND_SOC_AC97_BUS
1531 /* register any AC97 codecs */
1532 for (i = 0; i < card->num_rtd; i++) {
6b3ed785
AL
1533 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1534 if (ret < 0) {
1535 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1536 while (--i >= 0)
7d8316df 1537 soc_unregister_ac97_dai_link(card->rtd[i].codec);
6b3ed785 1538 goto probe_aux_dev_err;
f0fba2ad 1539 }
6b3ed785 1540 }
f0fba2ad
LG
1541#endif
1542
1543 card->instantiated = 1;
4f4c0072 1544 snd_soc_dapm_sync(&card->dapm);
f0fba2ad
LG
1545 mutex_unlock(&card->mutex);
1546 return;
1547
2eea392d
JN
1548probe_aux_dev_err:
1549 for (i = 0; i < card->num_aux_devs; i++)
1550 soc_remove_aux_dev(card, i);
1551
f0fba2ad 1552probe_dai_err:
0671fd8e 1553 soc_remove_dai_links(card);
f0fba2ad
LG
1554
1555card_probe_error:
87506549 1556 if (card->remove)
e7361ec4 1557 card->remove(card);
f0fba2ad
LG
1558
1559 snd_card_free(card->snd_card);
1560
1561 mutex_unlock(&card->mutex);
435c5e25 1562}
db2a4165 1563
435c5e25 1564/*
421f91d2 1565 * Attempt to initialise any uninitialised cards. Must be called with
435c5e25
MB
1566 * client_mutex.
1567 */
1568static void snd_soc_instantiate_cards(void)
1569{
1570 struct snd_soc_card *card;
1571 list_for_each_entry(card, &card_list, list)
1572 snd_soc_instantiate_card(card);
1573}
1574
1575/* probes a new socdev */
1576static int soc_probe(struct platform_device *pdev)
1577{
f0fba2ad 1578 struct snd_soc_card *card = platform_get_drvdata(pdev);
435c5e25 1579 int ret = 0;
435c5e25 1580
70a7ca34
VK
1581 /*
1582 * no card, so machine driver should be registering card
1583 * we should not be here in that case so ret error
1584 */
1585 if (!card)
1586 return -EINVAL;
1587
435c5e25
MB
1588 /* Bodge while we unpick instantiation */
1589 card->dev = &pdev->dev;
f0fba2ad 1590
435c5e25
MB
1591 ret = snd_soc_register_card(card);
1592 if (ret != 0) {
1593 dev_err(&pdev->dev, "Failed to register card\n");
1594 return ret;
1595 }
1596
1597 return 0;
db2a4165
FM
1598}
1599
b0e26485 1600static int soc_cleanup_card_resources(struct snd_soc_card *card)
db2a4165
FM
1601{
1602 int i;
db2a4165 1603
b0e26485
VK
1604 /* make sure any delayed work runs */
1605 for (i = 0; i < card->num_rtd; i++) {
1606 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1607 flush_delayed_work_sync(&rtd->delayed_work);
1608 }
914dc182 1609
b0e26485
VK
1610 /* remove auxiliary devices */
1611 for (i = 0; i < card->num_aux_devs; i++)
1612 soc_remove_aux_dev(card, i);
db2a4165 1613
b0e26485 1614 /* remove and free each DAI */
0671fd8e 1615 soc_remove_dai_links(card);
2eea392d 1616
b0e26485 1617 soc_cleanup_card_debugfs(card);
f0fba2ad 1618
b0e26485
VK
1619 /* remove the card */
1620 if (card->remove)
e7361ec4 1621 card->remove(card);
a6052154 1622
0aaae527
LPC
1623 snd_soc_dapm_free(&card->dapm);
1624
b0e26485
VK
1625 kfree(card->rtd);
1626 snd_card_free(card->snd_card);
1627 return 0;
1628
1629}
1630
1631/* removes a socdev */
1632static int soc_remove(struct platform_device *pdev)
1633{
1634 struct snd_soc_card *card = platform_get_drvdata(pdev);
db2a4165 1635
c5af3a2e 1636 snd_soc_unregister_card(card);
db2a4165
FM
1637 return 0;
1638}
1639
6f8ab4ac 1640int snd_soc_poweroff(struct device *dev)
51737470 1641{
6f8ab4ac 1642 struct snd_soc_card *card = dev_get_drvdata(dev);
f0fba2ad 1643 int i;
51737470
MB
1644
1645 if (!card->instantiated)
416356fc 1646 return 0;
51737470
MB
1647
1648 /* Flush out pmdown_time work - we actually do want to run it
1649 * now, we're shutting down so no imminent restart. */
f0fba2ad
LG
1650 for (i = 0; i < card->num_rtd; i++) {
1651 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
5b84ba26 1652 flush_delayed_work_sync(&rtd->delayed_work);
f0fba2ad 1653 }
51737470 1654
f0fba2ad 1655 snd_soc_dapm_shutdown(card);
416356fc
MB
1656
1657 return 0;
51737470 1658}
6f8ab4ac 1659EXPORT_SYMBOL_GPL(snd_soc_poweroff);
51737470 1660
6f8ab4ac
MB
1661const struct dev_pm_ops snd_soc_pm_ops = {
1662 .suspend = snd_soc_suspend,
1663 .resume = snd_soc_resume,
1664 .poweroff = snd_soc_poweroff,
416356fc 1665};
deb2607e 1666EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
416356fc 1667
db2a4165
FM
1668/* ASoC platform driver */
1669static struct platform_driver soc_driver = {
1670 .driver = {
1671 .name = "soc-audio",
8b45a209 1672 .owner = THIS_MODULE,
6f8ab4ac 1673 .pm = &snd_soc_pm_ops,
db2a4165
FM
1674 },
1675 .probe = soc_probe,
1676 .remove = soc_remove,
db2a4165
FM
1677};
1678
096e49d5
MB
1679/**
1680 * snd_soc_codec_volatile_register: Report if a register is volatile.
1681 *
1682 * @codec: CODEC to query.
1683 * @reg: Register to query.
1684 *
1685 * Boolean function indiciating if a CODEC register is volatile.
1686 */
181e055e
MB
1687int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1688 unsigned int reg)
096e49d5 1689{
1500b7b5
DP
1690 if (codec->volatile_register)
1691 return codec->volatile_register(codec, reg);
096e49d5
MB
1692 else
1693 return 0;
1694}
1695EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1696
239c9706
DP
1697/**
1698 * snd_soc_codec_readable_register: Report if a register is readable.
1699 *
1700 * @codec: CODEC to query.
1701 * @reg: Register to query.
1702 *
1703 * Boolean function indicating if a CODEC register is readable.
1704 */
1705int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1706 unsigned int reg)
1707{
1708 if (codec->readable_register)
1709 return codec->readable_register(codec, reg);
1710 else
63fa0a28 1711 return 1;
239c9706
DP
1712}
1713EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1714
1715/**
1716 * snd_soc_codec_writable_register: Report if a register is writable.
1717 *
1718 * @codec: CODEC to query.
1719 * @reg: Register to query.
1720 *
1721 * Boolean function indicating if a CODEC register is writable.
1722 */
1723int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1724 unsigned int reg)
1725{
1726 if (codec->writable_register)
1727 return codec->writable_register(codec, reg);
1728 else
63fa0a28 1729 return 1;
239c9706
DP
1730}
1731EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1732
f1442bc1
LG
1733int snd_soc_platform_read(struct snd_soc_platform *platform,
1734 unsigned int reg)
1735{
1736 unsigned int ret;
1737
1738 if (!platform->driver->read) {
1739 dev_err(platform->dev, "platform has no read back\n");
1740 return -1;
1741 }
1742
1743 ret = platform->driver->read(platform, reg);
1744 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
a82ce2ae 1745 trace_snd_soc_preg_read(platform, reg, ret);
f1442bc1
LG
1746
1747 return ret;
1748}
1749EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1750
1751int snd_soc_platform_write(struct snd_soc_platform *platform,
1752 unsigned int reg, unsigned int val)
1753{
1754 if (!platform->driver->write) {
1755 dev_err(platform->dev, "platform has no write back\n");
1756 return -1;
1757 }
1758
1759 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
a82ce2ae 1760 trace_snd_soc_preg_write(platform, reg, val);
f1442bc1
LG
1761 return platform->driver->write(platform, reg, val);
1762}
1763EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1764
db2a4165
FM
1765/**
1766 * snd_soc_new_ac97_codec - initailise AC97 device
1767 * @codec: audio codec
1768 * @ops: AC97 bus operations
1769 * @num: AC97 codec number
1770 *
1771 * Initialises AC97 codec resources for use by ad-hoc devices only.
1772 */
1773int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1774 struct snd_ac97_bus_ops *ops, int num)
1775{
1776 mutex_lock(&codec->mutex);
1777
1778 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1779 if (codec->ac97 == NULL) {
1780 mutex_unlock(&codec->mutex);
1781 return -ENOMEM;
1782 }
1783
1784 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1785 if (codec->ac97->bus == NULL) {
1786 kfree(codec->ac97);
1787 codec->ac97 = NULL;
1788 mutex_unlock(&codec->mutex);
1789 return -ENOMEM;
1790 }
1791
1792 codec->ac97->bus->ops = ops;
1793 codec->ac97->num = num;
0562f788
MW
1794
1795 /*
1796 * Mark the AC97 device to be created by us. This way we ensure that the
1797 * device will be registered with the device subsystem later on.
1798 */
1799 codec->ac97_created = 1;
1800
db2a4165
FM
1801 mutex_unlock(&codec->mutex);
1802 return 0;
1803}
1804EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1805
1806/**
1807 * snd_soc_free_ac97_codec - free AC97 codec device
1808 * @codec: audio codec
1809 *
1810 * Frees AC97 codec device resources.
1811 */
1812void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1813{
1814 mutex_lock(&codec->mutex);
f0fba2ad
LG
1815#ifdef CONFIG_SND_SOC_AC97_BUS
1816 soc_unregister_ac97_dai_link(codec);
1817#endif
db2a4165
FM
1818 kfree(codec->ac97->bus);
1819 kfree(codec->ac97);
1820 codec->ac97 = NULL;
0562f788 1821 codec->ac97_created = 0;
db2a4165
FM
1822 mutex_unlock(&codec->mutex);
1823}
1824EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1825
c3753707
MB
1826unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1827{
1828 unsigned int ret;
1829
c3acec26 1830 ret = codec->read(codec, reg);
c3753707 1831 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
a8b1d34f 1832 trace_snd_soc_reg_read(codec, reg, ret);
c3753707
MB
1833
1834 return ret;
1835}
1836EXPORT_SYMBOL_GPL(snd_soc_read);
1837
1838unsigned int snd_soc_write(struct snd_soc_codec *codec,
1839 unsigned int reg, unsigned int val)
1840{
1841 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
a8b1d34f 1842 trace_snd_soc_reg_write(codec, reg, val);
c3acec26 1843 return codec->write(codec, reg, val);
c3753707
MB
1844}
1845EXPORT_SYMBOL_GPL(snd_soc_write);
1846
5fb609d4
DP
1847unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
1848 unsigned int reg, const void *data, size_t len)
1849{
1850 return codec->bulk_write_raw(codec, reg, data, len);
1851}
1852EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
1853
db2a4165
FM
1854/**
1855 * snd_soc_update_bits - update codec register bits
1856 * @codec: audio codec
1857 * @reg: codec register
1858 * @mask: register mask
1859 * @value: new value
1860 *
1861 * Writes new register value.
1862 *
180c329d 1863 * Returns 1 for change, 0 for no change, or negative error code.
db2a4165
FM
1864 */
1865int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
46f5822f 1866 unsigned int mask, unsigned int value)
db2a4165
FM
1867{
1868 int change;
46f5822f 1869 unsigned int old, new;
180c329d
TT
1870 int ret;
1871
1872 ret = snd_soc_read(codec, reg);
1873 if (ret < 0)
1874 return ret;
db2a4165 1875
180c329d 1876 old = ret;
78bf3c9a 1877 new = (old & ~mask) | (value & mask);
db2a4165 1878 change = old != new;
180c329d
TT
1879 if (change) {
1880 ret = snd_soc_write(codec, reg, new);
1881 if (ret < 0)
1882 return ret;
1883 }
db2a4165 1884
db2a4165
FM
1885 return change;
1886}
1887EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1888
6c508c62
EN
1889/**
1890 * snd_soc_update_bits_locked - update codec register bits
1891 * @codec: audio codec
1892 * @reg: codec register
1893 * @mask: register mask
1894 * @value: new value
1895 *
1896 * Writes new register value, and takes the codec mutex.
1897 *
1898 * Returns 1 for change else 0.
1899 */
dd1b3d53
MB
1900int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1901 unsigned short reg, unsigned int mask,
1902 unsigned int value)
6c508c62
EN
1903{
1904 int change;
1905
1906 mutex_lock(&codec->mutex);
1907 change = snd_soc_update_bits(codec, reg, mask, value);
1908 mutex_unlock(&codec->mutex);
1909
1910 return change;
1911}
dd1b3d53 1912EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
6c508c62 1913
db2a4165
FM
1914/**
1915 * snd_soc_test_bits - test register for change
1916 * @codec: audio codec
1917 * @reg: codec register
1918 * @mask: register mask
1919 * @value: new value
1920 *
1921 * Tests a register with a new value and checks if the new value is
1922 * different from the old value.
1923 *
1924 * Returns 1 for change else 0.
1925 */
1926int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
46f5822f 1927 unsigned int mask, unsigned int value)
db2a4165
FM
1928{
1929 int change;
46f5822f 1930 unsigned int old, new;
db2a4165 1931
db2a4165
FM
1932 old = snd_soc_read(codec, reg);
1933 new = (old & ~mask) | value;
1934 change = old != new;
db2a4165
FM
1935
1936 return change;
1937}
1938EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1939
db2a4165
FM
1940/**
1941 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1942 * @substream: the pcm substream
1943 * @hw: the hardware parameters
1944 *
1945 * Sets the substream runtime hardware parameters.
1946 */
1947int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1948 const struct snd_pcm_hardware *hw)
1949{
1950 struct snd_pcm_runtime *runtime = substream->runtime;
1951 runtime->hw.info = hw->info;
1952 runtime->hw.formats = hw->formats;
1953 runtime->hw.period_bytes_min = hw->period_bytes_min;
1954 runtime->hw.period_bytes_max = hw->period_bytes_max;
1955 runtime->hw.periods_min = hw->periods_min;
1956 runtime->hw.periods_max = hw->periods_max;
1957 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1958 runtime->hw.fifo_size = hw->fifo_size;
1959 return 0;
1960}
1961EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1962
1963/**
1964 * snd_soc_cnew - create new control
1965 * @_template: control template
1966 * @data: control private data
ac11a2b3 1967 * @long_name: control long name
efb7ac3f 1968 * @prefix: control name prefix
db2a4165
FM
1969 *
1970 * Create a new mixer control from a template control.
1971 *
1972 * Returns 0 for success, else error.
1973 */
1974struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
efb7ac3f
MB
1975 void *data, char *long_name,
1976 const char *prefix)
db2a4165
FM
1977{
1978 struct snd_kcontrol_new template;
efb7ac3f
MB
1979 struct snd_kcontrol *kcontrol;
1980 char *name = NULL;
1981 int name_len;
db2a4165
FM
1982
1983 memcpy(&template, _template, sizeof(template));
db2a4165
FM
1984 template.index = 0;
1985
efb7ac3f
MB
1986 if (!long_name)
1987 long_name = template.name;
1988
1989 if (prefix) {
1990 name_len = strlen(long_name) + strlen(prefix) + 2;
57cf9d45 1991 name = kmalloc(name_len, GFP_KERNEL);
efb7ac3f
MB
1992 if (!name)
1993 return NULL;
1994
1995 snprintf(name, name_len, "%s %s", prefix, long_name);
1996
1997 template.name = name;
1998 } else {
1999 template.name = long_name;
2000 }
2001
2002 kcontrol = snd_ctl_new1(&template, data);
2003
2004 kfree(name);
2005
2006 return kcontrol;
db2a4165
FM
2007}
2008EXPORT_SYMBOL_GPL(snd_soc_cnew);
2009
3e8e1952
IM
2010/**
2011 * snd_soc_add_controls - add an array of controls to a codec.
2012 * Convienience function to add a list of controls. Many codecs were
2013 * duplicating this code.
2014 *
2015 * @codec: codec to add controls to
2016 * @controls: array of controls to add
2017 * @num_controls: number of elements in the array
2018 *
2019 * Return 0 for success, else error.
2020 */
2021int snd_soc_add_controls(struct snd_soc_codec *codec,
2022 const struct snd_kcontrol_new *controls, int num_controls)
2023{
f0fba2ad 2024 struct snd_card *card = codec->card->snd_card;
3e8e1952
IM
2025 int err, i;
2026
2027 for (i = 0; i < num_controls; i++) {
2028 const struct snd_kcontrol_new *control = &controls[i];
efb7ac3f
MB
2029 err = snd_ctl_add(card, snd_soc_cnew(control, codec,
2030 control->name,
2031 codec->name_prefix));
3e8e1952 2032 if (err < 0) {
082100dc 2033 dev_err(codec->dev, "%s: Failed to add %s: %d\n",
efb7ac3f 2034 codec->name, control->name, err);
3e8e1952
IM
2035 return err;
2036 }
2037 }
2038
2039 return 0;
2040}
2041EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2042
a491a5c8
LG
2043/**
2044 * snd_soc_add_platform_controls - add an array of controls to a platform.
2045 * Convienience function to add a list of controls.
2046 *
2047 * @platform: platform to add controls to
2048 * @controls: array of controls to add
2049 * @num_controls: number of elements in the array
2050 *
2051 * Return 0 for success, else error.
2052 */
2053int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2054 const struct snd_kcontrol_new *controls, int num_controls)
2055{
2056 struct snd_card *card = platform->card->snd_card;
2057 int err, i;
2058
2059 for (i = 0; i < num_controls; i++) {
2060 const struct snd_kcontrol_new *control = &controls[i];
2061 err = snd_ctl_add(card, snd_soc_cnew(control, platform,
2062 control->name, NULL));
2063 if (err < 0) {
2064 dev_err(platform->dev, "Failed to add %s %d\n",control->name, err);
2065 return err;
2066 }
2067 }
2068
2069 return 0;
2070}
2071EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2072
db2a4165
FM
2073/**
2074 * snd_soc_info_enum_double - enumerated double mixer info callback
2075 * @kcontrol: mixer control
2076 * @uinfo: control element information
2077 *
2078 * Callback to provide information about a double enumerated
2079 * mixer control.
2080 *
2081 * Returns 0 for success.
2082 */
2083int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2084 struct snd_ctl_elem_info *uinfo)
2085{
2086 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2087
2088 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2089 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
f8ba0b7b 2090 uinfo->value.enumerated.items = e->max;
db2a4165 2091
f8ba0b7b
JS
2092 if (uinfo->value.enumerated.item > e->max - 1)
2093 uinfo->value.enumerated.item = e->max - 1;
db2a4165
FM
2094 strcpy(uinfo->value.enumerated.name,
2095 e->texts[uinfo->value.enumerated.item]);
2096 return 0;
2097}
2098EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2099
2100/**
2101 * snd_soc_get_enum_double - enumerated double mixer get callback
2102 * @kcontrol: mixer control
ac11a2b3 2103 * @ucontrol: control element information
db2a4165
FM
2104 *
2105 * Callback to get the value of a double enumerated mixer.
2106 *
2107 * Returns 0 for success.
2108 */
2109int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2110 struct snd_ctl_elem_value *ucontrol)
2111{
2112 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2113 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
46f5822f 2114 unsigned int val, bitmask;
db2a4165 2115
f8ba0b7b 2116 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
db2a4165
FM
2117 ;
2118 val = snd_soc_read(codec, e->reg);
3ff3f64b
MB
2119 ucontrol->value.enumerated.item[0]
2120 = (val >> e->shift_l) & (bitmask - 1);
db2a4165
FM
2121 if (e->shift_l != e->shift_r)
2122 ucontrol->value.enumerated.item[1] =
2123 (val >> e->shift_r) & (bitmask - 1);
2124
2125 return 0;
2126}
2127EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2128
2129/**
2130 * snd_soc_put_enum_double - enumerated double mixer put callback
2131 * @kcontrol: mixer control
ac11a2b3 2132 * @ucontrol: control element information
db2a4165
FM
2133 *
2134 * Callback to set the value of a double enumerated mixer.
2135 *
2136 * Returns 0 for success.
2137 */
2138int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2139 struct snd_ctl_elem_value *ucontrol)
2140{
2141 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2142 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
46f5822f
DR
2143 unsigned int val;
2144 unsigned int mask, bitmask;
db2a4165 2145
f8ba0b7b 2146 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
db2a4165 2147 ;
f8ba0b7b 2148 if (ucontrol->value.enumerated.item[0] > e->max - 1)
db2a4165
FM
2149 return -EINVAL;
2150 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2151 mask = (bitmask - 1) << e->shift_l;
2152 if (e->shift_l != e->shift_r) {
f8ba0b7b 2153 if (ucontrol->value.enumerated.item[1] > e->max - 1)
db2a4165
FM
2154 return -EINVAL;
2155 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2156 mask |= (bitmask - 1) << e->shift_r;
2157 }
2158
6c508c62 2159 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
db2a4165
FM
2160}
2161EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2162
2e72f8e3
PU
2163/**
2164 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2165 * @kcontrol: mixer control
2166 * @ucontrol: control element information
2167 *
2168 * Callback to get the value of a double semi enumerated mixer.
2169 *
2170 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2171 * used for handling bitfield coded enumeration for example.
2172 *
2173 * Returns 0 for success.
2174 */
2175int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2176 struct snd_ctl_elem_value *ucontrol)
2177{
2178 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
74155556 2179 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
46f5822f 2180 unsigned int reg_val, val, mux;
2e72f8e3
PU
2181
2182 reg_val = snd_soc_read(codec, e->reg);
2183 val = (reg_val >> e->shift_l) & e->mask;
2184 for (mux = 0; mux < e->max; mux++) {
2185 if (val == e->values[mux])
2186 break;
2187 }
2188 ucontrol->value.enumerated.item[0] = mux;
2189 if (e->shift_l != e->shift_r) {
2190 val = (reg_val >> e->shift_r) & e->mask;
2191 for (mux = 0; mux < e->max; mux++) {
2192 if (val == e->values[mux])
2193 break;
2194 }
2195 ucontrol->value.enumerated.item[1] = mux;
2196 }
2197
2198 return 0;
2199}
2200EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2201
2202/**
2203 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2204 * @kcontrol: mixer control
2205 * @ucontrol: control element information
2206 *
2207 * Callback to set the value of a double semi enumerated mixer.
2208 *
2209 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2210 * used for handling bitfield coded enumeration for example.
2211 *
2212 * Returns 0 for success.
2213 */
2214int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2215 struct snd_ctl_elem_value *ucontrol)
2216{
2217 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
74155556 2218 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
46f5822f
DR
2219 unsigned int val;
2220 unsigned int mask;
2e72f8e3
PU
2221
2222 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2223 return -EINVAL;
2224 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2225 mask = e->mask << e->shift_l;
2226 if (e->shift_l != e->shift_r) {
2227 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2228 return -EINVAL;
2229 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2230 mask |= e->mask << e->shift_r;
2231 }
2232
6c508c62 2233 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2e72f8e3
PU
2234}
2235EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2236
db2a4165
FM
2237/**
2238 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2239 * @kcontrol: mixer control
2240 * @uinfo: control element information
2241 *
2242 * Callback to provide information about an external enumerated
2243 * single mixer.
2244 *
2245 * Returns 0 for success.
2246 */
2247int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2248 struct snd_ctl_elem_info *uinfo)
2249{
2250 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2251
2252 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2253 uinfo->count = 1;
f8ba0b7b 2254 uinfo->value.enumerated.items = e->max;
db2a4165 2255
f8ba0b7b
JS
2256 if (uinfo->value.enumerated.item > e->max - 1)
2257 uinfo->value.enumerated.item = e->max - 1;
db2a4165
FM
2258 strcpy(uinfo->value.enumerated.name,
2259 e->texts[uinfo->value.enumerated.item]);
2260 return 0;
2261}
2262EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2263
2264/**
2265 * snd_soc_info_volsw_ext - external single mixer info callback
2266 * @kcontrol: mixer control
2267 * @uinfo: control element information
2268 *
2269 * Callback to provide information about a single external mixer control.
2270 *
2271 * Returns 0 for success.
2272 */
2273int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2274 struct snd_ctl_elem_info *uinfo)
2275{
a7a4ac86
PZ
2276 int max = kcontrol->private_value;
2277
fd5dfad9 2278 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
a7a4ac86
PZ
2279 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2280 else
2281 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
db2a4165 2282
db2a4165
FM
2283 uinfo->count = 1;
2284 uinfo->value.integer.min = 0;
a7a4ac86 2285 uinfo->value.integer.max = max;
db2a4165
FM
2286 return 0;
2287}
2288EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2289
db2a4165
FM
2290/**
2291 * snd_soc_info_volsw - single mixer info callback
2292 * @kcontrol: mixer control
2293 * @uinfo: control element information
2294 *
e8f5a103
PU
2295 * Callback to provide information about a single mixer control, or a double
2296 * mixer control that spans 2 registers.
db2a4165
FM
2297 *
2298 * Returns 0 for success.
2299 */
2300int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2301 struct snd_ctl_elem_info *uinfo)
2302{
4eaa9819
JS
2303 struct soc_mixer_control *mc =
2304 (struct soc_mixer_control *)kcontrol->private_value;
d11bb4a9 2305 int platform_max;
db2a4165 2306
d11bb4a9
PU
2307 if (!mc->platform_max)
2308 mc->platform_max = mc->max;
2309 platform_max = mc->platform_max;
2310
2311 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
a7a4ac86
PZ
2312 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2313 else
2314 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2315
e8f5a103 2316 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
db2a4165 2317 uinfo->value.integer.min = 0;
d11bb4a9 2318 uinfo->value.integer.max = platform_max;
db2a4165
FM
2319 return 0;
2320}
2321EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2322
2323/**
2324 * snd_soc_get_volsw - single mixer get callback
2325 * @kcontrol: mixer control
ac11a2b3 2326 * @ucontrol: control element information
db2a4165 2327 *
f7915d99
PU
2328 * Callback to get the value of a single mixer control, or a double mixer
2329 * control that spans 2 registers.
db2a4165
FM
2330 *
2331 * Returns 0 for success.
2332 */
2333int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2334 struct snd_ctl_elem_value *ucontrol)
2335{
4eaa9819
JS
2336 struct soc_mixer_control *mc =
2337 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 2338 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d 2339 unsigned int reg = mc->reg;
f7915d99 2340 unsigned int reg2 = mc->rreg;
815ecf8d
JS
2341 unsigned int shift = mc->shift;
2342 unsigned int rshift = mc->rshift;
4eaa9819 2343 int max = mc->max;
815ecf8d
JS
2344 unsigned int mask = (1 << fls(max)) - 1;
2345 unsigned int invert = mc->invert;
db2a4165
FM
2346
2347 ucontrol->value.integer.value[0] =
2348 (snd_soc_read(codec, reg) >> shift) & mask;
f7915d99 2349 if (invert)
db2a4165 2350 ucontrol->value.integer.value[0] =
a7a4ac86 2351 max - ucontrol->value.integer.value[0];
f7915d99
PU
2352
2353 if (snd_soc_volsw_is_stereo(mc)) {
2354 if (reg == reg2)
2355 ucontrol->value.integer.value[1] =
2356 (snd_soc_read(codec, reg) >> rshift) & mask;
2357 else
2358 ucontrol->value.integer.value[1] =
2359 (snd_soc_read(codec, reg2) >> shift) & mask;
2360 if (invert)
db2a4165 2361 ucontrol->value.integer.value[1] =
a7a4ac86 2362 max - ucontrol->value.integer.value[1];
db2a4165
FM
2363 }
2364
2365 return 0;
2366}
2367EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2368
2369/**
2370 * snd_soc_put_volsw - single mixer put callback
2371 * @kcontrol: mixer control
ac11a2b3 2372 * @ucontrol: control element information
db2a4165 2373 *
974815ba
PU
2374 * Callback to set the value of a single mixer control, or a double mixer
2375 * control that spans 2 registers.
db2a4165
FM
2376 *
2377 * Returns 0 for success.
2378 */
2379int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2380 struct snd_ctl_elem_value *ucontrol)
2381{
4eaa9819
JS
2382 struct soc_mixer_control *mc =
2383 (struct soc_mixer_control *)kcontrol->private_value;
db2a4165 2384 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d 2385 unsigned int reg = mc->reg;
974815ba 2386 unsigned int reg2 = mc->rreg;
815ecf8d
JS
2387 unsigned int shift = mc->shift;
2388 unsigned int rshift = mc->rshift;
4eaa9819 2389 int max = mc->max;
815ecf8d
JS
2390 unsigned int mask = (1 << fls(max)) - 1;
2391 unsigned int invert = mc->invert;
974815ba
PU
2392 int err;
2393 bool type_2r = 0;
2394 unsigned int val2 = 0;
2395 unsigned int val, val_mask;
db2a4165
FM
2396
2397 val = (ucontrol->value.integer.value[0] & mask);
2398 if (invert)
a7a4ac86 2399 val = max - val;
db2a4165
FM
2400 val_mask = mask << shift;
2401 val = val << shift;
974815ba 2402 if (snd_soc_volsw_is_stereo(mc)) {
db2a4165
FM
2403 val2 = (ucontrol->value.integer.value[1] & mask);
2404 if (invert)
a7a4ac86 2405 val2 = max - val2;
974815ba
PU
2406 if (reg == reg2) {
2407 val_mask |= mask << rshift;
2408 val |= val2 << rshift;
2409 } else {
2410 val2 = val2 << shift;
2411 type_2r = 1;
2412 }
db2a4165 2413 }
6c508c62 2414 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
3ff3f64b 2415 if (err < 0)
db2a4165
FM
2416 return err;
2417
974815ba
PU
2418 if (type_2r)
2419 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2420
db2a4165
FM
2421 return err;
2422}
974815ba 2423EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
db2a4165 2424
e13ac2e9
MB
2425/**
2426 * snd_soc_info_volsw_s8 - signed mixer info callback
2427 * @kcontrol: mixer control
2428 * @uinfo: control element information
2429 *
2430 * Callback to provide information about a signed mixer control.
2431 *
2432 * Returns 0 for success.
2433 */
2434int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2435 struct snd_ctl_elem_info *uinfo)
2436{
4eaa9819
JS
2437 struct soc_mixer_control *mc =
2438 (struct soc_mixer_control *)kcontrol->private_value;
d11bb4a9 2439 int platform_max;
4eaa9819 2440 int min = mc->min;
e13ac2e9 2441
d11bb4a9
PU
2442 if (!mc->platform_max)
2443 mc->platform_max = mc->max;
2444 platform_max = mc->platform_max;
2445
e13ac2e9
MB
2446 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2447 uinfo->count = 2;
2448 uinfo->value.integer.min = 0;
d11bb4a9 2449 uinfo->value.integer.max = platform_max - min;
e13ac2e9
MB
2450 return 0;
2451}
2452EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2453
2454/**
2455 * snd_soc_get_volsw_s8 - signed mixer get callback
2456 * @kcontrol: mixer control
ac11a2b3 2457 * @ucontrol: control element information
e13ac2e9
MB
2458 *
2459 * Callback to get the value of a signed mixer control.
2460 *
2461 * Returns 0 for success.
2462 */
2463int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2464 struct snd_ctl_elem_value *ucontrol)
2465{
4eaa9819
JS
2466 struct soc_mixer_control *mc =
2467 (struct soc_mixer_control *)kcontrol->private_value;
e13ac2e9 2468 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d 2469 unsigned int reg = mc->reg;
4eaa9819 2470 int min = mc->min;
e13ac2e9
MB
2471 int val = snd_soc_read(codec, reg);
2472
2473 ucontrol->value.integer.value[0] =
2474 ((signed char)(val & 0xff))-min;
2475 ucontrol->value.integer.value[1] =
2476 ((signed char)((val >> 8) & 0xff))-min;
2477 return 0;
2478}
2479EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2480
2481/**
2482 * snd_soc_put_volsw_sgn - signed mixer put callback
2483 * @kcontrol: mixer control
ac11a2b3 2484 * @ucontrol: control element information
e13ac2e9
MB
2485 *
2486 * Callback to set the value of a signed mixer control.
2487 *
2488 * Returns 0 for success.
2489 */
2490int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2491 struct snd_ctl_elem_value *ucontrol)
2492{
4eaa9819
JS
2493 struct soc_mixer_control *mc =
2494 (struct soc_mixer_control *)kcontrol->private_value;
e13ac2e9 2495 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
815ecf8d 2496 unsigned int reg = mc->reg;
4eaa9819 2497 int min = mc->min;
46f5822f 2498 unsigned int val;
e13ac2e9
MB
2499
2500 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2501 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2502
6c508c62 2503 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
e13ac2e9
MB
2504}
2505EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2506
637d3847
PU
2507/**
2508 * snd_soc_limit_volume - Set new limit to an existing volume control.
2509 *
2510 * @codec: where to look for the control
2511 * @name: Name of the control
2512 * @max: new maximum limit
2513 *
2514 * Return 0 for success, else error.
2515 */
2516int snd_soc_limit_volume(struct snd_soc_codec *codec,
2517 const char *name, int max)
2518{
f0fba2ad 2519 struct snd_card *card = codec->card->snd_card;
637d3847
PU
2520 struct snd_kcontrol *kctl;
2521 struct soc_mixer_control *mc;
2522 int found = 0;
2523 int ret = -EINVAL;
2524
2525 /* Sanity check for name and max */
2526 if (unlikely(!name || max <= 0))
2527 return -EINVAL;
2528
2529 list_for_each_entry(kctl, &card->controls, list) {
2530 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2531 found = 1;
2532 break;
2533 }
2534 }
2535 if (found) {
2536 mc = (struct soc_mixer_control *)kctl->private_value;
2537 if (max <= mc->max) {
d11bb4a9 2538 mc->platform_max = max;
637d3847
PU
2539 ret = 0;
2540 }
2541 }
2542 return ret;
2543}
2544EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2545
b6f4bb38 2546/**
2547 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2548 * mixer info callback
2549 * @kcontrol: mixer control
2550 * @uinfo: control element information
2551 *
2552 * Returns 0 for success.
2553 */
2554int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2555 struct snd_ctl_elem_info *uinfo)
2556{
2557 struct soc_mixer_control *mc =
2558 (struct soc_mixer_control *)kcontrol->private_value;
2559 int max = mc->max;
2560 int min = mc->min;
2561
2562 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2563 uinfo->count = 2;
2564 uinfo->value.integer.min = 0;
2565 uinfo->value.integer.max = max-min;
2566
2567 return 0;
2568}
2569EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2570
2571/**
2572 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2573 * mixer get callback
2574 * @kcontrol: mixer control
2575 * @uinfo: control element information
2576 *
2577 * Returns 0 for success.
2578 */
2579int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2580 struct snd_ctl_elem_value *ucontrol)
2581{
2582 struct soc_mixer_control *mc =
2583 (struct soc_mixer_control *)kcontrol->private_value;
2584 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2585 unsigned int mask = (1<<mc->shift)-1;
2586 int min = mc->min;
2587 int val = snd_soc_read(codec, mc->reg) & mask;
2588 int valr = snd_soc_read(codec, mc->rreg) & mask;
2589
20630c7f
SL
2590 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2591 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
b6f4bb38 2592 return 0;
2593}
2594EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2595
2596/**
2597 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2598 * mixer put callback
2599 * @kcontrol: mixer control
2600 * @uinfo: control element information
2601 *
2602 * Returns 0 for success.
2603 */
2604int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2605 struct snd_ctl_elem_value *ucontrol)
2606{
2607 struct soc_mixer_control *mc =
2608 (struct soc_mixer_control *)kcontrol->private_value;
2609 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2610 unsigned int mask = (1<<mc->shift)-1;
2611 int min = mc->min;
2612 int ret;
2613 unsigned int val, valr, oval, ovalr;
2614
2615 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2616 val &= mask;
2617 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2618 valr &= mask;
2619
2620 oval = snd_soc_read(codec, mc->reg) & mask;
2621 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2622
2623 ret = 0;
2624 if (oval != val) {
2625 ret = snd_soc_write(codec, mc->reg, val);
2626 if (ret < 0)
f1df5aec 2627 return ret;
b6f4bb38 2628 }
2629 if (ovalr != valr) {
2630 ret = snd_soc_write(codec, mc->rreg, valr);
2631 if (ret < 0)
f1df5aec 2632 return ret;
b6f4bb38 2633 }
2634
2635 return 0;
2636}
2637EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2638
8c6529db
LG
2639/**
2640 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2641 * @dai: DAI
2642 * @clk_id: DAI specific clock ID
2643 * @freq: new clock frequency in Hz
2644 * @dir: new clock direction - input/output.
2645 *
2646 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2647 */
2648int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2649 unsigned int freq, int dir)
2650{
f0fba2ad
LG
2651 if (dai->driver && dai->driver->ops->set_sysclk)
2652 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
ec4ee52a 2653 else if (dai->codec && dai->codec->driver->set_sysclk)
da1c6ea6 2654 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
ec4ee52a 2655 freq, dir);
8c6529db
LG
2656 else
2657 return -EINVAL;
2658}
2659EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2660
ec4ee52a
MB
2661/**
2662 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2663 * @codec: CODEC
2664 * @clk_id: DAI specific clock ID
da1c6ea6 2665 * @source: Source for the clock
ec4ee52a
MB
2666 * @freq: new clock frequency in Hz
2667 * @dir: new clock direction - input/output.
2668 *
2669 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2670 */
2671int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
da1c6ea6 2672 int source, unsigned int freq, int dir)
ec4ee52a
MB
2673{
2674 if (codec->driver->set_sysclk)
da1c6ea6
MB
2675 return codec->driver->set_sysclk(codec, clk_id, source,
2676 freq, dir);
ec4ee52a
MB
2677 else
2678 return -EINVAL;
2679}
2680EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2681
8c6529db
LG
2682/**
2683 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2684 * @dai: DAI
ac11a2b3 2685 * @div_id: DAI specific clock divider ID
8c6529db
LG
2686 * @div: new clock divisor.
2687 *
2688 * Configures the clock dividers. This is used to derive the best DAI bit and
2689 * frame clocks from the system or master clock. It's best to set the DAI bit
2690 * and frame clocks as low as possible to save system power.
2691 */
2692int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2693 int div_id, int div)
2694{
f0fba2ad
LG
2695 if (dai->driver && dai->driver->ops->set_clkdiv)
2696 return dai->driver->ops->set_clkdiv(dai, div_id, div);
8c6529db
LG
2697 else
2698 return -EINVAL;
2699}
2700EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2701
2702/**
2703 * snd_soc_dai_set_pll - configure DAI PLL.
2704 * @dai: DAI
2705 * @pll_id: DAI specific PLL ID
85488037 2706 * @source: DAI specific source for the PLL
8c6529db
LG
2707 * @freq_in: PLL input clock frequency in Hz
2708 * @freq_out: requested PLL output clock frequency in Hz
2709 *
2710 * Configures and enables PLL to generate output clock based on input clock.
2711 */
85488037
MB
2712int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2713 unsigned int freq_in, unsigned int freq_out)
8c6529db 2714{
f0fba2ad
LG
2715 if (dai->driver && dai->driver->ops->set_pll)
2716 return dai->driver->ops->set_pll(dai, pll_id, source,
85488037 2717 freq_in, freq_out);
ec4ee52a
MB
2718 else if (dai->codec && dai->codec->driver->set_pll)
2719 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2720 freq_in, freq_out);
8c6529db
LG
2721 else
2722 return -EINVAL;
2723}
2724EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2725
ec4ee52a
MB
2726/*
2727 * snd_soc_codec_set_pll - configure codec PLL.
2728 * @codec: CODEC
2729 * @pll_id: DAI specific PLL ID
2730 * @source: DAI specific source for the PLL
2731 * @freq_in: PLL input clock frequency in Hz
2732 * @freq_out: requested PLL output clock frequency in Hz
2733 *
2734 * Configures and enables PLL to generate output clock based on input clock.
2735 */
2736int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2737 unsigned int freq_in, unsigned int freq_out)
2738{
2739 if (codec->driver->set_pll)
2740 return codec->driver->set_pll(codec, pll_id, source,
2741 freq_in, freq_out);
2742 else
2743 return -EINVAL;
2744}
2745EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2746
8c6529db
LG
2747/**
2748 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2749 * @dai: DAI
8c6529db
LG
2750 * @fmt: SND_SOC_DAIFMT_ format value.
2751 *
2752 * Configures the DAI hardware format and clocking.
2753 */
2754int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2755{
f0fba2ad
LG
2756 if (dai->driver && dai->driver->ops->set_fmt)
2757 return dai->driver->ops->set_fmt(dai, fmt);
8c6529db
LG
2758 else
2759 return -EINVAL;
2760}
2761EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2762
2763/**
2764 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2765 * @dai: DAI
a5479e38
DR
2766 * @tx_mask: bitmask representing active TX slots.
2767 * @rx_mask: bitmask representing active RX slots.
8c6529db 2768 * @slots: Number of slots in use.
a5479e38 2769 * @slot_width: Width in bits for each slot.
8c6529db
LG
2770 *
2771 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2772 * specific.
2773 */
2774int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
a5479e38 2775 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
8c6529db 2776{
f0fba2ad
LG
2777 if (dai->driver && dai->driver->ops->set_tdm_slot)
2778 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
a5479e38 2779 slots, slot_width);
8c6529db
LG
2780 else
2781 return -EINVAL;
2782}
2783EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2784
472df3cb
BS
2785/**
2786 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2787 * @dai: DAI
2788 * @tx_num: how many TX channels
2789 * @tx_slot: pointer to an array which imply the TX slot number channel
2790 * 0~num-1 uses
2791 * @rx_num: how many RX channels
2792 * @rx_slot: pointer to an array which imply the RX slot number channel
2793 * 0~num-1 uses
2794 *
2795 * configure the relationship between channel number and TDM slot number.
2796 */
2797int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2798 unsigned int tx_num, unsigned int *tx_slot,
2799 unsigned int rx_num, unsigned int *rx_slot)
2800{
f0fba2ad
LG
2801 if (dai->driver && dai->driver->ops->set_channel_map)
2802 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
472df3cb
BS
2803 rx_num, rx_slot);
2804 else
2805 return -EINVAL;
2806}
2807EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2808
8c6529db
LG
2809/**
2810 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2811 * @dai: DAI
2812 * @tristate: tristate enable
2813 *
2814 * Tristates the DAI so that others can use it.
2815 */
2816int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2817{
f0fba2ad
LG
2818 if (dai->driver && dai->driver->ops->set_tristate)
2819 return dai->driver->ops->set_tristate(dai, tristate);
8c6529db
LG
2820 else
2821 return -EINVAL;
2822}
2823EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2824
2825/**
2826 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2827 * @dai: DAI
2828 * @mute: mute enable
2829 *
2830 * Mutes the DAI DAC.
2831 */
2832int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2833{
f0fba2ad
LG
2834 if (dai->driver && dai->driver->ops->digital_mute)
2835 return dai->driver->ops->digital_mute(dai, mute);
8c6529db
LG
2836 else
2837 return -EINVAL;
2838}
2839EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2840
c5af3a2e
MB
2841/**
2842 * snd_soc_register_card - Register a card with the ASoC core
2843 *
ac11a2b3 2844 * @card: Card to register
c5af3a2e 2845 *
c5af3a2e 2846 */
70a7ca34 2847int snd_soc_register_card(struct snd_soc_card *card)
c5af3a2e 2848{
f0fba2ad
LG
2849 int i;
2850
c5af3a2e
MB
2851 if (!card->name || !card->dev)
2852 return -EINVAL;
2853
5a504963
SW
2854 for (i = 0; i < card->num_links; i++) {
2855 struct snd_soc_dai_link *link = &card->dai_link[i];
2856
2857 /*
2858 * Codec must be specified by 1 of name or OF node,
2859 * not both or neither.
2860 */
2861 if (!!link->codec_name == !!link->codec_of_node) {
2862 dev_err(card->dev,
2863 "Neither/both codec name/of_node are set\n");
2864 return -EINVAL;
2865 }
2866
2867 /*
2868 * Platform may be specified by either name or OF node, but
2869 * can be left unspecified, and a dummy platform will be used.
2870 */
2871 if (link->platform_name && link->platform_of_node) {
2872 dev_err(card->dev,
2873 "Both platform name/of_node are set\n");
2874 return -EINVAL;
2875 }
2876
2877 /*
2878 * CPU DAI must be specified by 1 of name or OF node,
2879 * not both or neither.
2880 */
2881 if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
2882 dev_err(card->dev,
2883 "Neither/both cpu_dai name/of_node are set\n");
2884 return -EINVAL;
2885 }
2886 }
2887
ed77cc12
MB
2888 dev_set_drvdata(card->dev, card);
2889
111c6419
SW
2890 snd_soc_initialize_card_lists(card);
2891
150dd2f8
VK
2892 soc_init_card_debugfs(card);
2893
2eea392d
JN
2894 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
2895 (card->num_links + card->num_aux_devs),
2896 GFP_KERNEL);
f0fba2ad
LG
2897 if (card->rtd == NULL)
2898 return -ENOMEM;
2eea392d 2899 card->rtd_aux = &card->rtd[card->num_links];
f0fba2ad
LG
2900
2901 for (i = 0; i < card->num_links; i++)
2902 card->rtd[i].dai_link = &card->dai_link[i];
2903
c5af3a2e 2904 INIT_LIST_HEAD(&card->list);
db432b41 2905 INIT_LIST_HEAD(&card->dapm_dirty);
c5af3a2e 2906 card->instantiated = 0;
f0fba2ad 2907 mutex_init(&card->mutex);
c5af3a2e
MB
2908
2909 mutex_lock(&client_mutex);
2910 list_add(&card->list, &card_list);
435c5e25 2911 snd_soc_instantiate_cards();
c5af3a2e
MB
2912 mutex_unlock(&client_mutex);
2913
2914 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2915
2916 return 0;
2917}
70a7ca34 2918EXPORT_SYMBOL_GPL(snd_soc_register_card);
c5af3a2e
MB
2919
2920/**
2921 * snd_soc_unregister_card - Unregister a card with the ASoC core
2922 *
ac11a2b3 2923 * @card: Card to unregister
c5af3a2e 2924 *
c5af3a2e 2925 */
70a7ca34 2926int snd_soc_unregister_card(struct snd_soc_card *card)
c5af3a2e 2927{
b0e26485
VK
2928 if (card->instantiated)
2929 soc_cleanup_card_resources(card);
c5af3a2e
MB
2930 mutex_lock(&client_mutex);
2931 list_del(&card->list);
2932 mutex_unlock(&client_mutex);
c5af3a2e
MB
2933 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2934
2935 return 0;
2936}
70a7ca34 2937EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
c5af3a2e 2938
f0fba2ad
LG
2939/*
2940 * Simplify DAI link configuration by removing ".-1" from device names
2941 * and sanitizing names.
2942 */
0b9a214a 2943static char *fmt_single_name(struct device *dev, int *id)
f0fba2ad
LG
2944{
2945 char *found, name[NAME_SIZE];
2946 int id1, id2;
2947
2948 if (dev_name(dev) == NULL)
2949 return NULL;
2950
58818a77 2951 strlcpy(name, dev_name(dev), NAME_SIZE);
f0fba2ad
LG
2952
2953 /* are we a "%s.%d" name (platform and SPI components) */
2954 found = strstr(name, dev->driver->name);
2955 if (found) {
2956 /* get ID */
2957 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2958
2959 /* discard ID from name if ID == -1 */
2960 if (*id == -1)
2961 found[strlen(dev->driver->name)] = '\0';
2962 }
2963
2964 } else {
2965 /* I2C component devices are named "bus-addr" */
2966 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2967 char tmp[NAME_SIZE];
2968
2969 /* create unique ID number from I2C addr and bus */
05899446 2970 *id = ((id1 & 0xffff) << 16) + id2;
f0fba2ad
LG
2971
2972 /* sanitize component name for DAI link creation */
2973 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
58818a77 2974 strlcpy(name, tmp, NAME_SIZE);
f0fba2ad
LG
2975 } else
2976 *id = 0;
2977 }
2978
2979 return kstrdup(name, GFP_KERNEL);
2980}
2981
2982/*
2983 * Simplify DAI link naming for single devices with multiple DAIs by removing
2984 * any ".-1" and using the DAI name (instead of device name).
2985 */
2986static inline char *fmt_multiple_name(struct device *dev,
2987 struct snd_soc_dai_driver *dai_drv)
2988{
2989 if (dai_drv->name == NULL) {
2990 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2991 dev_name(dev));
2992 return NULL;
2993 }
2994
2995 return kstrdup(dai_drv->name, GFP_KERNEL);
2996}
2997
9115171a
MB
2998/**
2999 * snd_soc_register_dai - Register a DAI with the ASoC core
3000 *
ac11a2b3 3001 * @dai: DAI to register
9115171a 3002 */
f0fba2ad
LG
3003int snd_soc_register_dai(struct device *dev,
3004 struct snd_soc_dai_driver *dai_drv)
9115171a 3005{
f0fba2ad
LG
3006 struct snd_soc_dai *dai;
3007
3008 dev_dbg(dev, "dai register %s\n", dev_name(dev));
9115171a 3009
f0fba2ad
LG
3010 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3011 if (dai == NULL)
a7393623 3012 return -ENOMEM;
9115171a 3013
f0fba2ad
LG
3014 /* create DAI component name */
3015 dai->name = fmt_single_name(dev, &dai->id);
3016 if (dai->name == NULL) {
3017 kfree(dai);
3018 return -ENOMEM;
3019 }
6335d055 3020
f0fba2ad
LG
3021 dai->dev = dev;
3022 dai->driver = dai_drv;
3023 if (!dai->driver->ops)
3024 dai->driver->ops = &null_dai_ops;
9115171a
MB
3025
3026 mutex_lock(&client_mutex);
3027 list_add(&dai->list, &dai_list);
435c5e25 3028 snd_soc_instantiate_cards();
9115171a
MB
3029 mutex_unlock(&client_mutex);
3030
3031 pr_debug("Registered DAI '%s'\n", dai->name);
3032
3033 return 0;
3034}
3035EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3036
3037/**
3038 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3039 *
ac11a2b3 3040 * @dai: DAI to unregister
9115171a 3041 */
f0fba2ad 3042void snd_soc_unregister_dai(struct device *dev)
9115171a 3043{
f0fba2ad
LG
3044 struct snd_soc_dai *dai;
3045
3046 list_for_each_entry(dai, &dai_list, list) {
3047 if (dev == dai->dev)
3048 goto found;
3049 }
3050 return;
3051
3052found:
9115171a
MB
3053 mutex_lock(&client_mutex);
3054 list_del(&dai->list);
3055 mutex_unlock(&client_mutex);
3056
3057 pr_debug("Unregistered DAI '%s'\n", dai->name);
f0fba2ad
LG
3058 kfree(dai->name);
3059 kfree(dai);
9115171a
MB
3060}
3061EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3062
3063/**
3064 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3065 *
ac11a2b3
MB
3066 * @dai: Array of DAIs to register
3067 * @count: Number of DAIs
9115171a 3068 */
f0fba2ad
LG
3069int snd_soc_register_dais(struct device *dev,
3070 struct snd_soc_dai_driver *dai_drv, size_t count)
9115171a 3071{
f0fba2ad
LG
3072 struct snd_soc_dai *dai;
3073 int i, ret = 0;
3074
720ffa4c 3075 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
9115171a
MB
3076
3077 for (i = 0; i < count; i++) {
f0fba2ad
LG
3078
3079 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
c46e0079
AL
3080 if (dai == NULL) {
3081 ret = -ENOMEM;
3082 goto err;
3083 }
f0fba2ad
LG
3084
3085 /* create DAI component name */
3086 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3087 if (dai->name == NULL) {
3088 kfree(dai);
3089 ret = -EINVAL;
9115171a 3090 goto err;
f0fba2ad
LG
3091 }
3092
3093 dai->dev = dev;
f0fba2ad 3094 dai->driver = &dai_drv[i];
0f9141c9
MB
3095 if (dai->driver->id)
3096 dai->id = dai->driver->id;
3097 else
3098 dai->id = i;
f0fba2ad
LG
3099 if (!dai->driver->ops)
3100 dai->driver->ops = &null_dai_ops;
3101
3102 mutex_lock(&client_mutex);
3103 list_add(&dai->list, &dai_list);
3104 mutex_unlock(&client_mutex);
3105
3106 pr_debug("Registered DAI '%s'\n", dai->name);
9115171a
MB
3107 }
3108
1dcb4f38 3109 mutex_lock(&client_mutex);
f0fba2ad 3110 snd_soc_instantiate_cards();
1dcb4f38 3111 mutex_unlock(&client_mutex);
9115171a
MB
3112 return 0;
3113
3114err:
3115 for (i--; i >= 0; i--)
f0fba2ad 3116 snd_soc_unregister_dai(dev);
9115171a
MB
3117
3118 return ret;
3119}
3120EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3121
3122/**
3123 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3124 *
ac11a2b3
MB
3125 * @dai: Array of DAIs to unregister
3126 * @count: Number of DAIs
9115171a 3127 */
f0fba2ad 3128void snd_soc_unregister_dais(struct device *dev, size_t count)
9115171a
MB
3129{
3130 int i;
3131
3132 for (i = 0; i < count; i++)
f0fba2ad 3133 snd_soc_unregister_dai(dev);
9115171a
MB
3134}
3135EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3136
12a48a8c
MB
3137/**
3138 * snd_soc_register_platform - Register a platform with the ASoC core
3139 *
ac11a2b3 3140 * @platform: platform to register
12a48a8c 3141 */
f0fba2ad
LG
3142int snd_soc_register_platform(struct device *dev,
3143 struct snd_soc_platform_driver *platform_drv)
12a48a8c 3144{
f0fba2ad
LG
3145 struct snd_soc_platform *platform;
3146
3147 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3148
3149 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3150 if (platform == NULL)
a7393623 3151 return -ENOMEM;
f0fba2ad
LG
3152
3153 /* create platform component name */
3154 platform->name = fmt_single_name(dev, &platform->id);
3155 if (platform->name == NULL) {
3156 kfree(platform);
3157 return -ENOMEM;
3158 }
12a48a8c 3159
f0fba2ad
LG
3160 platform->dev = dev;
3161 platform->driver = platform_drv;
b7950641
LG
3162 platform->dapm.dev = dev;
3163 platform->dapm.platform = platform;
64a648c2 3164 platform->dapm.stream_event = platform_drv->stream_event;
12a48a8c
MB
3165
3166 mutex_lock(&client_mutex);
3167 list_add(&platform->list, &platform_list);
435c5e25 3168 snd_soc_instantiate_cards();
12a48a8c
MB
3169 mutex_unlock(&client_mutex);
3170
3171 pr_debug("Registered platform '%s'\n", platform->name);
3172
3173 return 0;
3174}
3175EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3176
3177/**
3178 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3179 *
ac11a2b3 3180 * @platform: platform to unregister
12a48a8c 3181 */
f0fba2ad 3182void snd_soc_unregister_platform(struct device *dev)
12a48a8c 3183{
f0fba2ad
LG
3184 struct snd_soc_platform *platform;
3185
3186 list_for_each_entry(platform, &platform_list, list) {
3187 if (dev == platform->dev)
3188 goto found;
3189 }
3190 return;
3191
3192found:
12a48a8c
MB
3193 mutex_lock(&client_mutex);
3194 list_del(&platform->list);
3195 mutex_unlock(&client_mutex);
3196
3197 pr_debug("Unregistered platform '%s'\n", platform->name);
f0fba2ad
LG
3198 kfree(platform->name);
3199 kfree(platform);
12a48a8c
MB
3200}
3201EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3202
151ab22c
MB
3203static u64 codec_format_map[] = {
3204 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3205 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3206 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3207 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3208 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3209 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3210 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3211 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3212 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3213 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3214 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3215 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3216 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3217 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3218 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3219 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3220};
3221
3222/* Fix up the DAI formats for endianness: codecs don't actually see
3223 * the endianness of the data but we're using the CPU format
3224 * definitions which do need to include endianness so we ensure that
3225 * codec DAIs always have both big and little endian variants set.
3226 */
3227static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3228{
3229 int i;
3230
3231 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3232 if (stream->formats & codec_format_map[i])
3233 stream->formats |= codec_format_map[i];
3234}
3235
0d0cf00a
MB
3236/**
3237 * snd_soc_register_codec - Register a codec with the ASoC core
3238 *
ac11a2b3 3239 * @codec: codec to register
0d0cf00a 3240 */
f0fba2ad 3241int snd_soc_register_codec(struct device *dev,
001ae4c0
MB
3242 const struct snd_soc_codec_driver *codec_drv,
3243 struct snd_soc_dai_driver *dai_drv,
3244 int num_dai)
0d0cf00a 3245{
3335ddca 3246 size_t reg_size;
f0fba2ad
LG
3247 struct snd_soc_codec *codec;
3248 int ret, i;
151ab22c 3249
f0fba2ad
LG
3250 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3251
3252 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3253 if (codec == NULL)
3254 return -ENOMEM;
0d0cf00a 3255
f0fba2ad
LG
3256 /* create CODEC component name */
3257 codec->name = fmt_single_name(dev, &codec->id);
3258 if (codec->name == NULL) {
3259 kfree(codec);
3260 return -ENOMEM;
3261 }
3262
23bbce34
DP
3263 if (codec_drv->compress_type)
3264 codec->compress_type = codec_drv->compress_type;
3265 else
3266 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3267
c3acec26
MB
3268 codec->write = codec_drv->write;
3269 codec->read = codec_drv->read;
1500b7b5
DP
3270 codec->volatile_register = codec_drv->volatile_register;
3271 codec->readable_register = codec_drv->readable_register;
8020454c 3272 codec->writable_register = codec_drv->writable_register;
ce6120cc
LG
3273 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3274 codec->dapm.dev = dev;
3275 codec->dapm.codec = codec;
474b62d6 3276 codec->dapm.seq_notifier = codec_drv->seq_notifier;
64a648c2 3277 codec->dapm.stream_event = codec_drv->stream_event;
7a30a3db
DP
3278 codec->dev = dev;
3279 codec->driver = codec_drv;
3280 codec->num_dai = num_dai;
3281 mutex_init(&codec->mutex);
ce6120cc 3282
f0fba2ad
LG
3283 /* allocate CODEC register cache */
3284 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3335ddca 3285 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
aea170a0 3286 codec->reg_size = reg_size;
3335ddca
DP
3287 /* it is necessary to make a copy of the default register cache
3288 * because in the case of using a compression type that requires
3289 * the default register cache to be marked as __devinitconst the
3290 * kernel might have freed the array by the time we initialize
3291 * the cache.
3292 */
2aa86323
DP
3293 if (codec_drv->reg_cache_default) {
3294 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3295 reg_size, GFP_KERNEL);
3296 if (!codec->reg_def_copy) {
3297 ret = -ENOMEM;
3298 goto fail;
3299 }
f0fba2ad 3300 }
151ab22c
MB
3301 }
3302
1500b7b5
DP
3303 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3304 if (!codec->volatile_register)
3305 codec->volatile_register = snd_soc_default_volatile_register;
3306 if (!codec->readable_register)
3307 codec->readable_register = snd_soc_default_readable_register;
8020454c
DP
3308 if (!codec->writable_register)
3309 codec->writable_register = snd_soc_default_writable_register;
1500b7b5
DP
3310 }
3311
f0fba2ad
LG
3312 for (i = 0; i < num_dai; i++) {
3313 fixup_codec_formats(&dai_drv[i].playback);
3314 fixup_codec_formats(&dai_drv[i].capture);
3315 }
3316
26b01ccd
MB
3317 /* register any DAIs */
3318 if (num_dai) {
3319 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3320 if (ret < 0)
fdf0f54d 3321 goto fail;
26b01ccd 3322 }
f0fba2ad 3323
0d0cf00a
MB
3324 mutex_lock(&client_mutex);
3325 list_add(&codec->list, &codec_list);
3326 snd_soc_instantiate_cards();
3327 mutex_unlock(&client_mutex);
3328
3329 pr_debug("Registered codec '%s'\n", codec->name);
0d0cf00a 3330 return 0;
f0fba2ad 3331
fdf0f54d 3332fail:
3335ddca
DP
3333 kfree(codec->reg_def_copy);
3334 codec->reg_def_copy = NULL;
f0fba2ad
LG
3335 kfree(codec->name);
3336 kfree(codec);
3337 return ret;
0d0cf00a
MB
3338}
3339EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3340
3341/**
3342 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3343 *
ac11a2b3 3344 * @codec: codec to unregister
0d0cf00a 3345 */
f0fba2ad 3346void snd_soc_unregister_codec(struct device *dev)
0d0cf00a 3347{
f0fba2ad
LG
3348 struct snd_soc_codec *codec;
3349 int i;
3350
3351 list_for_each_entry(codec, &codec_list, list) {
3352 if (dev == codec->dev)
3353 goto found;
3354 }
3355 return;
3356
3357found:
26b01ccd
MB
3358 if (codec->num_dai)
3359 for (i = 0; i < codec->num_dai; i++)
3360 snd_soc_unregister_dai(dev);
f0fba2ad 3361
0d0cf00a
MB
3362 mutex_lock(&client_mutex);
3363 list_del(&codec->list);
3364 mutex_unlock(&client_mutex);
3365
3366 pr_debug("Unregistered codec '%s'\n", codec->name);
f0fba2ad 3367
7a30a3db 3368 snd_soc_cache_exit(codec);
3335ddca 3369 kfree(codec->reg_def_copy);
1aafcd4d 3370 kfree(codec->name);
f0fba2ad 3371 kfree(codec);
0d0cf00a
MB
3372}
3373EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3374
bec4fa05
SW
3375/* Retrieve a card's name from device tree */
3376int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3377 const char *propname)
3378{
3379 struct device_node *np = card->dev->of_node;
3380 int ret;
3381
3382 ret = of_property_read_string_index(np, propname, 0, &card->name);
3383 /*
3384 * EINVAL means the property does not exist. This is fine providing
3385 * card->name was previously set, which is checked later in
3386 * snd_soc_register_card.
3387 */
3388 if (ret < 0 && ret != -EINVAL) {
3389 dev_err(card->dev,
3390 "Property '%s' could not be read: %d\n",
3391 propname, ret);
3392 return ret;
3393 }
3394
3395 return 0;
3396}
3397EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3398
a4a54dd5
SW
3399int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3400 const char *propname)
3401{
3402 struct device_node *np = card->dev->of_node;
3403 int num_routes;
3404 struct snd_soc_dapm_route *routes;
3405 int i, ret;
3406
3407 num_routes = of_property_count_strings(np, propname);
3408 if (num_routes & 1) {
3409 dev_err(card->dev,
3410 "Property '%s's length is not even\n",
3411 propname);
3412 return -EINVAL;
3413 }
3414 num_routes /= 2;
3415 if (!num_routes) {
3416 dev_err(card->dev,
3417 "Property '%s's length is zero\n",
3418 propname);
3419 return -EINVAL;
3420 }
3421
3422 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3423 GFP_KERNEL);
3424 if (!routes) {
3425 dev_err(card->dev,
3426 "Could not allocate DAPM route table\n");
3427 return -EINVAL;
3428 }
3429
3430 for (i = 0; i < num_routes; i++) {
3431 ret = of_property_read_string_index(np, propname,
3432 2 * i, &routes[i].sink);
3433 if (ret) {
3434 dev_err(card->dev,
3435 "Property '%s' index %d could not be read: %d\n",
3436 propname, 2 * i, ret);
3437 return -EINVAL;
3438 }
3439 ret = of_property_read_string_index(np, propname,
3440 (2 * i) + 1, &routes[i].source);
3441 if (ret) {
3442 dev_err(card->dev,
3443 "Property '%s' index %d could not be read: %d\n",
3444 propname, (2 * i) + 1, ret);
3445 return -EINVAL;
3446 }
3447 }
3448
3449 card->num_dapm_routes = num_routes;
3450 card->dapm_routes = routes;
3451
3452 return 0;
3453}
3454EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3455
c9b3a40f 3456static int __init snd_soc_init(void)
db2a4165 3457{
384c89e2 3458#ifdef CONFIG_DEBUG_FS
8a9dab1a
MB
3459 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3460 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
384c89e2
MB
3461 printk(KERN_WARNING
3462 "ASoC: Failed to create debugfs directory\n");
8a9dab1a 3463 snd_soc_debugfs_root = NULL;
384c89e2 3464 }
c3c5a19a 3465
8a9dab1a 3466 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
c3c5a19a
MB
3467 &codec_list_fops))
3468 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3469
8a9dab1a 3470 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
f3208780
MB
3471 &dai_list_fops))
3472 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
19c7ac27 3473
8a9dab1a 3474 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
19c7ac27
MB
3475 &platform_list_fops))
3476 pr_warn("ASoC: Failed to create platform list debugfs file\n");
384c89e2
MB
3477#endif
3478
fb257897
MB
3479 snd_soc_util_init();
3480
db2a4165
FM
3481 return platform_driver_register(&soc_driver);
3482}
4abe8e16 3483module_init(snd_soc_init);
db2a4165 3484
7d8c16a6 3485static void __exit snd_soc_exit(void)
db2a4165 3486{
fb257897
MB
3487 snd_soc_util_exit();
3488
384c89e2 3489#ifdef CONFIG_DEBUG_FS
8a9dab1a 3490 debugfs_remove_recursive(snd_soc_debugfs_root);
384c89e2 3491#endif
3ff3f64b 3492 platform_driver_unregister(&soc_driver);
db2a4165 3493}
db2a4165
FM
3494module_exit(snd_soc_exit);
3495
3496/* Module information */
d331124d 3497MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
db2a4165
FM
3498MODULE_DESCRIPTION("ALSA SoC Core");
3499MODULE_LICENSE("GPL");
8b45a209 3500MODULE_ALIAS("platform:soc-audio");