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