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