ASoC: Provide a dummy wrapper of snd_soc_set_dmi_name()
[linux-2.6-block.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>
741a509f 33#include <linux/pinctrl/consumer.h>
f0e8ed85 34#include <linux/ctype.h>
5a0e3ad6 35#include <linux/slab.h>
bec4fa05 36#include <linux/of.h>
345233d7 37#include <linux/dmi.h>
db2a4165 38#include <sound/core.h>
3028eb8c 39#include <sound/jack.h>
db2a4165
FM
40#include <sound/pcm.h>
41#include <sound/pcm_params.h>
42#include <sound/soc.h>
01d7584c 43#include <sound/soc-dpcm.h>
8a978234 44#include <sound/soc-topology.h>
db2a4165
FM
45#include <sound/initval.h>
46
a8b1d34f
MB
47#define CREATE_TRACE_POINTS
48#include <trace/events/asoc.h>
49
f0fba2ad
LG
50#define NAME_SIZE 32
51
384c89e2 52#ifdef CONFIG_DEBUG_FS
8a9dab1a
MB
53struct dentry *snd_soc_debugfs_root;
54EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
384c89e2
MB
55#endif
56
c5af3a2e 57static DEFINE_MUTEX(client_mutex);
12a48a8c 58static LIST_HEAD(platform_list);
0d0cf00a 59static LIST_HEAD(codec_list);
030e79f6 60static LIST_HEAD(component_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 96 int ret;
13fd179f
DP
97
98 /* +2 for ': ' and + 1 for '\n' */
99 if (wordsize + regsize + 2 + 1 != len)
100 return -EINVAL;
101
d6b6c2ca
TI
102 sprintf(buf, "%.*x: ", wordsize, reg);
103 buf += wordsize + 2;
13fd179f 104
d6b6c2ca
TI
105 ret = snd_soc_read(codec, reg);
106 if (ret < 0)
107 memset(buf, 'X', regsize);
108 else
109 sprintf(buf, "%.*x", regsize, ret);
110 buf[regsize] = '\n';
111 /* no NUL-termination needed */
13fd179f
DP
112 return 0;
113}
114
2624d5fa 115/* codec register dump */
13fd179f
DP
116static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
117 size_t count, loff_t pos)
2624d5fa 118{
13fd179f 119 int i, step = 1;
2bc9a81e 120 int wordsize, regsize;
13fd179f
DP
121 int len;
122 size_t total = 0;
123 loff_t p = 0;
2bc9a81e 124
00b317a4
SW
125 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
126 regsize = codec->driver->reg_word_size * 2;
2624d5fa 127
13fd179f
DP
128 len = wordsize + regsize + 2 + 1;
129
f0fba2ad 130 if (!codec->driver->reg_cache_size)
2624d5fa
MB
131 return 0;
132
f0fba2ad
LG
133 if (codec->driver->reg_cache_step)
134 step = codec->driver->reg_cache_step;
2624d5fa 135
f0fba2ad 136 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
20a0ec27
LPC
137 /* only support larger than PAGE_SIZE bytes debugfs
138 * entries for the default case */
139 if (p >= pos) {
140 if (total + len >= count - 1)
141 break;
142 format_register_str(codec, i, buf + total, len);
143 total += len;
5164d74d 144 }
20a0ec27 145 p += len;
2624d5fa
MB
146 }
147
13fd179f 148 total = min(total, count - 1);
2624d5fa 149
13fd179f 150 return total;
2624d5fa 151}
13fd179f 152
2624d5fa
MB
153static ssize_t codec_reg_show(struct device *dev,
154 struct device_attribute *attr, char *buf)
155{
36ae1a96 156 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
f0fba2ad 157
13fd179f 158 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
2624d5fa
MB
159}
160
161static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
162
dbe21408
MB
163static ssize_t pmdown_time_show(struct device *dev,
164 struct device_attribute *attr, char *buf)
165{
36ae1a96 166 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
dbe21408 167
f0fba2ad 168 return sprintf(buf, "%ld\n", rtd->pmdown_time);
dbe21408
MB
169}
170
171static ssize_t pmdown_time_set(struct device *dev,
172 struct device_attribute *attr,
173 const char *buf, size_t count)
174{
36ae1a96 175 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
c593b520 176 int ret;
dbe21408 177
b785a492 178 ret = kstrtol(buf, 10, &rtd->pmdown_time);
c593b520
MB
179 if (ret)
180 return ret;
dbe21408
MB
181
182 return count;
183}
184
185static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
186
d29697dc
TI
187static struct attribute *soc_dev_attrs[] = {
188 &dev_attr_codec_reg.attr,
189 &dev_attr_pmdown_time.attr,
190 NULL
191};
192
193static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
194 struct attribute *attr, int idx)
195{
196 struct device *dev = kobj_to_dev(kobj);
197 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
198
199 if (attr == &dev_attr_pmdown_time.attr)
200 return attr->mode; /* always visible */
201 return rtd->codec ? attr->mode : 0; /* enabled only with codec */
202}
203
204static const struct attribute_group soc_dapm_dev_group = {
205 .attrs = soc_dapm_dev_attrs,
206 .is_visible = soc_dev_attr_is_visible,
207};
208
209static const struct attribute_group soc_dev_roup = {
210 .attrs = soc_dev_attrs,
211 .is_visible = soc_dev_attr_is_visible,
212};
213
214static const struct attribute_group *soc_dev_attr_groups[] = {
215 &soc_dapm_dev_group,
216 &soc_dev_roup,
217 NULL
218};
219
2624d5fa 220#ifdef CONFIG_DEBUG_FS
2624d5fa 221static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
13fd179f 222 size_t count, loff_t *ppos)
2624d5fa
MB
223{
224 ssize_t ret;
225 struct snd_soc_codec *codec = file->private_data;
13fd179f
DP
226 char *buf;
227
228 if (*ppos < 0 || !count)
229 return -EINVAL;
230
231 buf = kmalloc(count, GFP_KERNEL);
2624d5fa
MB
232 if (!buf)
233 return -ENOMEM;
13fd179f
DP
234
235 ret = soc_codec_reg_show(codec, buf, count, *ppos);
236 if (ret >= 0) {
237 if (copy_to_user(user_buf, buf, ret)) {
238 kfree(buf);
239 return -EFAULT;
240 }
241 *ppos += ret;
242 }
243
2624d5fa
MB
244 kfree(buf);
245 return ret;
246}
247
248static ssize_t codec_reg_write_file(struct file *file,
249 const char __user *user_buf, size_t count, loff_t *ppos)
250{
251 char buf[32];
34e268d8 252 size_t buf_size;
2624d5fa
MB
253 char *start = buf;
254 unsigned long reg, value;
2624d5fa 255 struct snd_soc_codec *codec = file->private_data;
b785a492 256 int ret;
2624d5fa
MB
257
258 buf_size = min(count, (sizeof(buf)-1));
259 if (copy_from_user(buf, user_buf, buf_size))
260 return -EFAULT;
261 buf[buf_size] = 0;
2624d5fa
MB
262
263 while (*start == ' ')
264 start++;
265 reg = simple_strtoul(start, &start, 16);
2624d5fa
MB
266 while (*start == ' ')
267 start++;
b785a492
JH
268 ret = kstrtoul(start, 16, &value);
269 if (ret)
270 return ret;
0d51a9cb
MB
271
272 /* Userspace has been fiddling around behind the kernel's back */
373d4d09 273 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
0d51a9cb 274
e4f078d8 275 snd_soc_write(codec, reg, value);
2624d5fa
MB
276 return buf_size;
277}
278
279static const struct file_operations codec_reg_fops = {
234e3405 280 .open = simple_open,
2624d5fa
MB
281 .read = codec_reg_read_file,
282 .write = codec_reg_write_file,
6038f373 283 .llseek = default_llseek,
2624d5fa
MB
284};
285
81c7cfd1 286static void soc_init_component_debugfs(struct snd_soc_component *component)
e73f3de5 287{
6553bf06
LPC
288 if (!component->card->debugfs_card_root)
289 return;
290
81c7cfd1
LPC
291 if (component->debugfs_prefix) {
292 char *name;
e73f3de5 293
81c7cfd1
LPC
294 name = kasprintf(GFP_KERNEL, "%s:%s",
295 component->debugfs_prefix, component->name);
296 if (name) {
297 component->debugfs_root = debugfs_create_dir(name,
298 component->card->debugfs_card_root);
299 kfree(name);
300 }
301 } else {
302 component->debugfs_root = debugfs_create_dir(component->name,
303 component->card->debugfs_card_root);
304 }
e73f3de5 305
81c7cfd1
LPC
306 if (!component->debugfs_root) {
307 dev_warn(component->dev,
308 "ASoC: Failed to create component debugfs directory\n");
309 return;
310 }
e73f3de5 311
81c7cfd1
LPC
312 snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
313 component->debugfs_root);
e73f3de5 314
81c7cfd1
LPC
315 if (component->init_debugfs)
316 component->init_debugfs(component);
e73f3de5
RK
317}
318
81c7cfd1 319static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
2624d5fa 320{
81c7cfd1
LPC
321 debugfs_remove_recursive(component->debugfs_root);
322}
d6ce4cf3 323
81c7cfd1
LPC
324static void soc_init_codec_debugfs(struct snd_soc_component *component)
325{
326 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
2624d5fa
MB
327
328 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
81c7cfd1 329 codec->component.debugfs_root,
2624d5fa
MB
330 codec, &codec_reg_fops);
331 if (!codec->debugfs_reg)
10e8aa9a
MM
332 dev_warn(codec->dev,
333 "ASoC: Failed to create codec register debugfs file\n");
731f1ab2
SG
334}
335
c3c5a19a
MB
336static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
337 size_t count, loff_t *ppos)
338{
339 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2b194f9d 340 ssize_t len, ret = 0;
c3c5a19a
MB
341 struct snd_soc_codec *codec;
342
343 if (!buf)
344 return -ENOMEM;
345
34e81ab4
LPC
346 mutex_lock(&client_mutex);
347
2b194f9d
MB
348 list_for_each_entry(codec, &codec_list, list) {
349 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
f4333203 350 codec->component.name);
2b194f9d
MB
351 if (len >= 0)
352 ret += len;
353 if (ret > PAGE_SIZE) {
354 ret = PAGE_SIZE;
355 break;
356 }
357 }
c3c5a19a 358
34e81ab4
LPC
359 mutex_unlock(&client_mutex);
360
c3c5a19a
MB
361 if (ret >= 0)
362 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
363
364 kfree(buf);
365
366 return ret;
367}
368
369static const struct file_operations codec_list_fops = {
370 .read = codec_list_read_file,
371 .llseek = default_llseek,/* read accesses f_pos */
372};
373
f3208780
MB
374static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
375 size_t count, loff_t *ppos)
376{
377 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2b194f9d 378 ssize_t len, ret = 0;
1438c2f6 379 struct snd_soc_component *component;
f3208780
MB
380 struct snd_soc_dai *dai;
381
382 if (!buf)
383 return -ENOMEM;
384
34e81ab4
LPC
385 mutex_lock(&client_mutex);
386
1438c2f6
LPC
387 list_for_each_entry(component, &component_list, list) {
388 list_for_each_entry(dai, &component->dai_list, list) {
389 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
390 dai->name);
391 if (len >= 0)
392 ret += len;
393 if (ret > PAGE_SIZE) {
394 ret = PAGE_SIZE;
395 break;
396 }
2b194f9d
MB
397 }
398 }
f3208780 399
34e81ab4
LPC
400 mutex_unlock(&client_mutex);
401
2b194f9d 402 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
f3208780
MB
403
404 kfree(buf);
405
406 return ret;
407}
408
409static const struct file_operations dai_list_fops = {
410 .read = dai_list_read_file,
411 .llseek = default_llseek,/* read accesses f_pos */
412};
413
19c7ac27
MB
414static ssize_t platform_list_read_file(struct file *file,
415 char __user *user_buf,
416 size_t count, loff_t *ppos)
417{
418 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2b194f9d 419 ssize_t len, ret = 0;
19c7ac27
MB
420 struct snd_soc_platform *platform;
421
422 if (!buf)
423 return -ENOMEM;
424
34e81ab4
LPC
425 mutex_lock(&client_mutex);
426
2b194f9d
MB
427 list_for_each_entry(platform, &platform_list, list) {
428 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
f4333203 429 platform->component.name);
2b194f9d
MB
430 if (len >= 0)
431 ret += len;
432 if (ret > PAGE_SIZE) {
433 ret = PAGE_SIZE;
434 break;
435 }
436 }
19c7ac27 437
34e81ab4
LPC
438 mutex_unlock(&client_mutex);
439
2b194f9d 440 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
19c7ac27
MB
441
442 kfree(buf);
443
444 return ret;
445}
446
447static const struct file_operations platform_list_fops = {
448 .read = platform_list_read_file,
449 .llseek = default_llseek,/* read accesses f_pos */
450};
451
a6052154
JN
452static void soc_init_card_debugfs(struct snd_soc_card *card)
453{
6553bf06
LPC
454 if (!snd_soc_debugfs_root)
455 return;
456
a6052154 457 card->debugfs_card_root = debugfs_create_dir(card->name,
8a9dab1a 458 snd_soc_debugfs_root);
3a45b867 459 if (!card->debugfs_card_root) {
a6052154 460 dev_warn(card->dev,
7c08be84 461 "ASoC: Failed to create card debugfs directory\n");
3a45b867
JN
462 return;
463 }
464
465 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
466 card->debugfs_card_root,
467 &card->pop_time);
468 if (!card->debugfs_pop_time)
469 dev_warn(card->dev,
f110bfc7 470 "ASoC: Failed to create pop time debugfs file\n");
a6052154
JN
471}
472
473static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
474{
475 debugfs_remove_recursive(card->debugfs_card_root);
476}
477
6553bf06
LPC
478
479static void snd_soc_debugfs_init(void)
480{
481 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
482 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
483 pr_warn("ASoC: Failed to create debugfs directory\n");
484 snd_soc_debugfs_root = NULL;
485 return;
486 }
487
488 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
489 &codec_list_fops))
490 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
491
492 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
493 &dai_list_fops))
494 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
495
496 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
497 &platform_list_fops))
498 pr_warn("ASoC: Failed to create platform list debugfs file\n");
499}
500
501static void snd_soc_debugfs_exit(void)
502{
503 debugfs_remove_recursive(snd_soc_debugfs_root);
504}
505
2624d5fa
MB
506#else
507
81c7cfd1 508#define soc_init_codec_debugfs NULL
b95fccbc 509
81c7cfd1
LPC
510static inline void soc_init_component_debugfs(
511 struct snd_soc_component *component)
731f1ab2
SG
512{
513}
514
81c7cfd1
LPC
515static inline void soc_cleanup_component_debugfs(
516 struct snd_soc_component *component)
731f1ab2
SG
517{
518}
519
b95fccbc
AL
520static inline void soc_init_card_debugfs(struct snd_soc_card *card)
521{
522}
523
524static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
525{
526}
6553bf06
LPC
527
528static inline void snd_soc_debugfs_init(void)
529{
530}
531
532static inline void snd_soc_debugfs_exit(void)
533{
534}
535
2624d5fa
MB
536#endif
537
47c88fff
LG
538struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
539 const char *dai_link, int stream)
540{
1a497983 541 struct snd_soc_pcm_runtime *rtd;
47c88fff 542
1a497983
ML
543 list_for_each_entry(rtd, &card->rtd_list, list) {
544 if (rtd->dai_link->no_pcm &&
545 !strcmp(rtd->dai_link->name, dai_link))
546 return rtd->pcm->streams[stream].substream;
47c88fff 547 }
f110bfc7 548 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
47c88fff
LG
549 return NULL;
550}
551EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
552
1a497983
ML
553static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
554 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
555{
556 struct snd_soc_pcm_runtime *rtd;
557
558 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
559 if (!rtd)
560 return NULL;
561
562 rtd->card = card;
563 rtd->dai_link = dai_link;
564 rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) *
565 dai_link->num_codecs,
566 GFP_KERNEL);
567 if (!rtd->codec_dais) {
568 kfree(rtd);
569 return NULL;
570 }
571
572 return rtd;
573}
574
575static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
576{
577 if (rtd && rtd->codec_dais)
578 kfree(rtd->codec_dais);
579 kfree(rtd);
580}
581
582static void soc_add_pcm_runtime(struct snd_soc_card *card,
583 struct snd_soc_pcm_runtime *rtd)
584{
585 list_add_tail(&rtd->list, &card->rtd_list);
586 rtd->num = card->num_rtd;
587 card->num_rtd++;
588}
589
590static void soc_remove_pcm_runtimes(struct snd_soc_card *card)
591{
592 struct snd_soc_pcm_runtime *rtd, *_rtd;
593
594 list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) {
595 list_del(&rtd->list);
596 soc_free_pcm_runtime(rtd);
597 }
598
599 card->num_rtd = 0;
600}
601
47c88fff
LG
602struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
603 const char *dai_link)
604{
1a497983 605 struct snd_soc_pcm_runtime *rtd;
47c88fff 606
1a497983
ML
607 list_for_each_entry(rtd, &card->rtd_list, list) {
608 if (!strcmp(rtd->dai_link->name, dai_link))
609 return rtd;
47c88fff 610 }
f110bfc7 611 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
47c88fff
LG
612 return NULL;
613}
614EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
615
9d58a077
RF
616static void codec2codec_close_delayed_work(struct work_struct *work)
617{
618 /* Currently nothing to do for c2c links
619 * Since c2c links are internal nodes in the DAPM graph and
620 * don't interface with the outside world or application layer
621 * we don't have to do any special handling on close.
622 */
623}
624
6f8ab4ac 625#ifdef CONFIG_PM_SLEEP
db2a4165 626/* powers down audio subsystem for suspend */
6f8ab4ac 627int snd_soc_suspend(struct device *dev)
db2a4165 628{
6f8ab4ac 629 struct snd_soc_card *card = dev_get_drvdata(dev);
d9fc4063 630 struct snd_soc_component *component;
1a497983
ML
631 struct snd_soc_pcm_runtime *rtd;
632 int i;
db2a4165 633
c5599b87
LPC
634 /* If the card is not initialized yet there is nothing to do */
635 if (!card->instantiated)
e3509ff0
DM
636 return 0;
637
6ed25978
AG
638 /* Due to the resume being scheduled into a workqueue we could
639 * suspend before that's finished - wait for it to complete.
640 */
f0fba2ad
LG
641 snd_power_lock(card->snd_card);
642 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
643 snd_power_unlock(card->snd_card);
6ed25978
AG
644
645 /* we're going to block userspace touching us until resume completes */
f0fba2ad 646 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
6ed25978 647
a00f90f9 648 /* mute any active DACs */
1a497983 649 list_for_each_entry(rtd, &card->rtd_list, list) {
3efab7dc 650
1a497983 651 if (rtd->dai_link->ignore_suspend)
3efab7dc
MB
652 continue;
653
1a497983
ML
654 for (i = 0; i < rtd->num_codecs; i++) {
655 struct snd_soc_dai *dai = rtd->codec_dais[i];
88bd870f
BC
656 struct snd_soc_dai_driver *drv = dai->driver;
657
658 if (drv->ops->digital_mute && dai->playback_active)
659 drv->ops->digital_mute(dai, 1);
660 }
db2a4165
FM
661 }
662
4ccab3e7 663 /* suspend all pcms */
1a497983
ML
664 list_for_each_entry(rtd, &card->rtd_list, list) {
665 if (rtd->dai_link->ignore_suspend)
3efab7dc
MB
666 continue;
667
1a497983 668 snd_pcm_suspend_all(rtd->pcm);
3efab7dc 669 }
4ccab3e7 670
87506549 671 if (card->suspend_pre)
70b2ac12 672 card->suspend_pre(card);
db2a4165 673
1a497983
ML
674 list_for_each_entry(rtd, &card->rtd_list, list) {
675 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3efab7dc 676
1a497983 677 if (rtd->dai_link->ignore_suspend)
3efab7dc
MB
678 continue;
679
bc263214 680 if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control)
f0fba2ad 681 cpu_dai->driver->suspend(cpu_dai);
db2a4165
FM
682 }
683
37660b6d 684 /* close any waiting streams */
1a497983
ML
685 list_for_each_entry(rtd, &card->rtd_list, list)
686 flush_delayed_work(&rtd->delayed_work);
db2a4165 687
1a497983 688 list_for_each_entry(rtd, &card->rtd_list, list) {
3efab7dc 689
1a497983 690 if (rtd->dai_link->ignore_suspend)
3efab7dc
MB
691 continue;
692
1a497983 693 snd_soc_dapm_stream_event(rtd,
7bd3a6f3 694 SNDRV_PCM_STREAM_PLAYBACK,
7bd3a6f3 695 SND_SOC_DAPM_STREAM_SUSPEND);
f0fba2ad 696
1a497983 697 snd_soc_dapm_stream_event(rtd,
7bd3a6f3 698 SNDRV_PCM_STREAM_CAPTURE,
7bd3a6f3 699 SND_SOC_DAPM_STREAM_SUSPEND);
db2a4165
FM
700 }
701
8be4da29
LPC
702 /* Recheck all endpoints too, their state is affected by suspend */
703 dapm_mark_endpoints_dirty(card);
e2d32ff6
MB
704 snd_soc_dapm_sync(&card->dapm);
705
9178feb4 706 /* suspend all COMPONENTs */
d9fc4063
KM
707 list_for_each_entry(component, &card->component_dev_list, card_list) {
708 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
d9fc4063 709
9178feb4 710 /* If there are paths active then the COMPONENT will be held with
f0fba2ad 711 * bias _ON and should not be suspended. */
9178feb4 712 if (!component->suspended) {
4890140f 713 switch (snd_soc_dapm_get_bias_level(dapm)) {
f0fba2ad 714 case SND_SOC_BIAS_STANDBY:
125a25da 715 /*
9178feb4 716 * If the COMPONENT is capable of idle
125a25da
MB
717 * bias off then being in STANDBY
718 * means it's doing something,
719 * otherwise fall through.
720 */
4890140f 721 if (dapm->idle_bias_off) {
9178feb4 722 dev_dbg(component->dev,
10e8aa9a 723 "ASoC: idle_bias_off CODEC on over suspend\n");
125a25da
MB
724 break;
725 }
a8093297 726
f0fba2ad 727 case SND_SOC_BIAS_OFF:
9178feb4
KM
728 if (component->suspend)
729 component->suspend(component);
730 component->suspended = 1;
731 if (component->regmap)
732 regcache_mark_dirty(component->regmap);
988e8cc4 733 /* deactivate pins to sleep state */
9178feb4 734 pinctrl_pm_select_sleep_state(component->dev);
f0fba2ad
LG
735 break;
736 default:
9178feb4
KM
737 dev_dbg(component->dev,
738 "ASoC: COMPONENT is on over suspend\n");
f0fba2ad
LG
739 break;
740 }
1547aba9
MB
741 }
742 }
db2a4165 743
1a497983
ML
744 list_for_each_entry(rtd, &card->rtd_list, list) {
745 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3efab7dc 746
1a497983 747 if (rtd->dai_link->ignore_suspend)
3efab7dc
MB
748 continue;
749
bc263214 750 if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control)
f0fba2ad 751 cpu_dai->driver->suspend(cpu_dai);
988e8cc4
NC
752
753 /* deactivate pins to sleep state */
754 pinctrl_pm_select_sleep_state(cpu_dai->dev);
db2a4165
FM
755 }
756
87506549 757 if (card->suspend_post)
70b2ac12 758 card->suspend_post(card);
db2a4165
FM
759
760 return 0;
761}
6f8ab4ac 762EXPORT_SYMBOL_GPL(snd_soc_suspend);
db2a4165 763
6ed25978
AG
764/* deferred resume work, so resume can complete before we finished
765 * setting our codec back up, which can be very slow on I2C
766 */
767static void soc_resume_deferred(struct work_struct *work)
db2a4165 768{
f0fba2ad
LG
769 struct snd_soc_card *card =
770 container_of(work, struct snd_soc_card, deferred_resume_work);
1a497983 771 struct snd_soc_pcm_runtime *rtd;
d9fc4063 772 struct snd_soc_component *component;
1a497983 773 int i;
db2a4165 774
6ed25978
AG
775 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
776 * so userspace apps are blocked from touching us
777 */
778
f110bfc7 779 dev_dbg(card->dev, "ASoC: starting resume work\n");
6ed25978 780
9949788b 781 /* Bring us up into D2 so that DAPM starts enabling things */
f0fba2ad 782 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
9949788b 783
87506549 784 if (card->resume_pre)
70b2ac12 785 card->resume_pre(card);
db2a4165 786
bc263214 787 /* resume control bus DAIs */
1a497983
ML
788 list_for_each_entry(rtd, &card->rtd_list, list) {
789 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3efab7dc 790
1a497983 791 if (rtd->dai_link->ignore_suspend)
3efab7dc
MB
792 continue;
793
bc263214 794 if (cpu_dai->driver->resume && cpu_dai->driver->bus_control)
f0fba2ad
LG
795 cpu_dai->driver->resume(cpu_dai);
796 }
797
d9fc4063 798 list_for_each_entry(component, &card->component_dev_list, card_list) {
9178feb4
KM
799 if (component->suspended) {
800 if (component->resume)
801 component->resume(component);
802 component->suspended = 0;
1547aba9
MB
803 }
804 }
db2a4165 805
1a497983 806 list_for_each_entry(rtd, &card->rtd_list, list) {
3efab7dc 807
1a497983 808 if (rtd->dai_link->ignore_suspend)
3efab7dc
MB
809 continue;
810
1a497983 811 snd_soc_dapm_stream_event(rtd,
d9b0951b 812 SNDRV_PCM_STREAM_PLAYBACK,
7bd3a6f3 813 SND_SOC_DAPM_STREAM_RESUME);
f0fba2ad 814
1a497983 815 snd_soc_dapm_stream_event(rtd,
d9b0951b 816 SNDRV_PCM_STREAM_CAPTURE,
7bd3a6f3 817 SND_SOC_DAPM_STREAM_RESUME);
db2a4165
FM
818 }
819
3ff3f64b 820 /* unmute any active DACs */
1a497983 821 list_for_each_entry(rtd, &card->rtd_list, list) {
3efab7dc 822
1a497983 823 if (rtd->dai_link->ignore_suspend)
3efab7dc
MB
824 continue;
825
1a497983
ML
826 for (i = 0; i < rtd->num_codecs; i++) {
827 struct snd_soc_dai *dai = rtd->codec_dais[i];
88bd870f
BC
828 struct snd_soc_dai_driver *drv = dai->driver;
829
830 if (drv->ops->digital_mute && dai->playback_active)
831 drv->ops->digital_mute(dai, 0);
832 }
db2a4165
FM
833 }
834
1a497983
ML
835 list_for_each_entry(rtd, &card->rtd_list, list) {
836 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3efab7dc 837
1a497983 838 if (rtd->dai_link->ignore_suspend)
3efab7dc
MB
839 continue;
840
bc263214 841 if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control)
f0fba2ad 842 cpu_dai->driver->resume(cpu_dai);
db2a4165
FM
843 }
844
87506549 845 if (card->resume_post)
70b2ac12 846 card->resume_post(card);
db2a4165 847
f110bfc7 848 dev_dbg(card->dev, "ASoC: resume work completed\n");
6ed25978 849
8be4da29
LPC
850 /* Recheck all endpoints too, their state is affected by suspend */
851 dapm_mark_endpoints_dirty(card);
e2d32ff6 852 snd_soc_dapm_sync(&card->dapm);
1a7aaa58
JK
853
854 /* userspace can access us now we are back as we were before */
855 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
6ed25978
AG
856}
857
858/* powers up audio subsystem after a suspend */
6f8ab4ac 859int snd_soc_resume(struct device *dev)
6ed25978 860{
6f8ab4ac 861 struct snd_soc_card *card = dev_get_drvdata(dev);
bc263214 862 bool bus_control = false;
1a497983 863 struct snd_soc_pcm_runtime *rtd;
b9dd94a8 864
c5599b87
LPC
865 /* If the card is not initialized yet there is nothing to do */
866 if (!card->instantiated)
5ff1ddf2
EM
867 return 0;
868
988e8cc4 869 /* activate pins from sleep state */
1a497983 870 list_for_each_entry(rtd, &card->rtd_list, list) {
88bd870f
BC
871 struct snd_soc_dai **codec_dais = rtd->codec_dais;
872 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
873 int j;
874
988e8cc4
NC
875 if (cpu_dai->active)
876 pinctrl_pm_select_default_state(cpu_dai->dev);
88bd870f
BC
877
878 for (j = 0; j < rtd->num_codecs; j++) {
879 struct snd_soc_dai *codec_dai = codec_dais[j];
880 if (codec_dai->active)
881 pinctrl_pm_select_default_state(codec_dai->dev);
882 }
988e8cc4
NC
883 }
884
bc263214
LPC
885 /*
886 * DAIs that also act as the control bus master might have other drivers
887 * hanging off them so need to resume immediately. Other drivers don't
888 * have that problem and may take a substantial amount of time to resume
64ab9baa
MB
889 * due to I/O costs and anti-pop so handle them out of line.
890 */
1a497983
ML
891 list_for_each_entry(rtd, &card->rtd_list, list) {
892 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
bc263214 893 bus_control |= cpu_dai->driver->bus_control;
82e14e8b 894 }
bc263214
LPC
895 if (bus_control) {
896 dev_dbg(dev, "ASoC: Resuming control bus master immediately\n");
82e14e8b
SW
897 soc_resume_deferred(&card->deferred_resume_work);
898 } else {
f110bfc7 899 dev_dbg(dev, "ASoC: Scheduling resume work\n");
82e14e8b 900 if (!schedule_work(&card->deferred_resume_work))
f110bfc7 901 dev_err(dev, "ASoC: resume work item may be lost\n");
64ab9baa 902 }
6ed25978 903
db2a4165
FM
904 return 0;
905}
6f8ab4ac 906EXPORT_SYMBOL_GPL(snd_soc_resume);
db2a4165 907#else
6f8ab4ac
MB
908#define snd_soc_suspend NULL
909#define snd_soc_resume NULL
db2a4165
FM
910#endif
911
85e7652d 912static const struct snd_soc_dai_ops null_dai_ops = {
02a06d30
BS
913};
914
65d9361f
LPC
915static struct snd_soc_component *soc_find_component(
916 const struct device_node *of_node, const char *name)
12023a9a 917{
65d9361f 918 struct snd_soc_component *component;
12023a9a 919
34e81ab4
LPC
920 lockdep_assert_held(&client_mutex);
921
65d9361f
LPC
922 list_for_each_entry(component, &component_list, list) {
923 if (of_node) {
924 if (component->dev->of_node == of_node)
925 return component;
926 } else if (strcmp(component->name, name) == 0) {
927 return component;
12023a9a 928 }
12023a9a
MLC
929 }
930
931 return NULL;
932}
933
fbb88b5c
ML
934/**
935 * snd_soc_find_dai - Find a registered DAI
936 *
937 * @dlc: name of the DAI and optional component info to match
938 *
939 * This function will search all regsitered components and their DAIs to
940 * find the DAI of the same name. The component's of_node and name
941 * should also match if being specified.
942 *
943 * Return: pointer of DAI, or NULL if not found.
944 */
305e9020 945struct snd_soc_dai *snd_soc_find_dai(
14621c7e 946 const struct snd_soc_dai_link_component *dlc)
12023a9a 947{
14621c7e
LPC
948 struct snd_soc_component *component;
949 struct snd_soc_dai *dai;
3e0aa8d8 950 struct device_node *component_of_node;
12023a9a 951
34e81ab4
LPC
952 lockdep_assert_held(&client_mutex);
953
14621c7e
LPC
954 /* Find CPU DAI from registered DAIs*/
955 list_for_each_entry(component, &component_list, list) {
3e0aa8d8
JS
956 component_of_node = component->dev->of_node;
957 if (!component_of_node && component->dev->parent)
958 component_of_node = component->dev->parent->of_node;
959
960 if (dlc->of_node && component_of_node != dlc->of_node)
14621c7e 961 continue;
1ffae361 962 if (dlc->name && strcmp(component->name, dlc->name))
14621c7e
LPC
963 continue;
964 list_for_each_entry(dai, &component->dai_list, list) {
965 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
12023a9a 966 continue;
12023a9a 967
14621c7e 968 return dai;
12023a9a
MLC
969 }
970 }
971
972 return NULL;
973}
305e9020 974EXPORT_SYMBOL_GPL(snd_soc_find_dai);
12023a9a 975
17fb1755
ML
976
977/**
978 * snd_soc_find_dai_link - Find a DAI link
979 *
980 * @card: soc card
981 * @id: DAI link ID to match
982 * @name: DAI link name to match, optional
8abab35f 983 * @stream_name: DAI link stream name to match, optional
17fb1755
ML
984 *
985 * This function will search all existing DAI links of the soc card to
986 * find the link of the same ID. Since DAI links may not have their
987 * unique ID, so name and stream name should also match if being
988 * specified.
989 *
990 * Return: pointer of DAI link, or NULL if not found.
991 */
992struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
993 int id, const char *name,
994 const char *stream_name)
995{
996 struct snd_soc_dai_link *link, *_link;
997
998 lockdep_assert_held(&client_mutex);
999
1000 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1001 if (link->id != id)
1002 continue;
1003
1004 if (name && (!link->name || strcmp(name, link->name)))
1005 continue;
1006
1007 if (stream_name && (!link->stream_name
1008 || strcmp(stream_name, link->stream_name)))
1009 continue;
1010
1011 return link;
1012 }
1013
1014 return NULL;
1015}
1016EXPORT_SYMBOL_GPL(snd_soc_find_dai_link);
1017
49a5ba1c
ML
1018static bool soc_is_dai_link_bound(struct snd_soc_card *card,
1019 struct snd_soc_dai_link *dai_link)
1020{
1021 struct snd_soc_pcm_runtime *rtd;
1022
1023 list_for_each_entry(rtd, &card->rtd_list, list) {
1024 if (rtd->dai_link == dai_link)
1025 return true;
1026 }
1027
1028 return false;
1029}
1030
6f2f1ff0
ML
1031static int soc_bind_dai_link(struct snd_soc_card *card,
1032 struct snd_soc_dai_link *dai_link)
db2a4165 1033{
1a497983 1034 struct snd_soc_pcm_runtime *rtd;
88bd870f 1035 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
14621c7e 1036 struct snd_soc_dai_link_component cpu_dai_component;
1a497983 1037 struct snd_soc_dai **codec_dais;
435c5e25 1038 struct snd_soc_platform *platform;
06859fca 1039 struct device_node *platform_of_node;
848dd8be 1040 const char *platform_name;
88bd870f 1041 int i;
435c5e25 1042
6f2f1ff0 1043 dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
435c5e25 1044
49a5ba1c
ML
1045 if (soc_is_dai_link_bound(card, dai_link)) {
1046 dev_dbg(card->dev, "ASoC: dai link %s already bound\n",
1047 dai_link->name);
1048 return 0;
1049 }
435c5e25 1050
513cb311
SM
1051 rtd = soc_new_pcm_runtime(card, dai_link);
1052 if (!rtd)
1053 return -ENOMEM;
1054
14621c7e
LPC
1055 cpu_dai_component.name = dai_link->cpu_name;
1056 cpu_dai_component.of_node = dai_link->cpu_of_node;
1057 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
1058 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
b19e6e7b 1059 if (!rtd->cpu_dai) {
f110bfc7 1060 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
b19e6e7b 1061 dai_link->cpu_dai_name);
1a497983 1062 goto _err_defer;
f0fba2ad
LG
1063 }
1064
88bd870f 1065 rtd->num_codecs = dai_link->num_codecs;
848dd8be 1066
88bd870f 1067 /* Find CODEC from registered CODECs */
1a497983 1068 codec_dais = rtd->codec_dais;
88bd870f 1069 for (i = 0; i < rtd->num_codecs; i++) {
14621c7e 1070 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
88bd870f
BC
1071 if (!codec_dais[i]) {
1072 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
1073 codecs[i].dai_name);
1a497983 1074 goto _err_defer;
88bd870f 1075 }
12023a9a
MLC
1076 }
1077
88bd870f
BC
1078 /* Single codec links expect codec and codec_dai in runtime data */
1079 rtd->codec_dai = codec_dais[0];
1080 rtd->codec = rtd->codec_dai->codec;
1081
848dd8be
MB
1082 /* if there's no platform we match on the empty platform */
1083 platform_name = dai_link->platform_name;
5a504963 1084 if (!platform_name && !dai_link->platform_of_node)
848dd8be
MB
1085 platform_name = "snd-soc-dummy";
1086
b19e6e7b 1087 /* find one from the set of registered platforms */
f0fba2ad 1088 list_for_each_entry(platform, &platform_list, list) {
06859fca
CK
1089 platform_of_node = platform->dev->of_node;
1090 if (!platform_of_node && platform->dev->parent->of_node)
1091 platform_of_node = platform->dev->parent->of_node;
1092
5a504963 1093 if (dai_link->platform_of_node) {
06859fca 1094 if (platform_of_node != dai_link->platform_of_node)
5a504963
SW
1095 continue;
1096 } else {
f4333203 1097 if (strcmp(platform->component.name, platform_name))
5a504963
SW
1098 continue;
1099 }
2610ab77
SW
1100
1101 rtd->platform = platform;
02a06d30 1102 }
b19e6e7b 1103 if (!rtd->platform) {
f110bfc7 1104 dev_err(card->dev, "ASoC: platform %s not registered\n",
f0fba2ad 1105 dai_link->platform_name);
70fcad49 1106 goto _err_defer;
f0fba2ad 1107 }
b19e6e7b 1108
1a497983 1109 soc_add_pcm_runtime(card, rtd);
b19e6e7b 1110 return 0;
1a497983
ML
1111
1112_err_defer:
1113 soc_free_pcm_runtime(rtd);
1114 return -EPROBE_DEFER;
f0fba2ad
LG
1115}
1116
f1d45cc3 1117static void soc_remove_component(struct snd_soc_component *component)
d12cd198 1118{
abd31b32 1119 if (!component->card)
70090bbb 1120 return;
d12cd198 1121
d9fc4063 1122 list_del(&component->card_list);
589c3563 1123
f1d45cc3
LPC
1124 if (component->remove)
1125 component->remove(component);
589c3563 1126
f1d45cc3 1127 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
589c3563 1128
f1d45cc3 1129 soc_cleanup_component_debugfs(component);
abd31b32 1130 component->card = NULL;
f1d45cc3 1131 module_put(component->dev->driver->owner);
589c3563
JN
1132}
1133
e60cd14f 1134static void soc_remove_dai(struct snd_soc_dai *dai, int order)
f0fba2ad 1135{
f0fba2ad
LG
1136 int err;
1137
e60cd14f
LPC
1138 if (dai && dai->probed &&
1139 dai->driver->remove_order == order) {
1140 if (dai->driver->remove) {
1141 err = dai->driver->remove(dai);
f0fba2ad 1142 if (err < 0)
e60cd14f 1143 dev_err(dai->dev,
f110bfc7 1144 "ASoC: failed to remove %s: %d\n",
e60cd14f 1145 dai->name, err);
6b05eda6 1146 }
e60cd14f 1147 dai->probed = 0;
f0fba2ad 1148 }
b0aa88af
MLC
1149}
1150
1a497983
ML
1151static void soc_remove_link_dais(struct snd_soc_card *card,
1152 struct snd_soc_pcm_runtime *rtd, int order)
b0aa88af 1153{
e60cd14f 1154 int i;
b0aa88af
MLC
1155
1156 /* unregister the rtd device */
1157 if (rtd->dev_registered) {
b0aa88af
MLC
1158 device_unregister(rtd->dev);
1159 rtd->dev_registered = 0;
1160 }
1161
1162 /* remove the CODEC DAI */
88bd870f 1163 for (i = 0; i < rtd->num_codecs; i++)
e60cd14f 1164 soc_remove_dai(rtd->codec_dais[i], order);
6b05eda6 1165
e60cd14f 1166 soc_remove_dai(rtd->cpu_dai, order);
f0fba2ad 1167}
db2a4165 1168
1a497983
ML
1169static void soc_remove_link_components(struct snd_soc_card *card,
1170 struct snd_soc_pcm_runtime *rtd, int order)
62ae68fa 1171{
62ae68fa 1172 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
62ae68fa 1173 struct snd_soc_platform *platform = rtd->platform;
61aca564 1174 struct snd_soc_component *component;
88bd870f 1175 int i;
62ae68fa
SW
1176
1177 /* remove the platform */
70090bbb 1178 if (platform && platform->component.driver->remove_order == order)
f1d45cc3 1179 soc_remove_component(&platform->component);
62ae68fa
SW
1180
1181 /* remove the CODEC-side CODEC */
88bd870f 1182 for (i = 0; i < rtd->num_codecs; i++) {
61aca564 1183 component = rtd->codec_dais[i]->component;
70090bbb 1184 if (component->driver->remove_order == order)
61aca564 1185 soc_remove_component(component);
62ae68fa
SW
1186 }
1187
1188 /* remove any CPU-side CODEC */
1189 if (cpu_dai) {
70090bbb 1190 if (cpu_dai->component->driver->remove_order == order)
61aca564 1191 soc_remove_component(cpu_dai->component);
62ae68fa
SW
1192 }
1193}
1194
0671fd8e
KM
1195static void soc_remove_dai_links(struct snd_soc_card *card)
1196{
1a497983
ML
1197 int order;
1198 struct snd_soc_pcm_runtime *rtd;
f8f80361 1199 struct snd_soc_dai_link *link, *_link;
0671fd8e 1200
0168bf0d
LG
1201 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1202 order++) {
1a497983
ML
1203 list_for_each_entry(rtd, &card->rtd_list, list)
1204 soc_remove_link_dais(card, rtd, order);
62ae68fa
SW
1205 }
1206
1207 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1208 order++) {
1a497983
ML
1209 list_for_each_entry(rtd, &card->rtd_list, list)
1210 soc_remove_link_components(card, rtd, order);
0168bf0d 1211 }
62ae68fa 1212
f8f80361
ML
1213 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1214 if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK)
1215 dev_warn(card->dev, "Topology forgot to remove link %s?\n",
1216 link->name);
1217
1218 list_del(&link->list);
1219 card->num_dai_links--;
1220 }
0671fd8e
KM
1221}
1222
923c5e61
ML
1223static int snd_soc_init_multicodec(struct snd_soc_card *card,
1224 struct snd_soc_dai_link *dai_link)
1225{
1226 /* Legacy codec/codec_dai link is a single entry in multicodec */
1227 if (dai_link->codec_name || dai_link->codec_of_node ||
1228 dai_link->codec_dai_name) {
1229 dai_link->num_codecs = 1;
1230
1231 dai_link->codecs = devm_kzalloc(card->dev,
1232 sizeof(struct snd_soc_dai_link_component),
1233 GFP_KERNEL);
1234 if (!dai_link->codecs)
1235 return -ENOMEM;
1236
1237 dai_link->codecs[0].name = dai_link->codec_name;
1238 dai_link->codecs[0].of_node = dai_link->codec_of_node;
1239 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
1240 }
1241
1242 if (!dai_link->codecs) {
1243 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
1244 return -EINVAL;
1245 }
1246
1247 return 0;
1248}
1249
1250static int soc_init_dai_link(struct snd_soc_card *card,
1251 struct snd_soc_dai_link *link)
1252{
1253 int i, ret;
1254
1255 ret = snd_soc_init_multicodec(card, link);
1256 if (ret) {
1257 dev_err(card->dev, "ASoC: failed to init multicodec\n");
1258 return ret;
1259 }
1260
1261 for (i = 0; i < link->num_codecs; i++) {
1262 /*
1263 * Codec must be specified by 1 of name or OF node,
1264 * not both or neither.
1265 */
1266 if (!!link->codecs[i].name ==
1267 !!link->codecs[i].of_node) {
1268 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
1269 link->name);
1270 return -EINVAL;
1271 }
1272 /* Codec DAI name must be specified */
1273 if (!link->codecs[i].dai_name) {
1274 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
1275 link->name);
1276 return -EINVAL;
1277 }
1278 }
1279
1280 /*
1281 * Platform may be specified by either name or OF node, but
1282 * can be left unspecified, and a dummy platform will be used.
1283 */
1284 if (link->platform_name && link->platform_of_node) {
1285 dev_err(card->dev,
1286 "ASoC: Both platform name/of_node are set for %s\n",
1287 link->name);
1288 return -EINVAL;
1289 }
1290
1291 /*
1292 * CPU device may be specified by either name or OF node, but
1293 * can be left unspecified, and will be matched based on DAI
1294 * name alone..
1295 */
1296 if (link->cpu_name && link->cpu_of_node) {
1297 dev_err(card->dev,
1298 "ASoC: Neither/both cpu name/of_node are set for %s\n",
1299 link->name);
1300 return -EINVAL;
1301 }
1302 /*
1303 * At least one of CPU DAI name or CPU device name/node must be
1304 * specified
1305 */
1306 if (!link->cpu_dai_name &&
1307 !(link->cpu_name || link->cpu_of_node)) {
1308 dev_err(card->dev,
1309 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
1310 link->name);
1311 return -EINVAL;
1312 }
1313
1314 return 0;
0671fd8e
KM
1315}
1316
f8f80361
ML
1317/**
1318 * snd_soc_add_dai_link - Add a DAI link dynamically
1319 * @card: The ASoC card to which the DAI link is added
1320 * @dai_link: The new DAI link to add
1321 *
1322 * This function adds a DAI link to the ASoC card's link list.
1323 *
1324 * Note: Topology can use this API to add DAI links when probing the
1325 * topology component. And machine drivers can still define static
1326 * DAI links in dai_link array.
1327 */
1328int snd_soc_add_dai_link(struct snd_soc_card *card,
1329 struct snd_soc_dai_link *dai_link)
1330{
1331 if (dai_link->dobj.type
1332 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1333 dev_err(card->dev, "Invalid dai link type %d\n",
1334 dai_link->dobj.type);
1335 return -EINVAL;
1336 }
1337
1338 lockdep_assert_held(&client_mutex);
d6f220ea
ML
1339 /* Notify the machine driver for extra initialization
1340 * on the link created by topology.
1341 */
1342 if (dai_link->dobj.type && card->add_dai_link)
1343 card->add_dai_link(card, dai_link);
1344
f8f80361
ML
1345 list_add_tail(&dai_link->list, &card->dai_link_list);
1346 card->num_dai_links++;
1347
1348 return 0;
1349}
1350EXPORT_SYMBOL_GPL(snd_soc_add_dai_link);
1351
1352/**
1353 * snd_soc_remove_dai_link - Remove a DAI link from the list
1354 * @card: The ASoC card that owns the link
1355 * @dai_link: The DAI link to remove
1356 *
1357 * This function removes a DAI link from the ASoC card's link list.
1358 *
1359 * For DAI links previously added by topology, topology should
1360 * remove them by using the dobj embedded in the link.
1361 */
1362void snd_soc_remove_dai_link(struct snd_soc_card *card,
1363 struct snd_soc_dai_link *dai_link)
1364{
1365 struct snd_soc_dai_link *link, *_link;
1366
1367 if (dai_link->dobj.type
1368 && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) {
1369 dev_err(card->dev, "Invalid dai link type %d\n",
1370 dai_link->dobj.type);
1371 return;
1372 }
1373
1374 lockdep_assert_held(&client_mutex);
d6f220ea
ML
1375 /* Notify the machine driver for extra destruction
1376 * on the link created by topology.
1377 */
1378 if (dai_link->dobj.type && card->remove_dai_link)
1379 card->remove_dai_link(card, dai_link);
1380
f8f80361
ML
1381 list_for_each_entry_safe(link, _link, &card->dai_link_list, list) {
1382 if (link == dai_link) {
1383 list_del(&link->list);
1384 card->num_dai_links--;
1385 return;
1386 }
1387 }
1388}
1389EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link);
1390
ead9b919 1391static void soc_set_name_prefix(struct snd_soc_card *card,
94f99c87 1392 struct snd_soc_component *component)
ead9b919
JN
1393{
1394 int i;
1395
ff819b83 1396 if (card->codec_conf == NULL)
ead9b919
JN
1397 return;
1398
ff819b83
DP
1399 for (i = 0; i < card->num_configs; i++) {
1400 struct snd_soc_codec_conf *map = &card->codec_conf[i];
94f99c87 1401 if (map->of_node && component->dev->of_node != map->of_node)
3ca041ed 1402 continue;
94f99c87 1403 if (map->dev_name && strcmp(component->name, map->dev_name))
3ca041ed 1404 continue;
94f99c87 1405 component->name_prefix = map->name_prefix;
3ca041ed 1406 break;
ead9b919
JN
1407 }
1408}
1409
f1d45cc3
LPC
1410static int soc_probe_component(struct snd_soc_card *card,
1411 struct snd_soc_component *component)
589c3563 1412{
f1d45cc3 1413 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
888df395 1414 struct snd_soc_dai *dai;
f1d45cc3 1415 int ret;
589c3563 1416
1b7c1231 1417 if (!strcmp(component->name, "snd-soc-dummy"))
70090bbb 1418 return 0;
589c3563 1419
abd31b32 1420 if (component->card) {
1b7c1231
LPC
1421 if (component->card != card) {
1422 dev_err(component->dev,
1423 "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n",
1424 card->name, component->card->name);
1425 return -ENODEV;
1426 }
1427 return 0;
1428 }
589c3563 1429
f1d45cc3 1430 if (!try_module_get(component->dev->driver->owner))
70d29331
JN
1431 return -ENODEV;
1432
f1d45cc3
LPC
1433 component->card = card;
1434 dapm->card = card;
1435 soc_set_name_prefix(card, component);
589c3563 1436
f1d45cc3 1437 soc_init_component_debugfs(component);
d5d1e0be 1438
f1d45cc3
LPC
1439 if (component->dapm_widgets) {
1440 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1441 component->num_dapm_widgets);
b318ad50
NP
1442
1443 if (ret != 0) {
f1d45cc3 1444 dev_err(component->dev,
b318ad50
NP
1445 "Failed to create new controls %d\n", ret);
1446 goto err_probe;
1447 }
1448 }
77530150 1449
0634814f
LPC
1450 list_for_each_entry(dai, &component->dai_list, list) {
1451 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
261edc70 1452 if (ret != 0) {
0634814f 1453 dev_err(component->dev,
261edc70
NP
1454 "Failed to create DAI widgets %d\n", ret);
1455 goto err_probe;
1456 }
1457 }
888df395 1458
f1d45cc3
LPC
1459 if (component->probe) {
1460 ret = component->probe(component);
589c3563 1461 if (ret < 0) {
f1d45cc3
LPC
1462 dev_err(component->dev,
1463 "ASoC: failed to probe component %d\n", ret);
70d29331 1464 goto err_probe;
589c3563 1465 }
cb2cf612 1466
f1d45cc3
LPC
1467 WARN(dapm->idle_bias_off &&
1468 dapm->bias_level != SND_SOC_BIAS_OFF,
1469 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1470 component->name);
be09ad90
LG
1471 }
1472
f2ed6b07
ML
1473 /* machine specific init */
1474 if (component->init) {
1475 ret = component->init(component);
1476 if (ret < 0) {
1477 dev_err(component->dev,
1478 "Failed to do machine specific init %d\n", ret);
1479 goto err_probe;
1480 }
1481 }
1482
f1d45cc3
LPC
1483 if (component->controls)
1484 snd_soc_add_component_controls(component, component->controls,
1485 component->num_controls);
1486 if (component->dapm_routes)
1487 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1488 component->num_dapm_routes);
3fec6b6d 1489
f1d45cc3 1490 list_add(&dapm->list, &card->dapm_list);
d9fc4063 1491 list_add(&component->card_list, &card->component_dev_list);
956245e9
LG
1492
1493 return 0;
1494
1495err_probe:
f1d45cc3 1496 soc_cleanup_component_debugfs(component);
abd31b32 1497 component->card = NULL;
f1d45cc3 1498 module_put(component->dev->driver->owner);
956245e9
LG
1499
1500 return ret;
1501}
1502
36ae1a96
MB
1503static void rtd_release(struct device *dev)
1504{
1505 kfree(dev);
1506}
f0fba2ad 1507
5f3484ac
LPC
1508static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1509 const char *name)
503ae5e0 1510{
589c3563
JN
1511 int ret = 0;
1512
589c3563 1513 /* register the rtd device */
36ae1a96
MB
1514 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1515 if (!rtd->dev)
1516 return -ENOMEM;
1517 device_initialize(rtd->dev);
5f3484ac 1518 rtd->dev->parent = rtd->card->dev;
36ae1a96 1519 rtd->dev->release = rtd_release;
d29697dc 1520 rtd->dev->groups = soc_dev_attr_groups;
f294afed 1521 dev_set_name(rtd->dev, "%s", name);
36ae1a96 1522 dev_set_drvdata(rtd->dev, rtd);
b8c0dab9 1523 mutex_init(&rtd->pcm_mutex);
01d7584c
LG
1524 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1525 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1526 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1527 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
36ae1a96 1528 ret = device_add(rtd->dev);
589c3563 1529 if (ret < 0) {
865df9cb
CL
1530 /* calling put_device() here to free the rtd->dev */
1531 put_device(rtd->dev);
5f3484ac 1532 dev_err(rtd->card->dev,
f110bfc7 1533 "ASoC: failed to register runtime device: %d\n", ret);
589c3563
JN
1534 return ret;
1535 }
1536 rtd->dev_registered = 1;
589c3563
JN
1537 return 0;
1538}
1539
1a497983
ML
1540static int soc_probe_link_components(struct snd_soc_card *card,
1541 struct snd_soc_pcm_runtime *rtd,
62ae68fa
SW
1542 int order)
1543{
62ae68fa 1544 struct snd_soc_platform *platform = rtd->platform;
f1d45cc3 1545 struct snd_soc_component *component;
88bd870f 1546 int i, ret;
62ae68fa
SW
1547
1548 /* probe the CPU-side component, if it is a CODEC */
61aca564 1549 component = rtd->cpu_dai->component;
70090bbb 1550 if (component->driver->probe_order == order) {
61aca564 1551 ret = soc_probe_component(card, component);
62ae68fa
SW
1552 if (ret < 0)
1553 return ret;
1554 }
1555
88bd870f
BC
1556 /* probe the CODEC-side components */
1557 for (i = 0; i < rtd->num_codecs; i++) {
61aca564 1558 component = rtd->codec_dais[i]->component;
70090bbb 1559 if (component->driver->probe_order == order) {
f1d45cc3 1560 ret = soc_probe_component(card, component);
88bd870f
BC
1561 if (ret < 0)
1562 return ret;
1563 }
62ae68fa
SW
1564 }
1565
1566 /* probe the platform */
70090bbb 1567 if (platform->component.driver->probe_order == order) {
f1d45cc3 1568 ret = soc_probe_component(card, &platform->component);
62ae68fa
SW
1569 if (ret < 0)
1570 return ret;
1571 }
1572
1573 return 0;
1574}
1575
8e2be562 1576static int soc_probe_dai(struct snd_soc_dai *dai, int order)
b0aa88af
MLC
1577{
1578 int ret;
1579
8e2be562
LPC
1580 if (!dai->probed && dai->driver->probe_order == order) {
1581 if (dai->driver->probe) {
1582 ret = dai->driver->probe(dai);
b0aa88af 1583 if (ret < 0) {
8e2be562
LPC
1584 dev_err(dai->dev,
1585 "ASoC: failed to probe DAI %s: %d\n",
1586 dai->name, ret);
b0aa88af
MLC
1587 return ret;
1588 }
1589 }
1590
8e2be562 1591 dai->probed = 1;
b0aa88af
MLC
1592 }
1593
1594 return 0;
1595}
1596
25f7b701
AP
1597static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais,
1598 struct snd_soc_pcm_runtime *rtd)
1599{
1600 int i, ret = 0;
1601
1602 for (i = 0; i < num_dais; ++i) {
1603 struct snd_soc_dai_driver *drv = dais[i]->driver;
1604
1605 if (!rtd->dai_link->no_pcm && drv->pcm_new)
1606 ret = drv->pcm_new(rtd, dais[i]);
1607 if (ret < 0) {
1608 dev_err(dais[i]->dev,
1609 "ASoC: Failed to bind %s with pcm device\n",
1610 dais[i]->name);
1611 return ret;
1612 }
1613 }
1614
1615 return 0;
1616}
1617
2436a723
MLC
1618static int soc_link_dai_widgets(struct snd_soc_card *card,
1619 struct snd_soc_dai_link *dai_link,
3f901a02 1620 struct snd_soc_pcm_runtime *rtd)
2436a723 1621{
3f901a02
BC
1622 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1623 struct snd_soc_dai *codec_dai = rtd->codec_dai;
3de7c420 1624 struct snd_soc_dapm_widget *sink, *source;
2436a723
MLC
1625 int ret;
1626
88bd870f
BC
1627 if (rtd->num_codecs > 1)
1628 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1629
2436a723 1630 /* link the DAI widgets */
3de7c420
VK
1631 sink = codec_dai->playback_widget;
1632 source = cpu_dai->capture_widget;
1633 if (sink && source) {
2436a723 1634 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
3de7c420
VK
1635 dai_link->num_params,
1636 source, sink);
2436a723
MLC
1637 if (ret != 0) {
1638 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
3de7c420 1639 sink->name, source->name, ret);
2436a723
MLC
1640 return ret;
1641 }
1642 }
1643
3de7c420
VK
1644 sink = cpu_dai->playback_widget;
1645 source = codec_dai->capture_widget;
1646 if (sink && source) {
2436a723 1647 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
3de7c420
VK
1648 dai_link->num_params,
1649 source, sink);
2436a723
MLC
1650 if (ret != 0) {
1651 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
3de7c420 1652 sink->name, source->name, ret);
2436a723
MLC
1653 return ret;
1654 }
1655 }
1656
1657 return 0;
1658}
1659
1a497983
ML
1660static int soc_probe_link_dais(struct snd_soc_card *card,
1661 struct snd_soc_pcm_runtime *rtd, int order)
f0fba2ad 1662{
1a497983 1663 struct snd_soc_dai_link *dai_link = rtd->dai_link;
c74184ed 1664 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
88bd870f 1665 int i, ret;
f0fba2ad 1666
f110bfc7 1667 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1a497983 1668 card->name, rtd->num, order);
f0fba2ad 1669
f0fba2ad
LG
1670 /* set default power off timeout */
1671 rtd->pmdown_time = pmdown_time;
1672
8e2be562
LPC
1673 ret = soc_probe_dai(cpu_dai, order);
1674 if (ret)
1675 return ret;
db2a4165 1676
f0fba2ad 1677 /* probe the CODEC DAI */
88bd870f 1678 for (i = 0; i < rtd->num_codecs; i++) {
8e2be562 1679 ret = soc_probe_dai(rtd->codec_dais[i], order);
88bd870f
BC
1680 if (ret)
1681 return ret;
1682 }
fe3e78e0 1683
0168bf0d
LG
1684 /* complete DAI probe during last probe */
1685 if (order != SND_SOC_COMP_ORDER_LAST)
1686 return 0;
1687
5f3484ac
LPC
1688 /* do machine specific initialization */
1689 if (dai_link->init) {
1690 ret = dai_link->init(rtd);
1691 if (ret < 0) {
1692 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1693 dai_link->name, ret);
1694 return ret;
1695 }
1696 }
1697
a5053a8e
KM
1698 if (dai_link->dai_fmt)
1699 snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1700
5f3484ac 1701 ret = soc_post_component_init(rtd, dai_link->name);
589c3563 1702 if (ret)
f0fba2ad 1703 return ret;
fe3e78e0 1704
5f3484ac
LPC
1705#ifdef CONFIG_DEBUG_FS
1706 /* add DPCM sysfs entries */
2e55b90a
LPC
1707 if (dai_link->dynamic)
1708 soc_dpcm_debugfs_add(rtd);
5f3484ac
LPC
1709#endif
1710
6f0c4226 1711 if (cpu_dai->driver->compress_new) {
1245b700 1712 /*create compress_device"*/
1a497983 1713 ret = cpu_dai->driver->compress_new(rtd, rtd->num);
c74184ed 1714 if (ret < 0) {
f110bfc7 1715 dev_err(card->dev, "ASoC: can't create compress %s\n",
1245b700 1716 dai_link->stream_name);
c74184ed
MB
1717 return ret;
1718 }
1719 } else {
1245b700
NK
1720
1721 if (!dai_link->params) {
1722 /* create the pcm */
1a497983 1723 ret = soc_new_pcm(rtd, rtd->num);
1245b700 1724 if (ret < 0) {
f110bfc7 1725 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1245b700 1726 dai_link->stream_name, ret);
c74184ed
MB
1727 return ret;
1728 }
25f7b701
AP
1729 ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd);
1730 if (ret < 0)
1731 return ret;
1732 ret = soc_link_dai_pcm_new(rtd->codec_dais,
1733 rtd->num_codecs, rtd);
1734 if (ret < 0)
1735 return ret;
1245b700 1736 } else {
9d58a077
RF
1737 INIT_DELAYED_WORK(&rtd->delayed_work,
1738 codec2codec_close_delayed_work);
1739
1245b700 1740 /* link the DAI widgets */
3f901a02 1741 ret = soc_link_dai_widgets(card, dai_link, rtd);
2436a723
MLC
1742 if (ret)
1743 return ret;
c74184ed 1744 }
f0fba2ad
LG
1745 }
1746
f0fba2ad
LG
1747 return 0;
1748}
1749
44c69bb1 1750static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
3ca041ed
SR
1751{
1752 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
f2ed6b07
ML
1753 struct snd_soc_component *component;
1754 const char *name;
1755 struct device_node *codec_of_node;
1756
1757 if (aux_dev->codec_of_node || aux_dev->codec_name) {
1758 /* codecs, usually analog devices */
1759 name = aux_dev->codec_name;
1760 codec_of_node = aux_dev->codec_of_node;
1761 component = soc_find_component(codec_of_node, name);
1762 if (!component) {
1763 if (codec_of_node)
1764 name = of_node_full_name(codec_of_node);
1765 goto err_defer;
1766 }
1767 } else if (aux_dev->name) {
1768 /* generic components */
1769 name = aux_dev->name;
1770 component = soc_find_component(NULL, name);
1771 if (!component)
1772 goto err_defer;
1773 } else {
1774 dev_err(card->dev, "ASoC: Invalid auxiliary device\n");
1775 return -EINVAL;
3ca041ed 1776 }
fb099cb7 1777
f2ed6b07 1778 component->init = aux_dev->init;
1a653aa4 1779 component->auxiliary = 1;
d2e3a135 1780 list_add(&component->card_aux_list, &card->aux_comp_list);
1a653aa4 1781
44c69bb1 1782 return 0;
f2ed6b07
ML
1783
1784err_defer:
1785 dev_err(card->dev, "ASoC: %s not registered\n", name);
1786 return -EPROBE_DEFER;
b19e6e7b
MB
1787}
1788
f2ed6b07 1789static int soc_probe_aux_devices(struct snd_soc_card *card)
2eea392d 1790{
d2e3a135 1791 struct snd_soc_component *comp, *tmp;
f2ed6b07 1792 int order;
44c69bb1 1793 int ret;
3ca041ed 1794
f2ed6b07
ML
1795 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1796 order++) {
d2e3a135
SN
1797 list_for_each_entry_safe(comp, tmp, &card->aux_comp_list,
1798 card_aux_list) {
f2ed6b07
ML
1799 if (comp->driver->probe_order == order) {
1800 ret = soc_probe_component(card, comp);
1801 if (ret < 0) {
1802 dev_err(card->dev,
1803 "ASoC: failed to probe aux component %s %d\n",
1804 comp->name, ret);
1805 return ret;
1806 }
d2e3a135 1807 list_del(&comp->card_aux_list);
f2ed6b07 1808 }
5f3484ac
LPC
1809 }
1810 }
2eea392d 1811
f2ed6b07 1812 return 0;
2eea392d
JN
1813}
1814
f2ed6b07 1815static void soc_remove_aux_devices(struct snd_soc_card *card)
2eea392d 1816{
f2ed6b07
ML
1817 struct snd_soc_component *comp, *_comp;
1818 int order;
2eea392d 1819
f2ed6b07
ML
1820 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1821 order++) {
1822 list_for_each_entry_safe(comp, _comp,
1a653aa4
KM
1823 &card->component_dev_list, card_list) {
1824
1825 if (!comp->auxiliary)
1826 continue;
1827
f2ed6b07
ML
1828 if (comp->driver->remove_order == order) {
1829 soc_remove_component(comp);
1a653aa4 1830 comp->auxiliary = 0;
f2ed6b07
ML
1831 }
1832 }
2eea392d 1833 }
2eea392d
JN
1834}
1835
f90fb3f7 1836static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
fdf0f54d
DP
1837{
1838 int ret;
1839
1840 if (codec->cache_init)
1841 return 0;
1842
fdf0f54d
DP
1843 ret = snd_soc_cache_init(codec);
1844 if (ret < 0) {
10e8aa9a
MM
1845 dev_err(codec->dev,
1846 "ASoC: Failed to set cache compression type: %d\n",
1847 ret);
fdf0f54d
DP
1848 return ret;
1849 }
1850 codec->cache_init = 1;
1851 return 0;
1852}
1853
ce64c8b9
LPC
1854/**
1855 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1856 * @rtd: The runtime for which the DAI link format should be changed
1857 * @dai_fmt: The new DAI link format
1858 *
1859 * This function updates the DAI link format for all DAIs connected to the DAI
1860 * link for the specified runtime.
1861 *
1862 * Note: For setups with a static format set the dai_fmt field in the
1863 * corresponding snd_dai_link struct instead of using this function.
1864 *
1865 * Returns 0 on success, otherwise a negative error code.
1866 */
1867int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1868 unsigned int dai_fmt)
1869{
1870 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1871 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1872 unsigned int i;
1873 int ret;
1874
1875 for (i = 0; i < rtd->num_codecs; i++) {
1876 struct snd_soc_dai *codec_dai = codec_dais[i];
1877
1878 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1879 if (ret != 0 && ret != -ENOTSUPP) {
1880 dev_warn(codec_dai->dev,
1881 "ASoC: Failed to set DAI format: %d\n", ret);
1882 return ret;
1883 }
1884 }
1885
1886 /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */
1887 if (cpu_dai->codec) {
1888 unsigned int inv_dai_fmt;
1889
1890 inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK;
1891 switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1892 case SND_SOC_DAIFMT_CBM_CFM:
1893 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1894 break;
1895 case SND_SOC_DAIFMT_CBM_CFS:
1896 inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1897 break;
1898 case SND_SOC_DAIFMT_CBS_CFM:
1899 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1900 break;
1901 case SND_SOC_DAIFMT_CBS_CFS:
1902 inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1903 break;
1904 }
1905
1906 dai_fmt = inv_dai_fmt;
1907 }
1908
1909 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
1910 if (ret != 0 && ret != -ENOTSUPP) {
1911 dev_warn(cpu_dai->dev,
1912 "ASoC: Failed to set DAI format: %d\n", ret);
1913 return ret;
1914 }
1915
1916 return 0;
1917}
ddaca25a 1918EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
ce64c8b9 1919
345233d7 1920
1f5a4535 1921#ifdef CONFIG_DMI
345233d7
LG
1922/* Trim special characters, and replace '-' with '_' since '-' is used to
1923 * separate different DMI fields in the card long name. Only number and
1924 * alphabet characters and a few separator characters are kept.
1925 */
1926static void cleanup_dmi_name(char *name)
1927{
1928 int i, j = 0;
1929
1930 for (i = 0; name[i]; i++) {
1931 if (isalnum(name[i]) || (name[i] == '.')
1932 || (name[i] == '_'))
1933 name[j++] = name[i];
1934 else if (name[i] == '-')
1935 name[j++] = '_';
1936 }
1937
1938 name[j] = '\0';
1939}
1940
1941/**
1942 * snd_soc_set_dmi_name() - Register DMI names to card
1943 * @card: The card to register DMI names
1944 * @flavour: The flavour "differentiator" for the card amongst its peers.
1945 *
1946 * An Intel machine driver may be used by many different devices but are
1947 * difficult for userspace to differentiate, since machine drivers ususally
1948 * use their own name as the card short name and leave the card long name
1949 * blank. To differentiate such devices and fix bugs due to lack of
1950 * device-specific configurations, this function allows DMI info to be used
1951 * as the sound card long name, in the format of
1952 * "vendor-product-version-board"
1953 * (Character '-' is used to separate different DMI fields here).
1954 * This will help the user space to load the device-specific Use Case Manager
1955 * (UCM) configurations for the card.
1956 *
1957 * Possible card long names may be:
1958 * DellInc.-XPS139343-01-0310JH
1959 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1960 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1961 *
1962 * This function also supports flavoring the card longname to provide
1963 * the extra differentiation, like "vendor-product-version-board-flavor".
1964 *
1965 * We only keep number and alphabet characters and a few separator characters
1966 * in the card long name since UCM in the user space uses the card long names
1967 * as card configuration directory names and AudoConf cannot support special
1968 * charactors like SPACE.
1969 *
1970 * Returns 0 on success, otherwise a negative error code.
1971 */
1972int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour)
1973{
1974 const char *vendor, *product, *product_version, *board;
1975 size_t longname_buf_size = sizeof(card->snd_card->longname);
1976 size_t len;
1977
1978 if (card->long_name)
1979 return 0; /* long name already set by driver or from DMI */
1980
1981 /* make up dmi long name as: vendor.product.version.board */
1982 vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1983 if (!vendor) {
1984 dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1985 return 0;
1986 }
1987
1988 snprintf(card->dmi_longname, sizeof(card->snd_card->longname),
1989 "%s", vendor);
1990 cleanup_dmi_name(card->dmi_longname);
1991
1992 product = dmi_get_system_info(DMI_PRODUCT_NAME);
1993 if (product) {
1994 len = strlen(card->dmi_longname);
1995 snprintf(card->dmi_longname + len,
1996 longname_buf_size - len,
1997 "-%s", product);
1998
1999 len++; /* skip the separator "-" */
2000 if (len < longname_buf_size)
2001 cleanup_dmi_name(card->dmi_longname + len);
2002
2003 /* some vendors like Lenovo may only put a self-explanatory
2004 * name in the product version field
2005 */
2006 product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
2007 if (product_version) {
2008 len = strlen(card->dmi_longname);
2009 snprintf(card->dmi_longname + len,
2010 longname_buf_size - len,
2011 "-%s", product_version);
2012
2013 len++;
2014 if (len < longname_buf_size)
2015 cleanup_dmi_name(card->dmi_longname + len);
2016 }
2017 }
2018
2019 board = dmi_get_system_info(DMI_BOARD_NAME);
2020 if (board) {
2021 len = strlen(card->dmi_longname);
2022 snprintf(card->dmi_longname + len,
2023 longname_buf_size - len,
2024 "-%s", board);
2025
2026 len++;
2027 if (len < longname_buf_size)
2028 cleanup_dmi_name(card->dmi_longname + len);
2029 } else if (!product) {
2030 /* fall back to using legacy name */
2031 dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
2032 return 0;
2033 }
2034
2035 /* Add flavour to dmi long name */
2036 if (flavour) {
2037 len = strlen(card->dmi_longname);
2038 snprintf(card->dmi_longname + len,
2039 longname_buf_size - len,
2040 "-%s", flavour);
2041
2042 len++;
2043 if (len < longname_buf_size)
2044 cleanup_dmi_name(card->dmi_longname + len);
2045 }
2046
2047 /* set the card long name */
2048 card->long_name = card->dmi_longname;
2049
2050 return 0;
2051}
2052EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name);
1f5a4535 2053#endif /* CONFIG_DMI */
345233d7 2054
b19e6e7b 2055static int snd_soc_instantiate_card(struct snd_soc_card *card)
f0fba2ad 2056{
fdf0f54d 2057 struct snd_soc_codec *codec;
1a497983 2058 struct snd_soc_pcm_runtime *rtd;
61b0088b 2059 struct snd_soc_dai_link *dai_link;
ce64c8b9 2060 int ret, i, order;
fe3e78e0 2061
34e81ab4 2062 mutex_lock(&client_mutex);
01b9d99a 2063 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
dbe21408 2064
f0fba2ad 2065 /* bind DAIs */
b19e6e7b 2066 for (i = 0; i < card->num_links; i++) {
6f2f1ff0 2067 ret = soc_bind_dai_link(card, &card->dai_link[i]);
b19e6e7b
MB
2068 if (ret != 0)
2069 goto base_error;
2070 }
fe3e78e0 2071
44c69bb1 2072 /* bind aux_devs too */
b19e6e7b 2073 for (i = 0; i < card->num_aux_devs; i++) {
44c69bb1 2074 ret = soc_bind_aux_dev(card, i);
b19e6e7b
MB
2075 if (ret != 0)
2076 goto base_error;
f0fba2ad 2077 }
435c5e25 2078
f8f80361
ML
2079 /* add predefined DAI links to the list */
2080 for (i = 0; i < card->num_links; i++)
2081 snd_soc_add_dai_link(card, card->dai_link+i);
2082
fdf0f54d
DP
2083 /* initialize the register cache for each available codec */
2084 list_for_each_entry(codec, &codec_list, list) {
2085 if (codec->cache_init)
2086 continue;
f90fb3f7 2087 ret = snd_soc_init_codec_cache(codec);
b19e6e7b
MB
2088 if (ret < 0)
2089 goto base_error;
fdf0f54d
DP
2090 }
2091
f0fba2ad 2092 /* card bind complete so register a sound card */
102b5a8d 2093 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
f0fba2ad
LG
2094 card->owner, 0, &card->snd_card);
2095 if (ret < 0) {
10e8aa9a
MM
2096 dev_err(card->dev,
2097 "ASoC: can't create sound card for card %s: %d\n",
2098 card->name, ret);
b19e6e7b 2099 goto base_error;
f0fba2ad 2100 }
f0fba2ad 2101
0757d834
LPC
2102 soc_init_card_debugfs(card);
2103
e37a4970
MB
2104 card->dapm.bias_level = SND_SOC_BIAS_OFF;
2105 card->dapm.dev = card->dev;
2106 card->dapm.card = card;
2107 list_add(&card->dapm.list, &card->dapm_list);
2108
d5d1e0be
LPC
2109#ifdef CONFIG_DEBUG_FS
2110 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
2111#endif
2112
88ee1c61 2113#ifdef CONFIG_PM_SLEEP
f0fba2ad
LG
2114 /* deferred resume work */
2115 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
2116#endif
db2a4165 2117
9a841ebb
MB
2118 if (card->dapm_widgets)
2119 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2120 card->num_dapm_widgets);
2121
f23e860e
NC
2122 if (card->of_dapm_widgets)
2123 snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2124 card->num_of_dapm_widgets);
2125
f0fba2ad
LG
2126 /* initialise the sound card only once */
2127 if (card->probe) {
e7361ec4 2128 ret = card->probe(card);
f0fba2ad
LG
2129 if (ret < 0)
2130 goto card_probe_error;
2131 }
fe3e78e0 2132
62ae68fa 2133 /* probe all components used by DAI links on this card */
0168bf0d
LG
2134 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2135 order++) {
1a497983
ML
2136 list_for_each_entry(rtd, &card->rtd_list, list) {
2137 ret = soc_probe_link_components(card, rtd, order);
0168bf0d 2138 if (ret < 0) {
f110bfc7
LG
2139 dev_err(card->dev,
2140 "ASoC: failed to instantiate card %d\n",
2141 ret);
62ae68fa
SW
2142 goto probe_dai_err;
2143 }
2144 }
2145 }
2146
f2ed6b07
ML
2147 /* probe auxiliary components */
2148 ret = soc_probe_aux_devices(card);
2149 if (ret < 0)
2150 goto probe_dai_err;
2151
61b0088b
ML
2152 /* Find new DAI links added during probing components and bind them.
2153 * Components with topology may bring new DAIs and DAI links.
2154 */
2155 list_for_each_entry(dai_link, &card->dai_link_list, list) {
2156 if (soc_is_dai_link_bound(card, dai_link))
2157 continue;
2158
2159 ret = soc_init_dai_link(card, dai_link);
2160 if (ret)
2161 goto probe_dai_err;
2162 ret = soc_bind_dai_link(card, dai_link);
2163 if (ret)
2164 goto probe_dai_err;
2165 }
2166
62ae68fa
SW
2167 /* probe all DAI links on this card */
2168 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
2169 order++) {
1a497983
ML
2170 list_for_each_entry(rtd, &card->rtd_list, list) {
2171 ret = soc_probe_link_dais(card, rtd, order);
62ae68fa 2172 if (ret < 0) {
f110bfc7
LG
2173 dev_err(card->dev,
2174 "ASoC: failed to instantiate card %d\n",
2175 ret);
0168bf0d
LG
2176 goto probe_dai_err;
2177 }
f0fba2ad
LG
2178 }
2179 }
db2a4165 2180
888df395 2181 snd_soc_dapm_link_dai_widgets(card);
b893ea5f 2182 snd_soc_dapm_connect_dai_link_widgets(card);
888df395 2183
b7af1daf 2184 if (card->controls)
022658be 2185 snd_soc_add_card_controls(card, card->controls, card->num_controls);
b7af1daf 2186
b8ad29de
MB
2187 if (card->dapm_routes)
2188 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2189 card->num_dapm_routes);
2190
f23e860e
NC
2191 if (card->of_dapm_routes)
2192 snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2193 card->num_of_dapm_routes);
75d9ac46 2194
f0fba2ad 2195 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
f0fba2ad 2196 "%s", card->name);
22de71ba
LG
2197 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
2198 "%s", card->long_name ? card->long_name : card->name);
f0e8ed85
MB
2199 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
2200 "%s", card->driver_name ? card->driver_name : card->name);
2201 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
2202 switch (card->snd_card->driver[i]) {
2203 case '_':
2204 case '-':
2205 case '\0':
2206 break;
2207 default:
2208 if (!isalnum(card->snd_card->driver[i]))
2209 card->snd_card->driver[i] = '_';
2210 break;
2211 }
2212 }
f0fba2ad 2213
28e9ad92
MB
2214 if (card->late_probe) {
2215 ret = card->late_probe(card);
2216 if (ret < 0) {
f110bfc7 2217 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
28e9ad92
MB
2218 card->name, ret);
2219 goto probe_aux_dev_err;
2220 }
2221 }
2222
824ef826 2223 snd_soc_dapm_new_widgets(card);
8c193b8d 2224
f0fba2ad
LG
2225 ret = snd_card_register(card->snd_card);
2226 if (ret < 0) {
f110bfc7
LG
2227 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2228 ret);
6b3ed785 2229 goto probe_aux_dev_err;
db2a4165
FM
2230 }
2231
f0fba2ad 2232 card->instantiated = 1;
4f4c0072 2233 snd_soc_dapm_sync(&card->dapm);
f0fba2ad 2234 mutex_unlock(&card->mutex);
34e81ab4 2235 mutex_unlock(&client_mutex);
b19e6e7b
MB
2236
2237 return 0;
f0fba2ad 2238
2eea392d 2239probe_aux_dev_err:
f2ed6b07 2240 soc_remove_aux_devices(card);
2eea392d 2241
f0fba2ad 2242probe_dai_err:
0671fd8e 2243 soc_remove_dai_links(card);
f0fba2ad
LG
2244
2245card_probe_error:
87506549 2246 if (card->remove)
e7361ec4 2247 card->remove(card);
f0fba2ad 2248
2210438b 2249 snd_soc_dapm_free(&card->dapm);
0757d834 2250 soc_cleanup_card_debugfs(card);
f0fba2ad
LG
2251 snd_card_free(card->snd_card);
2252
b19e6e7b 2253base_error:
1a497983 2254 soc_remove_pcm_runtimes(card);
f0fba2ad 2255 mutex_unlock(&card->mutex);
34e81ab4 2256 mutex_unlock(&client_mutex);
db2a4165 2257
b19e6e7b 2258 return ret;
435c5e25
MB
2259}
2260
2261/* probes a new socdev */
2262static int soc_probe(struct platform_device *pdev)
2263{
f0fba2ad 2264 struct snd_soc_card *card = platform_get_drvdata(pdev);
435c5e25 2265
70a7ca34
VK
2266 /*
2267 * no card, so machine driver should be registering card
2268 * we should not be here in that case so ret error
2269 */
2270 if (!card)
2271 return -EINVAL;
2272
fe4085e8 2273 dev_warn(&pdev->dev,
f110bfc7 2274 "ASoC: machine %s should use snd_soc_register_card()\n",
fe4085e8
MB
2275 card->name);
2276
435c5e25
MB
2277 /* Bodge while we unpick instantiation */
2278 card->dev = &pdev->dev;
f0fba2ad 2279
28d528c8 2280 return snd_soc_register_card(card);
db2a4165
FM
2281}
2282
b0e26485 2283static int soc_cleanup_card_resources(struct snd_soc_card *card)
db2a4165 2284{
1a497983 2285 struct snd_soc_pcm_runtime *rtd;
db2a4165 2286
b0e26485 2287 /* make sure any delayed work runs */
1a497983 2288 list_for_each_entry(rtd, &card->rtd_list, list)
43829731 2289 flush_delayed_work(&rtd->delayed_work);
914dc182 2290
b0e26485 2291 /* remove and free each DAI */
0671fd8e 2292 soc_remove_dai_links(card);
1a497983 2293 soc_remove_pcm_runtimes(card);
2eea392d 2294
f2ed6b07
ML
2295 /* remove auxiliary devices */
2296 soc_remove_aux_devices(card);
2297
d1e81428 2298 snd_soc_dapm_free(&card->dapm);
b0e26485 2299 soc_cleanup_card_debugfs(card);
f0fba2ad 2300
b0e26485
VK
2301 /* remove the card */
2302 if (card->remove)
e7361ec4 2303 card->remove(card);
a6052154 2304
b0e26485
VK
2305 snd_card_free(card->snd_card);
2306 return 0;
2307
2308}
2309
2310/* removes a socdev */
2311static int soc_remove(struct platform_device *pdev)
2312{
2313 struct snd_soc_card *card = platform_get_drvdata(pdev);
db2a4165 2314
c5af3a2e 2315 snd_soc_unregister_card(card);
db2a4165
FM
2316 return 0;
2317}
2318
6f8ab4ac 2319int snd_soc_poweroff(struct device *dev)
51737470 2320{
6f8ab4ac 2321 struct snd_soc_card *card = dev_get_drvdata(dev);
1a497983 2322 struct snd_soc_pcm_runtime *rtd;
51737470
MB
2323
2324 if (!card->instantiated)
416356fc 2325 return 0;
51737470
MB
2326
2327 /* Flush out pmdown_time work - we actually do want to run it
2328 * now, we're shutting down so no imminent restart. */
1a497983 2329 list_for_each_entry(rtd, &card->rtd_list, list)
43829731 2330 flush_delayed_work(&rtd->delayed_work);
51737470 2331
f0fba2ad 2332 snd_soc_dapm_shutdown(card);
416356fc 2333
988e8cc4 2334 /* deactivate pins to sleep state */
1a497983 2335 list_for_each_entry(rtd, &card->rtd_list, list) {
88bd870f 2336 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1a497983 2337 int i;
88bd870f 2338
988e8cc4 2339 pinctrl_pm_select_sleep_state(cpu_dai->dev);
1a497983
ML
2340 for (i = 0; i < rtd->num_codecs; i++) {
2341 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
88bd870f
BC
2342 pinctrl_pm_select_sleep_state(codec_dai->dev);
2343 }
988e8cc4
NC
2344 }
2345
416356fc 2346 return 0;
51737470 2347}
6f8ab4ac 2348EXPORT_SYMBOL_GPL(snd_soc_poweroff);
51737470 2349
6f8ab4ac 2350const struct dev_pm_ops snd_soc_pm_ops = {
b1dd5897
VK
2351 .suspend = snd_soc_suspend,
2352 .resume = snd_soc_resume,
2353 .freeze = snd_soc_suspend,
2354 .thaw = snd_soc_resume,
6f8ab4ac 2355 .poweroff = snd_soc_poweroff,
b1dd5897 2356 .restore = snd_soc_resume,
416356fc 2357};
deb2607e 2358EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
416356fc 2359
db2a4165
FM
2360/* ASoC platform driver */
2361static struct platform_driver soc_driver = {
2362 .driver = {
2363 .name = "soc-audio",
6f8ab4ac 2364 .pm = &snd_soc_pm_ops,
db2a4165
FM
2365 },
2366 .probe = soc_probe,
2367 .remove = soc_remove,
db2a4165
FM
2368};
2369
db2a4165
FM
2370/**
2371 * snd_soc_cnew - create new control
2372 * @_template: control template
2373 * @data: control private data
ac11a2b3 2374 * @long_name: control long name
efb7ac3f 2375 * @prefix: control name prefix
db2a4165
FM
2376 *
2377 * Create a new mixer control from a template control.
2378 *
2379 * Returns 0 for success, else error.
2380 */
2381struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
3056557f 2382 void *data, const char *long_name,
efb7ac3f 2383 const char *prefix)
db2a4165
FM
2384{
2385 struct snd_kcontrol_new template;
efb7ac3f
MB
2386 struct snd_kcontrol *kcontrol;
2387 char *name = NULL;
db2a4165
FM
2388
2389 memcpy(&template, _template, sizeof(template));
db2a4165
FM
2390 template.index = 0;
2391
efb7ac3f
MB
2392 if (!long_name)
2393 long_name = template.name;
2394
2395 if (prefix) {
2b581074 2396 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
efb7ac3f
MB
2397 if (!name)
2398 return NULL;
2399
efb7ac3f
MB
2400 template.name = name;
2401 } else {
2402 template.name = long_name;
2403 }
2404
2405 kcontrol = snd_ctl_new1(&template, data);
2406
2407 kfree(name);
2408
2409 return kcontrol;
db2a4165
FM
2410}
2411EXPORT_SYMBOL_GPL(snd_soc_cnew);
2412
022658be
LG
2413static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2414 const struct snd_kcontrol_new *controls, int num_controls,
2415 const char *prefix, void *data)
2416{
2417 int err, i;
2418
2419 for (i = 0; i < num_controls; i++) {
2420 const struct snd_kcontrol_new *control = &controls[i];
2421 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2422 control->name, prefix));
2423 if (err < 0) {
f110bfc7
LG
2424 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2425 control->name, err);
022658be
LG
2426 return err;
2427 }
2428 }
2429
2430 return 0;
2431}
2432
4fefd698
DP
2433struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2434 const char *name)
2435{
2436 struct snd_card *card = soc_card->snd_card;
2437 struct snd_kcontrol *kctl;
2438
2439 if (unlikely(!name))
2440 return NULL;
2441
2442 list_for_each_entry(kctl, &card->controls, list)
2443 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2444 return kctl;
2445 return NULL;
2446}
2447EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2448
0f2780ad
LPC
2449/**
2450 * snd_soc_add_component_controls - Add an array of controls to a component.
2451 *
2452 * @component: Component to add controls to
2453 * @controls: Array of controls to add
2454 * @num_controls: Number of elements in the array
2455 *
2456 * Return: 0 for success, else error.
2457 */
2458int snd_soc_add_component_controls(struct snd_soc_component *component,
2459 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2460{
2461 struct snd_card *card = component->card->snd_card;
2462
2463 return snd_soc_add_controls(card, component->dev, controls,
2464 num_controls, component->name_prefix, component);
2465}
2466EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2467
3e8e1952 2468/**
022658be
LG
2469 * snd_soc_add_codec_controls - add an array of controls to a codec.
2470 * Convenience function to add a list of controls. Many codecs were
3e8e1952
IM
2471 * duplicating this code.
2472 *
2473 * @codec: codec to add controls to
2474 * @controls: array of controls to add
2475 * @num_controls: number of elements in the array
2476 *
2477 * Return 0 for success, else error.
2478 */
022658be 2479int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
0f2780ad 2480 const struct snd_kcontrol_new *controls, unsigned int num_controls)
3e8e1952 2481{
0f2780ad
LPC
2482 return snd_soc_add_component_controls(&codec->component, controls,
2483 num_controls);
3e8e1952 2484}
022658be 2485EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
3e8e1952 2486
a491a5c8
LG
2487/**
2488 * snd_soc_add_platform_controls - add an array of controls to a platform.
022658be 2489 * Convenience function to add a list of controls.
a491a5c8
LG
2490 *
2491 * @platform: platform to add controls to
2492 * @controls: array of controls to add
2493 * @num_controls: number of elements in the array
2494 *
2495 * Return 0 for success, else error.
2496 */
2497int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
0f2780ad 2498 const struct snd_kcontrol_new *controls, unsigned int num_controls)
a491a5c8 2499{
0f2780ad
LPC
2500 return snd_soc_add_component_controls(&platform->component, controls,
2501 num_controls);
a491a5c8
LG
2502}
2503EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2504
022658be
LG
2505/**
2506 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2507 * Convenience function to add a list of controls.
2508 *
2509 * @soc_card: SoC card to add controls to
2510 * @controls: array of controls to add
2511 * @num_controls: number of elements in the array
2512 *
2513 * Return 0 for success, else error.
2514 */
2515int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2516 const struct snd_kcontrol_new *controls, int num_controls)
2517{
2518 struct snd_card *card = soc_card->snd_card;
2519
2520 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2521 NULL, soc_card);
2522}
2523EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2524
2525/**
2526 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2527 * Convienience function to add a list of controls.
2528 *
2529 * @dai: DAI to add controls to
2530 * @controls: array of controls to add
2531 * @num_controls: number of elements in the array
2532 *
2533 * Return 0 for success, else error.
2534 */
2535int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2536 const struct snd_kcontrol_new *controls, int num_controls)
2537{
313665b9 2538 struct snd_card *card = dai->component->card->snd_card;
022658be
LG
2539
2540 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2541 NULL, dai);
2542}
2543EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2544
8c6529db
LG
2545/**
2546 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2547 * @dai: DAI
2548 * @clk_id: DAI specific clock ID
2549 * @freq: new clock frequency in Hz
2550 * @dir: new clock direction - input/output.
2551 *
2552 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2553 */
2554int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2555 unsigned int freq, int dir)
2556{
f0fba2ad
LG
2557 if (dai->driver && dai->driver->ops->set_sysclk)
2558 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
ec4ee52a 2559 else if (dai->codec && dai->codec->driver->set_sysclk)
da1c6ea6 2560 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
ec4ee52a 2561 freq, dir);
8c6529db 2562 else
1104a9c8 2563 return -ENOTSUPP;
8c6529db
LG
2564}
2565EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2566
ec4ee52a
MB
2567/**
2568 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2569 * @codec: CODEC
2570 * @clk_id: DAI specific clock ID
da1c6ea6 2571 * @source: Source for the clock
ec4ee52a
MB
2572 * @freq: new clock frequency in Hz
2573 * @dir: new clock direction - input/output.
2574 *
2575 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2576 */
2577int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
da1c6ea6 2578 int source, unsigned int freq, int dir)
ec4ee52a
MB
2579{
2580 if (codec->driver->set_sysclk)
da1c6ea6
MB
2581 return codec->driver->set_sysclk(codec, clk_id, source,
2582 freq, dir);
ec4ee52a 2583 else
1104a9c8 2584 return -ENOTSUPP;
ec4ee52a
MB
2585}
2586EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2587
8c6529db
LG
2588/**
2589 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2590 * @dai: DAI
ac11a2b3 2591 * @div_id: DAI specific clock divider ID
8c6529db
LG
2592 * @div: new clock divisor.
2593 *
2594 * Configures the clock dividers. This is used to derive the best DAI bit and
2595 * frame clocks from the system or master clock. It's best to set the DAI bit
2596 * and frame clocks as low as possible to save system power.
2597 */
2598int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2599 int div_id, int div)
2600{
f0fba2ad
LG
2601 if (dai->driver && dai->driver->ops->set_clkdiv)
2602 return dai->driver->ops->set_clkdiv(dai, div_id, div);
8c6529db
LG
2603 else
2604 return -EINVAL;
2605}
2606EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2607
2608/**
2609 * snd_soc_dai_set_pll - configure DAI PLL.
2610 * @dai: DAI
2611 * @pll_id: DAI specific PLL ID
85488037 2612 * @source: DAI specific source for the PLL
8c6529db
LG
2613 * @freq_in: PLL input clock frequency in Hz
2614 * @freq_out: requested PLL output clock frequency in Hz
2615 *
2616 * Configures and enables PLL to generate output clock based on input clock.
2617 */
85488037
MB
2618int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2619 unsigned int freq_in, unsigned int freq_out)
8c6529db 2620{
f0fba2ad
LG
2621 if (dai->driver && dai->driver->ops->set_pll)
2622 return dai->driver->ops->set_pll(dai, pll_id, source,
85488037 2623 freq_in, freq_out);
ec4ee52a
MB
2624 else if (dai->codec && dai->codec->driver->set_pll)
2625 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2626 freq_in, freq_out);
8c6529db
LG
2627 else
2628 return -EINVAL;
2629}
2630EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2631
ec4ee52a
MB
2632/*
2633 * snd_soc_codec_set_pll - configure codec PLL.
2634 * @codec: CODEC
2635 * @pll_id: DAI specific PLL ID
2636 * @source: DAI specific source for the PLL
2637 * @freq_in: PLL input clock frequency in Hz
2638 * @freq_out: requested PLL output clock frequency in Hz
2639 *
2640 * Configures and enables PLL to generate output clock based on input clock.
2641 */
2642int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2643 unsigned int freq_in, unsigned int freq_out)
2644{
2645 if (codec->driver->set_pll)
2646 return codec->driver->set_pll(codec, pll_id, source,
2647 freq_in, freq_out);
2648 else
2649 return -EINVAL;
2650}
2651EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2652
e54cf76b
LG
2653/**
2654 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
2655 * @dai: DAI
231b86b1 2656 * @ratio: Ratio of BCLK to Sample rate.
e54cf76b
LG
2657 *
2658 * Configures the DAI for a preset BCLK to sample rate ratio.
2659 */
2660int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2661{
2662 if (dai->driver && dai->driver->ops->set_bclk_ratio)
2663 return dai->driver->ops->set_bclk_ratio(dai, ratio);
2664 else
2665 return -EINVAL;
2666}
2667EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
2668
8c6529db
LG
2669/**
2670 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2671 * @dai: DAI
8c6529db
LG
2672 * @fmt: SND_SOC_DAIFMT_ format value.
2673 *
2674 * Configures the DAI hardware format and clocking.
2675 */
2676int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2677{
5e4ba569 2678 if (dai->driver == NULL)
8c6529db 2679 return -EINVAL;
5e4ba569
SG
2680 if (dai->driver->ops->set_fmt == NULL)
2681 return -ENOTSUPP;
2682 return dai->driver->ops->set_fmt(dai, fmt);
8c6529db
LG
2683}
2684EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2685
89c67857 2686/**
e5c21514 2687 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
89c67857
XL
2688 * @slots: Number of slots in use.
2689 * @tx_mask: bitmask representing active TX slots.
2690 * @rx_mask: bitmask representing active RX slots.
2691 *
2692 * Generates the TDM tx and rx slot default masks for DAI.
2693 */
e5c21514 2694static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
89c67857
XL
2695 unsigned int *tx_mask,
2696 unsigned int *rx_mask)
2697{
2698 if (*tx_mask || *rx_mask)
2699 return 0;
2700
2701 if (!slots)
2702 return -EINVAL;
2703
2704 *tx_mask = (1 << slots) - 1;
2705 *rx_mask = (1 << slots) - 1;
2706
2707 return 0;
2708}
2709
8c6529db 2710/**
e46c9366
LPC
2711 * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation
2712 * @dai: The DAI to configure
a5479e38
DR
2713 * @tx_mask: bitmask representing active TX slots.
2714 * @rx_mask: bitmask representing active RX slots.
8c6529db 2715 * @slots: Number of slots in use.
a5479e38 2716 * @slot_width: Width in bits for each slot.
8c6529db 2717 *
e46c9366
LPC
2718 * This function configures the specified DAI for TDM operation. @slot contains
2719 * the total number of slots of the TDM stream and @slot_with the width of each
2720 * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the
2721 * active slots of the TDM stream for the specified DAI, i.e. which slots the
2722 * DAI should write to or read from. If a bit is set the corresponding slot is
2723 * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to
2724 * the first slot, bit 1 to the second slot and so on. The first active slot
2725 * maps to the first channel of the DAI, the second active slot to the second
2726 * channel and so on.
2727 *
2728 * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask,
2729 * @rx_mask and @slot_width will be ignored.
2730 *
2731 * Returns 0 on success, a negative error code otherwise.
8c6529db
LG
2732 */
2733int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
a5479e38 2734 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
8c6529db 2735{
e5c21514
XL
2736 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
2737 dai->driver->ops->xlate_tdm_slot_mask(slots,
89c67857
XL
2738 &tx_mask, &rx_mask);
2739 else
e5c21514 2740 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
89c67857 2741
88bd870f
BC
2742 dai->tx_mask = tx_mask;
2743 dai->rx_mask = rx_mask;
2744
f0fba2ad
LG
2745 if (dai->driver && dai->driver->ops->set_tdm_slot)
2746 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
a5479e38 2747 slots, slot_width);
8c6529db 2748 else
b2cbb6e1 2749 return -ENOTSUPP;
8c6529db
LG
2750}
2751EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2752
472df3cb
BS
2753/**
2754 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2755 * @dai: DAI
2756 * @tx_num: how many TX channels
2757 * @tx_slot: pointer to an array which imply the TX slot number channel
2758 * 0~num-1 uses
2759 * @rx_num: how many RX channels
2760 * @rx_slot: pointer to an array which imply the RX slot number channel
2761 * 0~num-1 uses
2762 *
2763 * configure the relationship between channel number and TDM slot number.
2764 */
2765int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2766 unsigned int tx_num, unsigned int *tx_slot,
2767 unsigned int rx_num, unsigned int *rx_slot)
2768{
f0fba2ad
LG
2769 if (dai->driver && dai->driver->ops->set_channel_map)
2770 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
472df3cb
BS
2771 rx_num, rx_slot);
2772 else
2773 return -EINVAL;
2774}
2775EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2776
8c6529db
LG
2777/**
2778 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2779 * @dai: DAI
2780 * @tristate: tristate enable
2781 *
2782 * Tristates the DAI so that others can use it.
2783 */
2784int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2785{
f0fba2ad
LG
2786 if (dai->driver && dai->driver->ops->set_tristate)
2787 return dai->driver->ops->set_tristate(dai, tristate);
8c6529db
LG
2788 else
2789 return -EINVAL;
2790}
2791EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2792
2793/**
2794 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2795 * @dai: DAI
2796 * @mute: mute enable
da18396f 2797 * @direction: stream to mute
8c6529db
LG
2798 *
2799 * Mutes the DAI DAC.
2800 */
da18396f
MB
2801int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
2802 int direction)
8c6529db 2803{
da18396f
MB
2804 if (!dai->driver)
2805 return -ENOTSUPP;
2806
2807 if (dai->driver->ops->mute_stream)
2808 return dai->driver->ops->mute_stream(dai, mute, direction);
2809 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
2810 dai->driver->ops->digital_mute)
f0fba2ad 2811 return dai->driver->ops->digital_mute(dai, mute);
8c6529db 2812 else
04570c62 2813 return -ENOTSUPP;
8c6529db
LG
2814}
2815EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2816
c5af3a2e
MB
2817/**
2818 * snd_soc_register_card - Register a card with the ASoC core
2819 *
ac11a2b3 2820 * @card: Card to register
c5af3a2e 2821 *
c5af3a2e 2822 */
70a7ca34 2823int snd_soc_register_card(struct snd_soc_card *card)
c5af3a2e 2824{
923c5e61 2825 int i, ret;
1a497983 2826 struct snd_soc_pcm_runtime *rtd;
f0fba2ad 2827
c5af3a2e
MB
2828 if (!card->name || !card->dev)
2829 return -EINVAL;
2830
5a504963
SW
2831 for (i = 0; i < card->num_links; i++) {
2832 struct snd_soc_dai_link *link = &card->dai_link[i];
2833
923c5e61 2834 ret = soc_init_dai_link(card, link);
88bd870f 2835 if (ret) {
923c5e61 2836 dev_err(card->dev, "ASoC: failed to init link %s\n",
10e8aa9a 2837 link->name);
923c5e61 2838 return ret;
5a504963
SW
2839 }
2840 }
2841
ed77cc12
MB
2842 dev_set_drvdata(card->dev, card);
2843
111c6419
SW
2844 snd_soc_initialize_card_lists(card);
2845
f8f80361
ML
2846 INIT_LIST_HEAD(&card->dai_link_list);
2847 card->num_dai_links = 0;
2848
1a497983
ML
2849 INIT_LIST_HEAD(&card->rtd_list);
2850 card->num_rtd = 0;
2851
db432b41 2852 INIT_LIST_HEAD(&card->dapm_dirty);
8a978234 2853 INIT_LIST_HEAD(&card->dobj_list);
c5af3a2e 2854 card->instantiated = 0;
f0fba2ad 2855 mutex_init(&card->mutex);
a73fb2df 2856 mutex_init(&card->dapm_mutex);
c5af3a2e 2857
b19e6e7b
MB
2858 ret = snd_soc_instantiate_card(card);
2859 if (ret != 0)
4e2576bd 2860 return ret;
c5af3a2e 2861
988e8cc4 2862 /* deactivate pins to sleep state */
1a497983 2863 list_for_each_entry(rtd, &card->rtd_list, list) {
88bd870f
BC
2864 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2865 int j;
2866
2867 for (j = 0; j < rtd->num_codecs; j++) {
2868 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
2869 if (!codec_dai->active)
2870 pinctrl_pm_select_sleep_state(codec_dai->dev);
2871 }
2872
988e8cc4
NC
2873 if (!cpu_dai->active)
2874 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2875 }
2876
b19e6e7b 2877 return ret;
c5af3a2e 2878}
70a7ca34 2879EXPORT_SYMBOL_GPL(snd_soc_register_card);
c5af3a2e
MB
2880
2881/**
2882 * snd_soc_unregister_card - Unregister a card with the ASoC core
2883 *
ac11a2b3 2884 * @card: Card to unregister
c5af3a2e 2885 *
c5af3a2e 2886 */
70a7ca34 2887int snd_soc_unregister_card(struct snd_soc_card *card)
c5af3a2e 2888{
01e0df66
LPC
2889 if (card->instantiated) {
2890 card->instantiated = false;
1c325f77 2891 snd_soc_dapm_shutdown(card);
b0e26485 2892 soc_cleanup_card_resources(card);
8f6f9b29 2893 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
01e0df66 2894 }
c5af3a2e
MB
2895
2896 return 0;
2897}
70a7ca34 2898EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
c5af3a2e 2899
f0fba2ad
LG
2900/*
2901 * Simplify DAI link configuration by removing ".-1" from device names
2902 * and sanitizing names.
2903 */
0b9a214a 2904static char *fmt_single_name(struct device *dev, int *id)
f0fba2ad
LG
2905{
2906 char *found, name[NAME_SIZE];
2907 int id1, id2;
2908
2909 if (dev_name(dev) == NULL)
2910 return NULL;
2911
58818a77 2912 strlcpy(name, dev_name(dev), NAME_SIZE);
f0fba2ad
LG
2913
2914 /* are we a "%s.%d" name (platform and SPI components) */
2915 found = strstr(name, dev->driver->name);
2916 if (found) {
2917 /* get ID */
2918 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2919
2920 /* discard ID from name if ID == -1 */
2921 if (*id == -1)
2922 found[strlen(dev->driver->name)] = '\0';
2923 }
2924
2925 } else {
2926 /* I2C component devices are named "bus-addr" */
2927 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2928 char tmp[NAME_SIZE];
2929
2930 /* create unique ID number from I2C addr and bus */
05899446 2931 *id = ((id1 & 0xffff) << 16) + id2;
f0fba2ad
LG
2932
2933 /* sanitize component name for DAI link creation */
2934 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
58818a77 2935 strlcpy(name, tmp, NAME_SIZE);
f0fba2ad
LG
2936 } else
2937 *id = 0;
2938 }
2939
2940 return kstrdup(name, GFP_KERNEL);
2941}
2942
2943/*
2944 * Simplify DAI link naming for single devices with multiple DAIs by removing
2945 * any ".-1" and using the DAI name (instead of device name).
2946 */
2947static inline char *fmt_multiple_name(struct device *dev,
2948 struct snd_soc_dai_driver *dai_drv)
2949{
2950 if (dai_drv->name == NULL) {
10e8aa9a
MM
2951 dev_err(dev,
2952 "ASoC: error - multiple DAI %s registered with no name\n",
2953 dev_name(dev));
f0fba2ad
LG
2954 return NULL;
2955 }
2956
2957 return kstrdup(dai_drv->name, GFP_KERNEL);
2958}
2959
9115171a 2960/**
32c9ba54 2961 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
9115171a 2962 *
32c9ba54 2963 * @component: The component for which the DAIs should be unregistered
9115171a 2964 */
32c9ba54 2965static void snd_soc_unregister_dais(struct snd_soc_component *component)
9115171a 2966{
5c1d5f09 2967 struct snd_soc_dai *dai, *_dai;
9115171a 2968
5c1d5f09 2969 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
32c9ba54
LPC
2970 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
2971 dai->name);
5c1d5f09 2972 list_del(&dai->list);
32c9ba54 2973 kfree(dai->name);
f0fba2ad 2974 kfree(dai);
f0fba2ad 2975 }
9115171a 2976}
9115171a 2977
5e4fb372
ML
2978/* Create a DAI and add it to the component's DAI list */
2979static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component,
2980 struct snd_soc_dai_driver *dai_drv,
2981 bool legacy_dai_naming)
2982{
2983 struct device *dev = component->dev;
2984 struct snd_soc_dai *dai;
2985
2986 dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev));
2987
2988 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2989 if (dai == NULL)
2990 return NULL;
2991
2992 /*
2993 * Back in the old days when we still had component-less DAIs,
2994 * instead of having a static name, component-less DAIs would
2995 * inherit the name of the parent device so it is possible to
2996 * register multiple instances of the DAI. We still need to keep
2997 * the same naming style even though those DAIs are not
2998 * component-less anymore.
2999 */
3000 if (legacy_dai_naming &&
3001 (dai_drv->id == 0 || dai_drv->name == NULL)) {
3002 dai->name = fmt_single_name(dev, &dai->id);
3003 } else {
3004 dai->name = fmt_multiple_name(dev, dai_drv);
3005 if (dai_drv->id)
3006 dai->id = dai_drv->id;
3007 else
3008 dai->id = component->num_dai;
3009 }
3010 if (dai->name == NULL) {
3011 kfree(dai);
3012 return NULL;
3013 }
3014
3015 dai->component = component;
3016 dai->dev = dev;
3017 dai->driver = dai_drv;
3018 if (!dai->driver->ops)
3019 dai->driver->ops = &null_dai_ops;
3020
3021 list_add(&dai->list, &component->dai_list);
3022 component->num_dai++;
3023
3024 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3025 return dai;
3026}
3027
9115171a 3028/**
32c9ba54 3029 * snd_soc_register_dais - Register a DAI with the ASoC core
9115171a 3030 *
6106d129
LPC
3031 * @component: The component the DAIs are registered for
3032 * @dai_drv: DAI driver to use for the DAIs
ac11a2b3 3033 * @count: Number of DAIs
32c9ba54
LPC
3034 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3035 * parent's name.
9115171a 3036 */
6106d129 3037static int snd_soc_register_dais(struct snd_soc_component *component,
bb13109d
LPC
3038 struct snd_soc_dai_driver *dai_drv, size_t count,
3039 bool legacy_dai_naming)
9115171a 3040{
6106d129 3041 struct device *dev = component->dev;
f0fba2ad 3042 struct snd_soc_dai *dai;
32c9ba54
LPC
3043 unsigned int i;
3044 int ret;
f0fba2ad 3045
5b5e0928 3046 dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count);
9115171a 3047
bb13109d 3048 component->dai_drv = dai_drv;
bb13109d 3049
9115171a 3050 for (i = 0; i < count; i++) {
f0fba2ad 3051
5e4fb372
ML
3052 dai = soc_add_dai(component, dai_drv + i,
3053 count == 1 && legacy_dai_naming);
c46e0079
AL
3054 if (dai == NULL) {
3055 ret = -ENOMEM;
3056 goto err;
3057 }
9115171a 3058 }
f0fba2ad 3059
9115171a 3060 return 0;
f0fba2ad 3061
9115171a 3062err:
32c9ba54 3063 snd_soc_unregister_dais(component);
f0fba2ad 3064
9115171a
MB
3065 return ret;
3066}
9115171a 3067
68003e6c
ML
3068/**
3069 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
3070 *
3071 * @component: The component the DAIs are registered for
3072 * @dai_drv: DAI driver to use for the DAI
3073 *
3074 * Topology can use this API to register DAIs when probing a component.
3075 * These DAIs's widgets will be freed in the card cleanup and the DAIs
3076 * will be freed in the component cleanup.
3077 */
3078int snd_soc_register_dai(struct snd_soc_component *component,
3079 struct snd_soc_dai_driver *dai_drv)
3080{
3081 struct snd_soc_dapm_context *dapm =
3082 snd_soc_component_get_dapm(component);
3083 struct snd_soc_dai *dai;
3084 int ret;
054880fe 3085
68003e6c
ML
3086 if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) {
3087 dev_err(component->dev, "Invalid dai type %d\n",
3088 dai_drv->dobj.type);
3089 return -EINVAL;
9115171a
MB
3090 }
3091
68003e6c
ML
3092 lockdep_assert_held(&client_mutex);
3093 dai = soc_add_dai(component, dai_drv, false);
3094 if (!dai)
3095 return -ENOMEM;
9115171a 3096
68003e6c
ML
3097 /* Create the DAI widgets here. After adding DAIs, topology may
3098 * also add routes that need these widgets as source or sink.
3099 */
3100 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
3101 if (ret != 0) {
3102 dev_err(component->dev,
3103 "Failed to create DAI widgets %d\n", ret);
3104 }
9115171a
MB
3105
3106 return ret;
3107}
68003e6c 3108EXPORT_SYMBOL_GPL(snd_soc_register_dai);
9115171a 3109
14e8bdeb
LPC
3110static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3111 enum snd_soc_dapm_type type, int subseq)
d191bd8d 3112{
14e8bdeb 3113 struct snd_soc_component *component = dapm->component;
d191bd8d 3114
14e8bdeb
LPC
3115 component->driver->seq_notifier(component, type, subseq);
3116}
d191bd8d 3117
14e8bdeb
LPC
3118static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3119 int event)
3120{
3121 struct snd_soc_component *component = dapm->component;
d191bd8d 3122
14e8bdeb
LPC
3123 return component->driver->stream_event(component, event);
3124}
e2c330b9 3125
bb13109d
LPC
3126static int snd_soc_component_initialize(struct snd_soc_component *component,
3127 const struct snd_soc_component_driver *driver, struct device *dev)
d191bd8d 3128{
ce0fc93a
LPC
3129 struct snd_soc_dapm_context *dapm;
3130
bb13109d
LPC
3131 component->name = fmt_single_name(dev, &component->id);
3132 if (!component->name) {
3133 dev_err(dev, "ASoC: Failed to allocate name\n");
d191bd8d
KM
3134 return -ENOMEM;
3135 }
3136
bb13109d
LPC
3137 component->dev = dev;
3138 component->driver = driver;
61aca564
LPC
3139 component->probe = component->driver->probe;
3140 component->remove = component->driver->remove;
9178feb4
KM
3141 component->suspend = component->driver->suspend;
3142 component->resume = component->driver->resume;
99b04f4c
KM
3143 component->pcm_new = component->driver->pcm_new;
3144 component->pcm_free= component->driver->pcm_free;
d191bd8d 3145
4890140f 3146 dapm = &component->dapm;
ce0fc93a
LPC
3147 dapm->dev = dev;
3148 dapm->component = component;
3149 dapm->bias_level = SND_SOC_BIAS_OFF;
5819c2fa 3150 dapm->idle_bias_off = true;
14e8bdeb
LPC
3151 if (driver->seq_notifier)
3152 dapm->seq_notifier = snd_soc_component_seq_notifier;
3153 if (driver->stream_event)
3154 dapm->stream_event = snd_soc_component_stream_event;
d191bd8d 3155
61aca564
LPC
3156 component->controls = driver->controls;
3157 component->num_controls = driver->num_controls;
3158 component->dapm_widgets = driver->dapm_widgets;
3159 component->num_dapm_widgets = driver->num_dapm_widgets;
3160 component->dapm_routes = driver->dapm_routes;
3161 component->num_dapm_routes = driver->num_dapm_routes;
3162
bb13109d
LPC
3163 INIT_LIST_HEAD(&component->dai_list);
3164 mutex_init(&component->io_mutex);
d191bd8d 3165
bb13109d
LPC
3166 return 0;
3167}
d191bd8d 3168
20feb881 3169static void snd_soc_component_setup_regmap(struct snd_soc_component *component)
886f5692 3170{
20feb881
LPC
3171 int val_bytes = regmap_get_val_bytes(component->regmap);
3172
3173 /* Errors are legitimate for non-integer byte multiples */
3174 if (val_bytes > 0)
3175 component->val_bytes = val_bytes;
3176}
3177
e874bf5f
LPC
3178#ifdef CONFIG_REGMAP
3179
20feb881
LPC
3180/**
3181 * snd_soc_component_init_regmap() - Initialize regmap instance for the component
3182 * @component: The component for which to initialize the regmap instance
3183 * @regmap: The regmap instance that should be used by the component
3184 *
3185 * This function allows deferred assignment of the regmap instance that is
3186 * associated with the component. Only use this if the regmap instance is not
3187 * yet ready when the component is registered. The function must also be called
3188 * before the first IO attempt of the component.
3189 */
3190void snd_soc_component_init_regmap(struct snd_soc_component *component,
3191 struct regmap *regmap)
3192{
3193 component->regmap = regmap;
3194 snd_soc_component_setup_regmap(component);
886f5692 3195}
20feb881
LPC
3196EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
3197
3198/**
3199 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component
3200 * @component: The component for which to de-initialize the regmap instance
3201 *
3202 * Calls regmap_exit() on the regmap instance associated to the component and
3203 * removes the regmap instance from the component.
3204 *
3205 * This function should only be used if snd_soc_component_init_regmap() was used
3206 * to initialize the regmap instance.
3207 */
3208void snd_soc_component_exit_regmap(struct snd_soc_component *component)
3209{
3210 regmap_exit(component->regmap);
3211 component->regmap = NULL;
3212}
3213EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
886f5692 3214
e874bf5f 3215#endif
886f5692 3216
bb13109d
LPC
3217static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
3218{
20feb881
LPC
3219 if (!component->write && !component->read) {
3220 if (!component->regmap)
3221 component->regmap = dev_get_regmap(component->dev, NULL);
3222 if (component->regmap)
3223 snd_soc_component_setup_regmap(component);
3224 }
886f5692 3225
bb13109d 3226 list_add(&component->list, &component_list);
8a978234 3227 INIT_LIST_HEAD(&component->dobj_list);
bb13109d 3228}
d191bd8d 3229
bb13109d
LPC
3230static void snd_soc_component_add(struct snd_soc_component *component)
3231{
d191bd8d 3232 mutex_lock(&client_mutex);
bb13109d 3233 snd_soc_component_add_unlocked(component);
d191bd8d 3234 mutex_unlock(&client_mutex);
bb13109d 3235}
d191bd8d 3236
bb13109d
LPC
3237static void snd_soc_component_cleanup(struct snd_soc_component *component)
3238{
3239 snd_soc_unregister_dais(component);
3240 kfree(component->name);
3241}
d191bd8d 3242
bb13109d
LPC
3243static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
3244{
3245 list_del(&component->list);
3246}
d191bd8d 3247
d191bd8d
KM
3248int snd_soc_register_component(struct device *dev,
3249 const struct snd_soc_component_driver *cmpnt_drv,
3250 struct snd_soc_dai_driver *dai_drv,
3251 int num_dai)
3252{
3253 struct snd_soc_component *cmpnt;
bb13109d 3254 int ret;
d191bd8d 3255
bb13109d 3256 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
d191bd8d
KM
3257 if (!cmpnt) {
3258 dev_err(dev, "ASoC: Failed to allocate memory\n");
3259 return -ENOMEM;
3260 }
3261
bb13109d
LPC
3262 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
3263 if (ret)
3264 goto err_free;
3265
3d59400f 3266 cmpnt->ignore_pmdown_time = true;
98e639fb 3267 cmpnt->registered_as_component = true;
3d59400f 3268
bb13109d
LPC
3269 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
3270 if (ret < 0) {
f42cf8d6 3271 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
bb13109d
LPC
3272 goto err_cleanup;
3273 }
d191bd8d 3274
bb13109d 3275 snd_soc_component_add(cmpnt);
4da53393 3276
bb13109d 3277 return 0;
4da53393 3278
bb13109d
LPC
3279err_cleanup:
3280 snd_soc_component_cleanup(cmpnt);
3281err_free:
3282 kfree(cmpnt);
3283 return ret;
4da53393 3284}
bb13109d 3285EXPORT_SYMBOL_GPL(snd_soc_register_component);
4da53393 3286
d191bd8d
KM
3287/**
3288 * snd_soc_unregister_component - Unregister a component from the ASoC core
3289 *
628536ea 3290 * @dev: The device to unregister
d191bd8d
KM
3291 */
3292void snd_soc_unregister_component(struct device *dev)
3293{
3294 struct snd_soc_component *cmpnt;
3295
34e81ab4 3296 mutex_lock(&client_mutex);
d191bd8d 3297 list_for_each_entry(cmpnt, &component_list, list) {
98e639fb 3298 if (dev == cmpnt->dev && cmpnt->registered_as_component)
d191bd8d
KM
3299 goto found;
3300 }
34e81ab4 3301 mutex_unlock(&client_mutex);
d191bd8d
KM
3302 return;
3303
3304found:
8a978234 3305 snd_soc_tplg_component_remove(cmpnt, SND_SOC_TPLG_INDEX_ALL);
34e81ab4
LPC
3306 snd_soc_component_del_unlocked(cmpnt);
3307 mutex_unlock(&client_mutex);
bb13109d
LPC
3308 snd_soc_component_cleanup(cmpnt);
3309 kfree(cmpnt);
d191bd8d
KM
3310}
3311EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
d191bd8d 3312
f1d45cc3 3313static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
e2c330b9
LPC
3314{
3315 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
d191bd8d 3316
f1d45cc3 3317 return platform->driver->probe(platform);
e2c330b9
LPC
3318}
3319
f1d45cc3 3320static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
e2c330b9
LPC
3321{
3322 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
3323
f1d45cc3 3324 platform->driver->remove(platform);
d191bd8d 3325}
d191bd8d 3326
99b04f4c
KM
3327static int snd_soc_platform_drv_pcm_new(struct snd_soc_pcm_runtime *rtd)
3328{
3329 struct snd_soc_platform *platform = rtd->platform;
3330
3331 return platform->driver->pcm_new(rtd);
3332}
3333
3334static void snd_soc_platform_drv_pcm_free(struct snd_pcm *pcm)
3335{
3336 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
3337 struct snd_soc_platform *platform = rtd->platform;
3338
3339 platform->driver->pcm_free(pcm);
3340}
3341
12a48a8c 3342/**
71a45cda
LPC
3343 * snd_soc_add_platform - Add a platform to the ASoC core
3344 * @dev: The parent device for the platform
3345 * @platform: The platform to add
7d1442b4 3346 * @platform_drv: The driver for the platform
12a48a8c 3347 */
71a45cda 3348int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
d79e57db 3349 const struct snd_soc_platform_driver *platform_drv)
12a48a8c 3350{
b37f1d12
LPC
3351 int ret;
3352
bb13109d
LPC
3353 ret = snd_soc_component_initialize(&platform->component,
3354 &platform_drv->component_driver, dev);
3355 if (ret)
3356 return ret;
12a48a8c 3357
f0fba2ad
LG
3358 platform->dev = dev;
3359 platform->driver = platform_drv;
f1d45cc3
LPC
3360
3361 if (platform_drv->probe)
3362 platform->component.probe = snd_soc_platform_drv_probe;
3363 if (platform_drv->remove)
3364 platform->component.remove = snd_soc_platform_drv_remove;
99b04f4c
KM
3365 if (platform_drv->pcm_new)
3366 platform->component.pcm_new = snd_soc_platform_drv_pcm_new;
3367 if (platform_drv->pcm_free)
3368 platform->component.pcm_free = snd_soc_platform_drv_pcm_free;
12a48a8c 3369
81c7cfd1
LPC
3370#ifdef CONFIG_DEBUG_FS
3371 platform->component.debugfs_prefix = "platform";
3372#endif
12a48a8c
MB
3373
3374 mutex_lock(&client_mutex);
bb13109d 3375 snd_soc_component_add_unlocked(&platform->component);
12a48a8c
MB
3376 list_add(&platform->list, &platform_list);
3377 mutex_unlock(&client_mutex);
3378
f4333203
LPC
3379 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
3380 platform->component.name);
12a48a8c
MB
3381
3382 return 0;
3383}
71a45cda 3384EXPORT_SYMBOL_GPL(snd_soc_add_platform);
12a48a8c
MB
3385
3386/**
71a45cda 3387 * snd_soc_register_platform - Register a platform with the ASoC core
12a48a8c 3388 *
628536ea
JC
3389 * @dev: The device for the platform
3390 * @platform_drv: The driver for the platform
12a48a8c 3391 */
71a45cda
LPC
3392int snd_soc_register_platform(struct device *dev,
3393 const struct snd_soc_platform_driver *platform_drv)
12a48a8c 3394{
f0fba2ad 3395 struct snd_soc_platform *platform;
71a45cda 3396 int ret;
f0fba2ad 3397
71a45cda 3398 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
f0fba2ad 3399
71a45cda
LPC
3400 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3401 if (platform == NULL)
3402 return -ENOMEM;
3403
3404 ret = snd_soc_add_platform(dev, platform, platform_drv);
3405 if (ret)
3406 kfree(platform);
3407
3408 return ret;
3409}
3410EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3411
3412/**
3413 * snd_soc_remove_platform - Remove a platform from the ASoC core
3414 * @platform: the platform to remove
3415 */
3416void snd_soc_remove_platform(struct snd_soc_platform *platform)
3417{
b37f1d12 3418
12a48a8c
MB
3419 mutex_lock(&client_mutex);
3420 list_del(&platform->list);
bb13109d 3421 snd_soc_component_del_unlocked(&platform->component);
12a48a8c
MB
3422 mutex_unlock(&client_mutex);
3423
71a45cda 3424 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
f4333203 3425 platform->component.name);
decc27b0
DM
3426
3427 snd_soc_component_cleanup(&platform->component);
71a45cda
LPC
3428}
3429EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3430
3431struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3432{
3433 struct snd_soc_platform *platform;
3434
34e81ab4 3435 mutex_lock(&client_mutex);
71a45cda 3436 list_for_each_entry(platform, &platform_list, list) {
34e81ab4
LPC
3437 if (dev == platform->dev) {
3438 mutex_unlock(&client_mutex);
71a45cda 3439 return platform;
34e81ab4 3440 }
71a45cda 3441 }
34e81ab4 3442 mutex_unlock(&client_mutex);
71a45cda
LPC
3443
3444 return NULL;
3445}
3446EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3447
3448/**
3449 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3450 *
628536ea 3451 * @dev: platform to unregister
71a45cda
LPC
3452 */
3453void snd_soc_unregister_platform(struct device *dev)
3454{
3455 struct snd_soc_platform *platform;
3456
3457 platform = snd_soc_lookup_platform(dev);
3458 if (!platform)
3459 return;
3460
3461 snd_soc_remove_platform(platform);
f0fba2ad 3462 kfree(platform);
12a48a8c
MB
3463}
3464EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3465
151ab22c
MB
3466static u64 codec_format_map[] = {
3467 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3468 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3469 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3470 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3471 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3472 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3473 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3474 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3475 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3476 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3477 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3478 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3479 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3480 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3481 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3482 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3483};
3484
3485/* Fix up the DAI formats for endianness: codecs don't actually see
3486 * the endianness of the data but we're using the CPU format
3487 * definitions which do need to include endianness so we ensure that
3488 * codec DAIs always have both big and little endian variants set.
3489 */
3490static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3491{
3492 int i;
3493
3494 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3495 if (stream->formats & codec_format_map[i])
3496 stream->formats |= codec_format_map[i];
3497}
3498
f1d45cc3
LPC
3499static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
3500{
3501 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3502
3503 return codec->driver->probe(codec);
3504}
3505
3506static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
3507{
3508 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3509
3510 codec->driver->remove(codec);
3511}
3512
9178feb4
KM
3513static int snd_soc_codec_drv_suspend(struct snd_soc_component *component)
3514{
3515 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3516
3517 return codec->driver->suspend(codec);
3518}
3519
3520static int snd_soc_codec_drv_resume(struct snd_soc_component *component)
3521{
3522 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3523
3524 return codec->driver->resume(codec);
3525}
3526
e2c330b9
LPC
3527static int snd_soc_codec_drv_write(struct snd_soc_component *component,
3528 unsigned int reg, unsigned int val)
3529{
3530 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3531
3532 return codec->driver->write(codec, reg, val);
3533}
3534
3535static int snd_soc_codec_drv_read(struct snd_soc_component *component,
3536 unsigned int reg, unsigned int *val)
3537{
3538 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
3539
3540 *val = codec->driver->read(codec, reg);
3541
3542 return 0;
3543}
3544
68f831c2
LPC
3545static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
3546 enum snd_soc_bias_level level)
3547{
3548 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
3549
3550 return codec->driver->set_bias_level(codec, level);
3551}
3552
0d0cf00a
MB
3553/**
3554 * snd_soc_register_codec - Register a codec with the ASoC core
3555 *
628536ea
JC
3556 * @dev: The parent device for this codec
3557 * @codec_drv: Codec driver
3558 * @dai_drv: The associated DAI driver
3559 * @num_dai: Number of DAIs
0d0cf00a 3560 */
f0fba2ad 3561int snd_soc_register_codec(struct device *dev,
001ae4c0
MB
3562 const struct snd_soc_codec_driver *codec_drv,
3563 struct snd_soc_dai_driver *dai_drv,
3564 int num_dai)
0d0cf00a 3565{
4890140f 3566 struct snd_soc_dapm_context *dapm;
f0fba2ad 3567 struct snd_soc_codec *codec;
bb13109d 3568 struct snd_soc_dai *dai;
f0fba2ad 3569 int ret, i;
151ab22c 3570
f0fba2ad
LG
3571 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3572
3573 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3574 if (codec == NULL)
3575 return -ENOMEM;
0d0cf00a 3576
f1d45cc3 3577 codec->component.codec = codec;
ce0fc93a 3578
bb13109d
LPC
3579 ret = snd_soc_component_initialize(&codec->component,
3580 &codec_drv->component_driver, dev);
3581 if (ret)
3582 goto err_free;
f0fba2ad 3583
f1d45cc3
LPC
3584 if (codec_drv->probe)
3585 codec->component.probe = snd_soc_codec_drv_probe;
3586 if (codec_drv->remove)
3587 codec->component.remove = snd_soc_codec_drv_remove;
9178feb4
KM
3588 if (codec_drv->suspend)
3589 codec->component.suspend = snd_soc_codec_drv_suspend;
3590 if (codec_drv->resume)
3591 codec->component.resume = snd_soc_codec_drv_resume;
e2c330b9
LPC
3592 if (codec_drv->write)
3593 codec->component.write = snd_soc_codec_drv_write;
3594 if (codec_drv->read)
3595 codec->component.read = snd_soc_codec_drv_read;
3d59400f 3596 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4890140f
LPC
3597
3598 dapm = snd_soc_codec_get_dapm(codec);
3599 dapm->idle_bias_off = codec_drv->idle_bias_off;
3600 dapm->suspend_bias_off = codec_drv->suspend_bias_off;
14e8bdeb 3601 if (codec_drv->seq_notifier)
4890140f 3602 dapm->seq_notifier = codec_drv->seq_notifier;
68f831c2 3603 if (codec_drv->set_bias_level)
4890140f 3604 dapm->set_bias_level = snd_soc_codec_set_bias_level;
7a30a3db
DP
3605 codec->dev = dev;
3606 codec->driver = codec_drv;
e2c330b9 3607 codec->component.val_bytes = codec_drv->reg_word_size;
ce6120cc 3608
81c7cfd1
LPC
3609#ifdef CONFIG_DEBUG_FS
3610 codec->component.init_debugfs = soc_init_codec_debugfs;
3611 codec->component.debugfs_prefix = "codec";
3612#endif
3613
886f5692
LPC
3614 if (codec_drv->get_regmap)
3615 codec->component.regmap = codec_drv->get_regmap(dev);
a39f75f7 3616
f0fba2ad
LG
3617 for (i = 0; i < num_dai; i++) {
3618 fixup_codec_formats(&dai_drv[i].playback);
3619 fixup_codec_formats(&dai_drv[i].capture);
3620 }
3621
bb13109d 3622 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
5acd7dfb 3623 if (ret < 0) {
f42cf8d6 3624 dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret);
bb13109d 3625 goto err_cleanup;
26b01ccd 3626 }
f0fba2ad 3627
bb13109d
LPC
3628 list_for_each_entry(dai, &codec->component.dai_list, list)
3629 dai->codec = codec;
f0fba2ad 3630
e1328a83 3631 mutex_lock(&client_mutex);
bb13109d 3632 snd_soc_component_add_unlocked(&codec->component);
054880fe 3633 list_add(&codec->list, &codec_list);
e1328a83
KM
3634 mutex_unlock(&client_mutex);
3635
f4333203
LPC
3636 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
3637 codec->component.name);
0d0cf00a 3638 return 0;
f0fba2ad 3639
bb13109d
LPC
3640err_cleanup:
3641 snd_soc_component_cleanup(&codec->component);
3642err_free:
f0fba2ad
LG
3643 kfree(codec);
3644 return ret;
0d0cf00a
MB
3645}
3646EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3647
3648/**
3649 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3650 *
628536ea 3651 * @dev: codec to unregister
0d0cf00a 3652 */
f0fba2ad 3653void snd_soc_unregister_codec(struct device *dev)
0d0cf00a 3654{
f0fba2ad 3655 struct snd_soc_codec *codec;
f0fba2ad 3656
34e81ab4 3657 mutex_lock(&client_mutex);
f0fba2ad
LG
3658 list_for_each_entry(codec, &codec_list, list) {
3659 if (dev == codec->dev)
3660 goto found;
3661 }
34e81ab4 3662 mutex_unlock(&client_mutex);
f0fba2ad
LG
3663 return;
3664
3665found:
0d0cf00a 3666 list_del(&codec->list);
bb13109d 3667 snd_soc_component_del_unlocked(&codec->component);
0d0cf00a
MB
3668 mutex_unlock(&client_mutex);
3669
f4333203
LPC
3670 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
3671 codec->component.name);
f0fba2ad 3672
bb13109d 3673 snd_soc_component_cleanup(&codec->component);
7a30a3db 3674 snd_soc_cache_exit(codec);
f0fba2ad 3675 kfree(codec);
0d0cf00a
MB
3676}
3677EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3678
bec4fa05 3679/* Retrieve a card's name from device tree */
b07609ce
KM
3680int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3681 const char *propname)
bec4fa05 3682{
b07609ce 3683 struct device_node *np;
bec4fa05
SW
3684 int ret;
3685
7e07e7c0
TB
3686 if (!card->dev) {
3687 pr_err("card->dev is not set before calling %s\n", __func__);
3688 return -EINVAL;
3689 }
3690
b07609ce 3691 np = card->dev->of_node;
7e07e7c0 3692
bec4fa05
SW
3693 ret = of_property_read_string_index(np, propname, 0, &card->name);
3694 /*
3695 * EINVAL means the property does not exist. This is fine providing
3696 * card->name was previously set, which is checked later in
3697 * snd_soc_register_card.
3698 */
3699 if (ret < 0 && ret != -EINVAL) {
3700 dev_err(card->dev,
f110bfc7 3701 "ASoC: Property '%s' could not be read: %d\n",
bec4fa05
SW
3702 propname, ret);
3703 return ret;
3704 }
3705
3706 return 0;
3707}
b07609ce 3708EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
bec4fa05 3709
9a6d4860
XL
3710static const struct snd_soc_dapm_widget simple_widgets[] = {
3711 SND_SOC_DAPM_MIC("Microphone", NULL),
3712 SND_SOC_DAPM_LINE("Line", NULL),
3713 SND_SOC_DAPM_HP("Headphone", NULL),
3714 SND_SOC_DAPM_SPK("Speaker", NULL),
3715};
3716
21efde50 3717int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
9a6d4860
XL
3718 const char *propname)
3719{
21efde50 3720 struct device_node *np = card->dev->of_node;
9a6d4860
XL
3721 struct snd_soc_dapm_widget *widgets;
3722 const char *template, *wname;
3723 int i, j, num_widgets, ret;
3724
3725 num_widgets = of_property_count_strings(np, propname);
3726 if (num_widgets < 0) {
3727 dev_err(card->dev,
3728 "ASoC: Property '%s' does not exist\n", propname);
3729 return -EINVAL;
3730 }
3731 if (num_widgets & 1) {
3732 dev_err(card->dev,
3733 "ASoC: Property '%s' length is not even\n", propname);
3734 return -EINVAL;
3735 }
3736
3737 num_widgets /= 2;
3738 if (!num_widgets) {
3739 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3740 propname);
3741 return -EINVAL;
3742 }
3743
3744 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3745 GFP_KERNEL);
3746 if (!widgets) {
3747 dev_err(card->dev,
3748 "ASoC: Could not allocate memory for widgets\n");
3749 return -ENOMEM;
3750 }
3751
3752 for (i = 0; i < num_widgets; i++) {
3753 ret = of_property_read_string_index(np, propname,
3754 2 * i, &template);
3755 if (ret) {
3756 dev_err(card->dev,
3757 "ASoC: Property '%s' index %d read error:%d\n",
3758 propname, 2 * i, ret);
3759 return -EINVAL;
3760 }
3761
3762 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3763 if (!strncmp(template, simple_widgets[j].name,
3764 strlen(simple_widgets[j].name))) {
3765 widgets[i] = simple_widgets[j];
3766 break;
3767 }
3768 }
3769
3770 if (j >= ARRAY_SIZE(simple_widgets)) {
3771 dev_err(card->dev,
3772 "ASoC: DAPM widget '%s' is not supported\n",
3773 template);
3774 return -EINVAL;
3775 }
3776
3777 ret = of_property_read_string_index(np, propname,
3778 (2 * i) + 1,
3779 &wname);
3780 if (ret) {
3781 dev_err(card->dev,
3782 "ASoC: Property '%s' index %d read error:%d\n",
3783 propname, (2 * i) + 1, ret);
3784 return -EINVAL;
3785 }
3786
3787 widgets[i].name = wname;
3788 }
3789
f23e860e
NC
3790 card->of_dapm_widgets = widgets;
3791 card->num_of_dapm_widgets = num_widgets;
9a6d4860
XL
3792
3793 return 0;
3794}
21efde50 3795EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
9a6d4860 3796
6131084a
JS
3797static int snd_soc_of_get_slot_mask(struct device_node *np,
3798 const char *prop_name,
3799 unsigned int *mask)
3800{
3801 u32 val;
6c84e591 3802 const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
6131084a
JS
3803 int i;
3804
3805 if (!of_slot_mask)
3806 return 0;
3807 val /= sizeof(u32);
3808 for (i = 0; i < val; i++)
3809 if (be32_to_cpup(&of_slot_mask[i]))
3810 *mask |= (1 << i);
3811
3812 return val;
3813}
3814
89c67857 3815int snd_soc_of_parse_tdm_slot(struct device_node *np,
6131084a
JS
3816 unsigned int *tx_mask,
3817 unsigned int *rx_mask,
89c67857
XL
3818 unsigned int *slots,
3819 unsigned int *slot_width)
3820{
3821 u32 val;
3822 int ret;
3823
6131084a
JS
3824 if (tx_mask)
3825 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3826 if (rx_mask)
3827 snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3828
89c67857
XL
3829 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
3830 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3831 if (ret)
3832 return ret;
3833
3834 if (slots)
3835 *slots = val;
3836 }
3837
3838 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
3839 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3840 if (ret)
3841 return ret;
3842
3843 if (slot_width)
3844 *slot_width = val;
3845 }
3846
3847 return 0;
3848}
3849EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3850
440a3006 3851void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card,
5e3cdaa2
KM
3852 struct snd_soc_codec_conf *codec_conf,
3853 struct device_node *of_node,
3854 const char *propname)
3855{
440a3006 3856 struct device_node *np = card->dev->of_node;
5e3cdaa2
KM
3857 const char *str;
3858 int ret;
3859
3860 ret = of_property_read_string(np, propname, &str);
3861 if (ret < 0) {
3862 /* no prefix is not error */
3863 return;
3864 }
3865
3866 codec_conf->of_node = of_node;
3867 codec_conf->name_prefix = str;
3868}
440a3006 3869EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix);
5e3cdaa2 3870
2bc644af 3871int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
a4a54dd5
SW
3872 const char *propname)
3873{
2bc644af 3874 struct device_node *np = card->dev->of_node;
e3b1e6a1 3875 int num_routes;
a4a54dd5
SW
3876 struct snd_soc_dapm_route *routes;
3877 int i, ret;
3878
3879 num_routes = of_property_count_strings(np, propname);
c34ce320 3880 if (num_routes < 0 || num_routes & 1) {
10e8aa9a
MM
3881 dev_err(card->dev,
3882 "ASoC: Property '%s' does not exist or its length is not even\n",
3883 propname);
a4a54dd5
SW
3884 return -EINVAL;
3885 }
3886 num_routes /= 2;
3887 if (!num_routes) {
f110bfc7 3888 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
a4a54dd5
SW
3889 propname);
3890 return -EINVAL;
3891 }
3892
e3b1e6a1 3893 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
a4a54dd5
SW
3894 GFP_KERNEL);
3895 if (!routes) {
3896 dev_err(card->dev,
f110bfc7 3897 "ASoC: Could not allocate DAPM route table\n");
a4a54dd5
SW
3898 return -EINVAL;
3899 }
3900
3901 for (i = 0; i < num_routes; i++) {
3902 ret = of_property_read_string_index(np, propname,
e3b1e6a1 3903 2 * i, &routes[i].sink);
a4a54dd5 3904 if (ret) {
c871bd0b
MB
3905 dev_err(card->dev,
3906 "ASoC: Property '%s' index %d could not be read: %d\n",
3907 propname, 2 * i, ret);
a4a54dd5
SW
3908 return -EINVAL;
3909 }
3910 ret = of_property_read_string_index(np, propname,
e3b1e6a1 3911 (2 * i) + 1, &routes[i].source);
a4a54dd5
SW
3912 if (ret) {
3913 dev_err(card->dev,
c871bd0b
MB
3914 "ASoC: Property '%s' index %d could not be read: %d\n",
3915 propname, (2 * i) + 1, ret);
a4a54dd5
SW
3916 return -EINVAL;
3917 }
3918 }
3919
f23e860e
NC
3920 card->num_of_dapm_routes = num_routes;
3921 card->of_dapm_routes = routes;
a4a54dd5
SW
3922
3923 return 0;
3924}
2bc644af 3925EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
a4a54dd5 3926
a7930ed4 3927unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
389cb834
JS
3928 const char *prefix,
3929 struct device_node **bitclkmaster,
3930 struct device_node **framemaster)
a7930ed4
KM
3931{
3932 int ret, i;
3933 char prop[128];
3934 unsigned int format = 0;
3935 int bit, frame;
3936 const char *str;
3937 struct {
3938 char *name;
3939 unsigned int val;
3940 } of_fmt_table[] = {
3941 { "i2s", SND_SOC_DAIFMT_I2S },
3942 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
3943 { "left_j", SND_SOC_DAIFMT_LEFT_J },
3944 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
3945 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
3946 { "ac97", SND_SOC_DAIFMT_AC97 },
3947 { "pdm", SND_SOC_DAIFMT_PDM},
3948 { "msb", SND_SOC_DAIFMT_MSB },
3949 { "lsb", SND_SOC_DAIFMT_LSB },
a7930ed4
KM
3950 };
3951
3952 if (!prefix)
3953 prefix = "";
3954
3955 /*
3956 * check "[prefix]format = xxx"
3957 * SND_SOC_DAIFMT_FORMAT_MASK area
3958 */
3959 snprintf(prop, sizeof(prop), "%sformat", prefix);
3960 ret = of_property_read_string(np, prop, &str);
3961 if (ret == 0) {
3962 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3963 if (strcmp(str, of_fmt_table[i].name) == 0) {
3964 format |= of_fmt_table[i].val;
3965 break;
3966 }
3967 }
3968 }
3969
3970 /*
8c2d6a9f 3971 * check "[prefix]continuous-clock"
a7930ed4
KM
3972 * SND_SOC_DAIFMT_CLOCK_MASK area
3973 */
8c2d6a9f 3974 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
51930295 3975 if (of_property_read_bool(np, prop))
8c2d6a9f
KM
3976 format |= SND_SOC_DAIFMT_CONT;
3977 else
3978 format |= SND_SOC_DAIFMT_GATED;
a7930ed4
KM
3979
3980 /*
3981 * check "[prefix]bitclock-inversion"
3982 * check "[prefix]frame-inversion"
3983 * SND_SOC_DAIFMT_INV_MASK area
3984 */
3985 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3986 bit = !!of_get_property(np, prop, NULL);
3987
3988 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3989 frame = !!of_get_property(np, prop, NULL);
3990
3991 switch ((bit << 4) + frame) {
3992 case 0x11:
3993 format |= SND_SOC_DAIFMT_IB_IF;
3994 break;
3995 case 0x10:
3996 format |= SND_SOC_DAIFMT_IB_NF;
3997 break;
3998 case 0x01:
3999 format |= SND_SOC_DAIFMT_NB_IF;
4000 break;
4001 default:
4002 /* SND_SOC_DAIFMT_NB_NF is default */
4003 break;
4004 }
4005
4006 /*
4007 * check "[prefix]bitclock-master"
4008 * check "[prefix]frame-master"
4009 * SND_SOC_DAIFMT_MASTER_MASK area
4010 */
4011 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4012 bit = !!of_get_property(np, prop, NULL);
389cb834
JS
4013 if (bit && bitclkmaster)
4014 *bitclkmaster = of_parse_phandle(np, prop, 0);
a7930ed4
KM
4015
4016 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4017 frame = !!of_get_property(np, prop, NULL);
389cb834
JS
4018 if (frame && framemaster)
4019 *framemaster = of_parse_phandle(np, prop, 0);
a7930ed4
KM
4020
4021 switch ((bit << 4) + frame) {
4022 case 0x11:
4023 format |= SND_SOC_DAIFMT_CBM_CFM;
4024 break;
4025 case 0x10:
4026 format |= SND_SOC_DAIFMT_CBM_CFS;
4027 break;
4028 case 0x01:
4029 format |= SND_SOC_DAIFMT_CBS_CFM;
4030 break;
4031 default:
4032 format |= SND_SOC_DAIFMT_CBS_CFS;
4033 break;
4034 }
4035
4036 return format;
4037}
4038EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4039
1ad8ec53 4040int snd_soc_get_dai_name(struct of_phandle_args *args,
93b0f3ee 4041 const char **dai_name)
cb470087
KM
4042{
4043 struct snd_soc_component *pos;
3e0aa8d8 4044 struct device_node *component_of_node;
93b0f3ee 4045 int ret = -EPROBE_DEFER;
cb470087
KM
4046
4047 mutex_lock(&client_mutex);
4048 list_for_each_entry(pos, &component_list, list) {
3e0aa8d8
JS
4049 component_of_node = pos->dev->of_node;
4050 if (!component_of_node && pos->dev->parent)
4051 component_of_node = pos->dev->parent->of_node;
4052
4053 if (component_of_node != args->np)
cb470087
KM
4054 continue;
4055
6833c452 4056 if (pos->driver->of_xlate_dai_name) {
93b0f3ee
JFM
4057 ret = pos->driver->of_xlate_dai_name(pos,
4058 args,
4059 dai_name);
6833c452
KM
4060 } else {
4061 int id = -1;
4062
93b0f3ee 4063 switch (args->args_count) {
6833c452
KM
4064 case 0:
4065 id = 0; /* same as dai_drv[0] */
4066 break;
4067 case 1:
93b0f3ee 4068 id = args->args[0];
6833c452
KM
4069 break;
4070 default:
4071 /* not supported */
4072 break;
4073 }
4074
4075 if (id < 0 || id >= pos->num_dai) {
4076 ret = -EINVAL;
3dcba280 4077 continue;
6833c452 4078 }
e41975ed
XL
4079
4080 ret = 0;
4081
4082 *dai_name = pos->dai_drv[id].name;
4083 if (!*dai_name)
4084 *dai_name = pos->name;
cb470087
KM
4085 }
4086
cb470087
KM
4087 break;
4088 }
4089 mutex_unlock(&client_mutex);
93b0f3ee
JFM
4090 return ret;
4091}
1ad8ec53 4092EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
93b0f3ee
JFM
4093
4094int snd_soc_of_get_dai_name(struct device_node *of_node,
4095 const char **dai_name)
4096{
4097 struct of_phandle_args args;
4098 int ret;
4099
4100 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4101 "#sound-dai-cells", 0, &args);
4102 if (ret)
4103 return ret;
4104
4105 ret = snd_soc_get_dai_name(&args, dai_name);
cb470087
KM
4106
4107 of_node_put(args.np);
4108
4109 return ret;
4110}
4111EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4112
93b0f3ee
JFM
4113/*
4114 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
4115 * @dev: Card device
4116 * @of_node: Device node
4117 * @dai_link: DAI link
4118 *
4119 * Builds an array of CODEC DAI components from the DAI link property
4120 * 'sound-dai'.
4121 * The array is set in the DAI link and the number of DAIs is set accordingly.
4122 * The device nodes in the array (of_node) must be dereferenced by the caller.
4123 *
4124 * Returns 0 for success
4125 */
4126int snd_soc_of_get_dai_link_codecs(struct device *dev,
4127 struct device_node *of_node,
4128 struct snd_soc_dai_link *dai_link)
4129{
4130 struct of_phandle_args args;
4131 struct snd_soc_dai_link_component *component;
4132 char *name;
4133 int index, num_codecs, ret;
4134
4135 /* Count the number of CODECs */
4136 name = "sound-dai";
4137 num_codecs = of_count_phandle_with_args(of_node, name,
4138 "#sound-dai-cells");
4139 if (num_codecs <= 0) {
4140 if (num_codecs == -ENOENT)
4141 dev_err(dev, "No 'sound-dai' property\n");
4142 else
4143 dev_err(dev, "Bad phandle in 'sound-dai'\n");
4144 return num_codecs;
4145 }
4146 component = devm_kzalloc(dev,
4147 sizeof *component * num_codecs,
4148 GFP_KERNEL);
4149 if (!component)
4150 return -ENOMEM;
4151 dai_link->codecs = component;
4152 dai_link->num_codecs = num_codecs;
4153
4154 /* Parse the list */
4155 for (index = 0, component = dai_link->codecs;
4156 index < dai_link->num_codecs;
4157 index++, component++) {
4158 ret = of_parse_phandle_with_args(of_node, name,
4159 "#sound-dai-cells",
4160 index, &args);
4161 if (ret)
4162 goto err;
4163 component->of_node = args.np;
4164 ret = snd_soc_get_dai_name(&args, &component->dai_name);
4165 if (ret < 0)
4166 goto err;
4167 }
4168 return 0;
4169err:
4170 for (index = 0, component = dai_link->codecs;
4171 index < dai_link->num_codecs;
4172 index++, component++) {
4173 if (!component->of_node)
4174 break;
4175 of_node_put(component->of_node);
4176 component->of_node = NULL;
4177 }
4178 dai_link->codecs = NULL;
4179 dai_link->num_codecs = 0;
4180 return ret;
4181}
4182EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
4183
c9b3a40f 4184static int __init snd_soc_init(void)
db2a4165 4185{
6553bf06 4186 snd_soc_debugfs_init();
fb257897
MB
4187 snd_soc_util_init();
4188
db2a4165
FM
4189 return platform_driver_register(&soc_driver);
4190}
4abe8e16 4191module_init(snd_soc_init);
db2a4165 4192
7d8c16a6 4193static void __exit snd_soc_exit(void)
db2a4165 4194{
fb257897 4195 snd_soc_util_exit();
6553bf06 4196 snd_soc_debugfs_exit();
fb257897 4197
3ff3f64b 4198 platform_driver_unregister(&soc_driver);
db2a4165 4199}
db2a4165
FM
4200module_exit(snd_soc_exit);
4201
4202/* Module information */
d331124d 4203MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
db2a4165
FM
4204MODULE_DESCRIPTION("ALSA SoC Core");
4205MODULE_LICENSE("GPL");
8b45a209 4206MODULE_ALIAS("platform:soc-audio");