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