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