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