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