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