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