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